1 package com.hughes.android.dictionary.engine;
3 import android.content.Intent;
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;
11 import java.io.IOException;
12 import java.io.PrintStream;
13 import java.io.RandomAccessFile;
14 import java.lang.ref.SoftReference;
15 import java.util.List;
16 import java.util.regex.Pattern;
18 public class HtmlEntry extends AbstractEntry implements RAFSerializable<HtmlEntry>, Comparable<HtmlEntry> {
20 // Title is not HTML escaped.
21 public final String title;
22 public final LazyHtmlLoader lazyHtmlLoader;
25 public HtmlEntry(final EntrySource entrySource, String title) {
28 lazyHtmlLoader = null;
31 public HtmlEntry(Dictionary dictionary, RandomAccessFile raf, final int index) throws IOException {
32 super(dictionary, raf, index);
33 title = raf.readUTF();
34 lazyHtmlLoader = new LazyHtmlLoader(raf);
39 public void write(RandomAccessFile raf) throws IOException {
43 final byte[] bytes = getHtml().getBytes("UTF-8");
44 final byte[] zipBytes = StringUtil.zipBytes(bytes);
45 raf.writeInt(bytes.length);
46 raf.writeInt(zipBytes.length);
51 return html != null ? html : lazyHtmlLoader.getHtml();
55 public void addToDictionary(Dictionary dictionary) {
57 dictionary.htmlEntries.add(this);
58 index = dictionary.htmlEntries.size() - 1;
62 public RowBase CreateRow(int rowIndex, Index dictionaryIndex) {
63 return new Row(this.index, rowIndex, dictionaryIndex);
66 static final class Serializer implements RAFListSerializer<HtmlEntry> {
68 final Dictionary dictionary;
70 Serializer(Dictionary dictionary) {
71 this.dictionary = dictionary;
75 public HtmlEntry read(RandomAccessFile raf, final int index) throws IOException {
76 return new HtmlEntry(dictionary, raf, index);
80 public void write(RandomAccessFile raf, HtmlEntry t) throws IOException {
85 public String getRawText(final boolean compact) {
86 return title + ":\n" + getHtml();
91 public int compareTo(HtmlEntry another) {
92 if (title.compareTo(another.title) != 0) {
93 return title.compareTo(another.title);
95 return getHtml().compareTo(another.getHtml());
99 public String toString() {
100 return getRawText(false);
103 // --------------------------------------------------------------------
106 public static class Row extends RowBase {
108 boolean isExpanded = false;
110 Row(final RandomAccessFile raf, final int thisRowIndex,
111 final Index index) throws IOException {
112 super(raf, thisRowIndex, index);
115 Row(final int referenceIndex, final int thisRowIndex,
117 super(referenceIndex, thisRowIndex, index);
121 public String toString() {
122 return getRawText(false);
125 public HtmlEntry getEntry() {
126 return index.dict.htmlEntries.get(referenceIndex);
130 public void print(PrintStream out) {
131 final HtmlEntry entry = getEntry();
132 out.println("See also HtmlEntry:" + entry.title);
136 public String getRawText(boolean compact) {
137 final HtmlEntry entry = getEntry();
138 return entry.getRawText(compact);
142 public RowMatchType matches(final List<String> searchTokens, final Pattern orderedMatchPattern, final Transliterator normalizer, final boolean swapPairEntries) {
143 final String text = normalizer.transform(getRawText(false));
144 if (orderedMatchPattern.matcher(text).find()) {
145 return RowMatchType.ORDERED_MATCH;
147 for (int i = searchTokens.size() - 1; i >= 0; --i) {
148 final String searchToken = searchTokens.get(i);
149 if (!text.contains(searchToken)) {
150 return RowMatchType.NO_MATCH;
153 return RowMatchType.BAG_OF_WORDS_MATCH;
157 public static String htmlBody(final List<HtmlEntry> htmlEntries, final String indexShortName) {
158 final StringBuilder result = new StringBuilder();
159 for (final HtmlEntry htmlEntry : htmlEntries) {
160 final String titleEscaped = StringUtil.escapeUnicodeToPureHtml(htmlEntry.title);
161 result.append(String.format("<h1><a href=\"%s\">%s</a></h1>\n<p>%s\n",
162 formatQuickdicUrl(indexShortName, titleEscaped), titleEscaped,
163 htmlEntry.getHtml()));
165 return result.toString();
168 public static String formatQuickdicUrl(final String indexShortName, final String text) {
169 assert !indexShortName.contains(":");
170 assert text.length() > 0;
171 return String.format("q://d?%s&%s", indexShortName, StringUtil.encodeForUrl(text));
174 public static boolean isQuickdicUrl(String url) {
175 return url.startsWith("q://d?");
178 public static void quickdicUrlToIntent(final String url, final Intent intent) {
179 int firstColon = url.indexOf("?");
180 if (firstColon == -1) return;
181 int secondColon = url.indexOf("&", firstColon + 1);
182 if (secondColon == -1) return;
183 intent.putExtra(C.SEARCH_TOKEN, StringUtil.decodeFromUrl(url.substring(secondColon + 1)));
186 // --------------------------------------------------------------------
188 public static final class LazyHtmlLoader {
189 final RandomAccessFile raf;
192 final int numZipBytes;
194 // Not sure this volatile is right, but oh well.
195 volatile SoftReference<String> htmlRef = new SoftReference<String>(null);
197 private LazyHtmlLoader(final RandomAccessFile raf) throws IOException {
199 numBytes = raf.readInt();
200 numZipBytes = raf.readInt();
201 offset = raf.getFilePointer();
202 raf.skipBytes(numZipBytes);
205 public String getHtml() {
206 String html = htmlRef.get();
210 System.out.println("Loading Html: numBytes=" + numBytes + ", numZipBytes=" + numZipBytes);
211 final byte[] bytes = new byte[numBytes];
212 final byte[] zipBytes = new byte[numZipBytes];
217 } catch (IOException e) {
218 throw new RuntimeException(e);
222 StringUtil.unzipFully(zipBytes, bytes);
223 html = new String(bytes, "UTF-8");
224 } catch (IOException e) {
225 throw new RuntimeException(e);
227 htmlRef = new SoftReference<String>(html);