KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > ruby > rubyproject > RubyProjectUtil


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.rubyproject;
21
22 import java.io.File JavaDoc;
23 import java.net.MalformedURLException JavaDoc;
24 import java.net.URL JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.List JavaDoc;
27 import org.netbeans.modules.ruby.rubyproject.api.RubyInstallation;
28 import org.netbeans.api.project.Project;
29 import org.openide.filesystems.FileObject;
30 import org.openide.filesystems.FileUtil;
31
32 /**
33  * Miscellaneous utilities for the j2seproject module.
34  * @author Jiri Rechtacek
35  */

36 public class RubyProjectUtil {
37     private RubyProjectUtil () {}
38     
39     /**
40      * Returns the property value evaluated by RubyProject's PropertyEvaluator.
41      *
42      * @param p project
43      * @param value of property
44      * @return evaluated value of given property or null if the property not set or
45      * if the project doesn't provide RakeProjectHelper
46      */

47     public static Object JavaDoc getEvaluatedProperty(Project p, String JavaDoc value) {
48         if (value == null) {
49             return null;
50         }
51         RubyProject j2seprj = (RubyProject) p.getLookup().lookup(RubyProject.class);
52         if (j2seprj != null) {
53             return j2seprj.evaluator().evaluate(value);
54         } else {
55             return null;
56         }
57     }
58     
59     /** Check if the given file object represents a source with the main method.
60      *
61      * @param fo source
62      * @return true if the source contains the main method
63      */

64     public static boolean hasMainMethod(FileObject fo) {
65         // support for unit testing
66

67 // if (MainClassChooser.unitTestingSupport_hasMainMethodResult != null) {
68
// return MainClassChooser.unitTestingSupport_hasMainMethodResult.booleanValue ();
69
// }
70
// if (fo == null) {
71
// // ??? maybe better should be thrown IAE
72
// return false;
73
// }
74
//
75
// boolean has = false;
76
// JavaModel.getJavaRepository ().beginTrans (false);
77
//
78
// try {
79
// JavaModel.setClassPath(fo);
80
// Resource res = JavaModel.getResource (fo);
81
// assert res != null : "Resource found for FileObject " + fo;
82
// has = !res.getMain().isEmpty();
83
// } finally {
84
// JavaModel.getJavaRepository ().endTrans ();
85
// }
86
// return has;
87

88     return true;
89     }
90
91     /** Returns list of FQN of classes contains the main method.
92      *
93      * @param roots the classpath roots of source to start find
94      * @return list of names of classes, e.g, [sample.project1.Hello, sample.project.app.MainApp]
95      */

96     public static List JavaDoc/*String*/ getMainClasses (FileObject[] roots) {
97         List JavaDoc result = new ArrayList JavaDoc ();
98         for (int i=0; i<roots.length; i++) {
99             getMainClasses(roots[i], result);
100         }
101         return result;
102     }
103
104     public static void getAllScripts(String JavaDoc prefix, FileObject sourcesRoot, List JavaDoc/*<String>*/ result) {
105         FileObject children[] = sourcesRoot.getChildren();
106         if (!"".equals(prefix)) {
107             prefix += "/";
108             //prefix += ".";
109
}
110         for (int i = 0; i < children.length; i++) {
111             if (children[i].isData()) {
112                 if (children[i].getMIMEType().equals(RubyInstallation.RUBY_MIME_TYPE)) {
113                     result.add(prefix + children[i].getNameExt());
114                 }
115             }
116             if (children[i].isFolder()) {
117                 getAllScripts(prefix + children[i].getNameExt(), children[i], result);
118             }
119         }
120     }
121     
122     
123     /** Returns list of FQN of classes contains the main method.
124      *
125      * @param root the root of source to start find
126      * @param addInto list of names of classes, e.g, [sample.project1.Hello, sample.project.app.MainApp]
127      */

128     private static void getMainClasses (FileObject root, List JavaDoc/*<String>*/ addInto) {
129           getAllScripts("", root, addInto);
130         
131 // JMManager.getManager().waitScanFinished();
132
// JavaModel.getJavaRepository ().beginTrans (false);
133
// try {
134
// JavaModelPackage mofPackage = JavaModel.getJavaExtent(root);
135
// ClassIndex index = ClassIndex.getIndex (mofPackage);
136
// //Resource[] res = index.findResourcesForIdentifier ("main"); // NOI18N
137
// Collection col = index.findResourcesForIdent ("main"); // NOI18N
138
// Object[] arr = col.toArray ();
139
//
140
// if (arr == null) {
141
// // no main classes
142
// return;
143
// }
144
//
145
// for (int i = 0; i < arr.length; i++) {
146
// Resource res = (Resource)arr[i];
147
// Iterator mainIt=res.getMain().iterator();
148
//
149
// while (mainIt.hasNext()) {
150
// JavaClass jcls=(JavaClass)mainIt.next();
151
//
152
// addInto.add(jcls.getName());
153
// }
154
// }
155
// } finally {
156
// JavaModel.getJavaRepository ().endTrans (false);
157
// }
158
}
159     
160     /** Returns if the given class name exists under the sources root and
161      * it's a main class.
162      *
163      * @param className FQN of class
164      * @param roots roots of sources
165      * @return true if the class name exists and it's a main class
166      */

167     public static boolean isMainClass(String JavaDoc className, FileObject[] roots) {
168         // All classes are considered main classes in Ruby (the intent of this
169
// method is to return "executable classes", and all Ruby files are executable
170

171         // However, we should check that the file at least exists.
172
if (roots == null) {
173             return false;
174         }
175
176         for (FileObject root : roots) {
177             if (root.getFileObject(className) != null) {
178                 return true;
179             }
180         }
181         return false;
182     }
183   
184   
185     
186     /**
187      * Creates an URL of a classpath or sourcepath root
188      * For the existing directory it returns the URL obtained from {@link File#toUri()}
189      * For archive file it returns an URL of the root of the archive file
190      * For non existing directory it fixes the ending '/'
191      * @param root the file of a root
192      * @param offset a path relative to the root file or null (eg. src/ for jar:file:///lib.jar!/src/)"
193      * @return an URL of the root
194      * @throws MalformedURLException if the URL cannot be created
195      */

196     public static URL JavaDoc getRootURL (File JavaDoc root, String JavaDoc offset) throws MalformedURLException JavaDoc {
197         URL JavaDoc url = root.toURI().toURL();
198         if (FileUtil.isArchiveFile(url)) {
199             url = FileUtil.getArchiveRoot(url);
200         } else if (!root.exists()) {
201             url = new URL JavaDoc(url.toExternalForm() + "/"); // NOI18N
202
}
203         if (offset != null) {
204             assert offset.endsWith("/"); //NOI18N
205
url = new URL JavaDoc(url.toExternalForm() + offset); // NOI18N
206
}
207         return url;
208     }
209 }
210
Popular Tags