]> gitweb.fperrin.net Git - Dictionary.git/blobdiff - src/com/hughes/android/dictionary/engine/Dictionary.java
First decent implementation of HtmlEntry attached to TokenRow.
[Dictionary.git] / src / com / hughes / android / dictionary / engine / Dictionary.java
index b00868ecb8c1d43c95626ccbc4c31b70fdf88ad6..9224f51deb87cf52bd7063100ac80dd7860e3446 100644 (file)
 
 package com.hughes.android.dictionary.engine;
 
+import java.io.File;
 import java.io.IOException;
 import java.io.PrintStream;
 import java.io.RandomAccessFile;
 import java.util.ArrayList;
+import java.util.Collections;
 import java.util.List;
 
 import com.hughes.android.dictionary.DictionaryInfo;
@@ -31,7 +33,7 @@ public class Dictionary implements RAFSerializable<Dictionary> {
   
   static final int CACHE_SIZE = 5000;
   
-  static final int CURRENT_DICT_VERSION = 2;
+  static final int CURRENT_DICT_VERSION = 6;
   static final String END_OF_DICTIONARY = "END OF DICTIONARY";
   
   // persisted
@@ -40,6 +42,7 @@ public class Dictionary implements RAFSerializable<Dictionary> {
   public final String dictInfo;
   public final List<PairEntry> pairEntries;
   public final List<TextEntry> textEntries;
+  public final List<HtmlEntry> htmlEntries;
   public final List<EntrySource> sources;
   public final List<Index> indices;
   
@@ -57,6 +60,7 @@ public class Dictionary implements RAFSerializable<Dictionary> {
     this.dictInfo = dictInfo;
     pairEntries = new ArrayList<PairEntry>();
     textEntries = new ArrayList<TextEntry>();
+    htmlEntries = new ArrayList<HtmlEntry>();
     sources = new ArrayList<EntrySource>();
     indices = new ArrayList<Index>();
   }
@@ -70,13 +74,24 @@ public class Dictionary implements RAFSerializable<Dictionary> {
     dictInfo = raf.readUTF();
     
     // Load the sources, then seek past them, because reading them later disrupts the offset.
-    final RAFList<EntrySource> rafSources = RAFList.create(raf, EntrySource.SERIALIZER, raf.getFilePointer());
-    sources = new ArrayList<EntrySource>(rafSources);
-    raf.seek(rafSources.getEndOffset());
-    
-    pairEntries = CachingList.create(RAFList.create(raf, new PairEntry.Serializer(this), raf.getFilePointer()), CACHE_SIZE);
-    textEntries = CachingList.create(RAFList.create(raf, new TextEntry.Serializer(this), raf.getFilePointer()), CACHE_SIZE);
-    indices = CachingList.createFullyCached(RAFList.create(raf, indexSerializer, raf.getFilePointer()));
+    try {
+      final RAFList<EntrySource> rafSources = RAFList.create(raf, new EntrySource.Serializer(this), raf.getFilePointer());
+      sources = new ArrayList<EntrySource>(rafSources);
+      raf.seek(rafSources.getEndOffset());
+      
+      pairEntries = CachingList.create(RAFList.create(raf, new PairEntry.Serializer(this), raf.getFilePointer()), CACHE_SIZE);
+      textEntries = CachingList.create(RAFList.create(raf, new TextEntry.Serializer(this), raf.getFilePointer()), CACHE_SIZE);
+      if (dictFileVersion >= 5) {
+        htmlEntries = CachingList.create(RAFList.create(raf, new HtmlEntry.Serializer(this), raf.getFilePointer()), CACHE_SIZE);
+      } else {
+        htmlEntries = Collections.emptyList();
+      }
+      indices = CachingList.createFullyCached(RAFList.create(raf, indexSerializer, raf.getFilePointer()));
+    } catch (RuntimeException e) {
+      final IOException ioe = new IOException("RuntimeException loading dictionary");
+      ioe.initCause(e);
+      throw ioe;
+    }
     final String end = raf.readUTF(); 
     if (!end.equals(END_OF_DICTIONARY)) {
       throw new IOException("Dictionary seems corrupt: " + end);
@@ -88,9 +103,10 @@ public class Dictionary implements RAFSerializable<Dictionary> {
     raf.writeInt(dictFileVersion);
     raf.writeLong(creationMillis);
     raf.writeUTF(dictInfo);
-    RAFList.write(raf, sources, EntrySource.SERIALIZER);
+    RAFList.write(raf, sources, new EntrySource.Serializer(this));
     RAFList.write(raf, pairEntries, new PairEntry.Serializer(this));
     RAFList.write(raf, textEntries, new TextEntry.Serializer(this));
+    RAFList.write(raf, htmlEntries, new HtmlEntry.Serializer(this));
     RAFList.write(raf, indices, indexSerializer);
     raf.writeUTF(END_OF_DICTIONARY);
   }
@@ -105,8 +121,23 @@ public class Dictionary implements RAFSerializable<Dictionary> {
       t.write(raf);
     }};
     
+    final RAFListSerializer<HtmlEntry> htmlEntryIndexSerializer = new RAFListSerializer<HtmlEntry>() {
+        @Override
+        public void write(RandomAccessFile raf, HtmlEntry t) throws IOException {
+            if (t.index() == -1) throw new IndexOutOfBoundsException();
+            raf.writeInt(t.index());
+        }
+        @Override
+        public HtmlEntry read(RandomAccessFile raf, int readIndex) throws IOException {
+            return htmlEntries.get(raf.readInt());
+        }}; 
+    
     public void print(final PrintStream out) {
       out.println("dictInfo=" + dictInfo);
+      for (final EntrySource entrySource : sources) {
+        out.printf("EntrySource: %s %d\n", entrySource.name, entrySource.numEntries);
+      }
+      out.println();
       for (final Index index : indices) {
         out.printf("Index: %s %s\n", index.shortName, index.longName);
         index.print(out);
@@ -123,6 +154,28 @@ public class Dictionary implements RAFSerializable<Dictionary> {
       }
       return result;
     }
-
+    
+    public static DictionaryInfo getDictionaryInfo(final File file) {
+      RandomAccessFile raf = null;
+      try {
+        raf = new RandomAccessFile(file, "r");
+        final Dictionary dict = new Dictionary(raf);
+        final DictionaryInfo dictionaryInfo = dict.getDictionaryInfo();
+        dictionaryInfo.uncompressedFilename = file.getName();
+        dictionaryInfo.uncompressedBytes = file.length();
+        raf.close();
+        return dictionaryInfo;
+      } catch (IOException e) {
+        return null;
+      } finally {
+        if (raf != null) {
+          try {
+            raf.close();
+          } catch (IOException e) {
+            e.printStackTrace();
+          }
+        }
+      }
+    }
 
 }
\ No newline at end of file