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

Last change on this file since 5926 was 5881, checked in by stoecker, 11 years ago

javadoc fixes

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