1 package com.hughes.android.dictionary.engine;
3 import android.content.Intent;
4 import android.net.Uri;
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.io.UnsupportedEncodingException;
17 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.escapeToPureHtmlUnicode(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;
176 return String.format("qd:%s:%s", indexShortName, URLEncoder.encode(text, "UTF-8"));
177 } catch (UnsupportedEncodingException e) {
178 throw new RuntimeException(e);
182 public static boolean isQuickdicUrl(String url) {
183 return url.startsWith("qd:");
186 public static void quickdicUrlToIntent(final String url, final Intent intent) {
187 int firstColon = url.indexOf(":");
188 if (firstColon == -1) return;
189 int secondColon = url.indexOf(":", firstColon + 1);
190 if (secondColon == -1) return;
191 intent.putExtra(C.SEARCH_TOKEN, Uri.decode(url.substring(secondColon + 1)));
194 // --------------------------------------------------------------------
196 public static final class LazyHtmlLoader {
197 final RandomAccessFile raf;
200 final int numZipBytes;
202 // Not sure this volatile is right, but oh well.
203 volatile SoftReference<String> htmlRef = new SoftReference<String>(null);
205 private LazyHtmlLoader(final RandomAccessFile raf) throws IOException {
207 numBytes = raf.readInt();
208 numZipBytes = raf.readInt();
209 offset = raf.getFilePointer();
210 raf.skipBytes(numZipBytes);
213 public String getHtml() {
214 String html = htmlRef.get();
218 System.out.println("Loading Html: numBytes=" + numBytes + ", numZipBytes=" + numZipBytes);
219 final byte[] bytes = new byte[numBytes];
220 final byte[] zipBytes = new byte[numZipBytes];
225 } catch (IOException e) {
226 throw new RuntimeException(e);
230 StringUtil.unzipFully(zipBytes, bytes);
231 html = new String(bytes, "UTF-8");
232 } catch (IOException e) {
233 throw new RuntimeException(e);
235 htmlRef = new SoftReference<String>(html);