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 article = ctx.doc.createElementNS(DamlEngine.XHTML_NS, "article"); String lang = LangHelper.getLangRec(node); String explicitLang = LangHelper.getLangNorec(node); if ( explicitLang != null ) LangHelper.setLangNorec(article, explicitLang); node.getParentNode().replaceChild(article, node); final String entryNumberStr = node.getAttributeNS(null, "number"); if ( ! Pattern.matches("^\\d{4}$", entryNumberStr) ) throw new IllegalArgumentException("entry number attribute must be of the form NNNN"); final String entryDateStr = node.getAttributeNS(null, "date"); final 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"); final String entryYearStr = entryDateMatcher.group(1); final String entryMonthStr = entryDateMatcher.group(2); final String entryDayStr = entryDateMatcher.group(3); String entryDowStr = node.getAttributeNS(null, "day-of-week"); { final int entryYear = Integer.parseInt(entryYearStr); final int entryMonth = Integer.parseInt(entryMonthStr); final int entryDay = Integer.parseInt(entryDayStr); final String entryRefDowStr = Calendar.dateDowName(lang, entryYear, entryMonth, entryDay); if ( entryRefDowStr != null ) { if ( entryDowStr.equals("") ) { entryDowStr = entryRefDowStr; } else if ( ( ! entryDowStr.equals(entryRefDowStr) ) && node.getAttributeNS(null, "override-day-of-week").equals("") ) { System.err.println("warning: "+entryDateStr+" day of week given "+entryDowStr+" expected "+entryRefDowStr); } } } final String entryCatStr = node.getAttributeNS(null, "cat"); final String entryDoSinglePageStr = node.hasAttributeNS(null, "single-page") ? node.getAttributeNS(null, "single-page") : null; final String entryCdateStr = node.getAttributeNS(null, "cdate"); final 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); } } final WeblogLink lk = new WeblogLink(entryYearStr, entryMonthStr, entryDayStr, entryNumberStr, "", entryDoSinglePageStr); lk.setTypeStandard(); String entryIdStr = lk.getFragment(); article.setAttributeNS(null, "id", entryIdStr); { String classAtt = node.getAttributeNS(null, "class"); if ( ! classAtt.equals("") ) article.setAttributeNS(null, "class", "weblog-entry hentry "+classAtt); else article.setAttributeNS(null, "class", "weblog-entry hentry"); } { String styleAtt = node.getAttributeNS(null, "style"); if ( ! styleAtt.equals("") ) article.setAttributeNS(null, "style", styleAtt); } article.appendChild(ctx.doc.createTextNode("\n")); Element header = ctx.doc.createElementNS(DamlEngine.XHTML_NS, "header"); article.appendChild(header); article.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", lk.getTarget("")); permalink.setAttributeNS(null, "rel", "bookmark"); Element time = ctx.doc.createElementNS(DamlEngine.XHTML_NS, "time"); permalink.appendChild(time); if ( Pattern.matches("^\\d{4}-\\d{2}-\\d{2}T\\d{2}\\:\\d{2}(?:\\:\\d{2})?(?:Z|[\\+\\-]\\d{2}\\:\\d{2})$", entryCdateStr) ) time.setAttributeNS(null, "datetime", entryCdateStr); else System.err.println("warning: cdate value "+entryCdateStr +" is not a valid HTML5 datetime"); time.setAttributeNS(null, "pubdate", "pubdate"); time.setAttributeNS(null, "class", "published"); time.appendChild(ctx.doc.createTextNode(entryDateStr)); headlink.appendChild(permalink); if ( ! entryDowStr.equals("") ) headlink.appendChild(ctx.doc.createTextNode(" ("+entryDowStr+")")); Element mainDiv = ctx.doc.createElementNS(DamlEngine.XHTML_NS, "div"); article.appendChild(mainDiv); article.appendChild(ctx.doc.createTextNode("\n")); mainDiv.setAttributeNS(null, "class", "entry-content"); Context ctx2 = ctx.clone(); ctx2.ent = new Context.EntryContext(entryYearStr, entryMonthStr, entryDayStr, entryNumberStr, entryDowStr, entryDoSinglePageStr, entryCatList); ctx2.ent.headerNode = header; ctx2.ent.headlinkNode = headlink; ctx2.ent.mainDivNode = mainDiv; 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"); mainDiv.appendChild(ctx.doc.createTextNode("\n")); } else if ( child.getNodeType() == Node.ELEMENT_NODE ) { if ( child.getLocalName().equals("title") ) { article.appendChild(child); } else { mainDiv.appendChild(child); } TodoElement it = TodoElement.getTodoElement((Element)child, ctx2, this); toProcess.add(it); } } Element footer = ctx.doc.createElementNS(DamlEngine.XHTML_NS, "footer"); article.appendChild(footer); article.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); } }