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