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.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   * Given a list of directories, parse them out and store them as rendered
36   * packages, classes, imports, etc.
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       * All the packages that have been parsed
48       */
49      private Map<String, PackageType> packages = new HashMap<>();
50  
51      /**
52       * The default Java package.
53       */
54      private PackageType defaultPackage = new PackageType();
55  
56  
57      /**
58       * The list of exclude patterns to use.
59       */
60      private String[] excludes = null;
61  
62      /**
63       * The list of include patterns to use.
64       */
65      private String[] includes = { "**/*.java" };
66  
67      public PackageManager( FileManager fileManager )
68      {
69          this.fileManager = fileManager;
70      }
71  
72      /**
73       * Given the name of a package (Ex: org.apache.maven.util) obtain it from
74       * the PackageManager
75       */
76      public PackageType getPackageType( String name )
77      {
78  
79          //return the default package if the name is null.
80          if ( name == null )
81          {
82              return defaultPackage;
83          }
84  
85          return this.packages.get( name );
86      }
87  
88      /**
89       * Add a package to the PackageManager
90       */
91      public void addPackageType( PackageType packageType )
92      {
93          this.packages.put( packageType.getName(), packageType );
94      }
95  
96      /**
97       * Get all of the packages in the PackageManager
98       */
99      public Collection<PackageType> getPackageTypes()
100     {
101         return packages.values();
102     }
103 
104     /**
105      * Parse out all the directories on which this depends.
106      */
107     private void parse( Path baseDir )
108     {
109         // Go through each directory and get the java source 
110         // files for this dir.
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             //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 );
165         }
166     }
167 
168     /**
169      * Dump the package information to STDOUT. FOR DEBUG ONLY
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             //get the classes under the package and print those too.
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