Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add possibility to indent GPX output #31

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/main/java/me/himanshusoni/gpxparser/GPXWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
Expand All @@ -21,6 +22,10 @@
public class GPXWriter extends BaseGPX {

public void writeGPX(GPX gpx, OutputStream out) throws ParserConfigurationException, TransformerException {
writeGPX(gpx, out, 0);
}

public void writeGPX(GPX gpx, OutputStream out, int indent) throws ParserConfigurationException, TransformerException {
// TFE, 20180217: add default parser if none set
if (this.extensionParsers.isEmpty()) {
this.extensionParsers.add(new DummyExtensionParser());
Expand Down Expand Up @@ -77,6 +82,10 @@ public void writeGPX(GPX gpx, OutputStream out) throws ParserConfigurationExcept

DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(out);
if (indent > 0) {
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", Integer.toString(indent));
}
transformer.transform(source, result);
}

Expand Down