Search in sources :

Example 1 with ISAXReaderSettings

use of com.helger.xml.serialize.read.ISAXReaderSettings in project ph-commons by phax.

the class MicroReader method readMicroXML.

/**
 * Read the passed input source as MicroXML.
 *
 * @param aInputSource
 *        The input source to use. May be <code>null</code> in which case
 *        <code>null</code> is directly returned.
 * @param aSettings
 *        The settings to use. If <code>null</code> the default settings will
 *        be used.
 * @return <code>null</code> if either the input source is <code>null</code>
 *         or if the input was invalid XML.
 */
@Nullable
public static IMicroDocument readMicroXML(@WillClose @Nullable final InputSource aInputSource, @Nullable final ISAXReaderSettings aSettings) {
    if (aInputSource == null)
        return null;
    final EntityResolver aEntityResolver = aSettings == null ? null : aSettings.getEntityResolver();
    final MicroSAXHandler aMicroHandler = new MicroSAXHandler(false, aEntityResolver, true);
    // Copy and modify settings
    final SAXReaderSettings aRealSettings = SAXReaderSettings.createCloneOnDemand(aSettings);
    aRealSettings.setEntityResolver(aMicroHandler).setDTDHandler(aMicroHandler).setContentHandler(aMicroHandler).setLexicalHandler(aMicroHandler);
    if (aRealSettings.getErrorHandler() == null) {
        // Use MicroHandler as default error handler if none is specified
        aRealSettings.setErrorHandler(aMicroHandler);
    }
    if (aEntityResolver instanceof EntityResolver2) {
        // Ensure to use the new aEntityResolver2 APIs if available
        aRealSettings.setFeatureValue(EXMLParserFeature.USE_ENTITY_RESOLVER2, true);
    }
    if (SAXReader.readXMLSAX(aInputSource, aRealSettings).isFailure())
        return null;
    return aMicroHandler.getDocument();
}
Also used : ISAXReaderSettings(com.helger.xml.serialize.read.ISAXReaderSettings) SAXReaderSettings(com.helger.xml.serialize.read.SAXReaderSettings) EntityResolver(org.xml.sax.EntityResolver) EntityResolver2(org.xml.sax.ext.EntityResolver2) Nullable(javax.annotation.Nullable)

Example 2 with ISAXReaderSettings

use of com.helger.xml.serialize.read.ISAXReaderSettings in project ph-commons by phax.

the class MicroReaderTest method testReadEntity.

@Test
public void testReadEntity() {
    // Read file with notation
    final IMicroDocument doc = MicroReader.readMicroXML(new ClassPathResource("xml/xml-entity-public.xml"), new SAXReaderSettings().setEntityResolver(new EmptyEntityResolver()));
    assertNotNull(doc);
    final MicroSAXHandler aHdl = new MicroSAXHandler(true, new EmptyEntityResolver(), true);
    final ISAXReaderSettings aSettings = new SAXReaderSettings().setEntityResolver(aHdl).setDTDHandler(aHdl).setContentHandler(aHdl).setErrorHandler(aHdl).setLexicalHandler(aHdl);
    assertTrue(SAXReader.readXMLSAX(InputSourceFactory.create(ClassPathResource.getInputStream("xml/xml-entity-public.xml")), aSettings).isSuccess());
    assertNotNull(aHdl.getDocument());
    // Write again
    assertNotNull(MicroWriter.getNodeAsString(doc));
}
Also used : ISAXReaderSettings(com.helger.xml.serialize.read.ISAXReaderSettings) ISAXReaderSettings(com.helger.xml.serialize.read.ISAXReaderSettings) SAXReaderSettings(com.helger.xml.serialize.read.SAXReaderSettings) IMicroDocument(com.helger.xml.microdom.IMicroDocument) EmptyEntityResolver(com.helger.xml.sax.EmptyEntityResolver) ClassPathResource(com.helger.commons.io.resource.ClassPathResource) Test(org.junit.Test)

Example 3 with ISAXReaderSettings

use of com.helger.xml.serialize.read.ISAXReaderSettings in project ph-schematron by phax.

the class SchematronHelper method _recursiveResolveAllSchematronIncludes.

