Search in sources :

Example 1 with XsltTransformer

use of net.sf.saxon.s9api.XsltTransformer 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 XsltTransformer

use of net.sf.saxon.s9api.XsltTransformer 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 3 with XsltTransformer

use of net.sf.saxon.s9api.XsltTransformer in project ph-schematron by phax.

the class AbstractSchematronXSLTBasedResource method applySchematronValidation.

@Nullable
public final Document applySchematronValidation(@Nonnull final Node aXMLNode, @Nullable final String sBaseURI) throws TransformerException {
    ValueEnforcer.notNull(aXMLNode, "XMLNode");
    final ISchematronXSLTBasedProvider aXSLTProvider = getXSLTProvider();
    if (aXSLTProvider == null || !aXSLTProvider.isValidSchematron()) {
        // We cannot progress because of invalid Schematron
        return null;
    }
    // Debug print the created XSLT document
    if (SchematronDebug.isShowCreatedXSLT())
        s_aLogger.info("Created XSLT document: " + XMLWriter.getNodeAsString(aXSLTProvider.getXSLTDocument()));
    // Create result document
    final Document ret = XMLFactory.newDocument();
    // Create the transformer object from the templates specified in the
    // constructor
    final Transformer aTransformer = aXSLTProvider.getXSLTTransformer();
    // Ensure an error listener is present
    if (m_aCustomErrorListener != null)
        aTransformer.setErrorListener(m_aCustomErrorListener);
    else
        aTransformer.setErrorListener(new LoggingTransformErrorListener(Locale.US));
    // Set the optional URI Resolver
    if (m_aCustomURIResolver != null)
        aTransformer.setURIResolver(m_aCustomURIResolver);
    // Set all custom parameters
    if (m_aCustomParameters != null)
        for (final Map.Entry<String, ?> aEntry : m_aCustomParameters.entrySet()) aTransformer.setParameter(aEntry.getKey(), aEntry.getValue());
    if (s_aLogger.isDebugEnabled())
        s_aLogger.debug("Applying Schematron XSLT on XML [start]");
    // Enable this for hardcore Saxon debugging only
    if (false)
        if (aTransformer.getClass().getName().equals("net.sf.saxon.jaxp.TransformerImpl")) {
            final XsltTransformer aXT = ((TransformerImpl) aTransformer).getUnderlyingXsltTransformer();
            aXT.setMessageListener((a, b, c) -> s_aLogger.info("MessageListener: " + a + ", " + b + ", " + c));
            aXT.setTraceFunctionDestination(new StandardLogger(System.err));
            if (false)
                aXT.getUnderlyingController().setTraceListener(new XSLTTraceListener());
            if (false) {
                final XSLTTraceListener aTL = new XSLTTraceListener();
                aTL.setOutputDestination(new StandardLogger(System.err));
                aXT.getUnderlyingController().setTraceListener(TraceEventMulticaster.add(aTL, null));
            }
            if (false)
                System.out.println("mode=" + aXT.getInitialMode());
            if (false)
                System.out.println("temp=" + aXT.getInitialTemplate());
            if (false)
                System.out.println(aTransformer.getOutputProperties());
        }
    // Do the main transformation
    aTransformer.transform(new DOMSource(aXMLNode), new DOMResult(ret));
    if (s_aLogger.isDebugEnabled())
        s_aLogger.debug("Applying Schematron XSLT on XML [end]");
    // Debug print the created SVRL document
    if (SchematronDebug.isShowCreatedSVRL())
        s_aLogger.info("Created SVRL:\n" + XMLWriter.getNodeAsString(ret));
    return ret;
}
Also used : Transformer(javax.xml.transform.Transformer) DOMSource(javax.xml.transform.dom.DOMSource) DefaultTransformURIResolver(com.helger.xml.transform.DefaultTransformURIResolver) IGenericImplTrait(com.helger.commons.traits.IGenericImplTrait) TransformerException(javax.xml.transform.TransformerException) URIResolver(javax.xml.transform.URIResolver) LoggerFactory(org.slf4j.LoggerFactory) EValidity(com.helger.commons.state.EValidity) ToStringGenerator(com.helger.commons.string.ToStringGenerator) XsltTransformer(net.sf.saxon.s9api.XsltTransformer) ErrorListener(javax.xml.transform.ErrorListener) AbstractSchematronResource(com.helger.schematron.AbstractSchematronResource) XMLFactory(com.helger.xml.XMLFactory) Locale(java.util.Locale) Document(org.w3c.dom.Document) Map(java.util.Map) Node(org.w3c.dom.Node) ReturnsMutableCopy(com.helger.commons.annotation.ReturnsMutableCopy) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) EntityResolver(org.xml.sax.EntityResolver) StandardLogger(net.sf.saxon.lib.StandardLogger) Logger(org.slf4j.Logger) TransformerImpl(net.sf.saxon.jaxp.TransformerImpl) XSLTTraceListener(net.sf.saxon.trace.XSLTTraceListener) ISchematronXSLTValidator(com.helger.schematron.xslt.validator.ISchematronXSLTValidator) SVRLMarshaller(com.helger.schematron.svrl.SVRLMarshaller) IReadableResource(com.helger.commons.io.resource.IReadableResource) SchematronXSLTValidatorDefault(com.helger.schematron.xslt.validator.SchematronXSLTValidatorDefault) ICommonsOrderedMap(com.helger.commons.collection.impl.ICommonsOrderedMap) XMLWriter(com.helger.xml.serialize.write.XMLWriter) SchematronOutputType(org.oclc.purl.dsdl.svrl.SchematronOutputType) ValueEnforcer(com.helger.commons.ValueEnforcer) SchematronDebug(com.helger.schematron.SchematronDebug) LoggingTransformErrorListener(com.helger.xml.transform.LoggingTransformErrorListener) CommonsLinkedHashMap(com.helger.commons.collection.impl.CommonsLinkedHashMap) TraceEventMulticaster(net.sf.saxon.trace.TraceEventMulticaster) ReturnsMutableObject(com.helger.commons.annotation.ReturnsMutableObject) DOMResult(javax.xml.transform.dom.DOMResult) NotThreadSafe(javax.annotation.concurrent.NotThreadSafe) StandardLogger(net.sf.saxon.lib.StandardLogger) DOMSource(javax.xml.transform.dom.DOMSource) Transformer(javax.xml.transform.Transformer) XsltTransformer(net.sf.saxon.s9api.XsltTransformer) DOMResult(javax.xml.transform.dom.DOMResult) XsltTransformer(net.sf.saxon.s9api.XsltTransformer) LoggingTransformErrorListener(com.helger.xml.transform.LoggingTransformErrorListener) Document(org.w3c.dom.Document) XSLTTraceListener(net.sf.saxon.trace.XSLTTraceListener) Nullable(javax.annotation.Nullable)

