diff options
author | 2013-07-04 21:06:04 +0900 | |
---|---|---|
committer | 2013-07-04 21:06:04 +0900 | |
commit | edd1992ed329a84f0e9ef7056fda99f78eeb92b4 (patch) | |
tree | 5c541da1058b44a4773ae02a4934ec4b371f2ac3 /java/src/com/android/inputmethod/latin/AbstractDictionaryWriter.java | |
parent | 24025135562791d3f569e39509425b4c9624e310 (diff) | |
download | latinime-edd1992ed329a84f0e9ef7056fda99f78eeb92b4.tar.gz latinime-edd1992ed329a84f0e9ef7056fda99f78eeb92b4.tar.xz latinime-edd1992ed329a84f0e9ef7056fda99f78eeb92b4.zip |
DictionaryWriter to abstract binary dictionary writing.
UserHistoryDictionary should become to use ExpandableBinaryDictionary.
Bug: 6669677
Change-Id: I6831c7dd2d84207d1a8f51ba15d52e0a72205d0c
Diffstat (limited to 'java/src/com/android/inputmethod/latin/AbstractDictionaryWriter.java')
-rw-r--r-- | java/src/com/android/inputmethod/latin/AbstractDictionaryWriter.java | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/java/src/com/android/inputmethod/latin/AbstractDictionaryWriter.java b/java/src/com/android/inputmethod/latin/AbstractDictionaryWriter.java new file mode 100644 index 000000000..ebbcedc96 --- /dev/null +++ b/java/src/com/android/inputmethod/latin/AbstractDictionaryWriter.java @@ -0,0 +1,78 @@ +/* + * Copyright (C) 2013 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.inputmethod.latin; + +import android.content.Context; +import android.util.Log; + +import com.android.inputmethod.latin.makedict.UnsupportedFormatException; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; + +// TODO: Quit extending Dictionary after implementing dynamic binary dictionary. +abstract public class AbstractDictionaryWriter extends Dictionary { + /** Used for Log actions from this class */ + private static final String TAG = AbstractDictionaryWriter.class.getSimpleName(); + + private final Context mContext; + + public AbstractDictionaryWriter(final Context context, final String dictType) { + super(dictType); + mContext = context; + } + + abstract public void clear(); + + abstract public void addUnigramWord(final String word, final String shortcutTarget, + final int frequency, final boolean isNotAWord); + + abstract public void addBigramWords(final String word0, final String word1, + final int frequency, final boolean isValid); + + abstract public void removeBigramWords(final String word0, final String word1); + + abstract protected void writeBinaryDictionary(final FileOutputStream out) + throws IOException, UnsupportedFormatException; + + public void write(final String fileName) { + final String tempFileName = fileName + ".temp"; + final File file = new File(mContext.getFilesDir(), fileName); + final File tempFile = new File(mContext.getFilesDir(), tempFileName); + FileOutputStream out = null; + try { + out = new FileOutputStream(tempFile); + writeBinaryDictionary(out); + out.flush(); + out.close(); + tempFile.renameTo(file); + } catch (IOException e) { + Log.e(TAG, "IO exception while writing file", e); + } catch (UnsupportedFormatException e) { + Log.e(TAG, "Unsupported format", e); + } finally { + if (out != null) { + try { + out.close(); + } catch (IOException e) { + // ignore + } + } + } + } +} |