Search in sources :

Example 1 with Wrapper

use of com.helger.commons.wrapper.Wrapper 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 2 with Wrapper

use of com.helger.commons.wrapper.Wrapper in project ph-css by phax.

the class MainReadAllCSSOnDisc method main.

@SuppressFBWarnings("DMI_HARDCODED_ABSOLUTE_FILENAME")
public static void main(final String[] args) {
    int nFilesOK = 0;
    int nFilesError = 0;
    final ICommonsOrderedMap<File, ParseException> aErrors = new CommonsLinkedHashMap<>();
    final Wrapper<File> aCurrentFile = new Wrapper<>();
    final ICSSParseExceptionCallback aHdl = ex -> aErrors.put(aCurrentFile.get(), ex);
    for (final File aFile : new FileSystemRecursiveIterator(new File("/")).withFilter(IFileFilter.filenameEndsWith(".css"))) {
        if (false)
            s_aLogger.info(aFile.getAbsolutePath());
        aCurrentFile.set(aFile);
        final CascadingStyleSheet aCSS = CSSReader.readFromFile(aFile, StandardCharsets.UTF_8, ECSSVersion.CSS30, aHdl);
        if (aCSS == null) {
            nFilesError++;
            s_aLogger.warn("  " + aFile.getAbsolutePath() + " failed!");
        } else
            nFilesOK++;
    }
    s_aLogger.info("Done");
    for (final Map.Entry<File, ParseException> aEntry : aErrors.entrySet()) s_aLogger.info("  " + aEntry.getKey().getAbsolutePath() + ":\n" + aEntry.getValue().getMessage() + "\n");
    s_aLogger.info("OK: " + nFilesOK + "; Error: " + nFilesError);
}
Also used : Logger(org.slf4j.Logger) ICSSParseExceptionCallback(com.helger.css.handler.ICSSParseExceptionCallback) CSSReader(com.helger.css.reader.CSSReader) LoggerFactory(org.slf4j.LoggerFactory) ICommonsOrderedMap(com.helger.commons.collection.impl.ICommonsOrderedMap) ECSSVersion(com.helger.css.ECSSVersion) ParseException(com.helger.css.parser.ParseException) File(java.io.File) StandardCharsets(java.nio.charset.StandardCharsets) FileSystemRecursiveIterator(com.helger.commons.io.file.FileSystemRecursiveIterator) IFileFilter(com.helger.commons.io.file.IFileFilter) Wrapper(com.helger.commons.wrapper.Wrapper) Map(java.util.Map) CommonsLinkedHashMap(com.helger.commons.collection.impl.CommonsLinkedHashMap) CascadingStyleSheet(com.helger.css.decl.CascadingStyleSheet) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings) Wrapper(com.helger.commons.wrapper.Wrapper) CommonsLinkedHashMap(com.helger.commons.collection.impl.CommonsLinkedHashMap) ICSSParseExceptionCallback(com.helger.css.handler.ICSSParseExceptionCallback) CascadingStyleSheet(com.helger.css.decl.CascadingStyleSheet) FileSystemRecursiveIterator(com.helger.commons.io.file.FileSystemRecursiveIterator) ParseException(com.helger.css.parser.ParseException) File(java.io.File) ICommonsOrderedMap(com.helger.commons.collection.impl.ICommonsOrderedMap) Map(java.util.Map) CommonsLinkedHashMap(com.helger.commons.collection.impl.CommonsLinkedHashMap) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Aggregations

Wrapper (com.helger.commons.wrapper.Wrapper)2 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)2 CommonsLinkedHashMap (com.helger.commons.collection.impl.CommonsLinkedHashMap)1 ICommonsOrderedMap (com.helger.commons.collection.impl.ICommonsOrderedMap)1 DefaultHierarchyVisitorCallback (com.helger.commons.hierarchy.visit.DefaultHierarchyVisitorCallback)1 FileSystemRecursiveIterator (com.helger.commons.io.file.FileSystemRecursiveIterator)1 IFileFilter (com.helger.commons.io.file.IFileFilter)1 IReadableResource (com.helger.commons.io.resource.IReadableResource)1 ECSSVersion (com.helger.css.ECSSVersion)1 CascadingStyleSheet (com.helger.css.decl.CascadingStyleSheet)1 ICSSParseExceptionCallback (com.helger.css.handler.ICSSParseExceptionCallback)1 ParseException (com.helger.css.parser.ParseException)1 CSSReader (com.helger.css.reader.CSSReader)1 DefaultSchematronIncludeResolver (com.helger.schematron.resolve.DefaultSchematronIncludeResolver)1 IMicroDocument (com.helger.xml.microdom.IMicroDocument)1 IMicroElement (com.helger.xml.microdom.IMicroElement)1 IMicroNode (com.helger.xml.microdom.IMicroNode)1 File (java.io.File)1 IOException (java.io.IOException)1 StandardCharsets (java.nio.charset.StandardCharsets)1