From 7fb2155a48f7312ff71bc841ce587c44f333384b Mon Sep 17 00:00:00 2001 From: "David A. Madore" Date: Wed, 7 Sep 2011 14:51:59 +0200 Subject: Fix and correct some aspects of language handling. In particular, take better care to ensure that when an element is removed which might have an xml:lang attribute, its children keep their original language. --- org/madore/damlengine/LangHelper.java | 8 ++++++++ org/madore/damlengine/TodoBodyElement.java | 3 +++ org/madore/damlengine/TodoCommentsScript.java | 6 ++++++ org/madore/damlengine/TodoKillAElement.java | 3 +++ org/madore/damlengine/TodoKillAcronymElement.java | 6 +++--- org/madore/damlengine/TodoWeblogElement.java | 6 +++--- 6 files changed, 26 insertions(+), 6 deletions(-) (limited to 'org/madore') diff --git a/org/madore/damlengine/LangHelper.java b/org/madore/damlengine/LangHelper.java index a94f5be..aea1240 100644 --- a/org/madore/damlengine/LangHelper.java +++ b/org/madore/damlengine/LangHelper.java @@ -32,6 +32,14 @@ public class LangHelper { node.removeAttributeNS(DamlEngine.XML_NS, LANG); } + public static void setWeakLangNorec(Element node, String lang) { + if ( lang == null ) + throw new NullPointerException("lang is null in setWeakLangNorec"); + String currentLang = getLangNorec(node); + if ( currentLang == null ) + setLangNorec(node,lang); + } + public static String getLangRec(Node node) { // Never returns null (returns the empty string for no language). // node may be null (in which case, return ""). diff --git a/org/madore/damlengine/TodoBodyElement.java b/org/madore/damlengine/TodoBodyElement.java index 1db657b..2e48ee8 100644 --- a/org/madore/damlengine/TodoBodyElement.java +++ b/org/madore/damlengine/TodoBodyElement.java @@ -27,6 +27,7 @@ public final class TodoBodyElement extends TodoDefaultElement { throw new IllegalArgumentException("body node can only be child of daml node"); Element parent = (Element)(this.node.getParentNode()); + String explicitLang = LangHelper.getLangNorec(this.node); ArrayList childList = getChildList(this.node); ArrayList toProcess = new ArrayList(childList.size()); for ( Node child : childList ) { @@ -37,6 +38,8 @@ public final class TodoBodyElement extends TodoDefaultElement { } parent.insertBefore(child, node); if ( child.getNodeType() == Node.ELEMENT_NODE ) { + if ( explicitLang != null ) + LangHelper.setWeakLangNorec((Element)child, explicitLang); TodoElement it = TodoElement.getTodoElement((Element)child, this.ctx, this); toProcess.add(it); diff --git a/org/madore/damlengine/TodoCommentsScript.java b/org/madore/damlengine/TodoCommentsScript.java index 224d547..eb250e8 100644 --- a/org/madore/damlengine/TodoCommentsScript.java +++ b/org/madore/damlengine/TodoCommentsScript.java @@ -1,5 +1,6 @@ package org.madore.damlengine; +import java.util.regex.Pattern; import org.w3c.dom.*; public final class TodoCommentsScript extends TodoElement { @@ -13,6 +14,11 @@ public final class TodoCommentsScript extends TodoElement { @Override public void handle() { if ( ctx.gc.commentsJSParam == null ) { + 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); return; } diff --git a/org/madore/damlengine/TodoKillAElement.java b/org/madore/damlengine/TodoKillAElement.java index 4478fae..f230ee0 100644 --- a/org/madore/damlengine/TodoKillAElement.java +++ b/org/madore/damlengine/TodoKillAElement.java @@ -22,11 +22,14 @@ public final class TodoKillAElement extends TodoDefaultElement { @Override public void handleNodeOnly() { + String explicitLang = LangHelper.getLangNorec(this.node); ArrayList childList = getChildList(node); ArrayList toProcess = new ArrayList(childList.size()); for ( Node child : childList ) { node.getParentNode().insertBefore(child, node); if ( child.getNodeType() == Node.ELEMENT_NODE ) { + if ( explicitLang != null ) + LangHelper.setWeakLangNorec((Element)child, explicitLang); TodoElement it = TodoElement.getTodoElement((Element)child, this.ctx, this); toProcess.add(it); diff --git a/org/madore/damlengine/TodoKillAcronymElement.java b/org/madore/damlengine/TodoKillAcronymElement.java index 65664e9..f7e6190 100644 --- a/org/madore/damlengine/TodoKillAcronymElement.java +++ b/org/madore/damlengine/TodoKillAcronymElement.java @@ -28,9 +28,9 @@ public final class TodoKillAcronymElement extends TodoDefaultElement { "acronym "+node.getAttributeNS(null, "class")); else newNode.setAttributeNS(null, "class", "acronym"); - String lang = LangHelper.getLangNorec(node); - if ( lang != null ) - LangHelper.setLangNorec(newNode, lang); + String explicitLang = LangHelper.getLangNorec(node); + if ( explicitLang != null ) + LangHelper.setLangNorec(newNode, explicitLang); ArrayList attrList = getAttrList(this.node); for ( Attr attr : attrList ) { if ( attr.getNamespaceURI() == null ) { diff --git a/org/madore/damlengine/TodoWeblogElement.java b/org/madore/damlengine/TodoWeblogElement.java index 3d457a8..1cbc53c 100644 --- a/org/madore/damlengine/TodoWeblogElement.java +++ b/org/madore/damlengine/TodoWeblogElement.java @@ -25,7 +25,7 @@ public final class TodoWeblogElement extends TodoDefaultElement { public void handleNodeOnly() { System.err.println("warning: weblog element should not be processed this way!"); Node parent = node.getParentNode(); - String lang = LangHelper.getLangNorec(node); + String explicitLang = LangHelper.getLangNorec(node); ArrayList childList = getChildList(this.node); ArrayList toProcess = new ArrayList(childList.size()+8); for ( Node child : childList ) { @@ -36,8 +36,8 @@ public final class TodoWeblogElement extends TodoDefaultElement { } parent.insertBefore(child, node); if ( child.getNodeType() == Node.ELEMENT_NODE ) { - if ( lang != null ) - LangHelper.setLangRec((Element)child, lang); // Dubious + if ( explicitLang != null ) + LangHelper.setWeakLangNorec((Element)child, explicitLang); TodoElement it = TodoElement.getTodoElement((Element)child, this.ctx, this); toProcess.add(it); -- cgit v1.2.3