1 // Copyright 2011 Google Inc. All Rights Reserved.
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 package com.hughes.android.dictionary.engine;
18 import java.io.IOException;
19 import java.io.PrintStream;
20 import java.io.RandomAccessFile;
21 import java.util.ArrayList;
22 import java.util.List;
24 import com.hughes.android.dictionary.DictionaryInfo;
25 import com.hughes.util.CachingList;
26 import com.hughes.util.raf.RAFList;
27 import com.hughes.util.raf.RAFListSerializer;
28 import com.hughes.util.raf.RAFSerializable;
31 public class Dictionary implements RAFSerializable<Dictionary> {
33 static final int CACHE_SIZE = 5000;
35 static final int CURRENT_DICT_VERSION = 3;
36 static final String END_OF_DICTIONARY = "END OF DICTIONARY";
39 final int dictFileVersion;
40 final long creationMillis;
41 public final String dictInfo;
42 public final List<PairEntry> pairEntries;
43 public final List<TextEntry> textEntries;
44 public final List<EntrySource> sources;
45 public final List<Index> indices;
48 * dictFileVersion 1 adds:
49 * <li> links to sources?
51 * dictFileVersion 2 adds:
52 * <li> counts of tokens in indices.
55 public Dictionary(final String dictInfo) {
56 this.dictFileVersion = CURRENT_DICT_VERSION;
57 this.creationMillis = System.currentTimeMillis();
58 this.dictInfo = dictInfo;
59 pairEntries = new ArrayList<PairEntry>();
60 textEntries = new ArrayList<TextEntry>();
61 sources = new ArrayList<EntrySource>();
62 indices = new ArrayList<Index>();
65 public Dictionary(final RandomAccessFile raf) throws IOException {
66 dictFileVersion = raf.readInt();
67 if (dictFileVersion < 0 || dictFileVersion > CURRENT_DICT_VERSION) {
68 throw new IOException("Invalid dictionary version: " + dictFileVersion);
70 creationMillis = raf.readLong();
71 dictInfo = raf.readUTF();
73 // Load the sources, then seek past them, because reading them later disrupts the offset.
74 final RAFList<EntrySource> rafSources = RAFList.create(raf, new EntrySource.Serializer(this), raf.getFilePointer());
75 sources = new ArrayList<EntrySource>(rafSources);
76 raf.seek(rafSources.getEndOffset());
78 pairEntries = CachingList.create(RAFList.create(raf, new PairEntry.Serializer(this), raf.getFilePointer()), CACHE_SIZE);
79 textEntries = CachingList.create(RAFList.create(raf, new TextEntry.Serializer(this), raf.getFilePointer()), CACHE_SIZE);
80 indices = CachingList.createFullyCached(RAFList.create(raf, indexSerializer, raf.getFilePointer()));
81 final String end = raf.readUTF();
82 if (!end.equals(END_OF_DICTIONARY)) {
83 throw new IOException("Dictionary seems corrupt: " + end);
88 public void write(RandomAccessFile raf) throws IOException {
89 raf.writeInt(dictFileVersion);
90 raf.writeLong(creationMillis);
91 raf.writeUTF(dictInfo);
92 RAFList.write(raf, sources, new EntrySource.Serializer(this));
93 RAFList.write(raf, pairEntries, new PairEntry.Serializer(this));
94 RAFList.write(raf, textEntries, new TextEntry.Serializer(this));
95 RAFList.write(raf, indices, indexSerializer);
96 raf.writeUTF(END_OF_DICTIONARY);
99 private final RAFListSerializer<Index> indexSerializer = new RAFListSerializer<Index>() {
101 public Index read(RandomAccessFile raf, final int readIndex) throws IOException {
102 return new Index(Dictionary.this, raf);
105 public void write(RandomAccessFile raf, Index t) throws IOException {
109 public void print(final PrintStream out) {
110 out.println("dictInfo=" + dictInfo);
111 for (final EntrySource entrySource : sources) {
112 out.printf("EntrySource: %s %d\n", entrySource.name, entrySource.numEntries);
115 for (final Index index : indices) {
116 out.printf("Index: %s %s\n", index.shortName, index.longName);
122 public DictionaryInfo getDictionaryInfo() {
123 final DictionaryInfo result = new DictionaryInfo();
124 result.creationMillis = this.creationMillis;
125 result.dictInfo = this.dictInfo;
126 for (final Index index : indices) {
127 result.indexInfos.add(index.getIndexInfo());
132 public static DictionaryInfo getDictionaryInfo(final File file) {
133 RandomAccessFile raf = null;
135 raf = new RandomAccessFile(file, "r");
136 final Dictionary dict = new Dictionary(raf);
137 final DictionaryInfo dictionaryInfo = dict.getDictionaryInfo();
139 return dictionaryInfo;
140 } catch (IOException e) {
146 } catch (IOException e) {