package org.madore.damlengine; import java.util.Iterator; import java.util.ArrayList; import java.util.TreeSet; import java.util.regex.Pattern; import java.util.regex.Matcher; import org.w3c.dom.*; import org.w3c.dom.ls.DOMImplementationLS; import org.w3c.dom.ls.LSParser; import org.w3c.dom.ls.LSInput; public final class TodoWeblogIndexSelectElement extends TodoDefaultElement { public static class Factory extends TodoElement.Factory { @Override public TodoWeblogIndexSelectElement newItem(Element node, Context ctx, TodoItem caller) { return new TodoWeblogIndexSelectElement(node, ctx, caller); } } public TodoWeblogIndexSelectElement(Element node, Context ctx, TodoItem caller) { super(node, ctx, caller); } @Override public void handleNodeOnly() { WeblogSummary wsum = WeblogSummary.getSummary(); if ( wsum == null || wsum.entries == null ) { throw new IllegalStateException("weblog-index-select element encountered with no weblog summary available"); } TreeSet entlist = new TreeSet(wsum.entries.keySet()); Element ul = ctx.doc.createElementNS(DamlEngine.XHTML_NS, "ul"); ul.setAttributeNS(null, "id", "index"); ul.appendChild(ctx.doc.createTextNode("\n")); node.getParentNode().replaceChild(ul, node); final DOMImplementationLS domi = (DOMImplementationLS)ctx.doc.getImplementation(); LSParser par = domi.createLSParser(DOMImplementationLS.MODE_SYNCHRONOUS, null); ArrayList toProcess = new ArrayList(entlist.size()*2); Context ctx2 = ctx.clone(); ctx2.killA = true; for ( Iterator iter=entlist.descendingIterator() ; iter.hasNext() ; ) { WeblogSummary.EntrySummary ent = wsum.entries.get(iter.next()); Matcher matcher = Pattern.compile("^(\\d{4})-(\\d{2})-(\\d{2})$").matcher(ent.date); if ( ! matcher.matches() ) throw new IllegalArgumentException("entry "+ent.id+" has badly formed date"); String yearStr = matcher.group(1); String monthStr = matcher.group(2); String dayStr = matcher.group(3); String numberStr = String.format("%04d",ent.id); String targetFile = ""; targetFile = ((ctx.gc.uriToTop==null)?"":(ctx.gc.uriToTop+"weblog/")) + yearStr + "-" + monthStr + ".html"; String targetFragment = "d." + yearStr + "-" + monthStr + "-" + dayStr + "." + numberStr; String target = targetFile + "#" + targetFragment; Element li = ctx.doc.createElementNS(DamlEngine.XHTML_NS, "li"); ul.appendChild(li); ul.appendChild(ctx.doc.createTextNode("\n")); li.setAttributeNS(null, "id", targetFragment); li.setAttributeNS(null, "class", "weblog-index-entry"); LangHelper.setLangRec(li, ent.lang); Element link; if ( ent.titleXml == null ) { link = ctx.doc.createElementNS(DamlEngine.XHTML_NS, "a"); li.appendChild(link); link.appendChild(ctx.doc.createTextNode(ent.date)); } else { li.appendChild(ctx.doc.createTextNode(ent.date+": ")); link = ctx.doc.createElementNS(DamlEngine.XHTML_NS, "a"); li.appendChild(link); LSInput input = domi.createLSInput(); input.setStringData(ent.titleXml); Document temp = par.parse(input); Node newNode = ctx.doc.adoptNode(temp.getDocumentElement()); String explicitLang = LangHelper.getLangNorec(newNode); if ( explicitLang != null ) LangHelper.setLangNorec(link, explicitLang); ArrayList childList = getChildList(newNode); for ( Node child : childList ) { link.appendChild(child); if ( child instanceof Element ) { TodoElement it = TodoElement.getTodoElement((Element)child, ctx2, this); toProcess.add(it); } } } link.setAttributeNS(null, "href", target); } this.ownerDeque.registerAtStart(toProcess); } }