Changeset 2832 in josm for trunk/src


Ignore:
Timestamp:
2010-01-12T20:02:54+01:00 (14 years ago)
Author:
Gubaer
Message:

fixed NPE with new location for elemstyles.xml, see r2827

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

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/mappaint/MapPaintStyles.java

    r2827 r2832  
    11package org.openstreetmap.josm.gui.mappaint;
    22
     3import static org.openstreetmap.josm.tools.I18n.tr;
     4
     5import java.io.IOException;
    36import java.util.Collection;
    47import java.util.Collections;
     
    1215import org.openstreetmap.josm.tools.ImageProvider;
    1316import org.xml.sax.InputSource;
     17import org.xml.sax.SAXException;
    1418import org.xml.sax.XMLReader;
    1519import org.xml.sax.helpers.XMLReaderFactory;
     
    3135        {
    3236            String[] a;
    33             if(fileset.indexOf("=") >= 0)
     37            if(fileset.indexOf("=") >= 0) {
    3438                a = fileset.split("=", 2);
    35             else
     39            } else {
    3640                a = new String[] {"", fileset};
     41            }
    3742
    3843            /* non-prefixed path is generic path, always take it */
    39             if(a[0].length() == 0 || styleName.equals(a[0]))
     44            if(a[0].length() == 0 || styleName.equals(a[0])) {
    4045                dirs.add(a[1]);
     46            }
    4147        }
    4248        ImageIcon i = ImageProvider.getIfAvailable(dirs, "mappaint."+styleName, null, name);
     
    6167
    6268        Collection<String> files = Main.pref.getCollection("mappaint.style.sources", Collections.<String>emptySet());
    63         if(Main.pref.getBoolean("mappaint.style.enable-defaults", true))
    64         {
     69        if (Main.pref.getBoolean("mappaint.style.enable-defaults", true)) {
    6570            LinkedList<String> f = new LinkedList<String>();
    6671            f.add("resource://data/elemstyles.xml");
     
    6974        }
    7075
    71         for(String fileset : files)
    72         {
     76        for (String file : files) {
    7377            String[] a = null;
    74             try
    75             {
    76                 if(fileset.indexOf("=") >= 0)
    77                     a = fileset.split("=", 2);
    78                 else
    79                     a = new String[] {null, fileset};
     78            try {
     79                if (file.indexOf("=") >= 0) {
     80                    a = file.split("=", 2);
     81                } else {
     82                    a = new String[] { null, file };
     83                }
    8084                XMLReader xmlReader = XMLReaderFactory.createXMLReader();
    8185                ElemStyleHandler handler = new ElemStyleHandler(a[0]);
    8286                xmlReader.setContentHandler(handler);
    8387                xmlReader.setErrorHandler(handler);
    84                 xmlReader.parse(new InputSource(new MirroredInputStream(a[1])));
    85             }
    86             catch (Exception e)
    87             {
    88                 System.out.println("Mappaint-Style \"" + a[0] + "\" file \"" + a[1] + "\"");
    89                 System.out.println("Mappaint-Style problems: " + e);
     88                MirroredInputStream in = new MirroredInputStream(a[1]);
     89                xmlReader.parse(new InputSource(in));
     90            } catch(IOException e) {
     91                System.err.println(tr("Warning: failed to load Mappaint-Styles from ''{0}''. Exception was: {1}", a[1], e.toString()));
     92                e.printStackTrace();
     93            } catch(SAXException e) {
     94                System.err.println(tr("Warning: failed to parse Mappaint-Styles from ''{0}''. Exception was: {1}", a[1], e.toString()));
    9095                e.printStackTrace();
    9196            }
     
    9398        iconDirs = null;
    9499    }
    95 
    96100}
  • trunk/src/org/openstreetmap/josm/io/MirroredInputStream.java

    r2017 r2832  
    11// License: GPL. Copyright 2007 by Immanuel Scholz and others
    22package org.openstreetmap.josm.io;
     3
     4import static org.openstreetmap.josm.tools.I18n.tr;
    35
    46import java.io.BufferedInputStream;
     
    4143            if (url.getProtocol().equals("file")) {
    4244                file = new File(name.substring("file:/".length()));
    43                 if (!file.exists())
     45                if (!file.exists()) {
    4446                    file = new File(name.substring("file://".length()));
     47                }
    4548            } else {
    4649                file = checkLocal(url, destDir, maxTime);
     
    4952            if(name.startsWith("resource://")) {
    5053                fs = getClass().getResourceAsStream(
    51                 name.substring("resource:/".length()));
     54                        name.substring("resource:/".length()));
     55                if (fs == null)
     56                    throw new IOException(tr("Failed to open input stream for resource ''{0}''", name));
    5257                return;
    5358            }
     
    6166    public File getFile()
    6267    {
    63        return file;
     68        return file;
    6469    }
    6570
    6671    static public void cleanup(String name)
    6772    {
    68       cleanup(name, null);
     73        cleanup(name, null);
    6974    }
    7075    static public void cleanup(String name, String destDir)
     
    8085                    String[] lp = localPath.split(";");
    8186                    File lfile = new File(lp[1]);
    82                     if(lfile.exists())
     87                    if(lfile.exists()) {
    8388                        lfile.delete();
     89                    }
    8490                }
    8591                Main.pref.put("mirror." + url, null);
     
    94100            String[] lp = localPath.split(";");
    95101            file = new File(lp[1]);
    96             if (maxTime <= 0)
     102            if (maxTime <= 0) {
    97103                maxTime = Main.pref.getInteger("mirror.maxtime", 7*24*60*60);
     104            }
    98105            if (System.currentTimeMillis() - Long.parseLong(lp[0]) < maxTime*1000) {
    99                 if(file.exists()) {
     106                if(file.exists())
    100107                    return file;
    101                 }
    102108            }
    103109        }
    104         if(destDir == null)
     110        if(destDir == null) {
    105111            destDir = Main.pref.getPreferencesDir();
     112        }
    106113
    107114        File destDirFile = new File(destDir);
    108         if (!destDirFile.exists())
     115        if (!destDirFile.exists()) {
    109116            destDirFile.mkdirs();
     117        }
    110118
    111119        String a = url.toString().replaceAll("[^A-Za-z0-9_.-]", "_");
     
    121129            byte[] buffer = new byte[4096];
    122130            int length;
    123             while ((length = bis.read(buffer)) > -1)
     131            while ((length = bis.read(buffer)) > -1) {
    124132                bos.write(buffer, 0, length);
     133            }
    125134        } catch(IOException ioe) {
    126135            if (file != null)
     
    149158        return file;
    150159    }
     160    @Override
    151161    public int available() throws IOException
    152162    { return fs.available(); }
     163    @Override
    153164    public void close() throws IOException
    154165    { fs.close(); }
     166    @Override
    155167    public int read() throws IOException
    156168    { return fs.read(); }
     169    @Override
    157170    public int read(byte[] b) throws IOException
    158171    { return fs.read(b); }
     172    @Override
    159173    public int read(byte[] b, int off, int len) throws IOException
    160174    { return fs.read(b,off, len); }
     175    @Override
    161176    public long skip(long n) throws IOException
    162177    { return fs.skip(n); }
Note: See TracChangeset for help on using the changeset viewer.