X-Git-Url: https://gitweb.fperrin.net/?a=blobdiff_plain;f=tim%2Fprune%2Fsave%2FGpxExporter.java;h=fd4c869c03fac79608b194eb17ca3ffdf0720b08;hb=eebbb64b5d63f9eea43a0dff908c30361a376768;hp=7f75aeb17689397eccef5d1f45ba01a363f4f3c2;hpb=140e9d165f85c3d4f0435a311e091209313faa2a;p=GpsPrune.git
diff --git a/tim/prune/save/GpxExporter.java b/tim/prune/save/GpxExporter.java
index 7f75aeb..fd4c869 100644
--- a/tim/prune/save/GpxExporter.java
+++ b/tim/prune/save/GpxExporter.java
@@ -11,6 +11,7 @@ import java.io.FileOutputStream;
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;
@@ -32,11 +33,15 @@ import tim.prune.I18nManager;
import tim.prune.UpdateMessageBroker;
import tim.prune.config.Config;
import tim.prune.data.Altitude;
+import tim.prune.data.AudioFile;
import tim.prune.data.Coordinate;
import tim.prune.data.DataPoint;
import tim.prune.data.Field;
+import tim.prune.data.MediaFile;
+import tim.prune.data.Photo;
import tim.prune.data.Timestamp;
import tim.prune.data.TrackInfo;
+import tim.prune.gui.DialogCloser;
import tim.prune.load.GenericFileFilter;
import tim.prune.save.xml.GpxCacherList;
@@ -129,6 +134,8 @@ public class GpxExporter extends GenericFunction implements Runnable
mainPanel.add(checkPanel);
dialogPanel.add(mainPanel, BorderLayout.CENTER);
+ // close dialog if escape pressed
+ _nameField.addKeyListener(new DialogCloser(_dialog));
// button panel at bottom
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
@@ -144,8 +151,7 @@ public class GpxExporter extends GenericFunction implements Runnable
buttonPanel.add(okButton);
JButton cancelButton = new JButton(I18nManager.getText("button.cancel"));
cancelButton.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e)
- {
+ public void actionPerformed(ActionEvent e) {
_dialog.dispose();
}
});
@@ -238,9 +244,9 @@ public class GpxExporter extends GenericFunction implements Runnable
{
// normal writing to file
writer = new OutputStreamWriter(new FileOutputStream(_exportFile));
- boolean[] saveFlags = {_pointTypeSelector.getTrackpointsSelected(), _pointTypeSelector.getWaypointsSelected(),
- _pointTypeSelector.getPhotopointsSelected(), _pointTypeSelector.getJustSelection(),
- _timestampsCheckbox.isSelected()};
+ final boolean[] saveFlags = {_pointTypeSelector.getTrackpointsSelected(), _pointTypeSelector.getWaypointsSelected(),
+ _pointTypeSelector.getPhotopointsSelected(), _pointTypeSelector.getAudiopointsSelected(),
+ _pointTypeSelector.getJustSelection(), _timestampsCheckbox.isSelected()};
// write file
final int numPoints = exportData(writer, _trackInfo, _nameField.getText(),
_descriptionField.getText(), saveFlags, _copySourceCheckbox.isSelected());
@@ -279,7 +285,7 @@ public class GpxExporter extends GenericFunction implements Runnable
* @param inInfo track info object
* @param inName name of track (optional)
* @param inDesc description of track (optional)
- * @param inSaveFlags array of booleans to export tracks, waypoints, photos, timestamps
+ * @param inSaveFlags array of booleans to export tracks, waypoints, photos, audios, selection, timestamps
* @param inUseCopy true to copy source if available
* @return number of points written
* @throws IOException if io errors occur on write
@@ -290,8 +296,9 @@ public class GpxExporter extends GenericFunction implements Runnable
// Instantiate source file cachers in case we want to copy output
GpxCacherList gpxCachers = null;
if (inUseCopy) gpxCachers = new GpxCacherList(inInfo.getFileInfo());
- // Write or copy header
- inWriter.write(getHeaderString(gpxCachers));
+ // Write or copy headers
+ inWriter.write(getXmlHeaderString(inWriter));
+ inWriter.write(getGpxHeaderString(gpxCachers));
// Name field
String trackName = "PruneTrack";
if (inName != null && !inName.equals(""))
@@ -303,22 +310,17 @@ public class GpxExporter extends GenericFunction implements Runnable
}
// Description field
inWriter.write("\t");
- if (inDesc != null && !inDesc.equals("")) {
- inWriter.write(inDesc);
- }
- else {
- inWriter.write("Export from Prune");
- }
+ inWriter.write((inDesc != null && !inDesc.equals(""))?inDesc:"Export from Prune");
inWriter.write("\n");
int i = 0;
DataPoint point = null;
- boolean hasTrackpoints = false;
final boolean exportTrackpoints = inSaveFlags[0];
final boolean exportWaypoints = inSaveFlags[1];
final boolean exportPhotos = inSaveFlags[2];
- final boolean exportSelection = inSaveFlags[3];
- final boolean exportTimestamps = inSaveFlags[4];
+ final boolean exportAudios = inSaveFlags[3];
+ final boolean exportSelection = inSaveFlags[4];
+ final boolean exportTimestamps = inSaveFlags[5];
// Examine selection
int selStart = -1, selEnd = -1;
if (exportSelection) {
@@ -342,27 +344,25 @@ public class GpxExporter extends GenericFunction implements Runnable
inWriter.write('\n');
}
else {
- exportWaypoint(point, inWriter, exportTimestamps);
+ exportWaypoint(point, inWriter, exportTimestamps, exportPhotos, exportAudios);
}
numSaved++;
}
}
- else {
- hasTrackpoints = true;
- }
}
}
// Export both route points and then track points
- if (hasTrackpoints && (exportTrackpoints || exportPhotos))
+ if (exportTrackpoints || exportPhotos || exportAudios)
{
// Output all route points (if any)
numSaved += writeTrackPoints(inWriter, inInfo, exportSelection, exportTrackpoints, exportPhotos,
- exportTimestamps, true, gpxCachers, "1\n", null, "\t\n");
+ exportAudios, exportTimestamps, true, gpxCachers, "1\n",
+ null, "\t\n");
// Output all track points, if any
String trackStart = "\t" + trackName + "1\n";
numSaved += writeTrackPoints(inWriter, inInfo, exportSelection, exportTrackpoints, exportPhotos,
- exportTimestamps, false, gpxCachers, "\n\t\n",
- "\t\n");
+ exportAudios, exportTimestamps, false, gpxCachers, "\n\t\n", "\t\n");
}
inWriter.write("\n");
@@ -376,7 +376,8 @@ public class GpxExporter extends GenericFunction implements Runnable
* @param inExportSelection true to just output current selection
* @param inExportTrackpoints true to output track points
* @param inExportPhotos true to output photo points
- * @param exportTimestamps true to include timestamps in export
+ * @param inExportAudios true to output audio points
+ * @param inExportTimestamps true to include timestamps in export
* @param inOnlyCopies true to only export if source can be copied
* @param inCachers list of GpxCachers
* @param inPointTag tag to match for each point
@@ -386,9 +387,9 @@ public class GpxExporter extends GenericFunction implements Runnable
*/
private static int writeTrackPoints(OutputStreamWriter inWriter,
TrackInfo inInfo, boolean inExportSelection, boolean inExportTrackpoints,
- boolean inExportPhotos, boolean exportTimestamps, boolean inOnlyCopies,
- GpxCacherList inCachers, String inPointTag, String inStartTag,
- String inSegmentTag, String inEndTag)
+ boolean inExportPhotos, boolean inExportAudios, boolean exportTimestamps,
+ boolean inOnlyCopies, GpxCacherList inCachers, String inPointTag,
+ String inStartTag, String inSegmentTag, String inEndTag)
throws IOException
{
// Note: far too many input parameters to this method but avoids duplication
@@ -403,13 +404,14 @@ public class GpxExporter extends GenericFunction implements Runnable
DataPoint point = inInfo.getTrack().getPoint(i);
if ((!inExportSelection || (i>=selStart && i<=selEnd)) && !point.isWaypoint())
{
- if ((point.getPhoto()==null && inExportTrackpoints) || (point.getPhoto()!=null && inExportPhotos))
+ if ((point.getPhoto()==null && inExportTrackpoints) || (point.getPhoto()!=null && inExportPhotos)
+ || (point.getAudio()!=null && inExportAudios))
{
// get the source from the point (if any)
String pointSource = getPointSource(inCachers, point);
- boolean writePoint = (pointSource != null && pointSource.toLowerCase().startsWith(inPointTag))
- || (pointSource == null && !inOnlyCopies);
- if (writePoint)
+ // 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 || !inOnlyCopies)
{
// restart track segment if necessary
if ((numSaved > 0) && point.getSegmentStart() && (inSegmentTag != null)) {
@@ -421,7 +423,7 @@ public class GpxExporter extends GenericFunction implements Runnable
inWriter.write('\n');
}
else {
- if (!inOnlyCopies) {exportTrackpoint(point, inWriter, exportTimestamps);}
+ if (!inOnlyCopies) {exportTrackpoint(point, inWriter, exportTimestamps, inExportPhotos, inExportAudios);}
}
numSaved++;
}
@@ -450,6 +452,10 @@ public class GpxExporter extends GenericFunction implements Runnable
source = replaceGpxTags(source, "", "", inPoint.getAltitude().getStringValue(Altitude.Format.METRES));
source = replaceGpxTags(source, "", inPoint.getTimestamp().getText(Timestamp.FORMAT_ISO_8601));
if (inPoint.isWaypoint()) {source = replaceGpxTags(source, "", "", inPoint.getWaypointName());} // only for waypoints
+ // photo / audio links
+ if (source != null && (inPoint.hasMedia() || source.indexOf("") > 0)) {
+ source = replaceMediaLinks(source, makeMediaLink(inPoint));
+ }
return source;
}
@@ -489,11 +495,63 @@ public class GpxExporter extends GenericFunction implements Runnable
}
/**
- * Get the header string for the gpx
+ * Replace the media tags in the given XML string
+ * @param inSource source XML for point
+ * @param inValue value for the current point
+ * @return modified String, or null if not possible
+ */
+ private static String replaceMediaLinks(String inSource, String inValue)
+ {
+ if (inSource == null) {return null;}
+ // Note that this method is very similar to replaceGpxTags except there can be multiple link tags
+ // and the tags must have attributes. So either one heavily parameterized method or two.
+ // Look for start and end tags within source
+ final String STARTTEXT = " 0 && endPos > 0)
+ {
+ String origValue = inSource.substring(startPos, endPos + ENDTEXT.length());
+ if (inValue != null && origValue.equals(inValue)) {
+ // Value unchanged
+ return inSource;
+ }
+ else if (inValue == null || inValue.equals("")) {
+ // Need to delete value
+ return inSource.substring(0, startPos) + inSource.substring(endPos + ENDTEXT.length());
+ }
+ else {
+ // Need to replace value
+ return inSource.substring(0, startPos) + inValue + inSource.substring(endPos + ENDTEXT.length());
+ }
+ }
+ // Value not found for this field in original source
+ if (inValue == null || inValue.equals("")) {return inSource;}
+ return null;
+ }
+
+ /**
+ * Get the header string for the xml document including encoding
+ * @param inWriter writer object
+ * @return header string defining encoding
+ */
+ private static String getXmlHeaderString(OutputStreamWriter inWriter)
+ {
+ String encoding = inWriter.getEncoding();
+ try {
+ encoding = Charset.forName(encoding).name();
+ }
+ catch (Exception e) {} // ignore failure to find encoding
+ return "\n";
+ }
+
+ /**
+ * Get the header string for the gpx tag
* @param inCachers cacher list to ask for headers, if available
* @return header string from cachers or as default
*/
- private static String getHeaderString(GpxCacherList inCachers)
+ private static String getGpxHeaderString(GpxCacherList inCachers)
{
String gpxHeader = null;
if (inCachers != null) {gpxHeader = inCachers.getFirstHeader();}
@@ -505,7 +563,7 @@ public class GpxExporter extends GenericFunction implements Runnable
+ " xmlns=\"http://www.topografix.com/GPX/1/0\""
+ " xsi:schemaLocation=\"http://www.topografix.com/GPX/1/0 http://www.topografix.com/GPX/1/0/gpx.xsd\">\n";
}
- return "\n" + gpxHeader + "\n";
+ return gpxHeader + "\n";
}
/**
@@ -513,9 +571,12 @@ public class GpxExporter extends GenericFunction implements Runnable
* @param inPoint waypoint to export
* @param inWriter writer object
* @param inTimestamps true to export timestamps too
+ * @param inPhoto true to export link to photo
+ * @param inAudio true to export link to audio
* @throws IOException on write failure
*/
- private static void exportWaypoint(DataPoint inPoint, Writer inWriter, boolean inTimestamps)
+ private static void exportWaypoint(DataPoint inPoint, Writer inWriter, boolean inTimestamps,
+ boolean inPhoto, boolean inAudio)
throws IOException
{
inWriter.write("\t");
inWriter.write(inPoint.getWaypointName().trim());
inWriter.write("\n");
+ // Media links, if any
+ if (inPhoto && inPoint.getPhoto() != null)
+ {
+ inWriter.write("\t\t");
+ inWriter.write(makeMediaLink(inPoint.getPhoto()));
+ inWriter.write('\n');
+ }
+ if (inAudio && inPoint.getAudio() != null)
+ {
+ inWriter.write("\t\t");
+ inWriter.write(makeMediaLink(inPoint.getAudio()));
+ inWriter.write('\n');
+ }
// write waypoint type if any
String type = inPoint.getFieldValue(Field.WAYPT_TYPE);
if (type != null)
@@ -562,8 +636,11 @@ public class GpxExporter extends GenericFunction implements Runnable
* @param inPoint trackpoint to export
* @param inWriter writer object
* @param inTimestamps true to export timestamps too
+ * @param inExportPhoto true to export photo link
+ * @param inExportAudio true to export audio link
*/
- private static void exportTrackpoint(DataPoint inPoint, Writer inWriter, boolean inTimestamps)
+ private static void exportTrackpoint(DataPoint inPoint, Writer inWriter, boolean inTimestamps,
+ boolean inExportPhoto, boolean inExportAudio)
throws IOException
{
inWriter.write("\t\t");
}
+ // photo, audio
+ if (inPoint.getPhoto() != null && inExportPhoto) {
+ inWriter.write(makeMediaLink(inPoint.getPhoto()));
+ }
+ if (inPoint.getAudio() != null && inExportAudio) {
+ inWriter.write(makeMediaLink(inPoint.getAudio()));
+ }
inWriter.write("\n");
}
+
+ /**
+ * Make the xml for the media link(s)
+ * @param inPoint point to generate text for
+ * @return link tags, or null if no links
+ */
+ private static String makeMediaLink(DataPoint inPoint)
+ {
+ Photo photo = inPoint.getPhoto();
+ AudioFile audio = inPoint.getAudio();
+ if (photo == null && audio == null) {
+ return null;
+ }
+ String linkText = "";
+ if (photo != null) {
+ linkText = makeMediaLink(photo);
+ }
+ if (audio != null) {
+ linkText += makeMediaLink(audio);
+ }
+ return linkText;
+ }
+
+ /**
+ * Make the media link for a single media item
+ * @param inMedia media item, either photo or audio
+ * @return link for this media
+ */
+ private static String makeMediaLink(MediaFile inMedia)
+ {
+ return "" + inMedia.getFile().getName() + "";
+ }
}