Search in sources :

Example 11 with IMicroDocument

use of com.helger.xml.microdom.IMicroDocument 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 IPSErrorHandler aErrorHandler) {
    if (eRoot != null) {
        final DefaultSchematronIncludeResolver aIncludeResolver = new DefaultSchematronIncludeResolver(aResource);
        for (final IMicroElement aElement : eRoot.getAllChildElementsRecursive()) if (CSchematron.NAMESPACE_SCHEMATRON.equals(aElement.getNamespaceURI()) && 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.error(aResource, null, "Failed to resolve include '" + sHref + "'", null);
                    return ESuccess.FAILURE;
                }
                if (s_aLogger.isDebugEnabled())
                    s_aLogger.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.error(aResource, null, "Failed to parse include " + aIncludeRes, null);
                    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.warn(aResource, null, "Failed to resolve an element with the ID '" + sAnchor + "' in " + aIncludeRes + "! Therefore including the whole document!");
                        aIncludedContent = aIncludedDoc.getDocumentElement();
                    }
                }
                // Important to detach from parent!
                aIncludedContent.detachFromParent();
                // Check for correct namespace URI of included content
                if (!CSchematron.NAMESPACE_SCHEMATRON.equals(aIncludedContent.getNamespaceURI())) {
                    aErrorHandler.error(aResource, null, "The included resource " + aIncludeRes + " contains the wrong XML namespace URI '" + aIncludedContent.getNamespaceURI() + "' but was expected to have '" + CSchematron.NAMESPACE_SCHEMATRON + "'", null);
                    return ESuccess.FAILURE;
                }
                // Check that not a whole Schema but only a part is included
                if (CSchematronXML.ELEMENT_SCHEMA.equals(aIncludedContent.getLocalName())) {
                    aErrorHandler.warn(aResource, null, "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.");
                }
                // Recursive resolve includes
                if (_recursiveResolveAllSchematronIncludes(aIncludedContent, aIncludeRes, aSettings, aErrorHandler).isFailure())
                    return ESuccess.FAILURE;
                // Now replace "include" element with content in MicroDOM
                aElement.getParent().replaceChild(aElement, aIncludedContent);
            } catch (final IOException ex) {
                aErrorHandler.error(aResource, null, "Failed to read include '" + sHref + "'", ex);
                return ESuccess.FAILURE;
            }
        }
    }
    return ESuccess.SUCCESS;
}
Also used : 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) 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)

Example 12 with IMicroDocument

use of com.helger.xml.microdom.IMicroDocument in project ph-schematron by phax.

the class PSWriter method getAsDocument.

@Nonnull
@OverrideOnDemand
protected IMicroNode getAsDocument(@Nonnull final IMicroElement aElement) {
    final IMicroDocument aDoc = new MicroDocument();
    aDoc.appendChild(aElement);
    return aDoc;
}
Also used : IMicroDocument(com.helger.xml.microdom.IMicroDocument) MicroDocument(com.helger.xml.microdom.MicroDocument) IMicroDocument(com.helger.xml.microdom.IMicroDocument) Nonnull(javax.annotation.Nonnull) OverrideOnDemand(com.helger.commons.annotation.OverrideOnDemand)

Example 13 with IMicroDocument

use of com.helger.xml.microdom.IMicroDocument in project ph-schematron by phax.

the class PSReader method readSchema.

/**
 * Read the schema from the resource supplied in the constructor. First all
 * includes are resolved and than {@link #readSchemaFromXML(IMicroElement)} is
 * called.
 *
 * @return The read {@link PSSchema}.
 * @throws SchematronReadException
 *         If reading fails
 */
@Nonnull
public PSSchema readSchema() throws SchematronReadException {
    // Resolve all includes as the first action
    final SAXReaderSettings aSettings = new SAXReaderSettings().setEntityResolver(m_aEntityResolver);
    final IMicroDocument aDoc = SchematronHelper.getWithResolvedSchematronIncludes(m_aResource, aSettings, m_aErrorHandler);
    if (aDoc == null || aDoc.getDocumentElement() == null)
        throw new SchematronReadException(m_aResource, "Failed to resolve includes in Schematron resource " + m_aResource);
    if (SchematronDebug.isShowResolvedSourceSchematron())
        s_aLogger.info("Resolved source Schematron:\n" + MicroWriter.getNodeAsString(aDoc));
    return readSchemaFromXML(aDoc.getDocumentElement());
}
Also used : SAXReaderSettings(com.helger.xml.serialize.read.SAXReaderSettings) IMicroDocument(com.helger.xml.microdom.IMicroDocument) Nonnull(javax.annotation.Nonnull)

Example 14 with IMicroDocument

use of com.helger.xml.microdom.IMicroDocument in project as2-server by phax.

the class XMLCommandRegistry method load.

public void load(@Nonnull final InputStream in) throws OpenAS2Exception {
    final IMicroDocument aDoc = MicroReader.readMicroXML(in);
    final IMicroElement eRoot = aDoc.getDocumentElement();
    clearCommands();
    for (final IMicroElement eElement : eRoot.getAllChildElements()) {
        final String sNodeName = eElement.getTagName();
        if (sNodeName.equals("command"))
            loadCommand(eElement, null);
        else if (sNodeName.equals("multicommand"))
            loadMultiCommand(eElement, null);
        else
            throw new OpenAS2Exception("Undefined tag: " + sNodeName);
    }
}
Also used : OpenAS2Exception(com.helger.as2lib.exception.OpenAS2Exception) IMicroElement(com.helger.xml.microdom.IMicroElement) IMicroDocument(com.helger.xml.microdom.IMicroDocument)

Aggregations

IMicroDocument (com.helger.xml.microdom.IMicroDocument)14 IMicroElement (com.helger.xml.microdom.IMicroElement)7 IReadableResource (com.helger.commons.io.resource.IReadableResource)6 Nonnull (javax.annotation.Nonnull)4 Test (org.junit.Test)4 ClassPathResource (com.helger.commons.io.resource.ClassPathResource)3 PSReader (com.helger.schematron.pure.exchange.PSReader)3 PSSchema (com.helger.schematron.pure.model.PSSchema)3 MicroDocument (com.helger.xml.microdom.MicroDocument)3 File (java.io.File)3 CommandResult (com.helger.as2.cmd.CommandResult)2 OpenAS2Exception (com.helger.as2lib.exception.OpenAS2Exception)2 XMLPartnershipFactory (com.helger.as2lib.partner.xml.XMLPartnershipFactory)2 CommonsArrayList (com.helger.commons.collection.impl.CommonsArrayList)2 IMicroNode (com.helger.xml.microdom.IMicroNode)2 Partnership (com.helger.as2lib.partner.Partnership)1 Partner (com.helger.as2lib.partner.xml.Partner)1 ValueEnforcer (com.helger.commons.ValueEnforcer)1 Nonempty (com.helger.commons.annotation.Nonempty)1 OverrideOnDemand (com.helger.commons.annotation.OverrideOnDemand)1