X-Git-Url: https://gitweb.fperrin.net/?p=GpsPrune.git;a=blobdiff_plain;f=tim%2Fprune%2Fsave%2FGpxExporter.java;h=19007ac920dc33c45c2522def2adb648ea1368c7;hp=0e028d18ddd9abff7bf41125ff8018f997c1afa4;hb=4d5796d02a15808311c09448d79e6e7d1de9d636;hpb=ca9bdb3916f9c39adbbf95d06ac95c21dafbb4e6 diff --git a/tim/prune/save/GpxExporter.java b/tim/prune/save/GpxExporter.java index 0e028d1..19007ac 100644 --- a/tim/prune/save/GpxExporter.java +++ b/tim/prune/save/GpxExporter.java @@ -11,75 +11,105 @@ 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; import javax.swing.BoxLayout; +import javax.swing.ButtonGroup; import javax.swing.JButton; +import javax.swing.JCheckBox; import javax.swing.JDialog; import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; +import javax.swing.JRadioButton; import javax.swing.JTextField; -import javax.swing.filechooser.FileFilter; +import javax.swing.border.EtchedBorder; -import tim.prune.GpsPruner; +import tim.prune.App; +import tim.prune.GenericFunction; +import tim.prune.GpsPrune; 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.Field; +import tim.prune.data.MediaObject; +import tim.prune.data.Photo; +import tim.prune.data.RecentFile; import tim.prune.data.Timestamp; -import tim.prune.data.Track; import tim.prune.data.TrackInfo; +import tim.prune.gui.DialogCloser; +import tim.prune.load.GenericFileFilter; +import tim.prune.save.xml.GpxCacherList; +import tim.prune.save.xml.XmlUtils; + /** * Class to export track information * into a specified Gpx file */ -public class GpxExporter implements Runnable +public class GpxExporter extends GenericFunction implements Runnable { - private JFrame _parentFrame = null; - private Track _track = null; + private TrackInfo _trackInfo = null; private JDialog _dialog = null; private JTextField _nameField = null; private JTextField _descriptionField = null; - private JFileChooser _fileChooser = null; + private PointTypeSelector _pointTypeSelector = null; + private JCheckBox _timestampsCheckbox = null; + private JCheckBox _copySourceCheckbox = null; + private JPanel _encodingsPanel = null; + private JRadioButton _useSystemRadio = null, _forceUtf8Radio = null; private File _exportFile = null; + private static String _systemEncoding = null; - /** version number of Gpx */ - private static final String GPX_VERSION_NUMBER = "1.1"; /** this program name */ - private static final String GPX_CREATOR = "Prune v" + GpsPruner.VERSION_NUMBER + " activityworkshop.net"; + private static final String GPX_CREATOR = "GpsPrune v" + GpsPrune.VERSION_NUMBER + " activityworkshop.net"; /** - * Constructor giving frame and track - * @param inParentFrame parent frame - * @param inTrackInfo track info object to save + * Constructor + * @param inApp app object */ - public GpxExporter(JFrame inParentFrame, TrackInfo inTrackInfo) + public GpxExporter(App inApp) { - _parentFrame = inParentFrame; - _track = inTrackInfo.getTrack(); + super(inApp); + _trackInfo = inApp.getTrackInfo(); } + /** Get name key */ + public String getNameKey() { + return "function.exportgpx"; + } /** * Show the dialog to select options and export file */ - public void showDialog() + public void begin() { // Make dialog window if (_dialog == null) { - _dialog = new JDialog(_parentFrame, I18nManager.getText("dialog.exportgpx.title"), true); + _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(); } - _dialog.show(); + _pointTypeSelector.init(_app.getTrackInfo()); + _encodingsPanel.setVisible(!isSystemUtf8()); + if (!isSystemUtf8()) { + _useSystemRadio.setText(I18nManager.getText("dialog.exportgpx.encoding.system") + + " (" + (_systemEncoding == null ? "unknown" : _systemEncoding) + ")"); + } + _dialog.setVisible(true); } @@ -93,7 +123,7 @@ public class GpxExporter implements Runnable dialogPanel.setLayout(new BorderLayout()); JPanel mainPanel = new JPanel(); mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS)); - // Make a central panel with the text boxes + // Make a panel for the name/desc text boxes JPanel descPanel = new JPanel(); descPanel.setLayout(new GridLayout(2, 2)); descPanel.add(new JLabel(I18nManager.getText("dialog.exportgpx.name"))); @@ -103,8 +133,45 @@ public class GpxExporter implements Runnable _descriptionField = new JTextField(10); descPanel.add(_descriptionField); mainPanel.add(descPanel); + mainPanel.add(Box.createVerticalStrut(5)); + // point type selection (track points, waypoints, photo points) + _pointTypeSelector = new PointTypeSelector(); + mainPanel.add(_pointTypeSelector); + // checkboxes for timestamps and copying + JPanel checkPanel = new JPanel(); + _timestampsCheckbox = new JCheckBox(I18nManager.getText("dialog.exportgpx.includetimestamps")); + _timestampsCheckbox.setSelected(true); + checkPanel.add(_timestampsCheckbox); + _copySourceCheckbox = new JCheckBox(I18nManager.getText("dialog.exportgpx.copysource")); + _copySourceCheckbox.setSelected(true); + checkPanel.add(_copySourceCheckbox); + mainPanel.add(checkPanel); + // panel for selecting character encoding + _encodingsPanel = new JPanel(); + if (!isSystemUtf8()) + { + // only add this panel if system isn't utf8 (or can't be identified yet) + _encodingsPanel.setBorder(BorderFactory.createCompoundBorder( + BorderFactory.createEtchedBorder(EtchedBorder.LOWERED), BorderFactory.createEmptyBorder(4, 4, 4, 4))); + _encodingsPanel.setLayout(new BorderLayout()); + _encodingsPanel.add(new JLabel(I18nManager.getText("dialog.exportgpx.encoding")), BorderLayout.NORTH); + JPanel radioPanel = new JPanel(); + radioPanel.setLayout(new FlowLayout()); + ButtonGroup radioGroup = new ButtonGroup(); + _useSystemRadio = new JRadioButton(I18nManager.getText("dialog.exportgpx.encoding.system")); + _forceUtf8Radio = new JRadioButton(I18nManager.getText("dialog.exportgpx.encoding.utf8")); + radioGroup.add(_useSystemRadio); + radioGroup.add(_forceUtf8Radio); + radioPanel.add(_useSystemRadio); + radioPanel.add(_forceUtf8Radio); + _useSystemRadio.setSelected(true); + _encodingsPanel.add(radioPanel, BorderLayout.CENTER); + mainPanel.add(_encodingsPanel); + } 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)); @@ -120,13 +187,13 @@ public class GpxExporter 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(); } }); buttonPanel.add(cancelButton); dialogPanel.add(buttonPanel, BorderLayout.SOUTH); + dialogPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 15)); return dialogPanel; } @@ -136,31 +203,49 @@ public class GpxExporter implements Runnable */ private void startExport() { - // OK pressed, so choose output file - if (_fileChooser == null) - {_fileChooser = new JFileChooser();} - _fileChooser.setDialogType(JFileChooser.SAVE_DIALOG); - _fileChooser.setFileFilter(new FileFilter() { - public boolean accept(File f) - { - return (f != null && (f.isDirectory() - || f.getName().toLowerCase().endsWith(".gpx"))); - } - public String getDescription() - { - return I18nManager.getText("dialog.exportgpx.filetype"); - } - }); - _fileChooser.setAcceptAllFileFilterUsed(false); + // OK pressed, so check selections + if (!_pointTypeSelector.getAnythingSelected()) + { + JOptionPane.showMessageDialog(_parentFrame, I18nManager.getText("dialog.save.notypesselected"), + I18nManager.getText("dialog.saveoptions.title"), JOptionPane.WARNING_MESSAGE); + return; + } + // Choose output file + File saveFile = chooseGpxFile(_parentFrame); + if (saveFile != null) + { + // New file or overwrite confirmed, so initiate export in separate thread + _exportFile = saveFile; + new Thread(this).start(); + } + } + + + /** + * Select a GPX file to save to + * @param inParentFrame parent frame for file chooser dialog + * @return selected File, or null if selection cancelled + */ + public static File chooseGpxFile(JFrame inParentFrame) + { + File saveFile = null; + JFileChooser fileChooser = new JFileChooser(); + fileChooser.setDialogType(JFileChooser.SAVE_DIALOG); + fileChooser.setFileFilter(new GenericFileFilter("filetype.gpx", new String[] {"gpx"})); + fileChooser.setAcceptAllFileFilterUsed(false); + // start from directory in config which should be set + String configDir = Config.getConfigString(Config.KEY_TRACK_DIR); + if (configDir != null) {fileChooser.setCurrentDirectory(new File(configDir));} + // Allow choose again if an existing file is selected boolean chooseAgain = false; do { chooseAgain = false; - if (_fileChooser.showSaveDialog(_parentFrame) == JFileChooser.APPROVE_OPTION) + if (fileChooser.showSaveDialog(inParentFrame) == JFileChooser.APPROVE_OPTION) { // OK pressed and file chosen - File file = _fileChooser.getSelectedFile(); + File file = fileChooser.getSelectedFile(); // Check file extension if (!file.getName().toLowerCase().endsWith(".gpx")) { @@ -168,22 +253,23 @@ public class GpxExporter implements Runnable } // Check if file exists and if necessary prompt for overwrite Object[] buttonTexts = {I18nManager.getText("button.overwrite"), I18nManager.getText("button.cancel")}; - if (!file.exists() || JOptionPane.showOptionDialog(_parentFrame, + if (!file.exists() || JOptionPane.showOptionDialog(inParentFrame, I18nManager.getText("dialog.save.overwrite.text"), I18nManager.getText("dialog.save.overwrite.title"), JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE, null, buttonTexts, buttonTexts[1]) == JOptionPane.YES_OPTION) { - // New file or overwrite confirmed, so initiate export in separate thread - _exportFile = file; - new Thread(this).start(); + // new file or overwrite confirmed + saveFile = file; } else { + // file exists and overwrite cancelled - select again chooseAgain = true; } } } while (chooseAgain); + return saveFile; } @@ -192,17 +278,34 @@ public class GpxExporter 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 { - // normal writing to file - writer = new OutputStreamWriter(new FileOutputStream(_exportFile)); + // normal writing to file - firstly specify UTF8 encoding if requested + if (_forceUtf8Radio != null && _forceUtf8Radio.isSelected()) + writer = new OutputStreamWriter(new FileOutputStream(_exportFile), "UTF-8"); + else + writer = new OutputStreamWriter(new FileOutputStream(_exportFile)); + final boolean[] saveFlags = {_pointTypeSelector.getTrackpointsSelected(), _pointTypeSelector.getWaypointsSelected(), + _pointTypeSelector.getPhotopointsSelected(), _pointTypeSelector.getAudiopointsSelected(), + _pointTypeSelector.getJustSelection(), _timestampsCheckbox.isSelected()}; // write file - int numPoints = exportData(writer); + final int numPoints = exportData(writer, _trackInfo, _nameField.getText(), + _descriptionField.getText(), saveFlags, gpxCachers); // close file writer.close(); + // Store directory in config for later + Config.setConfigString(Config.KEY_TRACK_DIR, _exportFile.getParentFile().getAbsolutePath()); + // Add to recent file list + Config.getRecentFileList().addFile(new RecentFile(_exportFile, true)); // Show confirmation + UpdateMessageBroker.informSubscribers(); UpdateMessageBroker.informSubscribers(I18nManager.getText("confirm.save.ok1") + " " + numPoints + " " + I18nManager.getText("confirm.save.ok2") + " " + _exportFile.getAbsolutePath()); @@ -229,75 +332,375 @@ public class GpxExporter implements Runnable /** * Export the information to the given writer * @param inWriter writer object + * @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, audios, selection, timestamps + * @param inGpxCachers list of Gpx cachers containing input data * @return number of points written + * @throws IOException if io errors occur on write */ - private int exportData(OutputStreamWriter inWriter) throws IOException + public static int exportData(OutputStreamWriter inWriter, TrackInfo inInfo, String inName, + String inDesc, boolean[] inSaveFlags, GpxCacherList inGpxCachers) throws IOException { - inWriter.write("\n\n"); + // Write or copy headers + inWriter.write(getXmlHeaderString(inWriter)); + final String gpxHeader = getGpxHeaderString(inGpxCachers); + final boolean isVersion1_1 = (gpxHeader.toUpperCase().indexOf("GPX/1/1") > 0); + inWriter.write(gpxHeader); // Name field - if (_nameField != null && _nameField.getText() != null && !_nameField.getText().equals("")) + String trackName = (inName != null && !inName.equals("")) ? inName : "GpsPruneTrack"; + writeNameAndDescription(inWriter, inName, inDesc, isVersion1_1); + + int i = 0; + DataPoint point = null; + final boolean exportTrackpoints = inSaveFlags[0]; + final boolean exportWaypoints = inSaveFlags[1]; + final boolean exportPhotos = inSaveFlags[2]; + final boolean exportAudios = inSaveFlags[3]; + final boolean exportSelection = inSaveFlags[4]; + final boolean exportTimestamps = inSaveFlags[5]; + // Examine selection + int selStart = -1, selEnd = -1; + if (exportSelection) { + selStart = inInfo.getSelection().getStart(); + selEnd = inInfo.getSelection().getEnd(); + } + // Loop over waypoints + final int numPoints = inInfo.getTrack().getNumPoints(); + int numSaved = 0; + for (i=0; i"); - inWriter.write(_nameField.getText()); - inWriter.write("\n"); + point = inInfo.getTrack().getPoint(i); + if (!exportSelection || (i>=selStart && i<=selEnd)) + { + // Make a wpt element for each waypoint + if (point.isWaypoint() && exportWaypoints) + { + 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'); + } + else { + exportWaypoint(point, inWriter, exportTimestamps, exportPhotos, exportAudios); + } + numSaved++; + } + } } - // Description field - inWriter.write("\t"); - if (_descriptionField != null && _descriptionField.getText() != null && !_descriptionField.getText().equals("")) + // Export both route points and then track points + if (exportTrackpoints || exportPhotos || exportAudios) + { + // Output all route points (if any) + numSaved += writeTrackPoints(inWriter, inInfo, exportSelection, exportTrackpoints, exportPhotos, + exportAudios, exportTimestamps, true, inGpxCachers, "1\n", + null, "\t\n"); + // Output all track points, if any + String trackStart = "\t" + trackName + "1\n"; + numSaved += writeTrackPoints(inWriter, inInfo, exportSelection, exportTrackpoints, exportPhotos, + exportAudios, exportTimestamps, false, inGpxCachers, "\n\t\n", "\t\n"); + } + + inWriter.write("\n"); + return numSaved; + } + + + /** + * 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) { - inWriter.write(_descriptionField.getText()); + // GPX 1.1 has the name and description inside a metadata tag + inWriter.write("\t\n"); } - else + if (inName != null && !inName.equals("")) { - inWriter.write("Export from Prune"); + inWriter.write("\t\t"); + inWriter.write(inName); + inWriter.write("\n"); } + inWriter.write("\t\t"); + inWriter.write(desc); inWriter.write("\n"); + if (inIsVersion1_1) + { + inWriter.write("\t\n"); + } + } - int i = 0; - DataPoint point = null; - boolean hasTrackpoints = false; - // Loop over waypoints - int numPoints = _track.getNumPoints(); - for (i=0; i=selStart && i<=selEnd)) && !point.isWaypoint()) { - exportWaypoint(point, inWriter); + 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); + // 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)) { + inWriter.write(inSegmentTag); + } + if (numSaved == 0) {inWriter.write(inStartTag);} + if (pointSource != null) + { + // If timestamps checkbox is off, strip the time + if (!exportTimestamps) { + pointSource = stripTime(pointSource); + } + inWriter.write(pointSource); + inWriter.write('\n'); + } + else { + if (!inOnlyCopies) {exportTrackpoint(point, inWriter, exportTimestamps, inExportPhotos, inExportAudios);} + } + numSaved++; + } + } } - else - { - hasTrackpoints = true; + } + if (numSaved > 0) {inWriter.write(inEndTag);} + return numSaved; + } + + + /** + * Get the point source for the specified point + * @param inCachers list of GPX cachers to ask for source + * @param inPoint point object + * @return xml source if available, or null otherwise + */ + private static String getPointSource(GpxCacherList inCachers, DataPoint inPoint) + { + if (inCachers == null || inPoint == null) {return null;} + String source = inCachers.getSourceString(inPoint); + if (source == null || !inPoint.isModified()) {return source;} + // 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, "", "", inPoint.getAltitude().getStringValue(Altitude.Format.METRES)); + source = replaceGpxTags(source, "", inPoint.getTimestamp().getText(Timestamp.FORMAT_ISO_8601)); + if (inPoint.isWaypoint()) + { + source = replaceGpxTags(source, "", "", inPoint.getWaypointName()); + source = replaceGpxTags(source, "", "", + XmlUtils.fixCdata(inPoint.getFieldValue(Field.DESCRIPTION))); + } + // photo / audio links + if (source != null && (inPoint.hasMedia() || source.indexOf("") > 0)) { + source = replaceMediaLinks(source, makeMediaLink(inPoint)); + } + return source; + } + + /** + * Replace the given value into the given XML string + * @param inSource source XML for point + * @param inStartTag start tag for field + * @param inEndTag end tag for field + * @param inValue value to replace between start tag and end tag + * @return modified String, or null if not possible + */ + private static String replaceGpxTags(String inSource, String inStartTag, String inEndTag, String inValue) + { + if (inSource == null) {return null;} + // Look for start and end tags within source + final int startPos = inSource.indexOf(inStartTag); + final int endPos = inSource.indexOf(inEndTag, startPos+inStartTag.length()); + if (startPos > 0 && endPos > 0) + { + String origValue = inSource.substring(startPos + inStartTag.length(), endPos); + 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 + inEndTag.length()); + } + else { + // Need to replace value + return inSource.substring(0, startPos+inStartTag.length()) + inValue + inSource.substring(endPos); } } - // Output the track, if there is one - if (hasTrackpoints) + // Value not found for this field in original source + if (inValue == null || inValue.equals("")) {return inSource;} + return null; + } + + + /** + * 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) { - boolean firstPoint = true; - inWriter.write("\t\n"); - // Loop over track points - for (i=0; i\n\t\n"); - } - if (!point.isWaypoint()) { - // restart track segment if necessary - // export the track point - exportTrackpoint(point, inWriter); - firstPoint = false; - } + 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()); } - inWriter.write("\t\n"); } - inWriter.write("\n"); - return numPoints; + // 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) + { + return "\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; + } + + /** + * 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 + * @param inCachers cacher list to ask for headers, if available + * @return header string from cachers or as default + */ + private static String getGpxHeaderString(GpxCacherList inCachers) + { + String gpxHeader = null; + if (inCachers != null) {gpxHeader = inCachers.getFirstHeader();} + if (gpxHeader == null || gpxHeader.length() < 5) + { + // Create default (1.0) header + gpxHeader = "\n"; + } + return gpxHeader + "\n"; } @@ -305,33 +708,71 @@ public class GpxExporter implements Runnable * Export the specified waypoint into the file * @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 void exportWaypoint(DataPoint inPoint, Writer inWriter) throws IOException + private static void exportWaypoint(DataPoint inPoint, Writer inWriter, boolean inTimestamps, + boolean inPhoto, boolean inAudio) + throws IOException { inWriter.write("\t\n"); - inWriter.write("\t\t"); - inWriter.write(inPoint.getWaypointName().trim()); - inWriter.write("\n"); // altitude if available if (inPoint.hasAltitude()) { inWriter.write("\t\t"); - inWriter.write("" + inPoint.getAltitude().getValue(Altitude.FORMAT_METRES)); + inWriter.write("" + inPoint.getAltitude().getStringValue(Altitude.Format.METRES)); inWriter.write("\n"); } - // timestamp if available (point might have altitude and then be turned into a waypoint) - if (inPoint.hasTimestamp()) + // timestamp if available (point might have timestamp and then be turned into a waypoint) + if (inPoint.hasTimestamp() && inTimestamps) { inWriter.write("\t\t\n"); } - // TODO: Include waypt type in Gpx + // write waypoint name after elevation and time + inWriter.write("\t\t"); + inWriter.write(inPoint.getWaypointName().trim()); + inWriter.write("\n"); + // description, if any + String desc = XmlUtils.fixCdata(inPoint.getFieldValue(Field.DESCRIPTION)); + if (desc != null && !desc.equals("")) + { + inWriter.write("\t\t"); + inWriter.write(desc); + 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) + { + type = type.trim(); + if (!type.equals("")) + { + inWriter.write("\t\t"); + inWriter.write(type); + inWriter.write("\n"); + } + } inWriter.write("\t\n"); } @@ -340,28 +781,91 @@ public class GpxExporter implements Runnable * Export the specified trackpoint into the file * @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 void exportTrackpoint(DataPoint inPoint, Writer inWriter) throws IOException + private static void exportTrackpoint(DataPoint inPoint, Writer inWriter, boolean inTimestamps, + boolean inExportPhoto, boolean inExportAudio) + throws IOException { inWriter.write("\t\t"); // altitude if (inPoint.hasAltitude()) { inWriter.write(""); - inWriter.write("" + inPoint.getAltitude().getValue(Altitude.FORMAT_METRES)); + inWriter.write("" + inPoint.getAltitude().getStringValue(Altitude.Format.METRES)); inWriter.write(""); } - // timestamp if available - if (inPoint.hasTimestamp()) + // timestamp if available (and selected) + if (inPoint.hasTimestamp() && inTimestamps) { inWriter.write(""); } + // 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(); + AudioClip 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(MediaObject inMedia) + { + if (inMedia.getFile() != null) + // file link + return "" + inMedia.getName() + ""; + if (inMedia.getUrl() != null) + // url link + return "" + inMedia.getName() + ""; + // 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("", ""); + } }