1 19 package org.netbeans.modules.java.j2seplatform.platformdefinition; 20 21 import java.text.MessageFormat ; 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 ; 26 import java.io.IOException ; 27 import java.net.URL ; 28 import java.net.MalformedURLException ; 29 import java.util.zip.ZipFile ; 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 classpath) { 44 StringTokenizer tokenizer = new StringTokenizer(classpath, File.pathSeparator); 45 List list = new ArrayList(); 46 while (tokenizer.hasMoreTokens()) { 47 String item = tokenizer.nextToken(); 48 File f = FileUtil.normalizeFile(new File (item)); 49 URL url = getRootURL (f); 50 if (url!=null) { 51 list.add(ClassPathSupport.createResource(url)); 52 } 53 } 54 return ClassPathSupport.createClassPath(list); 55 } 56 57 static URL getRootURL (final File f) { 59 try { 60 URL url = f.toURI().toURL(); 61 if (FileUtil.isArchiveFile(url)) { 62 url = FileUtil.getArchiveRoot (url); 63 } 64 else if (!f.exists()) { 65 String surl = url.toExternalForm(); 66 if (!surl.endsWith("/")) { 67 url = new URL (surl+"/"); 68 } 69 } 70 else if (f.isFile()) { 71 try { 74 ZipFile z = new ZipFile (f); 75 z.close(); 76 url = FileUtil.getArchiveRoot (url); 77 } catch (IOException e) { 78 url = null; 79 } 80 } 81 return url; 82 } catch (MalformedURLException e) { 83 throw new AssertionError (e); 84 } 85 } 86 87 88 94 public static String normalizeName (String displayName) { 95 StringBuffer normalizedName = new StringBuffer (); 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 113 public static SpecificationVersion getSpecificationVersion(JavaPlatform plat) { 114 String version = (String )plat.getSystemProperties().get("java.specification.version"); if (version == null) { 116 version = "1.1"; 117 } 118 return makeSpec(version); 119 } 120 121 122 public static FileObject findTool (String toolName, Collection installFolders) { 123 return findTool (toolName, installFolders, null); 124 } 125 126 public static FileObject findTool (String toolName, Collection installFolders, String 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"); 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); if (tool!= null) { 142 return tool; 143 } 144 } 145 return null; 146 } 147 148 153 public static String getExtensions (String extPath) { 154 if (extPath == null) { 155 return null; 156 } 157 StringBuffer sb = new StringBuffer (); 158 StringTokenizer tk = new StringTokenizer (extPath, File.pathSeparator); 159 while (tk.hasMoreTokens()) { 160 File extFolder = FileUtil.normalizeFile(new File (tk.nextToken())); 161 File [] files = extFolder.listFiles(); 162 if (files != null) { 163 for (int i = 0; i < files.length; i++) { 164 File f = files[i]; 165 if (!f.exists()) { 166 ErrorManager.getDefault().log (ErrorManager.WARNING, 169 MessageFormat.format (NbBundle.getMessage(Util.class,"MSG_BrokenExtension"), 170 new Object [] {f,extFolder})); 171 continue; 172 } 173 if (Utilities.isMac() && "._.DS_Store".equals(f.getName())) { 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 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 199 private static SpecificationVersion makeSpec(String vers) { 200 if (vers != null) { 201 try { 202 return new SpecificationVersion(vers); 203 } catch (NumberFormatException nfe) { 204 System.err.println("WARNING: invalid specification version: " + vers); } 206 do { 207 vers = vers.substring(0, vers.length() - 1); 208 try { 209 return new SpecificationVersion(vers); 210 } catch (NumberFormatException nfe) { 211 } 213 } while (vers.length() > 0); 214 } 215 return new SpecificationVersion("0"); } 218 219 } 220 | Popular Tags |