Search in sources :

Example 1 with Serializer

use of net.sf.saxon.s9api.Serializer in project sirix by sirixdb.

the class TestNodeWrapperS9ApiXSLT method saxonTransform.

/**
 * Transform source document with the given stylesheet.
 *
 * @param xml
 *          Source xml file.
 * @param stylesheet
 *          Stylesheet to transform sourc xml file.
 * @throws SaxonApiException
 *           Exception from Saxon in case anything goes wrong.
 */
@Ignore("Not a test, utility method only")
public void saxonTransform(final File xml, final File stylesheet) throws SaxonApiException {
    final Processor proc = new Processor(false);
    final XsltCompiler comp = proc.newXsltCompiler();
    final XsltExecutable exp = comp.compile(new StreamSource(stylesheet));
    final XdmNode source = proc.newDocumentBuilder().build(new StreamSource(xml));
    final Serializer out = new Serializer();
    out.setOutputProperty(Serializer.Property.METHOD, "xml");
    out.setOutputProperty(Serializer.Property.INDENT, "yes");
    out.setOutputFile(new File(TestHelper.PATHS.PATH1.getFile(), "books1.html"));
    final XsltTransformer trans = exp.load();
    trans.setInitialContextNode(source);
    trans.setDestination(out);
    trans.transform();
}
Also used : Processor(net.sf.saxon.s9api.Processor) StreamSource(javax.xml.transform.stream.StreamSource) XsltTransformer(net.sf.saxon.s9api.XsltTransformer) XsltCompiler(net.sf.saxon.s9api.XsltCompiler) XdmNode(net.sf.saxon.s9api.XdmNode) File(java.io.File) XsltExecutable(net.sf.saxon.s9api.XsltExecutable) Serializer(net.sf.saxon.s9api.Serializer) Ignore(org.junit.Ignore)

Example 2 with Serializer

use of net.sf.saxon.s9api.Serializer in project sirix by sirixdb.

the class TestNodeWrapperS9ApiXSLT method testWithSerializer.

@Test
public void testWithSerializer() throws Exception {
    final Serializer serializer = new Serializer();
    serializer.setOutputProperty(Serializer.Property.METHOD, "xml");
    serializer.setOutputProperty(Serializer.Property.INDENT, "yes");
    final OutputStream out = new XSLTEvaluator(mHolder.getSession(), STYLESHEET, new ByteArrayOutputStream(), serializer).call();
    final StringBuilder sBuilder = readFile();
    final Diff diff = new Diff(sBuilder.toString(), out.toString());
    diff.overrideElementQualifier(new RecursiveElementNameAndTextQualifier());
    assertTrue(diff.toString(), diff.similar());
}
Also used : Diff(org.custommonkey.xmlunit.Diff) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) RecursiveElementNameAndTextQualifier(org.custommonkey.xmlunit.examples.RecursiveElementNameAndTextQualifier) XSLTEvaluator(org.sirix.saxon.evaluator.XSLTEvaluator) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Serializer(net.sf.saxon.s9api.Serializer) Test(org.junit.Test)

Example 3 with Serializer

use of net.sf.saxon.s9api.Serializer in project sirix by sirixdb.

the class XQueryEvaluatorOutputStream method call.

@Override
public Void call() throws Exception {
    try {
        final Processor proc = new Processor(false);
        final Configuration config = proc.getUnderlyingConfiguration();
        final NodeInfo doc = new DocumentWrapper(mSession, config);
        final XQueryCompiler comp = proc.newXQueryCompiler();
        final XQueryExecutable exp = comp.compile(mExpression);
        if (mSerializer == null) {
            final Serializer out = new Serializer();
            out.setOutputProperty(Serializer.Property.METHOD, "xml");
            out.setOutputProperty(Serializer.Property.OMIT_XML_DECLARATION, "yes");
            out.setOutputStream(mOut);
            mSerializer = out;
        }
        final net.sf.saxon.s9api.XQueryEvaluator exe = exp.load();
        exe.setSource(doc);
        exe.run(mSerializer);
        return null;
    } catch (final SaxonApiException e) {
        LOGGER.error("Saxon Exception: " + e.getMessage(), e);
        throw e;
    }
}
Also used : XQueryExecutable(net.sf.saxon.s9api.XQueryExecutable) DocumentWrapper(org.sirix.saxon.wrapper.DocumentWrapper) Processor(net.sf.saxon.s9api.Processor) Configuration(net.sf.saxon.Configuration) NodeInfo(net.sf.saxon.om.NodeInfo) XQueryCompiler(net.sf.saxon.s9api.XQueryCompiler) SaxonApiException(net.sf.saxon.s9api.SaxonApiException) Serializer(net.sf.saxon.s9api.Serializer)

Example 4 with Serializer

use of net.sf.saxon.s9api.Serializer in project sirix by sirixdb.

the class TestXSLTTransformation method testTransform.

