2 package com.hughes.android.dictionary.engine;
4 import com.hughes.android.dictionary.C;
5 import com.hughes.util.StringUtil;
6 import com.hughes.util.raf.RAFListSerializer;
7 import com.hughes.util.raf.RAFSerializable;
8 import com.ibm.icu.text.Transliterator;
10 import java.io.IOException;
11 import java.io.PrintStream;
12 import java.io.RandomAccessFile;
13 import java.lang.ref.SoftReference;
14 import java.util.List;
15 import java.util.regex.Pattern;
17 public class HtmlEntry extends AbstractEntry implements RAFSerializable<HtmlEntry>,
18 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)
33 super(dictionary, raf, index);
34 title = raf.readUTF();
35 lazyHtmlLoader = new LazyHtmlLoader(raf);
40 public void write(RandomAccessFile raf) throws IOException {
44 final byte[] bytes = getHtml().getBytes("UTF-8");
45 final byte[] zipBytes = StringUtil.zipBytes(bytes);
46 raf.writeInt(bytes.length);
47 raf.writeInt(zipBytes.length);
52 return html != null ? html : lazyHtmlLoader.getHtml();
56 public void addToDictionary(Dictionary dictionary) {
58 dictionary.htmlEntries.add(this);
59 index = dictionary.htmlEntries.size() - 1;
63 public RowBase CreateRow(int rowIndex, Index dictionaryIndex) {
64 return new Row(this.index, rowIndex, dictionaryIndex);
67 static final class Serializer implements RAFListSerializer<HtmlEntry> {
69 final Dictionary dictionary;
71 Serializer(Dictionary dictionary) {
72 this.dictionary = dictionary;
76 public HtmlEntry read(RandomAccessFile raf, final int index) throws IOException {
77 return new HtmlEntry(dictionary, raf, index);
81 public void write(RandomAccessFile raf, HtmlEntry t) throws IOException {
86 public String getRawText(final boolean compact) {
87 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 // --------------------------------------------------------------------
105 public static class Row extends RowBase {
107 boolean isExpanded = false;
109 Row(final RandomAccessFile raf, final int thisRowIndex,
110 final Index index) throws IOException {
111 super(raf, thisRowIndex, index);
114 Row(final int referenceIndex, final int thisRowIndex,
116 super(referenceIndex, thisRowIndex, index);
120 public String toString() {
121 return getRawText(false);
124 public HtmlEntry getEntry() {
125 return index.dict.htmlEntries.get(referenceIndex);
129 public void print(PrintStream out) {
130 final HtmlEntry entry = getEntry();
131 out.println("See also HtmlEntry:" + entry.title);
135 public String getRawText(boolean compact) {
136 final HtmlEntry entry = getEntry();
137 return entry.getRawText(compact);
141 public RowMatchType matches(final List<String> searchTokens,
142 final Pattern orderedMatchPattern, final Transliterator normalizer,
143 final boolean swapPairEntries) {
144 final String text = normalizer.transform(getRawText(false));
145 if (orderedMatchPattern.matcher(text).find()) {
146 return RowMatchType.ORDERED_MATCH;
148 for (int i = searchTokens.size() - 1; i >= 0; --i) {
149 final String searchToken = searchTokens.get(i);
150 if (!text.contains(searchToken)) {
151 return RowMatchType.NO_MATCH;
154 return RowMatchType.BAG_OF_WORDS_MATCH;
158 public static String htmlBody(final List<HtmlEntry> htmlEntries, final String indexShortName) {
159 final StringBuilder result = new StringBuilder();
160 for (final HtmlEntry htmlEntry : htmlEntries) {
161 final String titleEscaped = StringUtil.escapeUnicodeToPureHtml(htmlEntry.title);
162 result.append(String.format("<h1><a href=\"%s\">%s</a></h1>\n<p>%s\n",
163 formatQuickdicUrl(indexShortName, htmlEntry.title), titleEscaped,
164 htmlEntry.getHtml()));
166 return result.toString();
169 public static String formatQuickdicUrl(final String indexShortName, final String text) {
170 assert !indexShortName.contains(":");
171 assert text.length() > 0;
172 return String.format("q://d?%s&%s", indexShortName, StringUtil.encodeForUrl(text));
175 public static boolean isQuickdicUrl(String url) {
176 return url.startsWith("q://d?");
179 // --------------------------------------------------------------------
181 public static final class LazyHtmlLoader {
182 final RandomAccessFile raf;
185 final int numZipBytes;
187 // Not sure this volatile is right, but oh well.
188 volatile SoftReference<String> htmlRef = new SoftReference<String>(null);
190 private LazyHtmlLoader(final RandomAccessFile raf) throws IOException {
192 numBytes = raf.readInt();
193 numZipBytes = raf.readInt();
194 offset = raf.getFilePointer();
195 raf.skipBytes(numZipBytes);
198 public String getHtml() {
199 String html = htmlRef.get();
203 System.out.println("Loading Html: numBytes=" + numBytes + ", numZipBytes="
205 final byte[] bytes = new byte[numBytes];
206 final byte[] zipBytes = new byte[numZipBytes];
211 } catch (IOException e) {
212 throw new RuntimeException(e);
216 StringUtil.unzipFully(zipBytes, bytes);
217 html = new String(bytes, "UTF-8");
218 } catch (IOException e) {
219 throw new RuntimeException(e);
221 htmlRef = new SoftReference<String>(html);