@SuppressFBWarnings("RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE")
@Nonnull
private static ESuccess _recursiveResolveAllSchematronIncludes(@Nonnull final IMicroElement eRoot, @Nonnull final IReadableResource aResource, @Nullable final ISAXReaderSettings aSettings, @Nonnull final ISchematronErrorHandler aErrorHandler, final boolean bLenient) {
    if (eRoot != null) {
        final DefaultSchematronIncludeResolver aIncludeResolver = new DefaultSchematronIncludeResolver(aResource);
        for (final IMicroElement aElement : eRoot.getAllChildElementsRecursive()) if (isValidSchematronNS(aElement.getNamespaceURI(), bLenient) && aElement.getLocalName().equals(CSchematronXML.ELEMENT_INCLUDE)) {
            String sHref = aElement.getAttributeValue(CSchematronXML.ATTR_HREF);
            try {
                final int nHashIndex = sHref.indexOf('#');
                String sAnchor = null;
                if (nHashIndex >= 0) {
                    sAnchor = sHref.substring(nHashIndex + 1);
                    sHref = sHref.substring(0, nHashIndex);
                }
                final IReadableResource aIncludeRes = aIncludeResolver.getResolvedSchematronResource(sHref);
                if (aIncludeRes == null) {
                    aErrorHandler.handleError(SingleError.builderError().errorLocation(new SimpleLocation(aResource.getPath())).errorText("Failed to resolve include '" + sHref + "'").build());
                    return ESuccess.FAILURE;
                }
                if (LOGGER.isDebugEnabled())
                    LOGGER.debug("Resolved '" + sHref + "' relative to '" + aIncludeResolver.getBaseHref() + "' as '" + aIncludeRes.getPath() + "'");
                // Read XML to be included
                final IMicroDocument aIncludedDoc = MicroReader.readMicroXML(aIncludeRes, aSettings);
                if (aIncludedDoc == null) {
                    aErrorHandler.handleError(SingleError.builderError().errorLocation(new SimpleLocation(aResource.getPath())).errorText("Failed to parse include " + aIncludeRes).build());
                    return ESuccess.FAILURE;
                }
                IMicroElement aIncludedContent;
                if (sAnchor == null) {
                    // no anchor present - include the whole document
                    // Return the document element
                    aIncludedContent = aIncludedDoc.getDocumentElement();
                } else {
                    final String sFinalAnchor = sAnchor;
                    final Wrapper<IMicroElement> aMatch = new Wrapper<>();
                    // Also include the root element in the search
                    ChildrenProviderHierarchyVisitor.visitFrom(aIncludedDoc.getDocumentElement(), new DefaultHierarchyVisitorCallback<IMicroNode>() {

                        @Override
                        public EHierarchyVisitorReturn onItemBeforeChildren(final IMicroNode aItem) {
                            if (aItem.isElement()) {
                                final IMicroElement aCurElement = (IMicroElement) aItem;
                                final String sID = aCurElement.getAttributeValue("id");
                                if (sFinalAnchor.equals(sID))
                                    aMatch.set(aCurElement);
                            }
                            return EHierarchyVisitorReturn.CONTINUE;
                        }
                    }, true);
                    aIncludedContent = aMatch.get();
                    if (aIncludedContent == null) {
                        aErrorHandler.handleError(SingleError.builderWarn().errorLocation(new SimpleLocation(aResource.getPath())).errorText("Failed to resolve an element with the ID '" + sAnchor + "' in " + aIncludeRes + "! Therefore including the whole document!").build());
                        aIncludedContent = aIncludedDoc.getDocumentElement();
                    }
                }
                // Important to detach from parent!
                aIncludedContent.detachFromParent();
                // It is okay to include sthg else
                if (false) {
                    // Check for correct namespace URI of included content
                    if (!isValidSchematronNS(aIncludedContent.getNamespaceURI(), bLenient)) {
                        aErrorHandler.handleError(SingleError.builderError().errorLocation(new SimpleLocation(aResource.getPath())).errorText("The included resource " + aIncludeRes + " contains the wrong XML namespace URI '" + aIncludedContent.getNamespaceURI() + "' but was expected to have: " + StringHelper.getImplodedMapped(", ", getAllValidSchematronNS(bLenient), x -> "'" + x + "'")).build());
                        return ESuccess.FAILURE;
                    }
                }
                // Check that not a whole Schema but only a part is included
                if (isValidSchematronNS(aIncludedContent.getNamespaceURI(), bLenient) && CSchematronXML.ELEMENT_SCHEMA.equals(aIncludedContent.getLocalName())) {
                    aErrorHandler.handleError(SingleError.builderWarn().errorLocation(new SimpleLocation(aResource.getPath())).errorText("The included resource " + aIncludeRes + " seems to be a complete schema. To includes parts of a schema the respective element must be the root element of the included resource.").build());
                }
                // Recursive resolve includes
                if (_recursiveResolveAllSchematronIncludes(aIncludedContent, aIncludeRes, aSettings, aErrorHandler, bLenient).isFailure())
                    return ESuccess.FAILURE;
                // Now replace "include" element with content in MicroDOM
                aElement.getParent().replaceChild(aElement, aIncludedContent);
            } catch (final IOException ex) {
                aErrorHandler.handleError(SingleError.builderError().errorLocation(new SimpleLocation(aResource.getPath())).errorText("Failed to read include '" + sHref + "'").linkedException(ex).build());
                return ESuccess.FAILURE;
            }
        }
    }
    return ESuccess.SUCCESS;
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) ESuccess(com.helger.commons.state.ESuccess) ErrorList(com.helger.commons.error.list.ErrorList) LoggerFactory(org.slf4j.LoggerFactory) IMicroDocument(com.helger.xml.microdom.IMicroDocument) Source(javax.xml.transform.Source) ISAXReaderSettings(com.helger.xml.serialize.read.ISAXReaderSettings) SVRLHelper(com.helger.schematron.svrl.SVRLHelper) Nonempty(com.helger.commons.annotation.Nonempty) ChildrenProviderHierarchyVisitor(com.helger.commons.hierarchy.visit.ChildrenProviderHierarchyVisitor) IMicroElement(com.helger.xml.microdom.IMicroElement) Node(org.w3c.dom.Node) ReturnsMutableCopy(com.helger.commons.annotation.ReturnsMutableCopy) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) InputSource(org.xml.sax.InputSource) EHierarchyVisitorReturn(com.helger.commons.hierarchy.visit.EHierarchyVisitorReturn) DefaultSchematronIncludeResolver(com.helger.schematron.resolve.DefaultSchematronIncludeResolver) Logger(org.slf4j.Logger) CommonsArrayList(com.helger.commons.collection.impl.CommonsArrayList) StringHelper(com.helger.commons.string.StringHelper) IReadableResource(com.helger.commons.io.resource.IReadableResource) InputSourceFactory(com.helger.xml.sax.InputSourceFactory) IOException(java.io.IOException) SVRLResourceError(com.helger.schematron.svrl.SVRLResourceError) DefaultHierarchyVisitorCallback(com.helger.commons.hierarchy.visit.DefaultHierarchyVisitorCallback) ValueEnforcer(com.helger.commons.ValueEnforcer) IMicroNode(com.helger.xml.microdom.IMicroNode) SingleError(com.helger.commons.error.SingleError) ICommonsList(com.helger.commons.collection.impl.ICommonsList) Wrapper(com.helger.commons.wrapper.Wrapper) IHasInputStream(com.helger.commons.io.IHasInputStream) SchematronOutputType(com.helger.schematron.svrl.jaxb.SchematronOutputType) MicroReader(com.helger.xml.microdom.serialize.MicroReader) Immutable(javax.annotation.concurrent.Immutable) IErrorList(com.helger.commons.error.list.IErrorList) SimpleLocation(com.helger.commons.location.SimpleLocation) SVRLFailedAssert(com.helger.schematron.svrl.SVRLFailedAssert) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings) PresentForCodeCoverage(com.helger.commons.annotation.PresentForCodeCoverage) Wrapper(com.helger.commons.wrapper.Wrapper) IMicroElement(com.helger.xml.microdom.IMicroElement) IReadableResource(com.helger.commons.io.resource.IReadableResource) IMicroNode(com.helger.xml.microdom.IMicroNode) SimpleLocation(com.helger.commons.location.SimpleLocation) IMicroDocument(com.helger.xml.microdom.IMicroDocument) IOException(java.io.IOException) DefaultHierarchyVisitorCallback(com.helger.commons.hierarchy.visit.DefaultHierarchyVisitorCallback) DefaultSchematronIncludeResolver(com.helger.schematron.resolve.DefaultSchematronIncludeResolver) Nonnull(javax.annotation.Nonnull) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Aggregations

