1 /* 2 * Copyright (C) 2013 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.inputmethod.latin.makedict; 18 19 import com.android.inputmethod.latin.makedict.FormatSpec.DictionaryOptions; 20 import com.android.inputmethod.latin.makedict.FormatSpec.FormatOptions; 21 22 import java.io.File; 23 import java.util.HashMap; 24 25 public class BinaryDictUtils { 26 public static final int USE_BYTE_ARRAY = 1; 27 public static final int USE_BYTE_BUFFER = 2; 28 29 public static final String TEST_DICT_FILE_EXTENSION = ".testDict"; 30 31 public static final FormatSpec.FormatOptions VERSION2_OPTIONS = 32 new FormatSpec.FormatOptions(FormatSpec.VERSION2); 33 public static final FormatSpec.FormatOptions VERSION4_OPTIONS_WITHOUT_TIMESTAMP = 34 new FormatSpec.FormatOptions(FormatSpec.VERSION4, false /* hasTimestamp */); 35 public static final FormatSpec.FormatOptions VERSION4_OPTIONS_WITH_TIMESTAMP = 36 new FormatSpec.FormatOptions(FormatSpec.VERSION4, true /* hasTimestamp */); 37 makeDictionaryOptions(final String id, final String version, final FormatSpec.FormatOptions formatOptions)38 public static DictionaryOptions makeDictionaryOptions(final String id, final String version, 39 final FormatSpec.FormatOptions formatOptions) { 40 final DictionaryOptions options = new DictionaryOptions(new HashMap<String, String>()); 41 options.mAttributes.put(DictionaryHeader.DICTIONARY_LOCALE_KEY, "en_US"); 42 options.mAttributes.put(DictionaryHeader.DICTIONARY_ID_KEY, id); 43 options.mAttributes.put(DictionaryHeader.DICTIONARY_VERSION_KEY, version); 44 if (formatOptions.mHasTimestamp) { 45 options.mAttributes.put(DictionaryHeader.HAS_HISTORICAL_INFO_KEY, 46 DictionaryHeader.ATTRIBUTE_VALUE_TRUE); 47 options.mAttributes.put(DictionaryHeader.USES_FORGETTING_CURVE_KEY, 48 DictionaryHeader.ATTRIBUTE_VALUE_TRUE); 49 } 50 return options; 51 } 52 getDictFile(final String name, final String version, final FormatOptions formatOptions, final File directory)53 public static File getDictFile(final String name, final String version, 54 final FormatOptions formatOptions, final File directory) { 55 if (formatOptions.mVersion == FormatSpec.VERSION2) { 56 return new File(directory, name + "." + version + TEST_DICT_FILE_EXTENSION); 57 } else if (formatOptions.mVersion == FormatSpec.VERSION4) { 58 return new File(directory, name + "." + version); 59 } else { 60 throw new RuntimeException("the format option has a wrong version : " 61 + formatOptions.mVersion); 62 } 63 } 64 getDictEncoder(final File file, final FormatOptions formatOptions)65 public static DictEncoder getDictEncoder(final File file, final FormatOptions formatOptions) { 66 if (formatOptions.mVersion == FormatSpec.VERSION4) { 67 if (!file.isDirectory()) { 68 file.mkdir(); 69 } 70 return new Ver4DictEncoder(file); 71 } else if (formatOptions.mVersion == FormatSpec.VERSION2) { 72 return new Ver2DictEncoder(file); 73 } else { 74 throw new RuntimeException("The format option has a wrong version : " 75 + formatOptions.mVersion); 76 } 77 } 78 } 79