1 package com.hughes.android.dictionary.engine;
3 import android.content.Intent;
4 import android.net.Uri;
6 import com.hughes.android.dictionary.C;
7 import com.hughes.util.StringUtil;
8 import com.hughes.util.raf.RAFListSerializer;
9 import com.hughes.util.raf.RAFSerializable;
10 import com.ibm.icu.text.Transliterator;
12 import java.io.IOException;
13 import java.io.PrintStream;
14 import java.io.RandomAccessFile;
15 import java.io.UnsupportedEncodingException;
16 import java.lang.ref.SoftReference;
18 import java.net.URLEncoder;
19 import java.util.List;
20 import java.util.regex.Pattern;
22 public class HtmlEntry extends AbstractEntry implements RAFSerializable<HtmlEntry>, Comparable<HtmlEntry> {
24 // Title is not HTML escaped.
25 public final String title;
26 public final LazyHtmlLoader lazyHtmlLoader;
29 public HtmlEntry(final EntrySource entrySource, String title) {
32 lazyHtmlLoader = null;
35 public HtmlEntry(Dictionary dictionary, RandomAccessFile raf, final int index) throws IOException {
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();
95 public int compareTo(HtmlEntry another) {
96 if (title.compareTo(another.title) != 0) {
97 return title.compareTo(another.title);
99 return getHtml().compareTo(another.getHtml());
103 public String toString() {
104 return getRawText(false);
107 // --------------------------------------------------------------------
110 public static class Row extends RowBase {
112 boolean isExpanded = false;
114 Row(final RandomAccessFile raf, final int thisRowIndex,
115 final Index index) throws IOException {
116 super(raf, thisRowIndex, index);
119 Row(final int referenceIndex, final int thisRowIndex,
121 super(referenceIndex, thisRowIndex, index);
125 public String toString() {
126 return getRawText(false);
129 public HtmlEntry getEntry() {
130 return index.dict.htmlEntries.get(referenceIndex);
134 public void print(PrintStream out) {
135 final HtmlEntry entry = getEntry();
136 out.println("See also HtmlEntry:" + entry.title);
140 public String getRawText(boolean compact) {
141 final HtmlEntry entry = getEntry();
142 return entry.getRawText(compact);
146 public RowMatchType matches(final List<String> searchTokens, final Pattern orderedMatchPattern, final Transliterator normalizer, 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, titleEscaped), 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) return;
185 int secondColon = url.indexOf("&", firstColon + 1);
186 if (secondColon == -1) return;
187 intent.putExtra(C.SEARCH_TOKEN, StringUtil.decodeFromUrl(url.substring(secondColon + 1)));
190 // --------------------------------------------------------------------
192 public static final class LazyHtmlLoader {
193 final RandomAccessFile raf;
196 final int numZipBytes;
198 // Not sure this volatile is right, but oh well.
199 volatile SoftReference<String> htmlRef = new SoftReference<String>(null);
201 private LazyHtmlLoader(final RandomAccessFile raf) throws IOException {
203 numBytes = raf.readInt();
204 numZipBytes = raf.readInt();
205 offset = raf.getFilePointer();
206 raf.skipBytes(numZipBytes);
209 public String getHtml() {
210 String html = htmlRef.get();
214 System.out.println("Loading Html: numBytes=" + numBytes + ", numZipBytes=" + numZipBytes);
215 final byte[] bytes = new byte[numBytes];
216 final byte[] zipBytes = new byte[numZipBytes];
221 } catch (IOException e) {
222 throw new RuntimeException(e);
226 StringUtil.unzipFully(zipBytes, bytes);
227 html = new String(bytes, "UTF-8");
228 } catch (IOException e) {
229 throw new RuntimeException(e);
231 htmlRef = new SoftReference<String>(html);