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

Last change on this file since 9171 was 9019, checked in by Don-vip, 8 years ago

fix #12112 - Use java.util.Properties to read REVISION file in the Version-class (patch by floscher) + checkstyle

  • Property svn:eol-style set to native
File size: 5.9 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.IOException;
7import java.io.InputStream;
8import java.util.Map.Entry;
9import java.util.Properties;
10
11import org.openstreetmap.josm.Main;
12import org.openstreetmap.josm.tools.LanguageInfo;
13
14/**
15 * Provides basic information about the currently used JOSM build.
16 *
17 */
18public class Version {
19 /** constant to indicate that the current build isn't assigned a JOSM version number */
20 public static final int JOSM_UNKNOWN_VERSION = 0;
21
22 /** the unique instance */
23 private static Version instance;
24
25 /**
26 * Replies the unique instance of the version information
27 *
28 * @return the unique instance of the version information
29 */
30 public static synchronized Version getInstance() {
31 if (instance == null) {
32 instance = new Version();
33 instance.init();
34 }
35 return instance;
36 }
37
38 private int version;
39 private String releaseDescription;
40 private String time;
41 private String buildName;
42 private boolean isLocalBuild;
43
44 /**
45 * Initializes the version infos from the revision resource file
46 *
47 * @param revisionInfo the revision info from a revision resource file as InputStream
48 */
49 protected void initFromRevisionInfo(InputStream revisionInfo) {
50 if (revisionInfo == null) {
51 this.releaseDescription = tr("UNKNOWN");
52 this.version = JOSM_UNKNOWN_VERSION;
53 this.time = null;
54 return;
55 }
56
57 Properties properties = new Properties();
58 try {
59 properties.load(revisionInfo);
60 revisionInfo.close();
61 } catch (IOException e) {
62 Main.warn(tr("Error reading revision info from revision file: {0}", e.getMessage()));
63 }
64 String value = properties.getProperty("Revision");
65 if (value != null) {
66 value = value.trim();
67 try {
68 version = Integer.parseInt(value);
69 } catch (NumberFormatException e) {
70 version = 0;
71 Main.warn(tr("Unexpected JOSM version number in revision file, value is ''{0}''", value));
72 }
73 } else {
74 version = JOSM_UNKNOWN_VERSION;
75 }
76
77 // the last changed data
78 //
79 time = properties.getProperty("Last Changed Date");
80 if (time == null) {
81 time = properties.getProperty("Build-Date");
82 }
83
84 // is this a local build ?
85 //
86 isLocalBuild = false;
87 value = properties.getProperty("Is-Local-Build");
88 if (value != null && "true".equalsIgnoreCase(value.trim())) {
89 isLocalBuild = true;
90 }
91
92 // is this a specific build ?
93 //
94 buildName = null;
95 value = properties.getProperty("Build-Name");
96 if (value != null && !value.trim().isEmpty()) {
97 buildName = value.trim();
98 }
99
100 // the revision info
101 //
102 StringBuilder sb = new StringBuilder();
103 for (Entry<Object, Object> property: properties.entrySet()) {
104 sb.append(property.getKey()).append(':').append(property.getValue()).append('\n');
105 }
106 releaseDescription = sb.toString();
107 }
108
109 /**
110 * Initializes version info
111 */
112 public void init() {
113 InputStream stream = Main.class.getResourceAsStream("/REVISION");
114 if (stream == null) {
115 Main.warn(tr("The revision file ''/REVISION'' is missing."));
116 version = 0;
117 releaseDescription = "";
118 return;
119 }
120 initFromRevisionInfo(stream);
121 }
122
123 /**
124 * Replies the version string. Either the SVN revision "1234" (as string) or the
125 * the I18n equivalent of "UNKNOWN".
126 *
127 * @return the JOSM version
128 */
129 public String getVersionString() {
130 return version == 0 ? tr("UNKNOWN") : Integer.toString(version);
131 }
132
133 /**
134 * Replies a text with the release attributes
135 *
136 * @return a text with the release attributes
137 */
138 public String getReleaseAttributes() {
139 return releaseDescription;
140 }
141
142 /**
143 * Replies the build date as string
144 *
145 * @return the build date as string
146 */
147 public String getTime() {
148 return time;
149 }
150
151 /**
152 * Replies the JOSM version. Replies {@link #JOSM_UNKNOWN_VERSION} if the version isn't known.
153 * @return the JOSM version
154 */
155 public int getVersion() {
156 return version;
157 }
158
159 /**
160 * Replies true if this is a local build, i.e. an inofficial development build.
161 *
162 * @return true if this is a local build, i.e. an inofficial development build.
163 */
164 public boolean isLocalBuild() {
165 return isLocalBuild;
166 }
167
168 /**
169 * Returns the User-Agent string
170 * @return The User-Agent
171 */
172 public String getAgentString() {
173 return getAgentString(true);
174 }
175
176 /**
177 * Returns the User-Agent string, with or without OS details
178 * @param includeOsDetails Append Operating System details at the end of the User-Agent
179 * @return The User-Agent
180 * @since 5956
181 */
182 public String getAgentString(boolean includeOsDetails) {
183 int v = getVersion();
184 String s = (v == JOSM_UNKNOWN_VERSION) ? "UNKNOWN" : Integer.toString(v);
185 if (buildName != null) {
186 s += ' ' + buildName;
187 }
188 if (isLocalBuild() && v != JOSM_UNKNOWN_VERSION) {
189 s += " SVN";
190 }
191 String result = "JOSM/1.5 ("+ s+' '+LanguageInfo.getJOSMLocaleCode()+")";
192 if (includeOsDetails && Main.platform != null) {
193 result += ' ' + Main.platform.getOSDescription();
194 }
195 return result;
196 }
197
198 /**
199 * Returns the full User-Agent string
200 * @return The User-Agent
201 * @since 5868
202 */
203 public String getFullAgentString() {
204 return getAgentString() + " Java/"+System.getProperty("java.version");
205 }
206}
Note: See TracBrowser for help on using the repository browser.