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); } }