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;
17 import com.hughes.android.dictionary.DictionaryInfo;
18 import com.hughes.util.CachingList;
19 import com.hughes.util.StringUtil;
20 import com.hughes.util.raf.RAFList;
21 import com.hughes.util.raf.RAFListSerializer;
22 import com.hughes.util.raf.RAFSerializable;
24 import java.io.DataInput;
25 import java.io.DataOutput;
27 import java.io.IOException;
28 import java.io.PrintStream;
29 import java.io.RandomAccessFile;
30 import java.util.ArrayList;
31 import java.util.Collections;
32 import java.util.List;
34 public class Dictionary implements RAFSerializable<Dictionary> {
36 static final int CACHE_SIZE = 5000;
38 static final int CURRENT_DICT_VERSION = 7;
39 static final String END_OF_DICTIONARY = "END OF DICTIONARY";
42 final int dictFileVersion;
43 final long creationMillis;
44 public final String dictInfo;
45 public final List<PairEntry> pairEntries;
46 public final List<TextEntry> textEntries;
47 public final List<HtmlEntry> htmlEntries;
48 public final List<byte[]> htmlData;
49 public final List<EntrySource> sources;
50 public final List<Index> indices;
53 * dictFileVersion 1 adds: <li>links to sources? dictFileVersion 2 adds: <li>
54 * counts of tokens in indices.
57 public Dictionary(final String dictInfo) {
58 this.dictFileVersion = CURRENT_DICT_VERSION;
59 this.creationMillis = System.currentTimeMillis();
60 this.dictInfo = dictInfo;
61 pairEntries = new ArrayList<PairEntry>();
62 textEntries = new ArrayList<TextEntry>();
63 htmlEntries = new ArrayList<HtmlEntry>();
65 sources = new ArrayList<EntrySource>();
66 indices = new ArrayList<Index>();
69 public Dictionary(final RandomAccessFile raf) throws IOException {
70 dictFileVersion = raf.readInt();
71 if (dictFileVersion < 0 || dictFileVersion > CURRENT_DICT_VERSION) {
72 throw new IOException("Invalid dictionary version: " + dictFileVersion);
74 creationMillis = raf.readLong();
75 dictInfo = raf.readUTF();
77 // Load the sources, then seek past them, because reading them later
78 // disrupts the offset.
80 final RAFList<EntrySource> rafSources = RAFList.create(raf, new EntrySource.Serializer(
81 this), raf.getFilePointer(), dictFileVersion, dictInfo + " sources: ");
82 sources = new ArrayList<EntrySource>(rafSources);
83 raf.seek(rafSources.getEndOffset());
85 pairEntries = CachingList.create(
86 RAFList.create(raf, new PairEntry.Serializer(this), raf.getFilePointer(), dictFileVersion, dictInfo + " pairs: "),
88 textEntries = CachingList.create(
89 RAFList.create(raf, new TextEntry.Serializer(this), raf.getFilePointer(), dictFileVersion, dictInfo + " text: "),
91 if (dictFileVersion >= 5) {
92 htmlEntries = CachingList.create(
93 RAFList.create(raf, new HtmlEntry.Serializer(this), raf.getFilePointer(), dictFileVersion, dictInfo + " html: "),
96 htmlEntries = Collections.emptyList();
98 if (dictFileVersion >= 7) {
99 htmlData = RAFList.create(raf, new HtmlEntry.DataDeserializer(), raf.getFilePointer(), dictFileVersion, dictInfo + " html: ");
103 indices = CachingList.createFullyCached(RAFList.create(raf, indexSerializer,
104 raf.getFilePointer(), dictFileVersion, dictInfo + " index: "));
105 } catch (RuntimeException e) {
106 final IOException ioe = new IOException("RuntimeException loading dictionary");
110 final String end = raf.readUTF();
111 if (!end.equals(END_OF_DICTIONARY)) {
112 throw new IOException("Dictionary seems corrupt: " + end);
117 public void write(DataOutput out) throws IOException {
118 RandomAccessFile raf = (RandomAccessFile)out;
119 raf.writeInt(dictFileVersion);
120 raf.writeLong(creationMillis);
121 raf.writeUTF(dictInfo);
122 System.out.println("sources start: " + raf.getFilePointer());
123 RAFList.write(raf, sources, new EntrySource.Serializer(this));
124 System.out.println("pair start: " + raf.getFilePointer());
125 RAFList.write(raf, pairEntries, new PairEntry.Serializer(this), 64, true);
126 System.out.println("text start: " + raf.getFilePointer());
127 RAFList.write(raf, textEntries, new TextEntry.Serializer(this));
128 System.out.println("html index start: " + raf.getFilePointer());
129 RAFList.write(raf, htmlEntries, new HtmlEntry.Serializer(this), 64, true);
130 System.out.println("html data start: " + raf.getFilePointer());
131 assert htmlData == null;
132 RAFList.write(raf, htmlEntries, new HtmlEntry.DataSerializer(), 128, true);
133 System.out.println("indices start: " + raf.getFilePointer());
134 RAFList.write(raf, indices, indexSerializer);
135 System.out.println("end: " + raf.getFilePointer());
136 raf.writeUTF(END_OF_DICTIONARY);
139 private final RAFListSerializer<Index> indexSerializer = new RAFListSerializer<Index>() {
141 public Index read(DataInput raf, final int readIndex) throws IOException {
142 return new Index(Dictionary.this, raf);
146 public void write(DataOutput raf, Index t) throws IOException {
151 final RAFListSerializer<HtmlEntry> htmlEntryIndexSerializer = new RAFListSerializer<HtmlEntry>() {
153 public void write(DataOutput raf, HtmlEntry t) throws IOException {
158 public HtmlEntry read(DataInput raf, int readIndex) throws IOException {
159 return htmlEntries.get(raf.readInt());
163 public void print(final PrintStream out) {
164 out.println("dictInfo=" + dictInfo);
165 for (final EntrySource entrySource : sources) {
166 out.printf("EntrySource: %s %d\n", entrySource.name, entrySource.numEntries);
169 for (final Index index : indices) {
170 out.printf("Index: %s %s\n", index.shortName, index.longName);
176 public DictionaryInfo getDictionaryInfo() {
177 final DictionaryInfo result = new DictionaryInfo();
178 result.creationMillis = this.creationMillis;
179 result.dictInfo = this.dictInfo;
180 for (final Index index : indices) {
181 result.indexInfos.add(index.getIndexInfo());
186 public static DictionaryInfo getDictionaryInfo(final File file) {
187 RandomAccessFile raf = null;
189 raf = new RandomAccessFile(file, "r");
190 final Dictionary dict = new Dictionary(raf);
191 final DictionaryInfo dictionaryInfo = dict.getDictionaryInfo();
192 dictionaryInfo.uncompressedFilename = file.getName();
193 dictionaryInfo.uncompressedBytes = file.length();
195 return dictionaryInfo;
196 } catch (IOException e) {
197 final DictionaryInfo dictionaryInfo = new DictionaryInfo();
198 dictionaryInfo.uncompressedFilename = file.getName();
199 dictionaryInfo.uncompressedBytes = file.length();
200 return dictionaryInfo;
205 } catch (IOException e) {