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: package com.sun.tools.xjc.reader.dtd.bindinfo;
038:
039: import java.util.ArrayList;
040: import java.util.List;
041:
042: import org.w3c.dom.Element;
043: import org.w3c.dom.Node;
044: import org.w3c.dom.NodeList;
045:
046: /**
047: * @author Kohsuke Kawaguchi
048: */
049: public final class DOMUtil {
050: final static String getAttribute(Element e, String attName) {
051: if (e.getAttributeNode(attName) == null)
052: return null;
053: return e.getAttribute(attName);
054: }
055:
056: public static String getAttribute(Element e, String nsUri,
057: String local) {
058: if (e.getAttributeNodeNS(nsUri, local) == null)
059: return null;
060: return e.getAttributeNS(nsUri, local);
061: }
062:
063: public static Element getElement(Element e, String nsUri,
064: String localName) {
065: NodeList l = e.getChildNodes();
066: for (int i = 0; i < l.getLength(); i++) {
067: Node n = l.item(i);
068: if (n.getNodeType() == Node.ELEMENT_NODE) {
069: Element r = (Element) n;
070: if (equals(r.getLocalName(), localName)
071: && equals(fixNull(r.getNamespaceURI()), nsUri))
072: return r;
073: }
074: }
075: return null;
076: }
077:
078: /**
079: * Used for defensive string comparisons, as many DOM methods often return null
080: * depending on how they are created.
081: */
082: private static boolean equals(String a, String b) {
083: if (a == b)
084: return true;
085: if (a == null || b == null)
086: return false;
087: return a.equals(b);
088: }
089:
090: /**
091: * DOM API returns null for the default namespace whereas it should return "".
092: */
093: private static String fixNull(String s) {
094: if (s == null)
095: return "";
096: else
097: return s;
098: }
099:
100: public static Element getElement(Element e, String localName) {
101: return getElement(e, "", localName);
102: }
103:
104: public static List<Element> getChildElements(Element e) {
105: List<Element> r = new ArrayList<Element>();
106: NodeList l = e.getChildNodes();
107: for (int i = 0; i < l.getLength(); i++) {
108: Node n = l.item(i);
109: if (n.getNodeType() == Node.ELEMENT_NODE)
110: r.add((Element) n);
111: }
112: return r;
113: }
114:
115: public static List<Element> getChildElements(Element e,
116: String localName) {
117: List<Element> r = new ArrayList<Element>();
118: NodeList l = e.getChildNodes();
119: for (int i = 0; i < l.getLength(); i++) {
120: Node n = l.item(i);
121: if (n.getNodeType() == Node.ELEMENT_NODE) {
122: Element c = (Element) n;
123: if (c.getLocalName().equals(localName))
124: r.add(c);
125: }
126: }
127: return r;
128: }
129: }
|