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