net.sf.saxon.s9api.Serializer Java Examples

The following examples show how to use net.sf.saxon.s9api.Serializer. 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: HtmlExtractor.java    From validator with Apache License 2.0 6 votes vote down vote up
private String convertToString(final XdmNode element) {
    try {
        final StringWriter writer = new StringWriter();
        final Serializer serializer = this.processor.newSerializer(writer);
        serializer.serializeNode(element);
        return writer.toString();
    } catch (final SaxonApiException e) {
        throw new IllegalStateException("Can not convert to string", e);
    }
}
 
Example #2
Source File: CheckHandler.java    From validator with Apache License 2.0 5 votes vote down vote up
private byte[] serialize(final Result result) {
    try ( final ByteArrayOutputStream out = new ByteArrayOutputStream() ) {
        final Serializer serializer = this.processor.newSerializer(out);
        serializer.serializeNode(result.getReport());
        return out.toByteArray();
    } catch (final SaxonApiException | IOException e) {
        log.error("Error serializing result", e);
        throw new IllegalStateException("Can not serialize result", e);
    }
}
 
Example #3
Source File: SchemaValidationAction.java    From validator with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(final XdmNode node) throws SaxonApiException, IOException {
    try ( final ByteArrayOutputStream out = new ByteArrayOutputStream() ) {
        final Serializer serializer = this.processor.newSerializer();
        serializer.setOutputStream(out);
        serializer.serializeNode(node);
        serializer.close();
        this.bytes = out.toByteArray();
    }
}
 
Example #4
Source File: SchemaValidationAction.java    From validator with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(final XdmNode node) throws SaxonApiException, IOException {
    try ( final OutputStream out = Files.newOutputStream(this.file) ) {
        final Serializer serializer = this.processor.newSerializer();
        serializer.setOutputStream(out);
        serializer.serializeNode(node);
        serializer.close();
    }
}
 
Example #5
Source File: ExtractHtmlContentAction.java    From validator with Apache License 2.0 5 votes vote down vote up
private void print(final String origName, final XdmItem xdmItem) {
    final XdmNode node = (XdmNode) xdmItem;
    final String name = origName + "-" + node.getAttributeValue(NAME_ATTRIBUTE);
    final Path file = this.outputDirectory.resolve(name + ".html");
    final Serializer serializer = this.processor.newSerializer(file.toFile());
    try {
        log.info("Writing report html '{}' to {}", name, file.toAbsolutePath());
        serializer.serializeNode(node);
    } catch (final SaxonApiException e) {
        log.error("Error extracting html content to {}", file.toAbsolutePath(), e);
    }
}
 
Example #6
Source File: SerializeReportAction.java    From validator with Apache License 2.0 5 votes vote down vote up
@Override
public void check(Bag results) {
    final Path file = outputDirectory.resolve(results.getName() + "-report.xml");
    try {
        log.info("Serializing result to {}", file.toAbsolutePath());
        final Serializer serializer = processor.newSerializer(file.toFile());
        serializer.serializeNode(results.getReport());
    } catch (SaxonApiException e) {
        log.error("Can not serialize result report to {}", file.toAbsolutePath(), e);
    }
}
 
Example #7
Source File: PrintReportAction.java    From validator with Apache License 2.0 5 votes vote down vote up
@Override
public void check(Bag results) {
    try {
        final StringWriter writer = new StringWriter();
        final Serializer serializer = processor.newSerializer(writer);
        serializer.serializeNode(results.getReport());
        System.out.print(writer.toString());
    } catch (SaxonApiException e) {
        log.error("Error while printing result to stdout", e);
    }
}
 
Example #8
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 #9
Source File: SaxonRunnerApp.java    From intellij-xquery with Apache License 2.0 5 votes vote down vote up
@Override
public void runApp() throws Exception {
    doAdditionalConfiguration(processor);
    Serializer out = prepareSerializer();
    XQueryCompiler compiler = processor.newXQueryCompiler();
    XQueryExecutable executable = compiler.compile(new File(config.getMainFile()));
    setMainModule(executable.getUnderlyingCompiledQuery().getMainModule());
    XQueryEvaluator evaluator = executable.load();
    bindContextItem(evaluator);
    bindVariables(evaluator);
    evaluator.run(out);
}
 
Example #10
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 #11
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 #12
Source File: SaxonRunnerApp.java    From intellij-xquery with Apache License 2.0 4 votes vote down vote up
private Serializer prepareSerializer() {
    Serializer out = processor.newSerializer(output);
    out.setOutputProperty(Serializer.Property.METHOD, "xml");
    out.setOutputProperty(Serializer.Property.OMIT_XML_DECLARATION, "yes");
    return out;
}
 
Example #13
Source File: XSLTProcessor.java    From spark-xml-utils with Apache License 2.0 2 votes vote down vote up
/**
 * Set the output method (the default is xml).
 * @param method
 */
public void setOutputMethod(String method)  {
	serializer.setOutputProperty(Serializer.Property.METHOD, method);
}
 
Example #14
Source File: XPathProcessor.java    From spark-xml-utils with Apache License 2.0 2 votes vote down vote up
/**
 * Set the output method (the default is xml).
 * @param method
 */
public void setOutputMethod(String method)  {
	serializer.setOutputProperty(Serializer.Property.METHOD, method);
}
 
Example #15
Source File: XQueryProcessor.java    From spark-xml-utils with Apache License 2.0 2 votes vote down vote up
/**
 * Set the output method (the default is xml).
 * @param method
 */
public void setOutputMethod(String method)  {
	serializer.setOutputProperty(Serializer.Property.METHOD, method);
}