KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > clientproject > AppClientProjectUtil


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.j2ee.clientproject;
21
22 import java.io.File JavaDoc;
23 import java.net.MalformedURLException JavaDoc;
24 import java.net.URL JavaDoc;
25 import org.netbeans.api.java.classpath.ClassPath;
26 import org.netbeans.api.java.platform.JavaPlatform;
27 import org.netbeans.api.java.platform.JavaPlatformManager;
28 import org.netbeans.api.java.platform.Specification;
29 import org.netbeans.api.java.source.ClasspathInfo;
30 import org.netbeans.api.java.source.SourceUtils;
31 import org.netbeans.api.project.Project;
32 import org.netbeans.modules.j2ee.clientproject.ui.customizer.MainClassChooser;
33 import org.openide.filesystems.FileObject;
34 import org.openide.filesystems.FileUtil;
35
36 /**
37  * Miscellaneous utilities for the j2seproject module.
38  * @author Jiri Rechtacek
39  */

40 public class AppClientProjectUtil {
41
42     private AppClientProjectUtil () {}
43     
44     /**
45      * Returns the property value evaluated by AppClientProject's PropertyEvaluator.
46      *
47      *
48      * @param p project
49      * @param value of property
50      * @return evaluated value of given property or null if the property not set or
51      * if the project doesn't provide AntProjectHelper
52      */

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

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

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

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