From b39ca81f1300ad9e59839a9a52e9a6081dd4a517 Mon Sep 17 00:00:00 2001 From: "David A. Madore" Date: Sat, 9 Oct 2010 21:50:58 +0200 Subject: Handling of translations. --- org/madore/damlengine/Context.java | 2 + org/madore/damlengine/TodoBodyElement.java | 5 +- org/madore/damlengine/TodoElement.java | 1 + org/madore/damlengine/TodoTranslationElement.java | 40 ++++++++++++++++ org/madore/damlengine/TodoTranslations.java | 57 +++++++++++++++++++++++ 5 files changed, 103 insertions(+), 2 deletions(-) create mode 100644 org/madore/damlengine/TodoTranslationElement.java create mode 100644 org/madore/damlengine/TodoTranslations.java (limited to 'org/madore/damlengine') 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 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(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 toProcess = new ArrayList(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); + } + +} -- cgit v1.2.3