1 package org.apache.maven.jxr.pacman;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.codehaus.plexus.util.DirectoryScanner;
23 import org.apache.maven.jxr.log.Log;
24
25 import java.io.IOException;
26 import java.nio.file.Path;
27 import java.nio.file.Paths;
28 import java.util.Enumeration;
29 import java.util.HashSet;
30 import java.util.Hashtable;
31 import java.util.Set;
32
33
34
35
36
37 public class PackageManager
38 {
39 private final Log log;
40
41 private Set<Path> directories = new HashSet<>();
42
43
44
45
46 private Hashtable<String, PackageType> packages = new Hashtable<>();
47
48
49
50
51 private PackageTypee.html#PackageType">PackageType defaultPackage = new PackageType();
52
53 private FileManager fileManager;
54
55
56
57
58 private String[] excludes = null;
59
60
61
62
63 private String[] includes = { "**/*.java" };
64
65 public PackageManager( Log log, FileManager fileManager )
66 {
67 this.log = log;
68 this.fileManager = fileManager;
69 }
70
71
72
73
74
75 public PackageType getPackageType( String name )
76 {
77
78
79 if ( name == null )
80 {
81 return defaultPackage;
82 }
83
84 return this.packages.get( name );
85 }
86
87
88
89
90 public void addPackageType( PackageType packageType )
91 {
92 this.packages.put( packageType.getName(), packageType );
93 }
94
95
96
97
98 public Enumeration<PackageType> getPackageTypes()
99 {
100 return packages.elements();
101 }
102
103
104
105
106 private void parse( String directory )
107 {
108
109
110 log.debug( "Scanning " + directory );
111 DirectoryScanner directoryScanner = new DirectoryScanner();
112 Path baseDir = Paths.get( directory );
113 directoryScanner.setBasedir( baseDir.toFile() );
114 directoryScanner.setExcludes( excludes );
115 directoryScanner.setIncludes( includes );
116 directoryScanner.scan();
117
118 for ( String file : directoryScanner.getIncludedFiles() )
119 {
120 log.debug( "parsing... " + file );
121
122
123 try
124 {
125 Path fileName = baseDir.resolve( file );
126 JavaFile jfi = fileManager.getFile( fileName );
127
128
129
130 PackageType jp = this.getPackageType( jfi.getPackageType().getName() );
131
132 if ( jp == null )
133 {
134 this.addPackageType( jfi.getPackageType() );
135 jp = jfi.getPackageType();
136 }
137
138
139 if ( jfi.getClassTypes() != null && !jfi.getClassTypes().isEmpty() )
140 {
141 for ( ClassType ct : jfi.getClassTypes() )
142 {
143 jp.addClassType( ct );
144 }
145 }
146
147 }
148 catch ( IOException e )
149 {
150 e.printStackTrace();
151 }
152
153 }
154
155 }
156
157
158
159
160 public void process( Path directory )
161 {
162 if ( this.directories.add( directory ) )
163 {
164 this.parse( directory.toString() );
165 }
166 }
167
168
169
170
171 public void dump()
172 {
173
174 log.debug( "Dumping out PackageManager structure" );
175
176 Enumeration<PackageType> pts = this.getPackageTypes();
177
178 while ( pts.hasMoreElements() )
179 {
180
181
182 PackageType current = pts.nextElement();
183
184 log.debug( current.getName() );
185
186
187 Enumeration<ClassType> classes = current.getClassTypes();
188
189 while ( classes.hasMoreElements() )
190 {
191
192 ClassType currentClass = classes.nextElement();
193
194 log.debug( "\t" + currentClass.getName() );
195
196 }
197 }
198 }
199
200 public FileManager getFileManager()
201 {
202 return fileManager;
203 }
204
205 public void setExcludes( String[] excludes )
206 {
207 this.excludes = excludes;
208 }
209
210
211 public void setIncludes( String[] includes )
212 {
213 this.includes = includes;
214 }
215 }
216