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.UnsupportedEncodingException;
14 import java.lang.ref.SoftReference;
15 import java.nio.channels.FileChannel;
16 import java.util.List;
17 import java.util.regex.Pattern;
19 public class HtmlEntry extends AbstractEntry implements Comparable<HtmlEntry> {
21 // Title is not HTML escaped.
22 public final String title;
23 private final LazyHtmlLoader lazyHtmlLoader;
24 @SuppressWarnings("WeakerAccess")
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 private void writeBase(DataOutput raf) throws IOException {
46 private void writeData(DataOutput raf) throws IOException {
47 final byte[] bytes = getHtml().getBytes("UTF-8");
48 StringUtil.writeVarInt(raf, bytes.length);
52 private 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 private 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 Row(final DataInput raf, final int thisRowIndex,
157 final Index index, int extra) throws IOException {
158 super(raf, thisRowIndex, index, extra);
161 Row(final int referenceIndex, final int thisRowIndex,
163 super(referenceIndex, thisRowIndex, index);
167 public String toString() {
168 return getRawText(false);
171 public HtmlEntry getEntry() {
172 return index.dict.htmlEntries.get(referenceIndex);
176 public void print(PrintStream out) {
177 final HtmlEntry entry = getEntry();
178 out.println("See also HtmlEntry:" + entry.title);
182 public String getRawText(boolean compact) {
183 final HtmlEntry entry = getEntry();
184 return entry.getRawText(compact);
188 public RowMatchType matches(final List<String> searchTokens,
189 final Pattern orderedMatchPattern, final Transliterator normalizer,
190 final boolean swapPairEntries) {
191 final String text = normalizer.transform(getRawText(false));
192 if (orderedMatchPattern.matcher(text).find()) {
193 return RowMatchType.ORDERED_MATCH;
195 for (int i = searchTokens.size() - 1; i >= 0; --i) {
196 final String searchToken = searchTokens.get(i);
197 if (!text.contains(searchToken)) {
198 return RowMatchType.NO_MATCH;
201 return RowMatchType.BAG_OF_WORDS_MATCH;
205 public static String htmlBody(final List<HtmlEntry> htmlEntries, final String indexShortName) {
206 final StringBuilder result = new StringBuilder();
207 for (final HtmlEntry htmlEntry : htmlEntries) {
208 final String titleEscaped = StringUtil.escapeUnicodeToPureHtml(htmlEntry.title);
209 result.append(String.format("<h1><a href=\"%s\">%s</a></h1>\n<p>%s\n",
210 formatQuickdicUrl(indexShortName, htmlEntry.title), titleEscaped,
211 htmlEntry.getHtml()));
213 return result.toString();
216 @SuppressWarnings("WeakerAccess")
217 public static String formatQuickdicUrl(final String indexShortName, final String text) {
218 assert !indexShortName.contains(":");
219 assert text.length() > 0;
220 return String.format("q://d?%s&%s", indexShortName, StringUtil.encodeForUrl(text));
223 public static boolean isQuickdicUrl(String url) {
224 return url.startsWith("q://d?");
227 // --------------------------------------------------------------------
229 @SuppressWarnings("WeakerAccess")
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<>(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);
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<>(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<>(html);