From 367f7064a037d0101dc50d47905f0180bcd9e968 Mon Sep 17 00:00:00 2001 From: Himanshu Soni Date: Sun, 5 Jul 2015 23:33:58 +0100 Subject: [PATCH] version 0.9 release --- .classpath | 6 + .gitignore | 2 + .project | 17 + src/com/hs/gpxparser/BaseGPX.java | 32 ++ src/com/hs/gpxparser/GPXConstants.java | 127 +++++ src/com/hs/gpxparser/GPXParser.java | 538 ++++++++++++++++++ src/com/hs/gpxparser/GPXWriter.java | 458 +++++++++++++++ .../extension/DummyExtensionParser.java | 23 + .../gpxparser/extension/IExtensionParser.java | 14 + src/com/hs/gpxparser/modal/Bounds.java | 50 ++ src/com/hs/gpxparser/modal/Copyright.java | 38 ++ src/com/hs/gpxparser/modal/Email.java | 36 ++ src/com/hs/gpxparser/modal/Extension.java | 69 +++ src/com/hs/gpxparser/modal/GPX.java | 175 ++++++ src/com/hs/gpxparser/modal/Link.java | 40 ++ src/com/hs/gpxparser/modal/Metadata.java | 100 ++++ src/com/hs/gpxparser/modal/Person.java | 34 ++ src/com/hs/gpxparser/modal/Point.java | 51 ++ src/com/hs/gpxparser/modal/PointSegment.java | 28 + src/com/hs/gpxparser/modal/Route.java | 186 ++++++ src/com/hs/gpxparser/modal/Track.java | 165 ++++++ src/com/hs/gpxparser/modal/TrackSegment.java | 26 + src/com/hs/gpxparser/modal/Waypoint.java | 469 +++++++++++++++ src/com/hs/gpxparser/type/Fix.java | 65 +++ 24 files changed, 2749 insertions(+) create mode 100644 .classpath create mode 100644 .project create mode 100644 src/com/hs/gpxparser/BaseGPX.java create mode 100644 src/com/hs/gpxparser/GPXConstants.java create mode 100644 src/com/hs/gpxparser/GPXParser.java create mode 100644 src/com/hs/gpxparser/GPXWriter.java create mode 100644 src/com/hs/gpxparser/extension/DummyExtensionParser.java create mode 100644 src/com/hs/gpxparser/extension/IExtensionParser.java create mode 100644 src/com/hs/gpxparser/modal/Bounds.java create mode 100644 src/com/hs/gpxparser/modal/Copyright.java create mode 100644 src/com/hs/gpxparser/modal/Email.java create mode 100644 src/com/hs/gpxparser/modal/Extension.java create mode 100644 src/com/hs/gpxparser/modal/GPX.java create mode 100644 src/com/hs/gpxparser/modal/Link.java create mode 100644 src/com/hs/gpxparser/modal/Metadata.java create mode 100644 src/com/hs/gpxparser/modal/Person.java create mode 100644 src/com/hs/gpxparser/modal/Point.java create mode 100644 src/com/hs/gpxparser/modal/PointSegment.java create mode 100644 src/com/hs/gpxparser/modal/Route.java create mode 100644 src/com/hs/gpxparser/modal/Track.java create mode 100644 src/com/hs/gpxparser/modal/TrackSegment.java create mode 100644 src/com/hs/gpxparser/modal/Waypoint.java create mode 100644 src/com/hs/gpxparser/type/Fix.java diff --git a/.classpath b/.classpath new file mode 100644 index 0000000..fceb480 --- /dev/null +++ b/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/.gitignore b/.gitignore index 32858aa..ba436db 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ +bin/ + *.class # Mobile Tools for Java (J2ME) diff --git a/.project b/.project new file mode 100644 index 0000000..571fa1c --- /dev/null +++ b/.project @@ -0,0 +1,17 @@ + + + gpx-parser + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/src/com/hs/gpxparser/BaseGPX.java b/src/com/hs/gpxparser/BaseGPX.java new file mode 100644 index 0000000..50ad8cd --- /dev/null +++ b/src/com/hs/gpxparser/BaseGPX.java @@ -0,0 +1,32 @@ +package com.hs.gpxparser; + +import java.text.SimpleDateFormat; +import java.util.ArrayList; + +import com.hs.gpxparser.extension.IExtensionParser; + +class BaseGPX { + + protected final SimpleDateFormat xmlDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'kk:mm:ss'Z'"); + protected final ArrayList extensionParsers = new ArrayList(); + + /** + * Adds a new extension parser to be used when parsing a gpx steam + * + * @param parser + * an instance of a {@link IExtensionParser} implementation + */ + public void addExtensionParser(IExtensionParser parser) { + this.extensionParsers.add(parser); + } + + /** + * Removes an extension parser previously added + * + * @param parser + * an instance of a {@link IExtensionParser} implementation + */ + public void removeExtensionParser(IExtensionParser parser) { + this.extensionParsers.remove(parser); + } +} diff --git a/src/com/hs/gpxparser/GPXConstants.java b/src/com/hs/gpxparser/GPXConstants.java new file mode 100644 index 0000000..aefb225 --- /dev/null +++ b/src/com/hs/gpxparser/GPXConstants.java @@ -0,0 +1,127 @@ +package com.hs.gpxparser; + +public interface GPXConstants { + + /* Root Node */ + String NODE_GPX = "gpx"; + + /* GPX nodes and attributes */ + String ATTR_CREATOR = "creator"; + String ATTR_VERSION = "version"; + + String NODE_METADATA = "metadata"; + String NODE_TRK = "trk"; + String NODE_RTE = "rte"; + String NODE_WPT = "wpt"; + String NODE_EXTENSIONS = "extensions"; + /* End GPX nodes and attributes */ + + /* metadata nodes and attributes */ + String NODE_NAME = "name"; + String NODE_DESC = "desc"; + String NODE_AUTHOR = "author"; + String NODE_COPYRIGHT = "copyright"; + String NODE_LINK = "link"; + String NODE_TIME = "time"; + String NODE_KEYWORDS = "keywords"; + String NODE_BOUNDS = "bounds"; + // String NODE_EXTENSIONS = "extensions"; + /* end metadata nodes and attributes */ + + /* Waypoint nodes and attributes */ + String ATTR_LAT = "lat"; + String ATTR_LON = "lon"; + + String NODE_ELE = "ele"; + // String NODE_TIME = "time"; + String NODE_MAGVAR = "magvar"; + String NODE_GEOIDHEIGHT = "geoidheight"; + // String NODE_NAME = "name"; + String NODE_CMT = "cmt"; + // String NODE_DESC = "desc"; + String NODE_SRC = "src"; + // String NODE_LINK = "link"; + String NODE_SYM = "sym"; + String NODE_TYPE = "type"; + String NODE_FIX = "fix"; + String NODE_SAT = "sat"; + String NODE_HDOP = "hdop"; + String NODE_VDOP = "vdop"; + String NODE_PDOP = "pdop"; + String NODE_AGEOFGPSDATA = "ageofdgpsdata"; + String NODE_DGPSID = "dgpsid"; + // String NODE_EXTENSIONS = "extensions"; + /* end Waypoint nodes and attributes */ + + /* Route nodes */ + // String NODE_NAME = "name"; + // String NODE_CMT = "cmt"; + // String NODE_DESC = "desc"; + // String NODE_SRC = "src"; + // String NODE_LINK = "link"; + String NODE_NUMBER = "number"; + // String NODE_TYPE = "type"; + // String NODE_EXTENSIONS = "extensions"; + String NODE_RTEPT = "rtept"; + /* end route nodes */ + + /* Track nodes and attributes */ + // String NODE_NAME = "name"; + // String NODE_CMT = "cmt"; + // String NODE_DESC = "desc"; + // String NODE_SRC = "src"; + // String NODE_LINK = "link"; + // String NODE_NUMBER = "number"; + // String NODE_TYPE = "type"; + // String NODE_EXTENSIONS = "extensions"; + String NODE_TRKSEG = "trkseg"; + /* end Track nodes and attributes */ + + /* Track segment nodes */ + String NODE_TRKPT = "trkpt"; + // String NODE_EXTENSIONS = "extensions"; + /* end Track segment nodes */ + + /* Copyright Nodes and Attributes */ + String ATTR_AUTHOR = "author"; + String NODE_YEAR = "year"; + String NODE_LICENSE = "license"; + /* End Copyright Nodes and Attributes */ + + /* Link Nodes and Attributes */ + String ATTR_HREF = "href"; + String NODE_TEXT = "text"; + // String NODE_TYPE = "type"; + /* End Link Nodes and Attributes */ + + /* Email attributes */ + String ATTR_ID = "id"; + String ATTR_DOMAIN = "domain"; + /* End Email attributes */ + + /* Person Nodes */ + // String NODE_NAME = "name"; + String NODE_EMAIL = "email"; + // String NODE_LINK = "link"; + /* End Person Nodes */ + + /* Point Nodes and Attributes */ + // String ATTR_LAT = "lat"; + // String ATTR_LON = "lon"; + + // String NODE_ELE = "ele"; + // String NODE_TIME = "time"; + /* End Point Nodes and Attributes */ + + /* Point Segment Nodes */ + String NODE_PT = "pt"; + /* End Point Segment Nodes */ + + /* Bounds Attributes */ + String ATTR_MINLAT = "minlat"; + String ATTR_MINLON = "minlon"; + String ATTR_MAXLAT = "maxlat"; + String ATTR_MAXLON = "maxlon"; + /* End Bounds Attributes */ + +} diff --git a/src/com/hs/gpxparser/GPXParser.java b/src/com/hs/gpxparser/GPXParser.java new file mode 100644 index 0000000..b6710bc --- /dev/null +++ b/src/com/hs/gpxparser/GPXParser.java @@ -0,0 +1,538 @@ +package com.hs.gpxparser; + +import java.io.InputStream; +import java.text.ParseException; +import java.util.Date; +import java.util.Iterator; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; + +import org.w3c.dom.DOMException; +import org.w3c.dom.Document; +import org.w3c.dom.NamedNodeMap; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; + +import com.hs.gpxparser.extension.IExtensionParser; +import com.hs.gpxparser.modal.Bounds; +import com.hs.gpxparser.modal.Copyright; +import com.hs.gpxparser.modal.Email; +import com.hs.gpxparser.modal.GPX; +import com.hs.gpxparser.modal.Link; +import com.hs.gpxparser.modal.Metadata; +import com.hs.gpxparser.modal.Person; +import com.hs.gpxparser.modal.Route; +import com.hs.gpxparser.modal.Track; +import com.hs.gpxparser.modal.TrackSegment; +import com.hs.gpxparser.modal.Waypoint; +import com.hs.gpxparser.type.Fix; + +/** + *

