]> gitweb.fperrin.net Git - Dictionary.git/blob - src/com/hughes/android/dictionary/engine/Dictionary.java
Auto-format everything.
[Dictionary.git] / src / com / hughes / android / dictionary / engine / Dictionary.java
1 // Copyright 2011 Google Inc. All Rights Reserved.
2 //
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
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
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.
14
15 package com.hughes.android.dictionary.engine;
16
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;
22
23 import java.io.File;
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;
30
31 public class Dictionary implements RAFSerializable<Dictionary> {
32
33     static final int CACHE_SIZE = 5000;
34
35     static final int CURRENT_DICT_VERSION = 6;
36     static final String END_OF_DICTIONARY = "END OF DICTIONARY";
37
38     // persisted
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;
47
48     /**
49      * dictFileVersion 1 adds: <li>links to sources? dictFileVersion 2 adds: <li>
50      * counts of tokens in indices.
51      */
52
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>();
62     }
63
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);
68         }
69         creationMillis = raf.readLong();
70         dictInfo = raf.readUTF();
71
72         // Load the sources, then seek past them, because reading them later
73         // disrupts the offset.
74         try {
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());
79
80             pairEntries = CachingList.create(
81                     RAFList.create(raf, new PairEntry.Serializer(this), raf.getFilePointer()),
82                     CACHE_SIZE);
83             textEntries = CachingList.create(
84                     RAFList.create(raf, new TextEntry.Serializer(this), raf.getFilePointer()),
85                     CACHE_SIZE);
86             if (dictFileVersion >= 5) {
87                 htmlEntries = CachingList.create(
88                         RAFList.create(raf, new HtmlEntry.Serializer(this), raf.getFilePointer()),
89                         CACHE_SIZE);
90             } else {
91                 htmlEntries = Collections.emptyList();
92             }
93             indices = CachingList.createFullyCached(RAFList.create(raf, indexSerializer,
94                     raf.getFilePointer()));
95         } catch (RuntimeException e) {
96             final IOException ioe = new IOException("RuntimeException loading dictionary");
97             ioe.initCause(e);
98             throw ioe;
99         }
100         final String end = raf.readUTF();
101         if (!end.equals(END_OF_DICTIONARY)) {
102             throw new IOException("Dictionary seems corrupt: " + end);
103         }
104     }
105
106     @Override
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);
117     }
118
119     private final RAFListSerializer<Index> indexSerializer = new RAFListSerializer<Index>() {
120         @Override
121         public Index read(RandomAccessFile raf, final int readIndex) throws IOException {
122             return new Index(Dictionary.this, raf);
123         }
124
125         @Override
126         public void write(RandomAccessFile raf, Index t) throws IOException {
127             t.write(raf);
128         }
129     };
130
131     final RAFListSerializer<HtmlEntry> htmlEntryIndexSerializer = new RAFListSerializer<HtmlEntry>() {
132         @Override
133         public void write(RandomAccessFile raf, HtmlEntry t) throws IOException {
134             if (t.index() == -1)
135                 throw new IndexOutOfBoundsException();
136             raf.writeInt(t.index());
137         }
138
139         @Override
140         public HtmlEntry read(RandomAccessFile raf, int readIndex) throws IOException {
141             return htmlEntries.get(raf.readInt());
142         }
143     };
144
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);
149         }
150         out.println();
151         for (final Index index : indices) {
152             out.printf("Index: %s %s\n", index.shortName, index.longName);
153             index.print(out);
154             out.println();
155         }
156     }
157
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());
164         }
165         return result;
166     }
167
168     public static DictionaryInfo getDictionaryInfo(final File file) {
169         RandomAccessFile raf = null;
170         try {
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();
176             raf.close();
177             return dictionaryInfo;
178         } catch (IOException e) {
179             return null;
180         } finally {
181             if (raf != null) {
182                 try {
183                     raf.close();
184                 } catch (IOException e) {
185                     e.printStackTrace();
186                 }
187             }
188         }
189     }
190
191 }