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;
17 import java.net.URLEncoder;
18 import java.util.List;
19 import java.util.regex.Pattern;
21 public class HtmlEntry extends AbstractEntry implements RAFSerializable<HtmlEntry>, 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) throws IOException {
35 super(dictionary, raf, index);
36 title = raf.readUTF();
37 lazyHtmlLoader = new LazyHtmlLoader(raf);
42 public void write(RandomAccessFile raf) throws IOException {
46 final byte[] bytes = getHtml().getBytes("UTF-8");
47 final byte[] zipBytes = StringUtil.zipBytes(bytes);
48 raf.writeInt(bytes.length);
49 raf.writeInt(zipBytes.length);
54 return html != null ? html : lazyHtmlLoader.getHtml();
58 public void addToDictionary(Dictionary dictionary) {
60 dictionary.htmlEntries.add(this);
61 index = dictionary.htmlEntries.size() - 1;
65 public RowBase CreateRow(int rowIndex, Index dictionaryIndex) {
66 return new Row(this.index, rowIndex, dictionaryIndex);
69 static final class Serializer implements RAFListSerializer<HtmlEntry> {
71 final Dictionary dictionary;
73 Serializer(Dictionary dictionary) {
74 this.dictionary = dictionary;
78 public HtmlEntry read(RandomAccessFile raf, final int index) throws IOException {
79 return new HtmlEntry(dictionary, raf, index);
83 public void write(RandomAccessFile raf, HtmlEntry t) throws IOException {
88 public String getRawText(final boolean compact) {
89 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 // --------------------------------------------------------------------
109 public static class Row extends RowBase {
111 boolean isExpanded = false;
113 Row(final RandomAccessFile raf, final int thisRowIndex,
114 final Index index) throws IOException {
115 super(raf, thisRowIndex, index);
118 Row(final int referenceIndex, final int thisRowIndex,
120 super(referenceIndex, thisRowIndex, index);
124 public String toString() {
125 return getRawText(false);
128 public HtmlEntry getEntry() {
129 return index.dict.htmlEntries.get(referenceIndex);
133 public void print(PrintStream out) {
134 final HtmlEntry entry = getEntry();
135 out.println("See also HtmlEntry:" + entry.title);
139 public String getRawText(boolean compact) {
140 final HtmlEntry entry = getEntry();
141 return entry.getRawText(compact);
145 public RowMatchType matches(final List<String> searchTokens, final Pattern orderedMatchPattern, final Transliterator normalizer, final boolean swapPairEntries) {
146 final String text = normalizer.transform(getRawText(false));
147 if (orderedMatchPattern.matcher(text).find()) {
148 return RowMatchType.ORDERED_MATCH;
150 for (int i = searchTokens.size() - 1; i >= 0; --i) {
151 final String searchToken = searchTokens.get(i);
152 if (!text.contains(searchToken)) {
153 return RowMatchType.NO_MATCH;
156 return RowMatchType.BAG_OF_WORDS_MATCH;
160 public static String htmlBody(final List<HtmlEntry> htmlEntries, final String indexShortName) {
161 final StringBuilder result = new StringBuilder();
162 for (final HtmlEntry htmlEntry : htmlEntries) {
163 final String titleEscaped = StringUtil.escapeToPureHtmlUnicode(htmlEntry.title);
164 result.append(String.format("<h1><a href=\"%s\">%s</a></h1>\n<p>%s\n",
165 formatQuickdicUrl(indexShortName, titleEscaped), titleEscaped,
166 htmlEntry.getHtml()));
168 return result.toString();
171 public static String formatQuickdicUrl(final String indexShortName, final String text) {
172 assert !indexShortName.contains(":");
173 assert text.length() > 0;
175 return String.format("qd:%s:%s", indexShortName, URLEncoder.encode(text, "UTF-8"));
176 } catch (UnsupportedEncodingException e) {
177 throw new RuntimeException(e);
181 public static boolean isQuickdicUrl(String url) {
182 return url.startsWith("qd:");
185 public static void quickdicUrlToIntent(final String url, final Intent intent) {
186 int firstColon = url.indexOf(":");
187 if (firstColon == -1) return;
188 int secondColon = url.indexOf(":", firstColon + 1);
189 if (secondColon == -1) return;
190 intent.putExtra(C.SEARCH_TOKEN, Uri.decode(url.substring(secondColon + 1)));
193 // --------------------------------------------------------------------
195 public static final class LazyHtmlLoader {
196 final RandomAccessFile raf;
199 final int numZipBytes;
201 // Not sure this volatile is right, but oh well.
202 volatile SoftReference<String> htmlRef = new SoftReference<String>(null);
204 private LazyHtmlLoader(final RandomAccessFile raf) throws IOException {
206 numBytes = raf.readInt();
207 numZipBytes = raf.readInt();
208 offset = raf.getFilePointer();
209 raf.skipBytes(numZipBytes);
212 public String getHtml() {
213 String html = htmlRef.get();
217 System.out.println("Loading Html: numBytes=" + numBytes + ", numZipBytes=" + 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);