BinaryDictUtils.java revision 7b55cd3e2b4966150fa4c44dd43ebfeb77058a43
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 VERSION3_OPTIONS = 35 new FormatSpec.FormatOptions(FormatSpec.VERSION3); 36 public static final FormatSpec.FormatOptions VERSION4_OPTIONS_WITHOUT_TIMESTAMP = 37 new FormatSpec.FormatOptions(FormatSpec.VERSION4, false /* hasTimestamp */); 38 public static final FormatSpec.FormatOptions VERSION4_OPTIONS_WITH_TIMESTAMP = 39 new FormatSpec.FormatOptions(FormatSpec.VERSION4, true /* hasTimestamp */); 40 41 public static DictionaryOptions makeDictionaryOptions(final String id, final String version) { 42 final DictionaryOptions options = new DictionaryOptions(new HashMap<String, String>()); 43 options.mAttributes.put(FileHeader.DICTIONARY_LOCALE_ATTRIBUTE, "en_US"); 44 options.mAttributes.put(FileHeader.DICTIONARY_ID_ATTRIBUTE, id); 45 options.mAttributes.put(FileHeader.DICTIONARY_VERSION_ATTRIBUTE, version); 46 return options; 47 } 48 49 public static File getDictFile(final String name, final String version, 50 final FormatOptions formatOptions, final File directory) { 51 if (formatOptions.mVersion == FormatSpec.VERSION2 52 || formatOptions.mVersion == FormatSpec.VERSION3) { 53 return new File(directory, name + "." + version + TEST_DICT_FILE_EXTENSION); 54 } else if (formatOptions.mVersion == FormatSpec.VERSION4) { 55 return new File(directory, name + "." + version); 56 } else { 57 throw new RuntimeException("the format option has a wrong version : " 58 + formatOptions.mVersion); 59 } 60 } 61 62 public static DictEncoder getDictEncoder(final File file, final FormatOptions formatOptions, 63 final File cacheDir) { 64 if (formatOptions.mVersion == FormatSpec.VERSION4) { 65 return new Ver4DictEncoder(cacheDir); 66 } else if (formatOptions.mVersion == FormatSpec.VERSION3 67 || formatOptions.mVersion == FormatSpec.VERSION2) { 68 return new Ver3DictEncoder(file); 69 } else { 70 throw new RuntimeException("The format option has a wrong version : " 71 + formatOptions.mVersion); 72 } 73 } 74 75 public static DictUpdater getDictUpdater(final File file, final FormatOptions formatOptions) 76 throws UnsupportedFormatException { 77 if (formatOptions.mVersion == FormatSpec.VERSION4) { 78 return new Ver4DictUpdater(file, DictDecoder.USE_WRITABLE_BYTEBUFFER); 79 } else if (formatOptions.mVersion == FormatSpec.VERSION3) { 80 return new Ver3DictUpdater(file, DictDecoder.USE_WRITABLE_BYTEBUFFER); 81 } else { 82 throw new UnsupportedFormatException("The format option has a wrong version : " 83 + formatOptions.mVersion); 84 } 85 } 86} 87