001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common Development
008: * and Distribution License("CDDL") (collectively, the "License"). You
009: * may not use this file except in compliance with the License. You can obtain
010: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
012: * language governing permissions and limitations under the License.
013: *
014: * When distributing the software, include this License Header Notice in each
015: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016: * Sun designates this particular file as subject to the "Classpath" exception
017: * as provided by Sun in the GPL Version 2 section of the License file that
018: * accompanied this code. If applicable, add the following below the License
019: * Header, with the fields enclosed by brackets [] replaced by your own
020: * identifying information: "Portions Copyrighted [year]
021: * [name of copyright owner]"
022: *
023: * Contributor(s):
024: *
025: * If you wish your version of this file to be governed by only the CDDL or
026: * only the GPL Version 2, indicate your decision by adding "[Contributor]
027: * elects to include this software in this distribution under the [CDDL or GPL
028: * Version 2] license." If you don't indicate a single choice of license, a
029: * recipient has the option to distribute your version of this file under
030: * either the CDDL, the GPL Version 2 or to extend the choice of license to
031: * its licensees as provided above. However, if you add GPL Version 2 code
032: * and therefore, elected the GPL Version 2 license, then the option applies
033: * only if the new code is made subject to such option by the copyright
034: * holder.
035: */
036:
037: /*
038: * DOMUtils.java
039: *
040: * Created on May 7th 2002
041: */
042:
043: package com.sun.tools.xjc.util;
044:
045: import java.util.ArrayList;
046:
047: import javax.xml.namespace.QName;
048:
049: import org.w3c.dom.DOMException;
050: import org.w3c.dom.Document;
051: import org.w3c.dom.Element;
052: import org.w3c.dom.Node;
053: import org.w3c.dom.NodeList;
054:
055: /**
056: *
057: * @author Vivek Pandey
058: * @version 1.0
059: *
060: */
061: public class DOMUtils {
062: /** Gets the fist child of the given name, or null. */
063: public static Element getFirstChildElement(Element parent,
064: String nsUri, String localPart) {
065: NodeList children = parent.getChildNodes();
066: for (int i = 0; i < children.getLength(); i++) {
067: Node item = children.item(i);
068: if (!(item instanceof Element))
069: continue;
070:
071: if (nsUri.equals(item.getNamespaceURI())
072: && localPart.equals(item.getLocalName()))
073: return (Element) item;
074: }
075: return null;
076: }
077:
078: /** Gets the child elements of the given name. */
079: public static Element[] getChildElements(Element parent,
080: String nsUri, String localPart) {
081: ArrayList a = new ArrayList();
082: NodeList children = parent.getChildNodes();
083: for (int i = 0; i < children.getLength(); i++) {
084: Node item = children.item(i);
085: if (!(item instanceof Element))
086: continue;
087:
088: if (nsUri.equals(item.getNamespaceURI())
089: && localPart.equals(item.getLocalName()))
090: a.add(item);
091: }
092: return (Element[]) a.toArray(new Element[a.size()]);
093: }
094:
095: /** Gets all the child elements. */
096: public static Element[] getChildElements(Element parent) {
097: ArrayList a = new ArrayList();
098: NodeList children = parent.getChildNodes();
099: for (int i = 0; i < children.getLength(); i++) {
100: Node item = children.item(i);
101: if (!(item instanceof Element))
102: continue;
103:
104: a.add(item);
105: }
106: return (Element[]) a.toArray(new Element[a.size()]);
107: }
108:
109: public static String getElementText(Element element)
110: throws DOMException {
111: for (Node child = element.getFirstChild(); child != null; child = child
112: .getNextSibling()) {
113: if (child.getNodeType() == Node.TEXT_NODE)
114: return child.getNodeValue();
115: }
116: return element.getNodeValue();
117: }
118:
119: public static Element getElement(Document parent, String name) {
120: NodeList children = parent.getElementsByTagName(name);
121: if (children.getLength() >= 1)
122: return (Element) children.item(0);
123: return null;
124: }
125:
126: public static Element getElement(Document parent, QName qname) {
127: NodeList children = parent.getElementsByTagNameNS(qname
128: .getNamespaceURI(), qname.getLocalPart());
129: if (children.getLength() >= 1)
130: return (Element) children.item(0);
131: return null;
132: }
133:
134: public static Element getElement(Document parent,
135: String namespaceURI, String localName) {
136: NodeList children = parent.getElementsByTagNameNS(namespaceURI,
137: localName);
138: if (children.getLength() >= 1)
139: return (Element) children.item(0);
140: return null;
141: }
142:
143: // these implementations look wrong to me, since getElementsByTagName returns
144: // all the elements in descendants, not just children.
145: //
146: // public static Element[] getChildElements(Element parent, QName qname) {
147: // NodeList children = parent.getElementsByTagNameNS(qname.uri, qname.localpart);
148: // return getElements(children);
149: // }
150: //
151: // public static Element[] getChildElements(Element parent, String namespaceURI,
152: // String localName) {
153: // NodeList children = parent.getElementsByTagNameNS(namespaceURI, localName);
154: // return getElements(children);
155: // }
156: //
157: // public static Element[] getChildElements(Element parent, String name) {
158: // NodeList children = parent.getElementsByTagName(name);
159: // return getElements(children);
160: // }
161:
162: public static Element[] getElements(NodeList children) {
163: Element[] elements = null;
164: int len = 0;
165: for (int i = 0; i < children.getLength(); ++i) {
166: if (elements == null)
167: elements = new Element[1];
168: if (elements.length == len) {
169: Element[] buf = new Element[elements.length + 1];
170: System.arraycopy(elements, 0, buf, 0, elements.length);
171: elements = buf;
172: }
173: elements[len++] = (Element) children.item(i);
174: }
175: return elements;
176: }
177: }
|