KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > source > parsing > FileObjects


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.java.source.parsing;
21
22
23 import java.io.ByteArrayInputStream JavaDoc;
24 import java.io.ByteArrayOutputStream JavaDoc;
25 import java.io.File JavaDoc;
26 import java.io.FileInputStream JavaDoc;
27 import java.io.FileOutputStream JavaDoc;
28 import java.io.IOException JavaDoc;
29 import java.io.InputStream JavaDoc;
30 import java.io.InputStreamReader JavaDoc;
31 import java.io.OutputStream JavaDoc;
32 import java.io.OutputStreamWriter JavaDoc;
33 import java.io.Reader JavaDoc;
34 import java.io.Writer JavaDoc;
35 import java.net.MalformedURLException JavaDoc;
36 import java.net.URI JavaDoc;
37 import java.net.URL JavaDoc;
38 import java.nio.CharBuffer JavaDoc;
39 import java.util.Comparator JavaDoc;
40 import java.util.zip.ZipEntry JavaDoc;
41 import java.util.zip.ZipFile JavaDoc;
42 import javax.lang.model.element.Modifier;
43 import javax.lang.model.element.NestingKind;
44 import javax.tools.JavaFileObject;
45 import org.netbeans.modules.java.ClassDataLoader;
46 import org.netbeans.modules.java.JavaDataLoader;
47 import org.netbeans.modules.java.preprocessorbridge.spi.JavaFileFilterImplementation;
48 import org.openide.filesystems.FileObject;
49 import org.openide.filesystems.FileUtil;
50 import org.openide.filesystems.FileUtil;
51 import org.openide.util.NbBundle;
52 import org.openide.util.Utilities;
53
54 /** Creates various kinds of file objects
55  *
56  * XXX - Rename to JavaFileObjects
57  *
58  * @author Petr Hrebejk
59  */

