]> gitweb.fperrin.net Git - Dictionary.git/blob - src/com/hughes/android/dictionary/engine/Dictionary.java
About dialog, added pictures, multi word search.
[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 java.io.File;
18 import java.io.IOException;
19 import java.io.PrintStream;
20 import java.io.RandomAccessFile;
21 import java.util.ArrayList;
22 import java.util.List;
23
24 import com.hughes.android.dictionary.DictionaryInfo;
25 import com.hughes.util.CachingList;
26 import com.hughes.util.raf.RAFList;
27 import com.hughes.util.raf.RAFListSerializer;
28 import com.hughes.util.raf.RAFSerializable;
29
30
31 public class Dictionary implements RAFSerializable<Dictionary> {
32   
33   static final int CACHE_SIZE = 5000;
34   
35   static final int CURRENT_DICT_VERSION = 4;
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<EntrySource> sources;
45   public final List<Index> indices;
46   
47   /**
48    * dictFileVersion 1 adds:
49    * <li> links to sources?
50    * 
51    * dictFileVersion 2 adds:
52    * <li> counts of tokens in indices.
53    */
54   
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     sources = new ArrayList<EntrySource>();
62     indices = new ArrayList<Index>();
63   }
64
65   public Dictionary(final RandomAccessFile raf) throws IOException {
66     dictFileVersion = raf.readInt();
67     if (dictFileVersion < 0 || dictFileVersion > CURRENT_DICT_VERSION) {
68       throw new IOException("Invalid dictionary version: " + dictFileVersion);
69     }
70     creationMillis = raf.readLong();
71     dictInfo = raf.readUTF();
72     
73     // Load the sources, then seek past them, because reading them later disrupts the offset.
74     try {
75       final RAFList<EntrySource> rafSources = RAFList.create(raf, new EntrySource.Serializer(this), raf.getFilePointer());
76       sources = new ArrayList<EntrySource>(rafSources);
77       raf.seek(rafSources.getEndOffset());
78       
79       pairEntries = CachingList.create(RAFList.create(raf, new PairEntry.Serializer(this), raf.getFilePointer()), CACHE_SIZE);
80       textEntries = CachingList.create(RAFList.create(raf, new TextEntry.Serializer(this), raf.getFilePointer()), CACHE_SIZE);
81       indices = CachingList.createFullyCached(RAFList.create(raf, indexSerializer, raf.getFilePointer()));
82     } catch (RuntimeException e) {
83       final IOException ioe = new IOException("RuntimeException loading dictionary");
84       ioe.initCause(e);
85       throw ioe;
86     }
87     final String end = raf.readUTF(); 
88     if (!end.equals(END_OF_DICTIONARY)) {
89       throw new IOException("Dictionary seems corrupt: " + end);
90     }
91   }
92   
93   @Override
94   public void write(RandomAccessFile raf) throws IOException {
95     raf.writeInt(dictFileVersion);
96     raf.writeLong(creationMillis);
97     raf.writeUTF(dictInfo);
98     RAFList.write(raf, sources, new EntrySource.Serializer(this));
99     RAFList.write(raf, pairEntries, new PairEntry.Serializer(this));
100     RAFList.write(raf, textEntries, new TextEntry.Serializer(this));
101     RAFList.write(raf, indices, indexSerializer);
102     raf.writeUTF(END_OF_DICTIONARY);
103   }
104
105   private final RAFListSerializer<Index> indexSerializer = new RAFListSerializer<Index>() {
106     @Override
107     public Index read(RandomAccessFile raf, final int readIndex) throws IOException {
108       return new Index(Dictionary.this, raf);
109     }
110     @Override
111     public void write(RandomAccessFile raf, Index t) throws IOException {
112       t.write(raf);
113     }};
114     
115     public void print(final PrintStream out) {
116       out.println("dictInfo=" + dictInfo);
117       for (final EntrySource entrySource : sources) {
118         out.printf("EntrySource: %s %d\n", entrySource.name, entrySource.numEntries);
119       }
120       out.println();
121       for (final Index index : indices) {
122         out.printf("Index: %s %s\n", index.shortName, index.longName);
123         index.print(out);
124         out.println();
125       }
126     }
127
128     public DictionaryInfo getDictionaryInfo() {
129       final DictionaryInfo result = new DictionaryInfo();
130       result.creationMillis = this.creationMillis;
131       result.dictInfo = this.dictInfo;
132       for (final Index index : indices) {
133         result.indexInfos.add(index.getIndexInfo());
134       }
135       return result;
136     }
137     
138     public static DictionaryInfo getDictionaryInfo(final File file) {
139       RandomAccessFile raf = null;
140       try {
141         raf = new RandomAccessFile(file, "r");
142         final Dictionary dict = new Dictionary(raf);
143         final DictionaryInfo dictionaryInfo = dict.getDictionaryInfo();
144         dictionaryInfo.uncompressedFilename = file.getName();
145         dictionaryInfo.uncompressedBytes = file.length();
146         raf.close();
147         return dictionaryInfo;
148       } catch (IOException e) {
149         return null;
150       } finally {
151         if (raf != null) {
152           try {
153             raf.close();
154           } catch (IOException e) {
155             e.printStackTrace();
156           }
157         }
158       }
159     }
160
161 }