From 54e84a00fc032ba566cbda41feafa71de77e1c43 Mon Sep 17 00:00:00 2001
From: Jean Chalard <jchalard@google.com>
Date: Fri, 3 Aug 2012 17:05:41 +0900
Subject: [PATCH] Make a makedict command for dicttool (A3)

This behaves exactly as the old makedict command. Further
changes will redirect the calls to makedict to this, so as
to consolidate similar code.

Groundwork for
Bug: 6429606

Change-Id: Ibeadbf48bec70f988a15ca36ebf5d1ce3b5b54ea
---
 .../latin/makedict/FusionDictionary.java      |  4 +-
 .../inputmethod/latin/makedict/Word.java      |  8 ++--
 tools/dicttool/Android.mk                     |  9 ++++-
 .../latin/dicttool}/DictionaryMaker.java      | 15 +++++--
 .../inputmethod/latin/dicttool/Dicttool.java  |  1 +
 .../inputmethod/latin/dicttool/Makedict.java  | 40 +++++++++++++++++++
 .../latin/dicttool}/MakedictLog.java          |  0
 .../latin/dicttool}/XmlDictInputOutput.java   |  4 +-
 8 files changed, 70 insertions(+), 11 deletions(-)
 rename tools/{makedict/src/com/android/inputmethod/latin/makedict => dicttool/src/android/inputmethod/latin/dicttool}/DictionaryMaker.java (96%)
 create mode 100644 tools/dicttool/src/android/inputmethod/latin/dicttool/Makedict.java
 rename tools/{makedict/src/com/android/inputmethod/latin/makedict => dicttool/src/android/inputmethod/latin/dicttool}/MakedictLog.java (100%)
 rename tools/{makedict/src/com/android/inputmethod/latin/makedict => dicttool/src/android/inputmethod/latin/dicttool}/XmlDictInputOutput.java (98%)

