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

Last change on this file since 12042 was 11899, checked in by Don-vip, 7 years ago

fix #14646 - extra space in JOSM user agent since r11889 (regression)

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