summaryrefslogtreecommitdiffstats
path: root/org/madore/damlengine/TodoDamlElement.java
blob: 9885f36631b293dbf413d6b2c8a885b41dc56f3c (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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package org.madore.damlengine;

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

public final class TodoDamlElement extends TodoDefaultElement {

    public static class Factory extends TodoElement.Factory {
	@Override
	public TodoDamlElement newItem(Element node,
				       Context ctx,
				       TodoItem caller) {
	    return new TodoDamlElement(node, ctx, caller);
	}
    }

    public TodoDamlElement(Element node,
			   Context ctx,
			   TodoItem caller) {
	super(node, ctx, caller);
    }

    @Override
    public void handleNodeOnly() {
	if ( ! ( caller instanceof DamlEngine.RootTodo ) )
	    throw new IllegalArgumentException("daml node can only be root node");
	final String uriToTopName = "uri-to-top";
	if ( node.hasAttributeNS(null, uriToTopName) )
	    ctx.uriToTop = node.getAttributeNS(null, uriToTopName);
	final String fileNameName = "file.name";
	if ( node.hasAttributeNS(null, fileNameName) )
	    ctx.fileName = node.getAttributeNS(null, fileNameName);

	if ( ctx.htmlNode != null )
	    throw new IllegalStateException("html node already defined at daml node");
	ctx.htmlNode = ctx.doc.createElementNS(DamlEngine.XHTML_NS, "html");
	String lang = LangHelper.getLangNorec(node);
	if ( lang != null )
	    LangHelper.setLangNorec(ctx.htmlNode, lang);
	node.getParentNode().replaceChild(ctx.htmlNode, node);
	ctx.htmlNode.appendChild(ctx.doc.createTextNode("\n"));
	ctx.htmlNode.appendChild(ctx.doc.createComment(" This file is automatically generated.  Do not edit! "));
	ctx.htmlNode.appendChild(ctx.doc.createTextNode("\n"));
	if ( ctx.headNode != null )
	    throw new IllegalStateException("head node already defined at daml node");
	ctx.headNode = ctx.doc.createElementNS(DamlEngine.XHTML_NS, "head");
	ctx.htmlNode.appendChild(ctx.headNode);
	ctx.htmlNode.appendChild(ctx.doc.createTextNode("\n"));
	ctx.headNode.appendChild(ctx.doc.createTextNode("\n"));

	ctx.styleContent = new StringBuffer();
	ctx.scriptContent = new StringBuffer();

	Element meta;
	meta = ctx.doc.createElementNS(DamlEngine.XHTML_NS, "meta");
	meta.setAttributeNS(null, "http-equiv", "Content-Type");
	meta.setAttributeNS(null, "content", "text/html; charset=utf-8");
	ctx.headNode.appendChild(meta);
	ctx.headNode.appendChild(ctx.doc.createTextNode("\n"));
	if ( lang != null ) {
	    meta = ctx.doc.createElementNS(DamlEngine.XHTML_NS, "meta");
	    meta.setAttributeNS(null, "http-equiv", "Content-Language");
	    meta.setAttributeNS(null, "content", lang);
	    ctx.headNode.appendChild(meta);
	    ctx.headNode.appendChild(ctx.doc.createTextNode("\n"));
	}
	meta = ctx.doc.createElementNS(DamlEngine.XHTML_NS, "link");
	meta.setAttributeNS(null, "rel", "Shortcut Icon");
	meta.setAttributeNS(null, "href", (((ctx.uriToTop!=null)?ctx.uriToTop:"")
					   +"favicon.ico"));
	ctx.headNode.appendChild(meta);
	ctx.headNode.appendChild(ctx.doc.createTextNode("\n"));

	ArrayList<Node> childList = getChildList(this.node);
	ArrayList<TodoElement> toProcess = new ArrayList<TodoElement>(childList.size());
	for ( Node child : childList ) {
	    if ( child.getNodeType() == Node.ELEMENT_NODE ) {
		if ( child.getLocalName().equals("body") ) {
		    ctx.htmlNode.appendChild(ctx.doc.createTextNode("\n"));
		    ctx.htmlNode.appendChild(child);
		    ctx.htmlNode.appendChild(ctx.doc.createTextNode("\n"));
		} else {
		    ctx.headNode.appendChild(child);
		    ctx.headNode.appendChild(ctx.doc.createTextNode("\n"));
		}
		TodoElement it
		    = TodoElement.getTodoElement((Element)child, this.ctx, this);
		toProcess.add(it);
	    } else if ( child.getNodeType() == Node.TEXT_NODE
			|| child.getNodeType() == Node.CDATA_SECTION_NODE ) {
		if ( ! Pattern.matches("^\\s*$",((CharacterData)child).getData()) )
		    throw new IllegalArgumentException("daml element cannot contain text");
	    }
	}
	this.ownerDeque.registerAtStart(toProcess);
	this.ownerDeque.
	    registerAtEnd(new TodoStyleOrScript(TodoStyleOrScript.Type.STYLE,
						this.ctx, this));
	this.ownerDeque.
	    registerAtEnd(new TodoStyleOrScript(TodoStyleOrScript.Type.SCRIPT,
						this.ctx, this));
    }

}