KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > ruby > railsprojects > 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.ruby.railsprojects;
21
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.Arrays JavaDoc;
26 import java.util.HashSet JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.Set JavaDoc;
30 import org.netbeans.api.project.Project;
31 import org.netbeans.api.project.ProjectManager;
32 import org.netbeans.api.project.ProjectUtils;
33 import org.netbeans.api.queries.CollocationQuery;
34 import org.netbeans.spi.project.AuxiliaryConfiguration;
35 import org.netbeans.modules.ruby.spi.project.support.rake.RakeProjectHelper;
36 import org.netbeans.modules.ruby.spi.project.support.rake.PropertyEvaluator;
37 import org.netbeans.modules.ruby.spi.project.support.rake.PropertyUtils;
38 import org.openide.ErrorManager;
39 import org.openide.filesystems.FileObject;
40 import org.openide.filesystems.FileUtil;
41 import org.w3c.dom.Element JavaDoc;
42 import org.w3c.dom.Node JavaDoc;
43 import org.w3c.dom.NodeList JavaDoc;
44 import org.w3c.dom.Text JavaDoc;
45
46 /**
47  * This was originally org.netbeans.modules.ant.freeform.spi.support.Util
48  * Miscellaneous helper methods.
49  * @author Jesse Glick, David Konecny
50  */

