+ * 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 = "<link";
+ final String ENDTEXT = "</link>";
+ final int startPos = inSource.indexOf(STARTTEXT);
+ final int endPos = inSource.lastIndexOf(ENDTEXT);
+ if (startPos > 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)
+ {
+ 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;
+ }
+
+ /**
+ * 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