net.sf.saxon.s9api.Processor Java Examples

The following examples show how to use net.sf.saxon.s9api.Processor. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ConfigurationLoader.java    From validator with Apache License 2.0 6 votes vote down vote up
public Configuration build() {
    final ResolvingConfigurationStrategy resolving = getResolvingConfigurationStrategy();
    final Processor processor = resolving.getProcessor();
    final ContentRepository contentRepository = new ContentRepository(resolving, getScenarioRepository());

    final Scenarios def = loadScenarios(contentRepository.getScenarioSchema(), processor);
    final List<Scenario> scenarios = initializeScenarios(def, contentRepository);
    final Scenario fallbackScenario = createFallback(def, contentRepository);
    final DefaultConfiguration configuration = new DefaultConfiguration(scenarios, fallbackScenario);
    configuration.setAdditionalParameters(this.parameters);
    configuration.setAuthor(def.getAuthor());
    configuration.setDate(def.getDate().toString());
    configuration.setName(def.getName());
    configuration.setContentRepository(contentRepository);
    configuration.getAdditionalParameters().put(Keys.SCENARIOS_FILE, this.scenarioDefinition);
    configuration.getAdditionalParameters().put(Keys.SCENARIO_DEFINITION, def);
    return (configuration);
}
 
Example #2
Source File: DefaultCheck.java    From validator with Apache License 2.0 6 votes vote down vote up
/**
 * Erzeugt eine neue Instanz mit der angegebenen Konfiguration.
 *
 * @param configuration die Konfiguration
 */
public DefaultCheck(final Configuration configuration) {
    this.configuration = configuration;
    final ContentRepository content = configuration.getContentRepository();
    final Processor processor = content.getProcessor();
    this.conversionService = new ConversionService();

    this.checkSteps = new ArrayList<>();
    this.checkSteps.add(new DocumentParseAction(processor));
    this.checkSteps.add(new CreateDocumentIdentificationAction());
    this.checkSteps.add(new ScenarioSelectionAction(new ScenarioRepository(configuration)));
    this.checkSteps.add(new SchemaValidationAction(content.getResolvingConfigurationStrategy(), processor));
    this.checkSteps.add(new SchematronValidationAction(content.getResolver(), this.conversionService));
    this.checkSteps.add(new ValidateReportInputAction(this.conversionService, content.getReportInputSchema()));
    this.checkSteps.add(new CreateReportAction(processor, this.conversionService, content.getResolver()));
    this.checkSteps.add(new ComputeAcceptanceAction());
}
 
Example #3
Source File: XPathProcessor.java    From GeoTriples with Apache License 2.0 6 votes vote down vote up
public String execute(Node node, String expression)
		throws SaxonApiException {

	Processor proc = new Processor(false);
	XPathCompiler xpath = proc.newXPathCompiler();
	DocumentBuilder builder = proc.newDocumentBuilder();

	String fileName = getClass().getResource(
			map.getLogicalSource().getIdentifier()).getFile();

	XdmNode doc = builder.build(new File(fileName));
	String expre = replace(node, expression);
	expression = expression.replaceAll(
			"\\{" + expression.split("\\{")[1].split("\\}")[0] + "\\}", "'"
					+ expre + "'");

	XPathSelector selector = xpath.compile(expression).load();
	selector.setContextItem(doc);

	// Evaluate the expression.
	Object result = selector.evaluate();

	return result.toString();

}
 
Example #4
Source File: StrictRelativeResolvingStrategy.java    From validator with Apache License 2.0 6 votes vote down vote up
@Override
protected Processor createProcessor() {
    final Processor processor = new Processor(false);
    // verhindere global im Prinzip alle resolving strategien
    final SecureUriResolver resolver = new SecureUriResolver();
    processor.getUnderlyingConfiguration().setCollectionFinder(resolver);
    processor.getUnderlyingConfiguration().setOutputURIResolver(resolver);
    processor.getUnderlyingConfiguration().setUnparsedTextURIResolver(resolver);

    // grundsätzlich Feature-konfiguration:
    processor.setConfigurationProperty(Feature.DTD_VALIDATION, false);
    processor.setConfigurationProperty(Feature.ENTITY_RESOLVER_CLASS, "");
    processor.setConfigurationProperty(Feature.XINCLUDE, false);
    processor.setConfigurationProperty(Feature.ALLOW_EXTERNAL_FUNCTIONS, false);

    // Konfiguration des zu verwendenden Parsers, wenn Saxon selbst einen erzeugen muss, bspw. beim XSL parsen
    processor.setConfigurationProperty(FeatureKeys.XML_PARSER_FEATURE + encode(FEATURE_SECURE_PROCESSING), true);
    processor.setConfigurationProperty(FeatureKeys.XML_PARSER_FEATURE + encode(DISSALLOW_DOCTYPE_DECL_FEATURE), true);
    processor.setConfigurationProperty(FeatureKeys.XML_PARSER_FEATURE + encode(LOAD_EXTERNAL_DTD_FEATURE), false);
    processor.setConfigurationProperty(FeatureKeys.XML_PARSER_FEATURE + encode(XMLConstants.ACCESS_EXTERNAL_DTD), false);
    return processor;
}
 