@Test
public void testTransform() throws Exception {
    final Processor proc = new Processor(false);
    final XsltCompiler compiler = proc.newXsltCompiler();
    try {
        final XsltExecutable exec = compiler.compile(new StreamSource(new File(STYLESHEET)));
        final XsltTransformer transform = exec.load();
        transform.setSource(new StreamSource(new FileInputStream(INPUT)));
        final Serializer serializer = new Serializer();
        final OutputStream out = new ByteArrayOutputStream();
        serializer.setOutputStream(out);
        transform.setDestination(serializer);
        transform.transform();
        final StringBuilder expected = TestHelper.readFile(new File(EXPECTED), false);
        assertEquals("XML files match", expected.toString(), new StringBuilder("<root>").append(out.toString()).append("</root>").toString());
    } catch (final SaxonApiException e) {
        LOGWRAPPER.error(e);
    }
}
Also used : Processor(net.sf.saxon.s9api.Processor) StreamSource(javax.xml.transform.stream.StreamSource) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) XsltTransformer(net.sf.saxon.s9api.XsltTransformer) XsltCompiler(net.sf.saxon.s9api.XsltCompiler) ByteArrayOutputStream(java.io.ByteArrayOutputStream) File(java.io.File) SaxonApiException(net.sf.saxon.s9api.SaxonApiException) FileInputStream(java.io.FileInputStream) XsltExecutable(net.sf.saxon.s9api.XsltExecutable) Serializer(net.sf.saxon.s9api.Serializer) Test(org.junit.Test)

Example 5 with Serializer

use of net.sf.saxon.s9api.Serializer in project sirix by sirixdb.

the class XSLTEvaluator method call.

@Override
public OutputStream call() {
    final Processor proc = new Processor(false);
    final XsltCompiler comp = proc.newXsltCompiler();
    XsltExecutable exp;
    XdmNode source;
    try {
        final Configuration config = proc.getUnderlyingConfiguration();
        final NodeInfo doc = new DocumentWrapper(mSession, config);
        exp = comp.compile(new StreamSource(mStylesheet));
        source = proc.newDocumentBuilder().build(doc);
        if (mSerializer == null) {
            final Serializer out = new Serializer();
            out.setOutputProperty(Serializer.Property.METHOD, "xml");
            out.setOutputProperty(Serializer.Property.INDENT, "yes");
            out.setOutputStream(mOut);
            mSerializer = out;
        } else {
            mSerializer.setOutputStream(mOut);
        }
        final XsltTransformer trans = exp.load();
        trans.setInitialContextNode(source);
        trans.setDestination(mSerializer);
        trans.transform();
    } catch (final SaxonApiException e) {
        LOGGER.error("Saxon exception: " + e.getMessage(), e);
    } catch (final SirixException e) {
        LOGGER.error("TT exception: " + e.getMessage(), e);
    }
    return mOut;
}
Also used : DocumentWrapper(org.sirix.saxon.wrapper.DocumentWrapper) Processor(net.sf.saxon.s9api.Processor) Configuration(net.sf.saxon.Configuration) NodeInfo(net.sf.saxon.om.NodeInfo) StreamSource(javax.xml.transform.stream.StreamSource) XsltTransformer(net.sf.saxon.s9api.XsltTransformer) SirixException(org.sirix.exception.SirixException) XsltCompiler(net.sf.saxon.s9api.XsltCompiler) XdmNode(net.sf.saxon.s9api.XdmNode) SaxonApiException(net.sf.saxon.s9api.SaxonApiException) XsltExecutable(net.sf.saxon.s9api.XsltExecutable) Serializer(net.sf.saxon.s9api.Serializer)

Aggregations

Serializer (net.sf.saxon.s9api.Serializer)6 Processor (net.sf.saxon.s9api.Processor)5 StreamSource (javax.xml.transform.stream.StreamSource)4 SaxonApiException (net.sf.saxon.s9api.SaxonApiException)4 XsltCompiler (net.sf.saxon.s9api.XsltCompiler)4 XsltExecutable (net.sf.saxon.s9api.XsltExecutable)4 XsltTransformer (net.sf.saxon.s9api.XsltTransformer)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 File (java.io.File)3 OutputStream (java.io.OutputStream)2 Configuration (net.sf.saxon.Configuration)2 NodeInfo (net.sf.saxon.om.NodeInfo)2 XdmNode (net.sf.saxon.s9api.XdmNode)2 Test (org.junit.Test)2 DocumentWrapper (org.sirix.saxon.wrapper.DocumentWrapper)2 FileInputStream (java.io.FileInputStream)1 StringReader (java.io.StringReader)1 XQueryCompiler (net.sf.saxon.s9api.XQueryCompiler)1 XQueryExecutable (net.sf.saxon.s9api.XQueryExecutable)1 Text (org.apache.hadoop.io.Text)1