ISAXReaderSettings (com.helger.xml.serialize.read.ISAXReaderSettings)3 IMicroDocument (com.helger.xml.microdom.IMicroDocument)2 SAXReaderSettings (com.helger.xml.serialize.read.SAXReaderSettings)2 Nullable (javax.annotation.Nullable)2 ValueEnforcer (com.helger.commons.ValueEnforcer)1 Nonempty (com.helger.commons.annotation.Nonempty)1 PresentForCodeCoverage (com.helger.commons.annotation.PresentForCodeCoverage)1 ReturnsMutableCopy (com.helger.commons.annotation.ReturnsMutableCopy)1 CommonsArrayList (com.helger.commons.collection.impl.CommonsArrayList)1 ICommonsList (com.helger.commons.collection.impl.ICommonsList)1 SingleError (com.helger.commons.error.SingleError)1 ErrorList (com.helger.commons.error.list.ErrorList)1 IErrorList (com.helger.commons.error.list.IErrorList)1 ChildrenProviderHierarchyVisitor (com.helger.commons.hierarchy.visit.ChildrenProviderHierarchyVisitor)1 DefaultHierarchyVisitorCallback (com.helger.commons.hierarchy.visit.DefaultHierarchyVisitorCallback)1 EHierarchyVisitorReturn (com.helger.commons.hierarchy.visit.EHierarchyVisitorReturn)1 IHasInputStream (com.helger.commons.io.IHasInputStream)1 ClassPathResource (com.helger.commons.io.resource.ClassPathResource)1 IReadableResource (com.helger.commons.io.resource.IReadableResource)1 SimpleLocation (com.helger.commons.location.SimpleLocation)1