summaryrefslogtreecommitdiffstats
path: root/org/madore/damlengine/TodoDamlElement.java
blob: dff27ee8fa5dee032af6ff4cf9247aaaddcc451c (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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package org.madore.damlengine;

import java.util.ArrayList;
import java.util.regex.Pattern;
import java.io.Reader;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.IOException;
import java.nio.file.Files;
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.gc.uriToTop = node.getAttributeNS(null, uriToTopName);
	final String fileNameName = "file.name";
	if ( node.hasAttributeNS(null, fileNameName) )
	    ctx.gc.fileName = node.getAttributeNS(null, fileNameName);

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

	ctx.gc.styleContent = new StringBuffer();
	try {
	    Reader rd = new BufferedReader(new InputStreamReader(Files.newInputStream(DamlEngine.templatePath.resolve("included.css")), "UTF-8"));
	    int ch;
	    while ((ch = rd.read()) > -1)
		ctx.gc.styleContent.append((char)ch);
	    rd.close();
	} catch (IOException e) {
	    throw new RuntimeException(e);
	}
	ctx.gc.scriptContent = new StringBuffer();
	try {
	    Reader rd = new BufferedReader(new InputStreamReader(Files.newInputStream(DamlEngine.templatePath.resolve("included.js")), "UTF-8"));
	    int ch;
	    while ((ch = rd.read()) > -1)
		ctx.gc.scriptContent.append((char)ch);
	    rd.close();
	} catch (IOException e) {
	    throw new RuntimeException(e);
	}

	Element meta;
	meta = ctx.doc.createElementNS(DamlEngine.XHTML_NS, "meta");
	meta.setAttributeNS(null, "charset", "utf-8");
	headNode.appendChild(meta);
	headNode.appendChild(ctx.doc.createTextNode("\n"));
	meta = ctx.doc.createElementNS(DamlEngine.XHTML_NS, "meta");
	meta.setAttributeNS(null, "name", "viewport");
	meta.setAttributeNS(null, "content", "width=device-width, initial-scale=1");
	headNode.appendChild(meta);
	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.gc.uriToTop!=null)?ctx.gc.uriToTop:"")
					   +"favicon.ico"));
	headNode.appendChild(meta);
	headNode.appendChild(ctx.doc.createTextNode("\n"));

	ArrayList<Node> childList = getChildList(this.node);
	ArrayList<TodoElement> toProcessFirst = new ArrayList<TodoElement>();
	ArrayList<TodoElement> toProcess = new ArrayList<TodoElement>(childList.size()+8);
	Element header = ctx.doc.createElementNS(DamlEngine.XHTML_NS, "header");
	bodyNode.appendChild(ctx.doc.createTextNode("\n"));
	bodyNode.appendChild(header);
	Element hgroup = ctx.doc.createElementNS(DamlEngine.XHTML_NS, "hgroup");
	header.appendChild(ctx.doc.createTextNode("\n"));
	header.appendChild(hgroup);
	if ( node.getAttributeNS(null, "notitle").equals("") ) {
	    Element token = ctx.doc.createElementNS(DamlEngine.DAML_NS,
						    "d:implicit-do-title");
	    hgroup.appendChild(ctx.doc.createTextNode("\n"));
	    hgroup.appendChild(token);
	    toProcess.add(new TodoTitleOrSubtitle(TodoTitleOrSubtitle.Type.TITLE,
						  token, this.ctx, this));
	}
	if ( node.getAttributeNS(null, "nosubtitle").equals("") ) {
	    Element token = ctx.doc.createElementNS(DamlEngine.DAML_NS,
						    "d:implicit-do-subtitle");
	    hgroup.appendChild(ctx.doc.createTextNode("\n"));
	    hgroup.appendChild(token);
	    toProcess.add(new TodoTitleOrSubtitle(TodoTitleOrSubtitle.Type.SUBTITLE,
						  token, this.ctx, this));
	}
	if ( node.getAttributeNS(null, "nonavbar").equals("") ) {
	    Element token = ctx.doc.createElementNS(DamlEngine.DAML_NS,
						    "d:implicit-do-navbar");
	    header.appendChild(ctx.doc.createTextNode("\n"));
	    header.appendChild(token);
	    toProcess.add(new TodoNavbar(token, this.ctx, this));
	}
	if ( node.getAttributeNS(null, "notranslations").equals("") ) {
	    Element token = ctx.doc.createElementNS(DamlEngine.DAML_NS,
						    "d:implicit-do-translations");
	    header.appendChild(ctx.doc.createTextNode("\n"));
	    header.appendChild(token);
	    toProcess.add(new TodoTranslations(token, this.ctx, this));
	}
	for ( Node child : childList ) {
	    if ( child.getNodeType() == Node.ELEMENT_NODE ) {
		if ( child.getLocalName().equals("body")
		     || child.getLocalName().equals("weblog") ) {
		    bodyNode.appendChild(child);
		    bodyNode.appendChild(ctx.doc.createTextNode("\n"));
		    TodoElement it
			= TodoElement.getTodoElement((Element)child, this.ctx, this);
		    toProcess.add(it);
		} else {
		    headNode.appendChild(child);
		    headNode.appendChild(ctx.doc.createTextNode("\n"));
		    TodoElement it
			= TodoElement.getTodoElement((Element)child, this.ctx, this);
		    toProcessFirst.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");
	    }
	}
	if ( node.getAttributeNS(null, "nofooter").equals("") ) {
	    Element token = ctx.doc.createElementNS(DamlEngine.DAML_NS,
						    "d:implicit-do-footer");
	    bodyNode.appendChild(token);
	    bodyNode.appendChild(ctx.doc.createTextNode("\n"));
	    toProcess.add(new TodoFooter(token, this.ctx, this));
	}
	this.ownerDeque.registerAtStart(toProcess);
	this.ownerDeque.registerAtStart(toProcessFirst);
	this.ownerDeque.
	    registerAtEnd(new TodoStyleOrScript.
			  HeadStyleOrScript(TodoStyleOrScript.Type.STYLE,
					    this.ctx, this));
	this.ownerDeque.
	    registerAtEnd(new TodoStyleOrScript.
			  HeadStyleOrScript(TodoStyleOrScript.Type.SCRIPT,
					    this.ctx, this));
    }

}