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 561db7363..cc890f60c 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 @@ public class DictionaryMaker { 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 252c3d655..d8d94a13c 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.Node; 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; @@ -263,6 +267,35 @@ public class XmlDictInputOutput { } } + /** + * 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*\\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. *