Search in sources :

Example 1 with URLResource

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

the class MainFetchW3C_CSSTests method _fetch.

private static void _fetch(final String sURL, final String sDestDir) throws MalformedURLException {
    final ICommonsList<String> aCSSFilenames = new CommonsArrayList<>();
    System.out.println("Fetching from " + sURL);
    final ICommonsList<String> aIndex = StreamHelper.readStreamLines(new URLResource(sURL + "index.html"), StandardCharsets.UTF_8);
    {
        // Remove doctype
        aIndex.remove(0);
        // Fix HTML to be XML
        for (int i = 0; i < aIndex.size(); ++i) {
            final String sLine = aIndex.get(i);
            if (sLine.contains("<link"))
                aIndex.set(i, sLine + "</link>");
        }
    }
    final IMicroDocument aDoc = MicroReader.readMicroXML(StringHelper.getImploded('\n', aIndex));
    MicroVisitor.visit(aDoc, new DefaultHierarchyVisitorCallback<IMicroNode>() {

        @Override
        public EHierarchyVisitorReturn onItemBeforeChildren(final IMicroNode aItem) {
            if (aItem.isElement()) {
                final IMicroElement e = (IMicroElement) aItem;
                if (e.getTagName().equals("a")) {
                    final String sHref = e.getAttributeValue("href");
                    if (sHref.endsWith(".xml"))
                        aCSSFilenames.add(StringHelper.replaceAll(sHref, ".xml", ".css"));
                }
            }
            return EHierarchyVisitorReturn.CONTINUE;
        }
    });
    System.out.println("Fetching a total of " + aCSSFilenames.size() + " files");
    int i = 0;
    for (final String sCSSFilename : aCSSFilenames) {
        System.out.println("  " + (++i) + ".: " + sCSSFilename);
        final String sContent = StreamHelper.getAllBytesAsString(new URLResource(sURL + sCSSFilename), StandardCharsets.UTF_8);
        SimpleFileIO.writeFile(new File(sDestDir, sCSSFilename), sContent, StandardCharsets.UTF_8);
    }
}
Also used : URLResource(com.helger.commons.io.resource.URLResource) IMicroElement(com.helger.xml.microdom.IMicroElement) IMicroNode(com.helger.xml.microdom.IMicroNode) EHierarchyVisitorReturn(com.helger.commons.hierarchy.visit.EHierarchyVisitorReturn) IMicroDocument(com.helger.xml.microdom.IMicroDocument) File(java.io.File) CommonsArrayList(com.helger.commons.collection.impl.CommonsArrayList)

Example 2 with URLResource

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

the class SchematronResourceHelper method getNodeOfSource.

/**
 * Convert the passed transform source into a DOM node. Currently on
 * {@link DOMSource} and {@link StreamSource} can be handled.
 *
 * @param aSource
 *        The transform source to use. May not be <code>null</code>.
 * @param aDRS
 *        DOMReader settings to use. May not be <code>null</code>.
 * @return The DOM node and never <code>null</code>.
 * @throws SAXException
 *         In case XML parsing fails
 * @throws IllegalArgumentException
 *         in case an unsupported {@link Source} implementation is provided.
 */
@Nullable
public static Node getNodeOfSource(@Nonnull final Source aSource, @Nonnull final DOMReaderSettings aDRS) throws SAXException {
    ValueEnforcer.notNull(aSource, "Source");
    ValueEnforcer.notNull(aDRS, "DOMReaderSettings");
    if (aSource instanceof DOMSource) {
        // Node is already in DOMSource
        return ((DOMSource) aSource).getNode();
    }
    if (aSource instanceof StreamSource) {
        // In StreamSource it can either be a byte stream or a character stream or
        // a system ID
        final StreamSource aStreamSource = (StreamSource) aSource;
        final InputStream aIS = aStreamSource.getInputStream();
        if (aIS != null) {
            // Byte stream
            final Document aDoc = DOMReader.readXMLDOM(aIS, aDRS != null ? aDRS : new DOMReaderSettings());
            if (aDoc == null)
                throw new IllegalArgumentException("Failed to read source " + aSource + " as XML from InputStream " + aIS);
            return aDoc;
        }
        final Reader aReader = aStreamSource.getReader();
        if (aReader != null) {
            // CHaracter stream
            final Document aDoc = DOMReader.readXMLDOM(aReader, aDRS);
            if (aDoc == null)
                throw new IllegalArgumentException("Failed to read source " + aSource + " as XML from Reader " + aReader);
            return aDoc;
        }
        final String sSystemID = aStreamSource.getSystemId();
        if (StringHelper.hasText(sSystemID)) {
            // System ID
            try {
                final URLResource aURL = new URLResource(sSystemID);
                final Document aDoc = DOMReader.readXMLDOM(aURL, aDRS);
                if (aDoc == null)
                    throw new IllegalArgumentException("Failed to read source " + aSource + " as XML from SystemID '" + sSystemID + "'");
                return aDoc;
            } catch (final MalformedURLException ex) {
                throw new IllegalArgumentException("Failed to read source " + aSource + " as XML from SystemID '" + sSystemID + "': " + ex.getMessage());
            }
        }
        // Neither InputStream nor Reader present
        s_aLogger.error("StreamSource contains neither InputStream nor Reader nor SystemID - cannot handle!");
        return null;
    }
    final String sMsg = "Can only handle DOMSource and StreamSource - having " + aSource + " with system ID '" + aSource.getSystemId() + "'";
    s_aLogger.error(sMsg);
    throw new IllegalArgumentException(sMsg);
}
Also used : URLResource(com.helger.commons.io.resource.URLResource) DOMSource(javax.xml.transform.dom.DOMSource) MalformedURLException(java.net.MalformedURLException) InputStream(java.io.InputStream) StreamSource(javax.xml.transform.stream.StreamSource) Reader(java.io.Reader) DOMReader(com.helger.xml.serialize.read.DOMReader) DOMReaderSettings(com.helger.xml.serialize.read.DOMReaderSettings) Document(org.w3c.dom.Document) Nullable(javax.annotation.Nullable)

Aggregations

URLResource (com.helger.commons.io.resource.URLResource)2 CommonsArrayList (com.helger.commons.collection.impl.CommonsArrayList)1 EHierarchyVisitorReturn (com.helger.commons.hierarchy.visit.EHierarchyVisitorReturn)1 IMicroDocument (com.helger.xml.microdom.IMicroDocument)1 IMicroElement (com.helger.xml.microdom.IMicroElement)1 IMicroNode (com.helger.xml.microdom.IMicroNode)1 DOMReader (com.helger.xml.serialize.read.DOMReader)1 DOMReaderSettings (com.helger.xml.serialize.read.DOMReaderSettings)1 File (java.io.File)1 InputStream (java.io.InputStream)1 Reader (java.io.Reader)1 MalformedURLException (java.net.MalformedURLException)1 Nullable (javax.annotation.Nullable)1 DOMSource (javax.xml.transform.dom.DOMSource)1 StreamSource (javax.xml.transform.stream.StreamSource)1 Document (org.w3c.dom.Document)1