Example 4 with XsltTransformer

use of net.sf.saxon.s9api.XsltTransformer 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)

Example 5 with XsltTransformer

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

the class XMLReduce method reduce.

@Override
public void reduce(final DateWritable paramKey, final Iterable<Text> paramValue, final Context paramContext) throws IOException, InterruptedException {
    final StringBuilder builder = new StringBuilder("<root>");
    for (final Text event : paramValue) {
        System.out.println(event.toString());
        builder.append(event.toString());
    }
    builder.append("</root>");
    // System.out.println(builder.toString());
    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 StringReader(builder.toString())));
        final ByteArrayOutputStream out = new ByteArrayOutputStream();
        final Serializer serializer = new Serializer();
        serializer.setOutputStream(out);
        transform.setDestination(serializer);
        transform.transform();
        final String value = out.toString();
        // System.out.println(value);
        paramContext.write(null, new Text(value));
    } catch (final SaxonApiException e) {
        LOGWRAPPER.error(e);
    }
}
Also used : Processor(net.sf.saxon.s9api.Processor) StreamSource(javax.xml.transform.stream.StreamSource) Text(org.apache.hadoop.io.Text) XsltCompiler(net.sf.saxon.s9api.XsltCompiler) ByteArrayOutputStream(java.io.ByteArrayOutputStream) SaxonApiException(net.sf.saxon.s9api.SaxonApiException) XsltExecutable(net.sf.saxon.s9api.XsltExecutable) XsltTransformer(net.sf.saxon.s9api.XsltTransformer) StringReader(java.io.StringReader) File(java.io.File) Serializer(net.sf.saxon.s9api.Serializer)

Aggregations

XsltTransformer (net.sf.saxon.s9api.XsltTransformer)5 StreamSource (javax.xml.transform.stream.StreamSource)4 Processor (net.sf.saxon.s9api.Processor)4 Serializer (net.sf.saxon.s9api.Serializer)4 XsltCompiler (net.sf.saxon.s9api.XsltCompiler)4 XsltExecutable (net.sf.saxon.s9api.XsltExecutable)4 File (java.io.File)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 SaxonApiException (net.sf.saxon.s9api.SaxonApiException)2 ValueEnforcer (com.helger.commons.ValueEnforcer)1 ReturnsMutableCopy (com.helger.commons.annotation.ReturnsMutableCopy)1 ReturnsMutableObject (com.helger.commons.annotation.ReturnsMutableObject)1 CommonsLinkedHashMap (com.helger.commons.collection.impl.CommonsLinkedHashMap)1 ICommonsOrderedMap (com.helger.commons.collection.impl.ICommonsOrderedMap)1 IReadableResource (com.helger.commons.io.resource.IReadableResource)1 EValidity (com.helger.commons.state.EValidity)1 ToStringGenerator (com.helger.commons.string.ToStringGenerator)1 IGenericImplTrait (com.helger.commons.traits.IGenericImplTrait)1 AbstractSchematronResource (com.helger.schematron.AbstractSchematronResource)1 SchematronDebug (com.helger.schematron.SchematronDebug)1