From 3d574c5090212ad271e1878719b4704a0466d2c5 Mon Sep 17 00:00:00 2001 From: "David A. Madore" Date: Sun, 10 Oct 2010 22:47:29 +0200 Subject: Despammed email tags. --- org/madore/damlengine/TodoElement.java | 3 + org/madore/damlengine/TodoEmailAtOrDotElement.java | 74 ++++++++++++++++++++++ .../damlengine/TodoEmailDespammedElement.java | 47 ++++++++++++++ org/madore/damlengine/TodoMetaElement.java | 2 - 4 files changed, 124 insertions(+), 2 deletions(-) create mode 100644 org/madore/damlengine/TodoEmailAtOrDotElement.java create mode 100644 org/madore/damlengine/TodoEmailDespammedElement.java diff --git a/org/madore/damlengine/TodoElement.java b/org/madore/damlengine/TodoElement.java index 01e5863..80565cc 100644 --- a/org/madore/damlengine/TodoElement.java +++ b/org/madore/damlengine/TodoElement.java @@ -25,6 +25,9 @@ public abstract class TodoElement extends TodoItem { damlFactories.put("translation", new TodoTranslationElement.Factory()); damlFactories.put("meta-description", new TodoMetaElement.Factory(TodoMetaElement.Type.DESCRIPTION)); damlFactories.put("meta-keywords", new TodoMetaElement.Factory(TodoMetaElement.Type.KEYWORDS)); + damlFactories.put("email-despammed", new TodoEmailDespammedElement.Factory()); + damlFactories.put("email-at", new TodoEmailAtOrDotElement.Factory(TodoEmailAtOrDotElement.Type.AT)); + damlFactories.put("email-dot", new TodoEmailAtOrDotElement.Factory(TodoEmailAtOrDotElement.Type.DOT)); } protected final Element node; diff --git a/org/madore/damlengine/TodoEmailAtOrDotElement.java b/org/madore/damlengine/TodoEmailAtOrDotElement.java new file mode 100644 index 0000000..8e3eed3 --- /dev/null +++ b/org/madore/damlengine/TodoEmailAtOrDotElement.java @@ -0,0 +1,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); + } + +} diff --git a/org/madore/damlengine/TodoEmailDespammedElement.java b/org/madore/damlengine/TodoEmailDespammedElement.java new file mode 100644 index 0000000..9d393ed --- /dev/null +++ b/org/madore/damlengine/TodoEmailDespammedElement.java @@ -0,0 +1,47 @@ +package org.madore.damlengine; + +import java.util.ArrayList; +import org.w3c.dom.*; + +public final class TodoEmailDespammedElement extends TodoDefaultElement { + + public static class Factory extends TodoElement.Factory { + @Override + public TodoEmailDespammedElement newItem(Element node, + Context ctx, + TodoItem caller) { + return new TodoEmailDespammedElement(node, ctx, caller); + } + } + + public TodoEmailDespammedElement(Element node, + Context ctx, + TodoItem caller) { + super(node, ctx, caller); + } + + @Override + public void handleNodeOnly() { + Element out0 = ctx.doc.createElementNS(DamlEngine.XHTML_NS, "code"); + String lang = LangHelper.getLangNorec(node); + if ( lang != null ) + LangHelper.setLangNorec(out0, lang); + Element out1 = ctx.doc.createElementNS(DamlEngine.XHTML_NS, "a"); + out0.appendChild(out1); + out1.setAttributeNS(null, "class", "despammed-address"); + ArrayList childList = getChildList(node); + ArrayList toProcess + = new ArrayList(childList.size()+8); + for ( Node child : childList ) { + out1.appendChild(child); + if ( child.getNodeType() == Node.ELEMENT_NODE ) { + TodoElement it + = TodoElement.getTodoElement((Element)child, this.ctx, this); + toProcess.add(it); + } + } + node.getParentNode().replaceChild(out0, node); + this.ownerDeque.registerAtStart(toProcess); + } + +} diff --git a/org/madore/damlengine/TodoMetaElement.java b/org/madore/damlengine/TodoMetaElement.java index 1089bf9..509d560 100644 --- a/org/madore/damlengine/TodoMetaElement.java +++ b/org/madore/damlengine/TodoMetaElement.java @@ -1,7 +1,5 @@ package org.madore.damlengine; -import java.util.ArrayList; -import java.util.regex.Pattern; import org.w3c.dom.*; public final class TodoMetaElement extends TodoDefaultElement { -- cgit v1.2.3