summaryrefslogtreecommitdiffstats
path: root/org/madore/damlengine/TodoCutHere.java
blob: 4438b5fabf765b603983101ab6f543d3dbd700d6 (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
package org.madore.damlengine;

import org.w3c.dom.*;

public final class TodoCutHere extends TodoDefaultElement {

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

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

    @Override
    public void handleNodeOnly() {
	if ( ctx.ent == null )
	    throw new IllegalStateException("entry context not defined in cut-here element");
	String lang = LangHelper.getLangRec(node);
	String explicitLang = LangHelper.getLangNorec(node);
	final WeblogLink lk
	    = new WeblogLink(ctx.ent.year, ctx.ent.month,
			     ctx.ent.day, ctx.ent.number,
			     ".CUT", ctx.ent.specialName);
	lk.setTypeSingle();
	if ( ctx.wsc == null
	     || ctx.wsc instanceof Context.WeblogSingleSelectionContext ) {
	    Element div = ctx.doc.createElementNS(DamlEngine.XHTML_NS, "div");
	    div.setAttributeNS(null, "id", lk.getFragment());
	    div.setAttributeNS(null, "class", "cut-anchor");
	    div.appendChild(ctx.doc.createComment(" EMPTY "));
	    node.getParentNode().replaceChild(div, node);
	} else {
	    // Remove every node that follows.
	    Node killPoint = node;
	    while ( ! killPoint.isSameNode(ctx.ent.mainDivNode) ) {
		Node parent = killPoint.getParentNode();
		while ( killPoint.getNextSibling() != null )
		    parent.removeChild(killPoint.getNextSibling());
		// FIXME: deleted node may already have found its way
		// in the todo deque, which means it could cause a
		// null pointer exception by trying to access its
		// parent node.  Do something about this!
		killPoint = parent;
	    }
	    // Remove the cut-point node itself.
	    node.getParentNode().removeChild(node);
	    // Now add the link to the full content.
	    Element p = ctx.doc.createElementNS(DamlEngine.XHTML_NS, "p");
	    ctx.ent.mainDivNode.appendChild(p);
	    Element a = ctx.doc.createElementNS(DamlEngine.XHTML_NS, "a");
	    p.appendChild(a);
	    final String baseDir = ((ctx.gc.uriToTop==null)?"":(ctx.gc.uriToTop+"weblog/"));
	    a.setAttributeNS(null, "href", lk.getTarget(baseDir));
	    if ( explicitLang != null )
		LangHelper.setLangNorec(p, explicitLang);
	    p.setAttributeNS(null, "class", "cut-link");
	    if ( lang.equals("en") )
		a.appendChild(ctx.doc.createTextNode("[Continue reading\u2026]"));
	    else if ( lang.equals("fr") )
		a.appendChild(ctx.doc.createTextNode("[Lire la suite\u2026]"));
	    else if ( lang.equals("de") )
		a.appendChild(ctx.doc.createTextNode("[Weiter lesen\u2026]"));
	    else if ( lang.equals("ia") )
		a.appendChild(ctx.doc.createTextNode("[Leger ultra\u2026]"));
	    else
		a.appendChild(ctx.doc.createTextNode("[\u2026]"));
	}
    }

}