2 package com.hughes.android.dictionary.engine;
4 import com.hughes.util.StringUtil;
5 import com.hughes.util.raf.RAFListSerializer;
6 import com.ibm.icu.text.Transliterator;
8 import java.io.DataInput;
9 import java.io.DataOutput;
10 import java.io.IOException;
11 import java.io.PrintStream;
12 import java.io.RandomAccessFile;
13 import java.io.UnsupportedEncodingException;
14 import java.lang.ref.SoftReference;
15 import java.util.List;
16 import java.util.regex.Pattern;
18 public class HtmlEntry extends AbstractEntry implements 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, DataInput raf, final int index)
33 super(dictionary, raf, index);
34 title = raf.readUTF();
35 lazyHtmlLoader = new LazyHtmlLoader(raf, dictionary.htmlData, index);
39 public void writeBase(DataOutput raf) throws IOException {
44 public void writeData(DataOutput raf) throws IOException {
45 final byte[] bytes = getHtml().getBytes("UTF-8");
46 StringUtil.writeVarInt(raf, bytes.length);
50 public static byte[] readData(DataInput raf) throws IOException {
51 int len = StringUtil.readVarInt(raf);
52 final byte[] bytes = new byte[len];
58 return html != null ? html : lazyHtmlLoader.getHtml();
62 public void addToDictionary(Dictionary dictionary) {
64 dictionary.htmlEntries.add(this);
65 index = dictionary.htmlEntries.size() - 1;
69 public RowBase CreateRow(int rowIndex, Index dictionaryIndex) {
70 return new Row(this.index, rowIndex, dictionaryIndex);
73 static final class Serializer implements RAFListSerializer<HtmlEntry> {
75 final Dictionary dictionary;
77 Serializer(Dictionary dictionary) {
78 this.dictionary = dictionary;
82 public HtmlEntry read(DataInput raf, final int index) throws IOException {
83 return new HtmlEntry(dictionary, raf, index);
87 public void write(DataOutput raf, HtmlEntry t) throws IOException {
92 static final class DataSerializer implements RAFListSerializer<HtmlEntry> {
94 public HtmlEntry read(DataInput raf, final int index) throws IOException {
100 public void write(DataOutput raf, HtmlEntry t) throws IOException {
105 static final class DataDeserializer implements RAFListSerializer<byte[]> {
107 public byte[] read(DataInput raf, final int index) throws IOException {
108 return HtmlEntry.readData(raf);
112 public void write(DataOutput raf, byte[] t) throws IOException {
117 public String getRawText(final boolean compact) {
118 return title + ":\n" + getHtml();
122 public int compareTo(HtmlEntry another) {
123 if (title.compareTo(another.title) != 0) {
124 return title.compareTo(another.title);
126 return getHtml().compareTo(another.getHtml());
130 public String toString() {
131 return getRawText(false);
134 // --------------------------------------------------------------------
136 public static class Row extends RowBase {
138 boolean isExpanded = false;
140 Row(final DataInput raf, final int thisRowIndex,
141 final Index index, int extra) throws IOException {
142 super(raf, thisRowIndex, index, extra);
145 Row(final int referenceIndex, final int thisRowIndex,
147 super(referenceIndex, thisRowIndex, index);
151 public String toString() {
152 return getRawText(false);
155 public HtmlEntry getEntry() {
156 return index.dict.htmlEntries.get(referenceIndex);
160 public void print(PrintStream out) {
161 final HtmlEntry entry = getEntry();
162 out.println("See also HtmlEntry:" + entry.title);
166 public String getRawText(boolean compact) {
167 final HtmlEntry entry = getEntry();
168 return entry.getRawText(compact);
172 public RowMatchType matches(final List<String> searchTokens,
173 final Pattern orderedMatchPattern, final Transliterator normalizer,
174 final boolean swapPairEntries) {
175 final String text = normalizer.transform(getRawText(false));
176 if (orderedMatchPattern.matcher(text).find()) {
177 return RowMatchType.ORDERED_MATCH;
179 for (int i = searchTokens.size() - 1; i >= 0; --i) {
180 final String searchToken = searchTokens.get(i);
181 if (!text.contains(searchToken)) {
182 return RowMatchType.NO_MATCH;
185 return RowMatchType.BAG_OF_WORDS_MATCH;
189 public static String htmlBody(final List<HtmlEntry> htmlEntries, final String indexShortName) {
190 final StringBuilder result = new StringBuilder();
191 for (final HtmlEntry htmlEntry : htmlEntries) {
192 final String titleEscaped = StringUtil.escapeUnicodeToPureHtml(htmlEntry.title);
193 result.append(String.format("<h1><a href=\"%s\">%s</a></h1>\n<p>%s\n",
194 formatQuickdicUrl(indexShortName, htmlEntry.title), titleEscaped,
195 htmlEntry.getHtml()));
197 return result.toString();
200 public static String formatQuickdicUrl(final String indexShortName, final String text) {
201 assert !indexShortName.contains(":");
202 assert text.length() > 0;
203 return String.format("q://d?%s&%s", indexShortName, StringUtil.encodeForUrl(text));
206 public static boolean isQuickdicUrl(String url) {
207 return url.startsWith("q://d?");
210 // --------------------------------------------------------------------
212 public static final class LazyHtmlLoader {
213 final RandomAccessFile raf;
216 final int numZipBytes;
217 final List<byte[]> data;
220 // Not sure this volatile is right, but oh well.
221 volatile SoftReference<String> htmlRef = new SoftReference<String>(null);
223 private LazyHtmlLoader(final DataInput inp, List<byte[]> data, int index) throws IOException {
230 this.numZipBytes = -1;
233 raf = (RandomAccessFile)inp;
234 numBytes = raf.readInt();
235 numZipBytes = raf.readInt();
236 offset = raf.getFilePointer();
237 raf.skipBytes(numZipBytes);
240 public String getHtml() {
241 String html = htmlRef.get();
247 html = new String(data.get(index), "UTF-8");
248 } catch (UnsupportedEncodingException e) {
249 throw new RuntimeException("Dictionary HTML data corrupted", e);
251 htmlRef = new SoftReference<String>(html);
254 System.out.println("Loading Html: numBytes=" + numBytes + ", numZipBytes="
256 final byte[] zipBytes = new byte[numZipBytes];
261 } catch (IOException e) {
262 throw new RuntimeException("Failed to read HTML data from dictionary", e);
266 final byte[] bytes = StringUtil.unzipFully(zipBytes, numBytes);
267 html = new String(bytes, "UTF-8");
268 } catch (IOException e) {
269 throw new RuntimeException("Dictionary HTML data corrupted", e);
271 htmlRef = new SoftReference<String>(html);