KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > webservice > util > DOMUtils


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7
8 // $Id: DOMUtils.java,v 1.1.4.1 2005/06/14 13:28:18 tdiesler Exp $
9
package org.jboss.webservice.util;
10
11 // $Id: DOMUtils.java,v 1.1.4.1 2005/06/14 13:28:18 tdiesler Exp $
12

13 import org.jboss.logging.Logger;
14 import org.w3c.dom.Element JavaDoc;
15 import org.w3c.dom.Node JavaDoc;
16 import org.w3c.dom.NodeList JavaDoc;
17
18 import javax.xml.namespace.QName JavaDoc;
19
20 /**
21  * A utility class for common w3c DOM operations.
22  *
23  * @author Thomas.Diesler@jboss.org
24  * @since 15-May-2004
25  */

26 public final class DOMUtils
27 {
28    // provide logging
29
private static final Logger log = Logger.getLogger(DOMUtils.class);
30
31    // hide the constructor
32
private DOMUtils()
33    {
34    }
35
36    /**
37     * Get the value from the given attribute
38     *
39     * @return null if the attribute value is empty or not present
40     */

41    public static String JavaDoc getAttributeValue(Element JavaDoc el, String JavaDoc attrName)
42    {
43       String JavaDoc attr = el.getAttribute(attrName);
44
45       if ("".equals(attr))
46          attr = null;
47
48       return attr;
49    }
50
51    /**
52     * Get the qname value from the given attribute
53     */

54    public static QName JavaDoc getAttributeValueAsQName(Element JavaDoc el, String JavaDoc attrName)
55    {
56       QName JavaDoc qname = null;
57
58       String JavaDoc attr = getAttributeValue(el, attrName);
59       if (attr != null)
60       {
61          String JavaDoc prefix = "";
62          String JavaDoc localPart = attr;
63          String JavaDoc namespaceURI = "";
64
65          int colonIndex = attr.indexOf(":");
66          if (colonIndex > 0)
67          {
68             prefix = attr.substring(0, colonIndex);
69             localPart = attr.substring(colonIndex + 1);
70
71             Element JavaDoc nsElement = el;
72             while (namespaceURI.equals("") && nsElement != null)
73             {
74                namespaceURI = nsElement.getAttribute("xmlns:" + prefix);
75                if (namespaceURI.equals(""))
76                   nsElement = getParentElement(nsElement);
77             }
78
79             if (namespaceURI.equals(""))
80                throw new IllegalArgumentException JavaDoc("Cannot find namespace uri for: " + attr);
81          }
82
83          qname = new QName JavaDoc(namespaceURI, localPart, prefix);
84       }
85
86       return qname;
87    }
88
89    /**
90     * Get the boolean value from the given attribute
91     */

92    public static boolean getAttributeValueAsBoolean(Element JavaDoc el, String JavaDoc attrName)
93    {
94       String JavaDoc attrVal = getAttributeValue(el, attrName);
95       boolean ret = "true".equalsIgnoreCase(attrVal) || "1".equalsIgnoreCase(attrVal);
96       return ret;
97    }
98
99    /**
100     * Get the boolean value from the given attribute
101     */

102    public static Integer JavaDoc getAttributeValueAsInteger(Element JavaDoc el, String JavaDoc attrName)
103    {
104       Integer JavaDoc retVal = null;
105       String JavaDoc attrVal = getAttributeValue(el, attrName);
106       if (attrVal != null)
107       {
108          retVal = new Integer JavaDoc(attrVal);
109       }
110       return retVal;
111    }
112
113    /**
114     * Gets the first child element
115     */

116    public static Element JavaDoc getFirstChildElement(Element JavaDoc el)
117    {
118       return getFirstChildElement(el, null);
119    }
120
121    /**
122     * Gets the first child element for a givenname
123     */

124    public static Element JavaDoc getFirstChildElement(Element JavaDoc el, String JavaDoc tagName)
125    {
126       Element JavaDoc childElement = null;
127       NodeList JavaDoc nlist = el.getChildNodes();
128       for (int i = 0; childElement == null && i < nlist.getLength(); i++)
129       {
130          Node JavaDoc node = nlist.item(i);
131          if (node.getNodeType() == Node.ELEMENT_NODE)
132          {
133             if (tagName == null || tagName.equals(node.getLocalName()))
134                childElement = (Element JavaDoc)node;
135          }
136       }
137       return childElement;
138    }
139
140    /**
141     * Gets parent element or null if there is none
142     */

143    public static Element JavaDoc getParentElement(Element JavaDoc el)
144    {
145       Node JavaDoc parent = el.getParentNode();
146       return (parent instanceof Element JavaDoc ? (Element JavaDoc)parent : null);
147    }
148 }
Popular Tags