Example #5
Source File: SaxonSecurityTest.java    From validator with Apache License 2.0 5 votes vote down vote up
@Test
public void testEvilStylesheets() throws IOException {
    final Processor p = TestObjectFactory.createProcessor();
    for (int i = 1; i <= 5; i++) {
        try {
            final URL resource = SaxonSecurityTest.class.getResource(String.format("/evil/evil%s.xsl", i));
            final XsltCompiler compiler = p.newXsltCompiler();
            final RelativeUriResolver resolver = new RelativeUriResolver(Simple.REPOSITORY_URI);
            compiler.setURIResolver(resolver);
            final XsltExecutable executable = compiler.compile(new StreamSource(resource.openStream()));
            final XsltTransformer transformer = executable.load();
            final Source document = InputFactory.read("<root/>".getBytes(), "dummy").getSource();
            // transformer.getUnderlyingController().setUnparsedTextURIResolver(resolver);
            transformer.setURIResolver(resolver);
            transformer.setSource(document);
            final XdmDestination result = new XdmDestination();
            transformer.setDestination(result);
            transformer.transform();

            // wenn der Punkt erreicht wird, sollte wenigstens, das Element evil nicht mit 'bösen' Inhalten gefüllt sein!
            if (StringUtils.isNotBlank(result.getXdmNode().getStringValue())) {
                fail(String.format("Saxon configuration should prevent expansion within %s", resource));
            }

        } catch (final SaxonApiException | RuntimeException e) {
            log.info("Expected exception detected {}", e.getMessage(), e);
        }
    }
}
 
Example #6
Source File: SaxonCommand.java    From kite with Apache License 2.0 5 votes vote down vote up
public SaxonCommand(CommandBuilder builder, Config config, Command parent, Command child, MorphlineContext context) {
    super(builder, config, parent, child, context);
    
    this.isTracing = getConfigs().getBoolean(config, "isTracing", false);
    boolean isLicensedSaxonEdition = getConfigs().getBoolean(config, "isLicensedSaxonEdition", false);
    this.processor = new Processor(isLicensedSaxonEdition);
    this.documentBuilder = processor.newDocumentBuilder();
    
    Config features = getConfigs().getConfig(config, "features", ConfigFactory.empty());
    for (Map.Entry<String, Object> entry : new Configs().getEntrySet(features)) {
      processor.setConfigurationProperty(entry.getKey(), entry.getValue());
    }
    
    for (String clazz : getConfigs().getStringList(config, "extensionFunctions", Collections.<String>emptyList())) {
      Object function;
      try {
        function = Class.forName(clazz).newInstance();
      } catch (Exception e) {
        throw new MorphlineCompilationException("Cannot instantiate extension function: " + clazz, config);
      }
      
      if (function instanceof ExtensionFunction) {
        processor.registerExtensionFunction((ExtensionFunction) function);              
//      }
//      else if (function instanceof ExtensionFunctionDefinition) {
//        processor.registerExtensionFunction((ExtensionFunctionDefinition) function);              
      } else {
        throw new MorphlineCompilationException("Extension function has wrong class: " + clazz, config);
      }
    }
  }
 
