Throw exceptions in some cases
app/src/main/java/eu/lepiller/nani/dictionary/Dictionary.java
| 12 | 12 | ||
| 13 | 13 | abstract public class Dictionary { | |
| 14 | 14 | private String name; | |
| 15 | - | private String description; | |
| 15 | + | private String description, fullDescription; | |
| 16 | 16 | File file; | |
| 17 | 17 | ||
| 18 | - | Dictionary(String n, String descr, File cacheDir) { | |
| 18 | + | Dictionary(String n, String descr, String fullDescr, File cacheDir) { | |
| 19 | 19 | name = n; | |
| 20 | 20 | description = descr; | |
| 21 | + | fullDescription = fullDescr; | |
| 21 | 22 | this.file = new File(cacheDir, "/dico/" + name); | |
| 22 | 23 | } | |
| 23 | 24 | ||
… | |||
| 28 | 29 | public String getDescription() { | |
| 29 | 30 | return description; | |
| 30 | 31 | } | |
| 32 | + | public String getFullDescription() { | |
| 33 | + | return fullDescription; | |
| 34 | + | } | |
| 31 | 35 | ||
| 32 | 36 | protected File getFile() { | |
| 33 | 37 | return file; | |
| 34 | 38 | } | |
| 35 | 39 | ||
| 40 | + | abstract public int getSize(); | |
| 41 | + | ||
| 36 | 42 | abstract public boolean isDownloaded(); | |
| 37 | 43 | ||
| 38 | 44 | abstract public int size(); | |
app/src/main/java/eu/lepiller/nani/dictionary/DictionaryException.java
| 1 | 1 | package eu.lepiller.nani.dictionary; | |
| 2 | 2 | ||
| 3 | - | public interface DictionaryException { | |
| 3 | + | public class DictionaryException extends Exception { | |
| 4 | 4 | } |
app/src/main/java/eu/lepiller/nani/dictionary/DictionaryFactory.java
| 15 | 15 | dictionaries = new ArrayList<>(); | |
| 16 | 16 | dictionaries.add(new JMDict("JMdict_e", | |
| 17 | 17 | context.getString(R.string.dico_jmdict_e), | |
| 18 | + | context.getString(R.string.dico_jmdict_long), | |
| 18 | 19 | context.getCacheDir(), | |
| 19 | 20 | "https://nani.lepiller.eu/dicos/JMdict_e.nani")); | |
| 20 | 21 | dictionaries.add(new JMDict("JMdict_dut", | |
| 21 | 22 | context.getString(R.string.dico_jmdict_dut), | |
| 23 | + | context.getString(R.string.dico_jmdict_long), | |
| 22 | 24 | context.getCacheDir(), | |
| 23 | 25 | "https://nani.lepiller.eu/dicos/JMdict_dut.nani")); | |
| 24 | 26 | dictionaries.add(new JMDict("JMdict_fre", | |
| 25 | 27 | context.getString(R.string.dico_jmdict_fre), | |
| 28 | + | context.getString(R.string.dico_jmdict_long), | |
| 26 | 29 | context.getCacheDir(), | |
| 27 | 30 | "https://nani.lepiller.eu/dicos/JMdict_fre.nani")); | |
| 28 | 31 | dictionaries.add(new JMDict("JMdict_ger", | |
| 29 | 32 | context.getString(R.string.dico_jmdict_ger), | |
| 33 | + | context.getString(R.string.dico_jmdict_long), | |
| 30 | 34 | context.getCacheDir(), | |
| 31 | 35 | "https://nani.lepiller.eu/dicos/JMdict_ger.nani")); | |
| 32 | 36 | dictionaries.add(new JMDict("JMdict_hun", | |
| 33 | 37 | context.getString(R.string.dico_jmdict_hun), | |
| 38 | + | context.getString(R.string.dico_jmdict_long), | |
| 34 | 39 | context.getCacheDir(), | |
| 35 | 40 | "https://nani.lepiller.eu/dicos/JMdict_hun.nani")); | |
| 36 | 41 | dictionaries.add(new JMDict("JMdict_rus", | |
| 37 | 42 | context.getString(R.string.dico_jmdict_rus), | |
| 43 | + | context.getString(R.string.dico_jmdict_long), | |
| 38 | 44 | context.getCacheDir(), | |
| 39 | 45 | "https://nani.lepiller.eu/dicos/JMdict_rus.nani")); | |
| 40 | 46 | dictionaries.add(new JMDict("JMdict_slv", | |
| 41 | 47 | context.getString(R.string.dico_jmdict_slv), | |
| 48 | + | context.getString(R.string.dico_jmdict_long), | |
| 42 | 49 | context.getCacheDir(), | |
| 43 | 50 | "https://nani.lepiller.eu/dicos/JMdict_slv.nani")); | |
| 44 | 51 | dictionaries.add(new JMDict("JMdict_spa", | |
| 45 | 52 | context.getString(R.string.dico_jmdict_spa), | |
| 53 | + | context.getString(R.string.dico_jmdict_long), | |
| 46 | 54 | context.getCacheDir(), | |
| 47 | 55 | "https://nani.lepiller.eu/dicos/JMdict_spa.nani")); | |
| 48 | 56 | dictionaries.add(new JMDict("JMdict_swe", | |
| 49 | 57 | context.getString(R.string.dico_jmdict_swe), | |
| 58 | + | context.getString(R.string.dico_jmdict_long), | |
| 50 | 59 | context.getCacheDir(), | |
| 51 | 60 | "https://nani.lepiller.eu/dicos/JMdict_swe.nani")); | |
| 52 | 61 | } | |
| 53 | 62 | ||
| 54 | - | public static ArrayList<Result> search(Context context, String text) { | |
| 63 | + | public static ArrayList<Result> search(Context context, String text) throws DictionaryException { | |
| 55 | 64 | if(instance == null) | |
| 56 | 65 | instance = new DictionaryFactory(context); | |
| 57 | 66 | ||
| 67 | + | int available = 0; | |
| 58 | 68 | ArrayList<Result> results = new ArrayList<>(); | |
| 59 | 69 | for(Dictionary d: dictionaries) { | |
| 60 | - | if (d instanceof JMDict) { | |
| 70 | + | if (d instanceof JMDict && d.isDownloaded()) { | |
| 71 | + | available++; | |
| 61 | 72 | ArrayList<Result> dr = ((JMDict) d).search(text); | |
| 62 | 73 | if(dr != null) | |
| 63 | 74 | results.addAll(dr); | |
| 64 | 75 | } | |
| 65 | 76 | } | |
| 77 | + | ||
| 78 | + | if(available == 0) { | |
| 79 | + | throw new NoDictionaryException(); | |
| 80 | + | } | |
| 66 | 81 | return results; | |
| 67 | 82 | } | |
| 68 | 83 |
app/src/main/java/eu/lepiller/nani/dictionary/IncompatibleFormatException.java
| 1 | 1 | package eu.lepiller.nani.dictionary; | |
| 2 | 2 | ||
| 3 | - | public class IncompatibleFormatException { | |
| 3 | + | public class IncompatibleFormatException extends DictionaryException { | |
| 4 | + | String name; | |
| 5 | + | ||
| 6 | + | public IncompatibleFormatException(String name) { | |
| 7 | + | this.name = name; | |
| 8 | + | } | |
| 9 | + | ||
| 10 | + | public String getName() { | |
| 11 | + | return name; | |
| 12 | + | } | |
| 4 | 13 | } |
app/src/main/java/eu/lepiller/nani/dictionary/JMDict.java
| 1 | 1 | package eu.lepiller.nani.dictionary; | |
| 2 | 2 | ||
| 3 | - | import android.support.annotation.RequiresPermission; | |
| 4 | 3 | import android.util.Log; | |
| 5 | 4 | ||
| 6 | 5 | import java.io.File; | |
… | |||
| 11 | 10 | import java.net.URL; | |
| 12 | 11 | import java.util.ArrayList; | |
| 13 | 12 | import java.util.Arrays; | |
| 14 | - | import java.util.Comparator; | |
| 15 | 13 | import java.util.HashMap; | |
| 16 | - | import java.util.List; | |
| 17 | 14 | import java.util.Map; | |
| 18 | 15 | ||
| 19 | 16 | import eu.lepiller.nani.R; | |
… | |||
| 41 | 38 | private String mUrl; | |
| 42 | 39 | private Huffman kanjiHuffman, readingHuffman, meaningHuffman; | |
| 43 | 40 | ||
| 44 | - | JMDict(String name, String description, File cacheDir, String url) { | |
| 45 | - | super(name, description, cacheDir); | |
| 41 | + | JMDict(String name, String description, String fullDescription, File cacheDir, String url) { | |
| 42 | + | super(name, description, fullDescription, cacheDir); | |
| 46 | 43 | mUrl = url; | |
| 47 | 44 | } | |
| 48 | 45 | ||
… | |||
| 87 | 84 | meaningHuffman = null; | |
| 88 | 85 | } | |
| 89 | 86 | ||
| 87 | + | public int getSize() { | |
| 88 | + | if(!isDownloaded()) | |
| 89 | + | return 0; | |
| 90 | + | ||
| 91 | + | return (int)(getFile().length() / 1000000); | |
| 92 | + | } | |
| 93 | + | ||
| 94 | + | private String getString(RandomAccessFile file) throws IOException { | |
| 95 | + | byte b; | |
| 96 | + | ArrayList<Byte> bs = new ArrayList<>(); | |
| 97 | + | while((b = file.readByte()) != 0) { | |
| 98 | + | bs.add(b); | |
| 99 | + | } | |
| 100 | + | byte[] str = new byte[bs.size()]; | |
| 101 | + | for(int j=0; j<bs.size(); j++) { | |
| 102 | + | str[j] = bs.get(j); | |
| 103 | + | } | |
| 104 | + | return new String(str, "UTF-8"); | |
| 105 | + | } | |
| 106 | + | ||
| 90 | 107 | private ArrayList<String> getStringList(RandomAccessFile file) throws IOException { | |
| 91 | 108 | ArrayList<String> results = new ArrayList<>(); | |
| 92 | 109 | int number = file.readShort(); | |
| 93 | 110 | for(int i=0; i<number; i++) { | |
| 94 | - | results.add(file.readUTF()); | |
| 111 | + | results.add(getString(file)); | |
| 95 | 112 | } | |
| 96 | 113 | return results; | |
| 97 | 114 | } | |
… | |||
| 111 | 128 | if(bits.isEmpty()) { | |
| 112 | 129 | byte by = file.readByte(); | |
| 113 | 130 | Log.d(TAG, "Read byte for huffman: " + by); | |
| 114 | - | short mod = (short)256; | |
| 115 | - | while(mod != 1) { | |
| 116 | - | mod /= 2; | |
| 117 | - | bits.add((by / mod) > 0); | |
| 118 | - | by = (byte)(by % mod); | |
| 131 | + | for(int i = 7; i>-1; i--) { | |
| 132 | + | bits.add((by&(1<<i))!=0); | |
| 119 | 133 | } | |
| 120 | 134 | Log.d(TAG, "Read byte for huffman: " + bits); | |
| 121 | 135 | } | |
… | |||
| 163 | 177 | ||
| 164 | 178 | private Result getValue(RandomAccessFile file, long pos) throws IOException { | |
| 165 | 179 | file.seek(pos); | |
| 166 | - | Log.d(TAG, "Getting value"); | |
| 180 | + | Log.d(TAG, "Getting value at " + pos); | |
| 167 | 181 | ArrayList<String> kanjis = getHuffmanStringList(file, kanjiHuffman); | |
| 168 | 182 | ||
| 169 | 183 | Log.d(TAG, "Getting readings"); | |
… | |||
| 188 | 202 | ArrayList<String> sense_limits = getStringList(file); | |
| 189 | 203 | ArrayList<String> sense_infos = getStringList(file); | |
| 190 | 204 | ArrayList<Result.Source> sense_sources = new ArrayList<>(); | |
| 191 | - | int source_number = file.readInt(); | |
| 205 | + | int source_number = file.readShort(); | |
| 192 | 206 | for(int j=0; j<source_number; j++) { | |
| 193 | 207 | ArrayList<String> source_content = getStringList(file); | |
| 194 | 208 | boolean source_wasei = file.read() != 0; | |
| 195 | - | String source_type = file.readUTF(); | |
| 196 | - | String source_language = file.readUTF(); | |
| 209 | + | String source_type = getString(file); | |
| 210 | + | String source_language = getString(file); | |
| 197 | 211 | sense_sources.add(new Result.Source(source_content, source_wasei, source_type, source_language)); | |
| 198 | 212 | } | |
| 199 | 213 | ArrayList<Integer> sense_tags = getIntList(file); | |
| 200 | 214 | ArrayList<String> sense_glosses = getHuffmanStringList(file, meaningHuffman); | |
| 201 | - | String sense_language = file.readUTF(); | |
| 215 | + | String sense_language = getString(file); | |
| 202 | 216 | senses.add(new Result.Sense(sense_references, sense_limits, sense_infos, sense_sources, | |
| 203 | 217 | sense_tags, sense_glosses, sense_language)); | |
| 204 | 218 | } | |
… | |||
| 272 | 286 | ||
| 273 | 287 | return new HuffmanTree(left, right); | |
| 274 | 288 | } else if (b == 0) { | |
| 275 | - | file.skipBytes(1); | |
| 289 | + | Log.d(TAG, "Skipping byte " + file.readByte()); | |
| 276 | 290 | return new HuffmanValue(""); | |
| 277 | 291 | } else { | |
| 278 | 292 | ArrayList<Byte> bs = new ArrayList<>(); | |
… | |||
| 288 | 302 | } | |
| 289 | 303 | } | |
| 290 | 304 | ||
| 291 | - | ArrayList<Result> search(String text) { | |
| 305 | + | ArrayList<Result> search(String text) throws IncompatibleFormatException { | |
| 292 | 306 | if (isDownloaded()) { | |
| 293 | 307 | try { | |
| 294 | 308 | RandomAccessFile file = new RandomAccessFile(getFile(), "r"); | |
… | |||
| 299 | 313 | ||
| 300 | 314 | // Check file format version | |
| 301 | 315 | if(!Arrays.equals(header, "NANI_JMDICT001".getBytes())) | |
| 302 | - | return null; | |
| 316 | + | throw new IncompatibleFormatException(getName()); | |
| 303 | 317 | ||
| 304 | 318 | byte[] search = text.getBytes(); | |
| 305 | 319 | ||
… | |||
| 335 | 349 | } | |
| 336 | 350 | ||
| 337 | 351 | int[] uniqResultsArray = new int[uniqResults.size()]; | |
| 352 | + | for(int i=0; i<uniqResults.size(); i++) { | |
| 353 | + | uniqResultsArray[i] = uniqResults.get(i); | |
| 354 | + | } | |
| 338 | 355 | Arrays.sort(uniqResultsArray); | |
| 339 | 356 | ||
| 357 | + | Log.d(TAG, uniqResults.toString()); | |
| 358 | + | ||
| 340 | 359 | int num = 0; | |
| 341 | - | for(Integer i: uniqResultsArray) { | |
| 360 | + | for(int i: uniqResultsArray) { | |
| 342 | 361 | if(num > 10) | |
| 343 | 362 | break; | |
| 344 | 363 | num++; | |