summaryrefslogtreecommitdiffstats
path: root/org/madore/damlengine/TodoStyleOrScript.java
blob: 553366d1e27535c1a89c065a14c03ca0d6616ede (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
package org.madore.damlengine;

import org.w3c.dom.*;

public final class TodoStyleOrScript extends TodoDefaultElement {

    public enum Type {
	STYLE("style", "text/css", "/* ", " */\n"),
	SCRIPT("script", "text/javascript", "// ", "\n");
	final String eltName;
	final String mimeType;
	final String preCdata;
	final String postCdata;
	Type(String eltName, String mimeType,
	     String preCdata, String postCdata) {
	    this.eltName = eltName;
	    this.mimeType = mimeType;
	    this.preCdata = preCdata;
	    this.postCdata = postCdata;
	}
    }

    public static class Factory extends TodoElement.Factory {
	final Type t;
	public Factory(Type t) {
	    super();
	    this.t = t;
	}
	@Override
	public TodoStyleOrScript newItem(Element node,
					 Context ctx,
					 TodoItem caller) {
	    return new TodoStyleOrScript(t, node, ctx, caller);
	}
    }

    final Type t;

    final CharSequence useThisContent;

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

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

    @Override
    public void handleNodeOnly() {
	String content;
	if ( useThisContent != null )
	    content = useThisContent.toString();
	else
	    content = node.getTextContent();
	while ( node.getLastChild() != null ) {
	    node.removeChild(node.getLastChild());
	}
	node.appendChild(ctx.doc.createTextNode("\n"+t.preCdata));
	node.appendChild(ctx.doc.
			 createCDATASection(t.postCdata+content
					    +t.preCdata));
	node.appendChild(ctx.doc.createTextNode(t.postCdata));
    }

    public static final class HeadStyleOrScript extends TodoItem {

	final Type t;

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

	@Override
	public void handle() {
	    if ( ctx.gc.headNode == null )
		throw new IllegalStateException("head node is null when doing style or script");
	    Element node
		= ctx.doc.createElementNS(DamlEngine.XHTML_NS, t.eltName);
	    node.setAttributeNS(null, "type", t.mimeType);
	    ctx.gc.headNode.appendChild(node);
	    ctx.gc.headNode.appendChild(ctx.doc.createTextNode("\n"));
	    StringBuffer content
		= (t==Type.SCRIPT)?ctx.gc.scriptContent:ctx.gc.styleContent;
	    this.ownerDeque.
		registerAtStart(new TodoStyleOrScript(t, node, this.ctx, this,
						      content));
	}

    }

}