From e15ba5ce226d0f927f34d03df74bb2a8c8f78b27 Mon Sep 17 00:00:00 2001
From: Thomas Cashman <thomas.cashman@viridiansoftware.com>
Date: Fri, 12 Nov 2021 10:00:38 +0000
Subject: [PATCH] Implement optional binary format for PoFiles

---
 .../main/java/org/mini2Dx/gettext/PoFile.java |  24 ++++
 .../org/mini2Dx/gettext/TranslationEntry.java | 113 ++++++++++++++++++
 .../java/org/mini2Dx/gettext/PoFileTest.java  |  26 ++++
 .../src/test/resources/sample_newline.po      |   5 +
 4 files changed, 168 insertions(+)
 create mode 100644 gettext-lib/src/test/resources/sample_newline.po

diff --git a/gettext-lib/src/main/java/org/mini2Dx/gettext/PoFile.java b/gettext-lib/src/main/java/org/mini2Dx/gettext/PoFile.java
index eb5e5b8..375bc1c 100644
--- a/gettext-lib/src/main/java/org/mini2Dx/gettext/PoFile.java
+++ b/gettext-lib/src/main/java/org/mini2Dx/gettext/PoFile.java
@@ -105,6 +105,30 @@ public void saveTo(File file) throws IOException {
 		printWriter.close();
 	}
 
+	public void saveToBin(File file) throws IOException {
+		final DataOutputStream outputStream = new DataOutputStream(new FileOutputStream(file));
+		outputStream.writeInt(entries.size());
+		for(TranslationEntry translationEntry : entries) {
+			translationEntry.writeTo(outputStream);
+		}
+		outputStream.flush();
+		outputStream.close();
+	}
+
+	public static PoFile readFromBin(Locale locale, InputStream inputStream) throws IOException {
+		final DataInputStream dataInputStream = new DataInputStream(inputStream);
+		final int totalEntries = dataInputStream.readInt();
+
+		final PoFile poFile = new PoFile(locale);
+		for(int i = 0; i < totalEntries; i++) {
+			final TranslationEntry translationEntry = new TranslationEntry();
+			translationEntry.readFrom(dataInputStream);
+			poFile.entries.add(translationEntry);
+		}
+		dataInputStream.close();
+		return poFile;
+	}
+
 	@Override
 	public void enterEntry(GetTextParser.EntryContext ctx) {
 		currentEntry = new TranslationEntry();
diff --git a/gettext-lib/src/main/java/org/mini2Dx/gettext/TranslationEntry.java b/gettext-lib/src/main/java/org/mini2Dx/gettext/TranslationEntry.java
index 278b903..18b5c8c 100644
--- a/gettext-lib/src/main/java/org/mini2Dx/gettext/TranslationEntry.java
+++ b/gettext-lib/src/main/java/org/mini2Dx/gettext/TranslationEntry.java
@@ -15,6 +15,9 @@
  ******************************************************************************/
 package org.mini2Dx.gettext;
 
+import java.io.DataInputStream;
+import java.io.DataOutputStream;
+import java.io.IOException;
 import java.io.PrintWriter;
 import java.util.ArrayList;
 import java.util.List;
@@ -94,6 +97,116 @@ public void writeTo(PrintWriter printWriter) {
 		}
 	}
 
