BinaryDictUtils.java revision 26bd46095a05843e7574dfcf7db53406f215525d
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 17package com.android.inputmethod.latin.makedict; 18 19import com.android.inputmethod.latin.makedict.FormatSpec.FileHeader; 20import com.android.inputmethod.latin.makedict.FormatSpec.FormatOptions; 21import com.android.inputmethod.latin.makedict.FusionDictionary.DictionaryOptions; 22 23import java.io.File; 24import java.util.HashMap; 25 26public class BinaryDictUtils { 27 public static final int USE_BYTE_ARRAY = 1; 28 public static final int USE_BYTE_BUFFER = 2; 29 30 public static final String TEST_DICT_FILE_EXTENSION = ".testDict"; 31 32 public static final FormatSpec.FormatOptions VERSION2_OPTIONS = 33 new FormatSpec.FormatOptions(FormatSpec.VERSION2); 34 public static final FormatSpec.FormatOptions VERSION4_OPTIONS_WITHOUT_TIMESTAMP = 35 new FormatSpec.FormatOptions(FormatSpec.VERSION4, false /* hasTimestamp */); 36 public static final FormatSpec.FormatOptions VERSION4_OPTIONS_WITH_TIMESTAMP = 37 new FormatSpec.FormatOptions(FormatSpec.VERSION4, true /* hasTimestamp */); 38 39 public static DictionaryOptions makeDictionaryOptions(final String id, final String version, 40 final FormatSpec.FormatOptions formatOptions) { 41 final DictionaryOptions options = new DictionaryOptions(new HashMap<String, String>()); 42 options.mAttributes.put(FileHeader.DICTIONARY_LOCALE_ATTRIBUTE, "en_US"); 43 options.mAttributes.put(FileHeader.DICTIONARY_ID_ATTRIBUTE, id); 44 options.mAttributes.put(FileHeader.DICTIONARY_VERSION_ATTRIBUTE, version); 45 if (formatOptions.mHasTimestamp) { 46 options.mAttributes.put(FileHeader.HAS_HISTORICAL_INFO_ATTRIBUTE, 47 FileHeader.ATTRIBUTE_VALUE_TRUE); 48 options.mAttributes.put(FileHeader.USES_FORGETTING_CURVE_ATTRIBUTE, 49 FileHeader.ATTRIBUTE_VALUE_TRUE); 50 } 51 return options; 52 } 53 54 public static File getDictFile(final String name, final String version, 55 final FormatOptions formatOptions, final File directory) { 56 if (formatOptions.mVersion == FormatSpec.VERSION2) { 57 return new File(directory, name + "." + version + TEST_DICT_FILE_EXTENSION); 58 } else if (formatOptions.mVersion == FormatSpec.VERSION4) { 59 return new File(directory, name + "." + version); 60 } else { 61 throw new RuntimeException("the format option has a wrong version : " 62 + formatOptions.mVersion); 63 } 64 } 65 66 public static DictEncoder getDictEncoder(final File file, final FormatOptions formatOptions) { 67 if (formatOptions.mVersion == FormatSpec.VERSION4) { 68 if (!file.isDirectory()) { 69 file.mkdir(); 70 } 71 return new Ver4DictEncoder(file); 72 } else if (formatOptions.mVersion == FormatSpec.VERSION2) { 73 return new Ver2DictEncoder(file); 74 } else { 75 throw new RuntimeException("The format option has a wrong version : " 76 + formatOptions.mVersion); 77 } 78 } 79} 80