Source code: org/mrbook/utils/CvsId.java
1 /*
2 * -*- mode: java; c-basic-indent: 4; indent-tabs-mode: nil -*-
3 * :indentSize=4:noTabs=true:tabSize=4:indentOnTab=true:indentOnEnter=true:mode=java:
4 * ex: set tabstop=4 expandtab:
5 *
6 * MrPostman - webmail <-> email gateway
7 * Copyright (C) 2002-2003 MrPostman Development Group
8 * Projectpage: http://mrbook.org/mrpostman/
9 *
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 * In particular, this implies that users are responsible for
21 * using MrPostman after reading the terms and conditions given
22 * by their web-mail provider.
23 *
24 * You should have received a copy of the GNU General Public License
25 * Named LICENSE in the base directory of this distribution,
26 * if not, write to the Free Software
27 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
28 */
29
30 package org.mrbook.utils;
31
32 import java.io.IOException;
33
34 import java.lang.reflect.Field;
35
36 import java.util.Enumeration;
37 import java.util.jar.JarFile;
38 import java.util.regex.Matcher;
39 import java.util.regex.Pattern;
40 import java.util.zip.ZipEntry;
41
42
43 /**
44 * This class is a helper class to analyse classes with a CVSID attribute defined.
45 * The CVSID attribute ( as defined as well in the this class) allows dynamic discovery of
46 * the revision of a class.
47 * @author Lucas Bruand
48 */
49 public class CvsId {
50 public static final String CVSID = "$Id: CvsId.java,v 1.9 2003/03/29 16:55:26 lbruand Exp $";
51 public static Pattern pattern = Pattern.compile(
52 "\\$" /* Keep this string separated here in two to avoid keyword substitution from CVS. */
53 + "Id: (\\p{Graph}+) (\\S+) (\\d\\d\\d\\d/\\d\\d/\\d\\d \\d\\d:\\d\\d:\\d\\d) (\\S+) (\\S+) \\$");
54 public String filename;
55 public String revision;
56 public String date;
57 public String author;
58 public String tag;
59
60 public CvsId(String cvsid) throws NotACvsIdException {
61 setCvsId(cvsid);
62 }
63
64 public CvsId(Class classe) throws NotACvsIdException, NoCvsIdException {
65 String cvsid = forClass(classe);
66
67 if (cvsid.equals("")) {
68 throw new NoCvsIdException();
69 }
70 setCvsId(cvsid);
71 }
72
73 public CvsId(Object obj) throws NotACvsIdException, NoCvsIdException {
74 this(obj.getClass());
75 }
76
77 public void setCvsId(String cvsid) throws NotACvsIdException {
78 Matcher match = CvsId.pattern.matcher(cvsid);
79
80 if (match.matches()) {
81 filename = match.group(1);
82 revision = match.group(2);
83 date = match.group(3);
84 author = match.group(4);
85 tag = match.group(5);
86 } else {
87 throw new NotACvsIdException();
88 }
89 }
90
91 public String toString() {
92 return "$" /* Keep this string separated here in two to avoid keyword substitution from CVS. */ + "Id: "
93 + filename + " " + revision + " " + date + " " + author + " " + tag + " $";
94 }
95
96 public static void usage() {
97 System.out.println();
98 System.out.println(" CvsId <ClassName> to obtain the CVSID of a class in the classpath");
99 System.out.println(" or ");
100 System.out.println(" CvsId jarfile.jar to obtain all the CVSIDs of all the class in a jar file.");
101 System.out.println(" ex: CvsId org.mrbook.mrpostman.MrPostman ");
102 System.out.println();
103 }
104
105 public static String forClass(Class classe) {
106 try {
107 Field field = classe.getDeclaredField("CVSID");
108 return (String) field.get(null); // CVSID attribute has to be static.
109 } catch (NoSuchFieldException e) {
110 return "";
111 } catch (IllegalArgumentException e) {
112 return null;
113 } catch (IllegalAccessException e) {
114 return "";
115 }
116 }
117
118 public static String forClass(String classname) throws ClassNotFoundException {
119 return forClass(Class.forName(classname));
120 }
121
122 public static void main(String[] args) {
123 if (args.length != 1) {
124 usage();
125 System.exit(1);
126 }
127
128 if (args[0].endsWith(".jar")) {
129 try {
130 JarFile jar = new JarFile(args[0]);
131
132 for (Enumeration entries = jar.entries(); entries.hasMoreElements();) {
133 ZipEntry entry = (ZipEntry) entries.nextElement();
134 String name = entry.getName();
135
136 if (name.endsWith(".class")) {
137 String newname = name.substring(0, name.length() - 6);
138
139 if (newname.indexOf('$') != -1) {
140 continue;
141 }
142 String triname = newname.replace('/', '.');
143 System.out.print(triname + ": ");
144
145 try {
146 String result = forClass(triname);
147
148 if (result.equals("")) {
149 System.out.println("CVSID has no tag");
150 } else {
151 System.out.println(result);
152 }
153 } catch (ClassNotFoundException e) {
154 System.out.println(" Class " + triname + " doesn't exist");
155 }
156 }
157 }
158 } catch (IOException io) {
159 System.out.println("This is not a correct jarfile: " + args[0]);
160 }
161 } else {
162 try {
163 System.out.println(forClass(args[0]));
164 } catch (ClassNotFoundException e) {
165 System.out.println("Class " + args[0] + " doesn't exist in the classpath");
166 }
167 }
168 }
169
170 public class NotACvsIdException extends Exception {
171 }
172
173 public class NoCvsIdException extends NotACvsIdException {
174 }
175 }