Search in sources :

Example 1 with LoggingTransformErrorListener

use of com.helger.xml.transform.LoggingTransformErrorListener 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 2 with LoggingTransformErrorListener

use of com.helger.xml.transform.LoggingTransformErrorListener in project ph-schematron by phax.

the class SchematronResourceXSLTCache method createSchematronXSLTProvider.

@Nullable
public static SchematronProviderXSLTPrebuild createSchematronXSLTProvider(@Nonnull final IReadableResource aXSLTResource, @Nullable final ErrorListener aCustomErrorListener, @Nullable final URIResolver aCustomURIResolver) {
    if (s_aLogger.isInfoEnabled())
        s_aLogger.info("Compiling XSLT instance " + aXSLTResource.toString());
    final CollectingTransformErrorListener aCEH = new CollectingTransformErrorListener();
    final SchematronProviderXSLTPrebuild aXSLTPreprocessor = new SchematronProviderXSLTPrebuild(aXSLTResource, aCEH.andThen(aCustomErrorListener != null ? aCustomErrorListener : new LoggingTransformErrorListener(Locale.US)), aCustomURIResolver);
    if (!aXSLTPreprocessor.isValidSchematron()) {
        // Schematron is invalid -> parsing failed
        s_aLogger.warn("The XSLT resource '" + aXSLTResource.getResourceID() + "' is invalid!");
        for (final IError aError : aCEH.getErrorList()) s_aLogger.warn("  " + aError.getAsString(Locale.US));
        return null;
    }
    // If it is a valid schematron, there must be a result XSLT present!
    if (aXSLTPreprocessor.getXSLTDocument() == null) {
        // leave the check anyway.
        throw new IllegalStateException("No XSLT document retrieved from XSLT resource '" + aXSLTResource.getResourceID() + "'!");
    }
    // Create the main validator for the schematron
    return aXSLTPreprocessor;
}
Also used : LoggingTransformErrorListener(com.helger.xml.transform.LoggingTransformErrorListener) CollectingTransformErrorListener(com.helger.xml.transform.CollectingTransformErrorListener) IError(com.helger.commons.error.IError) Nullable(javax.annotation.Nullable)

Aggregations

LoggingTransformErrorListener (com.helger.xml.transform.LoggingTransformErrorListener)2 Nullable (javax.annotation.Nullable)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 IError (com.helger.commons.error.IError)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 SVRLMarshaller (com.helger.schematron.svrl.SVRLMarshaller)1 ISchematronXSLTValidator (com.helger.schematron.xslt.validator.ISchematronXSLTValidator)1 SchematronXSLTValidatorDefault (com.helger.schematron.xslt.validator.SchematronXSLTValidatorDefault)1 XMLFactory (com.helger.xml.XMLFactory)1 XMLWriter (com.helger.xml.serialize.write.XMLWriter)1 CollectingTransformErrorListener (com.helger.xml.transform.CollectingTransformErrorListener)1