KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > j2seproject > J2SEProjectUtil


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.java.j2seproject;
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.Collection JavaDoc;
27 import java.util.List JavaDoc;
28 import javax.lang.model.element.TypeElement;
29 import org.netbeans.api.java.classpath.ClassPath;
30 import org.netbeans.api.java.platform.JavaPlatform;
31 import org.netbeans.api.java.platform.JavaPlatformManager;
32 import org.netbeans.api.java.platform.Specification;
33 import org.netbeans.api.java.source.ClasspathInfo;
34 import org.netbeans.api.java.source.ElementHandle;
35 import org.netbeans.api.java.source.SourceUtils;
36 import org.netbeans.api.project.Project;
37 import org.netbeans.modules.java.j2seproject.ui.customizer.MainClassChooser;
38 import org.openide.filesystems.FileObject;
39 import org.openide.filesystems.FileUtil;
40
41 /**
42  * Miscellaneous utilities for the j2seproject module.
43  * @author Jiri Rechtacek
44  */

45 public class J2SEProjectUtil {
46     private J2SEProjectUtil () {}
47     
48     /**
49      * Returns the property value evaluated by J2SEProject's PropertyEvaluator.
50      *
51      * @param p project
52      * @param value of property
53      * @return evaluated value of given property or null if the property not set or
54      * if the project doesn't provide AntProjectHelper
55      */

56     public static Object JavaDoc getEvaluatedProperty(Project p, String JavaDoc value) {
57         if (value == null) {
58             return null;
59         }
60         J2SEProject j2seprj = (J2SEProject) p.getLookup().lookup(J2SEProject.class);
61         if (j2seprj != null) {
62             return j2seprj.evaluator().evaluate(value);
63         } else {
64             return null;
65         }
66     }
67     
68     /** Check if the given file object represents a source with the main method.
69      *
70      * @param fo source
71      * @return true if the source contains the main method
72      */

73     public static boolean hasMainMethod(FileObject fo) {
74         // support for unit testing
75
if (MainClassChooser.unitTestingSupport_hasMainMethodResult != null) {
76             return MainClassChooser.unitTestingSupport_hasMainMethodResult.booleanValue ();
77         }
78         if (fo == null) {
79             // ??? maybe better should be thrown IAE
80
return false;
81         }
82         return !SourceUtils.getMainClasses(fo).isEmpty();
83     }
84
85         
86     public static boolean isMainClass (final String JavaDoc className, ClassPath bootPath, ClassPath compilePath, ClassPath sourcePath) {
87         ClasspathInfo cpInfo = ClasspathInfo.create(bootPath, compilePath, sourcePath);
88         return SourceUtils.isMainClass(className, cpInfo);
89     }
90   
91   
92     
93     /**
94      * Creates an URL of a classpath or sourcepath root
95      * For the existing directory it returns the URL obtained from {@link File#toUri()}
96      * For archive file it returns an URL of the root of the archive file
97      * For non existing directory it fixes the ending '/'
98      * @param root the file of a root
99      * @param offset a path relative to the root file or null (eg. src/ for jar:file:///lib.jar!/src/)"
100      * @return an URL of the root
101      * @throws MalformedURLException if the URL cannot be created
102      */

103     public static URL JavaDoc getRootURL (File JavaDoc root, String JavaDoc offset) throws MalformedURLException JavaDoc {
104         URL JavaDoc url = root.toURI().toURL();
105         if (FileUtil.isArchiveFile(url)) {
106             url = FileUtil.getArchiveRoot(url);
107         } else if (!root.exists()) {
108             url = new URL JavaDoc(url.toExternalForm() + "/"); // NOI18N
109
}
110         if (offset != null) {
111             assert offset.endsWith("/"); //NOI18N
112
url = new URL JavaDoc(url.toExternalForm() + offset); // NOI18N
113
}
114         return url;
115     }
116     
117     
118     /**
119      * Returns the active platform used by the project or null if the active
120      * project platform is broken.
121      * @param activePlatformId the name of platform used by Ant script or null
122      * for default platform.
123      * @return active {@link JavaPlatform} or null if the project's platform
124      * is broken
125      */

126     public static JavaPlatform getActivePlatform (final String JavaDoc activePlatformId) {
127         final JavaPlatformManager pm = JavaPlatformManager.getDefault();
128         if (activePlatformId == null) {
129             return pm.getDefaultPlatform();
130         }
131         else {
132             JavaPlatform[] installedPlatforms = pm.getPlatforms(null, new Specification ("j2se",null)); //NOI18N
133
for (int i=0; i<installedPlatforms.length; i++) {
134                 String JavaDoc antName = (String JavaDoc) installedPlatforms[i].getProperties().get("platform.ant.name"); //NOI18N
135
if (antName != null && antName.equals(activePlatformId)) {
136                     return installedPlatforms[i];
137                 }
138             }
139             return null;
140         }
141     }
142 }
143
Popular Tags