summaryrefslogtreecommitdiffstats
path: root/org/madore/damlengine/TodoTitleOrSubtitle.java
blob: 3000c61317245423716295ddf1d7ded89be85c04 (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
package org.madore.damlengine;

import java.util.ArrayList;
import java.util.regex.Pattern;
import org.w3c.dom.*;

public final class TodoTitleOrSubtitle extends TodoElement {

    public enum Type {
	TITLE("h1","title"),
	SUBTITLE("p","subtitle");
	final String eltName;
	final String eltClass;
	Type(String eltName, String eltClass) {
	    this.eltName = eltName;
	    this.eltClass = eltClass;
	}
    }

    final Type t;

    public TodoTitleOrSubtitle(Type t,
			       Element node,
			       Context ctx,
			       TodoItem caller) {
	super(node, ctx, caller);
	this.t = t;
    }

    @Override
    public void handle() {
	if ( ((t==Type.TITLE)?ctx.gc.title:ctx.gc.subtitle) == null ) {
	    Node ws = node.getNextSibling();
	    if ( ws != null && ( ws.getNodeType() == Node.TEXT_NODE
		   || ws.getNodeType() == Node.CDATA_SECTION_NODE )
		 && Pattern.matches("^\\s*$",((CharacterData)ws).getData()) )
		node.getParentNode().removeChild(ws);
	    node.getParentNode().removeChild(node);
	    return;
	}
	Element elt = ctx.doc.createElementNS(DamlEngine.XHTML_NS, t.eltName);
	String expLang = (t==Type.TITLE)?ctx.gc.titleLang:ctx.gc.subtitleLang;
	if ( expLang != null )
	    LangHelper.setLangNorec(node, expLang);
	elt.setAttributeNS(null, "class", t.eltClass);
	node.getParentNode().replaceChild(elt, node);
	ArrayList<Node> childList
	    = TodoDefaultElement.getChildList((t==Type.TITLE)?ctx.gc.title:ctx.gc.subtitle);
	for ( Node child : childList )
	    elt.appendChild(child.cloneNode(true));
	TodoElement it
	    = TodoElement.getTodoElement(elt, this.ctx, this);
	this.ownerDeque.registerAtStart(it);
    }

}