Source Code Cross Referenced for FileObjects.java in  » IDE-Netbeans » gsf » org » netbeans » modules » gsfret » source » parsing » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » IDE Netbeans » gsf » org.netbeans.modules.gsfret.source.parsing 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003:         *
004:         * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005:         *
006:         * The contents of this file are subject to the terms of either the GNU
007:         * General Public License Version 2 only ("GPL") or the Common
008:         * Development and Distribution License("CDDL") (collectively, the
009:         * "License"). You may not use this file except in compliance with the
010:         * License. You can obtain a copy of the License at
011:         * http://www.netbeans.org/cddl-gplv2.html
012:         * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013:         * specific language governing permissions and limitations under the
014:         * License.  When distributing the software, include this License Header
015:         * Notice in each file and include the License file at
016:         * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
017:         * particular file as subject to the "Classpath" exception as provided
018:         * by Sun in the GPL Version 2 section of the License file that
019:         * accompanied this code. If applicable, add the following below the
020:         * License Header, with the fields enclosed by brackets [] replaced by
021:         * your own identifying information:
022:         * "Portions Copyrighted [year] [name of copyright owner]"
023:         *
024:         * Contributor(s):
025:         *
026:         * The Original Software is NetBeans. The Initial Developer of the Original
027:         * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028:         * Microsystems, Inc. All Rights Reserved.
029:         *
030:         * If you wish your version of this file to be governed by only the CDDL
031:         * or only the GPL Version 2, indicate your decision by adding
032:         * "[Contributor] elects to include this software in this distribution
033:         * under the [CDDL or GPL Version 2] license." If you do not indicate a
034:         * single choice of license, a recipient has the option to distribute
035:         * your version of this file under either the CDDL, the GPL Version 2 or
036:         * to extend the choice of license to its licensees as provided above.
037:         * However, if you add GPL Version 2 code and therefore, elected the GPL
038:         * Version 2 license, then the option applies only if the new code is
039:         * made subject to such option by the copyright holder.
040:         */
041:
042:        package org.netbeans.modules.gsfret.source.parsing;
043:
044:        import java.io.ByteArrayInputStream;
045:        import java.io.ByteArrayOutputStream;
046:        import java.io.File;
047:        import java.io.FileInputStream;
048:        import java.io.FileOutputStream;
049:        import java.io.IOException;
050:        import java.io.InputStream;
051:        import java.io.InputStreamReader;
052:        import java.io.OutputStream;
053:        import java.io.OutputStreamWriter;
054:        import java.io.Reader;
055:        import java.io.Writer;
056:        import java.net.MalformedURLException;
057:        import java.net.URI;
058:        import java.net.URL;
059:        import java.nio.CharBuffer;
060:        import java.util.Comparator;
061:        import java.util.zip.ZipEntry;
062:        import java.util.zip.ZipFile;
063:        import org.netbeans.modules.gsf.api.ParserFile;
064:        import org.netbeans.modules.gsf.api.ParserFile;
065:        import org.openide.filesystems.FileObject;
066:        import org.openide.filesystems.FileUtil;
067:        import org.openide.filesystems.FileUtil;
068:        import org.openide.util.NbBundle;
069:        import org.openide.util.Utilities;
070:
071:        /** Creates various kinds of file objects 
072:         * This file is originally from Retouche, the Java Support 
073:         * infrastructure in NetBeans. I have modified the file as little
074:         * as possible to make merging Retouche fixes back as simple as
075:         * possible. 
076:         *
077:         * I've ripped out a bunch of stuff here
078:         *
079:         * XXX - Rename to JavaFileObjects
080:         *
081:         * @author Petr Hrebejk
082:         */
083:        public class FileObjects {
084:
085:            public static final Comparator<String> SIMPLE_NAME_STRING_COMPARATOR = new SimpleNameStringComparator();
086:            public static final Comparator<FileObject> SIMPLE_NAME_FILEOBJECT_COMPARATOR = new SimpleNameFileObjectComparator();
087:
088:            //    public static final String JAVA  = JavaDataLoader.JAVA_EXTENSION;
089:            //    public static final String CLASS = ClassDataLoader.CLASS_EXTENSION;
090:            public static final String JAR = "jar"; //NOI18N
091:            public static final String FILE = "file"; //NOI18N
092:            public static final String ZIP = "zip"; //NOI18N
093:            public static final String HTML = "html"; //NOI18N
094:            public static final String SIG = "sig"; //NOI18N
095:            public static final String RS = "rs"; //NOI18N
096:
097:            /** Creates a new instance of FileObjects */
098:            private FileObjects() {
099:            }
100:
101:            // Public methods ----------------------------------------------------------
102:
103:            //    /**
104:            //     * Creates {@link JavaFileObject} for a ZIP entry of given name
105:            //     * @param zip a zip file
106:            //     * @param name the name of entry, the '/' char is a separator
107:            //     * @return {@link JavaFileObject}, never returns null
108:            //     */
109:            //    public static JavaFileObject zipFileObject( File zipFile, String folder, String baseName, long mtime) {
110:            //        assert zipFile != null;                
111:            //        return new ZipFileObject( zipFile, folder, baseName, mtime);
112:            //    }
113:            //    
114:            //    public static JavaFileObject zipFileObject(ZipFile zipFile, String folder, String baseName, long mtime) {
115:            //        assert zipFile != null;
116:            //        return new CachedZipFileObject (zipFile, folder, baseName, mtime);
117:            //    }
118:            //    
119:            //    /**
120:            //     * Creates {@link JavaFileObject} for a regular {@link File}
121:            //     * @param file for which the {@link JavaFileObject} should be created
122:            //     * @pram root - the classpath root owning the file
123:            //     * @return {@link JavaFileObject}, never returns null
124:            //     */
125:            //    public static JavaFileObject fileFileObject( final File file, final File root, final JavaFileFilterImplementation filter) {
126:            //        assert file != null;
127:            //        assert root != null;
128:            //        String[] pkgNamePair = getFolderAndBaseName(getRelativePath(root,file),File.separatorChar);
129:            //        return new RegularFileObject( file, convertFolder2Package(pkgNamePair[0], File.separatorChar), pkgNamePair[1], filter);
130:            //    }
131:            //    public static /*Java*/FileObject fileFileObject( final File file, final File root, Object/* final JavaFileFilterImplementation*/ filter) {
132:            //        assert file != null;
133:            //        assert root != null;
134:            //        // Ugh this is a performance KILLER
135:            //        return FileUtil.toFileObject(file); // XXX consider root?
136:            //    }
137:
138:            public static ParserFile fileFileObject(final File file,
139:                    final File root, boolean platform,
140:                    Object/* final JavaFileFilterImplementation*/filter) {
141:                assert file != null;
142:                assert root != null;
143:                return new FileParserFile(file, root, platform);
144:            }
145:
146:            private static class FileParserFile implements  ParserFile {
147:                private File file;
148:                private File root;
149:                private FileObject fileObject;
150:                private String relative;
151:                private boolean platform;
152:
153:                private FileParserFile(File file, File root, boolean platform) {
154:                    this .file = file;
155:                    this .root = root;
156:                    this .platform = platform;
157:                }
158:
159:                public FileObject getFileObject() {
160:                    if (fileObject == null) {
161:                        fileObject = FileUtil.toFileObject(file);
162:                    }
163:                    return fileObject;
164:                }
165:
166:                public String getRelativePath() {
167:                    if (relative == null) {
168:                        relative = FileObjects.getRelativePath(root, file);
169:                    }
170:
171:                    return relative;
172:                }
173:
174:                public String getNameExt() {
175:                    return file.getName();
176:                }
177:
178:                public String getExtension() {
179:                    String name = file.getName();
180:                    int index = name.lastIndexOf('.');
181:                    if (index != -1) {
182:                        return name.substring(index + 1);
183:                    } else {
184:                        return "";
185:                    }
186:                }
187:
188:                public String toString() {
189:                    return "FileParserFile(" + getNameExt() + ")";
190:                }
191:
192:                public boolean isPlatform() {
193:                    return platform;
194:                }
195:
196:                public File getFile() {
197:                    return file;
198:                }
199:            }
200:
201:            //    
202:            //    /**
203:            //     * Creates {@link JavaFileObject} for a NetBeans {@link FileObject}
204:            //     * Any client which needs to create {@link JavaFileObject} for java
205:            //     * source file should use this factory method.
206:            //     * @param {@link FileObject} for which the {@link JavaFileObject} should be created
207:            //     * @return {@link JavaFileObject}, never returns null
208:            //     * @exception {@link IOException} may be thrown
209:            //     */
210:            //    public static /*Java*/FileObject nbFileObject (final FileObject file) throws IOException {
211:            //        return nbFileObject (file, null, false);
212:            //    }
213:            //    
214:            //    /**
215:            //     * Creates {@link JavaFileObject} for a NetBeans {@link FileObject}
216:            //     * Any client which needs to create {@link JavaFileObject} for java
217:            //     * source file should use this factory method.
218:            //     * @param {@link FileObject} for which the {@link JavaFileObject} should be created
219:            //     * @param renderNow if true the snap shot of the file is taken immediately
220:            //     * @return {@link JavaFileObject}, never returns null
221:            //     * @exception {@link IOException} may be thrown
222:            //     */
223:            //    public static /*Java*/FileObject nbFileObject (final FileObject file, JavaFileFilterImplementation filter, boolean renderNow) throws IOException {
224:            //        assert file != null;
225:            //        if (!file.isValid() || file.isVirtual()) {
226:            //            throw new InvalidFileException (file);
227:            //        }
228:            //        return new SourceFileObject (file, filter, renderNow);
229:            //    }
230:            //    
231:            //    /**
232:            //     * Creates virtual {@link JavaFileObject} with given name and content.
233:            //     * This method should be used only by tests, regular client should never
234:            //     * use this method.
235:            //     * @param content the content of the {@link JavaFileObject}
236:            //     * @param name the name of the {@link JavaFileObject}
237:            //     * @return {@link JavaFileObject}, never returns null
238:            //     */
239:            //    public static JavaFileObject memoryFileObject( CharSequence content, CharSequence name ) {
240:            //        final String nameStr = name.toString();
241:            //        if (!nameStr.equals(getBaseName(nameStr))) {
242:            //            throw new IllegalArgumentException ("Memory is flat");      //NOI18N
243:            //        }
244:            //        int length = content.length();        
245:            //        if ( length != 0 && Character.isWhitespace( content.charAt( length - 1 ) ) ) {
246:            //            return new MemoryFileObject( nameStr, CharBuffer.wrap( content ) );
247:            //        }
248:            //        else {
249:            //            return new MemoryFileObject( nameStr, (CharBuffer)CharBuffer.allocate( length + 1 ).append( content ).append( ' ' ).flip() );
250:            //        }
251:            //        
252:            //    }            
253:
254:            public static String stripExtension(String fileName) {
255:                int dot = fileName.lastIndexOf(".");
256:                return (dot == -1 ? fileName : fileName.substring(0, dot));
257:            }
258:
259:            //    /**
260:            //     * Returns the name of JavaFileObject, similar to
261:            //     * {@link java.io.File#getName}
262:            //     */
263:            //    public static String getName (final JavaFileObject fo, final boolean noExt) {
264:            //        assert fo != null;
265:            //        if (fo instanceof Base) {
266:            //            Base baseFileObject = (Base) fo;
267:            //            if (noExt) {
268:            //                return baseFileObject.getName();
269:            //            }
270:            //            else {                
271:            //                StringBuilder sb = new StringBuilder ();
272:            //                sb.append (baseFileObject.getName());
273:            //                sb.append('.'); //NOI18N
274:            //                sb.append(baseFileObject.getExt());
275:            //                return sb.toString();
276:            //            }
277:            //        }
278:            //        try {
279:            //            final URL url = fo.toUri().toURL();
280:            //            String path = url.getPath();
281:            //            int index1 = path.lastIndexOf('/');
282:            //            int len;
283:            //            if (noExt) {
284:            //               final int index2 = path.lastIndexOf('.');
285:            //               if (index2>index1) {
286:            //                   len = index2;
287:            //               }
288:            //               else {
289:            //                   len = path.length();
290:            //               }
291:            //            }
292:            //            else {
293:            //                len = path.length();
294:            //            }
295:            //            path = path.substring(index1+1,len);
296:            //            return path;
297:            //        } catch (MalformedURLException e) {
298:            //            return null;
299:            //        }        
300:            //    }
301:            /**
302:             * Returns the name of JavaFileObject, similar to
303:             * {@link java.io.File#getName}
304:             */
305:            public static String getName(final/*Java*/FileObject fo,
306:                    final boolean noExt) {
307:                assert fo != null;
308:                return noExt ? fo.getName() : fo.getNameExt();
309:            }
310:
311:            /**
312:             * Returns the basename name without folder path
313:             *  @param file name, eg. obtained from {@link FileObjects#getPath} or {java.io.File.getPath}
314:             *  @return the base name
315:             *  @see #getBaseName(String,char)
316:             */
317:            public static String getBaseName(String fileName) {
318:                return getBaseName(fileName, File.separatorChar);
319:            }
320:
321:            /**
322:             * Returns the basename name without folder path. You can specify
323:             * the path separator since eg zip files uses '/' regardless of platform.
324:             *  @param file name, eg. obtained from {@link FileObjects#getPath} or {java.io.File.getPath}
325:             *  @param separator path separator
326:             *  @return the base name
327:             */
328:            public static String getBaseName(String fileName, char separator) {
329:                return getFolderAndBaseName(fileName, separator)[1];
330:            }
331:
332:            /**
333:             *Returns the folder (package name separated by original separators)
334:             *and base name.
335:             * @param path
336:             * @return array of 2 strings, 1st the folder 2nd the base name
337:             */
338:            public static String[] getFolderAndBaseName(final String fileName,
339:                    final char separator) {
340:                final int i = fileName.lastIndexOf(separator);
341:                if (i == -1) {
342:                    return new String[] { "", fileName }; //NOI18N
343:                } else {
344:                    return new String[] { fileName.substring(0, i),
345:                            fileName.substring(i + 1) };
346:                }
347:            }
348:
349:            public static String getBinaryName(final File file, final File root) {
350:                assert file != null && root != null;
351:                String fileName = FileObjects.getRelativePath(root, file);
352:                int index = fileName.lastIndexOf('.'); //NOI18N
353:                if (index > 0) {
354:                    fileName = fileName.substring(0, index);
355:                }
356:                return fileName.replace(File.separatorChar, '.'); //NOI18N        
357:            }
358:
359:            public static String getSimpleName( /*Java*/FileObject fo) {
360:
361:                String name = getName(fo, true);
362:                int i = name.lastIndexOf('$');
363:                if (i == -1) {
364:                    return name;
365:                } else {
366:                    return name.substring(i + 1);
367:                }
368:            }
369:
370:            public static String getSimpleName(String fileName) {
371:
372:                String name = getBaseName(fileName);
373:
374:                int i = name.lastIndexOf('$');
375:                if (i == -1) {
376:                    return name;
377:                } else {
378:                    return name.substring(i + 1);
379:                }
380:
381:            }
382:
383:            public static String convertPackage2Folder(String packageName) {
384:                return packageName.replace('.', '/');
385:            }
386:
387:            public static String convertFolder2Package(String packageName) {
388:                return convertFolder2Package(packageName, '/'); //NOI18N
389:            }
390:
391:            public static String convertFolder2Package(String packageName,
392:                    char folderSeparator) {
393:                return packageName.replace(folderSeparator, '.');
394:            }
395:
396:            public static String getRelativePath(final String packageName,
397:                    final String relativeName) {
398:                StringBuilder relativePath = new StringBuilder();
399:                relativePath.append(packageName.replace('.', '/'));
400:                relativePath.append(relativeName);
401:                return relativePath.toString();
402:            }
403:
404:            public static String[] getParentRelativePathAndName(
405:                    final String className) {
406:                if (className.charAt(className.length() - 1) == '.') {
407:                    return null;
408:                }
409:                final int index = className.lastIndexOf('.');
410:                if (index < 0) {
411:                    return new String[] { "", //NOI18N
412:                            className };
413:                } else {
414:                    return new String[] {
415:                            className.substring(0, index).replace('.', '/'), //NOI18N
416:                            className.substring(index + 1) };
417:                }
418:            }
419:
420:            public static File getRootFile(final URL url) {
421:                File rootFile;
422:                if ("jar".equals(url.getProtocol())) { //NOI18N
423:                    rootFile = new File(URI.create(FileUtil.getArchiveFile(url)
424:                            .toExternalForm()));
425:                } else {
426:                    rootFile = new File(URI.create(url.toExternalForm()));
427:                }
428:                return rootFile;
429:            }
430:
431:            public static void deleteRecursively(final File folder) {
432:                assert folder != null;
433:                if (folder.isDirectory()) {
434:                    File[] children = folder.listFiles();
435:                    if (children != null) {
436:                        for (File file : children) {
437:                            deleteRecursively(file);
438:                        }
439:                    }
440:                }
441:                folder.delete();
442:            }
443:
444:            // Private methods ---------------------------------------------------------
445:
446:            // Innerclasses ------------------------------------------------------------
447:
448:            //    public static abstract class Base implements JavaFileObject {
449:            //
450:            //        protected final JavaFileObject.Kind kind;
451:            //        protected final String pkgName;
452:            //        protected final String nameWithoutExt;
453:            //        protected final String ext;        
454:            //        
455:            //        protected Base (final String pkgName, final String name) {
456:            //            assert pkgName != null;
457:            //            assert name != null;
458:            //            this.pkgName = pkgName;
459:            //            String[] res = getNameExtPair(name);
460:            //            this.nameWithoutExt = res[0];
461:            //            this.ext = res[1];
462:            //            if (FileObjects.JAVA.equalsIgnoreCase(ext)) { //NOI18N
463:            //                this.kind = Kind.SOURCE;
464:            //            }
465:            //            else if (FileObjects.CLASS.equalsIgnoreCase(ext) || "sig".equals(ext)) {   //NOI18N
466:            //                this.kind = Kind.CLASS;
467:            //            }
468:            //            else if (FileObjects.HTML.equalsIgnoreCase(ext)) {    //NOI18N
469:            //                this.kind = Kind.HTML;
470:            //            }
471:            //            else {
472:            //                this.kind = Kind.OTHER;
473:            //            }
474:            //        }
475:            //        
476:            //        public JavaFileObject.Kind getKind() {
477:            //            return this.kind;
478:            //        }
479:            //        
480:            //        public boolean isNameCompatible (String simplename, JavaFileObject.Kind k) {
481:            //            if (this.kind != k) {
482:            //                return false;
483:            //            }
484:            //	    return nameWithoutExt.equals(simplename);
485:            //	}        
486:            //        
487:            //        public NestingKind getNestingKind() {
488:            //            return null;
489:            //        }
490:            //        
491:            //        public Modifier getAccessLevel() {
492:            //            return null;
493:            //        }
494:            //    
495:            //        @Override
496:            //        public String toString() {
497:            //            return this.toUri().toString();
498:            //        }
499:            //        
500:            //        public String getPackage () {
501:            //            return this.pkgName;
502:            //        }
503:            //        
504:            //        public String getNameWithoutExtension () {
505:            //            return this.nameWithoutExt;
506:            //        }
507:            //        
508:            //        public String getName () {
509:            //            return this.nameWithoutExt + '.' + ext;
510:            //        }
511:            //        
512:            //        public String getExt () {
513:            //            return this.ext;
514:            //        }        
515:            //        
516:            //        private static String[] getNameExtPair (String name) {
517:            //            int index = name.lastIndexOf ('.');            
518:            //            String namenx;
519:            //            String ext;
520:            //            if (index <= 0) {
521:            //                namenx =name;
522:            //                ext = "";   //NOI18N
523:            //            }
524:            //            else {
525:            //                namenx = name.substring(0,index);
526:            //                if (index == name.length()-1) {
527:            //                    ext = "";
528:            //                }
529:            //                else {
530:            //                    ext = name.substring(index+1);
531:            //                }
532:            //            }
533:            //            return new String[] {
534:            //              namenx,
535:            //              ext
536:            //            };
537:            //        }
538:            //    }
539:            //    
540:            //    public static abstract class FileBase extends Base {
541:            //        
542:            //        protected final File f;
543:            //        
544:            //        protected FileBase (final File file, final String pkgName, final String name) {
545:            //            super (pkgName, name);
546:            //            assert file != null;
547:            //            assert file.equals(FileUtil.normalizeFile(file));
548:            //            this.f = file;
549:            //        }
550:            //        
551:            //        public File getFile () {
552:            //            return this.f;
553:            //        }
554:            //    }
555:
556:            public static class InvalidFileException extends IOException {
557:
558:                public InvalidFileException() {
559:                    super ();
560:                }
561:
562:                public InvalidFileException(final FileObject fo) {
563:                    super (NbBundle.getMessage(FileObjects.class,
564:                            "FMT_InvalidFile", FileUtil.getFileDisplayName(fo)));
565:                }
566:            }
567:
568:            public static String getRelativePath(final File root, final File fo) {
569:                final String rootPath = root.getAbsolutePath();
570:                final String foPath = fo.getAbsolutePath();
571:                assert foPath.startsWith(rootPath);
572:                int index = rootPath.length();
573:                if (rootPath.charAt(index - 1) != File.separatorChar) {
574:                    index++;
575:                }
576:                int foIndex = foPath.length();
577:                if (foIndex <= index) {
578:                    return ""; //NOI18N
579:                }
580:                return foPath.substring(index);
581:            }
582:
583:            //    
584:            //    private static class RegularFileObject extends FileBase {
585:            //        
586:            //        private URI uriCache;
587:            //        private final JavaFileFilterImplementation filter;
588:            //
589:            //	public RegularFileObject(final File f, final String packageName, final String baseName, final JavaFileFilterImplementation filter) {
590:            //            super (f, packageName, baseName);
591:            //            this.filter = filter;
592:            //	}               
593:            //
594:            //        public InputStream openInputStream() throws IOException {
595:            //	    return new FileInputStream(f);
596:            //	}
597:            //
598:            //	public Reader openReader (boolean b) throws IOException {
599:            //	    throw new UnsupportedOperationException();
600:            //	}
601:            //
602:            //	public OutputStream openOutputStream() throws IOException {
603:            //	    return new FileOutputStream(f);
604:            //	}
605:            //
606:            //	public Writer openWriter() throws IOException {
607:            //	    //FIX: consider using encoding here
608:            //	    return new OutputStreamWriter(new FileOutputStream(f));
609:            //	}
610:            //
611:            //	public @Override boolean isNameCompatible(String simplename, JavaFileObject.Kind kind) {
612:            //	    boolean res = super.isNameCompatible(simplename, kind);
613:            //            if (res) {
614:            //                return res;
615:            //            }
616:            //            else if (Utilities.isWindows()) {
617:            //                return nameWithoutExt.equalsIgnoreCase(simplename);
618:            //            }
619:            //            else {
620:            //                return false;
621:            //            }
622:            //	} 	   
623:            //        
624:            //        public URI toUri () {
625:            //            if (this.uriCache == null) {
626:            //                this.uriCache = f.toURI();
627:            //            }
628:            //            return this.uriCache;
629:            //        }
630:            //
631:            //        public long getLastModified() {
632:            //	    return f.lastModified();
633:            //	}
634:            //
635:            //	public boolean delete() {
636:            //	    return f.delete();
637:            //	}
638:            //
639:            //	public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
640:            //            
641:            //            char[] result;
642:            //            InputStreamReader in = new InputStreamReader (new FileInputStream(this.f), encodingName);
643:            //            try {
644:            //                int len = (int)this.f.length();
645:            //                result = new char [len+1];
646:            //                int red = 0, rv;	    
647:            //                while ((rv=in.read(result,red,len-red))>0 && (red=red+rv)<len);
648:            //            } finally {
649:            //                in.close();
650:            //            }
651:            //            result[result.length-1]='\n'; //NOI18N
652:            //            CharSequence buffer = CharBuffer.wrap (result);
653:            //            if (this.filter != null) {
654:            //                buffer = this.filter.filterCharSequence(buffer);
655:            //            }
656:            //            return buffer;
657:            //	}
658:            //
659:            //	@Override
660:            //	public boolean equals(Object other) {
661:            //	    if (!(other instanceof RegularFileObject))
662:            //		return false;
663:            //	    RegularFileObject o = (RegularFileObject) other;
664:            //	    return f.equals(o.f);
665:            //	}
666:            //
667:            //	@Override
668:            //	public int hashCode() {
669:            //	    return f.hashCode();
670:            //	}
671:            //        
672:            //    }    
673:            //
674:            //    /** A subclass of FileObject representing zip entries.
675:            //     * XXX: What happens when the archive is deleted or rebuilt?
676:            //     */
677:            //    private abstract static class ZipFileBase extends Base {
678:            //        
679:            //        protected final long mtime;
680:            //        protected final String resName;
681:            //        
682:            //        public ZipFileBase (final String folderName, final String baseName, long mtime) {
683:            //            super (convertFolder2Package(folderName),baseName);
684:            //            this.mtime = mtime;
685:            //            if (folderName.length() == 0) {
686:            //                this.resName = baseName;
687:            //            }
688:            //            else {
689:            //                StringBuilder resName = new StringBuilder (folderName);
690:            //                resName.append('/');        //NOI18N
691:            //                resName.append(baseName);
692:            //                this.resName = resName.toString();
693:            //            }
694:            //        }
695:            //        
696:            //        public OutputStream openOutputStream() throws IOException {
697:            //	    throw new UnsupportedOperationException();
698:            //	}
699:            //
700:            //	public Reader openReader(boolean b) throws IOException {
701:            //	    throw new UnsupportedOperationException();
702:            //	}
703:            //
704:            //        public Writer openWriter() throws IOException {
705:            //	    throw new UnsupportedOperationException();
706:            //	}
707:            //        
708:            //        public long getLastModified() {
709:            //	    return mtime;
710:            //	}
711:            //
712:            //	public boolean delete() {
713:            //	    throw new UnsupportedOperationException();
714:            //	}
715:            //
716:            //	public CharBuffer getCharContent(boolean ignoreEncodingErrors) {
717:            //	    throw new UnsupportedOperationException();
718:            //	}
719:            //        
720:            //        public final URI toUri () {
721:            //            URI  zdirURI = this.getArchiveURI();
722:            //            return URI.create ("jar:"+zdirURI.toString()+"!/"+resName);  //NOI18N
723:            //        }
724:            //        
725:            //        @Override
726:            //	public int hashCode() {
727:            //	    return this.resName.hashCode();
728:            //	}                
729:            //        
730:            //	@Override
731:            //	public boolean equals(Object other) {
732:            //	    if (!(other instanceof ZipFileBase))
733:            //		return false;
734:            //	    ZipFileBase o = (ZipFileBase) other;
735:            //	    return getArchiveURI().equals(o.getArchiveURI()) && resName.equals(o.resName);
736:            //	}
737:            //        
738:            //        protected abstract URI getArchiveURI ();
739:            //        
740:            //    }
741:            //    
742:            //    private static class ZipFileObject extends ZipFileBase {
743:            //	
744:            //
745:            //	/** The zipfile containing the entry.
746:            //	 */
747:            //	private final File archiveFile;
748:            //        
749:            //
750:            //        ZipFileObject(final File archiveFile, final String folderName, final String baseName, long mtime) {
751:            //            super (folderName,baseName,mtime);
752:            //            assert archiveFile != null : "archiveFile == null";   //NOI18N
753:            //	    this.archiveFile = archiveFile;
754:            //            
755:            //	}
756:            //
757:            //        public InputStream openInputStream() throws IOException {            
758:            //            class ZipInputStream extends InputStream {
759:            //
760:            //                private ZipFile zipfile;
761:            //                private InputStream delegate;
762:            //
763:            //                public ZipInputStream (ZipFile zf) throws IOException {
764:            //                    this.zipfile = zf;
765:            //                    this.delegate = zf.getInputStream(new ZipEntry(resName));
766:            //                }
767:            //
768:            //                public int read() throws IOException {
769:            //                    throw new java.lang.UnsupportedOperationException("Not supported yet.");
770:            //                }
771:            //
772:            //                public int read(byte b[], int off, int len) throws IOException {
773:            //                    return delegate.read(b, off, len);
774:            //                }
775:            //
776:            //                public int available() throws IOException {
777:            //                    return this.delegate.available();
778:            //                }
779:            //
780:            //                public void close() throws IOException {
781:            //                    try {
782:            //                        this.delegate.close();
783:            //                    } finally {
784:            //                        this.zipfile.close();
785:            //                    }
786:            //                }
787:            //
788:            //
789:            //            };
790:            //            ZipFile zf = new ZipFile (archiveFile);
791:            //            return new ZipInputStream (zf);
792:            //	}
793:            //        
794:            //        public URI getArchiveURI () {
795:            //            return this.archiveFile.toURI();
796:            //        }
797:            //    }
798:            //    
799:            //    private static class CachedZipFileObject extends ZipFileBase {
800:            //        
801:            //        private ZipFile zipFile;
802:            //        
803:            //        CachedZipFileObject(final ZipFile zipFile, final String folderName, final String baseName, long mtime) {
804:            //            super (folderName,baseName,mtime);
805:            //            assert zipFile != null : "archiveFile == null";   //NOI18N
806:            //	    this.zipFile = zipFile;            
807:            //	}
808:            //        
809:            //        public InputStream openInputStream() throws IOException {
810:            //            return this.zipFile.getInputStream(new ZipEntry (this.resName));
811:            //	}
812:            //        
813:            //        public URI getArchiveURI () {
814:            //            return new File (this.zipFile.getName()).toURI();
815:            //        }
816:            //    }
817:            //    
818:            //    
819:            //    /** Temporay FileObject for parsing input stream.
820:            //     */    
821:            //    private static class MemoryFileObject extends Base {
822:            //        
823:            //        private String fileName;
824:            //        private CharBuffer cb;
825:            //        
826:            //        public MemoryFileObject( String fileName, CharBuffer cb ) {            
827:            //            super ("",fileName);    //NOI18N
828:            //            this.cb = cb;
829:            //            this.fileName = fileName;
830:            //        }                
831:            //        
832:            //
833:            //        /**
834:            //         * Get the character content of the file, if available.
835:            //         * @param ignoreEncodingErrors if true, encoding errros will be replaced by the 
836:            //         * default translation character; otherwise they should be reported as diagnostics.
837:            //         * @throws UnsupportedOperationException if character access is not supported
838:            //         */
839:            //        public java.nio.CharBuffer getCharContent(boolean ignoreEncodingErrors) throws java.io.IOException {
840:            //            return cb;
841:            //        }
842:            //
843:            //        public boolean delete() {
844:            //            // Do nothing
845:            //            return false;
846:            //        }        
847:            //
848:            //        public URI toUri () {
849:            //            return URI.create (this.nameWithoutExt);
850:            //        }
851:            //
852:            //        public long getLastModified() {
853:            //            return System.currentTimeMillis(); // XXX
854:            //        }
855:            //
856:            //        /**
857:            //         * Get an InputStream for this object.
858:            //         * 
859:            //         * @return an InputStream for this  object.
860:            //         * @throws UnsupportedOperationException if the byte access is not supported
861:            //         */
862:            //        public InputStream openInputStream() throws java.io.IOException {
863:            //            return new ByteArrayInputStream(cb.toString().getBytes("UTF-8"));
864:            //        }
865:            //
866:            //        /**
867:            //         * Get an OutputStream for this object.
868:            //         * 
869:            //         * @return an OutputStream for this  object.
870:            //         * @throws UnsupportedOperationException if byte access is not supported
871:            //         */
872:            //        public java.io.OutputStream openOutputStream() throws java.io.IOException {
873:            //            throw new UnsupportedOperationException();
874:            //        }
875:            //
876:            //        /**
877:            //         * Get a reader for this object.
878:            //         * 
879:            //         * @return a Reader for this file object.
880:            //         * @throws UnsupportedOperationException if character access is not supported
881:            //         * @throws IOException if an error occurs while opening the reader
882:            //         */
883:            //        public java.io.Reader openReader (boolean b) throws java.io.IOException {
884:            //            throw new UnsupportedOperationException();
885:            //        }
886:            //
887:            //        /**
888:            //         * Get a writer for this object.
889:            //         * @throws UnsupportedOperationException if character access is not supported
890:            //         * @throws IOException if an error occurs while opening the writer
891:            //         */
892:            //        public java.io.Writer openWriter() throws java.io.IOException {
893:            //            throw new UnsupportedOperationException();
894:            //        }
895:            //        
896:            //    }    
897:
898:            private static class SimpleNameStringComparator implements 
899:                    Comparator<String> {
900:
901:                public int compare(String o1, String o2) {
902:                    return getSimpleName(o1).compareTo(getSimpleName(o2));
903:                }
904:
905:            }
906:
907:            private static class SimpleNameFileObjectComparator implements 
908:                    Comparator</*Java*/FileObject> {
909:
910:                public int compare( /*Java*/FileObject o1, /*Java*/
911:                        FileObject o2) {
912:
913:                    String n1 = getSimpleName(o1);
914:                    String n2 = getSimpleName(o2);
915:
916:                    return n1.compareTo(n2);
917:                }
918:
919:            }
920:
921:            static final String encodingName = new OutputStreamWriter(
922:                    new ByteArrayOutputStream()).getEncoding();
923:
924:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.