60 public class FileObjects {
61     
62     public static final Comparator JavaDoc<String JavaDoc> SIMPLE_NAME_STRING_COMPARATOR = new SimpleNameStringComparator();
63     public static final Comparator JavaDoc<JavaFileObject> SIMPLE_NAME_FILEOBJECT_COMPARATOR = new SimpleNameFileObjectComparator();
64     
65     
66     public static final String JavaDoc JAVA = JavaDataLoader.JAVA_EXTENSION;
67     public static final String JavaDoc CLASS = ClassDataLoader.CLASS_EXTENSION;
68     public static final String JavaDoc JAR = "jar"; //NOI18N
69
public static final String JavaDoc ZIP = "zip"; //NOI18N
70
public static final String JavaDoc HTML = "html"; //NOI18N
71
public static final String JavaDoc SIG = "sig"; //NOI18N
72
public static final String JavaDoc RS = "rs"; //NOI18N
73

74     
75     /** Creates a new instance of FileObjects */
76     private FileObjects() {
77     }
78     
79     // Public methods ----------------------------------------------------------
80

81     
82     
83     /**
84      * Creates {@link JavaFileObject} for a ZIP entry of given name
85      * @param zip a zip file
86      * @param name the name of entry, the '/' char is a separator
87      * @return {@link JavaFileObject}, never returns null
88      */

89     public static JavaFileObject zipFileObject( File JavaDoc zipFile, String JavaDoc folder, String JavaDoc baseName, long mtime) {
90         assert zipFile != null;
91         return new ZipFileObject( zipFile, folder, baseName, mtime);
92     }
93     
94     public static JavaFileObject zipFileObject(ZipFile JavaDoc zipFile, String JavaDoc folder, String JavaDoc baseName, long mtime) {
95         assert zipFile != null;
96         return new CachedZipFileObject (zipFile, folder, baseName, mtime);
97     }
98     
99     /**
100      * Creates {@link JavaFileObject} for a regular {@link File}
101      * @param file for which the {@link JavaFileObject} should be created
102      * @pram root - the classpath root owning the file
103      * @return {@link JavaFileObject}, never returns null
104      */

105     public static JavaFileObject fileFileObject( final File JavaDoc file, final File JavaDoc root, final JavaFileFilterImplementation filter) {
106         assert file != null;
107         assert root != null;
108         String JavaDoc[] pkgNamePair = getFolderAndBaseName(getRelativePath(root,file),File.separatorChar);
109         return new RegularFileObject( file, convertFolder2Package(pkgNamePair[0], File.separatorChar), pkgNamePair[1], filter);
110     }
111     
112     /**
113      * Creates {@link JavaFileObject} for a NetBeans {@link FileObject}
114      * Any client which needs to create {@link JavaFileObject} for java
115      * source file should use this factory method.
116      * @param {@link FileObject} for which the {@link JavaFileObject} should be created
117      * @return {@link JavaFileObject}, never returns null
118      * @exception {@link IOException} may be thrown
119      */

120     public static JavaFileObject nbFileObject (final FileObject file) throws IOException JavaDoc {
121         return nbFileObject (file, null, false);
122     }
123     
124     /**
125      * Creates {@link JavaFileObject} for a NetBeans {@link FileObject}
126      * Any client which needs to create {@link JavaFileObject} for java
127      * source file should use this factory method.
128      * @param {@link FileObject} for which the {@link JavaFileObject} should be created
129      * @param renderNow if true the snap shot of the file is taken immediately
130      * @return {@link JavaFileObject}, never returns null
131      * @exception {@link IOException} may be thrown
132      */

133     public static JavaFileObject nbFileObject (final FileObject file, JavaFileFilterImplementation filter, boolean renderNow) throws IOException JavaDoc {
134         assert file != null;
135         if (!file.isValid() || file.isVirtual()) {
136             throw new InvalidFileException (file);
137         }
138         return new SourceFileObject (file, filter, renderNow);
139     }
140     
141     /**
142      * Creates virtual {@link JavaFileObject} with given name and content.
143      * This method should be used only by tests, regular client should never
144      * use this method.
145      * @param content the content of the {@link JavaFileObject}
146      * @param name the name of the {@link JavaFileObject}
147      * @return {@link JavaFileObject}, never returns null
148      */

149     public static JavaFileObject memoryFileObject( CharSequence JavaDoc content, CharSequence JavaDoc name ) {
150         final String JavaDoc nameStr = name.toString();
151         if (!nameStr.equals(getBaseName(nameStr))) {
152             throw new IllegalArgumentException JavaDoc ("Memory is flat"); //NOI18N
153
}
154         int length = content.length();
155         if ( length != 0 && Character.isWhitespace( content.charAt( length - 1 ) ) ) {
156             return new MemoryFileObject( nameStr, CharBuffer.wrap( content ) );
157         }
158         else {
159             return new MemoryFileObject( nameStr, (CharBuffer JavaDoc)CharBuffer.allocate( length + 1 ).append( content ).append( ' ' ).flip() );
160         }
161         
162     }
163     
164     public static String JavaDoc stripExtension( String JavaDoc fileName ) {
165         int dot = fileName.lastIndexOf(".");
166         return (dot == -1 ? fileName : fileName.substring(0, dot));
167     }
168     
169     
170     /**
171      * Returns the name of JavaFileObject, similar to
172      * {@link java.io.File#getName}
173      */

174     public static String JavaDoc getName (final JavaFileObject fo, final boolean noExt) {
175         assert fo != null;
176         if (fo instanceof Base) {
177             Base baseFileObject = (Base) fo;
178             if (noExt) {
179                 return baseFileObject.getName();
180             }
181             else {
182                 StringBuilder JavaDoc sb = new StringBuilder JavaDoc ();
183                 sb.append (baseFileObject.getName());
184                 sb.append('.'); //NOI18N
185
sb.append(baseFileObject.getExt());
186                 return sb.toString();
187             }
188         }
189         try {
190             final URL JavaDoc url = fo.toUri().toURL();
191             String JavaDoc path = url.getPath();
192             int index1 = path.lastIndexOf('/');
193             int len;
194             if (noExt) {
195                final int index2 = path.lastIndexOf('.');
196                if (index2>index1) {
197                    len = index2;
198                }
199                else {
200                    len = path.length();
201                }
202             }
203             else {
204                 len = path.length();
205             }
206             path = path.substring(index1+1,len);
207             return path;
208         } catch (MalformedURLException JavaDoc e) {
209             return null;
210         }
211     }
212         
213     
214     /**
215      * Returns the basename name without folder path
216      * @param file name, eg. obtained from {@link FileObjects#getPath} or {java.io.File.getPath}
217      * @return the base name
218      * @see #getBaseName(String,char)
219      */

220     public static String JavaDoc getBaseName( String JavaDoc fileName ) {
221         return getBaseName(fileName, File.separatorChar);
222     }
223     
224     /**
225      * Returns the basename name without folder path. You can specify
226      * the path separator since eg zip files uses '/' regardless of platform.
227      * @param file name, eg. obtained from {@link FileObjects#getPath} or {java.io.File.getPath}
228      * @param separator path separator
229      * @return the base name
230      */

231     public static String JavaDoc getBaseName( String JavaDoc fileName, char separator ) {
232         return getFolderAndBaseName(fileName, separator)[1];
233     }
234     
235     
236     /**
237      *Returns the folder (package name separated by original separators)
238      *and base name.
239      * @param path
240      * @return array of 2 strings, 1st the folder 2nd the base name
241      */

242     public static String JavaDoc[] getFolderAndBaseName (final String JavaDoc fileName, final char separator) {
243         final int i = fileName.lastIndexOf( separator );
244         if ( i == -1 ) {
245             return new String JavaDoc[] {"",fileName}; //NOI18N
246
}
247         else {
248             return new String JavaDoc[] {
249                 fileName.substring(0,i),
250                 fileName.substring( i + 1 )
251             };
252         }
253     }
254                 
255     public static String JavaDoc getBinaryName (final File JavaDoc file, final File JavaDoc root) {
256         assert file != null && root != null;
257         String JavaDoc fileName = FileObjects.getRelativePath (root, file);
258         int index = fileName.lastIndexOf('.'); //NOI18N
259
if (index > 0) {
260             fileName = fileName.substring(0,index);
261         }
262         return fileName.replace(File.separatorChar,'.'); //NOI18N
263
}
264     
265     public static String JavaDoc getSimpleName( JavaFileObject fo ) {
266         
267         String JavaDoc name = getName(fo,true);
268         int i = name.lastIndexOf( '$' );
269         if ( i == -1 ) {
270             return name;
271         }
272         else {
273             return name.substring( i + 1 );
274         }
275     }
276     
277     public static String JavaDoc getSimpleName( String JavaDoc fileName ) {
278         
279         String JavaDoc name = getBaseName( fileName );
280         
281         int i = name.lastIndexOf( '$' );
282         if ( i == -1 ) {
283             return name;
284         }
285         else {
286             return name.substring( i + 1 );
287         }
288         
289     }
290     
291     public static String JavaDoc convertPackage2Folder( String JavaDoc packageName ) {
292         return packageName.replace( '.', '/' );
293     }
294     
295     
296     public static String JavaDoc convertFolder2Package (String JavaDoc packageName) {
297         return convertFolder2Package (packageName, '/'); //NOI18N
298
}
299     
300     public static String JavaDoc convertFolder2Package( String JavaDoc packageName, char folderSeparator ) {
301         return packageName.replace( folderSeparator, '.' );
302     }
303     
304     
305     public static String JavaDoc getRelativePath (final String JavaDoc packageName, final String JavaDoc relativeName) {
306         StringBuilder JavaDoc relativePath = new StringBuilder JavaDoc ();
307         relativePath.append(packageName.replace('.','/'));
308         relativePath.append(relativeName);
309         return relativePath.toString();
310     }
311     
312     public static String JavaDoc[] getParentRelativePathAndName (final String JavaDoc className) {
313         if (className.charAt(className.length()-1) == '.') {
314             return null;
315         }
316         final int index = className.lastIndexOf('.');
317         if (index<0) {
318             return new String JavaDoc[] {
319                 "", //NOI18N
320
className
321             };
322         }
323         else {
324             return new String JavaDoc[] {
325                 className.substring(0,index).replace('.','/'), //NOI18N
326
className.substring(index+1)
327             };
328         }
329     }
330     
331     
332     // Private methods ---------------------------------------------------------
333

334     // Innerclasses ------------------------------------------------------------
335

336     public static abstract class Base implements JavaFileObject {
337
338         protected final JavaFileObject.Kind kind;
339         protected final String JavaDoc pkgName;
340         protected final String JavaDoc nameWithoutExt;
341         protected final String JavaDoc ext;
342         
343         protected Base (final String JavaDoc pkgName, final String JavaDoc name) {
344             assert pkgName != null;
345             assert name != null;
346             this.pkgName = pkgName;
347             String JavaDoc[] res = getNameExtPair(name);
348             this.nameWithoutExt = res[0];
349             this.ext = res[1];
350             if (FileObjects.JAVA.equalsIgnoreCase(ext)) { //NOI18N
351
this.kind = Kind.SOURCE;
352             }
353             else if (FileObjects.CLASS.equalsIgnoreCase(ext) || "sig".equals(ext)) { //NOI18N
354
this.kind = Kind.CLASS;
355             }
356             else if (FileObjects.HTML.equalsIgnoreCase(ext)) { //NOI18N
357
this.kind = Kind.HTML;
358             }
359             else {
360                 this.kind = Kind.OTHER;
361             }
362         }
363         
364         public JavaFileObject.Kind getKind() {
365             return this.kind;
366         }
367         
368         public boolean isNameCompatible (String JavaDoc simplename, JavaFileObject.Kind k) {
369             if (this.kind != k) {
370                 return false;
371             }
372         return nameWithoutExt.equals(simplename);
373     }
374         
375         public NestingKind getNestingKind() {
376             return null;
377         }
378         
379         public Modifier getAccessLevel() {
380             return null;
381         }
382     
383         @Override JavaDoc
384         public String JavaDoc toString() {
385             return this.toUri().toString();
386         }
387         
388         public String JavaDoc getPackage () {
389             return this.pkgName;
390         }
391         
392         public String JavaDoc getNameWithoutExtension () {
393             return this.nameWithoutExt;
394         }
395         
396         public String JavaDoc getName () {
397             return this.nameWithoutExt + '.' + ext;
398         }
399         
400         public String JavaDoc getExt () {
401             return this.ext;
402         }
403         
404         private static String JavaDoc[] getNameExtPair (String JavaDoc name) {
405             int index = name.lastIndexOf ('.');
406             String JavaDoc namenx;
407             String JavaDoc ext;
408             if (index <= 0) {
409                 namenx =name;
410                 ext = ""; //NOI18N
411
}
412             else {
413                 namenx = name.substring(0,index);
414                 if (index == name.length()-1) {
415                     ext = "";
416                 }
417                 else {
418                     ext = name.substring(index+1);
419                 }
420             }
421             return new String JavaDoc[] {
422               namenx,
423               ext
424             };
425         }
426     }
427     
428     public static abstract class FileBase extends Base {
429         
430         protected final File JavaDoc f;
431         
432         protected FileBase (final File JavaDoc file, final String JavaDoc pkgName, final String JavaDoc name) {
433             super (pkgName, name);
434             assert file != null;
435             assert file.equals(FileUtil.normalizeFile(file));
436             this.f = file;
437         }
438         
439         public File JavaDoc getFile () {
440             return this.f;
441         }
442     }
443     
444     
445     public static class InvalidFileException extends IOException JavaDoc {
446         
447         public InvalidFileException () {
448             super ();
449         }
450         
451         public InvalidFileException (final FileObject fo) {
452             super (NbBundle.getMessage(FileObjects.class,"FMT_InvalidFile",FileUtil.getFileDisplayName(fo)));
453         }
454     }
455     
456     
457     public static String JavaDoc getRelativePath (final File JavaDoc root, final File JavaDoc fo) {
458         final String JavaDoc rootPath = root.getAbsolutePath();
459         final String JavaDoc foPath = fo.getAbsolutePath();
460         assert foPath.startsWith(rootPath);
461         int index = rootPath.length();
462         if (rootPath.charAt(index-1)!=File.separatorChar) {
463             index++;
464         }
465         int foIndex = foPath.length();
466         if (foIndex <= index) {
467             return ""; //NOI18N
468
}
469         return foPath.substring(index);
470     }
471     
472     private static class RegularFileObject extends FileBase {
473         
474         private URI JavaDoc uriCache;
475         private final JavaFileFilterImplementation filter;
476
477     public RegularFileObject(final File JavaDoc f, final String JavaDoc packageName, final String JavaDoc baseName, final JavaFileFilterImplementation filter) {
478             super (f, packageName, baseName);
479             this.filter = filter;
480     }
481
482         public InputStream JavaDoc openInputStream() throws IOException JavaDoc {
483         return new FileInputStream JavaDoc(f);
484     }
485
486     public Reader JavaDoc openReader (boolean b) throws IOException JavaDoc {
487         throw new UnsupportedOperationException JavaDoc();
488     }
489
490     public OutputStream JavaDoc openOutputStream() throws IOException JavaDoc {
491         return new FileOutputStream JavaDoc(f);
492     }
493
494     public Writer JavaDoc openWriter() throws IOException JavaDoc {
495         //FIX: consider using encoding here
496
return new OutputStreamWriter JavaDoc(new FileOutputStream JavaDoc(f));
497     }
498
499     public @Override JavaDoc boolean isNameCompatible(String JavaDoc simplename, JavaFileObject.Kind kind) {
500         boolean res = super.isNameCompatible(simplename, kind);
501             if (res) {
502                 return res;
503             }
504             else if (Utilities.isWindows()) {
505                 return nameWithoutExt.equalsIgnoreCase(simplename);
506             }
507             else {
508                 return false;
509             }
510     }
511         
512         public URI JavaDoc toUri () {
513             if (this.uriCache == null) {
514                 this.uriCache = f.toURI();
515             }
516             return this.uriCache;
517         }
518
519         public long getLastModified() {
520         return f.lastModified();
521     }
522
523     public boolean delete() {
524         return f.delete();
525     }
526
527     public CharSequence JavaDoc getCharContent(boolean ignoreEncodingErrors) throws IOException JavaDoc {
528             
529             char[] result;
530             InputStreamReader JavaDoc in = new InputStreamReader JavaDoc (new FileInputStream JavaDoc(this.f), encodingName);
531             try {
532                 int len = (int)this.f.length();
533                 result = new char [len+1];
534                 int red = 0, rv;
535                 while ((rv=in.read(result,red,len-red))>0 && (red=red+rv)<len);
536             } finally {
537                 in.close();
538             }
539             result[result.length-1]='\n'; //NOI18N
540
CharSequence JavaDoc buffer = CharBuffer.wrap (result);
541             if (this.filter != null) {
542                 buffer = this.filter.filterCharSequence(buffer);
543             }
544             return buffer;
545     }
546
547     @Override JavaDoc
548     public boolean equals(Object JavaDoc other) {
549         if (!(other instanceof RegularFileObject))
550         return false;
551         RegularFileObject o = (RegularFileObject) other;
552         return f.equals(o.f);
553     }
554
555     @Override JavaDoc
556     public int hashCode() {
557         return f.hashCode();
558     }
559         
560     }
561
562     /** A subclass of FileObject representing zip entries.
563      * XXX: What happens when the archive is deleted or rebuilt?
564      */

565     public abstract static class ZipFileBase extends Base {
566         
567         protected final long mtime;
568         protected final String JavaDoc resName;
569         
570         public ZipFileBase (final String JavaDoc folderName, final String JavaDoc baseName, long mtime) {
571             super (convertFolder2Package(folderName),baseName);
572             this.mtime = mtime;
573             if (folderName.length() == 0) {
574                 this.resName = baseName;
575             }
576             else {
577                 StringBuilder JavaDoc resName = new StringBuilder JavaDoc (folderName);
578                 resName.append('/'); //NOI18N
579
resName.append(baseName);
580                 this.resName = resName.toString();
581             }
582         }
583         
584         public OutputStream JavaDoc openOutputStream() throws IOException JavaDoc {
585         throw new UnsupportedOperationException JavaDoc();
586     }
587
588     public Reader JavaDoc openReader(boolean b) throws IOException JavaDoc {
589             if (this.getKind() == JavaFileObject.Kind.CLASS) {
590                 throw new UnsupportedOperationException JavaDoc();
591             }
592             else {
593                 return new InputStreamReader JavaDoc (openInputStream(),FileObjects.encodingName);
594             }
595     }
596
597         public Writer JavaDoc openWriter() throws IOException JavaDoc {
598         throw new UnsupportedOperationException JavaDoc();
599     }
600         
601         public long getLastModified() {
602         return mtime;
603     }
604
605     public boolean delete() {
606         throw new UnsupportedOperationException JavaDoc();
607     }
608
609     public CharBuffer JavaDoc getCharContent(boolean ignoreEncodingErrors) throws IOException JavaDoc {
610         Reader JavaDoc r = openReader(ignoreEncodingErrors);
611             try {
612                 int red = 0, rv;
613
614                 int len = (int)this.getSize();
615                 char[] result = new char [len+1];
616                 while ((rv=r.read(result,red,len-red))>0 && (red=red+rv)<len);
617
618                 int j=0;
619                 for (int i=0; i<red;i++) {
620                     if (result[i] =='\r') { //NOI18N
621
if (i+1>=red || result[i+1]!='\n') { //NOI18N
622
result[j++] = '\n'; //NOI18N
623
}
624                     }
625                     else {
626                         result[j++] = result[i];
627                     }
628                 }
629                 result[j]='\n'; //NOI18N
630
return CharBuffer.wrap (result,0,j);
631             } finally {
632                 r.close();
633             }
634     }
635         
636         public final URI JavaDoc toUri () {
637             URI JavaDoc zdirURI = this.getArchiveURI();
638             return URI.create ("jar:"+zdirURI.toString()+"!/"+resName); //NOI18N
639
}
640         
641         @Override JavaDoc
642     public int hashCode() {
643         return this.resName.hashCode();
644     }
645         
646     @Override JavaDoc
647     public boolean equals(Object JavaDoc other) {
648         if (!(other instanceof ZipFileBase))
649         return false;
650         ZipFileBase o = (ZipFileBase) other;
651         return getArchiveURI().equals(o.getArchiveURI()) && resName.equals(o.resName);
652     }
653         
654         protected abstract URI JavaDoc getArchiveURI ();
655         
656         protected abstract long getSize() throws IOException JavaDoc;
657         
658     }
659     
660     private static class ZipFileObject extends ZipFileBase {
661     
662
663     /** The zipfile containing the entry.
664      */

665     private final File JavaDoc archiveFile;
666         
667
668         ZipFileObject(final File JavaDoc archiveFile, final String JavaDoc folderName, final String JavaDoc baseName, long mtime) {
669             super (folderName,baseName,mtime);
670             assert archiveFile != null : "archiveFile == null"; //NOI18N
671
this.archiveFile = archiveFile;
672             
673     }
674
675         public InputStream JavaDoc openInputStream() throws IOException JavaDoc {
676             class ZipInputStream extends InputStream JavaDoc {
677
678                 private ZipFile JavaDoc zipfile;
679                 private InputStream JavaDoc delegate;
680
681                 public ZipInputStream (ZipFile JavaDoc zf) throws IOException JavaDoc {
682                     this.zipfile = zf;
683                     this.delegate = zf.getInputStream(new ZipEntry JavaDoc(resName));
684                 }
685
686                 public int read() throws IOException JavaDoc {
687                     throw new java.lang.UnsupportedOperationException JavaDoc("Not supported yet.");
688                 }
689
690                 public int read(byte b[], int off, int len) throws IOException JavaDoc {
691                     return delegate.read(b, off, len);
692                 }
693
694                 public int available() throws IOException JavaDoc {
695                     return this.delegate.available();
696                 }
697
698                 public void close() throws IOException JavaDoc {
699                     try {
700                         this.delegate.close();
701                     } finally {
702                         this.zipfile.close();
703                     }
704                 }
705
706
707             };
708             ZipFile JavaDoc zf = new ZipFile JavaDoc (archiveFile);
709             return new ZipInputStream (zf);
710     }
711         
712         public URI JavaDoc getArchiveURI () {
713             return this.archiveFile.toURI();
714         }
715         
716         protected long getSize () throws IOException JavaDoc {
717             ZipFile JavaDoc zf = new ZipFile JavaDoc (archiveFile);
718             try {
719                 ZipEntry JavaDoc ze = zf.getEntry(this.resName);
720                 return ze == null ? 0L : ze.getSize();
721             } finally {
722                 zf.close();
723             }
724         }
725     }
726     
727     private static class CachedZipFileObject extends ZipFileBase {
728         
729         private ZipFile JavaDoc zipFile;
730         
731         CachedZipFileObject(final ZipFile JavaDoc zipFile, final String JavaDoc folderName, final String JavaDoc baseName, long mtime) {
732             super (folderName,baseName,mtime);
733             assert zipFile != null : "archiveFile == null"; //NOI18N
734
this.zipFile = zipFile;
735     }
736         
737         public InputStream JavaDoc openInputStream() throws IOException JavaDoc {
738             return this.zipFile.getInputStream(new ZipEntry JavaDoc (this.resName));
739     }
740         
741         public URI JavaDoc getArchiveURI () {
742             return new File JavaDoc (this.zipFile.getName()).toURI();
743         }
744         
745         protected long getSize() throws IOException JavaDoc {
746             ZipEntry JavaDoc ze = this.zipFile.getEntry(this.resName);
747             return ze == null ? 0L : ze.getSize();
748         }
749     }
750     
751     
752     /** Temporay FileObject for parsing input stream.
753      */

754     private static class MemoryFileObject extends Base {
755         
756         private String JavaDoc fileName;
757         private CharBuffer JavaDoc cb;
758         
759         public MemoryFileObject( String JavaDoc fileName, CharBuffer JavaDoc cb ) {
760             super ("",fileName); //NOI18N
761
this.cb = cb;
762             this.fileName = fileName;
763         }
764         
765
766         /**
767          * Get the character content of the file, if available.
768          * @param ignoreEncodingErrors if true, encoding errros will be replaced by the
769          * default translation character; otherwise they should be reported as diagnostics.
770          * @throws UnsupportedOperationException if character access is not supported
771          */

772         public java.nio.CharBuffer JavaDoc getCharContent(boolean ignoreEncodingErrors) throws java.io.IOException JavaDoc {
773             return cb;
774         }
775
776         public boolean delete() {
777             // Do nothing
778
return false;
779         }
780
781         public URI JavaDoc toUri () {
782             return URI.create (this.nameWithoutExt);
783         }
784
785         public long getLastModified() {
786             return System.currentTimeMillis(); // XXX
787
}
788
789         /**
790          * Get an InputStream for this object.
791          *
792          * @return an InputStream for this object.
793          * @throws UnsupportedOperationException if the byte access is not supported
794          */

795         public InputStream JavaDoc openInputStream() throws java.io.IOException JavaDoc {
796             return new ByteArrayInputStream JavaDoc(cb.toString().getBytes("UTF-8"));
797         }
798
799         /**
800          * Get an OutputStream for this object.
801          *
802          * @return an OutputStream for this object.
803          * @throws UnsupportedOperationException if byte access is not supported
804          */

805         public java.io.OutputStream JavaDoc openOutputStream() throws java.io.IOException JavaDoc {
806             throw new UnsupportedOperationException JavaDoc();
807         }
808
809         /**
810          * Get a reader for this object.
811          *
812          * @return a Reader for this file object.
813          * @throws UnsupportedOperationException if character access is not supported
814          * @throws IOException if an error occurs while opening the reader
815          */

816         public java.io.Reader JavaDoc openReader (boolean b) throws java.io.IOException JavaDoc {
817             throw new UnsupportedOperationException JavaDoc();
818         }
819
820         /**
821          * Get a writer for this object.
822          * @throws UnsupportedOperationException if character access is not supported
823          * @throws IOException if an error occurs while opening the writer
824          */

825         public java.io.Writer JavaDoc openWriter() throws java.io.IOException JavaDoc {
826             throw new UnsupportedOperationException JavaDoc();
827         }
828         
829     }
830     
831     private static class SimpleNameStringComparator implements Comparator JavaDoc<String JavaDoc> {
832         
833         public int compare( String JavaDoc o1, String JavaDoc o2 ) {
834             return getSimpleName( o1 ).compareTo( getSimpleName( o2 ) );
835         }
836                         
837     }
838     
839     private static class SimpleNameFileObjectComparator implements Comparator JavaDoc<JavaFileObject> {
840         
841         public int compare( JavaFileObject o1, JavaFileObject o2 ) {
842             
843             String JavaDoc n1 = getSimpleName( o1 );
844             String JavaDoc n2 = getSimpleName( o2 );
845                         
846             return n1.compareTo( n2 );
847         }
848                         
849     }
850     
851     static final String JavaDoc encodingName = new OutputStreamWriter JavaDoc(new ByteArrayOutputStream JavaDoc()).getEncoding();
852     
853 }
854
Popular Tags