KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > celtix > configuration > impl > ConfigurationMetadataUtils


1 package org.objectweb.celtix.configuration.impl;
2
3 import java.util.logging.Logger JavaDoc;
4
5 import javax.xml.namespace.QName JavaDoc;
6
7 import org.w3c.dom.Document JavaDoc;
8 import org.w3c.dom.Element JavaDoc;
9 import org.w3c.dom.Node JavaDoc;
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 JavaDoc LOG = LogUtils.getL7dLogger(ConfigurationMetadataUtils.class);
18     
19     /**
20      * prevents instantiation
21      *
22      */

23     private ConfigurationMetadataUtils() {
24     }
25     
26     public static String JavaDoc getElementValue(Node JavaDoc node) {
27         for (Node JavaDoc 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 JavaDoc elementAttributeToQName(Document JavaDoc document, Element JavaDoc element, String JavaDoc attrName) {
36         return stringToQName(document, element, element.getAttribute(attrName));
37     }
38     
39     public static QName JavaDoc elementValueToQName(Document JavaDoc document, Element JavaDoc element) {
40         return stringToQName(document, element, getElementValue(element));
41     }
42     
43     private static QName JavaDoc stringToQName(Document JavaDoc document, Element JavaDoc element, String JavaDoc s) {
44         
45         int index = s.indexOf(":");
46         if (index < 0) {
47             return new QName JavaDoc(s);
48         } else if (index == 0) {
49             throw new ConfigurationException(new Message("ILLEGAL_QNAME_EXC", LOG, s));
50         }
51         String JavaDoc prefix = s.substring(0, index);
52         String JavaDoc nsAttr = "xmlns:" + prefix;
53         String JavaDoc uri = null;
54         Element JavaDoc 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 JavaDoc)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 JavaDoc localPart = s.substring(index + 1);
73         return new QName JavaDoc(uri, localPart);
74     }
75 }
76
Popular Tags