Changeset 9019 in josm for trunk/src/org


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
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/actions/AboutAction.java

    r8870 r9019  
    44import static org.openstreetmap.josm.tools.I18n.tr;
    55
     6import java.awt.Color;
    67import java.awt.Dimension;
    78import java.awt.GridBagLayout;
     
    910import java.awt.event.ActionEvent;
    1011import java.awt.event.KeyEvent;
     12import java.io.BufferedReader;
     13import java.io.IOException;
     14import java.io.InputStream;
     15import java.io.InputStreamReader;
    1116
    1217import javax.swing.BorderFactory;
     
    1722import javax.swing.JScrollPane;
    1823import javax.swing.JTabbedPane;
     24import javax.swing.JTextArea;
    1925
    2026import org.openstreetmap.josm.Main;
     
    5763        JosmTextArea readme = new JosmTextArea();
    5864        readme.setEditable(false);
    59         readme.setText(Version.loadResourceFile(Main.class.getResource("/README")));
     65        setTextFromResourceFile(readme, "/README");
    6066        readme.setCaretPosition(0);
    6167
     
    6773        JosmTextArea contribution = new JosmTextArea();
    6874        contribution.setEditable(false);
    69         contribution.setText(Version.loadResourceFile(Main.class.getResource("/CONTRIBUTION")));
     75        setTextFromResourceFile(contribution, "/CONTRIBUTION");
    7076        contribution.setCaretPosition(0);
    7177
    7278        JosmTextArea license = new JosmTextArea();
    7379        license.setEditable(false);
    74         license.setText(Version.loadResourceFile(Main.class.getResource("/LICENSE")));
     80        setTextFromResourceFile(license, "/LICENSE");
    7581        license.setCaretPosition(0);
    7682
     
    111117    }
    112118
     119    /**
     120     * Reads the contents of the resource file that is described by the {@code filePath}-attribute and puts that text
     121     * into the {@link JTextArea} given by the {@code ta}-attribute.
     122     * @param ta the {@link JTextArea} to put the files contents into
     123     * @param filePath the path where the resource file to read resides
     124     */
     125    private void setTextFromResourceFile(JTextArea ta, String filePath) {
     126        InputStream is = getClass().getResourceAsStream(filePath);
     127        if (is == null) {
     128            displayErrorMessage(ta, tr("Failed to locate resource ''{0}''.", filePath));
     129        } else {
     130            try {
     131                BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
     132                String line;
     133                while ((line = br.readLine()) != null) {
     134                    ta.append(line+'\n');
     135                }
     136                br.close();
     137            } catch (IOException e) {
     138                Main.warn(e);
     139                displayErrorMessage(ta, tr("Failed to load resource ''{0}'', error is {1}.", filePath, e.toString()));
     140            }
     141        }
     142    }
     143
     144    private static void displayErrorMessage(JTextArea ta, String msg) {
     145        Main.warn(msg);
     146        ta.setForeground(new Color(200, 0, 0));
     147        ta.setText(msg);
     148    }
     149
    113150    private static JScrollPane createScrollPane(JosmTextArea area) {
    114151        area.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
  • 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.