diff options
author | David A. Madore <david+git@madore.org> | 2010-04-18 22:46:21 +0200 |
---|---|---|
committer | David A. Madore <david+git@madore.org> | 2010-04-18 22:46:21 +0200 |
commit | b3177265e0da7d02b99b98d6d70bcb1cda9ff2cd (patch) | |
tree | 0b80537ea8c0137d8385c08c56b0e0c34731c385 /org/madore/damlengine/LangHelper.java | |
parent | 9d602700ed1eae5ce4b443f5182e1a15e4875da2 (diff) | |
download | damlengine-b3177265e0da7d02b99b98d6d70bcb1cda9ff2cd.tar.gz damlengine-b3177265e0da7d02b99b98d6d70bcb1cda9ff2cd.tar.bz2 damlengine-b3177265e0da7d02b99b98d6d70bcb1cda9ff2cd.zip |
Fix misuse of getAttributeNS(), and handling of xml:lang.
Note to self: getAttributeNS() returns "", not null, when the
attribute is nonexistent.
Also, the XML spec (now) clearly states that "" is used for an
unknown/undefined language.
Diffstat (limited to 'org/madore/damlengine/LangHelper.java')
-rw-r--r-- | org/madore/damlengine/LangHelper.java | 24 |
1 files changed, 16 insertions, 8 deletions
diff --git a/org/madore/damlengine/LangHelper.java b/org/madore/damlengine/LangHelper.java index 280e5ca..e2a74db 100644 --- a/org/madore/damlengine/LangHelper.java +++ b/org/madore/damlengine/LangHelper.java @@ -2,15 +2,23 @@ package org.madore.damlengine; import org.w3c.dom.Node; import org.w3c.dom.Element; +import org.w3c.dom.Attr; 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 + // Returns null when no xml:lang attribute is found, + // otherwise, returns its value. + if ( node instanceof Element ) { + Attr lang = ((Element)node). + getAttributeNodeNS(DamlEngine.XML_NS, LANG); + if ( lang == null ) + return null; + else + return lang.getValue(); + } else return null; } @@ -25,22 +33,22 @@ public class LangHelper { } public static String getLangRec(Node node) { - // node may be null (in which case, return null) + // Never returns null (returns the empty string for no language). + // node may be null (in which case, return ""). while ( node != null ) { String lang = getLangNorec(node); if ( lang != null ) return lang; node = node.getParentNode(); } - return null; + return ""; } 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) ) + String parentLang = getLangRec(node.getParentNode()); + if ( lang.equals(parentLang) ) unsetLangNorec(node); else setLangNorec(node,lang); |