Changeset 9 in josm for src/org/openstreetmap/josm/io


Ignore:
Timestamp:
2005-10-04T00:09:32+02:00 (20 years ago)
Author:
imi
Message:
  • added support for DataReaders
  • added a nice status line and a tooltip when holding middle mouse button
Location:
src/org/openstreetmap/josm/io
Files:
1 added
1 edited

Legend:

Unmodified
Added
Removed
  • src/org/openstreetmap/josm/io/GpxReader.java

    r8 r9  
    33import java.io.IOException;
    44import java.io.Reader;
     5import java.util.HashMap;
    56
    67import org.jdom.Element;
     
    1011import org.openstreetmap.josm.data.GeoPoint;
    1112import org.openstreetmap.josm.data.osm.DataSet;
     13import org.openstreetmap.josm.data.osm.Key;
    1214import org.openstreetmap.josm.data.osm.LineSegment;
    1315import org.openstreetmap.josm.data.osm.Node;
     16import org.openstreetmap.josm.data.osm.OsmPrimitive;
    1417import org.openstreetmap.josm.data.osm.Track;
    1518import org.openstreetmap.josm.gui.Main;
    1619
    1720/**
    18  * Reads an gpx stream and construct a DataSet out of it. Some information may not be
    19  * imported, since JOSM does not fully support GPX.
     21 * Reads an gpx stream and construct a DataSet out of it.
     22 * Some information may not be imported, but GpxReader tries its best to load
     23 * all data possible in the key/value structure.
    2024 *
    2125 * @author imi
    2226 */
    23 public class GpxReader {
    24 
    25         public static final Namespace XSD = Namespace.getNamespace("http://www.w3.org/2001/XMLSchema");
    26         public static final Namespace GPX = Namespace.getNamespace("http://www.topografix.com/GPX/1/0");
     27public class GpxReader implements DataReader {
     28
     29        /**
     30         * The GPX namespace used.
     31         */
     32        private static final Namespace GPX = Namespace.getNamespace("http://www.topografix.com/GPX/1/0");
     33        /**
     34         * The OSM namespace used (for extensions).
     35         */
     36        private static final Namespace OSM = Namespace.getNamespace("osm");
     37
     38        /**
     39         * The data source from this reader.
     40         */
     41        public Reader source;
    2742       
    2843        /**
     44         * Construct a parser from a specific data source.
     45         * @param source The data source, as example a FileReader to read from a file.
     46         */
     47        public GpxReader(Reader source) {
     48                this.source = source;
     49        }
     50       
     51        /**
    2952         * Read the input stream and return a DataSet from the stream.
    30          *
    31          * @param in
    32          * @throws IOException          An error with the provided stream occoured.
    33          * @throws JDOMException        An parse error occoured.
    34          */
    35         public DataSet parse(Reader in) throws JDOMException, IOException {
     53         */
     54        public DataSet parse() throws ParseException, ConnectionException {
    3655                try {
    3756                        final SAXBuilder builder = new SAXBuilder();
    38                         Element root = builder.build(in).getRootElement();
     57                        Element root = builder.build(source).getRootElement();
    3958                        return parseDataSet(root);
    4059                } catch (NullPointerException npe) {
    41                         throw new JDOMException("NullPointerException. Probably a tag name mismatch.", npe);
     60                        throw new ParseException("NullPointerException. Probably a tag name mismatch.", npe);
    4261                } catch (ClassCastException cce) {
    43                         throw new JDOMException("ClassCastException. Probably a tag does not contain the correct type.", cce);
     62                        throw new ParseException("ClassCastException. Probably a tag does not contain the correct type.", cce);
     63                } catch (JDOMException e) {
     64                        throw new ParseException("The data could not be parsed. Reason: "+e.getMessage(), e);
     65                } catch (IOException e) {
     66                        throw new ConnectionException("The data could not be retrieved. Reason: "+e.getMessage(), e);
    4467                }
    4568        }
     
    5679                        Float.parseFloat(e.getAttributeValue("lat")),
    5780                        Float.parseFloat(e.getAttributeValue("lon")));
     81                for (Object o : e.getChildren()) {
     82                        Element child = (Element)o;
     83                        if (child.getName().equals("extensions"))
     84                                parseKeyValueExtensions(data, child);
     85                        else if (child.getName().equals("link"))
     86                                parseKeyValueLink(data, child);
     87                        else
     88                                parseKeyValueTag(data, child);
     89                }
    5890                return data;
     91        }
     92
     93        /**
     94         * Parse the extensions tag and add all properties found as key/value.
     95         * <code>osm.keys</code> may be <code>null</code>, in which case it is
     96         * created first. If <code>e</code> is <code>null</code>, nothing
     97         * happens.
     98         *
     99         * @param osm   The primitive to store the properties.
     100         * @param e             The extensions element to read the properties from.
     101         */
     102        private void parseKeyValueExtensions(OsmPrimitive osm, Element e) {
     103                if (e != null) {
     104                        if (osm.keys == null)
     105                                osm.keys = new HashMap<Key, String>();
     106                        for (Object o : e.getChildren("property", OSM)) {
     107                                Element child = (Element)o;
     108                                Key key = Key.get(child.getAttributeValue("name"));
     109                                osm.keys.put(key, child.getAttributeValue("value"));
     110                        }
     111                }
     112        }
     113
     114        /**
     115         * If the element is not <code>null</code>, read the data from it and put
     116         * it as the key with the name of the elements name in the given object.
     117         *
     118         * The <code>keys</code> - field of the element could be <code>null</code>,
     119         * in which case it is created first.
     120         *
     121         * @param osm     The osm primitive to put the key into.
     122         * @param e               The element to look for data.
     123         */
     124        private void parseKeyValueTag(OsmPrimitive osm, Element e) {
     125                if (e != null) {
     126                        if (osm.keys == null)
     127                                osm.keys = new HashMap<Key, String>();
     128                        osm.keys.put(Key.get(e.getName()), e.getValue());
     129                }
     130        }
     131
     132        /**
     133         * Parse the GPX linkType data information and store it as value in the
     134         * primitives <i>link</i> key. <code>osm.keys</code> may be
     135         * <code>null</code>, in which case it is created first. If
     136         * <code>e</code> is <code>null</code>, nothing happens.
     137         *
     138         * The format stored is: mimetype;url
     139         * Example: text/html;http://www.openstreetmap.org
     140         * @param osm   The osm primitive to store the data in.
     141         * @param e             The element in gpx:linkType - format.
     142         */
     143        private void parseKeyValueLink(Node osm, Element e) {
     144                if (e != null) {
     145                        if (osm.keys == null)
     146                                osm.keys = new HashMap<Key, String>();
     147                        String link = e.getChildText("type") + ";" + e.getChildText("text");
     148                        osm.keys.put(Key.get("link"), link);
     149                }
    59150        }
    60151
     
    105196                if (Main.pref.mergeNodes)
    106197                        for (Node n : data.nodes)
    107                                 if (node.equals(n))
     198                                if (node.coor.lat == n.coor.lat && node.coor.lon == n.coor.lon)
    108199                                        return n;
    109200                data.nodes.add(node);
Note: See TracChangeset for help on using the changeset viewer.