KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > projectimport > jbuilder > parsing > Util


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.projectimport.jbuilder.parsing;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.List JavaDoc;
24 import org.w3c.dom.Element JavaDoc;
25 import org.w3c.dom.Node JavaDoc;
26 import org.w3c.dom.NodeList JavaDoc;
27 import org.w3c.dom.Text JavaDoc;
28
29 /**
30  * Utility methods for the module.
31  */

32 class Util {
33
34     private Util() {}
35
36
37     // COPIED FROM org.netbeans.modules.project.ant:
38
// (except for namespace == null support in findElement)
39
// (and support for comments in findSubElements)
40

41     /**
42      * Search for an XML element in the direct children of a parent.
43      * DOM provides a similar method but it does a recursive search
44      * which we do not want. It also gives a node list and we want
45      * only one result.
46      * @param parent a parent element
47      * @param name the intended local name
48      * @param namespace the intended namespace (or null)
49      * @return the one child element with that name, or null if none or more than one
50      */

51     public static Element JavaDoc findElement(Element JavaDoc parent, String JavaDoc name, String JavaDoc namespace) {
52         Element JavaDoc result = null;
53         NodeList JavaDoc l = parent.getChildNodes();
54         for (int i = 0; i < l.getLength(); i++) {
55             if (l.item(i).getNodeType() == Node.ELEMENT_NODE) {
56                 Element JavaDoc el = (Element JavaDoc)l.item(i);
57                 if ((namespace == null && name.equals(el.getTagName())) ||
58                     (namespace != null && name.equals(el.getLocalName()) &&
59                                           namespace.equals(el.getNamespaceURI()))) {
60                     if (result == null) {
61                         result = el;
62                     } else {
63                         return null;
64                     }
65                 }
66             }
67         }
68         return result;
69     }
70     
71     /**
72      * Extract nested text from an element.
73      * Currently does not handle coalescing text nodes, CDATA sections, etc.
74      * @param parent a parent element
75      * @return the nested text, or null if none was found
76      */

77     public static String JavaDoc findText(Element JavaDoc parent) {
78         NodeList JavaDoc l = parent.getChildNodes();
79         for (int i = 0; i < l.getLength(); i++) {
80             if (l.item(i).getNodeType() == Node.TEXT_NODE) {
81                 Text JavaDoc text = (Text JavaDoc)l.item(i);
82                 return text.getNodeValue();
83             }
84         }
85         return null;
86     }
87     
88     /**
89      * Find all direct child elements of an element.
90      * More useful than {@link Element#getElementsByTagNameNS} because it does
91      * not recurse into recursive child elements.
92      * Children which are all-whitespace text nodes or comments are ignored; others cause
93      * an exception to be thrown.
94      * @param parent a parent element in a DOM tree
95      * @return a list of direct child elements (may be empty)
96      * @throws IllegalArgumentException if there are non-element children besides whitespace
97      */

98     public static List JavaDoc/*<Element>*/ findSubElements(Element JavaDoc parent) throws IllegalArgumentException JavaDoc {
99         NodeList JavaDoc l = parent.getChildNodes();
100         List JavaDoc/*<Element>*/ elements = new ArrayList JavaDoc(l.getLength());
101         for (int i = 0; i < l.getLength(); i++) {
102             Node JavaDoc n = l.item(i);
103             if (n.getNodeType() == Node.ELEMENT_NODE) {
104                 elements.add((Element JavaDoc)n);
105             } else if (n.getNodeType() == Node.TEXT_NODE) {
106                 String JavaDoc text = ((Text JavaDoc)n).getNodeValue();
107                 if (text.trim().length() > 0) {
108                     throw new IllegalArgumentException JavaDoc("non-ws text encountered in " + parent + ": " + text); // NOI18N
109
}
110             } else if (n.getNodeType() == Node.COMMENT_NODE) {
111                 // OK, ignore
112
} else {
113                 throw new IllegalArgumentException JavaDoc("unexpected non-element child of " + parent + ": " + n); // NOI18N
114
}
115         }
116         return elements;
117     }
118     
119 }
120
Popular Tags