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.BufferedReader;
|
---|
7 | import java.io.IOException;
|
---|
8 | import java.io.InputStreamReader;
|
---|
9 | import java.net.URL;
|
---|
10 | import java.util.HashMap;
|
---|
11 | import java.util.Map.Entry;
|
---|
12 | import java.util.regex.Matcher;
|
---|
13 | import java.util.regex.Pattern;
|
---|
14 |
|
---|
15 | import org.openstreetmap.josm.Main;
|
---|
16 | import org.openstreetmap.josm.tools.LanguageInfo;
|
---|
17 |
|
---|
18 | /**
|
---|
19 | * Provides basic information about the currently used JOSM build.
|
---|
20 | *
|
---|
21 | */
|
---|
22 | public class Version {
|
---|
23 | /** constant to indicate that the currnt 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 | BufferedReader in;
|
---|
38 | String s = null;
|
---|
39 | try {
|
---|
40 | in = new BufferedReader(new InputStreamReader(resource.openStream()));
|
---|
41 | StringBuffer sb = new StringBuffer();
|
---|
42 | for (String line = in.readLine(); line != null; line = in.readLine()) {
|
---|
43 | sb.append(line).append("\n");
|
---|
44 | }
|
---|
45 | s = sb.toString();
|
---|
46 | } catch (IOException e) {
|
---|
47 | System.err.println(tr("Failed to load resource ''{0}'', error is {1}.", resource.toString(), e.toString()));
|
---|
48 | e.printStackTrace();
|
---|
49 | }
|
---|
50 | return s;
|
---|
51 | }
|
---|
52 |
|
---|
53 | /**
|
---|
54 | * Replies the unique instance of the version information
|
---|
55 | *
|
---|
56 | * @return the unique instance of the version information
|
---|
57 | */
|
---|
58 |
|
---|
59 | static public Version getInstance() {
|
---|
60 | if (instance == null) {
|
---|
61 | instance = new Version();
|
---|
62 | instance.init();
|
---|
63 | }
|
---|
64 | return instance;
|
---|
65 | }
|
---|
66 |
|
---|
67 | private int version;
|
---|
68 | private String revision;
|
---|
69 | private String time;
|
---|
70 | private boolean isLocalBuild;
|
---|
71 |
|
---|
72 | protected HashMap<String, String> parseManifestStyleFormattedString(String content) {
|
---|
73 | HashMap<String, String> properties = new HashMap<String, String>();
|
---|
74 | if (content == null) return properties;
|
---|
75 | Pattern p = Pattern.compile("^([^:]+):(.*)$");
|
---|
76 | for (String line: content.split("\n")) {
|
---|
77 | if (line == null || line.trim().equals("")) {
|
---|
78 | continue;
|
---|
79 | }
|
---|
80 | if (line.matches("^\\s*#.*$")) {
|
---|
81 | continue;
|
---|
82 | }
|
---|
83 | Matcher m = p.matcher(line);
|
---|
84 | if (m.matches()) {
|
---|
85 | properties.put(m.group(1), m.group(2));
|
---|
86 | }
|
---|
87 | }
|
---|
88 | return properties;
|
---|
89 | }
|
---|
90 |
|
---|
91 | /**
|
---|
92 | * Initializes the version infos from the revision resource file
|
---|
93 | *
|
---|
94 | * @param revisionInfo the revision info loaded from a revision resource file
|
---|
95 | */
|
---|
96 | protected void initFromRevisionInfo(String revisionInfo) {
|
---|
97 | if (revisionInfo == null) {
|
---|
98 | this.revision = tr("UNKNOWN");
|
---|
99 | this.version = JOSM_UNKNOWN_VERSION;
|
---|
100 | this.time = null;
|
---|
101 | return;
|
---|
102 | }
|
---|
103 |
|
---|
104 | HashMap<String, String> properties = parseManifestStyleFormattedString(revisionInfo);
|
---|
105 | String value = properties.get("Revision");
|
---|
106 | if (value != null) {
|
---|
107 | value = value.trim();
|
---|
108 | try {
|
---|
109 | version = Integer.parseInt(value);
|
---|
110 | } catch(NumberFormatException e) {
|
---|
111 | version = 0;
|
---|
112 | System.err.println(tr("Warning: unexpected JOSM version number in revison file, value is ''{0}''", value));
|
---|
113 | }
|
---|
114 | } else {
|
---|
115 | version = JOSM_UNKNOWN_VERSION;
|
---|
116 | }
|
---|
117 |
|
---|
118 | // the last changed data
|
---|
119 | //
|
---|
120 | time = properties.get("Last Changed Date");
|
---|
121 | if (time == null) {
|
---|
122 | time = properties.get("Build-Date");
|
---|
123 | }
|
---|
124 |
|
---|
125 | // is this a local build ?
|
---|
126 | //
|
---|
127 | isLocalBuild = false;
|
---|
128 | value = properties.get("Is-Local-Build");
|
---|
129 | if (value != null && value.trim().toLowerCase().equals("true")) {
|
---|
130 | isLocalBuild = true;
|
---|
131 | }
|
---|
132 |
|
---|
133 | // the revision info
|
---|
134 | //
|
---|
135 | StringBuffer sb = new StringBuffer();
|
---|
136 | for(Entry<String,String> property: properties.entrySet()) {
|
---|
137 | sb.append(property.getKey()).append(":").append(property.getValue()).append("\n");
|
---|
138 | }
|
---|
139 | revision = sb.toString();
|
---|
140 | }
|
---|
141 |
|
---|
142 | public void init() {
|
---|
143 | URL u = Main.class.getResource("/REVISION");
|
---|
144 | if (u == null) {
|
---|
145 | System.err.println(tr("Warning: the revision file ''/REVISION'' is missing."));
|
---|
146 | version = 0;
|
---|
147 | revision = "";
|
---|
148 | return;
|
---|
149 | }
|
---|
150 | initFromRevisionInfo(loadResourceFile(u));
|
---|
151 | System.out.println(revision);
|
---|
152 | }
|
---|
153 |
|
---|
154 | /**
|
---|
155 | * Replies the version string. Either the SVN revision "1234" (as string) or the
|
---|
156 | * the I18n equivalent of "UNKNOWN".
|
---|
157 | *
|
---|
158 | * @return the JOSM version
|
---|
159 | */
|
---|
160 | public String getVersionString() {
|
---|
161 | return version == 0 ? tr("UNKNOWN") : Integer.toString(version);
|
---|
162 | }
|
---|
163 |
|
---|
164 | /**
|
---|
165 | * Replies a text with the release attributes
|
---|
166 | *
|
---|
167 | * @return a text with the release attributes
|
---|
168 | */
|
---|
169 | public String getReleaseAttributes() {
|
---|
170 | return revision;
|
---|
171 | }
|
---|
172 |
|
---|
173 | /**
|
---|
174 | * Replies the build date as string
|
---|
175 | *
|
---|
176 | * @return the build date as string
|
---|
177 | */
|
---|
178 | public String getTime() {
|
---|
179 | return time;
|
---|
180 | }
|
---|
181 |
|
---|
182 | /**
|
---|
183 | * Replies the JOSM version. Replies {@see #JOSM_UNKNOWN_VERSION} if the version isn't known.
|
---|
184 | * @return the JOSM version
|
---|
185 | */
|
---|
186 | public int getVersion() {
|
---|
187 | return version;
|
---|
188 | }
|
---|
189 |
|
---|
190 | /**
|
---|
191 | * Replies true if this is a local build, i.e. an inofficial development build.
|
---|
192 | *
|
---|
193 | * @return true if this is a local build, i.e. an inofficial development build.
|
---|
194 | */
|
---|
195 | public boolean isLocalBuild() {
|
---|
196 | return isLocalBuild;
|
---|
197 | }
|
---|
198 |
|
---|
199 | public String getAgentString() {
|
---|
200 | int v = getVersion();
|
---|
201 | String s = (v == JOSM_UNKNOWN_VERSION) ? "UNKNOWN" : Integer.toString(v);
|
---|
202 | if (isLocalBuild() && v != JOSM_UNKNOWN_VERSION) {
|
---|
203 | s += " SVN";
|
---|
204 | }
|
---|
205 | return "JOSM/1.5 ("+ s+" "+LanguageInfo.getJOSMLocaleCode()+")";
|
---|
206 | }
|
---|
207 | }
|
---|