Search in sources :

Example 1 with EHierarchyVisitorReturn

use of com.helger.commons.hierarchy.visit.EHierarchyVisitorReturn 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)

Aggregations

CommonsArrayList (com.helger.commons.collection.impl.CommonsArrayList)1 EHierarchyVisitorReturn (com.helger.commons.hierarchy.visit.EHierarchyVisitorReturn)1 URLResource (com.helger.commons.io.resource.URLResource)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