Search in sources :

Example 31 with IReadableResource

use of com.helger.commons.io.resource.IReadableResource in project ph-schematron by phax.

the class SchematronTestHelper method _readDI.

@Nonnull
private static ICommonsList<SchematronTestFile> _readDI(@Nonnull final IReadableResource aRes) {
    if (false)
        ClassPathHelper.getAllClassPathEntries().forEach(x -> {
            System.out.println(x);
            if (new File(x).isDirectory()) {
                final FileSystemRecursiveIterator it = new FileSystemRecursiveIterator(new File(x));
                it.forEach(y -> System.out.println(StringHelper.getRepeated("  ", it.getLevel()) + y));
            }
        });
    ValueEnforcer.notNull(aRes, "Resource");
    ValueEnforcer.isTrue(aRes.exists(), () -> "Resource " + aRes + " does not exist!");
    final ICommonsList<SchematronTestFile> ret = new CommonsArrayList<>();
    final IMicroDocument aDoc = MicroReader.readMicroXML(aRes);
    if (aDoc == null)
        throw new IllegalArgumentException("Failed to open/parse " + aRes + " as XML");
    String sLastParentDirBaseName = null;
    for (final IMicroElement eItem : aDoc.getDocumentElement().getAllChildElements()) if (eItem.getTagName().equals("directory"))
        sLastParentDirBaseName = eItem.getAttributeValue("basename");
    else if (eItem.getTagName().equals("file"))
        ret.add(new SchematronTestFile(sLastParentDirBaseName, new ClassPathResource(eItem.getAttributeValue("name")), eItem.getAttributeValue("basename")));
    else
        throw new IllegalArgumentException("Cannot handle " + eItem);
    return ret;
}
Also used : CommonsArrayList(com.helger.commons.collection.impl.CommonsArrayList) ClassPathHelper(com.helger.commons.lang.ClassPathHelper) StringHelper(com.helger.commons.string.StringHelper) IReadableResource(com.helger.commons.io.resource.IReadableResource) IMicroDocument(com.helger.xml.microdom.IMicroDocument) ClassPathResource(com.helger.commons.io.resource.ClassPathResource) File(java.io.File) ValueEnforcer(com.helger.commons.ValueEnforcer) FileSystemRecursiveIterator(com.helger.commons.io.file.FileSystemRecursiveIterator) ICommonsList(com.helger.commons.collection.impl.ICommonsList) Nonempty(com.helger.commons.annotation.Nonempty) IMicroElement(com.helger.xml.microdom.IMicroElement) MicroReader(com.helger.xml.microdom.serialize.MicroReader) Nonnull(javax.annotation.Nonnull) FileSystemRecursiveIterator(com.helger.commons.io.file.FileSystemRecursiveIterator) IMicroElement(com.helger.xml.microdom.IMicroElement) IMicroDocument(com.helger.xml.microdom.IMicroDocument) File(java.io.File) CommonsArrayList(com.helger.commons.collection.impl.CommonsArrayList) ClassPathResource(com.helger.commons.io.resource.ClassPathResource) Nonnull(javax.annotation.Nonnull)

Example 32 with IReadableResource

use of com.helger.commons.io.resource.IReadableResource 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 33 with IReadableResource

use of com.helger.commons.io.resource.IReadableResource in project ph-css by phax.

the class Issue19Test method testIssue.

@Test
public void testIssue() {
    // Multiple errors contained
    final IReadableResource aRes = new ClassPathResource("testfiles/css30/bad_but_browsercompliant/issue19.css");
    assertTrue(aRes.exists());
    final CascadingStyleSheet aCSS = CSSReader.readFromStream(aRes, new CSSReaderSettings().setFallbackCharset(StandardCharsets.UTF_8).setCSSVersion(ECSSVersion.CSS30).setCustomErrorHandler(new LoggingCSSParseErrorHandler()).setBrowserCompliantMode(true));
    assertNotNull(aCSS);
    if (false)
        System.out.println(new CSSWriter(ECSSVersion.CSS30).getCSSAsString(aCSS));
    assertEquals(1, aCSS.getRuleCount());
    assertEquals(1, aCSS.getStyleRuleCount());
}
Also used : CascadingStyleSheet(com.helger.css.decl.CascadingStyleSheet) IReadableResource(com.helger.commons.io.resource.IReadableResource) CSSWriter(com.helger.css.writer.CSSWriter) CSSReaderSettings(com.helger.css.reader.CSSReaderSettings) ClassPathResource(com.helger.commons.io.resource.ClassPathResource) LoggingCSSParseErrorHandler(com.helger.css.reader.errorhandler.LoggingCSSParseErrorHandler) Test(org.junit.Test)

