summaryrefslogtreecommitdiffstats
path: root/org/madore/damlengine/TodoTranslations.java
blob: 53a95032725df8adb8edcf64c264d3c22b105d07 (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
package org.madore.damlengine;

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

public final class TodoTranslations extends TodoElement {

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

    @Override
    public void handle() {
	if ( ctx.gc.translations == 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;
	}
	ArrayList<TodoElement> toProcess = new ArrayList<TodoElement>(ctx.gc.translations.size()+8);
	for ( String trlang : ctx.gc.translations ) {
	    Element p = ctx.doc.createElementNS(DamlEngine.XHTML_NS, "p");
	    LangHelper.setLangNorec(p, trlang);
	    p.setAttributeNS(null, "class", "translation-offer");
	    node.getParentNode().insertBefore(p, node);
	    node.getParentNode().insertBefore(ctx.doc.createTextNode("\n"), node);
	    String str = "[";
	    if ( trlang.equals("en") )
		str = "[An ";
	    else if ( trlang.equals("fr") )
		str = "[Une ";
	    p.appendChild(ctx.doc.createTextNode(str));
	    Element a = ctx.doc.createElementNS(DamlEngine.XHTML_NS, "a");
	    p.appendChild(a);
	    a.setAttributeNS(null, "href", ctx.gc.fileName+"."+trlang);
	    a.setAttributeNS(null, "hreflang", trlang);
	    a.setAttributeNS(null, "rel", "alternate");
	    str = trlang;
	    if ( trlang.equals("en") )
		str = "English version";
	    else if ( trlang.equals("fr") )
		str = "version fran\u00e7aise";
	    a.appendChild(ctx.doc.createTextNode(str));
	    str = "]";
	    if ( trlang.equals("en") )
		str = " of this page is also available.]";
	    else if ( trlang.equals("fr") )
		str = " de cette page est \u00e9galement disponible.]";
	    p.appendChild(ctx.doc.createTextNode(str));
	    toProcess.add(TodoElement.getTodoElement(p, this.ctx, this));
	}
	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);
	this.ownerDeque.registerAtStart(toProcess);
    }

}