001: /*
002: * Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: /*
027: * DOMUtils.java
028: *
029: * Created on May 7th 2002
030: */
031:
032: package com.sun.tools.internal.xjc.util;
033:
034: import java.util.ArrayList;
035:
036: import javax.xml.namespace.QName;
037:
038: import org.w3c.dom.DOMException;
039: import org.w3c.dom.Document;
040: import org.w3c.dom.Element;
041: import org.w3c.dom.Node;
042: import org.w3c.dom.NodeList;
043:
044: /**
045: *
046: * @author Vivek Pandey
047: * @version 1.0
048: *
049: */
050: public class DOMUtils {
051: /** Gets the fist child of the given name, or null. */
052: public static Element getFirstChildElement(Element parent,
053: String nsUri, String localPart) {
054: NodeList children = parent.getChildNodes();
055: for (int i = 0; i < children.getLength(); i++) {
056: Node item = children.item(i);
057: if (!(item instanceof Element))
058: continue;
059:
060: if (nsUri.equals(item.getNamespaceURI())
061: && localPart.equals(item.getLocalName()))
062: return (Element) item;
063: }
064: return null;
065: }
066:
067: /** Gets the child elements of the given name. */
068: public static Element[] getChildElements(Element parent,
069: String nsUri, String localPart) {
070: ArrayList a = new ArrayList();
071: NodeList children = parent.getChildNodes();
072: for (int i = 0; i < children.getLength(); i++) {
073: Node item = children.item(i);
074: if (!(item instanceof Element))
075: continue;
076:
077: if (nsUri.equals(item.getNamespaceURI())
078: && localPart.equals(item.getLocalName()))
079: a.add(item);
080: }
081: return (Element[]) a.toArray(new Element[a.size()]);
082: }
083:
084: /** Gets all the child elements. */
085: public static Element[] getChildElements(Element parent) {
086: ArrayList a = new ArrayList();
087: NodeList children = parent.getChildNodes();
088: for (int i = 0; i < children.getLength(); i++) {
089: Node item = children.item(i);
090: if (!(item instanceof Element))
091: continue;
092:
093: a.add(item);
094: }
095: return (Element[]) a.toArray(new Element[a.size()]);
096: }
097:
098: public static String getElementText(Element element)
099: throws DOMException {
100: for (Node child = element.getFirstChild(); child != null; child = child
101: .getNextSibling()) {
102: if (child.getNodeType() == Node.TEXT_NODE)
103: return child.getNodeValue();
104: }
105: return element.getNodeValue();
106: }
107:
108: public static Element getElement(Document parent, String name) {
109: NodeList children = parent.getElementsByTagName(name);
110: if (children.getLength() >= 1)
111: return (Element) children.item(0);
112: return null;
113: }
114:
115: public static Element getElement(Document parent, QName qname) {
116: NodeList children = parent.getElementsByTagNameNS(qname
117: .getNamespaceURI(), qname.getLocalPart());
118: if (children.getLength() >= 1)
119: return (Element) children.item(0);
120: return null;
121: }
122:
123: public static Element getElement(Document parent,
124: String namespaceURI, String localName) {
125: NodeList children = parent.getElementsByTagNameNS(namespaceURI,
126: localName);
127: if (children.getLength() >= 1)
128: return (Element) children.item(0);
129: return null;
130: }
131:
132: // these implementations look wrong to me, since getElementsByTagName returns
133: // all the elements in descendants, not just children.
134: //
135: // public static Element[] getChildElements(Element parent, QName qname) {
136: // NodeList children = parent.getElementsByTagNameNS(qname.uri, qname.localpart);
137: // return getElements(children);
138: // }
139: //
140: // public static Element[] getChildElements(Element parent, String namespaceURI,
141: // String localName) {
142: // NodeList children = parent.getElementsByTagNameNS(namespaceURI, localName);
143: // return getElements(children);
144: // }
145: //
146: // public static Element[] getChildElements(Element parent, String name) {
147: // NodeList children = parent.getElementsByTagName(name);
148: // return getElements(children);
149: // }
150:
151: public static Element[] getElements(NodeList children) {
152: Element[] elements = null;
153: int len = 0;
154: for (int i = 0; i < children.getLength(); ++i) {
155: if (elements == null)
156: elements = new Element[1];
157: if (elements.length == len) {
158: Element[] buf = new Element[elements.length + 1];
159: System.arraycopy(elements, 0, buf, 0, elements.length);
160: elements = buf;
161: }
162: elements[len++] = (Element) children.item(i);
163: }
164: return elements;
165: }
166: }
|