Changeset 28031 in osm for applications/editors/josm/plugins/opendata/src/org
- Timestamp:
- 2012-03-10T16:18:51+01:00 (13 years ago)
- Location:
- applications/editors/josm/plugins/opendata/src/org/openstreetmap/josm/plugins/opendata
- Files:
-
- 24 added
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/opendata/src/org/openstreetmap/josm/plugins/opendata/OdPlugin.java
r28018 r28031 45 45 private static OdPlugin instance; 46 46 47 public final XmlImporter xmlImporter; 48 47 49 public OdPlugin(PluginInformation info) { // NO_UCD 48 50 super(info); … … 57 59 new KmlKmzImporter(), new ShpImporter(), new MifTabImporter(), // Geographic file formats 58 60 new ZipImporter(), // Archive containing any of the others 59 new XmlImporter() // Generic importer for XML files (currently used for Neptune files)61 xmlImporter = new XmlImporter() // Generic importer for XML files (currently used for Neptune files) 60 62 })) { 61 63 ExtensionFileFilter.importers.add(0, importer); -
applications/editors/josm/plugins/opendata/src/org/openstreetmap/josm/plugins/opendata/core/datasets/fr/FrenchDataSetHandler.java
r28022 r28031 18 18 import static org.openstreetmap.josm.plugins.opendata.core.io.LambertCC9ZonesProjectionPatterns.lambertCC9Zones; 19 19 20 import java.io.File;21 import java.io.IOException;22 20 import java.net.MalformedURLException; 23 21 import java.net.URL; … … 26 24 import java.util.regex.Matcher; 27 25 import java.util.regex.Pattern; 28 29 import javax.xml.XMLConstants;30 import javax.xml.transform.Source;31 import javax.xml.transform.stream.StreamSource;32 import javax.xml.validation.Schema;33 import javax.xml.validation.SchemaFactory;34 import javax.xml.validation.Validator;35 26 36 27 import org.geotools.referencing.CRS; … … 47 38 import org.openstreetmap.josm.data.projection.UTM.Hemisphere; 48 39 import org.openstreetmap.josm.plugins.opendata.core.datasets.SimpleDataSetHandler; 49 import org.xml.sax.SAXException;50 40 51 41 public abstract class FrenchDataSetHandler extends SimpleDataSetHandler implements FrenchConstants { … … 283 273 } 284 274 } 285 286 protected URL getNeptuneSchema() {287 return FrenchDataSetHandler.class.getResource(NEPTUNE_XSD);288 }289 290 public final boolean acceptsXmlNeptuneFile(File file) {291 292 Source xmlFile = new StreamSource(file);293 294 try {295 SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);296 Schema schema = schemaFactory.newSchema(getNeptuneSchema());297 Validator validator = schema.newValidator();298 validator.validate(xmlFile);299 System.out.println(xmlFile.getSystemId() + " is valid");300 return true;301 } catch (SAXException e) {302 System.out.println(xmlFile.getSystemId() + " is NOT valid");303 System.out.println("Reason: " + e.getLocalizedMessage());304 } catch (IOException e) {305 System.out.println(xmlFile.getSystemId() + " is NOT valid");306 System.out.println("Reason: " + e.getLocalizedMessage());307 }308 309 return false;310 }311 275 } -
applications/editors/josm/plugins/opendata/src/org/openstreetmap/josm/plugins/opendata/core/io/NeptuneReader.java
r28022 r28031 16 16 package org.openstreetmap.josm.plugins.opendata.core.io; 17 17 18 import java.io.File; 19 import java.io.IOException; 18 20 import java.io.InputStream; 21 import java.net.URL; 22 import java.util.ArrayList; 19 23 import java.util.HashMap; 20 24 import java.util.List; 21 25 import java.util.Map; 22 26 27 import javax.xml.XMLConstants; 23 28 import javax.xml.bind.JAXBContext; 24 29 import javax.xml.bind.JAXBElement; 25 30 import javax.xml.bind.JAXBException; 26 31 import javax.xml.bind.Unmarshaller; 32 import javax.xml.transform.Source; 33 import javax.xml.transform.stream.StreamSource; 34 import javax.xml.validation.Schema; 35 import javax.xml.validation.SchemaFactory; 36 import javax.xml.validation.Validator; 27 37 28 38 import neptune.ChouetteAreaType; … … 50 60 import org.openstreetmap.josm.io.AbstractReader; 51 61 import org.openstreetmap.josm.plugins.opendata.core.datasets.AbstractDataSetHandler; 62 import org.openstreetmap.josm.plugins.opendata.core.datasets.fr.FrenchConstants; 63 import org.xml.sax.SAXException; 52 64 53 65 /** … … 55 67 * See http://www.chouette.mobi/IMG/pdf/NF__F_-Neptune-maj.pdf 56 68 */ 57 public class NeptuneReader extends AbstractReader {69 public class NeptuneReader extends AbstractReader implements FrenchConstants { 58 70 59 71 public static final String OSM_PUBLIC_TRANSPORT = "public_transport"; … … 76 88 public static final String OSM_FERRY = "ferry"; 77 89 90 private static final List<URL> schemas = new ArrayList<URL>(); 91 static { 92 schemas.add(NeptuneReader.class.getResource(NEPTUNE_XSD)); 93 } 94 78 95 private ChouettePTNetworkType root; 79 96 80 97 private final Map<String, OsmPrimitive> tridentObjects = new HashMap<String, OsmPrimitive>(); 98 99 public static final boolean acceptsXmlNeptuneFile(File file) { 100 return acceptsXmlNeptuneFile(file, null); 101 } 102 103 public static final boolean acceptsXmlNeptuneFile(File file, URL schemaURL) { 104 105 if (schemaURL == null) { 106 schemaURL = schemas.get(0); 107 } 108 109 Source xmlFile = new StreamSource(file); 110 111 try { 112 SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); 113 Schema schema = schemaFactory.newSchema(schemaURL); 114 Validator validator = schema.newValidator(); 115 validator.validate(xmlFile); 116 System.out.println(xmlFile.getSystemId() + " is valid"); 117 return true; 118 } catch (SAXException e) { 119 System.out.println(xmlFile.getSystemId() + " is NOT valid"); 120 System.out.println("Reason: " + e.getLocalizedMessage()); 121 } catch (IOException e) { 122 System.out.println(xmlFile.getSystemId() + " is NOT valid"); 123 System.out.println("Reason: " + e.getLocalizedMessage()); 124 } 125 126 return false; 127 } 81 128 82 129 public static DataSet parseDataSet(InputStream in, AbstractDataSetHandler handler, ProgressMonitor instance) throws JAXBException { … … 102 149 Node n = new Node(latlon); 103 150 ds.addPrimitive(n); 151 return n; 152 } 153 154 private Node createPlatform(StopPointType stop) { 155 Node n = createNode(createLatLon(stop)); 156 n.put(OSM_PUBLIC_TRANSPORT, OSM_PLATFORM); 157 linkTridentObjectToOsmPrimitive(stop, n); 158 n.put("name", stop.getName()); 104 159 return n; 105 160 } … … 223 278 for (String grandchildId : child.getContains()) { 224 279 if (grandchildId.contains("StopPoint")) { 225 // TODO226 280 StopPoint grandchild = findStopPoint(grandchildId); 227 281 if (grandchild == null) { … … 229 283 } else { 230 284 if (grandchild.getLongLatType().equals(LongLatTypeType.WGS_84)) { 231 Node stop = createStopPosition(grandchild);232 stopArea.addMember(new RelationMember(OSM_ STOP, stop));285 Node platform = createPlatform(grandchild); 286 stopArea.addMember(new RelationMember(OSM_PLATFORM, platform)); 233 287 } else { 234 288 System.err.println("Unsupported long/lat type: "+grandchild.getLongLatType()); … … 248 302 for (RelationMember member : stopArea.getMembers()) { 249 303 // Fix stop coordinates if needed 250 if (member.getRole().equals(OSM_ STOP) && isNullLatLon(member.getNode().getCoor())) {304 if (member.getRole().equals(OSM_PLATFORM) && isNullLatLon(member.getNode().getCoor())) { 251 305 member.getNode().setCoor(createLatLon(areaCentroid)); 252 306 } … … 295 349 if (start == null) { 296 350 System.err.println("Cannot find start StopPoint: "+ptlink.getStartOfLink()); 297 } else {351 } else if (start.get(OSM_PUBLIC_TRANSPORT).equals(OSM_STOP) || start.get(OSM_PUBLIC_TRANSPORT).equals(OSM_PLATFORM)) { 298 352 addStopToRoute(route, start); 299 353 } … … 301 355 if (end == null) { 302 356 System.err.println("Cannot find end StopPoint: "+ptlink.getEndOfLink()); 303 } else {357 } else if (end.get(OSM_PUBLIC_TRANSPORT).equals(OSM_STOP) || end.get(OSM_PUBLIC_TRANSPORT).equals(OSM_PLATFORM)) { 304 358 addStopToRoute(route, end); 305 359 } … … 310 364 return ds; 311 365 } 312 366 313 367 private static final boolean addStopToRoute(Relation route, OsmPrimitive stop) { 314 368 if (route.getMembersCount() == 0 || !route.getMember(route.getMembersCount()-1).getMember().equals(stop) ) { 315 route.addMember(new RelationMember( OSM_STOP, stop));369 route.addMember(new RelationMember(stop.get(OSM_PUBLIC_TRANSPORT), stop)); 316 370 return true; 317 371 } else { … … 319 373 } 320 374 } 375 376 public static List<URL> getSchemas() { 377 return schemas; 378 } 379 380 public static void registerSchema(URL resource) { 381 if (resource != null && !schemas.contains(resource)) { 382 schemas.add(resource); 383 } 384 } 321 385 } -
applications/editors/josm/plugins/opendata/src/org/openstreetmap/josm/plugins/opendata/core/io/XmlImporter.java
r28021 r28031 16 16 package org.openstreetmap.josm.plugins.opendata.core.io; 17 17 18 import java.io.File; 18 19 import java.io.InputStream; 20 import java.net.URL; 19 21 20 22 import javax.xml.bind.JAXBException; … … 30 32 } 31 33 34 /* (non-Javadoc) 35 * @see org.openstreetmap.josm.io.FileImporter#acceptFile(java.io.File) 36 */ 37 @Override 38 public boolean acceptFile(File pathname) { 39 if (super.acceptFile(pathname)) { 40 for (URL schemaURL : NeptuneReader.getSchemas()) { 41 if (NeptuneReader.acceptsXmlNeptuneFile(pathname, schemaURL)) { 42 return true; 43 } 44 } 45 } 46 return false; 47 } 48 32 49 @Override 33 50 protected DataSet parseDataSet(InputStream in, ProgressMonitor instance) 34 51 throws IllegalDataException { 35 52 try { 36 // TODO: check it is a neptune file37 53 return NeptuneReader.parseDataSet(in, handler, instance); 38 54 } catch (JAXBException e) { -
applications/editors/josm/plugins/opendata/src/org/openstreetmap/josm/plugins/opendata/core/io/archive/ZipReader.java
r28022 r28031 33 33 import org.openstreetmap.josm.gui.progress.ProgressMonitor; 34 34 import org.openstreetmap.josm.io.AbstractReader; 35 import org.openstreetmap.josm.plugins.opendata.OdPlugin; 35 36 import org.openstreetmap.josm.plugins.opendata.core.OdConstants; 36 37 import org.openstreetmap.josm.plugins.opendata.core.datasets.AbstractDataSetHandler; 37 import org.openstreetmap.josm.plugins.opendata.core.datasets.fr.FrenchDataSetHandler;38 38 import org.openstreetmap.josm.plugins.opendata.core.io.NeptuneReader; 39 39 import org.openstreetmap.josm.plugins.opendata.core.io.geographic.KmlReader; … … 146 146 from = TabReader.parseDataSet(in, file, handler, instance); 147 147 } else if (file.getName().toLowerCase().endsWith(XML_EXT)) { 148 if ( handler instanceof FrenchDataSetHandler && ((FrenchDataSetHandler)handler).acceptsXmlNeptuneFile(file)) {148 if (OdPlugin.getInstance().xmlImporter.acceptFile(file)) { 149 149 from = NeptuneReader.parseDataSet(in, handler, instance); 150 150 } else {
Note:
See TracChangeset
for help on using the changeset viewer.