View Javadoc
1   package org.apache.maven.jxr.pacman;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * Given a list of directories, parse them out and store them as rendered
35   * packages, classes, imports, etc.
36   */
37  public class PackageManager
38  {
39      private final Log log;
40  
41      private Set<Path> directories = new HashSet<>();
42  
43      /**
44       * All the packages that have been parsed
45       */
46      private Hashtable<String, PackageType> packages = new Hashtable<>();
47  
48      /**
49       * The default Java package.
50       */
51      private PackageTypee.html#PackageType">PackageType defaultPackage = new PackageType();
52  
53      private FileManager fileManager;
54  
55      /**
56       * The list of exclude patterns to use.
57       */
58      private String[] excludes = null;
59  
60      /**
61       * The list of include patterns to use.
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       * Given the name of a package (Ex: org.apache.maven.util) obtain it from
73       * the PackageManager
74       */
75      public PackageType getPackageType( String name )
76      {
77  
78          //return the default package if the name is null.
79          if ( name == null )
80          {
81              return defaultPackage;
82          }
83  
84          return this.packages.get( name );
85      }
86  
87      /**
88       * Add a package to the PackageManager
89       */
90      public void addPackageType( PackageType packageType )
91      {
92          this.packages.put( packageType.getName(), packageType );
93      }
94  
95      /**
96       * Get all of the packages in the PackageManager
97       */
98      public Enumeration<PackageType> getPackageTypes()
99      {
100         return packages.elements();
101     }
102 
103     /**
104      * Parse out all the directories on which this depends.
105      */
106     private void parse( String directory )
107     {
108         // Go through each directory and get the java source 
109         // files for this dir.
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             //now parse out this file to get the packages/classname/etc
123             try
124             {
125                 Path fileName = baseDir.resolve( file );
126                 JavaFile jfi = fileManager.getFile( fileName );
127 
128                 // now that we have this parsed out blend its information
129                 // with the current package structure
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                 // Add the current file's class(es) to this global package.
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      * Description of the Method
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      * Dump the package information to STDOUT. FOR DEBUG ONLY
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             //get the current package and print it.
182             PackageType current = pts.nextElement();
183 
184             log.debug( current.getName() );
185 
186             //get the classes under the package and print those too.
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