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() { if ( ctx.gc.translations == null ) { Node ws = node.getNextSibling(); if ( ws != null && ( 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); return; } ArrayList toProcess = new ArrayList(ctx.gc.translations.size()+8); for ( String trlang : ctx.gc.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.gc.fileName+"."+trlang); a.setAttributeNS(null, "hreflang", trlang); a.setAttributeNS(null, "rel", "alternate"); str = trlang; if ( trlang.equals("en") ) str = "English version"; else if ( trlang.equals("fr") ) str = "version fran\u00e7aise"; 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 \u00e9galement disponible.]"; p.appendChild(ctx.doc.createTextNode(str)); toProcess.add(TodoElement.getTodoElement(p, this.ctx, this)); } Node ws = node.getNextSibling(); if ( ws != null && ( 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); } }