Add symmetry and pre-emptively improve performance (A4)
The test for binary dictionary will soon entail decrypting and
uncompressing files if necessary to test for their headers, and will
become much slower than it is. It's better to be able to detect the
XML format too, and leave the slower test for last.
Bug: 7388852
Change-Id: I6b9a7944de80217e1571cab65dcd1cff347b3046
diff --git a/tools/dicttool/src/com/android/inputmethod/latin/dicttool/DictionaryMaker.java b/tools/dicttool/src/com/android/inputmethod/latin/dicttool/DictionaryMaker.java
index 561db73..cc890f6 100644
--- a/tools/dicttool/src/com/android/inputmethod/latin/dicttool/DictionaryMaker.java
+++ b/tools/dicttool/src/com/android/inputmethod/latin/dicttool/DictionaryMaker.java
@@ -172,12 +172,15 @@
String filename = args.get(0);
args.remove(0);
if (OPTION_INPUT_SOURCE.equals(arg)) {
- if (BinaryDictInputOutput.isBinaryDictionary(filename)) {
- inputBinary = filename;
+ if (XmlDictInputOutput.isXmlUnigramDictionary(filename)) {
+ inputUnigramXml = filename;
} else if (CombinedInputOutput.isCombinedDictionary(filename)) {
inputCombined = filename;
+ } else if (BinaryDictInputOutput.isBinaryDictionary(filename)) {
+ inputBinary = filename;
} else {
- inputUnigramXml = filename;
+ throw new IllegalArgumentException(
+ "Unknown format for file " + filename);
}
} else if (OPTION_INPUT_SHORTCUT_XML.equals(arg)) {
inputShortcutXml = filename;
diff --git a/tools/dicttool/src/com/android/inputmethod/latin/dicttool/XmlDictInputOutput.java b/tools/dicttool/src/com/android/inputmethod/latin/dicttool/XmlDictInputOutput.java
index 252c3d6..d8d94a1 100644
--- a/tools/dicttool/src/com/android/inputmethod/latin/dicttool/XmlDictInputOutput.java
+++ b/tools/dicttool/src/com/android/inputmethod/latin/dicttool/XmlDictInputOutput.java
@@ -22,6 +22,10 @@
import com.android.inputmethod.latin.makedict.FusionDictionary.WeightedString;
import com.android.inputmethod.latin.makedict.Word;
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.Writer;
@@ -264,6 +268,35 @@
}
/**
+ * Basic test to find out whether the file is in the unigram XML format or not.
+ *
+ * Concretely this only tests the header line.
+ *
+ * @param filename The name of the file to test.
+ * @return true if the file is in the unigram XML format, false otherwise
+ */
+ public static boolean isXmlUnigramDictionary(final String filename) {
+ BufferedReader reader = null;
+ try {
+ reader = new BufferedReader(new FileReader(new File(filename)));
+ final String firstLine = reader.readLine();
+ return firstLine.matches("^\\s*<wordlist .*>\\s*$");
+ } catch (FileNotFoundException e) {
+ return false;
+ } catch (IOException e) {
+ return false;
+ } finally {
+ if (reader != null) {
+ try {
+ reader.close();
+ } catch (IOException e) {
+ // do nothing
+ }
+ }
+ }
+ }
+
+ /**
* Reads a dictionary from an XML file.
*
* This is the public method that will parse an XML file and return the corresponding memory