From 72c90e8bef113bbbfa944f8a8ce6601d8d44ecfc Mon Sep 17 00:00:00 2001 From: "David A. Madore" Date: Sun, 18 Apr 2010 18:37:48 +0200 Subject: Helper class for xml:lang handling. --- org/madore/damlengine/DamlEngine.java | 1 + org/madore/damlengine/LangHelper.java | 49 +++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 org/madore/damlengine/LangHelper.java (limited to 'org/madore') 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); + } + +} -- cgit v1.2.3