Skip to content

Commit

Permalink
Merge pull request #2 from ThomasDaheim/Extend-GPXParser
Browse files Browse the repository at this point in the history
Fixed extension handling
  • Loading branch information
ThomasDaheim authored Feb 17, 2018
2 parents 89afbb3 + 903f221 commit f284748
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 18 deletions.
6 changes: 6 additions & 0 deletions src/com/hs/gpxparser/GPXParser.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.hs.gpxparser;

import com.hs.gpxparser.extension.DummyExtensionParser;
import java.io.InputStream;
import java.text.ParseException;
import java.util.Date;
Expand Down Expand Up @@ -63,6 +64,11 @@ public class GPXParser extends BaseGPX {
* @throws Exception
*/
public GPX parseGPX(InputStream in) throws Exception {
// TFE, 20180217: add default parser if none set
if (this.extensionParsers.isEmpty()) {
this.extensionParsers.add(new DummyExtensionParser());
}

DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = builder.parse(in);
Node firstChild = doc.getFirstChild();
Expand Down
10 changes: 8 additions & 2 deletions src/com/hs/gpxparser/GPXWriter.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.hs.gpxparser;

import com.hs.gpxparser.extension.DummyExtensionParser;
import java.io.OutputStream;
import java.util.Iterator;

Expand Down Expand Up @@ -33,7 +34,12 @@
public class GPXWriter extends BaseGPX {

public void writeGPX(GPX gpx, OutputStream out) throws ParserConfigurationException, TransformerException {
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
// TFE, 20180217: add default parser if none set
if (this.extensionParsers.isEmpty()) {
this.extensionParsers.add(new DummyExtensionParser());
}

DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = builder.newDocument();
Node gpxNode = doc.createElement(GPXConstants.NODE_GPX);

Expand Down Expand Up @@ -459,7 +465,7 @@ 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);
parser.writeExtensions(e, node, doc);
}
n.appendChild(node);
}
Expand Down
56 changes: 56 additions & 0 deletions src/com/hs/gpxparser/extension/DummyExtensionHolder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright (c) 2014ff Thomas Feuster
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.hs.gpxparser.extension;

import org.w3c.dom.NodeList;

/**
* Simple holder for node elements under <extensions>.
*
* Is used by the default DummyExtensionParser in case no other
* IExtensionParser are set. Otherwise the extensions would get lost
* when simply parsing and writing a gpx file.
*
* @author thomas
*/
public class DummyExtensionHolder {
private NodeList myNodeList;

DummyExtensionHolder() {
}

DummyExtensionHolder(final NodeList childNodes) {
myNodeList = childNodes;
}

public NodeList getNodeList() {
return myNodeList;
}

public void setNodeList(final NodeList nodeList) {
myNodeList = nodeList;
}
}
45 changes: 31 additions & 14 deletions src/com/hs/gpxparser/extension/DummyExtensionParser.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,40 @@
package com.hs.gpxparser.extension;

import com.hs.gpxparser.GPXConstants;
import com.hs.gpxparser.modal.Extension;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class DummyExtensionParser implements IExtensionParser {
@Override
public String getId() {
return "Basic Extension Parser";
}

@Override
public String getId() {
return "Basic Extension Parser";
}
@Override
public Object parseExtensions(Node node) {
// store all nodes under extension in DummyExtensionHolder - if any
if (GPXConstants.NODE_EXTENSIONS.equals(node.getNodeName()) && (node.getChildNodes().getLength() > 0)) {
return new DummyExtensionHolder(node.getChildNodes());
} else {
return null;
}
}

@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
}
@Override
public void writeExtensions(Extension e, Node node, Document doc) {
if(e.getExtensionData(getId()) != null) {
// add all nodes from DummyExtensionHolder to the document
final DummyExtensionHolder holder = (DummyExtensionHolder) e.getExtensionData(getId());
final NodeList nodes = holder.getNodeList();

// https://stackoverflow.com/questions/5786936/create-xml-document-using-nodelist
for (int i = 0; i < nodes.getLength(); i++) {
final Node extNode = nodes.item(i);
final Node copyNode = doc.importNode(extNode, true);
node.appendChild(copyNode);
}
}
}
}
4 changes: 3 additions & 1 deletion src/com/hs/gpxparser/extension/IExtensionParser.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.hs.gpxparser.extension;

import com.hs.gpxparser.modal.Extension;
import org.w3c.dom.Document;
import org.w3c.dom.Node;

Expand All @@ -9,6 +10,7 @@ public interface IExtensionParser {

public Object parseExtensions(Node node);

public void writeExtensions(Node node, Document doc);
// TFE, 20180216: can't write any data out if the node hasn't been passed as well!
public void writeExtensions(Extension e, Node node, Document doc);

}
8 changes: 7 additions & 1 deletion src/com/hs/gpxparser/modal/Extension.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,14 @@ public void setExtensionData(HashMap<String, Object> extensionData) {
* the extension parser's job to set it properly.
*/
public void addExtensionData(String parserId, Object data) {
// TFE, 20180217
// don't add null data - otherwise for each parser one null entry would be added
// to each extension in a gpx file! That is too much waste considering
// that in most cases no extensions at all are present
if (data == null) return;

if(extensionData == null) {
extensionData = new HashMap<String, Object>();
extensionData = new HashMap<>();
}
extensionData.put(parserId, data);
}
Expand Down

0 comments on commit f284748

Please sign in to comment.