| 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 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 | BufferedReader in; |
|---|
| 38 | String s = null; |
|---|
| 39 | try { |
|---|
| 40 | in = new BufferedReader(new InputStreamReader(resource.openStream(), "UTF-8")); |
|---|
| 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 releaseDescription; |
|---|
| 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.releaseDescription = 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 revision 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 | releaseDescription = 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 | releaseDescription = ""; |
|---|
| 148 | return; |
|---|
| 149 | } |
|---|
| 150 | initFromRevisionInfo(loadResourceFile(u)); |
|---|
| 151 | } |
|---|
| 152 | |
|---|
| 153 | /** |
|---|
| 154 | * Replies the version string. Either the SVN revision "1234" (as string) or the |
|---|
| 155 | * the I18n equivalent of "UNKNOWN". |
|---|
| 156 | * |
|---|
| 157 | * @return the JOSM version |
|---|
| 158 | */ |
|---|
| 159 | public String getVersionString() { |
|---|
| 160 | return version == 0 ? tr("UNKNOWN") : Integer.toString(version); |
|---|
| 161 | } |
|---|
| 162 | |
|---|
| 163 | /** |
|---|
| 164 | * Replies a text with the release attributes |
|---|
| 165 | * |
|---|
| 166 | * @return a text with the release attributes |
|---|
| 167 | */ |
|---|
| 168 | public String getReleaseAttributes() { |
|---|
| 169 | return releaseDescription; |
|---|
| 170 | } |
|---|
| 171 | |
|---|
| 172 | /** |
|---|
| 173 | * Replies the build date as string |
|---|
| 174 | * |
|---|
| 175 | * @return the build date as string |
|---|
| 176 | */ |
|---|
| 177 | public String getTime() { |
|---|
| 178 | return time; |
|---|
| 179 | } |
|---|
| 180 | |
|---|
| 181 | /** |
|---|
| 182 | * Replies the JOSM version. Replies {@see #JOSM_UNKNOWN_VERSION} if the version isn't known. |
|---|
| 183 | * @return the JOSM version |
|---|
| 184 | */ |
|---|
| 185 | public int getVersion() { |
|---|
| 186 | return version; |
|---|
| 187 | } |
|---|
| 188 | |
|---|
| 189 | /** |
|---|
| 190 | * Replies true if this is a local build, i.e. an inofficial development build. |
|---|
| 191 | * |
|---|
| 192 | * @return true if this is a local build, i.e. an inofficial development build. |
|---|
| 193 | */ |
|---|
| 194 | public boolean isLocalBuild() { |
|---|
| 195 | return isLocalBuild; |
|---|
| 196 | } |
|---|
| 197 | |
|---|
| 198 | public String getAgentString() { |
|---|
| 199 | int v = getVersion(); |
|---|
| 200 | String s = (v == JOSM_UNKNOWN_VERSION) ? "UNKNOWN" : Integer.toString(v); |
|---|
| 201 | if (isLocalBuild() && v != JOSM_UNKNOWN_VERSION) { |
|---|
| 202 | s += " SVN"; |
|---|
| 203 | } |
|---|
| 204 | return "JOSM/1.5 ("+ s+" "+LanguageInfo.getJOSMLocaleCode()+")"; |
|---|
| 205 | } |
|---|
| 206 | } |
|---|