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 STATIC_OPTIONS =
32             new FormatSpec.FormatOptions(FormatSpec.VERSION202);
33     public static final FormatSpec.FormatOptions DYNAMIC_OPTIONS_WITHOUT_TIMESTAMP =
34             new FormatSpec.FormatOptions(FormatSpec.VERSION4, false /* hasTimestamp */);
35     public static final FormatSpec.FormatOptions DYNAMIC_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                 || formatOptions.mVersion == FormatSpec.VERSION201
57                 || formatOptions.mVersion == FormatSpec.VERSION202) {
58             return new File(directory, name + "." + version + TEST_DICT_FILE_EXTENSION);
59         } else if (formatOptions.mVersion == FormatSpec.VERSION4) {
60             return new File(directory, name + "." + version);
61         } else {
62             throw new RuntimeException("the format option has a wrong version : "
63                     + formatOptions.mVersion);
64         }
65     }
66 
getDictEncoder(final File file, final FormatOptions formatOptions)67     public static DictEncoder getDictEncoder(final File file, final FormatOptions formatOptions) {
68         if (formatOptions.mVersion == FormatSpec.VERSION4) {
69             if (!file.isDirectory()) {
70                 file.mkdir();
71             }
72             return new Ver4DictEncoder(file);
73         } else if (formatOptions.mVersion == FormatSpec.VERSION202) {
74             return new Ver2DictEncoder(file, Ver2DictEncoder.CODE_POINT_TABLE_OFF);
75         } else {
76             throw new RuntimeException("The format option has a wrong version : "
77                     + formatOptions.mVersion);
78         }
79     }
80 }
81