51 public class Util {
52     
53     private Util() {}
54     
55     // XXX XML methods copied from ant/project... make a general API of these instead?
56

57     /**
58      * Search for an XML element in the direct children of a parent.
59      * DOM provides a similar method but it does a recursive search
60      * which we do not want. It also gives a node list and we want
61      * only one result.
62      * @param parent a parent element
63      * @param name the intended local name
64      * @param namespace the intended namespace
65      * @return the one child element with that name, or null if none or more than one
66      */

67     public static Element JavaDoc findElement(Element JavaDoc parent, String JavaDoc name, String JavaDoc namespace) {
68         Element JavaDoc result = null;
69         NodeList JavaDoc l = parent.getChildNodes();
70         for (int i = 0; i < l.getLength(); i++) {
71             if (l.item(i).getNodeType() == Node.ELEMENT_NODE) {
72                 Element JavaDoc el = (Element JavaDoc)l.item(i);
73                 if (name.equals(el.getLocalName()) && namespace.equals(el.getNamespaceURI())) {
74                     if (result == null) {
75                         result = el; // XXX Uhm, why don't we just return it???
76
} else {
77                         return null;
78                     }
79                 }
80             }
81         }
82         return result;
83     }
84     
85     /**
86      * Extract nested text from an element.
87      * Currently does not handle coalescing text nodes, CDATA sections, etc.
88      * @param parent a parent element
89      * @return the nested text, or null if none was found
90      */

91     public static String JavaDoc findText(Element JavaDoc parent) {
92         NodeList JavaDoc l = parent.getChildNodes();
93         for (int i = 0; i < l.getLength(); i++) {
94             if (l.item(i).getNodeType() == Node.TEXT_NODE) {
95                 Text JavaDoc text = (Text JavaDoc)l.item(i);
96                 return text.getNodeValue();
97             }
98         }
99         return null;
100     }
101     
102     /**
103      * Find all direct child elements of an element.
104      * More useful than {@link Element#getElementsByTagNameNS} because it does
105      * not recurse into recursive child elements.
106      * Children which are all-whitespace text nodes are ignored; others cause
107      * an exception to be thrown.
108      * @param parent a parent element in a DOM tree
109      * @return a list of direct child elements (may be empty)
110      * @throws IllegalArgumentException if there are non-element children besides whitespace
111      */

112     public static List JavaDoc<Element JavaDoc> findSubElements(Element JavaDoc parent) throws IllegalArgumentException JavaDoc {
113         NodeList JavaDoc l = parent.getChildNodes();
114         List JavaDoc<Element JavaDoc> elements = new ArrayList JavaDoc<Element JavaDoc>(l.getLength());
115         for (int i = 0; i < l.getLength(); i++) {
116             Node JavaDoc n = l.item(i);
117             if (n.getNodeType() == Node.ELEMENT_NODE) {
118                 elements.add((Element JavaDoc)n);
119             } else if (n.getNodeType() == Node.TEXT_NODE) {
120                 String JavaDoc text = ((Text JavaDoc)n).getNodeValue();
121                 if (text.trim().length() > 0) {
122                     throw new IllegalArgumentException JavaDoc("non-ws text encountered in " + parent + ": " + text); // NOI18N
123
}
124             } else if (n.getNodeType() == Node.COMMENT_NODE) {
125                 // skip
126
} else {
127                 throw new IllegalArgumentException JavaDoc("unexpected non-element child of " + parent + ": " + n); // NOI18N
128
}
129         }
130         return elements;
131     }
132
133     /**
134      * Finds AuxiliaryConfiguration for the given project helper. The method
135      * finds project associated with the helper and searches
136      * AuxiliaryConfiguration in project's lookup.
137      *
138      * @param helper instance of project's RakeProjectHelper
139      * @return project's AuxiliaryConfiguration
140      */

141     public static AuxiliaryConfiguration getAuxiliaryConfiguration(RakeProjectHelper helper) {
142         try {
143             Project p = ProjectManager.getDefault().findProject(helper.getProjectDirectory());
144             AuxiliaryConfiguration aux = p.getLookup().lookup(AuxiliaryConfiguration.class);
145             assert aux != null;
146             return aux;
147         } catch (IOException JavaDoc e) {
148             ErrorManager.getDefault().notify(e);
149             return null;
150         }
151     }
152
153 // /**
154
// * Relativize given file against the original project and if needed use
155
// * ${project.dir} property as base. If file cannot be relativized
156
// * the absolute filepath is returned.
157
// * @param projectBase original project base folder
158
// * @param freeformBase Freeform project base folder
159
// * @param location location to relativize
160
// * @return text suitable for storage in project.xml representing given location
161
// */
162
// public static String relativizeLocation(File projectBase, File freeformBase, File location) {
163
// if (CollocationQuery.areCollocated(projectBase, location)) {
164
// if (projectBase.equals(freeformBase)) {
165
// return PropertyUtils.relativizeFile(projectBase, location);
166
// } else if (projectBase.equals(location) && ProjectConstants.PROJECT_LOCATION_PREFIX.endsWith("/")) { // NOI18N
167
// return ProjectConstants.PROJECT_LOCATION_PREFIX.substring(0, ProjectConstants.PROJECT_LOCATION_PREFIX.length() - 1);
168
// } else {
169
// return ProjectConstants.PROJECT_LOCATION_PREFIX + PropertyUtils.relativizeFile(projectBase, location);
170
// }
171
// } else {
172
// return location.getAbsolutePath();
173
// }
174
// }
175

176     /**
177      * Resolve given string value (e.g. "${project.dir}/lib/lib1.jar")
178      * to a File.
179      * @param evaluator evaluator to use for properties resolving
180      * @param freeformProjectBase freeform project base folder
181      * @param val string to be resolved as file
182      * @return resolved File or null if file could not be resolved
183      */

184     public static File JavaDoc resolveFile(PropertyEvaluator evaluator, File JavaDoc freeformProjectBase, String JavaDoc val) {
185         String JavaDoc location = evaluator.evaluate(val);
186         if (location == null) {
187             return null;
188         }
189         return PropertyUtils.resolveFile(freeformProjectBase, location);
190     }
191
192 // /**
193
// * Returns location of original project base folder. The location can be dirrerent
194
// * from NetBeans metadata project folder.
195
// * @param helper RakeProjectHelper associated with the project
196
// * @param evaluator PropertyEvaluator associated with the project
197
// * @return location of original project base folder
198
// */
199
// public static File getProjectLocation(RakeProjectHelper helper, PropertyEvaluator evaluator) {
200
// //assert ProjectManager.mutex().isReadAccess() || ProjectManager.mutex().isWriteAccess();
201
// String loc = evaluator.getProperty(ProjectConstants.PROP_PROJECT_LOCATION);
202
// if (loc != null) {
203
// return helper.resolveFile(loc);
204
// } else {
205
// return FileUtil.toFile(helper.getProjectDirectory());
206
// }
207
// }
208

209     /**
210      * Append child element to the correct position according to given
211      * order.
212      * @param parent parent to which the child will be added
213      * @param el element to be added
214      * @param order order of the elements which must be followed
215      */

216     public static void appendChildElement(Element JavaDoc parent, Element JavaDoc el, String JavaDoc[] order) {
217         Element JavaDoc insertBefore = null;
218         List JavaDoc l = Arrays.asList(order);
219         int index = l.indexOf(el.getLocalName());
220         assert index != -1 : el.getLocalName()+" was not found in "+l; // NOI18N
221
Iterator JavaDoc it = Util.findSubElements(parent).iterator();
222         while (it.hasNext()) {
223             Element JavaDoc e = (Element JavaDoc)it.next();
224             int index2 = l.indexOf(e.getLocalName());
225             assert index2 != -1 : e.getLocalName()+" was not found in "+l; // NOI18N
226
if (index2 > index) {
227                 insertBefore = e;
228                 break;
229             }
230         }
231         parent.insertBefore(el, insertBefore);
232     }
233     
234 // /**Get the "default" (user-specified) ant script for the given freeform project.
235
// * Please note that this method may return <code>null</code> if there is no such script.
236
// *
237
// * WARNING: This method is there only for a limited set of usecases like the profiler plugin.
238
// * It should not be used by the freeform project natures.
239
// *
240
// * @param prj the freeform project
241
// * @return the "default" ant script or <code>null</code> if there is no such a script
242
// * @throws IllegalArgumentException if the passed project is not a freeform project.
243
// */
244
// public static FileObject getDefaultAntScript(Project prj) throws IllegalArgumentException {
245
// ProjectAccessor accessor = prj.getLookup().lookup(ProjectAccessor.class);
246
//
247
// if (accessor == null) {
248
// throw new IllegalArgumentException("Only FreeformProjects are supported.");
249
// }
250
//
251
// return FreeformProjectGenerator.getAntScript(accessor.getHelper(), accessor.getEvaluator());
252
// }
253

254 }
255
Popular Tags