source: josm/trunk/src/org/openstreetmap/josm/data/Version.java@ 5839

Last change on this file since 5839 was 5839, checked in by Don-vip, 11 years ago

see #8570 - close more streams

  • Property svn:eol-style set to native
File size: 6.7 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.io.BufferedReader;
7import java.io.IOException;
8import java.io.InputStreamReader;
9import java.net.URL;
10import java.util.HashMap;
11import java.util.Map.Entry;
12import java.util.regex.Matcher;
13import java.util.regex.Pattern;
14
15import org.openstreetmap.josm.Main;
16import org.openstreetmap.josm.tools.LanguageInfo;
17
18/**
19 * Provides basic information about the currently used JOSM build.
20 *
21 */
22public class Version {
23 /** constant to indicate that the current build isn't assigned a JOSM version number */
24 static public final int JOSM_UNKNOWN_VERSION = 0;
25
26 /** the unique instance */
27 private static Version instance;
28
29 /**
30 * Load the specified resource as string.
31 *
32 * @param resource the resource url to load
33 * @return the content of the resource file; null, if an error occurred
34 */
35 static public String loadResourceFile(URL resource) {
36 if (resource == null) return null;
37 BufferedReader in;
38 String s = null;
39 try {
40 in = new BufferedReader(new InputStreamReader(resource.openStream(), "UTF-8"));
41 StringBuffer sb = new StringBuffer();
42 try {
43 for (String line = in.readLine(); line != null; line = in.readLine()) {
44 sb.append(line).append("\n");
45 }
46 } finally {
47 in.close();
48 }
49 s = sb.toString();
50 } catch (IOException e) {
51 System.err.println(tr("Failed to load resource ''{0}'', error is {1}.", resource.toString(), e.toString()));
52 e.printStackTrace();
53 }
54 return s;
55 }
56
57 /**
58 * Replies the unique instance of the version information
59 *
60 * @return the unique instance of the version information
61 */
62
63 static public Version getInstance() {
64 if (instance == null) {
65 instance = new Version();
66 instance.init();
67 }
68 return instance;
69 }
70
71 private int version;
72 private String releaseDescription;
73 private String time;
74 private String buildName;
75 private boolean isLocalBuild;
76
77 protected HashMap<String, String> parseManifestStyleFormattedString(String content) {
78 HashMap<String, String> properties = new HashMap<String, String>();
79 if (content == null) return properties;
80 Pattern p = Pattern.compile("^([^:]+):(.*)$");
81 for (String line: content.split("\n")) {
82 if (line == null || line.trim().equals("")) {
83 continue;
84 }
85 if (line.matches("^\\s*#.*$")) {
86 continue;
87 }
88 Matcher m = p.matcher(line);
89 if (m.matches()) {
90 properties.put(m.group(1), m.group(2));
91 }
92 }
93 return properties;
94 }
95
96 /**
97 * Initializes the version infos from the revision resource file
98 *
99 * @param revisionInfo the revision info loaded from a revision resource file
100 */
101 protected void initFromRevisionInfo(String revisionInfo) {
102 if (revisionInfo == null) {
103 this.releaseDescription = tr("UNKNOWN");
104 this.version = JOSM_UNKNOWN_VERSION;
105 this.time = null;
106 return;
107 }
108
109 HashMap<String, String> properties = parseManifestStyleFormattedString(revisionInfo);
110 String value = properties.get("Revision");
111 if (value != null) {
112 value = value.trim();
113 try {
114 version = Integer.parseInt(value);
115 } catch(NumberFormatException e) {
116 version = 0;
117 System.err.println(tr("Warning: unexpected JOSM version number in revision file, value is ''{0}''", value));
118 }
119 } else {
120 version = JOSM_UNKNOWN_VERSION;
121 }
122
123 // the last changed data
124 //
125 time = properties.get("Last Changed Date");
126 if (time == null) {
127 time = properties.get("Build-Date");
128 }
129
130 // is this a local build ?
131 //
132 isLocalBuild = false;
133 value = properties.get("Is-Local-Build");
134 if (value != null && value.trim().toLowerCase().equals("true")) {
135 isLocalBuild = true;
136 }
137
138 // is this a specific build ?
139 //
140 buildName = null;
141 value = properties.get("Build-Name");
142 if (value != null && !value.trim().isEmpty()) {
143 buildName = value.trim();
144 }
145
146 // the revision info
147 //
148 StringBuffer sb = new StringBuffer();
149 for(Entry<String,String> property: properties.entrySet()) {
150 sb.append(property.getKey()).append(":").append(property.getValue()).append("\n");
151 }
152 releaseDescription = sb.toString();
153 }
154
155 public void init() {
156 URL u = Main.class.getResource("/REVISION");
157 if (u == null) {
158 System.err.println(tr("Warning: the revision file ''/REVISION'' is missing."));
159 version = 0;
160 releaseDescription = "";
161 return;
162 }
163 initFromRevisionInfo(loadResourceFile(u));
164 }
165
166 /**
167 * Replies the version string. Either the SVN revision "1234" (as string) or the
168 * the I18n equivalent of "UNKNOWN".
169 *
170 * @return the JOSM version
171 */
172 public String getVersionString() {
173 return version == 0 ? tr("UNKNOWN") : Integer.toString(version);
174 }
175
176 /**
177 * Replies a text with the release attributes
178 *
179 * @return a text with the release attributes
180 */
181 public String getReleaseAttributes() {
182 return releaseDescription;
183 }
184
185 /**
186 * Replies the build date as string
187 *
188 * @return the build date as string
189 */
190 public String getTime() {
191 return time;
192 }
193
194 /**
195 * Replies the JOSM version. Replies {@link #JOSM_UNKNOWN_VERSION} if the version isn't known.
196 * @return the JOSM version
197 */
198 public int getVersion() {
199 return version;
200 }
201
202 /**
203 * Replies true if this is a local build, i.e. an inofficial development build.
204 *
205 * @return true if this is a local build, i.e. an inofficial development build.
206 */
207 public boolean isLocalBuild() {
208 return isLocalBuild;
209 }
210
211 public String getAgentString() {
212 int v = getVersion();
213 String s = (v == JOSM_UNKNOWN_VERSION) ? "UNKNOWN" : Integer.toString(v);
214 if (buildName != null) {
215 s += " " + buildName;
216 }
217 if (isLocalBuild() && v != JOSM_UNKNOWN_VERSION) {
218 s += " SVN";
219 }
220 return "JOSM/1.5 ("+ s+" "+LanguageInfo.getJOSMLocaleCode()+") " + System.getProperty("os.name");
221 }
222}
Note: See TracBrowser for help on using the repository browser.