From c734c2aca1830643d169fd292e0c9d4d9306af5a Mon Sep 17 00:00:00 2001 From: Jean Chalard Date: Tue, 3 Apr 2012 13:19:57 +0900 Subject: [PATCH] Add a simple way to input dictionary header attributes Just add them as an attribute to the root of the XML node. Bug: 6202812 Change-Id: Idf040bfebf20a72f9e4370930a85d97df593f484 --- .../latin/makedict/BinaryDictInputOutput.java | 19 ++++++++++++++++--- .../latin/makedict/FusionDictionary.java | 16 +++++++++++++++- .../latin/makedict/XmlDictInputOutput.java | 14 +++++++++++++- 3 files changed, 44 insertions(+), 5 deletions(-) diff --git a/java/src/com/android/inputmethod/latin/makedict/BinaryDictInputOutput.java b/java/src/com/android/inputmethod/latin/makedict/BinaryDictInputOutput.java index af7f863ee..010ea6813 100644 --- a/java/src/com/android/inputmethod/latin/makedict/BinaryDictInputOutput.java +++ b/java/src/com/android/inputmethod/latin/makedict/BinaryDictInputOutput.java @@ -26,6 +26,7 @@ import java.io.OutputStream; import java.io.RandomAccessFile; import java.util.ArrayList; import java.util.Arrays; +import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.TreeMap; @@ -922,7 +923,14 @@ public class BinaryDictInputOutput { if (version >= FIRST_VERSION_WITH_HEADER_SIZE) { final int headerSizeOffset = index; index += 4; // Size of the header size - // TODO: Write out the header contents here. + + // Write out the options. + for (final String key : dict.mOptions.mAttributes.keySet()) { + final String value = dict.mOptions.mAttributes.get(key); + index += CharEncoding.writeString(buffer, index, key); + index += CharEncoding.writeString(buffer, index, value); + } + // Write out the header size. buffer[headerSizeOffset] = (byte) (0xFF & (index >> 24)); buffer[headerSizeOffset + 1] = (byte) (0xFF & (index >> 16)); @@ -1214,12 +1222,17 @@ public class BinaryDictInputOutput { source.readUnsignedShort(); final long headerSize; + final HashMap options = new HashMap(); if (version < FIRST_VERSION_WITH_HEADER_SIZE) { headerSize = source.getFilePointer(); } else { headerSize = (source.readUnsignedByte() << 24) + (source.readUnsignedByte() << 16) + (source.readUnsignedByte() << 8) + source.readUnsignedByte(); - // read the header body + while (source.getFilePointer() < headerSize) { + final String key = CharEncoding.readString(source); + final String value = CharEncoding.readString(source); + options.put(key, value); + } source.seek(headerSize); } @@ -1228,7 +1241,7 @@ public class BinaryDictInputOutput { final Node root = readNode(source, headerSize, reverseNodeMapping, reverseGroupMapping); FusionDictionary newDict = new FusionDictionary(root, - new FusionDictionary.DictionaryOptions()); + new FusionDictionary.DictionaryOptions(options)); if (null != dict) { for (Word w : dict) { newDict.add(w.mWord, w.mFrequency, w.mShortcutTargets, w.mBigrams); diff --git a/java/src/com/android/inputmethod/latin/makedict/FusionDictionary.java b/java/src/com/android/inputmethod/latin/makedict/FusionDictionary.java index d3ffb47ad..99b17048d 100644 --- a/java/src/com/android/inputmethod/latin/makedict/FusionDictionary.java +++ b/java/src/com/android/inputmethod/latin/makedict/FusionDictionary.java @@ -19,6 +19,7 @@ package com.android.inputmethod.latin.makedict; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; +import java.util.HashMap; import java.util.Iterator; import java.util.LinkedList; @@ -250,6 +251,10 @@ public class FusionDictionary implements Iterable { * There are no options at the moment, so this class is empty. */ public static class DictionaryOptions { + final HashMap mAttributes; + public DictionaryOptions(final HashMap attributes) { + mAttributes = attributes; + } } @@ -257,8 +262,13 @@ public class FusionDictionary implements Iterable { public final Node mRoot; public FusionDictionary() { - mOptions = new DictionaryOptions(); mRoot = new Node(); + mOptions = new DictionaryOptions(new HashMap()); + } + + public FusionDictionary(final HashMap attributes) { + mRoot = new Node(); + mOptions = new DictionaryOptions(attributes); } public FusionDictionary(final Node root, final DictionaryOptions options) { @@ -266,6 +276,10 @@ public class FusionDictionary implements Iterable { mOptions = options; } + public void addOptionAttribute(final String key, final String value) { + mOptions.mAttributes.put(key, value); + } + /** * Helper method to convert a String to an int array. */ diff --git a/tools/makedict/src/com/android/inputmethod/latin/makedict/XmlDictInputOutput.java b/tools/makedict/src/com/android/inputmethod/latin/makedict/XmlDictInputOutput.java index 483473b3c..1d45fd25f 100644 --- a/tools/makedict/src/com/android/inputmethod/latin/makedict/XmlDictInputOutput.java +++ b/tools/makedict/src/com/android/inputmethod/latin/makedict/XmlDictInputOutput.java @@ -40,6 +40,7 @@ import org.xml.sax.helpers.DefaultHandler; */ public class XmlDictInputOutput { + private static final String ROOT_TAG = "wordlist"; private static final String WORD_TAG = "w"; private static final String BIGRAM_TAG = "bigram"; private static final String SHORTCUT_TAG = "shortcut"; @@ -96,6 +97,11 @@ public class XmlDictInputOutput { mFreq = Integer.parseInt(attrs.getValue(attrIndex)); } } + } else if (ROOT_TAG.equals(localName)) { + for (int attrIndex = 0; attrIndex < attrs.getLength(); ++attrIndex) { + final String attrName = attrs.getLocalName(attrIndex); + mDictionary.mOptions.mAttributes.put(attrName, attrs.getValue(attrIndex)); + } } else { mState = UNKNOWN; } @@ -275,7 +281,13 @@ public class XmlDictInputOutput { set.add(word); } // TODO: use an XMLSerializer if this gets big - destination.write("\n"); + destination.write(" options = dict.mOptions.mAttributes; + for (final String key : dict.mOptions.mAttributes.keySet()) { + final String value = dict.mOptions.mAttributes.get(key); + destination.write(" " + key + "=\"" + value + "\""); + } + destination.write(">\n"); destination.write("\n"); for (Word word : set) { destination.write(" <" + WORD_TAG + " " + WORD_ATTR + "=\"" + word.mWord + "\" "