-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from ThomasDaheim/Extend-GPXParser
Fixed extension handling
- Loading branch information
Showing
6 changed files
with
111 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters