diff options
-rw-r--r-- | org/madore/damlengine/Context.java | 2 | ||||
-rw-r--r-- | org/madore/damlengine/TodoBodyElement.java | 5 | ||||
-rw-r--r-- | org/madore/damlengine/TodoElement.java | 1 | ||||
-rw-r--r-- | org/madore/damlengine/TodoTranslationElement.java | 40 | ||||
-rw-r--r-- | org/madore/damlengine/TodoTranslations.java | 57 |
5 files changed, 103 insertions, 2 deletions
diff --git a/org/madore/damlengine/Context.java b/org/madore/damlengine/Context.java index 2545f32..2cb56f9 100644 --- a/org/madore/damlengine/Context.java +++ b/org/madore/damlengine/Context.java @@ -1,5 +1,6 @@ package org.madore.damlengine; +import java.util.ArrayList; import org.w3c.dom.*; public class Context { @@ -11,6 +12,7 @@ public class Context { public String fileName; public StringBuffer styleContent; public StringBuffer scriptContent; + public ArrayList<String> translations; public Context(Document doc) { this.doc = doc; diff --git a/org/madore/damlengine/TodoBodyElement.java b/org/madore/damlengine/TodoBodyElement.java index 05073c8..ce1f064 100644 --- a/org/madore/damlengine/TodoBodyElement.java +++ b/org/madore/damlengine/TodoBodyElement.java @@ -42,12 +42,13 @@ public final class TodoBodyElement extends TodoDefaultElement { bodyNode.appendChild(token); toProcess.add(new TodoNavbar(token, this.ctx, this)); } - if ( node.getAttributeNS(null, "notranslations").equals("") ) { + if ( node.getAttributeNS(null, "notranslations").equals("") + && ctx.translations != null ) { Element token = ctx.doc.createElementNS(DamlEngine.DAML_NS, "d:implicit-do-translations"); bodyNode.appendChild(ctx.doc.createTextNode("\n")); bodyNode.appendChild(token); - // toProcess.add(new TodoTranslations(token, this.ctx, this)); + toProcess.add(new TodoTranslations(token, this.ctx, this)); } for ( Node child : childList ) { if ( child.getNodeType() == Node.TEXT_NODE diff --git a/org/madore/damlengine/TodoElement.java b/org/madore/damlengine/TodoElement.java index 4f5d3ab..8515688 100644 --- a/org/madore/damlengine/TodoElement.java +++ b/org/madore/damlengine/TodoElement.java @@ -20,6 +20,7 @@ public abstract class TodoElement extends TodoItem { damlDefaultFactory = new TodoDefaultElement.Factory(); damlFactories.put("daml", new TodoDamlElement.Factory()); damlFactories.put("body", new TodoBodyElement.Factory()); + damlFactories.put("translation", new TodoTranslationElement.Factory()); } protected final Element node; diff --git a/org/madore/damlengine/TodoTranslationElement.java b/org/madore/damlengine/TodoTranslationElement.java new file mode 100644 index 0000000..943f7a7 --- /dev/null +++ b/org/madore/damlengine/TodoTranslationElement.java @@ -0,0 +1,40 @@ +package org.madore.damlengine; + +import java.util.ArrayList; +import java.util.regex.Pattern; +import org.w3c.dom.*; + +public final class TodoTranslationElement extends TodoDefaultElement { + + public static class Factory extends TodoElement.Factory { + @Override + public TodoTranslationElement newItem(Element node, + Context ctx, + TodoItem caller) { + return new TodoTranslationElement(node, ctx, caller); + } + } + + public TodoTranslationElement(Element node, + Context ctx, + TodoItem caller) { + super(node, ctx, caller); + } + + @Override + public void handleNodeOnly() { + if ( ctx.translations == null ) + ctx.translations = new ArrayList<String>(8); + String lang = LangHelper.getLangNorec(node); + if ( lang == null ) + throw new IllegalArgumentException("translation element must have an xml:lang attribute"); + ctx.translations.add(lang); + Node ws = node.getNextSibling(); + if ( ( 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); + } + +} diff --git a/org/madore/damlengine/TodoTranslations.java b/org/madore/damlengine/TodoTranslations.java new file mode 100644 index 0000000..894e7a3 --- /dev/null +++ b/org/madore/damlengine/TodoTranslations.java @@ -0,0 +1,57 @@ +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() { + ArrayList<TodoElement> toProcess = new ArrayList<TodoElement>(ctx.translations.size()+8); + for ( String trlang : ctx.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.fileName+"."+trlang); + a.setAttributeNS(null, "hreflang", trlang); + str = trlang; + if ( trlang.equals("en") ) + str = "English version"; + else if ( trlang.equals("fr") ) + str = "version française"; + 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 également disponible.]"; + p.appendChild(ctx.doc.createTextNode(str)); + toProcess.add(TodoElement.getTodoElement(p, this.ctx, this)); + } + Node ws = node.getNextSibling(); + if ( ( 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); + } + +} |