Changeset 30738 in osm for applications/editors/josm/plugins/opendata/src/org/openstreetmap
- Timestamp:
- 2014-10-19T01:27:04+02:00 (10 years ago)
- 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 4 4 import java.io.File; 5 5 import java.io.FileInputStream; 6 import java.io.FileNotFoundException;7 6 import java.io.IOException; 8 7 import java.io.InputStream; … … 54 53 /** 55 54 * 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 57 56 */ 58 57 public class NeptuneReader extends AbstractReader implements FrenchConstants { … … 81 80 schemas.add(NeptuneReader.class.getResource(NEPTUNE_XSD)); 82 81 } 83 82 84 83 private ChouettePTNetworkType root; 85 84 86 85 private final Map<String, OsmPrimitive> tridentObjects = new HashMap<>(); 87 86 88 87 public static final boolean acceptsXmlNeptuneFile(File file) { 89 88 return acceptsXmlNeptuneFile(file, null); … … 91 90 92 91 public static final boolean acceptsXmlNeptuneFile(File file, URL schemaURL) { 93 92 94 93 if (schemaURL == null) { 95 94 schemaURL = schemas.get(0); 96 95 } 97 98 try { 99 FileInputStream in = new FileInputStream(file); 96 97 try (FileInputStream in = new FileInputStream(file)) { 100 98 Source xmlFile = new StreamSource(in); 101 99 try { … … 112 110 Main.error(xmlFile.getSystemId() + " is NOT valid"); 113 111 Main.error("Reason: " + e.getLocalizedMessage()); 114 } finally {115 try {116 in.close();117 } catch (IOException e) {118 // Ignore exception119 }120 112 } 121 } catch ( FileNotFoundException e) {113 } catch (IOException e) { 122 114 Main.error(e.getMessage()); 123 115 } 124 116 125 117 return false; 126 118 } 127 119 128 120 public static DataSet parseDataSet(InputStream in, AbstractDataSetHandler handler, ProgressMonitor instance) throws JAXBException { 129 121 return new NeptuneReader().parse(in, instance); … … 138 130 return doc.getValue(); 139 131 } 140 132 141 133 private final void linkTridentObjectToOsmPrimitive(TridentObjectType object, OsmPrimitive p) { 142 134 p.put("ref:neptune", object.getObjectId()); … … 151 143 return n; 152 144 } 153 145 154 146 private Node createPlatform(StopPointType stop) { 155 147 Node n = createNode(createLatLon(stop)); … … 181 173 return r; 182 174 } 183 175 184 176 protected Relation createNetwork(PTNetworkType network) { 185 177 Relation r = createRelation(OSM_NETWORK); … … 188 180 return r; 189 181 } 190 182 191 183 protected Relation createRouteMaster(LineType line) { 192 184 Relation r = createPtRelation(OSM_ROUTE_MASTER, line); … … 219 211 return r; 220 212 } 221 213 222 214 protected Relation createStopArea(StopAreaType sa) { 223 215 Relation r = createPtRelation(OSM_STOP_AREA, sa); … … 225 217 return r; 226 218 } 227 219 228 220 protected LatLon createLatLon(PointType point) { 229 221 return new LatLon(point.getLatitude().doubleValue(), point.getLongitude().doubleValue()); 230 222 } 231 223 232 224 protected final <T extends TridentObjectType> T findTridentObject(List<T> list, String id) { 233 225 for (T object : list) { … … 238 230 return null; 239 231 } 240 232 241 233 protected StopPoint findStopPoint(String id) { 242 234 return findTridentObject(root.getChouetteLineDescription().getStopPoint(), id); … … 311 303 } 312 304 } 313 305 314 306 } else if (childId.contains("StopPoint")) { 315 307 StopPoint child = findStopPoint(childId); … … 352 344 addStopToRoute(route, start); 353 345 } 354 346 355 347 if (end == null) { 356 348 System.err.println("Cannot find end StopPoint: "+ptlink.getEndOfLink()); … … 361 353 } 362 354 } 363 355 364 356 return ds; 365 357 } 366 358 367 359 private static final boolean addStopToRoute(Relation route, OsmPrimitive stop) { 368 360 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 33 33 34 34 private final IInArchive archive = new Handler(); 35 35 36 36 public SevenZipReader(InputStream in, AbstractDataSetHandler handler, boolean promptUser) throws IOException { 37 37 super(handler, handler != null ? handler.getArchiveHandler() : null, promptUser); … … 41 41 Utils.copyStream(in, out); 42 42 } 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); 50 51 } 51 throw new IOException(message);52 52 } 53 53 } 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) 56 56 throws IOException, XMLStreamException, FactoryConfigurationError, JAXBException { 57 57 return new SevenZipReader(in, handler, promptUser).parseDoc(instance); 58 58 } 59 59 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) 61 61 throws IOException, XMLStreamException, FactoryConfigurationError, JAXBException { 62 62 return new SevenZipReader(in, handler, promptUser).parseDocs(instance); … … 71 71 archive.Extract(null, -1, IInArchive.NExtract_NAskMode_kExtract, new ExtractCallback(archive, temp, candidates)); 72 72 } 73 73 74 74 private class ExtractCallback extends ArchiveExtractCallback { 75 75 private final List<File> candidates; 76 76 77 77 public ExtractCallback(IInArchive archive, File tempDir, List<File> candidates) { 78 78 Init(archive); … … 81 81 } 82 82 83 @Override 83 @Override 84 84 public int GetStream(int index, OutputStream[] outStream, int askExtractMode) throws IOException { 85 85 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 26 26 27 27 private final ZipInputStream zis; 28 28 29 29 private ZipEntry entry; 30 30 31 31 public ZipReader(InputStream in, AbstractDataSetHandler handler, boolean promptUser) { 32 32 super(handler, handler != null ? handler.getArchiveHandler() : null, promptUser); … … 34 34 } 35 35 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) 37 37 throws IOException, XMLStreamException, FactoryConfigurationError, JAXBException { 38 38 return new ZipReader(in, handler, promptUser).parseDoc(instance); 39 39 } 40 40 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) 42 42 throws IOException, XMLStreamException, FactoryConfigurationError, JAXBException { 43 43 return new ZipReader(in, handler, promptUser).parseDocs(instance); 44 44 } 45 45 46 @Override 46 47 protected void extractArchive(final File temp, final List<File> candidates) throws IOException, FileNotFoundException { 47 48 while ((entry = zis.getNextEntry()) != null) { … … 58 59 } 59 60 if (!entry.isDirectory()) { 60 if (!file.createNewFile()) { 61 if (!file.createNewFile()) { 61 62 throw new IOException("Could not create temp file: " + file.getAbsolutePath()); 62 63 } 63 64 // 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 } 69 71 } 70 fos.close();71 72 // Allow handler to perform specific treatments (for example, fix invalid .prj files) 72 73 if (archiveHandler != null) { -
applications/editors/josm/plugins/opendata/src/org/openstreetmap/josm/plugins/opendata/core/io/tabular/CsvImporter.java
r30723 r30738 18 18 19 19 public class CsvImporter extends AbstractImporter { 20 20 21 21 public static final ExtensionFileFilter CSV_FILE_FILTER = new ExtensionFileFilter( 22 22 OdConstants.CSV_EXT, OdConstants.CSV_EXT, tr("CSV files") + " (*."+OdConstants.CSV_EXT+")"); 23 23 24 24 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 26 26 public CsvImporter() { 27 27 super(CSV_FILE_FILTER); … … 46 46 boolean result = false; 47 47 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); 56 51 } catch (IOException e) { 57 52 // Ignore exceptions -
applications/editors/josm/plugins/opendata/src/org/openstreetmap/josm/plugins/opendata/core/modules/ModuleInformation.java
r30731 r30738 73 73 this.name = name; 74 74 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 ) { 80 79 Manifest manifest = jar.getManifest(); 81 80 if (manifest == null) … … 85 84 } catch (IOException e) { 86 85 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 }98 86 } 99 87 } -
applications/editors/josm/plugins/opendata/src/org/openstreetmap/josm/plugins/opendata/core/modules/ReadRemoteModuleInformationTask.java
r30563 r30738 25 25 import java.util.List; 26 26 27 import org.openstreetmap.josm.Main; 27 28 import org.openstreetmap.josm.data.Version; 28 29 import org.openstreetmap.josm.gui.PleaseWaitRunnable; … … 246 247 */ 247 248 protected void cacheModuleList(String site, String list) { 248 PrintWriter writer = null;249 249 try { 250 250 File moduleDir = OdPlugin.getInstance().getModulesDirectory(); 251 251 if (!moduleDir.exists()) { 252 252 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)); 254 255 } 255 256 } 256 257 File cacheFile = createSiteCacheFile(moduleDir, site, CacheType.PLUGIN_LIST); 257 258 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 } 260 262 } catch (IOException e) { 261 263 // 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); 268 265 } 269 266 } … … 300 297 File [] f = new File(location).listFiles( 301 298 new FilenameFilter() { 299 @Override 302 300 public boolean accept(File dir, String name) { 303 301 return name.matches("^([0-9]+-)?site.*\\.txt$") || -
applications/editors/josm/plugins/opendata/src/org/openstreetmap/josm/plugins/opendata/core/util/NamesFrUtils.java
r30723 r30738 11 11 12 12 import org.apache.commons.lang3.text.WordUtils; 13 import org.openstreetmap.josm.Main; 13 14 import org.openstreetmap.josm.data.osm.OsmPrimitive; 14 15 import org.openstreetmap.josm.plugins.opendata.core.OdConstants; … … 16 17 17 18 public abstract class NamesFrUtils { 18 19 19 20 private static Map<String, String> dictionary = initDictionary(); 20 21 … … 29 30 return result; 30 31 } 31 32 32 33 private static Map<String, String> initDictionary() { 33 34 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))) { 37 37 String line = reader.readLine(); // Skip first line 38 38 while ((line = reader.readLine()) != null) { … … 40 40 result.put(tab[0].replace("\"", ""), tab[1].replace("\"", "")); 41 41 } 42 reader.close();43 42 } catch (IOException e) { 44 e.printStackTrace();43 Main.error(e); 45 44 } 46 45 return result; … … 170 169 if (value != null) { 171 170 value = WordUtils.capitalizeFully(value); 172 // Cas particuliers 171 // Cas particuliers 173 172 if (value.equals("Boulingrin")) { // square Boulingrin, mal formé 174 173 value = "Sq Boulingrin";
Note:
See TracChangeset
for help on using the changeset viewer.