aboutsummaryrefslogtreecommitdiffstats
path: root/main/src/cgeo/geocaching/export/FieldNotes.java
diff options
context:
space:
mode:
Diffstat (limited to 'main/src/cgeo/geocaching/export/FieldNotes.java')
-rw-r--r--main/src/cgeo/geocaching/export/FieldNotes.java65
1 files changed, 65 insertions, 0 deletions
diff --git a/main/src/cgeo/geocaching/export/FieldNotes.java b/main/src/cgeo/geocaching/export/FieldNotes.java
new file mode 100644
index 0000000..11d725a
--- /dev/null
+++ b/main/src/cgeo/geocaching/export/FieldNotes.java
@@ -0,0 +1,65 @@
+package cgeo.geocaching.export;
+
+import cgeo.geocaching.Geocache;
+import cgeo.geocaching.LogEntry;
+import cgeo.geocaching.files.LocalStorage;
+import cgeo.geocaching.utils.FileUtils;
+import cgeo.geocaching.utils.SynchronizedDateFormat;
+
+import org.apache.commons.lang3.StringUtils;
+
+import java.io.File;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+import java.util.Locale;
+import java.util.TimeZone;
+
+/**
+ * Field Notes are simple plain text files, but poorly documented. Syntax:<br>
+ * <code>GCxxxxx,yyyy-mm-ddThh:mm:ssZ,Found it,"logtext"</code>
+ */
+class FieldNotes {
+
+ private static final SynchronizedDateFormat FIELD_NOTE_DATE_FORMAT = new SynchronizedDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", TimeZone.getTimeZone("UTC"), Locale.US);
+
+ private int size = 0;
+ private final StringBuilder buffer = new StringBuilder();
+
+ void add(final Geocache cache, final LogEntry log) {
+ size++;
+ buffer.append(cache.getGeocode())
+ .append(',')
+ .append(FIELD_NOTE_DATE_FORMAT.format(new Date(log.date)))
+ .append(',')
+ .append(StringUtils.capitalize(log.type.type))
+ .append(",\"")
+ .append(StringUtils.replaceChars(log.log, '"', '\''))
+ .append("\"\n");
+ }
+
+ public String getContent() {
+ return buffer.toString();
+ }
+
+ File writeToDirectory(File exportLocation) {
+ if (!LocalStorage.isExternalStorageAvailable()) {
+ return null;
+ }
+
+ FileUtils.mkdirs(exportLocation);
+
+ final SimpleDateFormat fileNameDateFormat = new SimpleDateFormat("yyyyMMddHHmmss", Locale.US);
+ final File exportFile = new File(exportLocation.toString() + '/' + fileNameDateFormat.format(new Date()) + ".txt");
+
+ if (!FileUtils.writeFileUTF16(exportFile, getContent())) {
+ return null;
+ }
+
+ return exportFile;
+ }
+
+ public int size() {
+ return size;
+ }
+
+}