Example #7
Source File: Engine.java    From teamengine with Apache License 2.0 5 votes vote down vote up
public Engine() throws Exception {
    String s = System.getProperty("te.cacheSize");
    if (s != null) {
        cacheSize = Integer.parseInt(s);
    }

    // Create processor
    processor = new Processor(false);

    // Modify its configuration settings
    Configuration config = processor.getUnderlyingConfiguration();
    config.setVersionWarning(false);

    // Use our custom error listener which reports line numbers in the CTL
    // source file
    errorListener = new TeErrorListener();
    config.setErrorListener(errorListener);

    // Create a compiler and document builder
    compiler = processor.newXsltCompiler();
    builder = processor.newDocumentBuilder();

    // Load an executable for the TECore.form method
    ClassLoader cl = Thread.currentThread().getContextClassLoader();
    InputStream is = cl.getResourceAsStream("com/occamlab/te/formfn.xsl");
    formExecutable = compiler.compile(new StreamSource(is));
    // Fortify Mod: We are done with the InputStream.  Release the resource
    is.close();
}
 
Example #8
Source File: SaxonDebuggerApp.java    From intellij-xquery with Apache License 2.0 5 votes vote down vote up
@Override
protected void doAdditionalConfiguration(Processor processor) {
    Configuration configuration = processor.getUnderlyingConfiguration();
    configuration.setBooleanProperty(FeatureKeys.EAGER_EVALUATION, true);
    StaticQueryContext context = configuration.getDefaultStaticQueryContext();
    configuration.setTraceListener(new SaxonTraceListener(this));
    context.setCodeInjector(new SaxonExtendedTraceCodeInjector());
    Optimizer optimizer = configuration.obtainOptimizer();
    optimizer.setOptimizationLevel(Optimizer.NO_OPTIMIZATION);
}
 
Example #9
Source File: Helper.java    From validator with Apache License 2.0 5 votes vote down vote up
public static String serialize(final XdmNode node) {
    try ( final StringWriter writer = new StringWriter() ) {
        final Processor processor = Helper.getTestProcessor();
        final Serializer serializer = processor.newSerializer(writer);
        serializer.serializeNode(node);
        return writer.toString();
    } catch (final SaxonApiException | IOException e) {
        throw new IllegalStateException("Can not serialize document", e);
    }
}
 
Example #10
Source File: CreateReportActionTest.java    From validator with Apache License 2.0 5 votes vote down vote up
@Test
public void testExecutionException() throws SaxonApiException {
    final Processor p = mock(Processor.class);
    final DocumentBuilder documentBuilder = mock(DocumentBuilder.class);
    this.action = new CreateReportAction(p, new ConversionService(), null);

    when(p.newDocumentBuilder()).thenReturn(documentBuilder);
    when(documentBuilder.build(any(Source.class))).thenThrow(new SaxonApiException("mocked"));
    final Bag bag = TestBagBuilder.createBag(InputFactory.read(Simple.SIMPLE_VALID), true);
    this.action.check(bag);
    assertThat(bag.isStopped()).isTrue();

}
 
Example #11
Source File: BaseResolvingStrategy.java    From validator with Apache License 2.0 5 votes vote down vote up
@Override
public Processor getProcessor() {
    if (this.processor == null) {
        this.processor = createProcessor();
    }
    return this.processor;
}
 
Example #12
Source File: ConfigurationLoader.java    From validator with Apache License 2.0 5 votes vote down vote up
private Scenarios loadScenarios(final Schema scenarioSchema, final Processor processor) {
    final ConversionService conversionService = new ConversionService();
    checkVersion(this.scenarioDefinition, processor);
    log.info("Loading scenarios from {}", this.scenarioDefinition);
    final CollectingErrorEventHandler handler = new CollectingErrorEventHandler();
    final Scenarios scenarios = conversionService.readXml(this.scenarioDefinition, Scenarios.class, scenarioSchema, handler);
    if (!handler.hasErrors()) {
        log.info("Loading scenario content from {}", this.getScenarioRepository());
    } else {
        throw new IllegalStateException(
                String.format("Can not load scenarios from %s due to %s", getScenarioDefinition(), handler.getErrorDescription()));
    }
    return scenarios;

}
 
Example #13
Source File: ConfigurationLoader.java    From validator with Apache License 2.0 5 votes vote down vote up
private static void checkVersion(final URI scenarioDefinition, final Processor processor) {
    try {
        final Result<XdmNode, XMLSyntaxError> result = new DocumentParseAction(processor)
                .parseDocument(InputFactory.read(scenarioDefinition.toURL()));
        if (result.isValid() && !isSupportedDocument(result.getObject())) {
            throw new IllegalStateException(String.format(
                    "Specified scenario configuration %s is not supported.%nThis version only supports definitions of '%s'",
                    scenarioDefinition, SUPPORTED_MAJOR_VERSION_SCHEMA));

        }
    } catch (final MalformedURLException e) {
        throw new IllegalStateException("Error reading definition file");
    }
}
 
