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