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);
}
}
Aggregations