X-Git-Url: https://gitweb.fperrin.net/?p=GpsPrune.git;a=blobdiff_plain;f=tim%2Fprune%2Fsave%2FGpxExporter.java;h=1b2263bc8260d6dc49115f969de5e9c40be1ffcb;hp=a4ac1a7de2e72ade39734e7f0099fe6e14f5faca;hb=88f2c3647ed9e055090484f01a959d4581f85e7d;hpb=326f489e36aa7f235bc19409a57bf4955cd50f24 diff --git a/tim/prune/save/GpxExporter.java b/tim/prune/save/GpxExporter.java index a4ac1a7..1b2263b 100644 --- a/tim/prune/save/GpxExporter.java +++ b/tim/prune/save/GpxExporter.java @@ -11,7 +11,6 @@ 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; @@ -67,7 +66,6 @@ public class GpxExporter extends GenericFunction implements Runnable 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"; @@ -99,15 +97,16 @@ public class GpxExporter extends GenericFunction implements Runnable _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); } @@ -148,7 +147,7 @@ public class GpxExporter extends GenericFunction implements Runnable 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( @@ -384,6 +383,7 @@ public class GpxExporter extends GenericFunction implements Runnable if (!exportTimestamps) { pointSource = stripTime(pointSource); } + inWriter.write('\t'); inWriter.write(pointSource); inWriter.write('\n'); } @@ -402,10 +402,10 @@ public class GpxExporter extends GenericFunction implements Runnable exportAudios, exportTimestamps, true, inGpxCachers, "1\n", null, "\t\n"); // Output all track points, if any - String trackStart = "\t" + trackName + "1\n"; + String trackStart = "\t\n\t\t" + trackName + "\n\t\t1\n\t\t\n"; numSaved += writeTrackPoints(inWriter, inInfo, exportSelection, exportTrackpoints, exportPhotos, exportAudios, exportTimestamps, false, inGpxCachers, "\n\t\n", "\t\n"); + "\t\n\t\n", "\t\t\n\t\n"); } inWriter.write("\n"); @@ -432,11 +432,13 @@ public class GpxExporter extends GenericFunction implements Runnable } if (inName != null && !inName.equals("")) { - inWriter.write("\t\t"); + if (inIsVersion1_1) {inWriter.write('\t');} + inWriter.write("\t"); inWriter.write(inName); inWriter.write("\n"); } - inWriter.write("\t\t"); + if (inIsVersion1_1) {inWriter.write('\t');} + inWriter.write("\t"); inWriter.write(desc); inWriter.write("\n"); if (inIsVersion1_1) @@ -486,7 +488,9 @@ public class GpxExporter extends GenericFunction implements Runnable // 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 @@ -626,63 +630,10 @@ public class GpxExporter extends GenericFunction implements Runnable */ private static String getXmlHeaderString(OutputStreamWriter inWriter) { - return "\n"; + 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 @@ -790,24 +741,24 @@ public class GpxExporter extends GenericFunction implements Runnable boolean inExportPhoto, boolean inExportAudio) throws IOException { - inWriter.write("\t\t"); + inWriter.write("\">\n"); // altitude if (inPoint.hasAltitude()) { - inWriter.write(""); + inWriter.write("\t\t\t\t"); inWriter.write("" + inPoint.getAltitude().getStringValue(UnitSetLibrary.UNITS_METRES)); - inWriter.write(""); + inWriter.write("\n"); } // timestamp if available (and selected) if (inPoint.hasTimestamp() && inTimestamps) { - inWriter.write("\n"); } // photo, audio if (inPoint.getPhoto() != null && inExportPhoto) { @@ -816,7 +767,7 @@ public class GpxExporter extends GenericFunction implements Runnable if (inPoint.getAudio() != null && inExportAudio) { inWriter.write(makeMediaLink(inPoint.getAudio())); } - inWriter.write("\n"); + inWriter.write("\t\t\t\n"); } @@ -867,6 +818,6 @@ public class GpxExporter extends GenericFunction implements Runnable */ private static String stripTime(String inPointSource) { - return inPointSource.replaceAll("", ""); + return inPointSource.replaceAll("[ \t]*", ""); } }