1 package org.objectweb.celtix.configuration.impl; 2 3 import java.util.logging.Logger ; 4 5 import javax.xml.namespace.QName ; 6 7 import org.w3c.dom.Document ; 8 import org.w3c.dom.Element ; 9 import org.w3c.dom.Node ; 10 11 import org.objectweb.celtix.common.i18n.Message; 12 import org.objectweb.celtix.common.logging.LogUtils; 13 import org.objectweb.celtix.configuration.ConfigurationException; 14 15 public final class ConfigurationMetadataUtils { 16 17 private static final Logger LOG = LogUtils.getL7dLogger(ConfigurationMetadataUtils.class); 18 19 23 private ConfigurationMetadataUtils() { 24 } 25 26 public static String getElementValue(Node node) { 27 for (Node nd = node.getFirstChild(); nd != null; nd = nd.getNextSibling()) { 28 if (Node.TEXT_NODE == nd.getNodeType()) { 29 return nd.getNodeValue(); 30 } 31 } 32 return null; 33 } 34 35 public static QName elementAttributeToQName(Document document, Element element, String attrName) { 36 return stringToQName(document, element, element.getAttribute(attrName)); 37 } 38 39 public static QName elementValueToQName(Document document, Element element) { 40 return stringToQName(document, element, getElementValue(element)); 41 } 42 43 private static QName stringToQName(Document document, Element element, String s) { 44 45 int index = s.indexOf(":"); 46 if (index < 0) { 47 return new QName (s); 48 } else if (index == 0) { 49 throw new ConfigurationException(new Message("ILLEGAL_QNAME_EXC", LOG, s)); 50 } 51 String prefix = s.substring(0, index); 52 String nsAttr = "xmlns:" + prefix; 53 String uri = null; 54 Element el = element; 55 while (null == uri || "".equals(uri)) { 56 uri = el.getAttribute(nsAttr); 57 if (null != uri && !"".equals(uri)) { 58 break; 59 } 60 if (el == document.getDocumentElement()) { 61 break; 62 } 63 el = (Element )el.getParentNode(); 64 } 65 if (null == uri || "".equals(uri)) { 66 throw new ConfigurationException(new Message("ILLEGAL_PREFIX_EXC", LOG, s)); 67 } 68 if (index >= (s.length() - 1)) { 69 throw new ConfigurationException(new Message("ILLEGAL_QNAME_EXC", LOG, s)); 70 } 71 72 String localPart = s.substring(index + 1); 73 return new QName (uri, localPart); 74 } 75 } 76 | Popular Tags |