package org.madore.damlengine; import java.util.ArrayList; import java.util.regex.Pattern; import java.util.regex.Matcher; import org.w3c.dom.*; public final class TodoEntryElement extends TodoDefaultElement { public static class Factory extends TodoElement.Factory { @Override public TodoEntryElement newItem(Element node, Context ctx, TodoItem caller) { return new TodoEntryElement(node, ctx, caller); } } public TodoEntryElement(Element node, Context ctx, TodoItem caller) { super(node, ctx, caller); } @Override public void handleNodeOnly() { if ( ! ( caller instanceof TodoWeblogElement || caller instanceof TodoWeblogSelectElement ) ) throw new IllegalArgumentException("entry node can only be child of weblog node"); Element div = ctx.doc.createElementNS(DamlEngine.XHTML_NS, "article"); String explicitLang = LangHelper.getLangNorec(node); if ( explicitLang != null ) LangHelper.setLangNorec(div, explicitLang); node.getParentNode().replaceChild(div, node); String entryNumberStr = node.getAttributeNS(null, "number"); if ( ! Pattern.matches("^\\d{4}$", entryNumberStr) ) throw new IllegalArgumentException("entry number attribute must be of the form NNNN"); String entryDateStr = node.getAttributeNS(null, "date"); Matcher entryDateMatcher = Pattern.compile("^(\\d{4})-(\\d{2})-(\\d{2})$").matcher(entryDateStr); if ( ! entryDateMatcher.matches() ) throw new IllegalArgumentException("entry date attribute must be of the form YYYY-MM-DD"); String entryYearStr = entryDateMatcher.group(1); String entryMonthStr = entryDateMatcher.group(2); String entryDayStr = entryDateMatcher.group(3); String entryDowStr = node.getAttributeNS(null, "day_of_week"); String entryCatStr = node.getAttributeNS(null, "cat"); ArrayList entryCatList; if ( entryCatStr.equals("") ) { entryCatList = new ArrayList(0); } else { String[] temp = entryCatStr.split("\\s+"); entryCatList = new ArrayList(temp.length); for ( String cat : temp ) { if ( ! cat.equals("") ) entryCatList.add(cat); } } String entryIdStr = "d."+entryDateStr+"."+entryNumberStr; div.setAttributeNS(null, "id", entryIdStr); div.setAttributeNS(null, "class", "weblog-entry"); { String styleAtt = node.getAttributeNS(null, "style"); if ( ! styleAtt.equals("") ) node.setAttributeNS(null, "style", styleAtt); } div.appendChild(ctx.doc.createTextNode("\n")); Element header = ctx.doc.createElementNS(DamlEngine.XHTML_NS, "header"); div.appendChild(header); div.appendChild(ctx.doc.createTextNode("\n")); Element headlink = ctx.doc.createElementNS(DamlEngine.XHTML_NS, "p"); headlink.setAttributeNS(null, "class", "weblog-entry-headlink"); header.appendChild(headlink); header.appendChild(ctx.doc.createTextNode("\n")); Element permalink = ctx.doc.createElementNS(DamlEngine.XHTML_NS, "a"); permalink.setAttributeNS(null, "href", entryYearStr+"-"+entryMonthStr+".html" +"#"+entryIdStr); permalink.setAttributeNS(null, "rel", "bookmark"); permalink.appendChild(ctx.doc.createTextNode(entryDateStr)); headlink.appendChild(permalink); if ( ! entryDowStr.equals("") ) headlink.appendChild(ctx.doc.createTextNode(" ("+entryDowStr+")")); Context ctx2 = ctx.clone(); ctx2.ent = new Context.EntryContext(entryYearStr, entryMonthStr, entryDayStr, entryNumberStr, entryDowStr, entryCatList); ctx2.ent.headerNode = header; ctx2.ent.headlinkNode = headlink; ArrayList childList = getChildList(this.node); ArrayList toProcess = new ArrayList(childList.size()+8); for ( Node child : childList ) { if ( child.getNodeType() == Node.TEXT_NODE || child.getNodeType() == Node.CDATA_SECTION_NODE ) { if ( ! Pattern.matches("^\\s*$",((CharacterData)child).getData()) ) throw new IllegalArgumentException("entry element cannot contain text"); div.appendChild(ctx.doc.createTextNode("\n")); } else if ( child.getNodeType() == Node.ELEMENT_NODE ) { div.appendChild(child); TodoElement it = TodoElement.getTodoElement((Element)child, ctx2, this); toProcess.add(it); } } Element footer = ctx.doc.createElementNS(DamlEngine.XHTML_NS, "footer"); div.appendChild(footer); div.appendChild(ctx.doc.createTextNode("\n")); if ( entryCatList.size() > 0 ) { Element token = ctx.doc.createElementNS(DamlEngine.DAML_NS, "d:implicit-do-categories"); footer.appendChild(token); footer.appendChild(ctx.doc.createTextNode("\n")); toProcess.add(new TodoCategories(token, ctx2, this)); } if ( node.getAttributeNS(null, "nocomments").equals("") ) { Element token = ctx.doc.createElementNS(DamlEngine.DAML_NS, "d:implicit-do-comments"); footer.appendChild(token); footer.appendChild(ctx.doc.createTextNode("\n")); toProcess.add(new TodoComments(token, ctx2, this)); } this.ownerDeque.registerAtStart(toProcess); } }