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.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 import java.io.IOException;
27 import java.nio.file.Path;
28 import java.util.Collection;
29 import java.util.HashMap;
30 import java.util.HashSet;
31 import java.util.Map;
32 import java.util.Set;
33
34
35
36
37
38 public class PackageManager
39 {
40 private static final Logger LOGGER = LoggerFactory.getLogger( PackageManager.class );
41
42 private final FileManager fileManager;
43
44 private Set<Path> directories = new HashSet<>();
45
46
47
48
49 private Map<String, PackageType> packages = new HashMap<>();
50
51
52
53
54 private PackageType defaultPackage = new PackageType();
55
56
57
58
59
60 private String[] excludes = null;
61
62
63
64
65 private String[] includes = { "**/*.java" };
66
67 public PackageManager( FileManager fileManager )
68 {
69 this.fileManager = fileManager;
70 }
71
72
73
74
75
76 public PackageType getPackageType( String name )
77 {
78
79
80 if ( name == null )
81 {
82 return defaultPackage;
83 }
84
85 return this.packages.get( name );
86 }
87
88
89
90
91 public void addPackageType( PackageType packageType )
92 {
93 this.packages.put( packageType.getName(), packageType );
94 }
95
96
97
98
99 public Collection<PackageType> getPackageTypes()
100 {
101 return packages.values();
102 }
103
104
105
106
107 private void parse( Path baseDir )
108 {
109
110
111 LOGGER.debug( "Scanning " + baseDir );
112 DirectoryScanner directoryScanner = new DirectoryScanner();
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 LOGGER.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 );
165 }
166 }
167
168
169
170
171 public void dump()
172 {
173
174 LOGGER.debug( "Dumping out PackageManager structure" );
175
176 for ( PackageType current : getPackageTypes() )
177 {
178 LOGGER.debug( current.getName() );
179
180
181 for ( ClassType currentClass : current.getClassTypes() )
182 {
183 LOGGER.debug( '\t' + currentClass.getName() );
184 }
185 }
186 }
187
188 public void setExcludes( String[] excludes )
189 {
190 this.excludes = excludes;
191 }
192
193
194 public void setIncludes( String[] includes )
195 {
196 this.includes = includes;
197 }
198 }
199