View Javadoc

1   package org.apache.maven.shared.dependency.analyzer;
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 java.io.File;
23  import java.io.FileInputStream;
24  import java.io.IOException;
25  import java.io.InputStream;
26  import java.net.URI;
27  import java.net.URISyntaxException;
28  import java.net.URL;
29  import java.util.jar.JarEntry;
30  import java.util.jar.JarInputStream;
31  
32  import org.codehaus.plexus.util.DirectoryScanner;
33  
34  /**
35   * 
36   * 
37   * @author <a href="mailto:markhobson@gmail.com">Mark Hobson</a>
38   * @version $Id: ClassFileVisitorUtils.java 661727 2008-05-30 14:21:49Z bentmann $
39   */
40  public final class ClassFileVisitorUtils
41  {
42      // constants --------------------------------------------------------------
43  
44      private static final String[] CLASS_INCLUDES = { "**/*.class" };
45  
46      // constructors -----------------------------------------------------------
47  
48      private ClassFileVisitorUtils()
49      {
50          // private constructor for utility class
51      }
52  
53      // public methods ---------------------------------------------------------
54  
55      public static void accept( URL url, ClassFileVisitor visitor )
56          throws IOException
57      {
58          if ( url.getPath().endsWith( ".jar" ) )
59          {
60              acceptJar( url, visitor );
61          }
62          else if ( url.getProtocol().equalsIgnoreCase( "file" ) )
63          {
64              try
65              {
66                  File file = new File( new URI( url.toString() ) );
67  
68                  if ( file.isDirectory() )
69                  {
70                      acceptDirectory( file, visitor );
71                  }
72                  else if ( file.exists() )
73                  {
74                      throw new IllegalArgumentException( "Cannot accept visitor on URL: " + url );
75                  }
76              }
77              catch ( URISyntaxException exception )
78              {
79                  IllegalArgumentException e = new IllegalArgumentException( "Cannot accept visitor on URL: " + url );
80                  e.initCause( exception );
81                  throw e;
82              }
83          }
84          else
85          {
86              throw new IllegalArgumentException( "Cannot accept visitor on URL: " + url );
87          }
88      }
89  
90      // private methods --------------------------------------------------------
91  
92      private static void acceptJar( URL url, ClassFileVisitor visitor )
93          throws IOException
94      {
95          JarInputStream in = new JarInputStream( url.openStream() );
96  
97          JarEntry entry = null;
98  
99          while ( ( entry = in.getNextJarEntry() ) != null )
100         {
101             String name = entry.getName();
102 
103             if ( name.endsWith( ".class" ) )
104                 visitClass( name, in, visitor );
105         }
106 
107         in.close();
108     }
109 
110     private static void acceptDirectory( File directory, ClassFileVisitor visitor )
111         throws IOException
112     {
113         if ( !directory.isDirectory() )
114             throw new IllegalArgumentException( "File is not a directory" );
115 
116         DirectoryScanner scanner = new DirectoryScanner();
117 
118         scanner.setBasedir( directory );
119         scanner.setIncludes( CLASS_INCLUDES );
120 
121         scanner.scan();
122 
123         String[] paths = scanner.getIncludedFiles();
124 
125         for ( int i = 0; i < paths.length; i++ )
126         {
127             String path = paths[i].replace( File.separatorChar, '/' );
128 
129             File file = new File( directory, path );
130             FileInputStream in = new FileInputStream( file );
131 
132             visitClass( path, in, visitor );
133 
134             in.close();
135         }
136     }
137 
138     private static void visitClass( String path, InputStream in, ClassFileVisitor visitor )
139     {
140         if ( !path.endsWith( ".class" ) )
141             throw new IllegalArgumentException( "Path is not a class" );
142 
143         String className = path.substring( 0, path.length() - 6 );
144 
145         className = className.replace( '/', '.' );
146 
147         visitor.visitClass( className, in );
148     }
149 }