2 package com.hughes.android.dictionary.engine;
4 import java.io.DataInput;
5 import java.io.DataOutput;
6 import java.io.IOException;
7 import java.io.PrintStream;
8 import java.lang.ref.SoftReference;
9 import java.nio.charset.StandardCharsets;
10 import java.util.List;
11 import java.util.regex.Pattern;
13 import com.hughes.util.DataInputBuffer;
14 import com.hughes.util.StringUtil;
15 import com.hughes.util.raf.RAFListSerializer;
16 import com.hughes.util.raf.RAFListSerializerSkippable;
17 import com.ibm.icu.text.Transliterator;
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, DataInput raf, final int index)
35 super(dictionary, raf, index);
36 title = raf.readUTF();
37 lazyHtmlLoader = new LazyHtmlLoader(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(StandardCharsets.UTF_8);
48 StringUtil.writeVarInt(raf, bytes.length);
52 private static DataInputBuffer readData(DataInput raf) throws IOException {
53 int len = StringUtil.readVarInt(raf);
54 return ((DataInputBuffer)raf).slice(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 RAFListSerializerSkippable<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 skip(DataInput raf, final int index) throws IOException {
88 if (dictionary.dictFileVersion >= 7)
90 StringUtil.readVarInt(raf);
96 int l = raf.readUnsignedShort();
101 public void write(DataOutput raf, HtmlEntry t) throws IOException {
106 static final class DataSerializer implements RAFListSerializer<HtmlEntry> {
108 public HtmlEntry read(DataInput raf, final int index) {
114 public void write(DataOutput raf, HtmlEntry t) throws IOException {
119 static final class DataDeserializer implements RAFListSerializer<DataInputBuffer> {
121 public DataInputBuffer read(DataInput raf, final int index) throws IOException {
122 return readData(raf);
126 public void write(DataOutput raf, DataInputBuffer t) {
131 private String getRawText(final boolean compact) {
132 return title + ":\n" + getHtml();
136 public int compareTo(/*@NonNull*/ HtmlEntry another) {
137 if (title.compareTo(another.title) != 0) {
138 return title.compareTo(another.title);
140 return getHtml().compareTo(another.getHtml());
144 public String toString() {
145 return getRawText(false);
148 // --------------------------------------------------------------------
150 public static class Row extends RowBase {
152 Row(final DataInput raf, final int thisRowIndex,
153 final Index index, int extra) throws IOException {
154 super(raf, thisRowIndex, index, extra);
157 Row(final int referenceIndex, final int thisRowIndex,
159 super(referenceIndex, thisRowIndex, index);
163 public String toString() {
164 return getRawText(false);
167 public HtmlEntry getEntry() {
168 return index.dict.htmlEntries.get(referenceIndex);
172 public void print(PrintStream out) {
173 final HtmlEntry entry = getEntry();
174 out.println("See also HtmlEntry:" + entry.title);
178 public String getRawText(boolean compact) {
179 final HtmlEntry entry = getEntry();
180 return entry.getRawText(compact);
184 public RowMatchType matches(final List<String> searchTokens,
185 final Pattern orderedMatchPattern, final Transliterator normalizer,
186 final boolean swapPairEntries) {
187 final String text = normalizer.transform(getRawText(false));
188 if (orderedMatchPattern.matcher(text).find()) {
189 return RowMatchType.ORDERED_MATCH;
191 for (int i = searchTokens.size() - 1; i >= 0; --i) {
192 final String searchToken = searchTokens.get(i);
193 if (!text.contains(searchToken)) {
194 return RowMatchType.NO_MATCH;
197 return RowMatchType.BAG_OF_WORDS_MATCH;
201 public static String htmlBody(final List<HtmlEntry> htmlEntries, final String indexShortName) {
202 final StringBuilder result = new StringBuilder();
203 for (final HtmlEntry htmlEntry : htmlEntries) {
204 final String titleEscaped = StringUtil.escapeUnicodeToPureHtml(htmlEntry.title);
205 result.append(String.format("<h1><a href=\"%s\">%s</a></h1>\n<p>%s\n",
206 formatQuickdicUrl(indexShortName, htmlEntry.title), titleEscaped,
207 htmlEntry.getHtml()));
209 return result.toString();
212 @SuppressWarnings("WeakerAccess")
213 public static String formatQuickdicUrl(final String indexShortName, final String text) {
214 assert !indexShortName.contains(":");
215 assert text.length() > 0;
216 StringBuilder s = new StringBuilder("q://d?");
217 s.append(indexShortName);
219 s.append(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 {
231 final DataInputBuffer buf;
233 final List<DataInputBuffer> data;
236 // Not sure this volatile is right, but oh well.
237 volatile SoftReference<String> htmlRef = new SoftReference<>(null);
239 private LazyHtmlLoader(final DataInput inp, List<DataInputBuffer> data, int index) throws IOException {
247 numBytes = Math.min(inp.readInt(), 20 * 1024 * 1024);
248 int numZipBytes = Math.min(inp.readInt(), 20 * 1024 * 1024);
249 DataInputBuffer b = (DataInputBuffer)inp;
250 buf = b.slice(numZipBytes);
254 String html = htmlRef.get();
259 html = data.get(index).asString();
260 htmlRef = new SoftReference<>(html);
263 System.out.println("Loading Html: numBytes=" + numBytes + ", numZipBytes="
265 final byte[] zipBytes = new byte[buf.limit()];
267 buf.readFully(zipBytes);
269 final byte[] bytes = StringUtil.unzipFully(zipBytes, numBytes);
270 html = new String(bytes, StandardCharsets.UTF_8);
271 } catch (IOException e) {
272 throw new RuntimeException("Dictionary HTML data corrupted", e);
274 htmlRef = new SoftReference<>(html);