KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > project > ant > 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.project.ant;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.List JavaDoc;
24 import org.openide.ErrorManager;
25 import org.w3c.dom.Element JavaDoc;
26 import org.w3c.dom.Node JavaDoc;
27 import org.w3c.dom.NodeList JavaDoc;
28 import org.w3c.dom.Text JavaDoc;
29 import org.xml.sax.ErrorHandler JavaDoc;
30 import org.xml.sax.SAXException JavaDoc;
31 import org.xml.sax.SAXParseException JavaDoc;
32
33 /**
34  * Utilities relating to Ant projects.
35  * @author Jesse Glick
36  */

37 public class Util {
38
39     private Util() {}
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
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         int len = l.getLength();
55         for (int i = 0; i < len; i++) {
56             if (l.item(i).getNodeType() == Node.ELEMENT_NODE) {
57                 Element JavaDoc el = (Element JavaDoc)l.item(i);
58                 if (name.equals(el.getLocalName()) && namespace.equals(el.getNamespaceURI())) {
59                     if (result == null) {
60                         result = el;
61                     } else {
62                         return null;
63                     }
64                 }
65             }
66         }
67         return result;
68     }
69     
70     /**
71      * Extract nested text from an element.
72      * Currently does not handle coalescing text nodes, CDATA sections, etc.
73      * @param parent a parent element
74      * @return the nested text, or null if none was found
75      */

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

97     public static List JavaDoc<Element JavaDoc> findSubElements(Element JavaDoc parent) throws IllegalArgumentException JavaDoc {
98         NodeList JavaDoc l = parent.getChildNodes();
99         List JavaDoc<Element JavaDoc> elements = new ArrayList JavaDoc<Element JavaDoc>(l.getLength());
100         for (int i = 0; i < l.getLength(); i++) {
101             Node JavaDoc n = l.item(i);
102             if (n.getNodeType() == Node.ELEMENT_NODE) {
103                 elements.add((Element JavaDoc)n);
104             } else if (n.getNodeType() == Node.TEXT_NODE) {
105                 String JavaDoc text = ((Text JavaDoc)n).getNodeValue();
106                 if (text.trim().length() > 0) {
107                     throw new IllegalArgumentException JavaDoc("non-ws text encountered in " + parent + ": " + text); // NOI18N
108
}
109             } else if (n.getNodeType() == Node.COMMENT_NODE) {
110                 // skip
111
} else {
112                 throw new IllegalArgumentException JavaDoc("unexpected non-element child of " + parent + ": " + n); // NOI18N
113
}
114         }
115         return elements;
116     }
117     
118     /**
119      * Create an XML error handler that rethrows errors and fatal errors and logs warnings.
120      * @return a standard error handler
121      */

122     public static ErrorHandler JavaDoc defaultErrorHandler() {
123         return new ErrHandler();
124     }
125     
126     private static final class ErrHandler implements ErrorHandler JavaDoc {
127         
128         public ErrHandler() {}
129         
130         private void annotate(SAXParseException JavaDoc exception) throws SAXException JavaDoc {
131             ErrorManager.getDefault().annotate(exception, ErrorManager.UNKNOWN,
132                 "Occurred at: " + exception.getSystemId() + ":" + exception.getLineNumber(), // NOI18N
133
null, null, null);
134         }
135         
136         public void fatalError(SAXParseException JavaDoc exception) throws SAXException JavaDoc {
137             annotate(exception);
138             throw exception;
139         }
140         
141         public void error(SAXParseException JavaDoc exception) throws SAXException JavaDoc {
142             annotate(exception);
143             throw exception;
144         }
145         
146         public void warning(SAXParseException JavaDoc exception) throws SAXException JavaDoc {
147             annotate(exception);
148             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, exception);
149         }
150         
151     }
152     
153 }
154
Popular Tags