Example #14
Source File: CommandLineApplication.java    From validator with Apache License 2.0 4 votes vote down vote up
private static int processActions(final CommandLine cmd) {
    try {

        long start = System.currentTimeMillis();
        final Option[] unavailable = new Option[] { HOST, PORT, WORKER_COUNT };
        warnUnusedOptions(cmd, unavailable, false);
        final Configuration config = Configuration.load(determineDefinition(cmd), determineRepository(cmd)).build();

        final InternalCheck check = new InternalCheck(config);
        final Path outputDirectory = determineOutputDirectory(cmd);

        final Processor processor = config.getContentRepository().getProcessor();
        if (cmd.hasOption(EXTRACT_HTML.getOpt())) {
            check.getCheckSteps().add(new ExtractHtmlContentAction(processor, outputDirectory));
        }
        check.getCheckSteps().add(new SerializeReportAction(outputDirectory, processor));
        if (cmd.hasOption(SERIALIZE_REPORT_INPUT.getOpt())) {
            check.getCheckSteps().add(new SerializeReportInputAction(outputDirectory, check.getConversionService()));
        }
        if (cmd.hasOption(PRINT.getOpt())) {
            check.getCheckSteps().add(new PrintReportAction(processor));
        }

        if (cmd.hasOption(CHECK_ASSERTIONS.getOpt())) {
            final Assertions assertions = loadAssertions(cmd.getOptionValue(CHECK_ASSERTIONS.getOpt()));
            check.getCheckSteps().add(new CheckAssertionAction(assertions, processor));
        }
        if (cmd.hasOption(PRINT_MEM_STATS.getOpt())) {
            check.getCheckSteps().add(new PrintMemoryStats());
        }

        log.info("Setup completed in {}ms\n", System.currentTimeMillis() - start);

        final Collection<Path> targets = determineTestTargets(cmd);
        start = System.currentTimeMillis();
        for (final Path p : targets) {
            final Input input = InputFactory.read(p);
            check.checkInput(input);
        }
        final boolean result = check.printAndEvaluate();
        log.info("Processing {} object(s) completed in {}ms", targets.size(), System.currentTimeMillis() - start);
        return result ? 0 : 1;

    } catch (final Exception e) {
        e.printStackTrace();
        if (cmd.hasOption(DEBUG.getOpt())) {
            log.error(e.getMessage(), e);
        } else {
            log.error(e.getMessage());
        }
        return -1;
    }
}
 
Example #15
Source File: ExtractHtmlContentAction.java    From validator with Apache License 2.0 4 votes vote down vote up
public ExtractHtmlContentAction(final Processor p, final Path outputDirectory) {
    this.outputDirectory = outputDirectory;
    this.htmlExtraction = new HtmlExtractor(p);
    this.processor = p;
}
 
Example #16
Source File: TestObjectFactory.java    From validator with Apache License 2.0 4 votes vote down vote up
public static Processor createProcessor() {
    if (processor == null) {
        processor = new StrictLocalResolvingStrategy().getProcessor();
    }
    return processor;
}
 
Example #17
Source File: Helper.java    From validator with Apache License 2.0 4 votes vote down vote up
public static Result<XdmNode, XMLSyntaxError> parseDocument(final Processor processor, final Input input) {
    return new DocumentParseAction(processor).parseDocument(input);
}
 
Example #18
Source File: Helper.java    From validator with Apache License 2.0 4 votes vote down vote up
public static Processor getTestProcessor() {
    // is always the same at the moment
    return createProcessor();
}
 
Example #19
Source File: Helper.java    From validator with Apache License 2.0 4 votes vote down vote up
public static Processor createProcessor() {
    return ResolvingMode.STRICT_RELATIVE.getStrategy().getProcessor();
}
 
Example #20
Source File: XPathProcessor.java    From spark-xml-utils with Apache License 2.0 4 votes vote down vote up
/**
 * Initialization to improve performance for repetitive invocations of filter and evaluate expressions
 * 
 * @throws XPathException
 */
