Ignore:
Timestamp:
2014-10-19T01:27:04+02:00 (10 years ago)
Author:
donvip
Message:

[josm_plugins] fix java 7 warnings / global usage of try-with-resource

Location:
applications/editors/josm/plugins/opendata/src/org/openstreetmap/josm/plugins/opendata/core
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/opendata/src/org/openstreetmap/josm/plugins/opendata/core/io/NeptuneReader.java

    r30723 r30738  
    44import java.io.File;
    55import java.io.FileInputStream;
    6 import java.io.FileNotFoundException;
    76import java.io.IOException;
    87import java.io.InputStream;
     
    5453/**
    5554 * NEPTUNE -> OSM converter
    56  * See http://www.chouette.mobi/IMG/pdf/NF__F_-Neptune-maj.pdf 
     55 * See http://www.chouette.mobi/IMG/pdf/NF__F_-Neptune-maj.pdf
    5756 */
    5857public class NeptuneReader extends AbstractReader implements FrenchConstants {
     
    8180        schemas.add(NeptuneReader.class.getResource(NEPTUNE_XSD));
    8281    }
    83    
     82
    8483    private ChouettePTNetworkType root;
    85    
     84
    8685    private final Map<String, OsmPrimitive> tridentObjects = new HashMap<>();
    87    
     86
    8887    public static final boolean acceptsXmlNeptuneFile(File file) {
    8988        return acceptsXmlNeptuneFile(file, null);
     
    9190
    9291    public static final boolean acceptsXmlNeptuneFile(File file, URL schemaURL) {
    93        
     92
    9493        if (schemaURL == null) {
    9594            schemaURL = schemas.get(0);
    9695        }
    97        
    98         try {
    99             FileInputStream in = new FileInputStream(file);
     96
     97        try (FileInputStream in = new FileInputStream(file)) {
    10098            Source xmlFile = new StreamSource(in);
    10199            try {
     
    112110                Main.error(xmlFile.getSystemId() + " is NOT valid");
    113111                Main.error("Reason: " + e.getLocalizedMessage());
    114             } finally {
    115                 try {
    116                     in.close();
    117                 } catch (IOException e) {
    118                     // Ignore exception
    119                 }
    120112            }
    121         } catch (FileNotFoundException e) {
     113        } catch (IOException e) {
    122114            Main.error(e.getMessage());
    123115        }
    124        
     116
    125117        return false;
    126118    }
    127    
     119
    128120    public static DataSet parseDataSet(InputStream in, AbstractDataSetHandler handler, ProgressMonitor instance) throws JAXBException {
    129121        return new NeptuneReader().parse(in, instance);
     
    138130        return doc.getValue();
    139131    }
    140    
     132
    141133    private final void linkTridentObjectToOsmPrimitive(TridentObjectType object, OsmPrimitive p) {
    142134        p.put("ref:neptune", object.getObjectId());
     
    151143        return n;
    152144    }
    153    
     145
    154146    private Node createPlatform(StopPointType stop) {
    155147        Node n = createNode(createLatLon(stop));
     
    181173        return r;
    182174    }
    183    
     175
    184176    protected Relation createNetwork(PTNetworkType network) {
    185177        Relation r = createRelation(OSM_NETWORK);
     
    188180        return r;
    189181    }
    190    
     182
    191183    protected Relation createRouteMaster(LineType line) {
    192184        Relation r = createPtRelation(OSM_ROUTE_MASTER, line);
     
    219211        return r;
    220212    }
    221    
     213
    222214    protected Relation createStopArea(StopAreaType sa) {
    223215        Relation r = createPtRelation(OSM_STOP_AREA, sa);
     
    225217        return r;
    226218    }
    227    
     219
    228220    protected LatLon createLatLon(PointType point) {
    229221        return new LatLon(point.getLatitude().doubleValue(), point.getLongitude().doubleValue());
    230222    }
    231    
     223
    232224    protected final <T extends TridentObjectType> T findTridentObject(List<T> list, String id) {
    233225        for (T object : list) {
     
    238230        return null;
    239231    }
    240    
     232
    241233    protected StopPoint findStopPoint(String id) {
    242234        return findTridentObject(root.getChouetteLineDescription().getStopPoint(), id);
     
    311303                            }
    312304                        }
    313                        
     305
    314306                    } else if (childId.contains("StopPoint")) {
    315307                        StopPoint child = findStopPoint(childId);
     
    352344                        addStopToRoute(route, start);
    353345                    }
    354                    
     346
    355347                    if (end == null) {
    356348                        System.err.println("Cannot find end StopPoint: "+ptlink.getEndOfLink());
     
    361353            }
    362354        }
    363        
     355
    364356        return ds;
    365357    }
    366        
     358
    367359    private static final boolean addStopToRoute(Relation route, OsmPrimitive stop) {
    368360        if (route.getMembersCount() == 0 || !route.getMember(route.getMembersCount()-1).getMember().equals(stop) ) {
  • applications/editors/josm/plugins/opendata/src/org/openstreetmap/josm/plugins/opendata/core/io/archive/SevenZipReader.java

    r30568 r30738  
    3333
    3434    private final IInArchive archive = new Handler();
    35    
     35
    3636    public SevenZipReader(InputStream in, AbstractDataSetHandler handler, boolean promptUser) throws IOException {
    3737        super(handler, handler != null ? handler.getArchiveHandler() : null, promptUser);
     
    4141            Utils.copyStream(in, out);
    4242        }
    43         IInStream random = new MyRandomAccessFile(tmpFile.getPath(), "r");
    44         if (archive.Open(random) != 0) {
    45             String message = "Unable to open 7z archive: "+tmpFile.getPath();
    46             Main.warn(message);
    47             random.close();
    48             if (!tmpFile.delete()) {
    49                 tmpFile.deleteOnExit();
     43        try (IInStream random = new MyRandomAccessFile(tmpFile.getPath(), "r")) {
     44            if (archive.Open(random) != 0) {
     45                String message = "Unable to open 7z archive: "+tmpFile.getPath();
     46                Main.warn(message);
     47                if (!tmpFile.delete()) {
     48                    tmpFile.deleteOnExit();
     49                }
     50                throw new IOException(message);
    5051            }
    51             throw new IOException(message);
    5252        }
    5353    }
    54    
    55     public static DataSet parseDataSet(InputStream in, AbstractDataSetHandler handler, ProgressMonitor instance, boolean promptUser) 
     54
     55    public static DataSet parseDataSet(InputStream in, AbstractDataSetHandler handler, ProgressMonitor instance, boolean promptUser)
    5656            throws IOException, XMLStreamException, FactoryConfigurationError, JAXBException {
    5757        return new SevenZipReader(in, handler, promptUser).parseDoc(instance);
    5858    }
    5959
    60     public static Map<File, DataSet> parseDataSets(InputStream in, AbstractDataSetHandler handler, ProgressMonitor instance, boolean promptUser) 
     60    public static Map<File, DataSet> parseDataSets(InputStream in, AbstractDataSetHandler handler, ProgressMonitor instance, boolean promptUser)
    6161            throws IOException, XMLStreamException, FactoryConfigurationError, JAXBException {
    6262        return new SevenZipReader(in, handler, promptUser).parseDocs(instance);
     
    7171        archive.Extract(null, -1, IInArchive.NExtract_NAskMode_kExtract, new ExtractCallback(archive, temp, candidates));
    7272    }
    73    
     73
    7474    private class ExtractCallback extends ArchiveExtractCallback {
    7575        private final List<File> candidates;
    76        
     76
    7777        public ExtractCallback(IInArchive archive, File tempDir, List<File> candidates) {
    7878            Init(archive);
     
    8181        }
    8282
    83         @Override 
     83        @Override
    8484        public int GetStream(int index, OutputStream[] outStream, int askExtractMode) throws IOException {
    8585            int res = super.GetStream(index, outStream, askExtractMode);
  • applications/editors/josm/plugins/opendata/src/org/openstreetmap/josm/plugins/opendata/core/io/archive/ZipReader.java

    r30723 r30738  
    2626
    2727    private final ZipInputStream zis;
    28    
     28
    2929    private ZipEntry entry;
    30    
     30
    3131    public ZipReader(InputStream in, AbstractDataSetHandler handler, boolean promptUser) {
    3232        super(handler, handler != null ? handler.getArchiveHandler() : null, promptUser);
     
    3434    }
    3535
    36     public static DataSet parseDataSet(InputStream in, AbstractDataSetHandler handler, ProgressMonitor instance, boolean promptUser) 
     36    public static DataSet parseDataSet(InputStream in, AbstractDataSetHandler handler, ProgressMonitor instance, boolean promptUser)
    3737            throws IOException, XMLStreamException, FactoryConfigurationError, JAXBException {
    3838        return new ZipReader(in, handler, promptUser).parseDoc(instance);
    3939    }
    4040
    41     public static Map<File, DataSet> parseDataSets(InputStream in, AbstractDataSetHandler handler, ProgressMonitor instance, boolean promptUser) 
     41    public static Map<File, DataSet> parseDataSets(InputStream in, AbstractDataSetHandler handler, ProgressMonitor instance, boolean promptUser)
    4242            throws IOException, XMLStreamException, FactoryConfigurationError, JAXBException {
    4343        return new ZipReader(in, handler, promptUser).parseDocs(instance);
    4444    }
    4545
     46    @Override
    4647    protected void extractArchive(final File temp, final List<File> candidates) throws IOException, FileNotFoundException {
    4748        while ((entry = zis.getNextEntry()) != null) {
     
    5859            }
    5960            if (!entry.isDirectory()) {
    60                 if (!file.createNewFile()) { 
     61                if (!file.createNewFile()) {
    6162                    throw new IOException("Could not create temp file: " + file.getAbsolutePath());
    6263                }
    6364                // Write temp file
    64                 FileOutputStream fos = new FileOutputStream(file);
    65                 byte[] buffer = new byte[8192];
    66                 int count = 0;
    67                 while ((count = zis.read(buffer, 0, buffer.length)) > 0) {
    68                     fos.write(buffer, 0, count);
     65                try (FileOutputStream fos = new FileOutputStream(file)) {
     66                    byte[] buffer = new byte[8192];
     67                    int count = 0;
     68                    while ((count = zis.read(buffer, 0, buffer.length)) > 0) {
     69                        fos.write(buffer, 0, count);
     70                    }
    6971                }
    70                 fos.close();
    7172                // Allow handler to perform specific treatments (for example, fix invalid .prj files)
    7273                if (archiveHandler != null) {
  • applications/editors/josm/plugins/opendata/src/org/openstreetmap/josm/plugins/opendata/core/io/tabular/CsvImporter.java

    r30723 r30738  
    1818
    1919public class CsvImporter extends AbstractImporter {
    20    
     20
    2121    public static final ExtensionFileFilter CSV_FILE_FILTER = new ExtensionFileFilter(
    2222            OdConstants.CSV_EXT, OdConstants.CSV_EXT, tr("CSV files") + " (*."+OdConstants.CSV_EXT+")");
    23    
     23
    2424    public static final String COLOMBUS_HEADER = "INDEX,TAG,DATE,TIME,LATITUDE N/S,LONGITUDE E/W,HEIGHT,SPEED,HEADING,FIX MODE,VALID,PDOP,HDOP,VDOP,VOX";
    25    
     25
    2626    public CsvImporter() {
    2727        super(CSV_FILE_FILTER);
     
    4646        boolean result = false;
    4747        if (file != null && file.isFile()) {
    48             try {
    49                 BufferedReader reader = new BufferedReader(new FileReader(file));
    50                 try {
    51                     String line = reader.readLine();
    52                     result = line != null && line.equalsIgnoreCase(COLOMBUS_HEADER);
    53                 } finally {
    54                     reader.close();
    55                 }
     48            try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
     49                String line = reader.readLine();
     50                result = line != null && line.equalsIgnoreCase(COLOMBUS_HEADER);
    5651            } catch (IOException e) {
    5752                // Ignore exceptions
  • applications/editors/josm/plugins/opendata/src/org/openstreetmap/josm/plugins/opendata/core/modules/ModuleInformation.java

    r30731 r30738  
    7373        this.name = name;
    7474        this.file = file;
    75         FileInputStream fis = null;
    76         JarInputStream jar = null;
    77         try {
    78             fis = new FileInputStream(file);
    79             jar = new JarInputStream(fis);
     75        try (
     76            FileInputStream fis = new FileInputStream(file);
     77            JarInputStream jar = new JarInputStream(fis);
     78        ) {
    8079            Manifest manifest = jar.getManifest();
    8180            if (manifest == null)
     
    8584        } catch (IOException e) {
    8685            throw new ModuleException(name, e);
    87         } finally {
    88             if (jar != null) {
    89                 try {
    90                     jar.close();
    91                 } catch(IOException e) { /* ignore */ }
    92             }
    93             if (fis != null) {
    94                 try {
    95                     fis.close();
    96                 } catch(IOException e) { /* ignore */ }
    97             }
    9886        }
    9987    }
  • applications/editors/josm/plugins/opendata/src/org/openstreetmap/josm/plugins/opendata/core/modules/ReadRemoteModuleInformationTask.java

    r30563 r30738  
    2525import java.util.List;
    2626
     27import org.openstreetmap.josm.Main;
    2728import org.openstreetmap.josm.data.Version;
    2829import org.openstreetmap.josm.gui.PleaseWaitRunnable;
     
    246247     */
    247248    protected void cacheModuleList(String site, String list) {
    248         PrintWriter writer = null;
    249249        try {
    250250            File moduleDir = OdPlugin.getInstance().getModulesDirectory();
    251251            if (!moduleDir.exists()) {
    252252                if (! moduleDir.mkdirs()) {
    253                     System.err.println(tr("Warning: failed to create module directory ''{0}''. Cannot cache module list from module site ''{1}''.", moduleDir.toString(), site));
     253                    Main.warn(tr("Warning: failed to create module directory ''{0}''. Cannot cache module list from module site ''{1}''.",
     254                            moduleDir.toString(), site));
    254255                }
    255256            }
    256257            File cacheFile = createSiteCacheFile(moduleDir, site, CacheType.PLUGIN_LIST);
    257258            getProgressMonitor().subTask(tr("Writing module list to local cache ''{0}''", cacheFile.toString()));
    258             writer = new PrintWriter(new OutputStreamWriter(new FileOutputStream(cacheFile), "utf-8"));
    259             writer.write(list);
     259            try (PrintWriter writer = new PrintWriter(new OutputStreamWriter(new FileOutputStream(cacheFile), "utf-8"))) {
     260                writer.write(list);
     261            }
    260262        } catch (IOException e) {
    261263            // just failed to write the cache file. No big deal, but log the exception anyway
    262             e.printStackTrace();
    263         } finally {
    264             if (writer != null) {
    265                 writer.flush();
    266                 writer.close();
    267             }
     264            Main.warn(e);
    268265        }
    269266    }
     
    300297            File [] f = new File(location).listFiles(
    301298                    new FilenameFilter() {
     299                        @Override
    302300                        public boolean accept(File dir, String name) {
    303301                            return name.matches("^([0-9]+-)?site.*\\.txt$") ||
  • applications/editors/josm/plugins/opendata/src/org/openstreetmap/josm/plugins/opendata/core/util/NamesFrUtils.java

    r30723 r30738  
    1111
    1212import org.apache.commons.lang3.text.WordUtils;
     13import org.openstreetmap.josm.Main;
    1314import org.openstreetmap.josm.data.osm.OsmPrimitive;
    1415import org.openstreetmap.josm.plugins.opendata.core.OdConstants;
     
    1617
    1718public abstract class NamesFrUtils {
    18    
     19
    1920    private static Map<String, String> dictionary = initDictionary();
    2021
     
    2930        return result;
    3031    }
    31    
     32
    3233    private static Map<String, String> initDictionary() {
    3334        Map<String, String> result = new HashMap<>();
    34         try {
    35             BufferedReader reader = new BufferedReader(new InputStreamReader(
    36                     SimpleDataSetHandler.class.getResourceAsStream(OdConstants.DICTIONARY_FR), OdConstants.UTF8));
     35        try (BufferedReader reader = new BufferedReader(new InputStreamReader(
     36                SimpleDataSetHandler.class.getResourceAsStream(OdConstants.DICTIONARY_FR), OdConstants.UTF8))) {
    3737            String line = reader.readLine(); // Skip first line
    3838            while ((line = reader.readLine()) != null) {
     
    4040                result.put(tab[0].replace("\"", ""), tab[1].replace("\"", ""));
    4141            }
    42             reader.close();
    4342        } catch (IOException e) {
    44             e.printStackTrace();
     43            Main.error(e);
    4544        }
    4645        return result;
     
    170169            if (value != null) {
    171170                value = WordUtils.capitalizeFully(value);
    172                 // Cas particuliers 
     171                // Cas particuliers
    173172                if (value.equals("Boulingrin")) { // square Boulingrin, mal formé
    174173                    value = "Sq Boulingrin";
Note: See TracChangeset for help on using the changeset viewer.