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.Collections;
23 import java.util.List;
25 import com.hughes.android.dictionary.DictionaryInfo;
26 import com.hughes.util.CachingList;
27 import com.hughes.util.raf.RAFList;
28 import com.hughes.util.raf.RAFListSerializer;
29 import com.hughes.util.raf.RAFSerializable;
32 public class Dictionary implements RAFSerializable<Dictionary> {
34 static final int CACHE_SIZE = 5000;
36 static final int CURRENT_DICT_VERSION = 6;
37 static final String END_OF_DICTIONARY = "END OF DICTIONARY";
40 final int dictFileVersion;
41 final long creationMillis;
42 public final String dictInfo;
43 public final List<PairEntry> pairEntries;
44 public final List<TextEntry> textEntries;
45 public final List<HtmlEntry> htmlEntries;
46 public final List<EntrySource> sources;
47 public final List<Index> indices;
50 * dictFileVersion 1 adds:
51 * <li> links to sources?
53 * dictFileVersion 2 adds:
54 * <li> 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>();
64 sources = new ArrayList<EntrySource>();
65 indices = new ArrayList<Index>();
68 public Dictionary(final RandomAccessFile raf) throws IOException {
69 dictFileVersion = raf.readInt();
70 if (dictFileVersion < 0 || dictFileVersion > CURRENT_DICT_VERSION) {
71 throw new IOException("Invalid dictionary version: " + dictFileVersion);
73 creationMillis = raf.readLong();
74 dictInfo = raf.readUTF();
76 // Load the sources, then seek past them, because reading them later disrupts the offset.
78 final RAFList<EntrySource> rafSources = RAFList.create(raf, new EntrySource.Serializer(this), raf.getFilePointer());
79 sources = new ArrayList<EntrySource>(rafSources);
80 raf.seek(rafSources.getEndOffset());
82 pairEntries = CachingList.create(RAFList.create(raf, new PairEntry.Serializer(this), raf.getFilePointer()), CACHE_SIZE);
83 textEntries = CachingList.create(RAFList.create(raf, new TextEntry.Serializer(this), raf.getFilePointer()), CACHE_SIZE);
84 if (dictFileVersion >= 5) {
85 htmlEntries = CachingList.create(RAFList.create(raf, new HtmlEntry.Serializer(this), raf.getFilePointer()), CACHE_SIZE);
87 htmlEntries = Collections.emptyList();
89 indices = CachingList.createFullyCached(RAFList.create(raf, indexSerializer, raf.getFilePointer()));
90 } catch (RuntimeException e) {
91 final IOException ioe = new IOException("RuntimeException loading dictionary");
95 final String end = raf.readUTF();
96 if (!end.equals(END_OF_DICTIONARY)) {
97 throw new IOException("Dictionary seems corrupt: " + end);
102 public void write(RandomAccessFile raf) throws IOException {
103 raf.writeInt(dictFileVersion);
104 raf.writeLong(creationMillis);
105 raf.writeUTF(dictInfo);
106 RAFList.write(raf, sources, new EntrySource.Serializer(this));
107 RAFList.write(raf, pairEntries, new PairEntry.Serializer(this));
108 RAFList.write(raf, textEntries, new TextEntry.Serializer(this));
109 RAFList.write(raf, htmlEntries, new HtmlEntry.Serializer(this));
110 RAFList.write(raf, indices, indexSerializer);
111 raf.writeUTF(END_OF_DICTIONARY);
114 private final RAFListSerializer<Index> indexSerializer = new RAFListSerializer<Index>() {
116 public Index read(RandomAccessFile raf, final int readIndex) throws IOException {
117 return new Index(Dictionary.this, raf);
120 public void write(RandomAccessFile raf, Index t) throws IOException {
124 final RAFListSerializer<HtmlEntry> htmlEntryIndexSerializer = new RAFListSerializer<HtmlEntry>() {
126 public void write(RandomAccessFile raf, HtmlEntry t) throws IOException {
127 if (t.index() == -1) throw new IndexOutOfBoundsException();
128 raf.writeInt(t.index());
131 public HtmlEntry read(RandomAccessFile raf, int readIndex) throws IOException {
132 return htmlEntries.get(raf.readInt());
135 public void print(final PrintStream out) {
136 out.println("dictInfo=" + dictInfo);
137 for (final EntrySource entrySource : sources) {
138 out.printf("EntrySource: %s %d\n", entrySource.name, entrySource.numEntries);
141 for (final Index index : indices) {
142 out.printf("Index: %s %s\n", index.shortName, index.longName);
148 public DictionaryInfo getDictionaryInfo() {
149 final DictionaryInfo result = new DictionaryInfo();
150 result.creationMillis = this.creationMillis;
151 result.dictInfo = this.dictInfo;
152 for (final Index index : indices) {
153 result.indexInfos.add(index.getIndexInfo());
158 public static DictionaryInfo getDictionaryInfo(final File file) {
159 RandomAccessFile raf = null;
161 raf = new RandomAccessFile(file, "r");
162 final Dictionary dict = new Dictionary(raf);
163 final DictionaryInfo dictionaryInfo = dict.getDictionaryInfo();
164 dictionaryInfo.uncompressedFilename = file.getName();
165 dictionaryInfo.uncompressedBytes = file.length();
167 return dictionaryInfo;
168 } catch (IOException e) {
174 } catch (IOException e) {