2 package com.hughes.android.dictionary.engine;
4 import android.content.Intent;
5 import android.util.Log;
7 import com.hughes.android.dictionary.C;
8 import com.hughes.util.StringUtil;
9 import com.hughes.util.raf.RAFListSerializer;
10 import com.hughes.util.raf.RAFSerializable;
11 import com.ibm.icu.text.Transliterator;
13 import java.io.IOException;
14 import java.io.PrintStream;
15 import java.io.RandomAccessFile;
16 import java.lang.ref.SoftReference;
17 import java.util.List;
18 import java.util.regex.Pattern;
20 public class HtmlEntry extends AbstractEntry implements RAFSerializable<HtmlEntry>,
21 Comparable<HtmlEntry> {
23 // Title is not HTML escaped.
24 public final String title;
25 public final LazyHtmlLoader lazyHtmlLoader;
28 public HtmlEntry(final EntrySource entrySource, String title) {
31 lazyHtmlLoader = null;
34 public HtmlEntry(Dictionary dictionary, RandomAccessFile raf, final int index)
36 super(dictionary, raf, index);
37 title = raf.readUTF();
38 lazyHtmlLoader = new LazyHtmlLoader(raf);
43 public void write(RandomAccessFile raf) throws IOException {
47 final byte[] bytes = getHtml().getBytes("UTF-8");
48 final byte[] zipBytes = StringUtil.zipBytes(bytes);
49 raf.writeInt(bytes.length);
50 raf.writeInt(zipBytes.length);
55 return html != null ? html : lazyHtmlLoader.getHtml();
59 public void addToDictionary(Dictionary dictionary) {
61 dictionary.htmlEntries.add(this);
62 index = dictionary.htmlEntries.size() - 1;
66 public RowBase CreateRow(int rowIndex, Index dictionaryIndex) {
67 return new Row(this.index, rowIndex, dictionaryIndex);
70 static final class Serializer implements RAFListSerializer<HtmlEntry> {
72 final Dictionary dictionary;
74 Serializer(Dictionary dictionary) {
75 this.dictionary = dictionary;
79 public HtmlEntry read(RandomAccessFile raf, final int index) throws IOException {
80 return new HtmlEntry(dictionary, raf, index);
84 public void write(RandomAccessFile raf, HtmlEntry t) throws IOException {
89 public String getRawText(final boolean compact) {
90 return title + ":\n" + getHtml();
94 public int compareTo(HtmlEntry another) {
95 if (title.compareTo(another.title) != 0) {
96 return title.compareTo(another.title);
98 return getHtml().compareTo(another.getHtml());
102 public String toString() {
103 return getRawText(false);
106 // --------------------------------------------------------------------
108 public static class Row extends RowBase {
110 boolean isExpanded = false;
112 Row(final RandomAccessFile raf, final int thisRowIndex,
113 final Index index) throws IOException {
114 super(raf, thisRowIndex, index);
117 Row(final int referenceIndex, final int thisRowIndex,
119 super(referenceIndex, thisRowIndex, index);
123 public String toString() {
124 return getRawText(false);
127 public HtmlEntry getEntry() {
128 return index.dict.htmlEntries.get(referenceIndex);
132 public void print(PrintStream out) {
133 final HtmlEntry entry = getEntry();
134 out.println("See also HtmlEntry:" + entry.title);
138 public String getRawText(boolean compact) {
139 final HtmlEntry entry = getEntry();
140 return entry.getRawText(compact);
144 public RowMatchType matches(final List<String> searchTokens,
145 final Pattern orderedMatchPattern, final Transliterator normalizer,
146 final boolean swapPairEntries) {
147 final String text = normalizer.transform(getRawText(false));
148 if (orderedMatchPattern.matcher(text).find()) {
149 return RowMatchType.ORDERED_MATCH;
151 for (int i = searchTokens.size() - 1; i >= 0; --i) {
152 final String searchToken = searchTokens.get(i);
153 if (!text.contains(searchToken)) {
154 return RowMatchType.NO_MATCH;
157 return RowMatchType.BAG_OF_WORDS_MATCH;
161 public static String htmlBody(final List<HtmlEntry> htmlEntries, final String indexShortName) {
162 final StringBuilder result = new StringBuilder();
163 for (final HtmlEntry htmlEntry : htmlEntries) {
164 final String titleEscaped = StringUtil.escapeUnicodeToPureHtml(htmlEntry.title);
165 result.append(String.format("<h1><a href=\"%s\">%s</a></h1>\n<p>%s\n",
166 formatQuickdicUrl(indexShortName, htmlEntry.title), titleEscaped,
167 htmlEntry.getHtml()));
169 return result.toString();
172 public static String formatQuickdicUrl(final String indexShortName, final String text) {
173 assert !indexShortName.contains(":");
174 assert text.length() > 0;
175 return String.format("q://d?%s&%s", indexShortName, StringUtil.encodeForUrl(text));
178 public static boolean isQuickdicUrl(String url) {
179 return url.startsWith("q://d?");
182 public static void quickdicUrlToIntent(final String url, final Intent intent) {
183 int firstColon = url.indexOf("?");
184 if (firstColon == -1)
186 int secondColon = url.indexOf("&", firstColon + 1);
187 if (secondColon == -1)
189 intent.putExtra(C.SEARCH_TOKEN, StringUtil.decodeFromUrl(url.substring(secondColon + 1)));
192 // --------------------------------------------------------------------
194 public static final class LazyHtmlLoader {
195 final RandomAccessFile raf;
198 final int numZipBytes;
200 // Not sure this volatile is right, but oh well.
201 volatile SoftReference<String> htmlRef = new SoftReference<String>(null);
203 private LazyHtmlLoader(final RandomAccessFile raf) throws IOException {
205 numBytes = raf.readInt();
206 numZipBytes = raf.readInt();
207 offset = raf.getFilePointer();
208 raf.skipBytes(numZipBytes);
211 public String getHtml() {
212 String html = htmlRef.get();
216 System.out.println("Loading Html: numBytes=" + numBytes + ", numZipBytes="
218 final byte[] bytes = new byte[numBytes];
219 final byte[] zipBytes = new byte[numZipBytes];
224 } catch (IOException e) {
225 throw new RuntimeException(e);
229 StringUtil.unzipFully(zipBytes, bytes);
230 html = new String(bytes, "UTF-8");
231 } catch (IOException e) {
232 throw new RuntimeException(e);
234 htmlRef = new SoftReference<String>(html);