package org.madore.damlengine; import java.util.ArrayList; import org.w3c.dom.*; public final class TodoTitleElement extends TodoDefaultElement { public static class Factory extends TodoElement.Factory { @Override public TodoTitleElement newItem(Element node, Context ctx, TodoItem caller) { return new TodoTitleElement(node, ctx, caller); } } public TodoTitleElement(Element node, Context ctx, TodoItem caller) { super(node, ctx, caller); } public class TodoAgain extends TodoItem { /* Make this a member class (i.e., not "static") so we can * access the "node" field of the container class. Another * option would have been to create a new subclass of * TodoElement and initialize it on the same node: which is * better? */ public TodoAgain(Context ctx, TodoItem caller) { super(ctx, caller); } @Override public void handle() { assert(this.ctx == TodoTitleElement.this.ctx); assert(this.caller == TodoTitleElement.this); if ( ctx.gc.title != null ) throw new IllegalArgumentException("attempting to redefine title"); ctx.gc.title = ctx.doc.createDocumentFragment(); ctx.gc.titleStr = node.getTextContent(); ctx.gc.titleLang = LangHelper.getLangRec(node); String lang = LangHelper.getLangNorec(node); ArrayList childList = getChildList(node); for ( Node child : childList ) { ctx.gc.title.appendChild(child); } Element tit = ctx.doc.createElementNS(DamlEngine.XHTML_NS, "title"); if ( lang != null ) LangHelper.setLangNorec(tit, lang); node.getParentNode().replaceChild(tit, node); tit.appendChild(ctx.doc.createTextNode(ctx.gc.titleStr)); } } @Override public void handleNodeOnly() { // First process the children, then come back to processing // title, so we can extract the text content after replacement. ArrayList childList = getChildList(this.node); ArrayList toProcess = new ArrayList(childList.size()); for ( Node child : childList ) { if ( child.getNodeType() == Node.ELEMENT_NODE ) { TodoElement it = TodoElement.getTodoElement((Element)child, this.ctx, this); toProcess.add(it); } } this.ownerDeque.registerAtStart(new TodoAgain(ctx, this)); this.ownerDeque.registerAtStart(toProcess); } }