+	public void writeTo(DataOutputStream outputStream) throws IOException {
+		outputStream.writeInt(translatorComments.size());
+		for(String comment : translatorComments) {
+			outputStream.writeUTF(comment);
+		}
+
+		outputStream.writeInt(extractedComments.size());
+		for(String comment : extractedComments) {
+			outputStream.writeUTF(comment);
+		}
+
+		outputStream.writeInt(flags.size());
+		for(String comment : flags) {
+			outputStream.writeUTF(comment);
+		}
+
+		outputStream.writeInt(mergeComments.size());
+		for(String comment : mergeComments) {
+			outputStream.writeUTF(comment);
+		}
+
+		if(reference == null) {
+			outputStream.writeUTF("");
+		} else {
+			outputStream.writeUTF(reference);
+		}
+		if(context == null) {
+			outputStream.writeUTF("");
+		} else {
+			outputStream.writeUTF(context);
+		}
+		if(id == null) {
+			outputStream.writeUTF("");
+		} else {
+			outputStream.writeUTF(id);
+		}
+		if(idPlural == null) {
+			outputStream.writeUTF("");
+		} else {
+			outputStream.writeUTF(idPlural);
+		}
+
+		if(strings.isEmpty()) {
+			if(idPlural != null && !idPlural.isEmpty()) {
+				outputStream.writeInt(3);
+				outputStream.writeUTF("");
+				outputStream.writeUTF("");
+				outputStream.writeUTF("");
+			} else {
+				outputStream.writeInt(1);
+				outputStream.writeUTF("");
+			}
+		} else if(strings.size() > 1) {
+			outputStream.writeInt(strings.size());
+			for(int i = 0; i < strings.size(); i++) {
+				final String str = strings.get(i);
+				if(str == null) {
+					outputStream.writeUTF("");
+				} else {
+					outputStream.writeUTF(str);
+				}
+			}
+		} else {
+			outputStream.writeInt(1);
+			outputStream.writeUTF(strings.get(0));
+		}
+	}
+
+	public void readFrom(DataInputStream inputStream) throws IOException {
+		final int totalTranslatorComments = inputStream.readInt();
+		for(int i = 0; i < totalTranslatorComments; i++) {
+			translatorComments.add(inputStream.readUTF());
+		}
+
+		final int totalExtractedComments = inputStream.readInt();
+		for(int i = 0; i < totalExtractedComments; i++) {
+			extractedComments.add(inputStream.readUTF());
+		}
+
+		final int totalFlags = inputStream.readInt();
+		for(int i = 0; i < totalFlags; i++) {
+			flags.add(inputStream.readUTF());
+		}
+
+		final int totalMergeComments = inputStream.readInt();
+		for(int i = 0; i < totalMergeComments; i++) {
+			mergeComments.add(inputStream.readUTF());
+		}
+
+		reference = inputStream.readUTF();
+		context = inputStream.readUTF();
+		id = inputStream.readUTF();
+		idPlural = inputStream.readUTF();
+
+		if(reference.isEmpty()) {
+			reference = null;
+		}
+		if(context.isEmpty()) {
+			context = null;
+		}
+		if(idPlural.isEmpty()) {
+			idPlural = null;
+		}
+
+		final int totalStrings = inputStream.readInt();
+		for(int i = 0; i < totalStrings; i++) {
+			strings.add(inputStream.readUTF());
+		}
+	}
+
 	public List<String> getTranslatorComments() {
 		return translatorComments;
 	}
diff --git a/gettext-lib/src/test/java/org/mini2Dx/gettext/PoFileTest.java b/gettext-lib/src/test/java/org/mini2Dx/gettext/PoFileTest.java
index a52bbf8..5214b81 100644
--- a/gettext-lib/src/test/java/org/mini2Dx/gettext/PoFileTest.java
+++ b/gettext-lib/src/test/java/org/mini2Dx/gettext/PoFileTest.java
@@ -4,9 +4,11 @@
 package org.mini2Dx.gettext;
 
 import org.junit.Assert;
+import org.junit.Ignore;
 import org.junit.Test;
 
 import java.io.File;
+import java.io.FileInputStream;
 import java.io.IOException;
 import java.nio.file.Files;
 import java.util.Locale;
@@ -28,4 +30,28 @@ public void testReadWritePoFile() throws IOException {
 			Assert.assertEquals(expectedEntry, resultEntry);
 		}
 	}
+
+	@Test
+	public void testReadWritePoFileBinary() throws IOException {
+		final PoFile expected = new PoFile(Locale.ENGLISH, GetTextTest.class.getResourceAsStream("/sample_en.po"));
+		final File tmpFile = Files.createTempFile("", ".po").toFile();
+		expected.saveToBin(tmpFile);
+
+		final PoFile result = PoFile.readFromBin(Locale.ENGLISH, new FileInputStream(tmpFile));
+
+		Assert.assertEquals(expected.getEntries().size(), result.getEntries().size());
+		for(int i = 0; i < expected.getEntries().size(); i++) {
+			final TranslationEntry expectedEntry = expected.getEntries().get(i);
+			final TranslationEntry resultEntry = result.getEntries().get(i);
+			Assert.assertEquals(expectedEntry, resultEntry);
+		}
+	}
+
+	@Ignore
+	@Test
+	public void testReadPoFileWithNewLineInStr() throws IOException {
+		final PoFile poFile = new PoFile(Locale.ENGLISH, GetTextTest.class.getResourceAsStream("/sample_newline.po"));
+		Assert.assertEquals(1, poFile.getEntries().size());
+		Assert.assertEquals("Unknown\nerror", poFile.getEntries().get(0).getId());
+	}
 }
diff --git a/gettext-lib/src/test/resources/sample_newline.po b/gettext-lib/src/test/resources/sample_newline.po
new file mode 100644
index 0000000..f1aa5c9
--- /dev/null
+++ b/gettext-lib/src/test/resources/sample_newline.po
@@ -0,0 +1,5 @@
+#  translator-comments
+#. extracted-comments
+#: src/msgcmp.java:322
+msgid "Unknown\nerror"
+msgstr "Unknown\nerror"
\ No newline at end of file