Skip to content

Commit

Permalink
Merge pull request #1 from ThomasDaheim/Extend-GPXParser
Browse files Browse the repository at this point in the history
Extend gpxparser
  • Loading branch information
ThomasDaheim authored Feb 5, 2018
2 parents f548e66 + 5045571 commit 89afbb3
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 14 deletions.
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ hs_err_pid*
# mpeltonen/sbt-idea plugin
.idea_modules/

# netbeans
nbproject/
build/
nbbuild/
dist/
nbdist/
.nb-gradle/
build.xml

### Java template
# Compiled class file
*.class
Expand Down
2 changes: 2 additions & 0 deletions src/com/hs/gpxparser/GPXConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ public interface GPXConstants {
String NODE_GPX = "gpx";

/* GPX nodes and attributes */
// TFE, 20180201: some gpx parser fail if no xmlns defined
String ATTR_XMLNS = "xmlns";
String ATTR_CREATOR = "creator";
String ATTR_VERSION = "version";

Expand Down
16 changes: 13 additions & 3 deletions src/com/hs/gpxparser/GPXParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.hs.gpxparser.modal.TrackSegment;
import com.hs.gpxparser.modal.Waypoint;
import com.hs.gpxparser.type.Fix;
import java.util.regex.Pattern;

/**
* <p>
Expand All @@ -48,6 +49,9 @@
* </code>
*/
public class GPXParser extends BaseGPX {

// TFE, 20180109: use pattern for parsing to improve performance
private final static Pattern datevaluePattern = Pattern.compile("([0-9\\-T]+:[0-9]{2}:[0-9.+]+):([0-9]{2})");

/**
* Parses a stream containing GPX data
Expand All @@ -73,7 +77,10 @@ public GPX parseGPX(InputStream in) throws Exception {
gpx.setVersion(attr.getNodeValue());
} else if (GPXConstants.ATTR_CREATOR.equals(attr.getNodeName())) {
gpx.setCreator(attr.getNodeValue());
}
// TFE, 20180201: support xmlns attribute
} else if (GPXConstants.ATTR_XMLNS.equals(attr.getNodeName())) {
gpx.setXmlns(attr.getNodeValue());
}
}

NodeList nodes = firstChild.getChildNodes();
Expand Down Expand Up @@ -508,8 +515,11 @@ private TrackSegment parseTrackSegment(Node node) throws Exception {
private Date getNodeValueAsDate(Node node) throws DOMException, ParseException {
Date val = null;
try {
val = xmlDateFormat.parse(node.getFirstChild().getNodeValue()
.replaceAll("([0-9\\-T]+:[0-9]{2}:[0-9.+]+):([0-9]{2})", "$1$2"));
// TFE, 20180109: use pre-compiled pattern instead of String.replaceAll
// val = xmlDateFormat.parse(node.getFirstChild().getNodeValue()
// .replaceAll("([0-9\\-T]+:[0-9]{2}:[0-9.+]+):([0-9]{2})", "$1$2"));
val = xmlDateFormat.parse(
datevaluePattern.matcher(node.getFirstChild().getNodeValue()).replaceAll("$1$2"));
} catch (ParseException e) {
e.printStackTrace();
} catch (Exception e) {
Expand Down
25 changes: 17 additions & 8 deletions src/com/hs/gpxparser/GPXWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ public void writeGPX(GPX gpx, OutputStream out) throws ParserConfigurationExcept
creatorNode.setNodeValue(gpx.getCreator());
attrs.setNamedItem(creatorNode);
}
// TFE, 20180201: support xmlns attribute
if (gpx.getXmlns()!= null) {
Node xmlnsNode = doc.createAttribute(GPXConstants.ATTR_XMLNS);
xmlnsNode.setNodeValue(gpx.getXmlns());
attrs.setNamedItem(xmlnsNode);
}
if (gpx.getMetadata() != null) {
this.addMetadataToNode(gpx.getMetadata(), gpxNode, doc);
}
Expand Down Expand Up @@ -346,8 +352,8 @@ private void addMetadataToNode(Metadata m, Node n, Document doc) {
}

private void addBoundsToNode(Bounds bounds, Node n, Document doc) {
Node bonundsNode = doc.createElement(GPXConstants.NODE_BOUNDS);
NamedNodeMap attributes = bonundsNode.getAttributes();
Node boundsNode = doc.createElement(GPXConstants.NODE_BOUNDS);
NamedNodeMap attributes = boundsNode.getAttributes();

Node node = doc.createAttribute(GPXConstants.ATTR_MINLAT);
node.setNodeValue(String.valueOf(bounds.getMinLat()));
Expand All @@ -364,6 +370,9 @@ private void addBoundsToNode(Bounds bounds, Node n, Document doc) {
node = doc.createAttribute(GPXConstants.ATTR_MAXLON);
node.setNodeValue(String.valueOf(bounds.getMaxLon()));
attributes.setNamedItem(node);

// TFE, 20180201: bugfix - boundsNode wasn't appended
n.appendChild(boundsNode);
}

private void addCopyrightToNode(Copyright copyright, Node n, Document doc) {
Expand All @@ -378,24 +387,23 @@ private void addCopyrightToNode(Copyright copyright, Node n, Document doc) {
if (copyright.getYear() != null) {
Node node = doc.createElement(GPXConstants.NODE_YEAR);
node.appendChild(doc.createTextNode(copyright.getYear()));
n.appendChild(node);
copyrightNode.appendChild(node);
}
if (copyright.getLicense() != null) {
Node node = doc.createElement(GPXConstants.NODE_LICENSE);
node.appendChild(doc.createTextNode(copyright.getLicense()));
n.appendChild(node);
copyrightNode.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);
authorNode.appendChild(node);
}
if (author.getEmail() != null) {
this.addEmailToNode(author.getEmail(), authorNode, doc);
Expand All @@ -419,12 +427,12 @@ private void addLinkToNode(Link link, Node n, Document doc) {
if (link.getText() != null) {
Node node = doc.createElement(GPXConstants.NODE_TEXT);
node.appendChild(doc.createTextNode(link.getText()));
n.appendChild(node);
linkNode.appendChild(node);
}
if (link.getType() != null) {
Node node = doc.createElement(GPXConstants.NODE_TYPE);
node.appendChild(doc.createTextNode(link.getType()));
n.appendChild(node);
linkNode.appendChild(node);
}

n.appendChild(linkNode);
Expand All @@ -444,6 +452,7 @@ private void addEmailToNode(Email email, Node n, Document doc) {
attributes.setNamedItem(node);
}

n.appendChild(emailNode);
}

private void addExtensionToNode(Extension e, Node n, Document doc) {
Expand Down
8 changes: 8 additions & 0 deletions src/com/hs/gpxparser/modal/Bounds.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,12 @@ public double getMaxLon() {
public void setMaxLon(double maxLon) {
this.maxLon = maxLon;
}

// TFE, 20180201: helper method to extend bounds easily
public void extendBounds(final Bounds bounds) {
this.minLat = Math.min(minLat, bounds.minLat);
this.maxLat = Math.max(maxLat, bounds.maxLat);
this.minLon = Math.min(minLon, bounds.minLon);
this.maxLon = Math.max(maxLon, bounds.maxLon);
}
}
28 changes: 25 additions & 3 deletions src/com/hs/gpxparser/modal/GPX.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
public class GPX extends Extension {

// Attributes
// TFE, 20180201: support xmlns attribute
private String xmlns;
private String creator;
private String version = "1.1";

Expand All @@ -30,9 +32,9 @@ public class GPX extends Extension {
private HashSet<Waypoint> waypoints;

public GPX() {
this.waypoints = new HashSet<Waypoint>();
this.tracks = new HashSet<Track>();
this.routes = new HashSet<Route>();
this.waypoints = new HashSet<>();
this.tracks = new HashSet<>();
this.routes = new HashSet<>();
}

/**
Expand Down Expand Up @@ -75,6 +77,15 @@ public void addWaypoint(Waypoint waypoint) {

}

/**
* Returns the xmlns of this gpx object
*
* @return A String representing the xmlns of a gpx object
*/
public String getXmlns() {
return xmlns;
}

/**
* Returns the creator of this gpx object
*
Expand Down Expand Up @@ -120,6 +131,17 @@ public HashSet<Waypoint> getWaypoints() {
return this.waypoints;
}

/**
* Setter for gpx xmlns property. This maps to <i>xmlns</i> attribute
* value.
*
* @param xmlns
* A String representing the xmlns of a gpx file.
*/
public void setXmlns(String xmlns) {
this.xmlns = xmlns;
}

/**
* Setter for gpx creator property. This maps to <i>creator</i> attribute
* value.
Expand Down

0 comments on commit 89afbb3

Please sign in to comment.