Example 34 with IReadableResource

use of com.helger.commons.io.resource.IReadableResource in project ph-css by phax.

the class Issue24Test method testIssue.

@Test
public void testIssue() {
    final IReadableResource aRes = new ClassPathResource("testfiles/css30/bad_but_browsercompliant/issue24.css");
    assertTrue(aRes.exists());
    final CascadingStyleSheet aCSS = CSSReader.readFromStream(aRes, new CSSReaderSettings().setFallbackCharset(StandardCharsets.UTF_8).setBrowserCompliantMode(true).setCustomErrorHandler(new LoggingCSSParseErrorHandler()));
    assertNotNull(aCSS);
    if (false)
        System.out.println(new CSSWriter().getCSSAsString(aCSS));
}
Also used : CascadingStyleSheet(com.helger.css.decl.CascadingStyleSheet) IReadableResource(com.helger.commons.io.resource.IReadableResource) CSSWriter(com.helger.css.writer.CSSWriter) CSSReaderSettings(com.helger.css.reader.CSSReaderSettings) ClassPathResource(com.helger.commons.io.resource.ClassPathResource) LoggingCSSParseErrorHandler(com.helger.css.reader.errorhandler.LoggingCSSParseErrorHandler) Test(org.junit.Test)

Example 35 with IReadableResource

use of com.helger.commons.io.resource.IReadableResource in project ph-css by phax.

the class Issue4Test method testIssue4.

@Test
public void testIssue4() {
    final IReadableResource aRes = new ClassPathResource("testfiles/css30/good/issue4.css");
    assertTrue(aRes.exists());
    final CascadingStyleSheet aCSS = CSSReader.readFromStream(aRes, StandardCharsets.UTF_8, ECSSVersion.CSS30, new LoggingCSSParseErrorHandler());
    assertNotNull(aCSS);
    if (false)
        System.out.println(new CSSWriter(ECSSVersion.CSS30).getCSSAsString(aCSS));
}
Also used : CascadingStyleSheet(com.helger.css.decl.CascadingStyleSheet) IReadableResource(com.helger.commons.io.resource.IReadableResource) CSSWriter(com.helger.css.writer.CSSWriter) ClassPathResource(com.helger.commons.io.resource.ClassPathResource) LoggingCSSParseErrorHandler(com.helger.css.reader.errorhandler.LoggingCSSParseErrorHandler) Test(org.junit.Test)

Aggregations

IReadableResource (com.helger.commons.io.resource.IReadableResource)37 Test (org.junit.Test)25 ClassPathResource (com.helger.commons.io.resource.ClassPathResource)18 CascadingStyleSheet (com.helger.css.decl.CascadingStyleSheet)12 LoggingCSSParseErrorHandler (com.helger.css.reader.errorhandler.LoggingCSSParseErrorHandler)12 SchematronOutputType (org.oclc.purl.dsdl.svrl.SchematronOutputType)11 SchematronResourcePure (com.helger.schematron.pure.SchematronResourcePure)9 FileSystemResource (com.helger.commons.io.resource.FileSystemResource)8 CSSWriter (com.helger.css.writer.CSSWriter)8 PSSchema (com.helger.schematron.pure.model.PSSchema)7 SVRLMarshaller (com.helger.schematron.svrl.SVRLMarshaller)7 CSSReaderSettings (com.helger.css.reader.CSSReaderSettings)6 AbstractSchematronResource (com.helger.schematron.AbstractSchematronResource)6 IMicroDocument (com.helger.xml.microdom.IMicroDocument)6 CollectingPSErrorHandler (com.helger.schematron.pure.errorhandler.CollectingPSErrorHandler)5 PSReader (com.helger.schematron.pure.exchange.PSReader)5 IPSErrorHandler (com.helger.schematron.pure.errorhandler.IPSErrorHandler)4 IMicroElement (com.helger.xml.microdom.IMicroElement)3 File (java.io.File)3 Nonnull (javax.annotation.Nonnull)3