ConfigUtil { public Config buildConfig(HashMap<QName,ParameterValue> inputParameters) throws SaxonApiException, InvalidSyntaxException { try { Processor processor = new Processor(saxonConfig); if(!skipSchemaValidation) { try { System.setProperty("javax.xml.validation.SchemaFactory:http: SchemaFactory schemaFactory = SchemaFactory.newInstance("http: Source schemaSource = saxonConfig.getURIResolver().resolve("cp:/fr/efl/chaine/xslt/schemas/gaulois-pipe_config.xsd", null); Schema schema = schemaFactory.newSchema(schemaSource); SchemaValidationErrorListener errListener = new SchemaValidationErrorListener(); Validator validator = schema.newValidator(); validator.setErrorHandler(errListener); SAXSource saxSource = new SAXSource( __isConfigUriTrueURI ? new InputSource(new URL(configUri).openStream()) : new InputSource(new FileInputStream(new File(configUri))) ); validator.validate(saxSource); if(errListener.hasErrors()) { throw new InvalidSyntaxException(configUri+" does not respect gaulois-pipe schema"); } } catch(SAXException | TransformerException | IOException ex) { LOGGER.error("while verifying schema conformity",ex); } catch(Error er) { LOGGER.error("java.protocol.handler.pkgs="+System.getProperty("java.protocol.handler.pkgs")); LOGGER.error("while parsing config",er); throw er; } } if(LOGGER.isDebugEnabled() && __isConfigUriTrueURI) { LOGGER.debug("trying to resolve "+configUri); Source source = uriResolver.resolve(configUri, null); if(source==null) { LOGGER.error(configUri+" can not be resolved. Please try again, setting org.xmlresolver logger to DEBUG"); } } XdmNode configRoot = __isConfigUriTrueURI ? processor.newDocumentBuilder().build(uriResolver.resolve(configUri,null)) : processor.newDocumentBuilder().build(new File(configUri)); XPathCompiler xc = processor.newXPathCompiler(); xc.declareNamespace("cfg", Config.NS); XPathSelector xs = xc.compile("/*").load(); xs.setContextItem(configRoot); XdmNode root = (XdmNode)xs.evaluateSingle(); if(CONFIG_EL.equals(root.getNodeName())) { Config config = new Config(root); XdmSequenceIterator it = root.axisIterator(Axis.CHILD, Namespaces.QNAME); Namespaces namespaces = new Namespaces(); while(it.hasNext()) { XdmNode ns = (XdmNode)it.next(); XdmSequenceIterator itm = ns.axisIterator(Axis.CHILD, Namespaces.QN_MAPPING); while(itm.hasNext()) { XdmNode node = (XdmNode)itm.next(); String prefix = node.getAttributeValue(Namespaces.ATTR_PREFIX); String uri = node.getAttributeValue(Namespaces.ATTR_URI); namespaces.getMappings().put(prefix, uri); } } config.setNamespaces(namespaces); it.close(); XPathSelector gs = xc.compile("cfg:grammars/cfg:schema/@href").load(); gs.setContextItem(root); it=gs.evaluate().iterator(); List<String> grammars = new ArrayList<>(); while(it.hasNext()) { String href = it.next().getStringValue(); grammars.add(href); } it.close(); config.setSchemaLocations(grammars); HashMap<QName, ParameterValue> configParameters = new HashMap<>(); it = root.axisIterator(Axis.CHILD, Config.PARAMS_CHILD); while(it.hasNext()) { XdmNode params = (XdmNode)it.next(); XdmSequenceIterator itp = params.axisIterator(Axis.CHILD, new QName(Config.NS, "param")); while(itp.hasNext()) { ParameterValue pv = buildParameter((XdmNode)itp.next(),inputParameters); configParameters.put(pv.getKey(), pv); } } config.getParams().putAll(ParametersMerger.merge(inputParameters, configParameters)); config.setPipe(buildPipe((XdmNode)(root.axisIterator(Axis.CHILD, Pipe.QNAME).next()),config.getParams())); XdmSequenceIterator sourceIterator = root.axisIterator(Axis.CHILD, Sources.QNAME); if(sourceIterator.hasNext()) { config.setSources(buildSources((XdmNode)(sourceIterator.next()), config.getParams())); } XdmSequenceIterator outputIt = root.axisIterator(Axis.CHILD, Output.QNAME); if(outputIt.hasNext()) { LOGGER.warn("Defining <output/> in config is now deprecated - but still works. You should define it in <pipe/>"); if(config.getPipe().getOutput()==null) { config.getPipe().setOutput(buildOutput((XdmNode)(outputIt.next()), config.getParams())); } else { throw new InvalidSyntaxException("Using output outside of pipe is deprecated but supported, only if pipe has no output defined"); } } return config; } else { throw new InvalidSyntaxException("The file "+configUri+" does not respect schema saxon-pipe_config.xsd"); } } catch(TransformerException ex) { throw new InvalidSyntaxException(configUri+" can not be read."); } } ConfigUtil(Configuration saxonConfig, URIResolver uriResolver, String configUri);  ConfigUtil(Configuration saxonConfig, URIResolver uriResolver, String configUri, final boolean skipSchemaValidation, final String currentDir); Config buildConfig(HashMap<QName,ParameterValue> inputParameters); static QName resolveQName(String name); static void addConfigParameter(Config config, String parameterPattern, DatatypeFactory factory); static ParameterValue parseParameterPattern(String parameterPattern, DatatypeFactory factory); static void setNbThreads(Config config, String argument); static void addInputFile(Config config, String argument, DatatypeFactory factory); static void addTemplate(Config config, String argument, DatatypeFactory factory); static void setOutput(Config config, String argument);  }
@Test public void testCpConfigFile() throws InvalidSyntaxException, SaxonApiException { GauloisPipe piper = new GauloisPipe(configFactory); ConfigUtil cu = new ConfigUtil(configFactory.getConfiguration(), piper.getUriResolver(), "cp:/cp-xsl.xml"); assertTrue(cu.buildConfig(emptyInputParams)!=null); }
@Test public void testSourceFolderContent() throws InvalidSyntaxException, SaxonApiException, MalformedURLException { GauloisPipe piper = new GauloisPipe(configFactory); ConfigUtil cu = new ConfigUtil(configFactory.getConfiguration(), piper.getUriResolver(), "src/test/resources/folderContentRelative.xml"); Config config = cu.buildConfig(emptyInputParams); assertTrue("relative path in source folder fails",config.getSources().getFiles().size()>=34); cu = new ConfigUtil(configFactory.getConfiguration(), piper.getUriResolver(), "cp:/folderContentAbsUri.xml"); HashMap<QName, ParameterValue> params = new HashMap<>(); File currentPath = new File(System.getProperty("user.dir")); File test = new File(currentPath, "src/test/resources/folderContentAbsUri.xml"); if(!test.exists()) { currentPath = new File(currentPath, "gaulois-pipe"); test = new File(currentPath, "src/test/resources/folderContentAbsUri.xml"); if(!test.exists()) { fail("Unable to locate gaulois-pipe folder. Try moving to gaulois-pipe module folder."); } } QName sourceQn = new QName("source"); params.put(sourceQn, new ParameterValue(sourceQn, currentPath.toURI().toURL().toExternalForm(), datatypeFactory.XS_STRING)); config = cu.buildConfig(params); assertTrue("absolute URI in source folder fails", config.getSources().getFiles().size()>=34); }
@Test(expected = InvalidSyntaxException.class) public void testUnboundedPrefix() throws Exception { String configFilename = "src/test/resources/config/unmatchedPrefixDatatype.xml"; GauloisPipe piper = new GauloisPipe(configFactory); ConfigUtil cu = new ConfigUtil(configFactory.getConfiguration(), piper.getUriResolver(), configFilename); cu.buildConfig(emptyInputParams); fail("This config file must throw an Exception and it doesn't : "+configFilename); }
@Test(expected = InvalidSyntaxException.class) public void testUnboundedAbstractParam() throws Exception { String configFilename = "src/test/resources/config/abstractParam.xml"; GauloisPipe piper = new GauloisPipe(configFactory); ConfigUtil cu = new ConfigUtil(configFactory.getConfiguration(), piper.getUriResolver(), configFilename); Config config = cu.buildConfig(emptyInputParams); config.verify(); }
@Test() public void testBoundedAbstractParam() throws Exception { String configFilename = "src/test/resources/config/abstractParam.xml"; GauloisPipe piper = new GauloisPipe(configFactory); ConfigUtil cu = new ConfigUtil(configFactory.getConfiguration(), piper.getUriResolver(), configFilename); HashMap<QName,ParameterValue> cliParameters = new HashMap<>(); ParameterValue pv = new ParameterValue(new QName("int"), "8", datatypeFactory.XS_STRING); cliParameters.put(pv.getKey(), pv); Config config = cu.buildConfig(cliParameters); config.verify(); }
@Test(expected = InvalidSyntaxException.class) public void testBoundedAbstractParamWrongValue() throws Exception { String configFilename = "src/test/resources/config/abstractParam.xml"; GauloisPipe piper = new GauloisPipe(configFactory); ConfigUtil cu = new ConfigUtil(configFactory.getConfiguration(), piper.getUriResolver(), configFilename); HashMap<QName,ParameterValue> cliParameters = new HashMap<>(); ParameterValue pv = new ParameterValue(new QName("int"), "foe", datatypeFactory.XS_STRING); cliParameters.put(pv.getKey(), pv); Config config = cu.buildConfig(cliParameters); config.verify(); }