001: /*
002: * Copyright 2001-2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package samples.addr;
018:
019: import org.w3c.dom.Attr;
020: import org.w3c.dom.CharacterData;
021: import org.w3c.dom.Element;
022: import org.w3c.dom.Node;
023: import org.w3c.dom.NodeList;
024:
025: /**
026: * @author Matthew J. Duftler
027: * @author Sanjiva Weerawarana
028: */
029: public class DOMUtils {
030: /**
031: * The namespaceURI represented by the prefix <code>xmlns</code>.
032: */
033: private static String NS_URI_XMLNS = "http://www.w3.org/2000/xmlns/";
034:
035: /**
036: * Returns the value of an attribute of an element. Returns null
037: * if the attribute is not found (whereas Element.getAttribute
038: * returns "" if an attrib is not found).
039: *
040: * @param el Element whose attrib is looked for
041: * @param attrName name of attribute to look for
042: * @return the attribute value
043: */
044: static public String getAttribute(Element el, String attrName) {
045: String sRet = null;
046: Attr attr = el.getAttributeNode(attrName);
047:
048: if (attr != null) {
049: sRet = attr.getValue();
050: }
051: return sRet;
052: }
053:
054: /**
055: * Returns the value of an attribute of an element. Returns null
056: * if the attribute is not found (whereas Element.getAttributeNS
057: * returns "" if an attrib is not found).
058: *
059: * @param el Element whose attrib is looked for
060: * @param namespaceURI namespace URI of attribute to look for
061: * @param localPart local part of attribute to look for
062: * @return the attribute value
063: */
064: static public String getAttributeNS(Element el,
065: String namespaceURI, String localPart) {
066: String sRet = null;
067: Attr attr = el.getAttributeNodeNS(namespaceURI, localPart);
068:
069: if (attr != null) {
070: sRet = attr.getValue();
071: }
072:
073: return sRet;
074: }
075:
076: /**
077: * Concat all the text and cdata node children of this elem and return
078: * the resulting text.
079: *
080: * @param parentEl the element whose cdata/text node values are to
081: * be combined.
082: * @return the concatanated string.
083: */
084: static public String getChildCharacterData(Element parentEl) {
085: if (parentEl == null) {
086: return null;
087: }
088: Node tempNode = parentEl.getFirstChild();
089: StringBuffer strBuf = new StringBuffer();
090: CharacterData charData;
091:
092: while (tempNode != null) {
093: switch (tempNode.getNodeType()) {
094: case Node.TEXT_NODE:
095: case Node.CDATA_SECTION_NODE:
096: charData = (CharacterData) tempNode;
097: strBuf.append(charData.getData());
098: break;
099: }
100: tempNode = tempNode.getNextSibling();
101: }
102: return strBuf.toString();
103: }
104:
105: /**
106: * Return the first child element of the given element. Null if no
107: * children are found.
108: *
109: * @param elem Element whose child is to be returned
110: * @return the first child element.
111: */
112: public static Element getFirstChildElement(Element elem) {
113: for (Node n = elem.getFirstChild(); n != null; n = n
114: .getNextSibling()) {
115: if (n.getNodeType() == Node.ELEMENT_NODE) {
116: return (Element) n;
117: }
118: }
119: return null;
120: }
121:
122: /**
123: * Return the next sibling element of the given element. Null if no
124: * more sibling elements are found.
125: *
126: * @param elem Element whose sibling element is to be returned
127: * @return the next sibling element.
128: */
129: public static Element getNextSiblingElement(Element elem) {
130: for (Node n = elem.getNextSibling(); n != null; n = n
131: .getNextSibling()) {
132: if (n.getNodeType() == Node.ELEMENT_NODE) {
133: return (Element) n;
134: }
135: }
136: return null;
137: }
138:
139: /**
140: * Return the first child element of the given element which has the
141: * given attribute with the given value.
142: *
143: * @param elem the element whose children are to be searched
144: * @param attrName the attrib that must be present
145: * @param attrValue the desired value of the attribute
146: *
147: * @return the first matching child element.
148: */
149: public static Element findChildElementWithAttribute(Element elem,
150: String attrName, String attrValue) {
151: for (Node n = elem.getFirstChild(); n != null; n = n
152: .getNextSibling()) {
153: if (n.getNodeType() == Node.ELEMENT_NODE) {
154: if (attrValue.equals(DOMUtils.getAttribute((Element) n,
155: attrName))) {
156: return (Element) n;
157: }
158: }
159: }
160: return null;
161: }
162:
163: /**
164: * Count number of children of a certain type of the given element.
165: *
166: * @param elem the element whose kids are to be counted
167: *
168: * @return the number of matching kids.
169: */
170: public static int countKids(Element elem, short nodeType) {
171: int nkids = 0;
172: for (Node n = elem.getFirstChild(); n != null; n = n
173: .getNextSibling()) {
174: if (n.getNodeType() == nodeType) {
175: nkids++;
176: }
177: }
178: return nkids;
179: }
180:
181: /**
182: * Given a prefix and a node, return the namespace URI that the prefix
183: * has been associated with. This method is useful in resolving the
184: * namespace URI of attribute values which are being interpreted as
185: * QNames. If prefix is null, this method will return the default
186: * namespace.
187: *
188: * @param context the starting node (looks up recursively from here)
189: * @param prefix the prefix to find an xmlns:prefix=uri for
190: *
191: * @return the namespace URI or null if not found
192: */
193: public static String getNamespaceURIFromPrefix(Node context,
194: String prefix) {
195: short nodeType = context.getNodeType();
196: Node tempNode = null;
197:
198: switch (nodeType) {
199: case Node.ATTRIBUTE_NODE: {
200: tempNode = ((Attr) context).getOwnerElement();
201: break;
202: }
203: case Node.ELEMENT_NODE: {
204: tempNode = context;
205: break;
206: }
207: default: {
208: tempNode = context.getParentNode();
209: break;
210: }
211: }
212:
213: while (tempNode != null
214: && tempNode.getNodeType() == Node.ELEMENT_NODE) {
215: Element tempEl = (Element) tempNode;
216: String namespaceURI = (prefix == null) ? getAttribute(
217: tempEl, "xmlns") : getAttributeNS(tempEl,
218: NS_URI_XMLNS, prefix);
219:
220: if (namespaceURI != null) {
221: return namespaceURI;
222: } else {
223: tempNode = tempEl.getParentNode();
224: }
225: }
226:
227: return null;
228: }
229:
230: public static Element getElementByID(Element el, String id) {
231: if (el == null)
232: return null;
233: String this Id = el.getAttribute("id");
234: if (id.equals(this Id))
235: return el;
236:
237: NodeList list = el.getChildNodes();
238: for (int i = 0; i < list.getLength(); i++) {
239: Node node = list.item(i);
240: if (node instanceof Element) {
241: Element ret = getElementByID((Element) node, id);
242: if (ret != null)
243: return ret;
244: }
245: }
246:
247: return null;
248: }
249: }
|