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

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

Sonar/FindBugs - Loose coupling

  • Property svn:eol-style set to native
File size: 7.4 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.net.URL;
9import java.util.HashMap;
10import java.util.Map;
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;
17import org.openstreetmap.josm.tools.Utils;
18
19/**
20 * Provides basic information about the currently used JOSM build.
21 *
22 */
23public class Version {
24 /** constant to indicate that the current build isn't assigned a JOSM version number */
25 static public final int JOSM_UNKNOWN_VERSION = 0;
26
27 /** the unique instance */
28 private static Version instance;
29
30 /**
31 * Load the specified resource as string.
32 *
33 * @param resource the resource url to load
34 * @return the content of the resource file; null, if an error occurred
35 */
36 static public String loadResourceFile(URL resource) {
37 if (resource == null) return null;
38 String s = null;
39 try {
40 BufferedReader in = Utils.openURLReader(resource);
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 Utils.close(in);
48 }
49 s = sb.toString();
50 } catch (IOException e) {
51 Main.error(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 Map<String, String> parseManifestStyleFormattedString(String content) {
78 Map<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().isEmpty()) {
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 Map<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 Main.warn(tr("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().equalsIgnoreCase("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 /**
156 * Initializes version info
157 */
158 public void init() {
159 URL u = Main.class.getResource("/REVISION");
160 if (u == null) {
161 Main.warn(tr("The revision file ''/REVISION'' is missing."));
162 version = 0;
163 releaseDescription = "";
164 return;
165 }
166 initFromRevisionInfo(loadResourceFile(u));
167 }
168
169 /**
170 * Replies the version string. Either the SVN revision "1234" (as string) or the
171 * the I18n equivalent of "UNKNOWN".
172 *
173 * @return the JOSM version
174 */
175 public String getVersionString() {
176 return version == 0 ? tr("UNKNOWN") : Integer.toString(version);
177 }
178
179 /**
180 * Replies a text with the release attributes
181 *
182 * @return a text with the release attributes
183 */
184 public String getReleaseAttributes() {
185 return releaseDescription;
186 }
187
188 /**
189 * Replies the build date as string
190 *
191 * @return the build date as string
192 */
193 public String getTime() {
194 return time;
195 }
196
197 /**
198 * Replies the JOSM version. Replies {@link #JOSM_UNKNOWN_VERSION} if the version isn't known.
199 * @return the JOSM version
200 */
201 public int getVersion() {
202 return version;
203 }
204
205 /**
206 * Replies true if this is a local build, i.e. an inofficial development build.
207 *
208 * @return true if this is a local build, i.e. an inofficial development build.
209 */
210 public boolean isLocalBuild() {
211 return isLocalBuild;
212 }
213
214 /**
215 * Returns the User-Agent string
216 * @return The User-Agent
217 */
218 public String getAgentString() {
219 return getAgentString(true);
220 }
221
222 /**
223 * Returns the User-Agent string, with or without OS details
224 * @param includeOsDetails Append Operating System details at the end of the User-Agent
225 * @return The User-Agent
226 * @since 5956
227 */
228 public String getAgentString(boolean includeOsDetails) {
229 int v = getVersion();
230 String s = (v == JOSM_UNKNOWN_VERSION) ? "UNKNOWN" : Integer.toString(v);
231 if (buildName != null) {
232 s += " " + buildName;
233 }
234 if (isLocalBuild() && v != JOSM_UNKNOWN_VERSION) {
235 s += " SVN";
236 }
237 String result = "JOSM/1.5 ("+ s+" "+LanguageInfo.getJOSMLocaleCode()+")";
238 if (includeOsDetails) {
239 result += " " + Main.platform.getOSDescription();
240 }
241 return result;
242 }
243
244 /**
245 * Returns the full User-Agent string
246 * @return The User-Agent
247 * @since 5868
248 */
249 public String getFullAgentString() {
250 return getAgentString() + " Java/"+System.getProperty("java.version");
251 }
252}
Note: See TracBrowser for help on using the repository browser.