View Javadoc

1   /*
2    * Copyright 2005 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.vafer.jar;
17  
18  import java.io.IOException;
19  import java.util.jar.JarEntry;
20  import java.util.jar.JarInputStream;
21  
22  import org.apache.commons.io.IOUtils;
23  import org.apache.commons.io.output.NullOutputStream;
24  import org.vafer.jar.handler.JarHandler;
25  
26  
27  public final class JarProcessor {
28  
29  	public void processJars( final Jar[] pJars, final JarHandler pHandler ) throws IOException {
30  
31          pHandler.onStartProcessing();        
32          
33          for (int i = 0; i < pJars.length; i++) {
34          	final Jar jar = pJars[i];
35          	
36          	pHandler.onStartJar(jar);
37          	
38              final JarInputStream inputStream = new JarInputStream(jar.getInputStream());
39  
40              while (true) {
41                  final JarEntry entry = inputStream.getNextJarEntry();
42                  
43                  if (entry == null) {
44                      break;
45                  }
46                  
47                  // TODO: really?
48                  if (entry.isDirectory()) {
49                      // ignore directory entries
50                  	IOUtils.copy(inputStream, new NullOutputStream());
51                      continue;
52                  }
53  
54                  pHandler.onResource(entry, inputStream);                
55              }
56  
57          	pHandler.onStopJar(jar);
58          }
59  
60          pHandler.onStopProcessing();
61  	}
62  }