]> gitweb.fperrin.net Git - Dictionary.git/blob - src/com/hughes/android/dictionary/engine/Dictionary.java
Apply astyle code formattting.
[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.StringUtil;
20 import com.hughes.util.raf.RAFList;
21 import com.hughes.util.raf.RAFListSerializer;
22 import com.hughes.util.raf.RAFSerializable;
23
24 import java.io.DataInput;
25 import java.io.DataOutput;
26 import java.io.File;
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;
33
34 public class Dictionary implements RAFSerializable<Dictionary> {
35
36     static final int CACHE_SIZE = 5000;
37
38     static final int CURRENT_DICT_VERSION = 7;
39     static final String END_OF_DICTIONARY = "END OF DICTIONARY";
40
41     // persisted
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<byte[]> htmlData;
49     public final List<EntrySource> sources;
50     public final List<Index> indices;
51
52     /**
53      * dictFileVersion 1 adds: <li>links to sources? dictFileVersion 2 adds: <li>
54      * counts of tokens in indices.
55      */
56
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         htmlData = null;
65         sources = new ArrayList<EntrySource>();
66         indices = new ArrayList<Index>();
67     }
68
69     public Dictionary(final RandomAccessFile raf) throws IOException {
70         dictFileVersion = raf.readInt();
71         if (dictFileVersion < 0 || dictFileVersion > CURRENT_DICT_VERSION) {
72             throw new IOException("Invalid dictionary version: " + dictFileVersion);
73         }
74         creationMillis = raf.readLong();
75         dictInfo = raf.readUTF();
76
77         // Load the sources, then seek past them, because reading them later
78         // disrupts the offset.
79         try {
80             final RAFList<EntrySource> rafSources = RAFList.create(raf, new EntrySource.Serializer(
81                     this), raf.getFilePointer(), dictFileVersion, dictInfo + " sources: ");
82             sources = new ArrayList<EntrySource>(rafSources);
83             raf.seek(rafSources.getEndOffset());
84
85             pairEntries = CachingList.create(
86                               RAFList.create(raf, new PairEntry.Serializer(this), raf.getFilePointer(), dictFileVersion, dictInfo + " pairs: "),
87                               CACHE_SIZE);
88             textEntries = CachingList.create(
89                               RAFList.create(raf, new TextEntry.Serializer(this), raf.getFilePointer(), dictFileVersion, dictInfo + " text: "),
90                               CACHE_SIZE);
91             if (dictFileVersion >= 5) {
92                 htmlEntries = CachingList.create(
93                                   RAFList.create(raf, new HtmlEntry.Serializer(this), raf.getFilePointer(), dictFileVersion, dictInfo + " html: "),
94                                   CACHE_SIZE);
95             } else {
96                 htmlEntries = Collections.emptyList();
97             }
98             if (dictFileVersion >= 7) {
99                 htmlData = RAFList.create(raf, new HtmlEntry.DataDeserializer(), raf.getFilePointer(), dictFileVersion, dictInfo + " html: ");
100             } else {
101                 htmlData = null;
102             }
103             indices = CachingList.createFullyCached(RAFList.create(raf, indexSerializer,
104                                                     raf.getFilePointer(), dictFileVersion, dictInfo + " index: "));
105         } catch (RuntimeException e) {
106             final IOException ioe = new IOException("RuntimeException loading dictionary");
107             ioe.initCause(e);
108             throw ioe;
109         }
110         final String end = raf.readUTF();
111         if (!end.equals(END_OF_DICTIONARY)) {
112             throw new IOException("Dictionary seems corrupt: " + end);
113         }
114     }
115
116     @Override
117     public void write(DataOutput out) throws IOException {
118         RandomAccessFile raf = (RandomAccessFile)out;
119         raf.writeInt(dictFileVersion);
120         raf.writeLong(creationMillis);
121         raf.writeUTF(dictInfo);
122         System.out.println("sources start: " + raf.getFilePointer());
123         RAFList.write(raf, sources, new EntrySource.Serializer(this));
124         System.out.println("pair start: " + raf.getFilePointer());
125         RAFList.write(raf, pairEntries, new PairEntry.Serializer(this), 64, true);
126         System.out.println("text start: " + raf.getFilePointer());
127         RAFList.write(raf, textEntries, new TextEntry.Serializer(this));
128         System.out.println("html index start: " + raf.getFilePointer());
129         RAFList.write(raf, htmlEntries, new HtmlEntry.Serializer(this), 64, true);
130         System.out.println("html data start: " + raf.getFilePointer());
131         assert htmlData == null;
132         RAFList.write(raf, htmlEntries, new HtmlEntry.DataSerializer(), 128, true);
133         System.out.println("indices start: " + raf.getFilePointer());
134         RAFList.write(raf, indices, indexSerializer);
135         System.out.println("end: " + raf.getFilePointer());
136         raf.writeUTF(END_OF_DICTIONARY);
137     }
138
139     private final RAFListSerializer<Index> indexSerializer = new RAFListSerializer<Index>() {
140         @Override
141         public Index read(DataInput raf, final int readIndex) throws IOException {
142             return new Index(Dictionary.this, raf);
143         }
144
145         @Override
146         public void write(DataOutput raf, Index t) throws IOException {
147             t.write(raf);
148         }
149     };
150
151     final RAFListSerializer<HtmlEntry> htmlEntryIndexSerializer = new RAFListSerializer<HtmlEntry>() {
152         @Override
153         public void write(DataOutput raf, HtmlEntry t) throws IOException {
154             assert false;
155         }
156
157         @Override
158         public HtmlEntry read(DataInput raf, int readIndex) throws IOException {
159             return htmlEntries.get(raf.readInt());
160         }
161     };
162
163     public void print(final PrintStream out) {
164         out.println("dictInfo=" + dictInfo);
165         for (final EntrySource entrySource : sources) {
166             out.printf("EntrySource: %s %d\n", entrySource.name, entrySource.numEntries);
167         }
168         out.println();
169         for (final Index index : indices) {
170             out.printf("Index: %s %s\n", index.shortName, index.longName);
171             index.print(out);
172             out.println();
173         }
174     }
175
176     public DictionaryInfo getDictionaryInfo() {
177         final DictionaryInfo result = new DictionaryInfo();
178         result.creationMillis = this.creationMillis;
179         result.dictInfo = this.dictInfo;
180         for (final Index index : indices) {
181             result.indexInfos.add(index.getIndexInfo());
182         }
183         return result;
184     }
185
186     public static DictionaryInfo getDictionaryInfo(final File file) {
187         RandomAccessFile raf = null;
188         try {
189             raf = new RandomAccessFile(file, "r");
190             final Dictionary dict = new Dictionary(raf);
191             final DictionaryInfo dictionaryInfo = dict.getDictionaryInfo();
192             dictionaryInfo.uncompressedFilename = file.getName();
193             dictionaryInfo.uncompressedBytes = file.length();
194             raf.close();
195             return dictionaryInfo;
196         } catch (IOException e) {
197             final DictionaryInfo dictionaryInfo = new DictionaryInfo();
198             dictionaryInfo.uncompressedFilename = file.getName();
199             dictionaryInfo.uncompressedBytes = file.length();
200             return dictionaryInfo;
201         } finally {
202             if (raf != null) {
203                 try {
204                     raf.close();
205                 } catch (IOException e) {
206                     e.printStackTrace();
207                 }
208             }
209         }
210     }
211
212 }