]> gitweb.fperrin.net Git - Dictionary.git/blob - src/com/hughes/android/dictionary/engine/HtmlEntry.java
GZip HtmlEntry.
[Dictionary.git] / src / com / hughes / android / dictionary / engine / HtmlEntry.java
1 package com.hughes.android.dictionary.engine;
2
3 import android.content.Intent;
4
5 import com.hughes.android.dictionary.C;
6 import com.hughes.util.StringUtil;
7 import com.hughes.util.raf.RAFListSerializer;
8 import com.hughes.util.raf.RAFSerializable;
9 import com.ibm.icu.text.Transliterator;
10
11 import java.io.IOException;
12 import java.io.PrintStream;
13 import java.io.RandomAccessFile;
14 import java.util.List;
15 import java.util.regex.Pattern;
16
17 public class HtmlEntry extends AbstractEntry implements RAFSerializable<HtmlEntry>, Comparable<HtmlEntry> {
18   
19   // Title is not HTML escaped.
20   public final String title;
21   public String html;
22   
23   public HtmlEntry(final EntrySource entrySource, String title) {
24     super(entrySource);
25     this.title = title;
26   }
27   
28   public HtmlEntry(Dictionary dictionary, RandomAccessFile raf, final int index) throws IOException {
29     super(dictionary, raf, index);
30     title = raf.readUTF();
31
32     final byte[] bytes = new byte[raf.readInt()];
33     final byte[] zipBytes = new byte[raf.readInt()];
34     raf.read(zipBytes);
35     StringUtil.unzipFully(zipBytes, bytes);
36     html = new String(bytes, "UTF-8");
37   }
38   @Override
39   public void write(RandomAccessFile raf) throws IOException {
40     super.write(raf);
41     raf.writeUTF(title);
42
43     final byte[] bytes = html.getBytes("UTF-8");
44     final byte[] zipBytes = StringUtil.zipBytes(bytes);
45     raf.writeInt(bytes.length);
46     raf.writeInt(zipBytes.length);
47     raf.write(zipBytes);
48   }
49
50   @Override
51   public void addToDictionary(Dictionary dictionary) {
52     assert index == -1;
53     dictionary.htmlEntries.add(this);
54     index = dictionary.htmlEntries.size() - 1;
55   }
56   
57   @Override
58   public RowBase CreateRow(int rowIndex, Index dictionaryIndex) {
59     return new Row(this.index, rowIndex, dictionaryIndex);
60   }
61
62   
63   static final class Serializer implements RAFListSerializer<HtmlEntry> {
64     
65     final Dictionary dictionary;
66     
67     Serializer(Dictionary dictionary) {
68       this.dictionary = dictionary;
69     }
70
71     @Override
72     public HtmlEntry read(RandomAccessFile raf, final int index) throws IOException {
73       return new HtmlEntry(dictionary, raf, index);
74     }
75
76     @Override
77     public void write(RandomAccessFile raf, HtmlEntry t) throws IOException {
78       t.write(raf);
79     }
80   };
81
82   public String getRawText(final boolean compact) {
83     return title + ":\n" + html;
84   }
85
86   
87   @Override
88   public int compareTo(HtmlEntry another) {
89     if (title.compareTo(another.title) != 0) {
90       return title.compareTo(another.title);
91     }
92     return html.compareTo(another.html);
93   }
94   
95   @Override
96   public String toString() {
97     return getRawText(false);
98   }
99   
100   // --------------------------------------------------------------------
101   
102
103   public static class Row extends RowBase {
104     
105     boolean isExpanded = false;
106     
107     Row(final RandomAccessFile raf, final int thisRowIndex,
108         final Index index) throws IOException {
109       super(raf, thisRowIndex, index);
110     }
111
112     Row(final int referenceIndex, final int thisRowIndex,
113         final Index index) {
114       super(referenceIndex, thisRowIndex, index);
115     }
116     
117     @Override
118     public String toString() {
119       return getRawText(false);
120     }
121
122     public HtmlEntry getEntry() {
123       return index.dict.htmlEntries.get(referenceIndex);
124     }
125     
126     @Override
127     public void print(PrintStream out) {
128       final HtmlEntry entry = getEntry();
129       out.println("See also HtmlEntry:" + entry.title);
130     }
131
132     @Override
133     public String getRawText(boolean compact) {
134       final HtmlEntry entry = getEntry();
135       return entry.getRawText(compact);
136     }
137
138     @Override
139     public RowMatchType matches(final List<String> searchTokens, final Pattern orderedMatchPattern, final Transliterator normalizer, final boolean swapPairEntries) {
140       final String text = normalizer.transform(getRawText(false));
141       if (orderedMatchPattern.matcher(text).find()) {
142         return RowMatchType.ORDERED_MATCH;
143       }
144       for (int i = searchTokens.size() - 1; i >= 0; --i) {
145         final String searchToken = searchTokens.get(i);
146         if (!text.contains(searchToken)) {
147           return RowMatchType.NO_MATCH;
148         }
149       }
150       return RowMatchType.BAG_OF_WORDS_MATCH;
151     }
152   }
153
154     public static String htmlBody(final List<HtmlEntry> htmlEntries, final String indexShortName) {
155         final StringBuilder result = new StringBuilder();
156         for (final HtmlEntry htmlEntry : htmlEntries) {
157             final String titleEscaped = StringUtil.escapeToPureHtmlUnicode(htmlEntry.title);
158             result.append(String.format("<h1><a href=\"%s\">%s</a></h1>\n(%s)\n<p>%s\n", 
159                     formatQuickdicUrl(indexShortName, titleEscaped), titleEscaped, htmlEntry.entrySource.name,
160                     htmlEntry.html));
161         }
162         return result.toString();
163     }
164     
165     public static String formatQuickdicUrl(final String indexShortName, final String text) {
166         assert !indexShortName.contains(":");
167         return String.format("qd:%s:%s", indexShortName, text);
168     }
169
170     public static boolean isQuickdicUrl(String url) {
171         return url.startsWith("qd:");
172     }
173     
174     public static void quickdicUrlToIntent(final String url, final Intent intent) {
175         int firstColon = url.indexOf(":");
176         if (firstColon == -1) return;
177         int secondColon = url.indexOf(":", firstColon + 1);
178         if (secondColon == -1) return;
179         intent.putExtra(C.SEARCH_TOKEN, url.substring(secondColon + 1));
180     }
181
182 }