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.annotations.UsedForTesting; 20 21 import java.io.FileNotFoundException; 22 import java.io.IOException; 23 import java.util.ArrayList; 24 import java.util.TreeMap; 25 26 /** 27 * A base class of the binary dictionary decoder. 28 */ 29 public abstract class AbstractDictDecoder implements DictDecoder { 30 private static final int SUCCESS = 0; 31 private static final int ERROR_CANNOT_READ = 1; 32 private static final int ERROR_WRONG_FORMAT = 2; 33 34 @Override @UsedForTesting getTerminalPosition(final String word)35 public int getTerminalPosition(final String word) 36 throws IOException, UnsupportedFormatException { 37 if (!isDictBufferOpen()) { 38 openDictBuffer(); 39 } 40 return BinaryDictIOUtils.getTerminalPosition(this, word); 41 } 42 43 @Override @UsedForTesting readUnigramsAndBigramsBinary(final TreeMap<Integer, String> words, final TreeMap<Integer, Integer> frequencies, final TreeMap<Integer, ArrayList<PendingAttribute>> bigrams)44 public void readUnigramsAndBigramsBinary(final TreeMap<Integer, String> words, 45 final TreeMap<Integer, Integer> frequencies, 46 final TreeMap<Integer, ArrayList<PendingAttribute>> bigrams) 47 throws IOException, UnsupportedFormatException { 48 if (!isDictBufferOpen()) { 49 openDictBuffer(); 50 } 51 BinaryDictIOUtils.readUnigramsAndBigramsBinary(this, words, frequencies, bigrams); 52 } 53 54 /** 55 * Check whether the header contains the expected information. This is a no-error method, 56 * that will return an error code and never throw a checked exception. 57 * @return an error code, either ERROR_* or SUCCESS. 58 */ checkHeader()59 private int checkHeader() { 60 try { 61 readHeader(); 62 } catch (IOException e) { 63 return ERROR_CANNOT_READ; 64 } catch (UnsupportedFormatException e) { 65 return ERROR_WRONG_FORMAT; 66 } 67 return SUCCESS; 68 } 69 70 @Override hasValidRawBinaryDictionary()71 public boolean hasValidRawBinaryDictionary() { 72 return checkHeader() == SUCCESS; 73 } 74 75 // Placeholder implementations below. These are actually unused. 76 @Override openDictBuffer()77 public void openDictBuffer() throws FileNotFoundException, IOException, 78 UnsupportedFormatException { 79 } 80 81 @Override isDictBufferOpen()82 public boolean isDictBufferOpen() { 83 return false; 84 } 85 86 @Override readPtNode(final int ptNodePos)87 public PtNodeInfo readPtNode(final int ptNodePos) { 88 return null; 89 } 90 91 @Override setPosition(int newPos)92 public void setPosition(int newPos) { 93 } 94 95 @Override getPosition()96 public int getPosition() { 97 return 0; 98 } 99 100 @Override readPtNodeCount()101 public int readPtNodeCount() { 102 return 0; 103 } 104 } 105