View Javadoc

1   /*
2    * JarInspector - Copyright (C) 2004 Che Inc., Rosario Argentina
3    *
4    * This program is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Library General Public
6    * License as published by the Free Software Foundation; either
7    * version 2 of the License, or (at your option) any later version.
8    *
9    * This library is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12   * Library General Public License for more details.
13   *
14   * You should have received a copy of the GNU Library General Public
15   * License along with this library; if not, write to the Free
16   * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17   */
18  
19  package inc.che.jarinspector;
20  
21  import inc.che.common.resource.ResourceManager;
22  
23  import java.io.File;
24  import java.io.IOException;
25  import java.util.ArrayList;
26  import java.util.Collection;
27  import java.util.Comparator;
28  import java.util.Enumeration;
29  import java.util.jar.JarEntry;
30  import java.util.jar.JarFile;
31  
32  import org.apache.log4j.Logger;
33  
34  /***
35   *  <b>@todo</b>
36   * @version $Id: JarFileInfo.java,v 1.1 2005/03/06 12:56:57 stevemcmee Exp $
37   * @author <address> Steve McMee &lt;stevemcmee@sourceforge.net&gt; </address>
38   */
39  
40  public class JarFileInfo {
41  
42      public static class JarFileInfoItemJarComparator implements Comparator {
43  
44          public int compare(Object o1, Object o2) {
45              if (o1 instanceof JarFileInfoItem
46                  && o2 instanceof JarFileInfoItem) {
47                  return ((JarFileInfoItem) o1).jarFileName.compareTo(
48                      ((JarFileInfoItem) o2).jarFileName);
49              }
50              return 0;
51          }
52  
53      }
54  
55      public static class JarFileInfoItemClassComparator implements Comparator {
56  
57          public int compare(Object o1, Object o2) {
58              if (o1 instanceof JarFileInfoItem
59                  && o2 instanceof JarFileInfoItem) {
60                  return ((JarFileInfoItem) o1).className.compareTo(
61                      ((JarFileInfoItem) o2).className);
62              }
63              return 0;
64          }
65  
66      }
67      public static class JarFileInfoItem implements Comparable {
68  
69          /***
70           * The ResourceManager
71           */
72          private static ResourceManager resourceManager =
73              ResourceManager.getResourceManager(StringResources.TEXT_RESOURCES);
74  
75          private String className = null;
76          private String jarFileName = null;
77  
78          JarFileInfoItem(String className, String jarFileName) {
79              this.className = className;
80              this.jarFileName = jarFileName;
81          }
82  
83          public String getClassName() {
84              return this.className;
85          }
86          public String getJarFileName() {
87              return this.jarFileName;
88          }
89  
90          public String toString() {
91              return resourceManager.getText("info.label.class")
92                  + ":"
93                  + className
94                  + "\t "
95                  + resourceManager.getText("info.label.jar")
96                  + ":"
97                  + jarFileName;
98          }
99  
100         public int compareTo(Object o) {
101             if (o instanceof JarFileInfoItem) {
102                 return ((JarFileInfoItem) o).jarFileName.compareTo(jarFileName);
103             }
104             return 0;
105         }
106 
107     }
108 
109     /*** CVS ID of this file */
110     public static final String CVS_ID =
111         "$Id: JarFileInfo.java,v 1.1 2005/03/06 12:56:57 stevemcmee Exp $";
112 
113     /*** logger instance for this class */
114 
115     private static Logger log = Logger.getLogger(JarFileInfo.class);
116 
117     /*** the jarFile */
118 
119     private JarFile jarFile = null;
120     /***
121      * Constructor
122      * @param jarfile the JarFile to get the Info from
123      */
124 
125     public JarFileInfo(JarFile jarFile) {
126         this.jarFile = jarFile;
127     }
128 
129     public JarFileInfo(File file) throws IOException {
130         this(new JarFile(file));
131     }
132     public JarFileInfo(String jarFilePath) throws IOException {
133         this(new JarFile(jarFilePath));
134 
135     }
136     public Collection getAllItems() {
137         String entryName = null;
138         Collection items = new ArrayList();
139         for (Enumeration itemEnum = jarFile.entries();
140             itemEnum.hasMoreElements();
141             ) {
142             entryName = ((JarEntry) itemEnum.nextElement()).getName();
143             if (entryName.endsWith(".class") && entryName.indexOf("$") == -1) {
144                 entryName = entryName.substring(0, entryName.lastIndexOf("."));
145                 entryName = entryName.replace('/', '.');
146                 items.add(new JarFileInfoItem(entryName, jarFile.getName()));
147 
148             }
149         }
150 
151         return items;
152 
153     }
154 }