summaryrefslogtreecommitdiffstats
path: root/org/madore/damlengine/TodoTitleElement.java
blob: 9854f1ad0465388f817a94ea68d1b9a86118ffde (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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<Node> 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<Node> childList = getChildList(this.node);
	ArrayList<TodoElement> toProcess = new ArrayList<TodoElement>(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);
    }

}