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.raf.RAFList;
20 import com.hughes.util.raf.RAFListSerializer;
21 import com.hughes.util.raf.RAFSerializable;
23 import java.io.DataInput;
24 import java.io.DataOutput;
26 import java.io.IOException;
27 import java.io.PrintStream;
28 import java.io.RandomAccessFile;
29 import java.util.ArrayList;
30 import java.util.Collections;
31 import java.util.List;
33 public class Dictionary implements RAFSerializable<Dictionary> {
35 static final int CACHE_SIZE = 5000;
37 static final int CURRENT_DICT_VERSION = 6;
38 static final String END_OF_DICTIONARY = "END OF DICTIONARY";
41 final int dictFileVersion;
42 final long creationMillis;
43 public final String dictInfo;
44 public final List<PairEntry> pairEntries;
45 public final List<TextEntry> textEntries;
46 public final List<HtmlEntry> htmlEntries;
47 public final List<EntrySource> sources;
48 public final List<Index> indices;
51 * dictFileVersion 1 adds: <li>links to sources? dictFileVersion 2 adds: <li>
52 * 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 htmlEntries = new ArrayList<HtmlEntry>();
62 sources = new ArrayList<EntrySource>();
63 indices = new ArrayList<Index>();
66 public Dictionary(final RandomAccessFile raf) throws IOException {
67 dictFileVersion = raf.readInt();
68 if (dictFileVersion < 0 || dictFileVersion > CURRENT_DICT_VERSION) {
69 throw new IOException("Invalid dictionary version: " + dictFileVersion);
71 creationMillis = raf.readLong();
72 dictInfo = raf.readUTF();
74 // Load the sources, then seek past them, because reading them later
75 // disrupts the offset.
77 final RAFList<EntrySource> rafSources = RAFList.create(raf, new EntrySource.Serializer(
78 this), raf.getFilePointer());
79 sources = new ArrayList<EntrySource>(rafSources);
80 raf.seek(rafSources.getEndOffset());
82 pairEntries = CachingList.create(
83 RAFList.create(raf, new PairEntry.Serializer(this), raf.getFilePointer()),
85 textEntries = CachingList.create(
86 RAFList.create(raf, new TextEntry.Serializer(this), raf.getFilePointer()),
88 if (dictFileVersion >= 5) {
89 htmlEntries = CachingList.create(
90 RAFList.create(raf, new HtmlEntry.Serializer(this), raf.getFilePointer()),
93 htmlEntries = Collections.emptyList();
95 indices = CachingList.createFullyCached(RAFList.create(raf, indexSerializer,
96 raf.getFilePointer()));
97 } catch (RuntimeException e) {
98 final IOException ioe = new IOException("RuntimeException loading dictionary");
102 final String end = raf.readUTF();
103 if (!end.equals(END_OF_DICTIONARY)) {
104 throw new IOException("Dictionary seems corrupt: " + end);
109 public void write(DataOutput out) throws IOException {
110 RandomAccessFile raf = (RandomAccessFile)out;
111 raf.writeInt(dictFileVersion);
112 raf.writeLong(creationMillis);
113 raf.writeUTF(dictInfo);
114 RAFList.write(raf, sources, new EntrySource.Serializer(this));
115 RAFList.write(raf, pairEntries, new PairEntry.Serializer(this));
116 RAFList.write(raf, textEntries, new TextEntry.Serializer(this));
117 RAFList.write(raf, htmlEntries, new HtmlEntry.Serializer(this));
118 RAFList.write(raf, indices, indexSerializer);
119 raf.writeUTF(END_OF_DICTIONARY);
122 private final RAFListSerializer<Index> indexSerializer = new RAFListSerializer<Index>() {
124 public Index read(DataInput raf, final int readIndex) throws IOException {
125 return new Index(Dictionary.this, raf);
129 public void write(DataOutput raf, Index t) throws IOException {
134 final RAFListSerializer<HtmlEntry> htmlEntryIndexSerializer = new RAFListSerializer<HtmlEntry>() {
136 public void write(DataOutput raf, HtmlEntry t) throws IOException {
138 throw new IndexOutOfBoundsException();
139 raf.writeInt(t.index());
143 public HtmlEntry read(DataInput raf, int readIndex) throws IOException {
144 return htmlEntries.get(raf.readInt());
148 public void print(final PrintStream out) {
149 out.println("dictInfo=" + dictInfo);
150 for (final EntrySource entrySource : sources) {
151 out.printf("EntrySource: %s %d\n", entrySource.name, entrySource.numEntries);
154 for (final Index index : indices) {
155 out.printf("Index: %s %s\n", index.shortName, index.longName);
161 public DictionaryInfo getDictionaryInfo() {
162 final DictionaryInfo result = new DictionaryInfo();
163 result.creationMillis = this.creationMillis;
164 result.dictInfo = this.dictInfo;
165 for (final Index index : indices) {
166 result.indexInfos.add(index.getIndexInfo());
171 public static DictionaryInfo getDictionaryInfo(final File file) {
172 RandomAccessFile raf = null;
174 raf = new RandomAccessFile(file, "r");
175 final Dictionary dict = new Dictionary(raf);
176 final DictionaryInfo dictionaryInfo = dict.getDictionaryInfo();
177 dictionaryInfo.uncompressedFilename = file.getName();
178 dictionaryInfo.uncompressedBytes = file.length();
180 return dictionaryInfo;
181 } catch (IOException e) {
187 } catch (IOException e) {