KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.Collection JavaDoc;
26 import java.util.Collections JavaDoc;
27 import java.util.Comparator JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.LinkedList JavaDoc;
30 import java.util.List JavaDoc;
31 import javax.tools.JavaFileObject;
32 import org.netbeans.api.java.classpath.ClassPath;
33 import org.netbeans.modules.java.source.TestUtil;
34 import org.netbeans.modules.java.preprocessorbridge.spi.JavaFileFilterImplementation;
35 import org.netbeans.modules.java.source.util.Factory;
36
37 /** Class which takes a folder and behaves like an archive should behave.
38  * It does not apply any optimalizations. However it is good for comparing
39  * the results from real archives. Also contains some utility methods for
40  * comparing with other archives.
41  *
42  * @author Petr Hrebejk
43  */

44 class GoldenArchive implements Archive {
45
46     private File JavaDoc rootFolder;
47     private int rootLength;
48     
49     // Comparison methods ------------------------------------------------------
50

51 // public String getFoldersDiff( Archive archive ) {
52
// return TestUtil.collectionDiff( getFolders(), archive.getFolders() );
53
// }
54
//
55
// public String getFilesDiff( Archive archive ) {
56
// return TestUtil.collectionDiff( getFiles(), archive.getFiles() );
57
// }
58
//
59
// public String getFoldersDiff( String folderName, Archive archive ) {
60
// return TestUtil.collectionDiff( getFolders( folderName ), archive.getFolders( folderName ) );
61
// }
62

63     public String JavaDoc getFilesDiff( String JavaDoc folderName, Archive archive ) throws IOException JavaDoc {
64         return TestUtil.collectionDiff( getFiles( folderName, null, null), archive.getFiles( folderName, null, null) );
65     }
66
67     // Implementation of Archive -----------------------------------------------
68

69     public GoldenArchive( File JavaDoc rootFolder ) {
70         if ( !rootFolder.isDirectory() ) {
71             throw new IllegalArgumentException JavaDoc( "Root folder has to be a directory." );
72         }
73         if ( !rootFolder.canRead() ) {
74             throw new IllegalArgumentException JavaDoc( "Root folder has to be readable." );
75         }
76
77         this.rootFolder = rootFolder;
78         this.rootLength = rootFolder.getPath().length();
79     }
80
81
82     public Iterable JavaDoc<JavaFileObject> getFiles( String JavaDoc folderName, ClassPath.Entry e, JavaFileFilterImplementation filter ) {
83
84         File JavaDoc folder = new File JavaDoc( rootFolder, folderName );
85         
86         if ( !folder.exists() ) {
87             return null;
88         }
89                 
90         File JavaDoc[] files = folder.listFiles();
91         List JavaDoc<JavaFileObject> entries = new ArrayList JavaDoc<JavaFileObject>();
92         
93         for( File JavaDoc f : files ) {
94             if ( !f.isDirectory() ) {
95                 entries.add( FileObjects.fileFileObject (f, rootFolder, null));
96             }
97         }
98         
99         Collections.sort( entries, new Comparator JavaDoc<JavaFileObject> () {
100             public int compare (JavaFileObject o1, JavaFileObject o2) {
101                 return o1.toUri().toString().compareTo(o2.toUri().toString());
102             }
103         });
104
105         return entries;
106     }
107     
108     public void clear () {
109     }
110     
111     public void add (String JavaDoc pkg, String JavaDoc name) {
112         
113     }
114     
115     public void delete (String JavaDoc pkg, String JavaDoc name) {
116         
117     }
118     
119             
120     // Private methods -----------------------------------------------------
121

122     /** Finds all subfoders of given folder
123      */

124     private void storeFolders( Collection JavaDoc<File JavaDoc> dest, File JavaDoc folder, boolean isRoot ) {
125
126         File JavaDoc files[] = folder.listFiles();
127
128         boolean hasFiles = false;
129         for (File JavaDoc f : files) {
130             if ( f.isDirectory() ) {
131                 storeFolders( dest, f, false );
132             }
133             else {
134                 hasFiles = true;
135             }
136         }
137         
138         if ( hasFiles && !isRoot ) {
139             dest.add( folder );
140         }
141         
142     }
143
144     /** Find all files in given folder
145      */

146     private void storeFiles( Collection JavaDoc<File JavaDoc> dest, File JavaDoc folder ) {
147
148         File JavaDoc files[] = folder.listFiles();
149
150         for ( File JavaDoc f : files) {
151             if ( f.isDirectory() ) {
152                 storeFiles( dest, f );
153             }
154             else {
155                 dest.add( f );
156             }
157         }
158     }
159
160     
161 }
Popular Tags