private void init() throws XPathException {
	
	try {
		
		// Get the processor
		proc = new Processor(false);

		// Set any specified configuration properties for the processor
		if (featureMappings != null) {
			for (Entry<String, Object> entry : featureMappings.entrySet()) {
				proc.setConfigurationProperty(entry.getKey(), entry.getValue());
			}
		}
		
		//proc.setConfigurationProperty(FeatureKeys.ENTITY_RESOLVER_CLASS, "com.elsevier.spark_xml_utils.common.IgnoreDoctype");
		
		// Get the XPath compiler
		XPathCompiler xpathCompiler = proc.newXPathCompiler();

		// Set the namespace to prefix mappings
		this.setPrefixNamespaceMappings(xpathCompiler, namespaceMappings);

		// Compile the XPath expression  and get a document builder
		xsel = xpathCompiler.compile(xPathExpression).load();
		builder = proc.newDocumentBuilder();
	
		// Create and initialize the serializer  
		baos = new ByteArrayOutputStream();
		serializer = proc.newSerializer(baos);
		serializer.setOutputStream(baos);
		serializer.setOutputProperty(Serializer.Property.METHOD, "xml");
		serializer.setOutputProperty(Serializer.Property.OMIT_XML_DECLARATION,"yes");			
		serializer.setProcessor(proc);
		
	} catch (SaxonApiException e) {
		
		log.error("Problems creating an XPathProcessor.  " + e.getMessage(),e);
		throw new XPathException(e.getMessage());

	}
	
}
 
Example #21
Source File: XQueryProcessor.java    From spark-xml-utils with Apache License 2.0 4 votes vote down vote up
/**
 * Initialization to improve performance for repetitive invocations of evaluate expressions
 * 
 * @throws XQueryException
 */
private void init() throws XQueryException {
	
	try {
		
		// Get the processor
		proc = new Processor(false);

		// Set any specified configuration properties for the processor
		if (featureMappings != null) {
			for (Entry<String, Object> entry : featureMappings.entrySet()) {
				proc.setConfigurationProperty(entry.getKey(), entry.getValue());
			}
		}
		
		// Get the XQuery compiler
		XQueryCompiler xqueryCompiler = proc.newXQueryCompiler();
		xqueryCompiler.setEncoding(CharEncoding.UTF_8);

		// Set the namespace to prefix mappings
		this.setPrefixNamespaceMappings(xqueryCompiler, namespaceMappings);

		// Compile the XQuery expression and get an XQuery evaluator
		exp = xqueryCompiler.compile(xQueryExpression);
		eval = exp.load();
		
		// Create and initialize the serializer 
		baos = new ByteArrayOutputStream();
		serializer = proc.newSerializer(baos);
		// Appears ok to always set output property to xml (even if we are just returning a text string)
		serializer.setOutputProperty(Serializer.Property.METHOD, "xml");
		serializer.setOutputProperty(Serializer.Property.OMIT_XML_DECLARATION,"yes");
		serializer.setProcessor(proc);
		
	} catch (SaxonApiException e) {
		
		log.error("Problems creating an XQueryProcessor.  " + e.getMessage(),e);
		throw new XQueryException(e.getMessage());

	}
	
}
 
Example #22
Source File: SchemaValidationAction.java    From validator with Apache License 2.0 4 votes vote down vote up
FileSerializedDocument(final Processor processor) throws IOException {
    this.file = Files.createTempFile("validator", ".xml");
    this.processor = processor;
}
 
Example #23
Source File: Engine.java    From teamengine with Apache License 2.0 4 votes vote down vote up
public Processor getProcessor() {
    return processor;
}
 
Example #24
Source File: ResolvingConfigurationStrategy.java    From validator with Apache License 2.0 2 votes vote down vote up
/**
 * Returns a preconfigured {@link Processor Saxon Processor} for various tasks within the Validator. The validator
 * leverages the saxon s9api for internal processing e.g. xml reading and writing. So this is the main object to secure
 * for reading, transforming and writing xml files.
 * 
 * Note: you need exactly one instance for all validator related processing.
 * 
 * @return a preconfigured {@link Processor}
 */
Processor getProcessor();
 
Example #25
Source File: BaseResolvingStrategy.java    From validator with Apache License 2.0 votes vote down vote up
protected abstract Processor createProcessor(); 
Example #26
Source File: SaxonRunnerApp.java    From intellij-xquery with Apache License 2.0 votes vote down vote up
protected void doAdditionalConfiguration(Processor debugger) { }