summaryrefslogtreecommitdiffstats
path: root/org/madore/damlengine/TodoEmailAtOrDotElement.java
blob: e970264e17ed3c5994aebe1239c1aa7516ad1671 (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
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"),
	PLUS("plus-sign", "plus_sign.png", "plus", "plus");
	// Images generated with:
	// convert -size 16x20 'xc:transparent' -font 'Source-Code-Pro' -pointsize 20 -gravity South -annotate 0 '@' ~/public_html/images/commercial_at.png
	// convert -size 16x20 'xc:transparent' -font 'Source-Code-Pro' -pointsize 20 -gravity South -annotate 0 '.' ~/public_html/images/full_stop.png
	// convert -size 16x20 'xc:transparent' -font 'Source-Code-Pro' -pointsize 20 -gravity South -annotate 0 '+' ~/public_html/images/plus_sign.png
	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 img = ctx.doc.createElementNS(DamlEngine.XHTML_NS, "img");
	img.setAttributeNS(null, "src",
			   (ctx.gc.uriToTop==null?"":ctx.gc.uriToTop)+"images/"+t.imgName);
	img.setAttributeNS(null, "alt", "["+(lang.equals("fr")?t.frName:t.enName)+"]");
	img.setAttributeNS(null, "height", "20");
	img.setAttributeNS(null, "width", "16");
	span0.appendChild(img);
    }

}