01: package org.objectweb.celtix.configuration.impl;
02:
03: import java.util.logging.Logger;
04:
05: import javax.xml.namespace.QName;
06:
07: import org.w3c.dom.Document;
08: import org.w3c.dom.Element;
09: 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
18: .getL7dLogger(ConfigurationMetadataUtils.class);
19:
20: /**
21: * prevents instantiation
22: *
23: */
24: private ConfigurationMetadataUtils() {
25: }
26:
27: public static String getElementValue(Node node) {
28: for (Node nd = node.getFirstChild(); nd != null; nd = nd
29: .getNextSibling()) {
30: if (Node.TEXT_NODE == nd.getNodeType()) {
31: return nd.getNodeValue();
32: }
33: }
34: return null;
35: }
36:
37: public static QName elementAttributeToQName(Document document,
38: Element element, String attrName) {
39: return stringToQName(document, element, element
40: .getAttribute(attrName));
41: }
42:
43: public static QName elementValueToQName(Document document,
44: Element element) {
45: return stringToQName(document, element,
46: getElementValue(element));
47: }
48:
49: private static QName stringToQName(Document document,
50: Element element, String s) {
51:
52: int index = s.indexOf(":");
53: if (index < 0) {
54: return new QName(s);
55: } else if (index == 0) {
56: throw new ConfigurationException(new Message(
57: "ILLEGAL_QNAME_EXC", LOG, s));
58: }
59: String prefix = s.substring(0, index);
60: String nsAttr = "xmlns:" + prefix;
61: String uri = null;
62: Element el = element;
63: while (null == uri || "".equals(uri)) {
64: uri = el.getAttribute(nsAttr);
65: if (null != uri && !"".equals(uri)) {
66: break;
67: }
68: if (el == document.getDocumentElement()) {
69: break;
70: }
71: el = (Element) el.getParentNode();
72: }
73: if (null == uri || "".equals(uri)) {
74: throw new ConfigurationException(new Message(
75: "ILLEGAL_PREFIX_EXC", LOG, s));
76: }
77: if (index >= (s.length() - 1)) {
78: throw new ConfigurationException(new Message(
79: "ILLEGAL_QNAME_EXC", LOG, s));
80: }
81:
82: String localPart = s.substring(index + 1);
83: return new QName(uri, localPart);
84: }
85: }
|