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
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.ruby.rubyproject;
043:
044: import java.io.File;
045: import java.net.MalformedURLException;
046: import java.net.URL;
047: import java.util.ArrayList;
048: import java.util.List;
049: import org.netbeans.api.ruby.platform.RubyInstallation;
050: import org.netbeans.api.project.Project;
051: import org.openide.filesystems.FileObject;
052: import org.openide.filesystems.FileUtil;
053:
054: /**
055: * Miscellaneous utilities for the j2seproject module.
056: * @author Jiri Rechtacek
057: */
058: public class RubyProjectUtil {
059: private RubyProjectUtil() {
060: }
061:
062: /**
063: * Returns the property value evaluated by RubyProject's PropertyEvaluator.
064: *
065: * @param p project
066: * @param value of property
067: * @return evaluated value of given property or null if the property not set or
068: * if the project doesn't provide RakeProjectHelper
069: */
070: public static Object getEvaluatedProperty(Project p, String value) {
071: if (value == null) {
072: return null;
073: }
074: RubyProject j2seprj = p.getLookup().lookup(RubyProject.class);
075: if (j2seprj != null) {
076: return j2seprj.evaluator().evaluate(value);
077: } else {
078: return null;
079: }
080: }
081:
082: /** Check if the given file object represents a source with the main method.
083: *
084: * @param fo source
085: * @return true if the source contains the main method
086: */
087: public static boolean hasMainMethod(FileObject fo) {
088: // support for unit testing
089:
090: // if (MainClassChooser.unitTestingSupport_hasMainMethodResult != null) {
091: // return MainClassChooser.unitTestingSupport_hasMainMethodResult.booleanValue ();
092: // }
093: // if (fo == null) {
094: // // ??? maybe better should be thrown IAE
095: // return false;
096: // }
097: //
098: // boolean has = false;
099: // JavaModel.getJavaRepository ().beginTrans (false);
100: //
101: // try {
102: // JavaModel.setClassPath(fo);
103: // Resource res = JavaModel.getResource (fo);
104: // assert res != null : "Resource found for FileObject " + fo;
105: // has = !res.getMain().isEmpty();
106: // } finally {
107: // JavaModel.getJavaRepository ().endTrans ();
108: // }
109: // return has;
110:
111: return true;
112: }
113:
114: /** Returns list of FQN of classes contains the main method.
115: *
116: * @param roots the classpath roots of source to start find
117: * @return list of names of classes, e.g, [sample.project1.Hello, sample.project.app.MainApp]
118: */
119: public static List<String> getMainClasses(FileObject[] roots) {
120: List<String> result = new ArrayList<String>();
121: for (int i = 0; i < roots.length; i++) {
122: getMainClasses(roots[i], result);
123: }
124: return result;
125: }
126:
127: public static void getAllScripts(String prefix,
128: FileObject sourcesRoot, List<String> result) {
129: FileObject children[] = sourcesRoot.getChildren();
130: if (!"".equals(prefix)) {
131: prefix += "/";
132: //prefix += ".";
133: }
134: for (int i = 0; i < children.length; i++) {
135: if (children[i].isData()) {
136: if (children[i].getMIMEType().equals(
137: RubyInstallation.RUBY_MIME_TYPE)) {
138: result.add(prefix + children[i].getNameExt());
139: }
140: }
141: if (children[i].isFolder()) {
142: getAllScripts(prefix + children[i].getNameExt(),
143: children[i], result);
144: }
145: }
146: }
147:
148: /** Returns list of FQN of classes contains the main method.
149: *
150: * @param root the root of source to start find
151: * @param addInto list of names of classes, e.g, [sample.project1.Hello, sample.project.app.MainApp]
152: */
153: private static void getMainClasses(FileObject root,
154: List<String> addInto) {
155: getAllScripts("", root, addInto);
156:
157: // JMManager.getManager().waitScanFinished();
158: // JavaModel.getJavaRepository ().beginTrans (false);
159: // try {
160: // JavaModelPackage mofPackage = JavaModel.getJavaExtent(root);
161: // ClassIndex index = ClassIndex.getIndex (mofPackage);
162: // //Resource[] res = index.findResourcesForIdentifier ("main"); // NOI18N
163: // Collection col = index.findResourcesForIdent ("main"); // NOI18N
164: // Object[] arr = col.toArray ();
165: //
166: // if (arr == null) {
167: // // no main classes
168: // return;
169: // }
170: //
171: // for (int i = 0; i < arr.length; i++) {
172: // Resource res = (Resource)arr[i];
173: // Iterator mainIt=res.getMain().iterator();
174: //
175: // while (mainIt.hasNext()) {
176: // JavaClass jcls=(JavaClass)mainIt.next();
177: //
178: // addInto.add(jcls.getName());
179: // }
180: // }
181: // } finally {
182: // JavaModel.getJavaRepository ().endTrans (false);
183: // }
184: }
185:
186: /** Returns if the given class name exists under the sources root and
187: * it's a main class.
188: *
189: * @param className FQN of class
190: * @param roots roots of sources
191: * @return true if the class name exists and it's a main class
192: */
193: public static boolean isMainClass(String className,
194: FileObject[] roots) {
195: // All classes are considered main classes in Ruby (the intent of this
196: // method is to return "executable classes", and all Ruby files are executable
197:
198: // However, we should check that the file at least exists.
199: if (roots == null) {
200: return false;
201: }
202:
203: for (FileObject root : roots) {
204: if (root.getFileObject(className) != null) {
205: return true;
206: }
207: }
208: return false;
209: }
210:
211: /**
212: * Creates an URL of a classpath or sourcepath root
213: * For the existing directory it returns the URL obtained from {@link File#toUri()}
214: * For archive file it returns an URL of the root of the archive file
215: * For non existing directory it fixes the ending '/'
216: * @param root the file of a root
217: * @param offset a path relative to the root file or null (eg. src/ for jar:file:///lib.jar!/src/)"
218: * @return an URL of the root
219: * @throws MalformedURLException if the URL cannot be created
220: */
221: public static URL getRootURL(File root, String offset)
222: throws MalformedURLException {
223: URL url = root.toURI().toURL();
224: if (FileUtil.isArchiveFile(url)) {
225: url = FileUtil.getArchiveRoot(url);
226: } else if (!root.exists()) {
227: url = new URL(url.toExternalForm() + "/"); // NOI18N
228: }
229: if (offset != null) {
230: assert offset.endsWith("/"); //NOI18N
231: url = new URL(url.toExternalForm() + offset); // NOI18N
232: }
233: return url;
234: }
235: }
|