diff options
author | David A. Madore <david@procyon.(none)> | 2010-04-18 18:37:48 +0200 |
---|---|---|
committer | David A. Madore <david@procyon.(none)> | 2010-04-18 18:37:48 +0200 |
commit | 72c90e8bef113bbbfa944f8a8ce6601d8d44ecfc (patch) | |
tree | 27f56b1f2689b13a3ab0b0c98f5f2339133f50e3 | |
parent | 98f35c3443d6df4db5a73916d9a4cd80c482f4c5 (diff) | |
download | damlengine-72c90e8bef113bbbfa944f8a8ce6601d8d44ecfc.tar.gz damlengine-72c90e8bef113bbbfa944f8a8ce6601d8d44ecfc.tar.bz2 damlengine-72c90e8bef113bbbfa944f8a8ce6601d8d44ecfc.zip |
Helper class for xml:lang handling.
-rw-r--r-- | org/madore/damlengine/DamlEngine.java | 1 | ||||
-rw-r--r-- | org/madore/damlengine/LangHelper.java | 49 |
2 files changed, 50 insertions, 0 deletions
diff --git a/org/madore/damlengine/DamlEngine.java b/org/madore/damlengine/DamlEngine.java index 03e089d..4861f06 100644 --- a/org/madore/damlengine/DamlEngine.java +++ b/org/madore/damlengine/DamlEngine.java @@ -9,6 +9,7 @@ import org.apache.xerces.jaxp.DocumentBuilderFactoryImpl; public final class DamlEngine { + public static final String XML_NS = "http://www.w3.org/XML/1998/namespace"; public static final String DAML_NS = "http://www.madore.org/~david/NS/daml/"; private DamlEngine() { } // Forbid instantiation diff --git a/org/madore/damlengine/LangHelper.java b/org/madore/damlengine/LangHelper.java new file mode 100644 index 0000000..280e5ca --- /dev/null +++ b/org/madore/damlengine/LangHelper.java @@ -0,0 +1,49 @@ +package org.madore.damlengine; + +import org.w3c.dom.Node; +import org.w3c.dom.Element; + +public class LangHelper { + + public static final String LANG = "lang"; + + public static String getLangNorec(Node node) { + if ( node instanceof Element ) + return ((Element)node).getAttributeNS(DamlEngine.XML_NS, LANG); + else + return null; + } + + public static void setLangNorec(Element node, String lang) { + if ( lang == null ) + throw new Error("lang is null in setLangNorec"); + node.setAttributeNS(DamlEngine.XML_NS, LANG, lang); + } + + public static void unsetLangNorec(Element node) { + node.removeAttributeNS(DamlEngine.XML_NS, LANG); + } + + public static String getLangRec(Node node) { + // node may be null (in which case, return null) + while ( node != null ) { + String lang = getLangNorec(node); + if ( lang != null ) + return lang; + node = node.getParentNode(); + } + return null; + } + + public static void setLangRec(Element node, String lang) { + if ( lang == null ) + throw new Error("lang is null in setLangRec"); + Node parentNode = node.getParentNode(); + String parentLang = (parentNode!=null)?getLangNorec(parentNode):null; + if ( parentLang != null && lang.equals(parentLang) ) + unsetLangNorec(node); + else + setLangNorec(node,lang); + } + +} |