1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package mk.fsgrep.util;
18
19 import java.io.*;
20 import java.util.jar.Manifest;
21 import java.util.jar.Attributes;
22 import java.util.regex.Pattern;
23 import java.util.regex.Matcher;
24 import java.util.Map;
25 import java.util.HashMap;
26 import java.net.URL;
27
28
29 /***
30 * A utilty that extracts metadata from the JAR manifest. It is created by passing
31 * in a class from the desired jar (since there could be multiple jars on the classpath,
32 * and thus multiple manifests).
33 *
34 * @author murali
35 */
36 public class ManifestProperties {
37
38
39
40
41 private static final String RESOURCE_URI = "/META-INF/MANIFEST.MF";
42
43 public static final String UNDEFINED = "undefined";
44
45 private static final Pattern P_FORMAT = Pattern.compile("^(//d{2})");
46
47
48
49
50
51
52
53
54
55 private final Map<String,String> fMap = new HashMap<String,String>();
56
57 private String fLicense = "";
58
59
60
61
62 public ManifestProperties(Class pReferenceClass) {
63 String classContainer = pReferenceClass.getProtectionDomain().getCodeSource().getLocation().toString();
64 try {
65 URL manifestUrl = new URL("jar:" + classContainer + "!" + RESOURCE_URI);
66 Manifest manifest = new Manifest(manifestUrl.openStream());
67 Attributes mainAttributes = manifest.getMainAttributes();
68 for (Object o : mainAttributes.keySet()) {
69
70 String value = (String) mainAttributes.get(o);
71 if ((value != null) && !"".equals(value)) {
72 String key = String.valueOf(o);
73 fMap.put(key, value);
74 }
75 }
76 } catch (IOException ioe) {
77 System.err.println("Problem reading properties.");
78 ioe.printStackTrace();
79 }
80
81 try {
82 StringBuilder builder = new StringBuilder();
83 URL licenseUrl = new URL("jar:" + classContainer + "!/LICENSE.txt");
84 InputStream is = licenseUrl.openStream();
85 InputStreamReader isr = new InputStreamReader(is);
86 BufferedReader br = new BufferedReader(isr);
87
88 for (String line=br.readLine(); line!=null; line=br.readLine()) {
89 builder.append(line).append("\n");
90 }
91
92 br.close();
93 isr.close();
94 is.close();
95
96 fLicense = builder.toString();
97 } catch (FileNotFoundException fnfe) {
98 System.err.println(fnfe);
99 } catch (IOException ioe) {
100 System.err.println("Problem reading LICENSE.txt");
101 ioe.printStackTrace();
102 }
103 }
104
105
106
107
108
109 public String getProjectName() {
110 return fMap.get("ProjectName");
111 }
112
113 public String getProjectVersion() {
114 return fMap.get("ProjectVersion");
115 }
116
117 public String getBuildTime() {
118 return fMap.get("BuildTime");
119 }
120
121 public String getProjectDescription() {
122 return fMap.get("ProjectDescription");
123 }
124
125 public String getLicense() {
126 return fLicense;
127 }
128
129
130
131
132
133
134
135
136
137
138
139
140
141 public String toCopyrightString() {
142 StringBuilder builder = new StringBuilder("Copyright (c) ");
143 String copyrightStart = fMap.get("CopyrightStart");
144 builder.append(copyrightStart);
145 String buildTime = getBuildTime();
146 if ((buildTime != null) && !"".equals(buildTime)) {
147 Matcher matcher = P_FORMAT.matcher(buildTime);
148 if (matcher.find()) {
149 String copyrightEnd = "20" + matcher.group(1);
150 if (!copyrightEnd.equals(copyrightStart)) {
151 builder.append("-").append(copyrightEnd);
152 }
153 }
154 }
155 builder.append(" ").append(fMap.get("CopyrightHolder")).append(". All rights reserved.");
156 return builder.toString();
157 }
158
159 public String toProjectSummaryString() {
160 StringBuilder builder = new StringBuilder();
161 builder.append(getProjectName())
162 .append(", version ").append(getProjectVersion())
163 .append(" [").append(getBuildTime()).append("]");
164 return builder.toString();
165 }
166
167 public String toAboutString() {
168 StringBuilder builder = new StringBuilder();
169
170 builder.append(toProjectSummaryString()).append("\n\n");
171 builder.append(getProjectDescription()).append("\n\n");
172 builder.append(toCopyrightString()).append("\n\n");
173 builder.append(getLicense());
174
175 return builder.toString();
176 }
177
178
179
180
181
182 public String toString() {
183 return toProjectSummaryString();
184 }
185
186
187
188
189
190
191
192
193
194
195 public static void main(String[] args) {
196 try {
197 ManifestProperties mp = new ManifestProperties(ManifestProperties.class);
198 System.out.println(mp);
199 System.out.println(mp.toCopyrightString());
200 System.out.println(mp.getProjectDescription());
201 } catch (Throwable thr) {
202 System.err.println("Fatal error.");
203 thr.printStackTrace();
204 }
205 }
206
207
208 }