]> gitweb.fperrin.net Git - GpsPrune.git/blobdiff - tim/prune/save/GpxExporter.java
Version 14, October 2012
[GpsPrune.git] / tim / prune / save / GpxExporter.java
index a7f2823c42a2d5dfbd6b5766a4ec5fd619ed3262..19007ac920dc33c45c2522def2adb648ea1368c7 100644 (file)
@@ -278,6 +278,11 @@ public class GpxExporter extends GenericFunction implements Runnable
         */
        public void run()
        {
+               // Instantiate source file cachers in case we want to copy output
+               GpxCacherList gpxCachers = null;
+               if (_copySourceCheckbox.isSelected()) {
+                       gpxCachers = new GpxCacherList(_trackInfo.getFileInfo());
+               }
                OutputStreamWriter writer = null;
                try
                {
@@ -291,7 +296,7 @@ public class GpxExporter extends GenericFunction implements Runnable
                                _pointTypeSelector.getJustSelection(), _timestampsCheckbox.isSelected()};
                        // write file
                        final int numPoints = exportData(writer, _trackInfo, _nameField.getText(),
-                               _descriptionField.getText(), saveFlags, _copySourceCheckbox.isSelected());
+                               _descriptionField.getText(), saveFlags, gpxCachers);
 
                        // close file
                        writer.close();
@@ -331,32 +336,21 @@ public class GpxExporter extends GenericFunction implements Runnable
         * @param inName name of track (optional)
         * @param inDesc description of track (optional)
         * @param inSaveFlags array of booleans to export tracks, waypoints, photos, audios, selection, timestamps
-        * @param inUseCopy true to copy source if available
+        * @param inGpxCachers list of Gpx cachers containing input data
         * @return number of points written
         * @throws IOException if io errors occur on write
         */
        public static int exportData(OutputStreamWriter inWriter, TrackInfo inInfo, String inName,
-               String inDesc, boolean[] inSaveFlags, boolean inUseCopy) throws IOException
+               String inDesc, boolean[] inSaveFlags, GpxCacherList inGpxCachers) throws IOException
        {
-               // Instantiate source file cachers in case we want to copy output
-               GpxCacherList gpxCachers = null;
-               if (inUseCopy) gpxCachers = new GpxCacherList(inInfo.getFileInfo());
                // Write or copy headers
                inWriter.write(getXmlHeaderString(inWriter));
-               inWriter.write(getGpxHeaderString(gpxCachers));
+               final String gpxHeader = getGpxHeaderString(inGpxCachers);
+               final boolean isVersion1_1 = (gpxHeader.toUpperCase().indexOf("GPX/1/1") > 0);
+               inWriter.write(gpxHeader);
                // Name field
-               String trackName = "GpsPruneTrack";
-               if (inName != null && !inName.equals(""))
-               {
-                       trackName = inName;
-                       inWriter.write("\t<name>");
-                       inWriter.write(trackName);
-                       inWriter.write("</name>\n");
-               }
-               // Description field
-               inWriter.write("\t<desc>");
-               inWriter.write((inDesc != null && !inDesc.equals(""))?inDesc:"Export from GpsPrune");
-               inWriter.write("</desc>\n");
+               String trackName = (inName != null && !inName.equals("")) ? inName : "GpsPruneTrack";
+               writeNameAndDescription(inWriter, inName, inDesc, isVersion1_1);
 
                int i = 0;
                DataPoint point = null;
@@ -383,8 +377,13 @@ public class GpxExporter extends GenericFunction implements Runnable
                                // Make a wpt element for each waypoint
                                if (point.isWaypoint() && exportWaypoints)
                                {
-                                       String pointSource = (inUseCopy?getPointSource(gpxCachers, point):null);
-                                       if (pointSource != null) {
+                                       String pointSource = (inGpxCachers == null? null : getPointSource(inGpxCachers, point));
+                                       if (pointSource != null)
+                                       {
+                                               // If timestamp checkbox is off, strip time
+                                               if (!exportTimestamps) {
+                                                       pointSource = stripTime(pointSource);
+                                               }
                                                inWriter.write(pointSource);
                                                inWriter.write('\n');
                                        }
@@ -400,12 +399,12 @@ public class GpxExporter extends GenericFunction implements Runnable
                {
                        // Output all route points (if any)
                        numSaved += writeTrackPoints(inWriter, inInfo, exportSelection, exportTrackpoints, exportPhotos,
-                               exportAudios, exportTimestamps, true, gpxCachers, "<rtept", "\t<rte><number>1</number>\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";
                        numSaved += writeTrackPoints(inWriter, inInfo, exportSelection, exportTrackpoints, exportPhotos,
-                               exportAudios, exportTimestamps, false, gpxCachers, "<trkpt", trackStart,
+                               exportAudios, exportTimestamps, false, inGpxCachers, "<trkpt", trackStart,
                                "\t</trkseg>\n\t<trkseg>\n", "\t</trkseg></trk>\n");
                }
 
@@ -414,6 +413,38 @@ public class GpxExporter extends GenericFunction implements Runnable
        }
 
 
+       /**
+        * Write the name and description according to the GPX version number
+        * @param inWriter writer object
+        * @param inName name, or null if none supplied
+        * @param inDesc description, or null if none supplied
+        * @param inIsVersion1_1 true if gpx version 1.1, false for version 1.0
+        */
+       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)
+               {
+                       // GPX 1.1 has the name and description inside a metadata tag
+                       inWriter.write("\t<metadata>\n");
+               }
+               if (inName != null && !inName.equals(""))
+               {
+                       inWriter.write("\t\t<name>");
+                       inWriter.write(inName);
+                       inWriter.write("</name>\n");
+               }
+               inWriter.write("\t\t<desc>");
+               inWriter.write(desc);
+               inWriter.write("</desc>\n");
+               if (inIsVersion1_1)
+               {
+                       inWriter.write("\t</metadata>\n");
+               }
+       }
+
        /**
         * Loop through the track outputting the relevant track points
         * @param inWriter writer object for output
@@ -463,7 +494,12 @@ public class GpxExporter extends GenericFunction implements Runnable
                                                        inWriter.write(inSegmentTag);
                                                }
                                                if (numSaved == 0) {inWriter.write(inStartTag);}
-                                               if (pointSource != null) {
+                                               if (pointSource != null)
+                                               {
+                                                       // If timestamps checkbox is off, strip the time
+                                                       if (!exportTimestamps) {
+                                                               pointSource = stripTime(pointSource);
+                                                       }
                                                        inWriter.write(pointSource);
                                                        inWriter.write('\n');
                                                }
@@ -606,10 +642,6 @@ public class GpxExporter extends GenericFunction implements Runnable
                        encoding =  Charset.forName(encoding).name();
                }
                catch (Exception e) {} // ignore failure to find encoding
-               // Hack to fix bugs with Mac OSX (which reports MacRoman but is actually UTF-8)
-               if (encoding == null || encoding.toLowerCase().startsWith("macroman")) {
-                       encoding = "UTF-8";
-               }
                return encoding;
        }
 
@@ -825,4 +857,15 @@ public class GpxExporter extends GenericFunction implements Runnable
                // No link available, must have been loaded from zip file - no link possible
                return "";
        }
+
+
+       /**
+        * Strip the time from a GPX point source string
+        * @param inPointSource point source to copy
+        * @return point source with timestamp removed
+        */
+       private static String stripTime(String inPointSource)
+       {
+               return inPointSource.replaceAll("<time>.*?</time>", "");
+       }
 }