2 package com.hughes.android.dictionary.engine;
4 import com.hughes.util.StringUtil;
5 import com.hughes.util.raf.RAFListSerializer;
6 import com.hughes.util.raf.RAFListSerializerSkippable;
7 import com.ibm.icu.text.Transliterator;
9 import java.io.DataInput;
10 import java.io.DataOutput;
11 import java.io.IOException;
12 import java.io.PrintStream;
13 import java.io.RandomAccessFile;
14 import java.io.UnsupportedEncodingException;
15 import java.lang.ref.SoftReference;
16 import java.nio.channels.FileChannel;
17 import java.util.List;
18 import java.util.regex.Pattern;
20 public class HtmlEntry extends AbstractEntry implements Comparable<HtmlEntry> {
22 // Title is not HTML escaped.
23 public final String title;
24 public final LazyHtmlLoader lazyHtmlLoader;
27 public HtmlEntry(final EntrySource entrySource, String title) {
30 lazyHtmlLoader = null;
33 public HtmlEntry(Dictionary dictionary, FileChannel ch, DataInput raf, final int index)
35 super(dictionary, raf, index);
36 title = raf.readUTF();
37 lazyHtmlLoader = new LazyHtmlLoader(ch, raf, dictionary.htmlData, index);
41 public void writeBase(DataOutput raf) throws IOException {
46 public void writeData(DataOutput raf) throws IOException {
47 final byte[] bytes = getHtml().getBytes("UTF-8");
48 StringUtil.writeVarInt(raf, bytes.length);
52 public static byte[] readData(DataInput raf) throws IOException {
53 int len = StringUtil.readVarInt(raf);
54 final byte[] bytes = new byte[Math.min(len, 20 * 1024 * 1024)];
60 return html != null ? html : lazyHtmlLoader.getHtml();
64 public void addToDictionary(Dictionary dictionary) {
66 dictionary.htmlEntries.add(this);
67 index = dictionary.htmlEntries.size() - 1;
71 public RowBase CreateRow(int rowIndex, Index dictionaryIndex) {
72 return new Row(this.index, rowIndex, dictionaryIndex);
75 static final class Serializer implements RAFListSerializerSkippable<HtmlEntry> {
77 final Dictionary dictionary;
80 Serializer(Dictionary dictionary, FileChannel ch) {
81 this.dictionary = dictionary;
86 public HtmlEntry read(DataInput raf, final int index) throws IOException {
87 return new HtmlEntry(dictionary, ch, raf, index);
91 public void skip(DataInput raf, final int index) throws IOException {
92 if (dictionary.dictFileVersion >= 7)
94 StringUtil.readVarInt(raf);
100 int l = raf.readUnsignedShort();
105 public void write(DataOutput raf, HtmlEntry t) throws IOException {
110 static final class DataSerializer implements RAFListSerializer<HtmlEntry> {
112 public HtmlEntry read(DataInput raf, final int index) {
118 public void write(DataOutput raf, HtmlEntry t) throws IOException {
123 static final class DataDeserializer implements RAFListSerializer<byte[]> {
125 public byte[] read(DataInput raf, final int index) throws IOException {
126 return HtmlEntry.readData(raf);
130 public void write(DataOutput raf, byte[] t) {
135 public String getRawText(final boolean compact) {
136 return title + ":\n" + getHtml();
140 public int compareTo(HtmlEntry another) {
141 if (title.compareTo(another.title) != 0) {
142 return title.compareTo(another.title);
144 return getHtml().compareTo(another.getHtml());
148 public String toString() {
149 return getRawText(false);
152 // --------------------------------------------------------------------
154 public static class Row extends RowBase {
156 boolean isExpanded = false;
158 Row(final DataInput raf, final int thisRowIndex,
159 final Index index, int extra) throws IOException {
160 super(raf, thisRowIndex, index, extra);
163 Row(final int referenceIndex, final int thisRowIndex,
165 super(referenceIndex, thisRowIndex, index);
169 public String toString() {
170 return getRawText(false);
173 public HtmlEntry getEntry() {
174 return index.dict.htmlEntries.get(referenceIndex);
178 public void print(PrintStream out) {
179 final HtmlEntry entry = getEntry();
180 out.println("See also HtmlEntry:" + entry.title);
184 public String getRawText(boolean compact) {
185 final HtmlEntry entry = getEntry();
186 return entry.getRawText(compact);
190 public RowMatchType matches(final List<String> searchTokens,
191 final Pattern orderedMatchPattern, final Transliterator normalizer,
192 final boolean swapPairEntries) {
193 final String text = normalizer.transform(getRawText(false));
194 if (orderedMatchPattern.matcher(text).find()) {
195 return RowMatchType.ORDERED_MATCH;
197 for (int i = searchTokens.size() - 1; i >= 0; --i) {
198 final String searchToken = searchTokens.get(i);
199 if (!text.contains(searchToken)) {
200 return RowMatchType.NO_MATCH;
203 return RowMatchType.BAG_OF_WORDS_MATCH;
207 public static String htmlBody(final List<HtmlEntry> htmlEntries, final String indexShortName) {
208 final StringBuilder result = new StringBuilder();
209 for (final HtmlEntry htmlEntry : htmlEntries) {
210 final String titleEscaped = StringUtil.escapeUnicodeToPureHtml(htmlEntry.title);
211 result.append(String.format("<h1><a href=\"%s\">%s</a></h1>\n<p>%s\n",
212 formatQuickdicUrl(indexShortName, htmlEntry.title), titleEscaped,
213 htmlEntry.getHtml()));
215 return result.toString();
218 public static String formatQuickdicUrl(final String indexShortName, final String text) {
219 assert !indexShortName.contains(":");
220 assert text.length() > 0;
221 return String.format("q://d?%s&%s", indexShortName, StringUtil.encodeForUrl(text));
224 public static boolean isQuickdicUrl(String url) {
225 return url.startsWith("q://d?");
228 // --------------------------------------------------------------------
230 public static final class LazyHtmlLoader {
232 final FileChannel ch;
235 final int numZipBytes;
236 final List<byte[]> data;
239 // Not sure this volatile is right, but oh well.
240 volatile SoftReference<String> htmlRef = new SoftReference<String>(null);
242 private LazyHtmlLoader(FileChannel ch, final DataInput inp, List<byte[]> data, int index) throws IOException {
250 this.numZipBytes = -1;
255 numBytes = Math.min(raf.readInt(), 20 * 1024 * 1024);
256 numZipBytes = Math.min(raf.readInt(), 20 * 1024 * 1024);
257 offset = ch.position();
258 raf.skipBytes(numZipBytes);
261 public String getHtml() {
262 String html = htmlRef.get();
268 html = new String(data.get(index), "UTF-8");
269 } catch (UnsupportedEncodingException e) {
270 throw new RuntimeException("Dictionary HTML data corrupted", e);
272 htmlRef = new SoftReference<String>(html);
275 System.out.println("Loading Html: numBytes=" + numBytes + ", numZipBytes="
277 final byte[] zipBytes = new byte[numZipBytes];
281 raf.readFully(zipBytes);
282 } catch (IOException e) {
283 throw new RuntimeException("Failed to read HTML data from dictionary", e);
287 final byte[] bytes = StringUtil.unzipFully(zipBytes, numBytes);
288 html = new String(bytes, "UTF-8");
289 } catch (IOException e) {
290 throw new RuntimeException("Dictionary HTML data corrupted", e);
292 htmlRef = new SoftReference<String>(html);