import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
-import java.nio.charset.Charset;
import javax.swing.BorderFactory;
import javax.swing.Box;
import tim.prune.I18nManager;
import tim.prune.UpdateMessageBroker;
import tim.prune.config.Config;
-import tim.prune.data.Altitude;
import tim.prune.data.AudioClip;
import tim.prune.data.Coordinate;
import tim.prune.data.DataPoint;
import tim.prune.data.RecentFile;
import tim.prune.data.Timestamp;
import tim.prune.data.TrackInfo;
+import tim.prune.data.UnitSetLibrary;
import tim.prune.gui.DialogCloser;
import tim.prune.load.GenericFileFilter;
import tim.prune.save.xml.GpxCacherList;
private JPanel _encodingsPanel = null;
private JRadioButton _useSystemRadio = null, _forceUtf8Radio = null;
private File _exportFile = null;
- private static String _systemEncoding = null;
/** this program name */
private static final String GPX_CREATOR = "GpsPrune v" + GpsPrune.VERSION_NUMBER + " activityworkshop.net";
_dialog = new JDialog(_parentFrame, I18nManager.getText(getNameKey()), true);
_dialog.setLocationRelativeTo(_parentFrame);
_dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
- _systemEncoding = getSystemEncoding();
_dialog.getContentPane().add(makeDialogComponents());
_dialog.pack();
}
_pointTypeSelector.init(_app.getTrackInfo());
- _encodingsPanel.setVisible(!isSystemUtf8());
- if (!isSystemUtf8()) {
+ _encodingsPanel.setVisible(!XmlUtils.isSystemUtf8());
+ if (!XmlUtils.isSystemUtf8())
+ {
+ String systemEncoding = XmlUtils.getSystemEncoding();
_useSystemRadio.setText(I18nManager.getText("dialog.exportgpx.encoding.system")
- + " (" + (_systemEncoding == null ? "unknown" : _systemEncoding) + ")");
+ + " (" + (systemEncoding == null ? "unknown" : systemEncoding) + ")");
}
_dialog.setVisible(true);
}
mainPanel.add(checkPanel);
// panel for selecting character encoding
_encodingsPanel = new JPanel();
- if (!isSystemUtf8())
+ if (!XmlUtils.isSystemUtf8())
{
// only add this panel if system isn't utf8 (or can't be identified yet)
_encodingsPanel.setBorder(BorderFactory.createCompoundBorder(
final String gpxHeader = getGpxHeaderString(inGpxCachers);
final boolean isVersion1_1 = (gpxHeader.toUpperCase().indexOf("GPX/1/1") > 0);
inWriter.write(gpxHeader);
- // Name field
- String trackName = (inName != null && !inName.equals("")) ? inName : "GpsPruneTrack";
- writeNameAndDescription(inWriter, inName, inDesc, isVersion1_1);
+ // name and description
+ String trackName = (inName != null && !inName.equals("")) ? XmlUtils.fixCdata(inName) : "GpsPruneTrack";
+ String desc = (inDesc != null && !inDesc.equals("")) ? XmlUtils.fixCdata(inDesc) : "Export from GpsPrune";
+ writeNameAndDescription(inWriter, trackName, desc, isVersion1_1);
- int i = 0;
DataPoint point = null;
final boolean exportTrackpoints = inSaveFlags[0];
final boolean exportWaypoints = inSaveFlags[1];
// Loop over waypoints
final int numPoints = inInfo.getTrack().getNumPoints();
int numSaved = 0;
- for (i=0; i<numPoints; i++)
+ for (int i=0; i<numPoints; i++)
{
point = inInfo.getTrack().getPoint(i);
if (!exportSelection || (i>=selStart && i<=selEnd))
if (!exportTimestamps) {
pointSource = stripTime(pointSource);
}
+ inWriter.write('\t');
inWriter.write(pointSource);
inWriter.write('\n');
}
exportAudios, exportTimestamps, true, inGpxCachers, "<rtept", "\t<rte><number>1</number>\n",
null, "\t</rte>\n");
// Output all track points, if any
- String trackStart = "\t<trk><name>" + trackName + "</name><number>1</number><trkseg>\n";
+ String trackStart = "\t<trk>\n\t\t<name>" + trackName + "</name>\n\t\t<number>1</number>\n\t\t<trkseg>\n";
numSaved += writeTrackPoints(inWriter, inInfo, exportSelection, exportTrackpoints, exportPhotos,
exportAudios, exportTimestamps, false, inGpxCachers, "<trkpt", trackStart,
- "\t</trkseg>\n\t<trkseg>\n", "\t</trkseg></trk>\n");
+ "\t</trkseg>\n\t<trkseg>\n", "\t\t</trkseg>\n\t</trk>\n");
}
inWriter.write("</gpx>\n");
private static void writeNameAndDescription(OutputStreamWriter inWriter,
String inName, String inDesc, boolean inIsVersion1_1) throws IOException
{
- String desc = (inDesc != null && !inDesc.equals("")) ? inDesc : "Export from GpsPrune";
// Position of name and description fields needs to be different for GPX1.0 and GPX1.1
if (inIsVersion1_1)
{
}
if (inName != null && !inName.equals(""))
{
- inWriter.write("\t\t<name>");
+ if (inIsVersion1_1) {inWriter.write('\t');}
+ inWriter.write("\t<name>");
inWriter.write(inName);
inWriter.write("</name>\n");
}
- inWriter.write("\t\t<desc>");
- inWriter.write(desc);
+ if (inIsVersion1_1) {inWriter.write('\t');}
+ inWriter.write("\t<desc>");
+ inWriter.write(inDesc);
inWriter.write("</desc>\n");
if (inIsVersion1_1)
{
// get the source from the point (if any)
String pointSource = getPointSource(inCachers, point);
// Clear point source if it's the wrong type of point (eg changed from waypoint or route point)
- if (pointSource != null && !pointSource.toLowerCase().startsWith(inPointTag)) {pointSource = null;}
+ if (pointSource != null && !pointSource.trim().toLowerCase().startsWith(inPointTag)) {
+ pointSource = null;
+ }
if (pointSource != null || !inOnlyCopies)
{
// restart track segment if necessary
// Point has been modified - maybe it's possible to modify the source
source = replaceGpxTags(source, "lat=\"", "\"", inPoint.getLatitude().output(Coordinate.FORMAT_DECIMAL_FORCE_POINT));
source = replaceGpxTags(source, "lon=\"", "\"", inPoint.getLongitude().output(Coordinate.FORMAT_DECIMAL_FORCE_POINT));
- source = replaceGpxTags(source, "<ele>", "</ele>", inPoint.getAltitude().getStringValue(Altitude.Format.METRES));
- source = replaceGpxTags(source, "<time>", "</time>", inPoint.getTimestamp().getText(Timestamp.FORMAT_ISO_8601));
+ source = replaceGpxTags(source, "<ele>", "</ele>", inPoint.getAltitude().getStringValue(UnitSetLibrary.UNITS_METRES));
+ source = replaceGpxTags(source, "<time>", "</time>", inPoint.getTimestamp().getText(Timestamp.Format.ISO8601));
if (inPoint.isWaypoint())
{
- source = replaceGpxTags(source, "<name>", "</name>", inPoint.getWaypointName());
- source = replaceGpxTags(source, "<description>", "</description>",
+ source = replaceGpxTags(source, "<name>", "</name>", XmlUtils.fixCdata(inPoint.getWaypointName()));
+ if (source != null)
+ {
+ source = source.replaceAll("<description>", "<desc>").replaceAll("</description>", "</desc>");
+ }
+ source = replaceGpxTags(source, "<desc>", "</desc>",
XmlUtils.fixCdata(inPoint.getFieldValue(Field.DESCRIPTION)));
}
// photo / audio links
*/
private static String getXmlHeaderString(OutputStreamWriter inWriter)
{
- return "<?xml version=\"1.0\" encoding=\"" + getEncoding(inWriter) + "\"?>\n";
- }
-
-
- /**
- * Get the default system encoding using a writer
- * @param inWriter writer object
- * @return string defining encoding
- */
- private static String getEncoding(OutputStreamWriter inWriter)
- {
- String encoding = inWriter.getEncoding();
- try {
- encoding = Charset.forName(encoding).name();
- }
- catch (Exception e) {} // ignore failure to find encoding
- return encoding;
- }
-
-
- /**
- * Use a temporary file to obtain the name of the default system encoding
- * @return name of default system encoding, or null if write failed
- */
- private static String getSystemEncoding()
- {
- File tempFile = null;
- String encoding = null;
- try
- {
- tempFile = File.createTempFile("prune", null);
- OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(tempFile));
- encoding = getEncoding(writer);
- writer.close();
- }
- catch (IOException e) {} // value stays null
- // Delete temp file
- if (tempFile != null && tempFile.exists()) {
- if (!tempFile.delete()) {
- System.err.println("Cannot delete temp file: " + tempFile.getAbsolutePath());
- }
- }
- // If writing failed (eg permissions) then just ask system for default
- if (encoding == null) encoding = Charset.defaultCharset().name();
- return encoding;
+ return "<?xml version=\"1.0\" encoding=\"" + XmlUtils.getEncoding(inWriter) + "\"?>\n";
}
- /**
- * Creates temp file if necessary to check system encoding
- * @return true if system uses UTF-8 by default
- */
- private static boolean isSystemUtf8()
- {
- if (_systemEncoding == null) _systemEncoding = getSystemEncoding();
- return (_systemEncoding != null && _systemEncoding.toUpperCase().equals("UTF-8"));
- }
/**
* Get the header string for the gpx tag
if (inCachers != null) {gpxHeader = inCachers.getFirstHeader();}
if (gpxHeader == null || gpxHeader.length() < 5)
{
+ // TODO: Consider changing this to default to GPX 1.1
// Create default (1.0) header
gpxHeader = "<gpx version=\"1.0\" creator=\"" + GPX_CREATOR
+ "\"\n xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"
if (inPoint.hasAltitude())
{
inWriter.write("\t\t<ele>");
- inWriter.write("" + inPoint.getAltitude().getStringValue(Altitude.Format.METRES));
+ inWriter.write("" + inPoint.getAltitude().getStringValue(UnitSetLibrary.UNITS_METRES));
inWriter.write("</ele>\n");
}
// timestamp if available (point might have timestamp and then be turned into a waypoint)
if (inPoint.hasTimestamp() && inTimestamps)
{
inWriter.write("\t\t<time>");
- inWriter.write(inPoint.getTimestamp().getText(Timestamp.FORMAT_ISO_8601));
+ inWriter.write(inPoint.getTimestamp().getText(Timestamp.Format.ISO8601));
inWriter.write("</time>\n");
}
// write waypoint name after elevation and time
inWriter.write("\t\t<name>");
- inWriter.write(inPoint.getWaypointName().trim());
+ inWriter.write(XmlUtils.fixCdata(inPoint.getWaypointName().trim()));
inWriter.write("</name>\n");
// description, if any
String desc = XmlUtils.fixCdata(inPoint.getFieldValue(Field.DESCRIPTION));
if (desc != null && !desc.equals(""))
{
- inWriter.write("\t\t<description>");
+ inWriter.write("\t\t<desc>");
inWriter.write(desc);
- inWriter.write("</description>\n");
+ inWriter.write("</desc>\n");
}
// Media links, if any
if (inPhoto && inPoint.getPhoto() != null)
boolean inExportPhoto, boolean inExportAudio)
throws IOException
{
- inWriter.write("\t\t<trkpt lat=\"");
+ inWriter.write("\t\t\t<trkpt lat=\"");
inWriter.write(inPoint.getLatitude().output(Coordinate.FORMAT_DECIMAL_FORCE_POINT));
inWriter.write("\" lon=\"");
inWriter.write(inPoint.getLongitude().output(Coordinate.FORMAT_DECIMAL_FORCE_POINT));
- inWriter.write("\">");
+ inWriter.write("\">\n");
// altitude
if (inPoint.hasAltitude())
{
- inWriter.write("<ele>");
- inWriter.write("" + inPoint.getAltitude().getStringValue(Altitude.Format.METRES));
- inWriter.write("</ele>");
+ inWriter.write("\t\t\t\t<ele>");
+ inWriter.write("" + inPoint.getAltitude().getStringValue(UnitSetLibrary.UNITS_METRES));
+ inWriter.write("</ele>\n");
}
// timestamp if available (and selected)
if (inPoint.hasTimestamp() && inTimestamps)
{
- inWriter.write("<time>");
- inWriter.write(inPoint.getTimestamp().getText(Timestamp.FORMAT_ISO_8601));
- inWriter.write("</time>");
+ inWriter.write("\t\t\t\t<time>");
+ inWriter.write(inPoint.getTimestamp().getText(Timestamp.Format.ISO8601));
+ inWriter.write("</time>\n");
}
// photo, audio
if (inPoint.getPhoto() != null && inExportPhoto) {
if (inPoint.getAudio() != null && inExportAudio) {
inWriter.write(makeMediaLink(inPoint.getAudio()));
}
- inWriter.write("</trkpt>\n");
+ inWriter.write("\t\t\t</trkpt>\n");
}
*/
private static String stripTime(String inPointSource)
{
- return inPointSource.replaceAll("<time>.*?</time>", "");
+ return inPointSource.replaceAll("[ \t]*<time>.*?</time>", "");
}
}