1 19 20 package org.netbeans.modules.retouche.source.parsing; 21 22 23 import java.io.ByteArrayInputStream ; 24 import java.io.ByteArrayOutputStream ; 25 import java.io.File ; 26 import java.io.FileInputStream ; 27 import java.io.FileOutputStream ; 28 import java.io.IOException ; 29 import java.io.InputStream ; 30 import java.io.InputStreamReader ; 31 import java.io.OutputStream ; 32 import java.io.OutputStreamWriter ; 33 import java.io.Reader ; 34 import java.io.Writer ; 35 import java.net.MalformedURLException ; 36 import java.net.URI ; 37 import java.net.URL ; 38 import java.nio.CharBuffer ; 39 import java.util.Comparator ; 40 import java.util.zip.ZipEntry ; 41 import java.util.zip.ZipFile ; 42 import org.netbeans.api.gsf.ParserFile; 43 import org.netbeans.api.gsf.ParserFile; 44 import org.openide.filesystems.FileObject; 45 import org.openide.filesystems.FileUtil; 46 import org.openide.filesystems.FileUtil; 47 import org.openide.util.NbBundle; 48 import org.openide.util.Utilities; 49 50 62 public class FileObjects { 63 64 public static final Comparator <String > SIMPLE_NAME_STRING_COMPARATOR = new SimpleNameStringComparator(); 65 public static final Comparator <FileObject> SIMPLE_NAME_FILEOBJECT_COMPARATOR = new SimpleNameFileObjectComparator(); 66 67 68 public static final String HTML = "html"; public static final String SIG = "sig"; public static final String RS = "rs"; 76 77 78 private FileObjects() { 79 } 80 81 83 84 85 120 public static ParserFile fileFileObject( final File file, final File root, boolean platform, Object filter) { 121 assert file != null; 122 assert root != null; 123 return new FileParserFile(file, root, platform); 124 } 125 126 private static class FileParserFile implements ParserFile { 127 private File file; 128 private File root; 129 private FileObject fileObject; 130 private String relative; 131 private boolean platform; 132 133 private FileParserFile(File file, File root, boolean platform) { 134 this.file = file; 135 this.root = root; 136 this.platform = platform; 137 } 138 139 public FileObject getFileObject() { 140 if (fileObject == null) { 141 fileObject = FileUtil.toFileObject(file); 142 } 143 return fileObject; 144 } 145 146 public String getRelativePath() { 147 if (relative == null) { 148 relative = FileObjects.getRelativePath(root, file); 149 } 150 151 return relative; 152 } 153 154 public String getNameExt() { 155 return file.getName(); 156 } 157 158 public String getExtension() { 159 String name = file.getName(); 160 int index = name.lastIndexOf('.'); 161 if (index != -1) { 162 return name.substring(index+1); 163 } else { 164 return ""; 165 } 166 } 167 168 public String toString() { 169 return "FileParserFile(" + getNameExt() + ")"; 170 } 171 172 public boolean isPlatform() { 173 return platform; 174 } 175 } 176 177 230 public static String stripExtension( String fileName ) { 231 int dot = fileName.lastIndexOf("."); 232 return (dot == -1 ? fileName : fileName.substring(0, dot)); 233 } 234 235 236 282 public static String getName (final FileObject fo, final boolean noExt) { 283 assert fo != null; 284 return noExt ? fo.getName() : fo.getNameExt(); 285 } 286 287 288 294 public static String getBaseName( String fileName ) { 295 return getBaseName(fileName, File.separatorChar); 296 } 297 298 305 public static String getBaseName( String fileName, char separator ) { 306 return getFolderAndBaseName(fileName, separator)[1]; 307 } 308 309 310 316 public static String [] getFolderAndBaseName (final String fileName, final char separator) { 317 final int i = fileName.lastIndexOf( separator ); 318 if ( i == -1 ) { 319 return new String [] {"",fileName}; } 321 else { 322 return new String [] { 323 fileName.substring(0,i), 324 fileName.substring( i + 1 ) 325 }; 326 } 327 } 328 329 public static String getBinaryName (final File file, final File root) { 330 assert file != null && root != null; 331 String fileName = FileObjects.getRelativePath (root, file); 332 int index = fileName.lastIndexOf('.'); if (index > 0) { 334 fileName = fileName.substring(0,index); 335 } 336 return fileName.replace(File.separatorChar,'.'); } 338 339 public static String getSimpleName( FileObject fo ) { 340 341 String name = getName(fo,true); 342 int i = name.lastIndexOf( '$' ); 343 if ( i == -1 ) { 344 return name; 345 } 346 else { 347 return name.substring( i + 1 ); 348 } 349 } 350 351 public static String getSimpleName( String fileName ) { 352 353 String name = getBaseName( fileName ); 354 355 int i = name.lastIndexOf( '$' ); 356 if ( i == -1 ) { 357 return name; 358 } 359 else { 360 return name.substring( i + 1 ); 361 } 362 363 } 364 365 public static String convertPackage2Folder( String packageName ) { 366 return packageName.replace( '.', '/' ); 367 } 368 369 370 public static String convertFolder2Package (String packageName) { 371 return convertFolder2Package (packageName, '/'); } 373 374 public static String convertFolder2Package( String packageName, char folderSeparator ) { 375 return packageName.replace( folderSeparator, '.' ); 376 } 377 378 379 public static String getRelativePath (final String packageName, final String relativeName) { 380 StringBuilder relativePath = new StringBuilder (); 381 relativePath.append(packageName.replace('.','/')); 382 relativePath.append(relativeName); 383 return relativePath.toString(); 384 } 385 386 public static String [] getParentRelativePathAndName (final String className) { 387 if (className.charAt(className.length()-1) == '.') { 388 return null; 389 } 390 final int index = className.lastIndexOf('.'); 391 if (index<0) { 392 return new String [] { 393 "", className 395 }; 396 } 397 else { 398 return new String [] { 399 className.substring(0,index).replace('.','/'), className.substring(index+1) 401 }; 402 } 403 } 404 405 406 public static File getRootFile (final URL url) { 407 File rootFile; 408 if ("jar".equals(url.getProtocol())) { rootFile = new File (URI.create(FileUtil.getArchiveFile(url).toExternalForm())); 410 } 411 else { 412 rootFile = new File (URI.create(url.toExternalForm())); 413 } 414 return rootFile; 415 } 416 417 418 420 422 530 531 public static class InvalidFileException extends IOException { 532 533 public InvalidFileException () { 534 super (); 535 } 536 537 public InvalidFileException (final FileObject fo) { 538 super (NbBundle.getMessage(FileObjects.class,"FMT_InvalidFile",FileUtil.getFileDisplayName(fo))); 539 } 540 } 541 542 543 public static String getRelativePath (final File root, final File fo) { 544 final String rootPath = root.getAbsolutePath(); 545 final String foPath = fo.getAbsolutePath(); 546 assert foPath.startsWith(rootPath); 547 int index = rootPath.length(); 548 if (rootPath.charAt(index-1)!=File.separatorChar) { 549 index++; 550 } 551 int foIndex = foPath.length(); 552 if (foIndex <= index) { 553 return ""; } 555 return foPath.substring(index); 556 } 557 872 private static class SimpleNameStringComparator implements Comparator <String > { 873 874 public int compare( String o1, String o2 ) { 875 return getSimpleName( o1 ).compareTo( getSimpleName( o2 ) ); 876 } 877 878 } 879 880 private static class SimpleNameFileObjectComparator implements Comparator <FileObject> { 881 882 public int compare( FileObject o1, FileObject o2 ) { 883 884 String n1 = getSimpleName( o1 ); 885 String n2 = getSimpleName( o2 ); 886 887 return n1.compareTo( n2 ); 888 } 889 890 } 891 892 static final String encodingName = new OutputStreamWriter (new ByteArrayOutputStream ()).getEncoding(); 893 894 } 895 | Popular Tags |