1 19 20 package org.netbeans.modules.web.project; 21 22 import java.awt.*; 23 import java.io.File ; 24 import java.io.IOException ; 25 import java.util.Stack ; 26 import java.util.Vector ; 27 28 import javax.swing.*; 29 30 import org.openide.filesystems.FileObject; 31 import org.openide.filesystems.FileStateInvalidException; 32 import org.openide.filesystems.FileUtil; 33 34 import org.netbeans.spi.project.support.ant.PropertyUtils; 35 import org.netbeans.spi.project.support.ant.AntProjectHelper; 36 import org.netbeans.spi.project.support.ant.EditableProperties; 37 import org.netbeans.api.java.platform.JavaPlatform; 38 import org.netbeans.api.java.platform.JavaPlatformManager; 39 40 public class Utils { 41 42 private static final String javaKeywords[] = { 44 "abstract", "assert", "boolean", "break", "byte", "case", 45 "catch", "char", "class", "const", "continue", 46 "default", "do", "double", "else", "enum", "extends", 47 "final", "finally", "float", "for", "goto", 48 "if", "implements", "import", "instanceof", "int", 49 "interface", "long", "native", "new", "package", 50 "private", "protected", "public", "return", "short", 51 "static", "strictfp", "super", "switch", "synchronized", 52 "this", "throws", "transient", "try", "void", 53 "volatile", "while" }; 54 55 private static final String JSP_PACKAGE_NAME = "org.apache.jsp"; 56 57 private static final String PLATFORM_ANT_NAME = "platform.ant.name"; public static final String SPECIFICATION_J2SE = "j2se"; 60 public static File getRoot(File f) { 61 File rootF = f; 62 while (rootF.getParentFile() != null) { 63 rootF = rootF.getParentFile(); 64 } 65 return rootF; 66 } 67 68 public static FileObject getValidDir(File dir) throws IOException { 69 Stack stack = new Stack (); 70 while (!dir.exists()) { 71 stack.push (dir.getName()); 72 dir = dir.getParentFile(); 73 } 74 FileObject dirFO = FileUtil.toFileObject (dir); 75 if (dirFO == null) { 76 refreshFileSystem(dir); 77 dirFO = FileUtil.toFileObject (dir); 78 } 79 assert dirFO != null; 80 while (!stack.isEmpty()) { 81 dirFO = dirFO.createFolder((String )stack.pop()); 82 } 83 return dirFO; 84 } 85 86 private static void refreshFileSystem (final File dir) throws FileStateInvalidException { 87 File rootF = dir; 88 while (rootF.getParentFile() != null) { 89 rootF = rootF.getParentFile(); 90 } 91 FileObject dirFO = FileUtil.toFileObject(rootF); 92 assert dirFO != null : "At least disk roots must be mounted! " + rootF; dirFO.getFileSystem().refresh(false); 94 } 95 96 public static FileObject getValidEmptyDir(File dir) throws IOException { 97 final FileObject fo = getValidDir(dir); 98 if (fo.getChildren().length != 0) { 99 throw new IOException ("Dir has to be empty: " + dir); 100 } 101 return fo; 102 } 103 104 106 public static String createDefaultContext(String projectName) { 107 return "/" + PropertyUtils.getUsablePropertyName(projectName); 108 } 109 110 116 public static void updateProperties(AntProjectHelper h, String path, EditableProperties ep) { 117 EditableProperties properties = h.getProperties(path); 118 properties.putAll(ep); 119 h.putProperties(path, properties); 120 } 121 122 129 public static boolean isParentOrEqual(File folder, File file) { 130 if(folder != null || file != null) { 131 folder = FileUtil.normalizeFile(folder); 132 file = FileUtil.normalizeFile(file); 133 while(file != null) { 134 if(file.equals(folder)) { 135 return true; 136 } 137 file = file.getParentFile(); 138 } 139 } 140 return false; 141 } 142 143 149 public static JavaPlatform findJ2seJavaPlatform(String platformName) { 150 return findJavaPlatform(platformName, SPECIFICATION_J2SE); 151 } 152 153 159 public static JavaPlatform findJavaPlatform(String platformName) { 160 return findJavaPlatform(platformName, null); 161 } 162 163 private static JavaPlatform findJavaPlatform(String platformName, String specFilter) { 164 if(platformName != null) { 165 JavaPlatform[] platforms = JavaPlatformManager.getDefault().getInstalledPlatforms(); 166 for(int i = 0; i < platforms.length; i++) { 167 JavaPlatform platform = platforms[i]; 168 String antName = (String )platform.getProperties().get(PLATFORM_ANT_NAME); 169 if (antName != null && antName.equals(platformName)) { 170 if(specFilter == null || specFilter.equalsIgnoreCase(platform.getSpecification().getName())) 171 return platform; 172 } 173 } 174 } 175 return null; 176 } 177 178 183 static String getGeneratedJavaResource(String jspUri) { 184 int iSep = jspUri.lastIndexOf('/'); 185 String packageName = (iSep > 0) ? makeJavaPackage(jspUri.substring(0,iSep)) : ""; if (packageName.length() == 0) { 187 packageName = JSP_PACKAGE_NAME; 188 } 189 else { 190 packageName = JSP_PACKAGE_NAME + "." + packageName; } 192 String className = makeJavaIdentifier(jspUri.substring(iSep + 1)); 193 return packageName.replace('.', '/') + "/" + className + ".java"; } 195 196 204 private static final String makeJavaPackage(String path) { 205 String classNameComponents[] = split(path,"/"); 206 StringBuffer legalClassNames = new StringBuffer (); 207 for (int i = 0; i < classNameComponents.length; i++) { 208 legalClassNames.append(makeJavaIdentifier(classNameComponents[i])); 209 if (i < classNameComponents.length - 1) { 210 legalClassNames.append('.'); 211 } 212 } 213 return legalClassNames.toString(); 214 } 215 216 223 private static final String [] split(String path, String pat) { 224 Vector comps = new Vector (); 225 int pos = path.indexOf(pat); 226 int start = 0; 227 while( pos >= 0 ) { 228 if(pos > start ) { 229 String comp = path.substring(start,pos); 230 comps.add(comp); 231 } 232 start = pos + pat.length(); 233 pos = path.indexOf(pat,start); 234 } 235 if( start < path.length()) { 236 comps.add(path.substring(start)); 237 } 238 String [] result = new String [comps.size()]; 239 for(int i=0; i < comps.size(); i++) { 240 result[i] = (String )comps.elementAt(i); 241 } 242 return result; 243 } 244 245 253 private static final String makeJavaIdentifier(String identifier) { 254 StringBuffer modifiedIdentifier = 255 new StringBuffer (identifier.length()); 256 if (!Character.isJavaIdentifierStart(identifier.charAt(0))) { 257 modifiedIdentifier.append('_'); 258 } 259 for (int i = 0; i < identifier.length(); i++) { 260 char ch = identifier.charAt(i); 261 if (Character.isJavaIdentifierPart(ch) && ch != '_') { 262 modifiedIdentifier.append(ch); 263 } else if (ch == '.') { 264 modifiedIdentifier.append('_'); 265 } else { 266 modifiedIdentifier.append(mangleChar(ch)); 267 } 268 } 269 if (isJavaKeyword(modifiedIdentifier.toString())) { 270 modifiedIdentifier.append('_'); 271 } 272 return modifiedIdentifier.toString(); 273 } 274 275 279 private static final String mangleChar(char ch) { 280 char[] result = new char[5]; 281 result[0] = '_'; 282 result[1] = Character.forDigit((ch >> 12) & 0xf, 16); 283 result[2] = Character.forDigit((ch >> 8) & 0xf, 16); 284 result[3] = Character.forDigit((ch >> 4) & 0xf, 16); 285 result[4] = Character.forDigit(ch & 0xf, 16); 286 return new String (result); 287 } 288 289 293 private static boolean isJavaKeyword(String key) { 294 int i = 0; 295 int j = javaKeywords.length; 296 while (i < j) { 297 int k = (i+j)/2; 298 int result = javaKeywords[k].compareTo(key); 299 if (result == 0) { 300 return true; 301 } 302 if (result < 0) { 303 i = k+1; 304 } else { 305 j = k; 306 } 307 } 308 return false; 309 } 310 311 public static Color getErrorColor() { 312 Color c = UIManager.getColor("nb.errorForeground"); return c == null ? new Color(89,79,191) : c; 315 } 316 317 public static String toClasspathString(File [] classpathEntries) { 318 if (classpathEntries == null) { 319 return ""; 320 } 321 StringBuffer classpath = new StringBuffer (); 322 for (int i = 0; i < classpathEntries.length; i++) { 323 classpath.append(classpathEntries[i].getAbsolutePath()); 324 if (i + 1 < classpathEntries.length) { 325 classpath.append(':'); } 327 } 328 return classpath.toString(); 329 } 330 } | Popular Tags |