]> gitweb.fperrin.net Git - Dictionary.git/blob - src/com/hughes/android/dictionary/engine/Dictionary.java
Optimize imports, update dictionary info.
[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
32 public class Dictionary implements RAFSerializable<Dictionary> {
33   
34   static final int CACHE_SIZE = 5000;
35   
36   static final int CURRENT_DICT_VERSION = 6;
37   static final String END_OF_DICTIONARY = "END OF DICTIONARY";
38   
39   // persisted
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;
48   
49   /**
50    * dictFileVersion 1 adds:
51    * <li> links to sources?
52    * 
53    * dictFileVersion 2 adds:
54    * <li> 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     sources = new ArrayList<EntrySource>();
65     indices = new ArrayList<Index>();
66   }
67
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);
72     }
73     creationMillis = raf.readLong();
74     dictInfo = raf.readUTF();
75     
76     // Load the sources, then seek past them, because reading them later disrupts the offset.
77     try {
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());
81       
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);
86       } else {
87         htmlEntries = Collections.emptyList();
88       }
89       indices = CachingList.createFullyCached(RAFList.create(raf, indexSerializer, raf.getFilePointer()));
90     } catch (RuntimeException e) {
91       final IOException ioe = new IOException("RuntimeException loading dictionary");
92       ioe.initCause(e);
93       throw ioe;
94     }
95     final String end = raf.readUTF(); 
96     if (!end.equals(END_OF_DICTIONARY)) {
97       throw new IOException("Dictionary seems corrupt: " + end);
98     }
99   }
100   
101   @Override
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);
112   }
113
114   private final RAFListSerializer<Index> indexSerializer = new RAFListSerializer<Index>() {
115     @Override
116     public Index read(RandomAccessFile raf, final int readIndex) throws IOException {
117       return new Index(Dictionary.this, raf);
118     }
119     @Override
120     public void write(RandomAccessFile raf, Index t) throws IOException {
121       t.write(raf);
122     }};
123     
124     final RAFListSerializer<HtmlEntry> htmlEntryIndexSerializer = new RAFListSerializer<HtmlEntry>() {
125         @Override
126         public void write(RandomAccessFile raf, HtmlEntry t) throws IOException {
127             if (t.index() == -1) throw new IndexOutOfBoundsException();
128             raf.writeInt(t.index());
129         }
130         @Override
131         public HtmlEntry read(RandomAccessFile raf, int readIndex) throws IOException {
132             return htmlEntries.get(raf.readInt());
133         }}; 
134     
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);
139       }
140       out.println();
141       for (final Index index : indices) {
142         out.printf("Index: %s %s\n", index.shortName, index.longName);
143         index.print(out);
144         out.println();
145       }
146     }
147
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());
154       }
155       return result;
156     }
157     
158     public static DictionaryInfo getDictionaryInfo(final File file) {
159       RandomAccessFile raf = null;
160       try {
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();
166         raf.close();
167         return dictionaryInfo;
168       } catch (IOException e) {
169         return null;
170       } finally {
171         if (raf != null) {
172           try {
173             raf.close();
174           } catch (IOException e) {
175             e.printStackTrace();
176           }
177         }
178       }
179     }
180
181 }