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;
24 import java.io.IOException;
25 import java.io.PrintStream;
26 import java.io.RandomAccessFile;
27 import java.util.ArrayList;
28 import java.util.Collections;
29 import java.util.List;
31 public class Dictionary implements RAFSerializable<Dictionary> {
33 static final int CACHE_SIZE = 5000;
35 static final int CURRENT_DICT_VERSION = 6;
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<HtmlEntry> htmlEntries;
45 public final List<EntrySource> sources;
46 public final List<Index> indices;
49 * dictFileVersion 1 adds: <li>links to sources? dictFileVersion 2 adds: <li>
50 * counts of tokens in indices.
53 public Dictionary(final String dictInfo) {
54 this.dictFileVersion = CURRENT_DICT_VERSION;
55 this.creationMillis = System.currentTimeMillis();
56 this.dictInfo = dictInfo;
57 pairEntries = new ArrayList<PairEntry>();
58 textEntries = new ArrayList<TextEntry>();
59 htmlEntries = new ArrayList<HtmlEntry>();
60 sources = new ArrayList<EntrySource>();
61 indices = new ArrayList<Index>();
64 public Dictionary(final RandomAccessFile raf) throws IOException {
65 dictFileVersion = raf.readInt();
66 if (dictFileVersion < 0 || dictFileVersion > CURRENT_DICT_VERSION) {
67 throw new IOException("Invalid dictionary version: " + dictFileVersion);
69 creationMillis = raf.readLong();
70 dictInfo = raf.readUTF();
72 // Load the sources, then seek past them, because reading them later
73 // disrupts the offset.
75 final RAFList<EntrySource> rafSources = RAFList.create(raf, new EntrySource.Serializer(
76 this), raf.getFilePointer());
77 sources = new ArrayList<EntrySource>(rafSources);
78 raf.seek(rafSources.getEndOffset());
80 pairEntries = CachingList.create(
81 RAFList.create(raf, new PairEntry.Serializer(this), raf.getFilePointer()),
83 textEntries = CachingList.create(
84 RAFList.create(raf, new TextEntry.Serializer(this), raf.getFilePointer()),
86 if (dictFileVersion >= 5) {
87 htmlEntries = CachingList.create(
88 RAFList.create(raf, new HtmlEntry.Serializer(this), raf.getFilePointer()),
91 htmlEntries = Collections.emptyList();
93 indices = CachingList.createFullyCached(RAFList.create(raf, indexSerializer,
94 raf.getFilePointer()));
95 } catch (RuntimeException e) {
96 final IOException ioe = new IOException("RuntimeException loading dictionary");
100 final String end = raf.readUTF();
101 if (!end.equals(END_OF_DICTIONARY)) {
102 throw new IOException("Dictionary seems corrupt: " + end);
107 public void write(RandomAccessFile raf) throws IOException {
108 raf.writeInt(dictFileVersion);
109 raf.writeLong(creationMillis);
110 raf.writeUTF(dictInfo);
111 RAFList.write(raf, sources, new EntrySource.Serializer(this));
112 RAFList.write(raf, pairEntries, new PairEntry.Serializer(this));
113 RAFList.write(raf, textEntries, new TextEntry.Serializer(this));
114 RAFList.write(raf, htmlEntries, new HtmlEntry.Serializer(this));
115 RAFList.write(raf, indices, indexSerializer);
116 raf.writeUTF(END_OF_DICTIONARY);
119 private final RAFListSerializer<Index> indexSerializer = new RAFListSerializer<Index>() {
121 public Index read(RandomAccessFile raf, final int readIndex) throws IOException {
122 return new Index(Dictionary.this, raf);
126 public void write(RandomAccessFile raf, Index t) throws IOException {
131 final RAFListSerializer<HtmlEntry> htmlEntryIndexSerializer = new RAFListSerializer<HtmlEntry>() {
133 public void write(RandomAccessFile raf, HtmlEntry t) throws IOException {
135 throw new IndexOutOfBoundsException();
136 raf.writeInt(t.index());
140 public HtmlEntry read(RandomAccessFile raf, int readIndex) throws IOException {
141 return htmlEntries.get(raf.readInt());
145 public void print(final PrintStream out) {
146 out.println("dictInfo=" + dictInfo);
147 for (final EntrySource entrySource : sources) {
148 out.printf("EntrySource: %s %d\n", entrySource.name, entrySource.numEntries);
151 for (final Index index : indices) {
152 out.printf("Index: %s %s\n", index.shortName, index.longName);
158 public DictionaryInfo getDictionaryInfo() {
159 final DictionaryInfo result = new DictionaryInfo();
160 result.creationMillis = this.creationMillis;
161 result.dictInfo = this.dictInfo;
162 for (final Index index : indices) {
163 result.indexInfos.add(index.getIndexInfo());
168 public static DictionaryInfo getDictionaryInfo(final File file) {
169 RandomAccessFile raf = null;
171 raf = new RandomAccessFile(file, "r");
172 final Dictionary dict = new Dictionary(raf);
173 final DictionaryInfo dictionaryInfo = dict.getDictionaryInfo();
174 dictionaryInfo.uncompressedFilename = file.getName();
175 dictionaryInfo.uncompressedBytes = file.length();
177 return dictionaryInfo;
178 } catch (IOException e) {
184 } catch (IOException e) {