+ * This class defines methods for parsing and writing gpx files. + *

+ *
+ * Usage for parsing a gpx file into a {@link GPX} object:
+ * + * GPXParser p = new GPXParser();
+ * FileInputStream in = new FileInputStream("inFile.gpx");
+ * GPX gpx = p.parseGPX(in);
+ *

+ * Usage for writing a {@link GPX} object to a file:
+ * + * GPXParser p = new GPXParser();
+ * FileOutputStream out = new FileOutputStream("outFile.gpx");
+ * p.writeGPX(gpx, out);
+ * out.close();
+ *
+ */ +public class GPXParser extends BaseGPX { + + /** + * Parses a stream containing GPX data + * + * @param in + * the input stream + * @return {@link GPX} object containing parsed data, or null if no gpx data + * was found in the seream + * @throws Exception + */ + public GPX parseGPX(InputStream in) throws Exception { + DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); + Document doc = builder.parse(in); + Node firstChild = doc.getFirstChild(); + if (firstChild != null && GPXConstants.NODE_GPX.equals(firstChild.getNodeName())) { + GPX gpx = new GPX(); + + NamedNodeMap attrs = firstChild.getAttributes(); + + for (int idx = 0; idx < attrs.getLength(); idx++) { + Node attr = attrs.item(idx); + if (GPXConstants.ATTR_VERSION.equals(attr.getNodeName())) { + gpx.setVersion(attr.getNodeValue()); + } else if (GPXConstants.ATTR_CREATOR.equals(attr.getNodeName())) { + gpx.setCreator(attr.getNodeValue()); + } + } + + NodeList nodes = firstChild.getChildNodes(); + for (int idx = 0; idx < nodes.getLength(); idx++) { + Node currentNode = nodes.item(idx); + if (GPXConstants.NODE_METADATA.equals(currentNode.getNodeName())) { + Metadata m = this.parseMetadata(currentNode); + if (m != null) { + gpx.setMetadata(m); + } + } else if (GPXConstants.NODE_WPT.equals(currentNode.getNodeName())) { + Waypoint w = this.parseWaypoint(currentNode); + if (w != null) { + gpx.addWaypoint(w); + } + } else if (GPXConstants.NODE_RTE.equals(currentNode.getNodeName())) { + Route rte = this.parseRoute(currentNode); + if (rte != null) { + gpx.addRoute(rte); + } + } else if (GPXConstants.NODE_TRK.equals(currentNode.getNodeName())) { + Track trk = this.parseTrack(currentNode); + if (trk != null) { + gpx.addTrack(trk); + } + } else if (GPXConstants.NODE_EXTENSIONS.equals(currentNode.getNodeName())) { + for (IExtensionParser parser : this.extensionParsers) { + Object data = parser.parseExtensions(currentNode); + gpx.addExtensionData(parser.getId(), data); + } + } + } + return gpx; + } else { + throw new IllegalAccessException("Not a valid GPX file."); + } + } + + private Metadata parseMetadata(Node node) throws DOMException, ParseException { + if (node == null) { + return null; + } + + Metadata m = new Metadata(); + + NodeList childNodes = node.getChildNodes(); + if (childNodes != null) { + for (int idx = 0; idx < childNodes.getLength(); idx++) { + Node currentNode = childNodes.item(idx); + if (GPXConstants.NODE_NAME.equals(currentNode.getNodeName())) { + m.setName(this.getNodeValueAsString(currentNode)); + } else if (GPXConstants.NODE_DESC.equals(currentNode.getNodeName())) { + m.setDesc(this.getNodeValueAsString(currentNode)); + } else if (GPXConstants.NODE_AUTHOR.equals(currentNode.getNodeName())) { + Person author = this.parsePerson(currentNode); + if (author != null) { + m.setAuthor(author); + } + } else if (GPXConstants.NODE_COPYRIGHT.equals(currentNode.getNodeName())) { + Copyright copyright = this.parseCopyright(currentNode); + if (copyright != null) { + m.setCopyright(copyright); + } + } else if (GPXConstants.NODE_LINK.equals(currentNode.getNodeName())) { + Link link = this.parseLink(currentNode); + if (link != null) { + m.addLink(link); + } + } else if (GPXConstants.NODE_TIME.equals(currentNode.getNodeName())) { + m.setTime(this.getNodeValueAsDate(currentNode)); + } else if (GPXConstants.NODE_KEYWORDS.equals(currentNode.getNodeName())) { + m.setKeywords(this.getNodeValueAsString(currentNode)); + } else if (GPXConstants.NODE_BOUNDS.equals(currentNode.getNodeName())) { + Bounds bounds = this.parseBounds(currentNode); + if (bounds != null) { + m.setBounds(bounds); + } + } else if (GPXConstants.NODE_EXTENSIONS.equals(currentNode.getNodeName())) { + Iterator it = this.extensionParsers.iterator(); + while (it.hasNext()) { + IExtensionParser parser = it.next(); + Object data = parser.parseExtensions(currentNode); + m.addExtensionData(parser.getId(), data); + } + } + } + } + return m; + } + + private Person parsePerson(Node node) { + if (node == null) { + return null; + } + + Person p = new Person(); + NodeList childNodes = node.getChildNodes(); + if (childNodes != null) { + for (int idx = 0; idx < childNodes.getLength(); idx++) { + Node currentNode = childNodes.item(idx); + if (GPXConstants.NODE_NAME.equals(currentNode.getNodeName())) { + p.setName(this.getNodeValueAsString(currentNode)); + } else if (GPXConstants.NODE_EMAIL.equals(currentNode.getNodeName())) { + Email email = this.parseEmail(currentNode); + if (email != null) { + p.setEmail(email); + } + } else if (GPXConstants.NODE_LINK.equals(currentNode.getNodeName())) { + Link link = this.parseLink(currentNode); + if (link != null) { + p.setLink(link); + } + } + } + } + + return p; + } + + private Copyright parseCopyright(Node node) { + if (node == null) { + return null; + } + + Copyright c = new Copyright(null); + + NamedNodeMap attrs = node.getAttributes(); + + for (int idx = 0; idx < attrs.getLength(); idx++) { + Node attr = attrs.item(idx); + if (GPXConstants.ATTR_AUTHOR.equals(attr.getNodeName())) { + c.setAuthor(attr.getNodeValue()); + } + } + + NodeList childNodes = node.getChildNodes(); + if (childNodes != null) { + for (int idx = 0; idx < childNodes.getLength(); idx++) { + Node currentNode = childNodes.item(idx); + if (GPXConstants.NODE_YEAR.equals(currentNode.getNodeName())) { + c.setYear(this.getNodeValueAsString(currentNode)); + } else if (GPXConstants.NODE_LICENSE.equals(currentNode.getNodeName())) { + c.setLicense(this.getNodeValueAsString(currentNode)); + } + } + } + + return c; + } + + private Link parseLink(Node node) { + if (node == null) { + return null; + } + + Link l = new Link(null); + NamedNodeMap attrs = node.getAttributes(); + + for (int idx = 0; idx < attrs.getLength(); idx++) { + Node attr = attrs.item(idx); + if (GPXConstants.ATTR_HREF.equals(attr.getNodeName())) { + l.setHref(attr.getNodeValue()); + } + } + + NodeList childNodes = node.getChildNodes(); + if (childNodes != null) { + for (int idx = 0; idx < childNodes.getLength(); idx++) { + Node currentNode = childNodes.item(idx); + if (GPXConstants.NODE_TEXT.equals(currentNode.getNodeName())) { + l.setText(this.getNodeValueAsString(currentNode)); + } else if (GPXConstants.NODE_TYPE.equals(currentNode.getNodeName())) { + l.setType(this.getNodeValueAsString(currentNode)); + } + } + } + + return l; + } + + private Bounds parseBounds(Node node) { + if (node == null) { + return null; + } + + Bounds b = new Bounds(0, 0, 0, 0); + + NamedNodeMap attrs = node.getAttributes(); + + for (int idx = 0; idx < attrs.getLength(); idx++) { + Node attr = attrs.item(idx); + if (GPXConstants.ATTR_MINLAT.equals(attr.getNodeName())) { + b.setMinLat(Double.parseDouble(attr.getNodeValue())); + } else if (GPXConstants.ATTR_MINLON.equals(attr.getNodeName())) { + b.setMinLon(Double.parseDouble(attr.getNodeValue())); + } else if (GPXConstants.ATTR_MAXLAT.equals(attr.getNodeName())) { + b.setMaxLat(Double.parseDouble(attr.getNodeValue())); + } else if (GPXConstants.ATTR_MAXLON.equals(attr.getNodeName())) { + b.setMaxLon(Double.parseDouble(attr.getNodeValue())); + } + } + + return b; + } + + private Email parseEmail(Node node) { + if (node == null) { + return null; + } + + Email e = new Email(null, null); + + NamedNodeMap attrs = node.getAttributes(); + for (int idx = 0; idx < attrs.getLength(); idx++) { + Node attr = attrs.item(idx); + if (GPXConstants.ATTR_ID.equals(attr.getNodeName())) { + e.setId(attr.getNodeValue()); + } else if (GPXConstants.ATTR_DOMAIN.equals(attr.getNodeName())) { + e.setDomain(attr.getNodeValue()); + } + } + + return e; + } + + private Waypoint parseWaypoint(Node node) throws Exception { + if (node == null) { + return null; + } + Waypoint w = new Waypoint(0, 0); + NamedNodeMap attrs = node.getAttributes(); + // check for lat attribute + Node latNode = attrs.getNamedItem(GPXConstants.ATTR_LAT); + if (latNode != null) { + Double latVal = null; + latVal = Double.parseDouble(latNode.getNodeValue()); + w.setLatitude(latVal); + } else { + throw new Exception("no lat value in waypoint data."); + } + // check for lon attribute + Node lonNode = attrs.getNamedItem(GPXConstants.ATTR_LON); + if (lonNode != null) { + Double lonVal = Double.parseDouble(lonNode.getNodeValue()); + w.setLongitude(lonVal); + } else { + throw new Exception("no lon value in waypoint data."); + } + + NodeList childNodes = node.getChildNodes(); + if (childNodes != null) { + for (int idx = 0; idx < childNodes.getLength(); idx++) { + Node currentNode = childNodes.item(idx); + if (GPXConstants.NODE_ELE.equals(currentNode.getNodeName())) { + w.setElevation(this.getNodeValueAsDouble(currentNode)); + } else if (GPXConstants.NODE_TIME.equals(currentNode.getNodeName())) { + w.setTime(this.getNodeValueAsDate(currentNode)); + } else if (GPXConstants.NODE_MAGVAR.equals(currentNode.getNodeName())) { + w.setMagneticVariation(this.getNodeValueAsDouble(currentNode)); + } else if (GPXConstants.NODE_GEOIDHEIGHT.equals(currentNode.getNodeName())) { + w.setGeoIdHeight(this.getNodeValueAsDouble(currentNode)); + } else if (GPXConstants.NODE_NAME.equals(currentNode.getNodeName())) { + w.setName(this.getNodeValueAsString(currentNode)); + } else if (GPXConstants.NODE_CMT.equals(currentNode.getNodeName())) { + w.setComment(this.getNodeValueAsString(currentNode)); + } else if (GPXConstants.NODE_DESC.equals(currentNode.getNodeName())) { + w.setDescription(this.getNodeValueAsString(currentNode)); + } else if (GPXConstants.NODE_SRC.equals(currentNode.getNodeName())) { + w.setSrc(this.getNodeValueAsString(currentNode)); + } else if (GPXConstants.NODE_LINK.equals(currentNode.getNodeName())) { + Link link = this.parseLink(currentNode); + if (link != null) { + w.addLink(link); + } + } else if (GPXConstants.NODE_SYM.equals(currentNode.getNodeName())) { + w.setSym(this.getNodeValueAsString(currentNode)); + } else if (GPXConstants.NODE_TYPE.equals(currentNode.getNodeName())) { + w.setType(this.getNodeValueAsString(currentNode)); + } else if (GPXConstants.NODE_FIX.equals(currentNode.getNodeName())) { + w.setFix(this.getNodeValueAsFixType(currentNode)); + } else if (GPXConstants.NODE_SAT.equals(currentNode.getNodeName())) { + w.setSat(this.getNodeValueAsInteger(currentNode)); + } else if (GPXConstants.NODE_HDOP.equals(currentNode.getNodeName())) { + w.setHdop(this.getNodeValueAsDouble(currentNode)); + } else if (GPXConstants.NODE_VDOP.equals(currentNode.getNodeName())) { + w.setVdop(this.getNodeValueAsDouble(currentNode)); + } else if (GPXConstants.NODE_PDOP.equals(currentNode.getNodeName())) { + w.setPdop(this.getNodeValueAsDouble(currentNode)); + } else if (GPXConstants.NODE_AGEOFGPSDATA.equals(currentNode.getNodeName())) { + w.setAgeOfGPSData(this.getNodeValueAsDouble(currentNode)); + } else if (GPXConstants.NODE_DGPSID.equals(currentNode.getNodeName())) { + w.setdGpsStationId(this.getNodeValueAsInteger(currentNode)); + } else if (GPXConstants.NODE_EXTENSIONS.equals(currentNode.getNodeName())) { + Iterator it = this.extensionParsers.iterator(); + while (it.hasNext()) { + IExtensionParser parser = it.next(); + Object data = parser.parseExtensions(currentNode); + w.addExtensionData(parser.getId(), data); + } + } + } + } + + return w; + } + + private Route parseRoute(Node node) throws Exception { + if (node == null) { + return null; + } + Route rte = new Route(); + NodeList nodes = node.getChildNodes(); + if (nodes != null) { + for (int idx = 0; idx < nodes.getLength(); idx++) { + Node currentNode = nodes.item(idx); + if (GPXConstants.NODE_NAME.equals(currentNode.getNodeName())) { + rte.setName(this.getNodeValueAsString(currentNode)); + } else if (GPXConstants.NODE_CMT.equals(currentNode.getNodeName())) { + rte.setComment(this.getNodeValueAsString(currentNode)); + } else if (GPXConstants.NODE_DESC.equals(currentNode.getNodeName())) { + rte.setDescription(this.getNodeValueAsString(currentNode)); + } else if (GPXConstants.NODE_SRC.equals(currentNode.getNodeName())) { + rte.setSrc(this.getNodeValueAsString(currentNode)); + } else if (GPXConstants.NODE_LINK.equals(currentNode.getNodeName())) { + Link link = this.parseLink(currentNode); + if (link != null) { + rte.addLink(link); + } + } else if (GPXConstants.NODE_NUMBER.equals(currentNode.getNodeName())) { + rte.setNumber(this.getNodeValueAsInteger(currentNode)); + } else if (GPXConstants.NODE_TYPE.equals(currentNode.getNodeName())) { + rte.setType(this.getNodeValueAsString(currentNode)); + } else if (GPXConstants.NODE_EXTENSIONS.equals(currentNode.getNodeName())) { + Iterator it = this.extensionParsers.iterator(); + while (it.hasNext()) { + while (it.hasNext()) { + IExtensionParser parser = it.next(); + Object data = parser.parseExtensions(currentNode); + rte.addExtensionData(parser.getId(), data); + } + } + } else if (GPXConstants.NODE_RTEPT.equals(currentNode.getNodeName())) { + Waypoint wp = this.parseWaypoint(currentNode); + if (wp != null) { + rte.addRoutePoint(wp); + } + } + } + } + + return rte; + } + + private Track parseTrack(Node node) throws Exception { + if (node == null) { + return null; + } + Track trk = new Track(); + NodeList nodes = node.getChildNodes(); + if (nodes != null) { + for (int idx = 0; idx < nodes.getLength(); idx++) { + Node currentNode = nodes.item(idx); + if (GPXConstants.NODE_NAME.equals(currentNode.getNodeName())) { + trk.setName(this.getNodeValueAsString(currentNode)); + } else if (GPXConstants.NODE_CMT.equals(currentNode.getNodeName())) { + trk.setComment(this.getNodeValueAsString(currentNode)); + } else if (GPXConstants.NODE_DESC.equals(currentNode.getNodeName())) { + trk.setDescription(this.getNodeValueAsString(currentNode)); + } else if (GPXConstants.NODE_SRC.equals(currentNode.getNodeName())) { + trk.setSrc(this.getNodeValueAsString(currentNode)); + } else if (GPXConstants.NODE_LINK.equals(currentNode.getNodeName())) { + Link link = this.parseLink(currentNode); + if (link != null) { + trk.addLink(link); + } + } else if (GPXConstants.NODE_NUMBER.equals(currentNode.getNodeName())) { + trk.setNumber(this.getNodeValueAsInteger(currentNode)); + } else if (GPXConstants.NODE_TYPE.equals(currentNode.getNodeName())) { + trk.setType(this.getNodeValueAsString(currentNode)); + } else if (GPXConstants.NODE_EXTENSIONS.equals(currentNode.getNodeName())) { + Iterator it = this.extensionParsers.iterator(); + while (it.hasNext()) { + while (it.hasNext()) { + IExtensionParser parser = it.next(); + Object data = parser.parseExtensions(currentNode); + trk.addExtensionData(parser.getId(), data); + } + } + } else if (GPXConstants.NODE_TRKSEG.equals(currentNode.getNodeName())) { + TrackSegment trackSeg = this.parseTrackSegment(currentNode); + if (trackSeg != null) { + trk.addTrackSegment(trackSeg); + } + } + } + } + + return trk; + } + + private TrackSegment parseTrackSegment(Node node) throws Exception { + if (node == null) { + return null; + } + + TrackSegment ts = new TrackSegment(); + NodeList childNodes = node.getChildNodes(); + if (childNodes != null) { + for (int idx = 0; idx < childNodes.getLength(); idx++) { + Node currentNode = childNodes.item(idx); + if (GPXConstants.NODE_TRKPT.equals(currentNode.getNodeName())) { + Waypoint w = this.parseWaypoint(currentNode); + if (w != null) { + ts.addWaypoint(w); + } + } else if (GPXConstants.NODE_EXTENSIONS.equals(currentNode.getNodeName())) { + Iterator it = this.extensionParsers.iterator(); + while (it.hasNext()) { + while (it.hasNext()) { + IExtensionParser parser = it.next(); + Object data = parser.parseExtensions(currentNode); + ts.addExtensionData(parser.getId(), data); + } + } + } + } + } + + return ts; + } + + private Date getNodeValueAsDate(Node node) throws DOMException, ParseException { + Date val = null; + val = xmlDateFormat.parse(node.getFirstChild().getNodeValue()); + return val; + } + + private Double getNodeValueAsDouble(Node node) { + return Double.parseDouble(node.getFirstChild().getNodeValue()); + } + + private Fix getNodeValueAsFixType(Node node) { + return Fix.returnType(node.getFirstChild().getNodeValue()); + } + + private Integer getNodeValueAsInteger(Node node) { + return Integer.parseInt(node.getFirstChild().getNodeValue()); + } + + private String getNodeValueAsString(Node node) { + if (node == null) { + return null; + } + + Node child = node.getFirstChild(); + if (child == null) { + return null; + } + return child.getNodeValue(); + } + +} \ No newline at end of file diff --git a/src/com/hs/gpxparser/GPXWriter.java b/src/com/hs/gpxparser/GPXWriter.java new file mode 100644 index 0000000..1745495 --- /dev/null +++ b/src/com/hs/gpxparser/GPXWriter.java @@ -0,0 +1,458 @@ +package com.hs.gpxparser; + +import java.io.OutputStream; +import java.util.Iterator; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.transform.Transformer; +import javax.xml.transform.TransformerException; +import javax.xml.transform.TransformerFactory; +import javax.xml.transform.dom.DOMSource; +import javax.xml.transform.stream.StreamResult; + +import org.w3c.dom.Document; +import org.w3c.dom.NamedNodeMap; +import org.w3c.dom.Node; + +import com.hs.gpxparser.extension.IExtensionParser; +import com.hs.gpxparser.modal.Bounds; +import com.hs.gpxparser.modal.Copyright; +import com.hs.gpxparser.modal.Email; +import com.hs.gpxparser.modal.Extension; +import com.hs.gpxparser.modal.GPX; +import com.hs.gpxparser.modal.Link; +import com.hs.gpxparser.modal.Metadata; +import com.hs.gpxparser.modal.Person; +import com.hs.gpxparser.modal.Route; +import com.hs.gpxparser.modal.Track; +import com.hs.gpxparser.modal.TrackSegment; +import com.hs.gpxparser.modal.Waypoint; + +public class GPXWriter extends BaseGPX { + + public void writeGPX(GPX gpx, OutputStream out) throws ParserConfigurationException, TransformerException { + DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); + Document doc = builder.newDocument(); + Node gpxNode = doc.createElement(GPXConstants.NODE_GPX); + + NamedNodeMap attrs = gpxNode.getAttributes(); + if (gpx.getVersion() != null) { + Node verNode = doc.createAttribute(GPXConstants.ATTR_VERSION); + verNode.setNodeValue(gpx.getVersion()); + attrs.setNamedItem(verNode); + } + if (gpx.getCreator() != null) { + Node creatorNode = doc.createAttribute(GPXConstants.ATTR_CREATOR); + creatorNode.setNodeValue(gpx.getCreator()); + attrs.setNamedItem(creatorNode); + } + if (gpx.getMetadata() != null) { + this.addMetadataToNode(gpx.getMetadata(), gpxNode, doc); + } + if (gpx.getWaypoints() != null) { + for (Waypoint wp : gpx.getWaypoints()) { + this.addWaypointToNode(GPXConstants.NODE_WPT, wp, gpxNode, doc); + } + } + if (gpx.getRoutes() != null) { + for (Route route : gpx.getRoutes()) { + this.addRouteToNode(route, gpxNode, doc); + } + } + + if (gpx.getTracks() != null) { + for (Track track : gpx.getTracks()) { + this.addTrackToNode(track, gpxNode, doc); + } + } + + doc.appendChild(gpxNode); + + // Use a Transformer for output + TransformerFactory tFactory = TransformerFactory.newInstance(); + Transformer transformer = tFactory.newTransformer(); + + DOMSource source = new DOMSource(doc); + StreamResult result = new StreamResult(out); + transformer.transform(source, result); + } + + private void addTrackToNode(Track trk, Node n, Document doc) { + Node trkNode = doc.createElement(GPXConstants.NODE_TRK); + + if (trk.getName() != null) { + Node node = doc.createElement(GPXConstants.NODE_NAME); + node.appendChild(doc.createTextNode(trk.getName())); + trkNode.appendChild(node); + } + if (trk.getComment() != null) { + Node node = doc.createElement(GPXConstants.NODE_CMT); + node.appendChild(doc.createTextNode(trk.getComment())); + trkNode.appendChild(node); + } + if (trk.getDescription() != null) { + Node node = doc.createElement(GPXConstants.NODE_DESC); + node.appendChild(doc.createTextNode(trk.getDescription())); + trkNode.appendChild(node); + } + if (trk.getSrc() != null) { + Node node = doc.createElement(GPXConstants.NODE_SRC); + node.appendChild(doc.createTextNode(trk.getSrc())); + trkNode.appendChild(node); + } + if (trk.getLinks() != null) { + for (Link l : trk.getLinks()) { + this.addLinkToNode(l, trkNode, doc); + } + } + if (trk.getNumber() != null) { + Node node = doc.createElement(GPXConstants.NODE_NUMBER); + node.appendChild(doc.createTextNode(trk.getNumber().toString())); + trkNode.appendChild(node); + } + if (trk.getType() != null) { + Node node = doc.createElement(GPXConstants.NODE_TYPE); + node.appendChild(doc.createTextNode(trk.getType())); + trkNode.appendChild(node); + } + this.addExtensionToNode(trk, trkNode, doc); + + if (trk.getTrackSegments() != null) { + for (TrackSegment ts : trk.getTrackSegments()) { + this.addTrackSegmentToNode(ts, trkNode, doc); + } + } + n.appendChild(trkNode); + } + + private void addTrackSegmentToNode(TrackSegment ts, Node n, Document doc) { + Node tsNode = doc.createElement(GPXConstants.NODE_TRKSEG); + + for (Waypoint wp : ts.getWaypoints()) { + this.addWaypointToNode(GPXConstants.NODE_TRKPT, wp, tsNode, doc); + } + this.addExtensionToNode(ts, tsNode, doc); + + n.appendChild(tsNode); + + } + + private void addRouteToNode(Route rte, Node gpxNode, Document doc) { + Node rteNode = doc.createElement(GPXConstants.NODE_RTE); + + if (rte.getName() != null) { + Node node = doc.createElement(GPXConstants.NODE_NAME); + node.appendChild(doc.createTextNode(rte.getName())); + rteNode.appendChild(node); + } + if (rte.getComment() != null) { + Node node = doc.createElement(GPXConstants.NODE_CMT); + node.appendChild(doc.createTextNode(rte.getComment())); + rteNode.appendChild(node); + } + if (rte.getDescription() != null) { + Node node = doc.createElement(GPXConstants.NODE_DESC); + node.appendChild(doc.createTextNode(rte.getDescription())); + rteNode.appendChild(node); + } + if (rte.getSrc() != null) { + Node node = doc.createElement(GPXConstants.NODE_SRC); + node.appendChild(doc.createTextNode(rte.getSrc())); + rteNode.appendChild(node); + } + if (rte.getLinks() != null) { + for (Link l : rte.getLinks()) { + this.addLinkToNode(l, rteNode, doc); + } + } + if (rte.getNumber() != null) { + Node node = doc.createElement(GPXConstants.NODE_NUMBER); + node.appendChild(doc.createTextNode(rte.getNumber().toString())); + rteNode.appendChild(node); + } + if (rte.getType() != null) { + Node node = doc.createElement(GPXConstants.NODE_TYPE); + node.appendChild(doc.createTextNode(rte.getType())); + rteNode.appendChild(node); + } + this.addExtensionToNode(rte, rteNode, doc); + + if (rte.getRoutePoints() != null) { + Iterator it = rte.getRoutePoints().iterator(); + while (it.hasNext()) { + this.addWaypointToNode(GPXConstants.NODE_RTEPT, it.next(), rteNode, doc); + } + } + gpxNode.appendChild(rteNode); + } + + private void addWaypointToNode(String tag, Waypoint wpt, Node n, Document doc) { + Node wptNode = doc.createElement(tag); + NamedNodeMap attrs = wptNode.getAttributes(); + if (wpt.getLatitude() != 0) { + Node latNode = doc.createAttribute(GPXConstants.ATTR_LAT); + latNode.setNodeValue(String.valueOf(wpt.getLatitude())); + attrs.setNamedItem(latNode); + } + if (wpt.getLongitude() != 0) { + Node longNode = doc.createAttribute(GPXConstants.ATTR_LON); + longNode.setNodeValue(String.valueOf(wpt.getLongitude())); + attrs.setNamedItem(longNode); + } + if (wpt.getElevation() != 0) { + Node node = doc.createElement(GPXConstants.NODE_ELE); + node.appendChild(doc.createTextNode(String.valueOf(wpt.getElevation()))); + wptNode.appendChild(node); + } + if (wpt.getTime() != null) { + Node node = doc.createElement(GPXConstants.NODE_TIME); + node.appendChild(doc.createTextNode(xmlDateFormat.format(wpt.getTime()))); + wptNode.appendChild(node); + } + if (wpt.getMagneticVariation() != 0) { + Node node = doc.createElement(GPXConstants.NODE_MAGVAR); + node.appendChild(doc.createTextNode(String.valueOf(wpt.getMagneticVariation()))); + wptNode.appendChild(node); + } + if (wpt.getGeoIdHeight() != 0) { + Node node = doc.createElement(GPXConstants.NODE_GEOIDHEIGHT); + node.appendChild(doc.createTextNode(String.valueOf(wpt.getGeoIdHeight()))); + wptNode.appendChild(node); + } + if (wpt.getName() != null) { + Node node = doc.createElement(GPXConstants.NODE_NAME); + node.appendChild(doc.createTextNode(wpt.getName())); + wptNode.appendChild(node); + } + if (wpt.getComment() != null) { + Node node = doc.createElement(GPXConstants.NODE_CMT); + node.appendChild(doc.createTextNode(wpt.getComment())); + wptNode.appendChild(node); + } + if (wpt.getDescription() != null) { + Node node = doc.createElement(GPXConstants.NODE_DESC); + node.appendChild(doc.createTextNode(wpt.getDescription())); + wptNode.appendChild(node); + } + if (wpt.getSrc() != null) { + Node node = doc.createElement(GPXConstants.NODE_SRC); + node.appendChild(doc.createTextNode(wpt.getSrc())); + wptNode.appendChild(node); + } + if (wpt.getLinks() != null) { + for (Link l : wpt.getLinks()) { + this.addLinkToNode(l, wptNode, doc); + } + } + if (wpt.getSym() != null) { + Node node = doc.createElement(GPXConstants.NODE_SYM); + node.appendChild(doc.createTextNode(wpt.getSym())); + wptNode.appendChild(node); + } + if (wpt.getType() != null) { + Node node = doc.createElement(GPXConstants.NODE_TYPE); + node.appendChild(doc.createTextNode(wpt.getType())); + wptNode.appendChild(node); + } + if (wpt.getFix() != null) { + Node node = doc.createElement(GPXConstants.NODE_FIX); + node.appendChild(doc.createTextNode(wpt.getFix().toString())); + wptNode.appendChild(node); + } + if (wpt.getSat() != 0) { + Node node = doc.createElement(GPXConstants.NODE_SAT); + node.appendChild(doc.createTextNode(String.valueOf(wpt.getSat()))); + wptNode.appendChild(node); + } + if (wpt.getHdop() != 0) { + Node node = doc.createElement(GPXConstants.NODE_HDOP); + node.appendChild(doc.createTextNode(String.valueOf(wpt.getHdop()))); + wptNode.appendChild(node); + } + if (wpt.getVdop() != 0) { + Node node = doc.createElement(GPXConstants.NODE_VDOP); + node.appendChild(doc.createTextNode(String.valueOf(wpt.getVdop()))); + wptNode.appendChild(node); + } + if (wpt.getPdop() != 0) { + Node node = doc.createElement(GPXConstants.NODE_PDOP); + node.appendChild(doc.createTextNode(String.valueOf(wpt.getPdop()))); + wptNode.appendChild(node); + } + if (wpt.getAgeOfGPSData() != 0) { + Node node = doc.createElement(GPXConstants.NODE_AGEOFGPSDATA); + node.appendChild(doc.createTextNode(String.valueOf(wpt.getAgeOfGPSData()))); + wptNode.appendChild(node); + } + if (wpt.getdGpsStationId() != 0) { + Node node = doc.createElement(GPXConstants.NODE_DGPSID); + node.appendChild(doc.createTextNode(String.valueOf(wpt.getdGpsStationId()))); + wptNode.appendChild(node); + } + this.addExtensionToNode(wpt, wptNode, doc); + n.appendChild(wptNode); + } + + private void addMetadataToNode(Metadata m, Node n, Document doc) { + Node matadataNode = doc.createElement(GPXConstants.NODE_METADATA); + + if (m.getName() != null) { + Node node = doc.createElement(GPXConstants.NODE_NAME); + node.appendChild(doc.createTextNode(m.getName())); + matadataNode.appendChild(node); + } + + if (m.getDesc() != null) { + Node node = doc.createElement(GPXConstants.NODE_DESC); + node.appendChild(doc.createTextNode(m.getDesc())); + matadataNode.appendChild(node); + } + + if (m.getAuthor() != null) { + this.addAuthorToNode(m.getAuthor(), matadataNode, doc); + } + + if (m.getCopyright() != null) { + this.addCopyrightToNode(m.getCopyright(), matadataNode, doc); + } + + if (m.getLinks() != null) { + for (Link l : m.getLinks()) { + this.addLinkToNode(l, matadataNode, doc); + } + } + + if (m.getTime() != null) { + Node node = doc.createElement(GPXConstants.NODE_TIME); + node.appendChild(doc.createTextNode(xmlDateFormat.format(m.getTime()))); + matadataNode.appendChild(node); + } + + if (m.getKeywords() != null) { + Node node = doc.createElement(GPXConstants.NODE_KEYWORDS); + node.appendChild(doc.createTextNode(m.getKeywords())); + matadataNode.appendChild(node); + } + + if (m.getBounds() != null) { + this.addBoundsToNode(m.getBounds(), matadataNode, doc); + } + + this.addExtensionToNode(m, matadataNode, doc); + + n.appendChild(matadataNode); + } + + private void addBoundsToNode(Bounds bounds, Node n, Document doc) { + Node bonundsNode = doc.createElement(GPXConstants.NODE_BOUNDS); + NamedNodeMap attributes = bonundsNode.getAttributes(); + + Node node = doc.createAttribute(GPXConstants.ATTR_MINLAT); + node.setNodeValue(String.valueOf(bounds.getMinLat())); + attributes.setNamedItem(node); + + node = doc.createAttribute(GPXConstants.ATTR_MINLON); + node.setNodeValue(String.valueOf(bounds.getMinLon())); + attributes.setNamedItem(node); + + node = doc.createAttribute(GPXConstants.ATTR_MAXLAT); + node.setNodeValue(String.valueOf(bounds.getMaxLat())); + attributes.setNamedItem(node); + + node = doc.createAttribute(GPXConstants.ATTR_MAXLON); + node.setNodeValue(String.valueOf(bounds.getMaxLon())); + attributes.setNamedItem(node); + } + + private void addCopyrightToNode(Copyright copyright, Node n, Document doc) { + Node copyrightNode = doc.createElement(GPXConstants.NODE_COPYRIGHT); + NamedNodeMap attributes = copyrightNode.getAttributes(); + if (copyright.getAuthor() != null) { + Node node = doc.createAttribute(GPXConstants.ATTR_AUTHOR); + node.setNodeValue(copyright.getAuthor()); + attributes.setNamedItem(node); + } + + if (copyright.getYear() != null) { + Node node = doc.createElement(GPXConstants.NODE_YEAR); + node.appendChild(doc.createTextNode(copyright.getYear())); + n.appendChild(node); + } + if (copyright.getLicense() != null) { + Node node = doc.createElement(GPXConstants.NODE_LICENSE); + node.appendChild(doc.createTextNode(copyright.getLicense())); + n.appendChild(node); + } + + n.appendChild(copyrightNode); + + } + + private void addAuthorToNode(Person author, Node n, Document doc) { + Node authorNode = doc.createElement(GPXConstants.NODE_AUTHOR); + if (author.getName() != null) { + Node node = doc.createElement(GPXConstants.NODE_NAME); + node.appendChild(doc.createTextNode(author.getName())); + n.appendChild(node); + } + if (author.getEmail() != null) { + this.addEmailToNode(author.getEmail(), authorNode, doc); + } + + if (author.getLink() != null) { + this.addLinkToNode(author.getLink(), authorNode, doc); + } + n.appendChild(authorNode); + } + + private void addLinkToNode(Link link, Node n, Document doc) { + Node linkNode = doc.createElement(GPXConstants.NODE_LINK); + NamedNodeMap attributes = linkNode.getAttributes(); + if (link.getHref() != null) { + Node node = doc.createAttribute(GPXConstants.ATTR_HREF); + node.setNodeValue(link.getHref()); + attributes.setNamedItem(node); + } + + if (link.getText() != null) { + Node node = doc.createElement(GPXConstants.NODE_TEXT); + node.appendChild(doc.createTextNode(link.getText())); + n.appendChild(node); + } + if (link.getType() != null) { + Node node = doc.createElement(GPXConstants.NODE_TYPE); + node.appendChild(doc.createTextNode(link.getType())); + n.appendChild(node); + } + + n.appendChild(linkNode); + } + + private void addEmailToNode(Email email, Node n, Document doc) { + Node emailNode = doc.createElement(GPXConstants.NODE_EMAIL); + NamedNodeMap attributes = emailNode.getAttributes(); + if (email.getId() != null) { + Node node = doc.createAttribute(GPXConstants.ATTR_ID); + node.setNodeValue(email.getId()); + attributes.setNamedItem(node); + } + if (email.getDomain() != null) { + Node node = doc.createAttribute(GPXConstants.ATTR_DOMAIN); + node.setNodeValue(email.getDomain()); + attributes.setNamedItem(node); + } + + } + + private void addExtensionToNode(Extension e, Node n, Document doc) { + if (e.getExtensionsParsed() > 0) { + Node node = doc.createElement(GPXConstants.NODE_EXTENSIONS); + for (IExtensionParser parser : this.extensionParsers) { + parser.writeExtensions(node, doc); + } + n.appendChild(node); + } + } +} diff --git a/src/com/hs/gpxparser/extension/DummyExtensionParser.java b/src/com/hs/gpxparser/extension/DummyExtensionParser.java new file mode 100644 index 0000000..3b3f582 --- /dev/null +++ b/src/com/hs/gpxparser/extension/DummyExtensionParser.java @@ -0,0 +1,23 @@ +package com.hs.gpxparser.extension; + +import org.w3c.dom.Document; +import org.w3c.dom.Node; + +public class DummyExtensionParser implements IExtensionParser { + + @Override + public String getId() { + return "Basic Extension Parser"; + } + + @Override + public Object parseExtensions(Node node) { + // TODO get your object from the node + return null; + } + + @Override + public void writeExtensions(Node node, Document doc) { + // TODO write your object to node + } +} diff --git a/src/com/hs/gpxparser/extension/IExtensionParser.java b/src/com/hs/gpxparser/extension/IExtensionParser.java new file mode 100644 index 0000000..3282a6c --- /dev/null +++ b/src/com/hs/gpxparser/extension/IExtensionParser.java @@ -0,0 +1,14 @@ +package com.hs.gpxparser.extension; + +import org.w3c.dom.Document; +import org.w3c.dom.Node; + +public interface IExtensionParser { + + public String getId(); + + public Object parseExtensions(Node node); + + public void writeExtensions(Node node, Document doc); + +} diff --git a/src/com/hs/gpxparser/modal/Bounds.java b/src/com/hs/gpxparser/modal/Bounds.java new file mode 100644 index 0000000..ff83933 --- /dev/null +++ b/src/com/hs/gpxparser/modal/Bounds.java @@ -0,0 +1,50 @@ +package com.hs.gpxparser.modal; + +/** + * Created by Himanshu on 7/5/2015. + */ +public class Bounds { + private double minLat; + private double minLon; + private double maxLat; + private double maxLon; + + public Bounds(double minlat, double maxlat, double minlon, double maxlon) { + this.minLat = minlat; + this.maxLat = maxlat; + this.minLon = minlon; + this.maxLon = maxlon; + } + + public double getMinLat() { + return minLat; + } + + public void setMinLat(double minLat) { + this.minLat = minLat; + } + + public double getMinLon() { + return minLon; + } + + public void setMinLon(double minLon) { + this.minLon = minLon; + } + + public double getMaxLat() { + return maxLat; + } + + public void setMaxLat(double maxLat) { + this.maxLat = maxLat; + } + + public double getMaxLon() { + return maxLon; + } + + public void setMaxLon(double maxLon) { + this.maxLon = maxLon; + } +} diff --git a/src/com/hs/gpxparser/modal/Copyright.java b/src/com/hs/gpxparser/modal/Copyright.java new file mode 100644 index 0000000..e47988e --- /dev/null +++ b/src/com/hs/gpxparser/modal/Copyright.java @@ -0,0 +1,38 @@ +package com.hs.gpxparser.modal; + +/** + * Created by Himanshu on 7/5/2015. + */ +public class Copyright { + private String year; + private String license; + private String author; + + public Copyright(String author) { + this.author = author; + } + + public String getYear() { + return year; + } + + public void setYear(String year) { + this.year = year; + } + + public String getLicense() { + return license; + } + + public void setLicense(String license) { + this.license = license; + } + + public String getAuthor() { + return author; + } + + public void setAuthor(String author) { + this.author = author; + } +} diff --git a/src/com/hs/gpxparser/modal/Email.java b/src/com/hs/gpxparser/modal/Email.java new file mode 100644 index 0000000..936e44a --- /dev/null +++ b/src/com/hs/gpxparser/modal/Email.java @@ -0,0 +1,36 @@ +package com.hs.gpxparser.modal; + +/** + * Created by Himanshu on 7/5/2015. + */ +public class Email { + private String id; + private String domain; + + public Email(String id, String domain) { + this.id = id; + this.domain = domain; + } + + public Email(String emailId) { + String[] split = emailId.split("@"); + this.id = split[0]; + this.domain = split[1]; + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getDomain() { + return domain; + } + + public void setDomain(String domain) { + this.domain = domain; + } +} diff --git a/src/com/hs/gpxparser/modal/Extension.java b/src/com/hs/gpxparser/modal/Extension.java new file mode 100644 index 0000000..e23550b --- /dev/null +++ b/src/com/hs/gpxparser/modal/Extension.java @@ -0,0 +1,69 @@ + +package com.hs.gpxparser.modal; + +import java.util.HashMap; + +/** + * This class holds generic extension information from any node that can have extensions defined. + *
+ *

Any custom extension parser used when parsing a gpx file will return a custom object + * instance that will be kept in properties defined in this class.

+ *
+ *

Multiple extension parsers can be used when parsing. Every extension parser defines + * an unique id that will be used as a HashMap key for the parsed object values.

+ */ +public class Extension { + protected HashMap extensionData; + + public HashMap getExtensionData() { + return extensionData; + } + + /** + * Setter for extension HashMap. + * @param extensionData - A HashMap with extensions parser ids as keys and parsed objects as values. + */ + public void setExtensionData(HashMap extensionData) { + this.extensionData = extensionData; + } + + + /** + * Adds a new parsed extension object into the extension data with the key set by parserId. + * + * @param parserId a unique key representing the id of he extension parser used. + * @param data an object holding the parsed information. This can be any object type and it is + * the extension parser's job to set it properly. + */ + public void addExtensionData(String parserId, Object data) { + if(extensionData == null) { + extensionData = new HashMap(); + } + extensionData.put(parserId, data); + } + + /** + * Returns the extension data parsed by the extension parser with id parserId + * @param parserId a String representing the id of an extension parser + * @return the extension data parsed by the extension parser with id parserId + */ + public Object getExtensionData(String parserId) { + if(extensionData != null) { + return extensionData.get(parserId); + } else { + return null; + } + } + + /** + * Returns the number of extension data objects that are currently set. + * @return the number of extension data objects that are currently set. + */ + public int getExtensionsParsed() { + if(extensionData != null) { + return extensionData.size(); + } else { + return 0; + } + } +} diff --git a/src/com/hs/gpxparser/modal/GPX.java b/src/com/hs/gpxparser/modal/GPX.java new file mode 100644 index 0000000..0e6569c --- /dev/null +++ b/src/com/hs/gpxparser/modal/GPX.java @@ -0,0 +1,175 @@ +package com.hs.gpxparser.modal; + +import java.util.HashSet; + +/** + * This class holds gpx information from a <gpx> node.
+ *

+ * GPX specification for this tag: + *

+ * + * <gpx version="1.1" creator=""xsd:string [1]">
+ *    <metadata> xsd:string </metadata> [0..1]
+ *    <wpt> xsd:string </wpt> [0..1]
+ *    <rte> xsd:string </rte> [0..1]
+ *    <trk> xsd:string </trk> [0..1]
+ *    <extensions> extensionsType </extensions> [0..1]
+ * </gpx>
+ *
+ */ +public class GPX extends Extension { + + // Attributes + private String creator; + private String version = "1.1"; + + // Nodes + private Metadata metadata; + private HashSet routes; + private HashSet tracks; + private HashSet waypoints; + + public GPX() { + this.waypoints = new HashSet(); + this.tracks = new HashSet(); + this.routes = new HashSet(); + } + + /** + * Adds a new Route to a gpx object + * + * @param route + * a {@link Route} + */ + public void addRoute(Route route) { + if (this.routes == null) { + this.routes = new HashSet(); + } + this.routes.add(route); + } + + /** + * Adds a new track to a gpx object + * + * @param track + * a {@link Track} + */ + public void addTrack(Track track) { + if (this.tracks == null) { + this.tracks = new HashSet(); + } + this.tracks.add(track); + } + + /** + * Adds a new waypoint to a gpx object + * + * @param waypoint + * a {@link Waypoint} + */ + public void addWaypoint(Waypoint waypoint) { + if (this.waypoints == null) { + this.waypoints = new HashSet(); + } + this.waypoints.add(waypoint); + + } + + /** + * Returns the creator of this gpx object + * + * @return A String representing the creator of a gpx object + */ + public String getCreator() { + return this.creator; + } + + /** + * Getter for the list of routes from a gpx object + * + * @return a HashSet of {@link Route} + */ + public HashSet getRoutes() { + return this.routes; + } + + /** + * Getter for the list of Tracks from a gpx objecty + * + * @return a HashSet of {@link Track} + */ + public HashSet getTracks() { + return this.tracks; + } + + /** + * Returns the version of a gpx object + * + * @return A String representing the version of this gpx object + */ + public String getVersion() { + return this.version; + } + + /** + * Getter for the list of waypoints from a gpx objecty + * + * @return a HashSet of {@link Waypoint} + */ + public HashSet getWaypoints() { + return this.waypoints; + } + + /** + * Setter for gpx creator property. This maps to creator attribute + * value. + * + * @param creator + * A String representing the creator of a gpx file. + */ + public void setCreator(String creator) { + this.creator = creator; + } + + /** + * Setter for the list of routes from a gpx object + * + * @param routes + * a HashSet of {@link Route} + */ + public void setRoutes(HashSet routes) { + this.routes = routes; + } + + /** + * Setter for the list of tracks from a gpx object + * + * @param tracks + * a HashSet of {@link Track} + */ + public void setTracks(HashSet tracks) { + this.tracks = tracks; + } + + /** + * Setter for the list of waypoints from a gpx object + * + * @param waypoints + * a HashSet of {@link Waypoint} + */ + public void setWaypoints(HashSet waypoints) { + this.waypoints = waypoints; + } + + public Metadata getMetadata() { + return metadata; + } + + public void setMetadata(Metadata metadata) { + this.metadata = metadata; + } + + public void setVersion(String version) { + this.version = version; + } +} diff --git a/src/com/hs/gpxparser/modal/Link.java b/src/com/hs/gpxparser/modal/Link.java new file mode 100644 index 0000000..d866627 --- /dev/null +++ b/src/com/hs/gpxparser/modal/Link.java @@ -0,0 +1,40 @@ +package com.hs.gpxparser.modal; + +/** + * Created by Himanshu on 7/5/2015. + */ +public class Link { + + private String href; + + private String text; + private String type; + + public Link(String href) { + this.href = href; + } + + public String getHref() { + return href; + } + + public void setHref(String href) { + this.href = href; + } + + public String getText() { + return text; + } + + public void setText(String text) { + this.text = text; + } + + public String getType() { + return type; + } + + public void setType(String type) { + this.type = type; + } +} diff --git a/src/com/hs/gpxparser/modal/Metadata.java b/src/com/hs/gpxparser/modal/Metadata.java new file mode 100644 index 0000000..596f9ff --- /dev/null +++ b/src/com/hs/gpxparser/modal/Metadata.java @@ -0,0 +1,100 @@ +package com.hs.gpxparser.modal; + +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.HashSet; + +/** + * Created by Himanshu on 7/5/2015. + */ +public class Metadata extends Extension { + private String name; + private String desc; + private Person author; + private Copyright copyright; + private HashSet links; + private Date time; + private String keywords; + private Bounds bounds; + + public Metadata() { + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getDesc() { + return desc; + } + + public void setDesc(String desc) { + this.desc = desc; + } + + public String getKeywords() { + return keywords; + } + + public void setKeywords(String keywords) { + this.keywords = keywords; + } + + public Date getTime() { + return time; + } + + public void setTime(Date time) { + this.time = time; + } + + public void setTime(long timeMillis) throws ParseException { + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); + String date = sdf.format(new Date(timeMillis)); + this.time = sdf.parse(date); + } + + public Person getAuthor() { + return author; + } + + public void setAuthor(Person author) { + this.author = author; + } + + public Copyright getCopyright() { + return copyright; + } + + public void setCopyright(Copyright copyright) { + this.copyright = copyright; + } + + public HashSet getLinks() { + return links; + } + + public void setLinks(HashSet links) { + this.links = links; + } + + public void addLink(Link link) { + if (links == null) { + links = new HashSet<>(); + } + links.add(link); + } + + public Bounds getBounds() { + return bounds; + } + + public void setBounds(Bounds bounds) { + this.bounds = bounds; + } +} diff --git a/src/com/hs/gpxparser/modal/Person.java b/src/com/hs/gpxparser/modal/Person.java new file mode 100644 index 0000000..e5835f4 --- /dev/null +++ b/src/com/hs/gpxparser/modal/Person.java @@ -0,0 +1,34 @@ +package com.hs.gpxparser.modal; + +/** + * Created by Himanshu on 7/5/2015. + */ +public class Person { + private String name; + private Email email; + private Link link; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Email getEmail() { + return email; + } + + public void setEmail(Email email) { + this.email = email; + } + + public Link getLink() { + return link; + } + + public void setLink(Link link) { + this.link = link; + } +} diff --git a/src/com/hs/gpxparser/modal/Point.java b/src/com/hs/gpxparser/modal/Point.java new file mode 100644 index 0000000..27b4893 --- /dev/null +++ b/src/com/hs/gpxparser/modal/Point.java @@ -0,0 +1,51 @@ + +package com.hs.gpxparser.modal; + +import java.util.Date; + +public class Point { + + private double latitude; + private double longitude; + + private double elevation; + private Date time; + + public Point(double latitude, double longitude) { + this.latitude = latitude; + this.longitude = longitude; + } + + + public double getLatitude() { + return latitude; + } + + public void setLatitude(double latitude) { + this.latitude = latitude; + } + + public double getLongitude() { + return longitude; + } + + public void setLongitude(double longitude) { + this.longitude = longitude; + } + + public double getElevation() { + return elevation; + } + + public void setElevation(double elevation) { + this.elevation = elevation; + } + + public Date getTime() { + return time; + } + + public void setTime(Date time) { + this.time = time; + } +} diff --git a/src/com/hs/gpxparser/modal/PointSegment.java b/src/com/hs/gpxparser/modal/PointSegment.java new file mode 100644 index 0000000..486ca36 --- /dev/null +++ b/src/com/hs/gpxparser/modal/PointSegment.java @@ -0,0 +1,28 @@ +package com.hs.gpxparser.modal; + +import java.util.ArrayList; + +/** + * Created by Himanshu on 7/5/2015. + */ +public class PointSegment { + private ArrayList points; + + public PointSegment() { + } + + public ArrayList getPoints() { + return points; + } + + public void setPoints(ArrayList points) { + this.points = points; + } + + public void addPoint(Point point) { + if (points == null) { + points = new ArrayList<>(); + } + points.add(point); + } +} diff --git a/src/com/hs/gpxparser/modal/Route.java b/src/com/hs/gpxparser/modal/Route.java new file mode 100644 index 0000000..cf5952f --- /dev/null +++ b/src/com/hs/gpxparser/modal/Route.java @@ -0,0 +1,186 @@ + +package com.hs.gpxparser.modal; + +import java.util.ArrayList; +import java.util.HashSet; + +public class Route extends Extension { + private String name; + private String comment; + private String description; + private String src; + private HashSet links; + private int number; + + private String type; + private ArrayList routePoints; + + /** + * Returns the name of this route. + * + * @return A String representing the name of this route. + */ + public String getName() { + return name; + } + + /** + * Setter for route name property. This maps to <name> tag value. + * + * @param name A String representing the name of this route. + */ + public void setName(String name) { + this.name = name; + } + + /** + * Returns the comment of this route. + * + * @return A String representing the comment of this route. + */ + public String getComment() { + return comment; + } + + /** + * Setter for route comment property. This maps to <comment> tag value. + * + * @param comment A String representing the comment of this route. + */ + public void setComment(String comment) { + this.comment = comment; + } + + /** + * Returns the description of this route. + * + * @return A String representing the description of this route. + */ + public String getDescription() { + return description; + } + + /** + * Setter for route description property. This maps to <description> tag value. + * + * @param description A String representing the description of this route. + */ + public void setDescription(String description) { + this.description = description; + } + + /** + * Returns the src of this route. + * + * @return A String representing the src of this route. + */ + public String getSrc() { + return src; + } + + /** + * Setter for src type property. This maps to <src> tag value. + * + * @param src A String representing the src of this route. + */ + public void setSrc(String src) { + this.src = src; + } + + /** + * Returns the number of this route. + * + * @return A String representing the number of this route. + */ + public Integer getNumber() { + return number; + } + + /** + * Setter for route number property. This maps to <number> tag value. + * + * @param number An Integer representing the number of this route. + */ + public void setNumber(Integer number) { + this.number = number; + } + + /** + * Returns the type of this route. + * + * @return A String representing the type of this route. + */ + public String getType() { + return type; + } + + /** + * Setter for route type property. This maps to <type> tag value. + * + * @param type A String representing the type of this route. + */ + public void setType(String type) { + this.type = type; + } + + /** + * Getter for the list of waypoints of this route. + * + * @return an ArrayList of {@link Waypoint} representing the points of the route. + */ + public ArrayList getRoutePoints() { + return routePoints; + } + + /** + * Setter for the list of waypoints of this route. + * + * @param routePoints an ArrayList of {@link Waypoint} representing the points of the route. + */ + public void setRoutePoints(ArrayList routePoints) { + this.routePoints = routePoints; + } + + /** + * Adds this new waypoint to this route. + * + * @param waypoint a {@link Waypoint}. + */ + public void addRoutePoint(Waypoint waypoint) { + if (routePoints == null) { + routePoints = new ArrayList(); + } + routePoints.add(waypoint); + } + + public HashSet getLinks() { + return links; + } + + public void setLinks(HashSet links) { + this.links = links; + } + + public void addLink(Link link) { + if (links == null) { + links = new HashSet<>(); + } + links.add(link); + } + + /** + * Returns a String representation of this track. + */ + public String toString() { + StringBuffer sb = new StringBuffer(); + sb.append("rte["); + sb.append("name:" + name + " "); + int points = 0; + if (routePoints != null) { + points = routePoints.size(); + } + sb.append("rtepts:" + points + " "); + sb.append("]"); + return sb.toString(); + } +} diff --git a/src/com/hs/gpxparser/modal/Track.java b/src/com/hs/gpxparser/modal/Track.java new file mode 100644 index 0000000..5124c1c --- /dev/null +++ b/src/com/hs/gpxparser/modal/Track.java @@ -0,0 +1,165 @@ + +package com.hs.gpxparser.modal; + +import java.util.ArrayList; +import java.util.HashSet; + +public class Track extends Extension { + private String name; + private String comment; + private String description; + private String src; + private HashSet links; + private int number; + private String type; + private ArrayList trackSegments; + + /** + * Returns the name of this track. + * + * @return A String representing the name of this track. + */ + public String getName() { + return name; + } + + /** + * Setter for track name property. This maps to <name> tag value. + * + * @param name A String representing the name of this track. + */ + public void setName(String name) { + this.name = name; + } + + /** + * Returns the comment of this track. + * + * @return A String representing the comment of this track. + */ + public String getComment() { + return comment; + } + + /** + * Setter for track comment property. This maps to <comment> tag value. + * + * @param comment A String representing the comment of this track. + */ + public void setComment(String comment) { + this.comment = comment; + } + + /** + * Returns the description of this track. + * + * @return A String representing the description of this track. + */ + public String getDescription() { + return description; + } + + /** + * Setter for track description property. This maps to <description> tag value. + * + * @param description A String representing the description of this track. + */ + public void setDescription(String description) { + this.description = description; + } + + /** + * Returns the src of this track. + * + * @return A String representing the src of this track. + */ + public String getSrc() { + return src; + } + + /** + * Setter for src type property. This maps to <src> tag value. + * + * @param src A String representing the src of this track. + */ + public void setSrc(String src) { + this.src = src; + } + + /** + * Returns the number of this track. + * + * @return A String representing the number of this track. + */ + public Integer getNumber() { + return number; + } + + /** + * Setter for track number property. This maps to <number> tag value. + * + * @param number An Integer representing the number of this track. + */ + public void setNumber(Integer number) { + this.number = number; + } + + /** + * Returns the type of this track. + * + * @return A String representing the type of this track. + */ + public String getType() { + return type; + } + + /** + * Setter for track type property. This maps to <type> tag value. + * + * @param type A String representing the type of this track. + */ + public void setType(String type) { + this.type = type; + } + + public HashSet getLinks() { + return links; + } + + public void setLinks(HashSet links) { + this.links = links; + } + + public void addLink(Link link) { + if (links == null) { + links = new HashSet<>(); + } + links.add(link); + } + + /** + * Returns a String representation of this track. + */ + public String toString() { + StringBuffer sb = new StringBuffer(); + sb.append("trk["); + sb.append("name:" + name + " "); + sb.append("]"); + return sb.toString(); + } + + public ArrayList getTrackSegments() { + return trackSegments; + } + + public void setTrackSegments(ArrayList trackSegments) { + this.trackSegments = trackSegments; + } + + public void addTrackSegment(TrackSegment trackSegment) { + if (trackSegments == null) { + trackSegments = new ArrayList<>(); + } + trackSegments.add(trackSegment); + } +} diff --git a/src/com/hs/gpxparser/modal/TrackSegment.java b/src/com/hs/gpxparser/modal/TrackSegment.java new file mode 100644 index 0000000..3e6ecde --- /dev/null +++ b/src/com/hs/gpxparser/modal/TrackSegment.java @@ -0,0 +1,26 @@ +package com.hs.gpxparser.modal; + +import java.util.ArrayList; + +/** + * Created by Himanshu on 7/5/2015. + */ +public class TrackSegment extends Extension { + private ArrayList waypoints; + + public ArrayList getWaypoints() { + return waypoints; + } + + public void setWaypoints(ArrayList waypoints) { + this.waypoints = waypoints; + } + + public void addWaypoint(Waypoint wp) { + if (waypoints == null) { + waypoints = new ArrayList<>(); + } + waypoints.add(wp); + } + +} diff --git a/src/com/hs/gpxparser/modal/Waypoint.java b/src/com/hs/gpxparser/modal/Waypoint.java new file mode 100644 index 0000000..52414e1 --- /dev/null +++ b/src/com/hs/gpxparser/modal/Waypoint.java @@ -0,0 +1,469 @@ +package com.hs.gpxparser.modal; + +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.HashSet; +import java.util.Iterator; + +import com.hs.gpxparser.type.Fix; + +public class Waypoint extends Extension { + + private double latitude; + private double longitude; + + private double elevation; + private Date time; + private double magneticVariation; + private double geoIdHeight; + private String name; + private String comment; + private String description; + private String src; + private HashSet links; + /** + * Symbol + */ + private String sym; + private String type; + private Fix fix; + private int sat; + /** + * Horizontal dilution of precision. + */ + private double hdop; + /** + * Vertical dilution of precision. + */ + private double vdop; + /** + * Position dilution of precision. + */ + private double pdop; + + private double ageOfGpsData; + private int dGpsStationId; + + public Waypoint(double latitude, double longitude) { + this.latitude = latitude; + this.longitude = longitude; + } + + /** + * Returns the ageOfGPSData of this waypoint. + * + * @return a Double representing the ageOfGPSData of this waypoint. + */ + public double getAgeOfGPSData() { + return this.ageOfGpsData; + } + + /** + * Returns the comment of this waypoint. + * + * @return a String representing the comment of this waypoint. + */ + public String getComment() { + return this.comment; + } + + /** + * Returns the description of this waypoint. + * + * @return a String representing the description of this waypoint. + */ + public String getDescription() { + return this.description; + } + + /** + * Returns the dGpsStationId of this waypoint. + * + * @return an Integer representing the dGpsStationId of this waypoint. + */ + public int getdGpsStationId() { + return this.dGpsStationId; + } + + /** + * Returns the elevation of this waypoint. + * + * @return A Double representing the elevation of this waypoint. + */ + public double getElevation() { + return this.elevation; + } + + /** + * Returns the fix of this waypoint. + * + * @return A {@link Fix} representing the fix of this waypoint. + */ + public Fix getFix() { + return this.fix; + } + + /** + * Returns the geoid height of this waypoint. + * + * @return A String representing the geoid height of this waypoint. + */ + public double getGeoIdHeight() { + return this.geoIdHeight; + } + + /** + * Returns the hdop of this waypoint. + * + * @return a Double representing the hdop of this waypoint. + */ + public double getHdop() { + return this.hdop; + } + + /** + * Returns the latitude of this waypoint. + * + * @return a Double value representing the latitude of this waypoint. + */ + public double getLatitude() { + return this.latitude; + } + + /** + * Returns the longitude of this waypoint. + * + * @return a Double value representing the longitude of this waypoint. + */ + public double getLongitude() { + return this.longitude; + } + + /** + * Returns the magnetic declination of this waypoint. + * + * @return A Double representing the magnetic declination of this waypoint. + */ + public double getMagneticVariation() { + return this.magneticVariation; + } + + /** + * Returns the name of this waypoint. + * + * @return A String representing the name of this waypoint. + */ + public String getName() { + return this.name; + } + + /** + * Returns the pdop of this waypoint. + * + * @return a Double representing the pdop of this waypoint. + */ + public double getPdop() { + return this.pdop; + } + + /** + * Returns the sat of this waypoint. + * + * @return an Integer representing the sat of this waypoint. + */ + public int getSat() { + return this.sat; + } + + /** + * Returns the src of this waypoint. + * + * @return A String representing the src of this waypoint. + */ + public String getSrc() { + return this.src; + } + + /** + * Returns the sym of this waypoint. + * + * @return A String representing the sym of this waypoint. + */ + public String getSym() { + return this.sym; + } + + /** + * Returns the time of this waypoint. + * + * @return a Date representing the name of this waypoint. + */ + public Date getTime() { + return this.time; + } + + /** + * Returns the type of this waypoint. + * + * @return A String representing the type of this waypoint. + */ + public String getType() { + return this.type; + } + + /** + * Returns the vdop of this waypoint. + * + * @return a Double representing the vdop of this waypoint. + */ + public double getVdop() { + return this.vdop; + } + + /** + * Setter for waypoint ageOfGPSData property. This maps to + * <ageOfGPSData> tag value. + * + * @param ageOfGPSData + * A String representing the ageOfGPSData of this waypoint. + */ + public void setAgeOfGPSData(double ageOfGPSData) { + this.ageOfGpsData = ageOfGPSData; + } + + /** + * Setter for waypoint comment property. This maps to <cmt> tag value. + * + * @param comment + * A String representing the comment of this waypoint. + */ + public void setComment(String comment) { + this.comment = comment; + } + + /** + * Setter for waypoint description property. This maps to <desc> tag + * value. + * + * @param description + * A String representing the description of this waypoint. + */ + public void setDescription(String description) { + this.description = description; + } + + /** + * Setter for waypoint dGpsStationId property. This maps to + * <dGpsStationId> tag value. + * + * @param dGpsStationId + * an Integer representing the dGpsStationId of this waypoint. + */ + public void setdGpsStationId(int dGpsStationId) { + this.dGpsStationId = dGpsStationId; + } + + /** + * Setter for waypoint elevation property. This maps to <ele> tag + * value. + * + * @param elevation + * a Double value representing the elevation of this waypoint. + */ + public void setElevation(double elevation) { + this.elevation = elevation; + } + + /** + * Setter for waypoint fix property. This maps to <fix> tag value. + * + * @param fix + * a {@link Fix} representing the fix of this waypoint. + */ + public void setFix(Fix fix) { + this.fix = fix; + } + + /** + * Setter for waypoint geoid height property. This maps to + * <geoidheight> tag value. + * + * @param geoIdHeight + * A String representing the geoid height of this waypoint. + */ + public void setGeoIdHeight(double geoIdHeight) { + this.geoIdHeight = geoIdHeight; + } + + /** + * Setter for waypoint hdop property. This maps to <hdop> tag value. + * + * @param hdop + * a Double representing the name of this waypoint. + */ + public void setHdop(double hdop) { + this.hdop = hdop; + } + + /** + * Setter for waypoint latitude property. This maps to "lat" attribute + * value. + * + * @param latitude + * a Doube value representing the latitude of this waypoint. + */ + public void setLatitude(double latitude) { + this.latitude = latitude; + } + + /** + * Setter for waypoint longitude property. This maps to "long" attribute + * value. + * + * @param longitude + * a Doube value representing the longitude of this waypoint. + */ + public void setLongitude(double longitude) { + this.longitude = longitude; + } + + /** + * Setter for waypoint magnetic declination property. This maps to + * <magvar> tag value. + * + * @param magVar + * A String representing the magnetic declination of this + * waypoint. + */ + public void setMagneticVariation(double magVar) { + this.magneticVariation = magVar; + } + + /** + * Setter for waypoint name property. This maps to <name> tag value. + * + * @param name + * A String representing the name of this waypoint. + */ + public void setName(String name) { + this.name = name; + } + + /** + * Setter for waypoint pdop property. This maps to <pdop> tag value. + * + * @param pdop + * a Double representing the pdop of this waypoint. + */ + public void setPdop(double pdop) { + this.pdop = pdop; + } + + /** + * Setter for waypoint sat property. This maps to <sat> tag value. + * + * @param sat + * an Integer representing the sat of this waypoint. + */ + public void setSat(int sat) { + this.sat = sat; + } + + /** + * Setter for waypoint src property. This maps to <src> tag value. + * + * @param src + * a String representing the src of this waypoint. + */ + public void setSrc(String src) { + this.src = src; + } + + /** + * Setter for waypoint sym property. This maps to <sym> tag value. + * + * @param sym + * a String representing the sym of this waypoint. + */ + public void setSym(String sym) { + this.sym = sym; + } + + /** + * Setter for waypoint time property. This maps to <time> tag value. + * + * @param time + * a Date representing the time of this waypoint. + */ + public void setTime(Date time) { + this.time = time; + } + + /** + * Setter for waypoint type property. This maps to <type> tag value. + * + * @param type + * a String representing the type of this waypoint. + */ + public void setType(String type) { + this.type = type; + } + + /** + * Setter for waypoint vdop property. This maps to <vdop> tag value. + * + * @param vdop + * A String representing the vdop of this waypoint. + */ + public void setVdop(double vdop) { + this.vdop = vdop; + } + + /** + * Returns a String representation of this waypoint. + */ + @Override + public String toString() { + StringBuffer sb = new StringBuffer(); + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); + String date = ""; + if (this.time != null) { + date = sdf.format(this.time); + } + sb.append("["); + sb.append("name:'" + this.name + "' "); + sb.append("lat:" + this.latitude + " "); + sb.append("lon:" + this.longitude + " "); + sb.append("elv:" + this.elevation + " "); + sb.append("time:" + date + " "); + sb.append("fix:" + this.fix + " "); + if (this.extensionData != null) { + sb.append("extensions:{"); + Iterator it = this.extensionData.keySet().iterator(); + while (it.hasNext()) { + sb.append(it.next()); + if (it.hasNext()) { + sb.append(","); + } + } + sb.append("}"); + } + sb.append("]"); + return sb.toString(); + } + + public HashSet getLinks() { + return links; + } + + public void setLinks(HashSet links) { + this.links = links; + } + + public void addLink(Link link) { + if (links == null) { + links = new HashSet<>(); + } + links.add(link); + } +} diff --git a/src/com/hs/gpxparser/type/Fix.java b/src/com/hs/gpxparser/type/Fix.java new file mode 100644 index 0000000..24e4582 --- /dev/null +++ b/src/com/hs/gpxparser/type/Fix.java @@ -0,0 +1,65 @@ + +package com.hs.gpxparser.type; + +/** + *

Type of GPS fix. Value comes from list: {'none'|'2d'|'3d'|'dgps'|'pps'}

+ *
+ *
    + *
  • none = GPS had no fix.
  • + *
  • pps = military signal used
  • + *
+ *

To signify "the fix info is unknown", leave out fixType entirely.

+ */ +public class Fix { + + /** + * Constant that defines 'none' Fix type + */ + public static Fix NONE = new Fix("none"); + /** + * Constant that defines '2d' Fix type + */ + public static Fix TWO_D = new Fix("2d"); + /** + * Constant that defines '3d' Fix type + */ + public static Fix THREE_D = new Fix("3d"); + /** + * Constant that defines 'dgps' Fix type + */ + public static Fix DGPS = new Fix("dgps"); + + //'none'|'2d'|'3d'|'dgps'|'pps' + /** + * Constant that defines 'pps' Fix type + */ + public static Fix PPS = new Fix("pps"); + private String value; + + private Fix(String value) { + this.value = value; + } + + public static Fix returnType(String value) { + if (NONE.getValue().equals(value)) { + return NONE; + } else if (TWO_D.getValue().equals(value)) { + return TWO_D; + } else if (THREE_D.getValue().equals(value)) { + return THREE_D; + } else if (DGPS.getValue().equals(value)) { + return DGPS; + } else if (PPS.getValue().equals(value)) { + return PPS; + } + return null; + } + + public String getValue() { + return value; + } + + public String toString() { + return value; + } +}