Ignore:
Timestamp:
2015-11-18T00:18:07+01:00 (8 years ago)
Author:
Don-vip
Message:

fix #12112 - Use java.util.Properties to read REVISION file in the Version-class (patch by floscher) + checkstyle

Location:
trunk/src/org/openstreetmap/josm/data
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/Version.java

    r8846 r9019  
    44import static org.openstreetmap.josm.tools.I18n.tr;
    55
    6 import java.io.BufferedReader;
    76import java.io.IOException;
    8 import java.net.URL;
    9 import java.util.HashMap;
    10 import java.util.Map;
     7import java.io.InputStream;
    118import java.util.Map.Entry;
    12 import java.util.regex.Matcher;
    13 import java.util.regex.Pattern;
     9import java.util.Properties;
    1410
    1511import org.openstreetmap.josm.Main;
    1612import org.openstreetmap.josm.tools.LanguageInfo;
    17 import org.openstreetmap.josm.tools.Utils;
    1813
    1914/**
     
    2924
    3025    /**
    31      * Load the specified resource as string.
    32      *
    33      * @param resource the resource url to load
    34      * @return  the content of the resource file; null, if an error occurred
    35      */
    36     public static String loadResourceFile(URL resource) {
    37         if (resource == null) return null;
    38         String s = null;
    39         try {
    40             StringBuilder sb = new StringBuilder();
    41             try (BufferedReader in = Utils.openURLReader(resource)) {
    42                 for (String line = in.readLine(); line != null; line = in.readLine()) {
    43                     sb.append(line).append('\n');
    44                 }
    45             }
    46             s = sb.toString();
    47         } catch (IOException e) {
    48             Main.error(tr("Failed to load resource ''{0}'', error is {1}.", resource.toString(), e.toString()));
    49             Main.error(e);
    50         }
    51         return s;
    52     }
    53 
    54     /**
    5526     * Replies the unique instance of the version information
    5627     *
     
    7142    private boolean isLocalBuild;
    7243
    73     protected Map<String, String> parseManifestStyleFormattedString(String content) {
    74         Map<String, String> properties = new HashMap<>();
    75         if (content == null) return properties;
    76         Pattern p = Pattern.compile("^([^:]+):(.*)$");
    77         for (String line: content.split("\n")) {
    78             if (line == null || line.trim().isEmpty()) {
    79                 continue;
    80             }
    81             if (line.matches("^\\s*#.*$")) {
    82                 continue;
    83             }
    84             Matcher m = p.matcher(line);
    85             if (m.matches()) {
    86                 properties.put(m.group(1), m.group(2));
    87             }
    88         }
    89         return properties;
    90     }
    91 
    9244    /**
    9345     * Initializes the version infos from the revision resource file
    9446     *
    95      * @param revisionInfo the revision info loaded from a revision resource file
    96      */
    97     protected void initFromRevisionInfo(String revisionInfo) {
     47     * @param revisionInfo the revision info from a revision resource file as InputStream
     48     */
     49    protected void initFromRevisionInfo(InputStream revisionInfo) {
    9850        if (revisionInfo == null) {
    9951            this.releaseDescription = tr("UNKNOWN");
     
    10355        }
    10456
    105         Map<String, String> properties = parseManifestStyleFormattedString(revisionInfo);
    106         String value = properties.get("Revision");
     57        Properties properties = new Properties();
     58        try {
     59            properties.load(revisionInfo);
     60            revisionInfo.close();
     61        } catch (IOException e) {
     62            Main.warn(tr("Error reading revision info from revision file: {0}", e.getMessage()));
     63        }
     64        String value = properties.getProperty("Revision");
    10765        if (value != null) {
    10866            value = value.trim();
     
    11977        // the last changed data
    12078        //
    121         time = properties.get("Last Changed Date");
     79        time = properties.getProperty("Last Changed Date");
    12280        if (time == null) {
    123             time = properties.get("Build-Date");
     81            time = properties.getProperty("Build-Date");
    12482        }
    12583
     
    12785        //
    12886        isLocalBuild = false;
    129         value = properties.get("Is-Local-Build");
     87        value = properties.getProperty("Is-Local-Build");
    13088        if (value != null && "true".equalsIgnoreCase(value.trim()))  {
    13189            isLocalBuild = true;
     
    13593        //
    13694        buildName = null;
    137         value = properties.get("Build-Name");
     95        value = properties.getProperty("Build-Name");
    13896        if (value != null && !value.trim().isEmpty())  {
    13997            buildName = value.trim();
     
    143101        //
    144102        StringBuilder sb = new StringBuilder();
    145         for (Entry<String, String> property: properties.entrySet()) {
     103        for (Entry<Object, Object> property: properties.entrySet()) {
    146104            sb.append(property.getKey()).append(':').append(property.getValue()).append('\n');
    147105        }
     
    153111     */
    154112    public void init() {
    155         URL u = Main.class.getResource("/REVISION");
    156         if (u == null) {
     113        InputStream stream = Main.class.getResourceAsStream("/REVISION");
     114        if (stream == null) {
    157115            Main.warn(tr("The revision file ''/REVISION'' is missing."));
    158116            version = 0;
     
    160118            return;
    161119        }
    162         initFromRevisionInfo(loadResourceFile(u));
     120        initFromRevisionInfo(stream);
    163121    }
    164122
  • trunk/src/org/openstreetmap/josm/data/osm/visitor/paint/StyledMapRenderer.java

    r9016 r9019  
    451451    }
    452452
    453     protected void drawArea(OsmPrimitive osm, Path2D.Double path, Color color, MapImage fillImage, Float extent, boolean disabled, TextElement text) {
     453    protected void drawArea(OsmPrimitive osm, Path2D.Double path, Color color, MapImage fillImage, Float extent,
     454            boolean disabled, TextElement text) {
    454455
    455456        Shape area = path.createTransformedShape(nc.getAffineTransform());
Note: See TracChangeset for help on using the changeset viewer.