KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > j2seplatform > platformdefinition > 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 package org.netbeans.modules.java.j2seplatform.platformdefinition;
20
21 import java.text.MessageFormat JavaDoc;
22 import org.netbeans.api.java.classpath.ClassPath;
23 import org.netbeans.spi.java.classpath.support.ClassPathSupport;
24 import java.util.*;
25 import java.io.File JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.net.URL JavaDoc;
28 import java.net.MalformedURLException JavaDoc;
29 import java.util.zip.ZipFile JavaDoc;
30 import org.netbeans.api.java.platform.JavaPlatform;
31 import org.openide.ErrorManager;
32 import org.openide.filesystems.FileUtil;
33 import org.openide.filesystems.FileObject;
34 import org.openide.modules.SpecificationVersion;
35 import org.openide.util.NbBundle;
36 import org.openide.util.Utilities;
37
38 public class Util {
39
40     private Util () {
41     }
42
43     static ClassPath createClassPath(String JavaDoc classpath) {
44         StringTokenizer tokenizer = new StringTokenizer(classpath, File.pathSeparator);
45         List/*<PathResourceImplementation>*/ list = new ArrayList();
46         while (tokenizer.hasMoreTokens()) {
47             String JavaDoc item = tokenizer.nextToken();
48             File JavaDoc f = FileUtil.normalizeFile(new File JavaDoc(item));
49             URL JavaDoc url = getRootURL (f);
50             if (url!=null) {
51                 list.add(ClassPathSupport.createResource(url));
52             }
53         }
54         return ClassPathSupport.createClassPath(list);
55     }
56
57     // XXX this method could probably be removed... use standard FileUtil stuff
58
static URL JavaDoc getRootURL (final File JavaDoc f) {
59         try {
60             URL JavaDoc url = f.toURI().toURL();
61             if (FileUtil.isArchiveFile(url)) {
62                 url = FileUtil.getArchiveRoot (url);
63             }
64             else if (!f.exists()) {
65                 String JavaDoc surl = url.toExternalForm();
66                 if (!surl.endsWith("/")) {
67                     url = new URL JavaDoc (surl+"/");
68                 }
69             }
70             else if (f.isFile()) {
71                 //Slow but it will be called only in very rare cases:
72
//file on the classpath for which isArchiveFile returned false
73
try {
74                     ZipFile JavaDoc z = new ZipFile JavaDoc (f);
75                     z.close();
76                     url = FileUtil.getArchiveRoot (url);
77                 } catch (IOException JavaDoc e) {
78                     url = null;
79                 }
80             }
81             return url;
82         } catch (MalformedURLException JavaDoc e) {
83             throw new AssertionError JavaDoc(e);
84         }
85     }
86
87
88     /**
89      * Returns normalized name from display name.
90      * The normalized name should be used in the Ant properties and external files.
91      * @param displayName
92      * @return String
93      */

94     public static String JavaDoc normalizeName (String JavaDoc displayName) {
95         StringBuffer JavaDoc normalizedName = new StringBuffer JavaDoc ();
96         for (int i=0; i< displayName.length(); i++) {
97             char c = displayName.charAt(i);
98             if (Character.isJavaIdentifierPart(c) || c =='-' || c =='.') {
99                 normalizedName.append(c);
100             }
101             else {
102                 normalizedName.append('_');
103             }
104         }
105         return normalizedName.toString();
106     }
107
108     /**
109      * Returns specification version of the given platform.
110      *
111      * @return instance of SpecificationVersion representing the version; never null
112      */

113     public static SpecificationVersion getSpecificationVersion(JavaPlatform plat) {
114          String JavaDoc version = (String JavaDoc)plat.getSystemProperties().get("java.specification.version"); // NOI18N
115
if (version == null) {
116              version = "1.1";
117          }
118          return makeSpec(version);
119     }
120
121     
122     public static FileObject findTool (String JavaDoc toolName, Collection installFolders) {
123         return findTool (toolName, installFolders, null);
124     }
125
126     public static FileObject findTool (String JavaDoc toolName, Collection installFolders, String JavaDoc archFolderName) {
127         assert toolName != null;
128         for (Iterator it = installFolders.iterator(); it.hasNext();) {
129             FileObject root = (FileObject) it.next();
130             FileObject bin = root.getFileObject("bin"); //NOI18N
131
if (bin == null) {
132                 continue;
133             }
134             if (archFolderName != null) {
135                 bin = bin.getFileObject(archFolderName);
136                 if (bin == null) {
137                     continue;
138                 }
139             }
140             FileObject tool = bin.getFileObject(toolName, Utilities.isWindows() ? "exe" : null); //NOI18N
141
if (tool!= null) {
142                 return tool;
143             }
144         }
145         return null;
146     }
147
148     /**
149      * Get JRE extension JARs/ZIPs.
150      * @param extPath a native-format path for e.g. jre/lib/ext
151      * @return a native-format classpath for extension JARs and ZIPs found in it
152      */

153     public static String JavaDoc getExtensions (String JavaDoc extPath) {
154         if (extPath == null) {
155             return null;
156         }
157         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
158         StringTokenizer tk = new StringTokenizer (extPath, File.pathSeparator);
159         while (tk.hasMoreTokens()) {
160             File JavaDoc extFolder = FileUtil.normalizeFile(new File JavaDoc(tk.nextToken()));
161             File JavaDoc[] files = extFolder.listFiles();
162             if (files != null) {
163                 for (int i = 0; i < files.length; i++) {
164                     File JavaDoc f = files[i];
165                     if (!f.exists()) {
166                         //May happen, eg. broken link, it is safe to ignore it
167
//since it is an extension directory, but log it.
168
ErrorManager.getDefault().log (ErrorManager.WARNING,
169                             MessageFormat.format (NbBundle.getMessage(Util.class,"MSG_BrokenExtension"),
170                             new Object JavaDoc[] {f,extFolder}));
171                         continue;
172                     }
173                     if (Utilities.isMac() && "._.DS_Store".equals(f.getName())) { //NOI18N
174
//Ignore Apple temporary ._.DS_Store files in the lib/ext folder
175
continue;
176                     }
177                     FileObject fo = FileUtil.toFileObject(f);
178                     assert fo != null : "Must have defined a FileObject for existent file " + f;
179                     if (!FileUtil.isArchiveFile(fo)) {
180                         // #42961: Mac OS X has e.g. libmlib_jai.jnilib.
181
continue;
182                     }
183                     sb.append(File.pathSeparator);
184                     sb.append(files[i].getAbsolutePath());
185                 }
186             }
187         }
188         if (sb.length() == 0) {
189             return null;
190         }
191         return sb.substring(File.pathSeparator.length());
192     }
193
194     // copy pasted from org.openide.modules.Dependency:
195
/** Try to make a specification version from a string.
196      * Deal with errors gracefully and try to recover something from it.
197      * E.g. "1.4.0beta" is technically erroneous; correct to "1.4.0".
198      */

199     private static SpecificationVersion makeSpec(String JavaDoc vers) {
200         if (vers != null) {
201             try {
202                 return new SpecificationVersion(vers);
203             } catch (NumberFormatException JavaDoc nfe) {
204                 System.err.println("WARNING: invalid specification version: " + vers); // NOI18N
205
}
206             do {
207                 vers = vers.substring(0, vers.length() - 1);
208                 try {
209                     return new SpecificationVersion(vers);
210                 } catch (NumberFormatException JavaDoc nfe) {
211                     // ignore
212
}
213             } while (vers.length() > 0);
214         }
215         // Nothing decent in it at all; use zero.
216
return new SpecificationVersion("0"); // NOI18N
217
}
218
219 }
220
Popular Tags