diff --git a/java/src/com/android/inputmethod/latin/makedict/FusionDictionary.java b/java/src/com/android/inputmethod/latin/makedict/FusionDictionary.java
index 8b53c94271..5864db28e2 100644
--- a/java/src/com/android/inputmethod/latin/makedict/FusionDictionary.java
+++ b/java/src/com/android/inputmethod/latin/makedict/FusionDictionary.java
@@ -61,8 +61,8 @@ public class FusionDictionary implements Iterable<Word> {
      * This represents an "attribute", that is either a bigram or a shortcut.
      */
     public static class WeightedString {
-        final String mWord;
-        int mFrequency;
+        public final String mWord;
+        public int mFrequency;
         public WeightedString(String word, int frequency) {
             mWord = word;
             mFrequency = frequency;
diff --git a/java/src/com/android/inputmethod/latin/makedict/Word.java b/java/src/com/android/inputmethod/latin/makedict/Word.java
index d078267575..65fc72c40c 100644
--- a/java/src/com/android/inputmethod/latin/makedict/Word.java
+++ b/java/src/com/android/inputmethod/latin/makedict/Word.java
@@ -27,10 +27,10 @@ import java.util.Arrays;
  * This is chiefly used to iterate a dictionary.
  */
 public class Word implements Comparable<Word> {
-    final String mWord;
-    final int mFrequency;
-    final ArrayList<WeightedString> mShortcutTargets;
-    final ArrayList<WeightedString> mBigrams;
+    public final String mWord;
+    public final int mFrequency;
+    public final ArrayList<WeightedString> mShortcutTargets;
+    public final ArrayList<WeightedString> mBigrams;
 
     private int mHashCode = 0;
 
diff --git a/tools/dicttool/Android.mk b/tools/dicttool/Android.mk
index 9e8dbe0f81..e9c11acc47 100644
--- a/tools/dicttool/Android.mk
+++ b/tools/dicttool/Android.mk
@@ -16,9 +16,16 @@
 LOCAL_PATH := $(call my-dir)
 include $(CLEAR_VARS)
 
-LOCAL_SRC_FILES := $(call all-java-files-under,src)
+MAKEDICT_CORE_SOURCE_DIRECTORY := ../../java/src/com/android/inputmethod/latin/makedict
+
+LOCAL_MAIN_SRC_FILES := $(call all-java-files-under,$(MAKEDICT_CORE_SOURCE_DIRECTORY))
+LOCAL_TOOL_SRC_FILES := $(call all-java-files-under,src)
+LOCAL_SRC_FILES := $(LOCAL_TOOL_SRC_FILES) \
+        $(filter-out $(addprefix %/, $(notdir $(LOCAL_TOOL_SRC_FILES))), $(LOCAL_MAIN_SRC_FILES)) \
+        $(call all-java-files-under,tests)
 LOCAL_JAR_MANIFEST := etc/manifest.txt
 LOCAL_MODULE := dicttool
+LOCAL_JAVA_LIBRARIES := junit
 LOCAL_MODULE_TAGS := eng
 
 include $(BUILD_HOST_JAVA_LIBRARY)
diff --git a/tools/makedict/src/com/android/inputmethod/latin/makedict/DictionaryMaker.java b/tools/dicttool/src/android/inputmethod/latin/dicttool/DictionaryMaker.java
similarity index 96%
rename from tools/makedict/src/com/android/inputmethod/latin/makedict/DictionaryMaker.java
rename to tools/dicttool/src/android/inputmethod/latin/dicttool/DictionaryMaker.java
index 5e3921573b..9ebd3bbdd1 100644
--- a/tools/makedict/src/com/android/inputmethod/latin/makedict/DictionaryMaker.java
+++ b/tools/dicttool/src/android/inputmethod/latin/dicttool/DictionaryMaker.java
@@ -14,7 +14,12 @@
  * the License.
  */
 
-package com.android.inputmethod.latin.makedict;
+package com.android.inputmethod.latin.dicttool;
+
+import com.android.inputmethod.latin.makedict.BinaryDictInputOutput;
+import com.android.inputmethod.latin.makedict.FusionDictionary;
+import com.android.inputmethod.latin.makedict.MakedictLog;
+import com.android.inputmethod.latin.makedict.UnsupportedFormatException;
 
 import java.io.File;
 import java.io.FileInputStream;
@@ -102,7 +107,11 @@ public class DictionaryMaker {
         }
 
         private void displayHelp() {
-            MakedictLog.i("Usage: makedict "
+            MakedictLog.i(getHelp());
+        }
+
+        public static String getHelp() {
+            return "Usage: makedict "
                     + "[-s <unigrams.xml> [-b <bigrams.xml>] [-c <shortcuts.xml>] "
                     + "| -s <binary input>] [-d <binary output format version 2>] "
                     + "[-d1 <binary output format version 1>] [-x <xml output>] [-2]\n"
@@ -114,7 +123,7 @@ public class DictionaryMaker {
                     + "  are supported. All three can be output at the same time, but the same\n"
                     + "  output format cannot be specified several times. The behavior is\n"
                     + "  unspecified if the same file is specified for input and output, or for\n"
-                    + "  several outputs.");
+                    + "  several outputs.";
         }
 
         public Arguments(String[] argsArray) throws IOException {
diff --git a/tools/dicttool/src/android/inputmethod/latin/dicttool/Dicttool.java b/tools/dicttool/src/android/inputmethod/latin/dicttool/Dicttool.java
index 8fc0423b82..c14ce7b884 100644
--- a/tools/dicttool/src/android/inputmethod/latin/dicttool/Dicttool.java
+++ b/tools/dicttool/src/android/inputmethod/latin/dicttool/Dicttool.java
@@ -35,6 +35,7 @@ public class Dicttool {
         sCommands.put("info", Info.class);
         sCommands.put("compress", Compress.Compressor.class);
         sCommands.put("uncompress", Compress.Uncompressor.class);
+        sCommands.put("makedict", Makedict.class);
     }
 
     private static Command getCommandInstance(final String commandName) {
diff --git a/tools/dicttool/src/android/inputmethod/latin/dicttool/Makedict.java b/tools/dicttool/src/android/inputmethod/latin/dicttool/Makedict.java
new file mode 100644
index 0000000000..c004cfbe46
--- /dev/null
+++ b/tools/dicttool/src/android/inputmethod/latin/dicttool/Makedict.java
@@ -0,0 +1,40 @@
+/**
+ * Copyright (C) 2012 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+
+package com.android.inputmethod.latin.dicttool;
+
+import com.android.inputmethod.latin.makedict.UnsupportedFormatException;
+
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import javax.xml.parsers.ParserConfigurationException;
+import org.xml.sax.SAXException;
+
+public class Makedict extends Dicttool.Command {
+    public static final String COMMAND = "makedict";
+
+    public Makedict() {
+    }
+
+    public String getHelp() {
+        return DictionaryMaker.Arguments.getHelp();
+    }
+
+    public void run() throws FileNotFoundException, IOException, ParserConfigurationException,
+            SAXException, UnsupportedFormatException {
+        DictionaryMaker.main(mArgs);
+    }
+}
diff --git a/tools/makedict/src/com/android/inputmethod/latin/makedict/MakedictLog.java b/tools/dicttool/src/android/inputmethod/latin/dicttool/MakedictLog.java
similarity index 100%
rename from tools/makedict/src/com/android/inputmethod/latin/makedict/MakedictLog.java
rename to tools/dicttool/src/android/inputmethod/latin/dicttool/MakedictLog.java
diff --git a/tools/makedict/src/com/android/inputmethod/latin/makedict/XmlDictInputOutput.java b/tools/dicttool/src/android/inputmethod/latin/dicttool/XmlDictInputOutput.java
similarity index 98%
rename from tools/makedict/src/com/android/inputmethod/latin/makedict/XmlDictInputOutput.java
rename to tools/dicttool/src/android/inputmethod/latin/dicttool/XmlDictInputOutput.java
index 52f124dfb3..8e2e73505c 100644
--- a/tools/makedict/src/com/android/inputmethod/latin/makedict/XmlDictInputOutput.java
+++ b/tools/dicttool/src/android/inputmethod/latin/dicttool/XmlDictInputOutput.java
@@ -14,11 +14,13 @@
  * the License.
  */
 
-package com.android.inputmethod.latin.makedict;
+package com.android.inputmethod.latin.dicttool;
 
+import com.android.inputmethod.latin.makedict.FusionDictionary;
 import com.android.inputmethod.latin.makedict.FusionDictionary.DictionaryOptions;
 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.IOException;
 import java.io.InputStream;
-- 
GitLab