summaryrefslogtreecommitdiffstats
path: root/org/madore/damlengine/TodoEmailAtOrDotElement.java
blob: 8e3eed3ad7d33266f9096eb18d73cbbfaa9a770a (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
package org.madore.damlengine;

import org.w3c.dom.*;

public final class TodoEmailAtOrDotElement extends TodoDefaultElement {

    public enum Type {
	AT("commercial-at", "commercial_at.png", "at sign", "arobase"),
	DOT("full-stop", "full_stop.png", "dot", "point");
	final String charName;
	final String imgName;
	final String enName;
	final String frName;
	Type(String charName, String imgName,
	     String enName, String frName) {
	    this.charName = charName;
	    this.imgName = imgName;
	    this.enName = enName;
	    this.frName = frName;
	}
    }

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

    final Type t;

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

    @Override
    public void handleNodeOnly() {
	if ( ! ( caller instanceof TodoEmailDespammedElement ) )
	    throw new IllegalArgumentException(node.getLocalName()+" node can only be child of email-despammed node");
	Element span0 = ctx.doc.createElementNS(DamlEngine.XHTML_NS, "span");
	String expLang = LangHelper.getLangNorec(node);
	String lang = LangHelper.getLangRec(node);
	if ( lang == null || ! ( lang.equals("en") || lang.equals("fr") ) ) {
	    lang = "en";
	    expLang = "en";
	}
	if ( expLang != null )
	    LangHelper.setLangNorec(span0, expLang);
	span0.setAttributeNS(null, "class", "replace-"+t.charName);
	node.getParentNode().replaceChild(span0, node);
	Element span1 = ctx.doc.createElementNS(DamlEngine.XHTML_NS, "span");
	span1.setAttributeNS(null, "class", "subreplace-"+t.charName);
	span0.appendChild(span1);
	Element img = ctx.doc.createElementNS(DamlEngine.XHTML_NS, "img");
	img.setAttributeNS(null, "src",
			   (ctx.uriToTop==null?"":ctx.uriToTop)+"images/"+t.imgName);
	img.setAttributeNS(null, "alt", "["+(lang.equals("fr")?t.frName:t.enName)+"]");
	img.setAttributeNS(null, "height", "15");
	img.setAttributeNS(null, "width", "16");
	span1.appendChild(img);
    }

}