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<EntrySource> sources;
49 public final List<Index> indices;
52 * dictFileVersion 1 adds: <li>links to sources? dictFileVersion 2 adds: <li>
53 * counts of tokens in indices.
56 public Dictionary(final String dictInfo) {
57 this.dictFileVersion = CURRENT_DICT_VERSION;
58 this.creationMillis = System.currentTimeMillis();
59 this.dictInfo = dictInfo;
60 pairEntries = new ArrayList<PairEntry>();
61 textEntries = new ArrayList<TextEntry>();
62 htmlEntries = new ArrayList<HtmlEntry>();
63 sources = new ArrayList<EntrySource>();
64 indices = new ArrayList<Index>();
67 public Dictionary(final RandomAccessFile raf) throws IOException {
68 dictFileVersion = raf.readInt();
69 if (dictFileVersion < 0 || dictFileVersion > CURRENT_DICT_VERSION) {
70 throw new IOException("Invalid dictionary version: " + dictFileVersion);
72 creationMillis = raf.readLong();
73 dictInfo = raf.readUTF();
75 // Load the sources, then seek past them, because reading them later
76 // disrupts the offset.
78 final RAFList<EntrySource> rafSources = RAFList.create(raf, new EntrySource.Serializer(
79 this), raf.getFilePointer(), dictFileVersion);
80 sources = new ArrayList<EntrySource>(rafSources);
81 raf.seek(rafSources.getEndOffset());
83 pairEntries = CachingList.create(
84 RAFList.create(raf, new PairEntry.Serializer(this), raf.getFilePointer(), dictFileVersion, dictFileVersion >= 7 ? 64 : 1, dictFileVersion >= 7),
86 textEntries = CachingList.create(
87 RAFList.create(raf, new TextEntry.Serializer(this), raf.getFilePointer(), dictFileVersion),
89 if (dictFileVersion >= 5) {
90 htmlEntries = CachingList.create(
91 RAFList.create(raf, new HtmlEntry.Serializer(this), raf.getFilePointer(), dictFileVersion),
94 htmlEntries = Collections.emptyList();
96 indices = CachingList.createFullyCached(RAFList.create(raf, indexSerializer,
97 raf.getFilePointer(), dictFileVersion));
98 } catch (RuntimeException e) {
99 final IOException ioe = new IOException("RuntimeException loading dictionary");
103 final String end = raf.readUTF();
104 if (!end.equals(END_OF_DICTIONARY)) {
105 throw new IOException("Dictionary seems corrupt: " + end);
110 public void write(DataOutput out) throws IOException {
111 RandomAccessFile raf = (RandomAccessFile)out;
112 raf.writeInt(dictFileVersion);
113 raf.writeLong(creationMillis);
114 raf.writeUTF(dictInfo);
115 System.out.println("sources start: " + raf.getFilePointer());
116 RAFList.write(raf, sources, new EntrySource.Serializer(this));
117 System.out.println("pair start: " + raf.getFilePointer());
118 RAFList.write(raf, pairEntries, new PairEntry.Serializer(this), 64, true);
119 System.out.println("text start: " + raf.getFilePointer());
120 RAFList.write(raf, textEntries, new TextEntry.Serializer(this));
121 System.out.println("html start: " + raf.getFilePointer());
122 RAFList.write(raf, htmlEntries, new HtmlEntry.Serializer(this));
123 System.out.println("indices start: " + raf.getFilePointer());
124 RAFList.write(raf, indices, indexSerializer);
125 System.out.println("end: " + raf.getFilePointer());
126 raf.writeUTF(END_OF_DICTIONARY);
129 private final RAFListSerializer<Index> indexSerializer = new RAFListSerializer<Index>() {
131 public Index read(DataInput raf, final int readIndex) throws IOException {
132 return new Index(Dictionary.this, raf);
136 public void write(DataOutput raf, Index t) throws IOException {
141 final RAFListSerializer<HtmlEntry> htmlEntryIndexSerializer = new RAFListSerializer<HtmlEntry>() {
143 public void write(DataOutput raf, HtmlEntry t) throws IOException {
145 throw new IndexOutOfBoundsException();
146 raf.writeInt(t.index());
150 public HtmlEntry read(DataInput raf, int readIndex) throws IOException {
151 return htmlEntries.get(raf.readInt());
155 public void print(final PrintStream out) {
156 out.println("dictInfo=" + dictInfo);
157 for (final EntrySource entrySource : sources) {
158 out.printf("EntrySource: %s %d\n", entrySource.name, entrySource.numEntries);
161 for (final Index index : indices) {
162 out.printf("Index: %s %s\n", index.shortName, index.longName);
168 public DictionaryInfo getDictionaryInfo() {
169 final DictionaryInfo result = new DictionaryInfo();
170 result.creationMillis = this.creationMillis;
171 result.dictInfo = this.dictInfo;
172 for (final Index index : indices) {
173 result.indexInfos.add(index.getIndexInfo());
178 public static DictionaryInfo getDictionaryInfo(final File file) {
179 RandomAccessFile raf = null;
181 raf = new RandomAccessFile(file, "r");
182 final Dictionary dict = new Dictionary(raf);
183 final DictionaryInfo dictionaryInfo = dict.getDictionaryInfo();
184 dictionaryInfo.uncompressedFilename = file.getName();
185 dictionaryInfo.uncompressedBytes = file.length();
187 return dictionaryInfo;
188 } catch (IOException e) {
194 } catch (IOException e) {