KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > Ident


1 /*--
2
3  $Id: Ident.java,v 1.2 2004/02/06 09:57:48 jhunter Exp $
4
5  Copyright (C) 2000-2004 Jason Hunter & Brett McLaughlin.
6  All rights reserved.
7
8  Redistribution and use in source and binary forms, with or without
9  modification, are permitted provided that the following conditions
10  are met:
11
12  1. Redistributions of source code must retain the above copyright
13     notice, this list of conditions, and the following disclaimer.
14
15  2. Redistributions in binary form must reproduce the above copyright
16     notice, this list of conditions, and the disclaimer that follows
17     these conditions in the documentation and/or other materials
18     provided with the distribution.
19
20  3. The name "JDOM" must not be used to endorse or promote products
21     derived from this software without prior written permission. For
22     written permission, please contact <request_AT_jdom_DOT_org>.
23
24  4. Products derived from this software may not be called "JDOM", nor
25     may "JDOM" appear in their name, without prior written permission
26     from the JDOM Project Management <request_AT_jdom_DOT_org>.
27
28  In addition, we request (but do not require) that you include in the
29  end-user documentation provided with the redistribution and/or in the
30  software itself an acknowledgement equivalent to the following:
31      "This product includes software developed by the
32       JDOM Project (http://www.jdom.org/)."
33  Alternatively, the acknowledgment may be graphical using the logos
34  available at http://www.jdom.org/images/logos.
35
36  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  DISCLAIMED. IN NO EVENT SHALL THE JDOM AUTHORS OR THE PROJECT
40  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  SUCH DAMAGE.
48
49  This software consists of voluntary contributions made by many
50  individuals on behalf of the JDOM Project and was originally
51  created by Jason Hunter <jhunter_AT_jdom_DOT_org> and
52  Brett McLaughlin <brett_AT_jdom_DOT_org>. For more information
53  on the JDOM Project, please see <http://www.jdom.org/>.
54
55  */

56
57  import java.util.zip.ZipFile JavaDoc;
58 import java.util.zip.ZipEntry JavaDoc;
59
60 import java.io.IOException JavaDoc;
61 import java.io.InputStream JavaDoc;
62 import java.io.FileInputStream JavaDoc;
63
64 import java.util.HashMap JavaDoc;
65 import java.util.Enumeration JavaDoc;
66
67 /**
68  * <p>
69  * The <code>Ident</code> class is a class which works like the
70  * ident(1) program used to display RCS keys stored in binary files,
71  * but with the added facility of being able to look inside .zip/.jar
72  * files and check each one on the file contain within.
73  * </p>
74  *
75  * <p>
76  * Known issues: The class can get confused if a $ appears in the class
77  * file where it's not a delimiter.
78  *
79  * @author Jools Enticknap
80  */

81 public final class Ident {
82     
83     /** Starting character for an RCS string */
84     private final static int KDELIM = '$';
85
86     /** Delimiter withing the tags */
87     private final static int VDELIM = ':';
88     
89     /** All the known RCS tags */
90     private final static String JavaDoc[] rcsTags = {
91         "Author", // The login name of the user who checked in the revision.
92

93         "Date", // The date and time the revision was checked in.
94

95         "Header", // A standard header containing the full pathname of the RCS
96
// file, the revision number, the date and time, the author,
97
// the state, and the locker (if locked).
98

99         "Id", // Same as $Header: /home/cvs/jdom-contrib/src/java/Ident.java,v 1.2 2004/02/06 09:57:48 jhunter Exp $, except that the RCS filename is without
100
// a path.
101

102         "Locker", // The login name of the user who locked the revision (empty
103
// if not locked).
104

105         "Log", // The log message supplied during checkin. For ident's
106
// purposes, this is equivalent to $RCSfile: Ident.java,v $.
107

108         "Name", // The symbolic name used to check out the revision, if any.
109

110         "RCSfile",// The name of the RCS file without a path.
111

112         "Revision",// The revision number assigned to the revision.
113

114         "Source", // The full pathname of the RCS file.
115

116         "State" // The state assigned to the revision with the -s option
117
// of rcs(1) or ci(1).
118
};
119     
120     /** A Map of all the tags for easy lookup */
121     private final static HashMap JavaDoc tagMap;
122     
123     public static void main(String JavaDoc args[]) throws IOException JavaDoc {
124         if (args.length > 0 ) {
125             for (int i = 0; i < args.length; i++) {
126                 System.out.println(args[i]+":");
127                 if (isZip(args[i])) {
128                     ZipFile JavaDoc zipFile = new ZipFile JavaDoc(args[i]);
129                     Enumeration JavaDoc entries = zipFile.entries();
130                     while (entries.hasMoreElements()) {
131                         ZipEntry JavaDoc ze = (ZipEntry JavaDoc)entries.nextElement();
132                         System.out.println(" ->"+ze.getName()+"<-");
133                         scan(zipFile.getInputStream(ze));
134                     }
135                 } else {
136                     FileInputStream JavaDoc fis = new FileInputStream JavaDoc(args[i]);
137                     scan(fis);
138                 }
139             }
140         } else {
141             printUsage();
142         }
143     }
144
145     private static void printUsage() {
146         System.out.println(
147           "This program prints RCS information about a class file.");
148         System.out.println("Usage: java Ident [classfile.class | jarfile.jar]");
149     }
150
151     private static boolean isZip(String JavaDoc fileName) {
152         return fileName.endsWith(".jar") ||
153                fileName.endsWith(".zip");
154     }
155
156     private static void scan(InputStream JavaDoc is) throws IOException JavaDoc {
157         int i = 0;
158         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
159
160         boolean inTag = false;
161         boolean validTag = false;
162         
163         while ((i = is.read()) != -1) {
164             
165             // We have found a starting tag.
166
if (i == KDELIM) {
167                 // We are at the end of the
168
if (inTag) {
169                     if (validTag) {
170                         System.out.println(" $"+sb.toString()+"$");
171                     }
172                     validTag = inTag = false;
173                     sb = new StringBuffer JavaDoc();
174                 } else {
175                     inTag = true;
176                     continue;
177                 }
178             } else if (i == VDELIM) {
179                 if (inTag && !validTag) {
180                     String JavaDoc tag = sb.toString();
181                     if (tagMap.get(tag) == null) {
182                         // Failed to match a tag, start again.
183
inTag = false;
184                         validTag = false;
185                         sb = new StringBuffer JavaDoc();
186                     } else {
187                         validTag = true;
188                     }
189                 }
190             }
191             
192             if (inTag) {
193                 sb.append((char)i);
194             }
195         }
196     }
197
198     static {
199         tagMap = new HashMap JavaDoc();
200         for (int i = 0; i < rcsTags.length; i++) {
201             tagMap.put(rcsTags[i],rcsTags[i]);
202         }
203     }
204 }
205
Popular Tags