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.File;
26  import java.io.IOException;
27  import java.util.Enumeration;
28  import java.util.Hashtable;
29  
30  /**
31   * Given a list of directories, parse them out and store them as rendered
32   * packages, classes, imports, etc.
33   */
34  public class PackageManager
35  {
36      private final Log log;
37  
38      private Hashtable<String, String> directories = new Hashtable<String, String>();
39  
40      /**
41       * All the packages that have been parsed
42       */
43      private Hashtable<String, PackageType> packages = new Hashtable<String, PackageType>();
44  
45      /**
46       * The default Java package.
47       */
48      private PackageType defaultPackage = new PackageType();
49  
50      private FileManager fileManager;
51  
52      /**
53       * The list of exclude patterns to use.
54       */
55      private String[] excludes = null;
56  
57      /**
58       * The list of include patterns to use.
59       */
60      private String[] includes = { "**/*.java" };
61  
62      public PackageManager( Log log, FileManager fileManager )
63      {
64          this.log = log;
65          this.fileManager = fileManager;
66      }
67  
68      /**
69       * Given the name of a package (Ex: org.apache.maven.util) obtain it from
70       * the PackageManager
71       */
72      public PackageType getPackageType( String name )
73      {
74  
75          //return the default package if the name is null.
76          if ( name == null )
77          {
78              return defaultPackage;
79          }
80  
81          return this.packages.get( name );
82      }
83  
84      /**
85       * Add a package to the PackageManager
86       */
87      public void addPackageType( PackageType packageType )
88      {
89          this.packages.put( packageType.getName(), packageType );
90      }
91  
92      /**
93       * Get all of the packages in the PackageManager
94       */
95      public Enumeration<PackageType> getPackageTypes()
96      {
97          return packages.elements();
98      }
99  
100     /**
101      * Parse out all the directories on which this depends.
102      */
103     private void parse( String directory )
104     {
105         // Go through each directory and get the java source 
106         // files for this dir.
107         log.debug( "Scanning " + directory );
108         DirectoryScanner directoryScanner = new DirectoryScanner();
109         File baseDir = new File( directory );
110         directoryScanner.setBasedir( baseDir );
111         directoryScanner.setExcludes( excludes );
112         directoryScanner.setIncludes( includes );
113         directoryScanner.scan();
114 
115         for ( String file : directoryScanner.getIncludedFiles() )
116         {
117             log.debug( "parsing... " + file );
118 
119             //now parse out this file to get the packages/classname/etc
120             try
121             {
122                 String fileName = new File( baseDir, file ).getAbsolutePath();
123                 JavaFile jfi = fileManager.getFile( fileName );
124 
125                 // now that we have this parsed out blend its information
126                 // with the current package structure
127                 PackageType jp = this.getPackageType( jfi.getPackageType().getName() );
128 
129                 if ( jp == null )
130                 {
131                     this.addPackageType( jfi.getPackageType() );
132                     jp = jfi.getPackageType();
133                 }
134 
135                 // Add the current file's class(es) to this global package.
136                 if ( jfi.getClassTypes() != null && !jfi.getClassTypes().isEmpty() )
137                 {
138                     for ( ClassType ct : jfi.getClassTypes() )
139                     {
140                         jp.addClassType( ct );
141                     }
142                 }
143 
144             }
145             catch ( IOException e )
146             {
147                 e.printStackTrace();
148             }
149 
150         }
151 
152     }
153 
154     /**
155      * Description of the Method
156      */
157     public void process( String directory )
158     {
159         if ( this.directories.get( directory ) == null )
160         {
161             this.parse( directory );
162             this.directories.put( directory, directory );
163         }
164     }
165 
166     /**
167      * Description of the Method
168      */
169     public void process( String[] directories )
170     {
171 
172         for ( int i = 0; i < directories.length; ++i )
173         {
174             this.process( directories[i] );
175         }
176 
177     }
178 
179     /**
180      * Dump the package information to STDOUT. FOR DEBUG ONLY
181      */
182     public void dump()
183     {
184 
185         log.debug( "Dumping out PackageManager structure" );
186 
187         Enumeration<PackageType> pts = this.getPackageTypes();
188 
189         while ( pts.hasMoreElements() )
190         {
191 
192             //get the current package and print it.
193             PackageType current = pts.nextElement();
194 
195             log.debug( current.getName() );
196 
197             //get the classes under the package and print those too.
198             Enumeration<ClassType> classes = current.getClassTypes();
199 
200             while ( classes.hasMoreElements() )
201             {
202 
203                 ClassType currentClass = classes.nextElement();
204 
205                 log.debug( "\t" + currentClass.getName() );
206 
207             }
208         }
209     }
210 
211     public FileManager getFileManager()
212     {
213         return fileManager;
214     }
215 
216     public void setExcludes( String[] excludes )
217     {
218         this.excludes = excludes;
219     }
220 
221 
222     public void setIncludes( String[] includes )
223     {
224         this.includes = includes;
225     }
226 }
227