]> gitweb.fperrin.net Git - Dictionary.git/blob - src/com/hughes/android/dictionary/engine/HtmlEntry.java
Some lint fixes.
[Dictionary.git] / src / com / hughes / android / dictionary / engine / HtmlEntry.java
1
2 package com.hughes.android.dictionary.engine;
3
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;
8
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.nio.charset.StandardCharsets;
17 import java.util.List;
18 import java.util.regex.Pattern;
19
20 public class HtmlEntry extends AbstractEntry implements Comparable<HtmlEntry> {
21
22     // Title is not HTML escaped.
23     public final String title;
24     private final LazyHtmlLoader lazyHtmlLoader;
25     @SuppressWarnings("WeakerAccess")
26     public String html;
27
28     public HtmlEntry(final EntrySource entrySource, String title) {
29         super(entrySource);
30         this.title = title;
31         lazyHtmlLoader = null;
32     }
33
34     public HtmlEntry(Dictionary dictionary, FileChannel ch, DataInput raf, final int index)
35     throws IOException {
36         super(dictionary, raf, index);
37         title = raf.readUTF();
38         lazyHtmlLoader = new LazyHtmlLoader(ch, raf, dictionary.htmlData, index);
39         html = null;
40     }
41
42     private void writeBase(DataOutput raf) throws IOException {
43         super.write(raf);
44         raf.writeUTF(title);
45     }
46
47     private void writeData(DataOutput raf) throws IOException {
48         final byte[] bytes = getHtml().getBytes(StandardCharsets.UTF_8);
49         StringUtil.writeVarInt(raf, bytes.length);
50         raf.write(bytes);
51     }
52
53     private static byte[] readData(DataInput raf) throws IOException {
54         int len = StringUtil.readVarInt(raf);
55         final byte[] bytes = new byte[Math.min(len, 20 * 1024 * 1024)];
56         raf.readFully(bytes);
57         return bytes;
58     }
59
60     String getHtml() {
61         return html != null ? html : lazyHtmlLoader.getHtml();
62     }
63
64     @Override
65     public void addToDictionary(Dictionary dictionary) {
66         assert index == -1;
67         dictionary.htmlEntries.add(this);
68         index = dictionary.htmlEntries.size() - 1;
69     }
70
71     @Override
72     public RowBase CreateRow(int rowIndex, Index dictionaryIndex) {
73         return new Row(this.index, rowIndex, dictionaryIndex);
74     }
75
76     static final class Serializer implements RAFListSerializerSkippable<HtmlEntry> {
77
78         final Dictionary dictionary;
79         final FileChannel ch;
80
81         Serializer(Dictionary dictionary, FileChannel ch) {
82             this.dictionary = dictionary;
83             this.ch = ch;
84         }
85
86         @Override
87         public HtmlEntry read(DataInput raf, final int index) throws IOException {
88             return new HtmlEntry(dictionary, ch, raf, index);
89         }
90
91         @Override
92         public void skip(DataInput raf, final int index) throws IOException {
93             if (dictionary.dictFileVersion >= 7)
94             {
95                 StringUtil.readVarInt(raf);
96             }
97             else
98             {
99                 raf.skipBytes(2);
100             }
101             int l = raf.readUnsignedShort();
102             raf.skipBytes(l);
103         }
104
105         @Override
106         public void write(DataOutput raf, HtmlEntry t) throws IOException {
107             t.writeBase(raf);
108         }
109     }
110
111     static final class DataSerializer implements RAFListSerializer<HtmlEntry> {
112         @Override
113         public HtmlEntry read(DataInput raf, final int index) {
114             assert false;
115             return null;
116         }
117
118         @Override
119         public void write(DataOutput raf, HtmlEntry t) throws IOException {
120             t.writeData(raf);
121         }
122     }
123
124     static final class DataDeserializer implements RAFListSerializer<byte[]> {
125         @Override
126         public byte[] read(DataInput raf, final int index) throws IOException {
127             return HtmlEntry.readData(raf);
128         }
129
130         @Override
131         public void write(DataOutput raf, byte[] t) {
132             assert false;
133         }
134     }
135
136     private String getRawText(final boolean compact) {
137         return title + ":\n" + getHtml();
138     }
139
140     @Override
141     public int compareTo(/*@NonNull*/ HtmlEntry another) {
142         if (title.compareTo(another.title) != 0) {
143             return title.compareTo(another.title);
144         }
145         return getHtml().compareTo(another.getHtml());
146     }
147
148     @Override
149     public String toString() {
150         return getRawText(false);
151     }
152
153     // --------------------------------------------------------------------
154
155     public static class Row extends RowBase {
156
157         Row(final DataInput raf, final int thisRowIndex,
158             final Index index, int extra) throws IOException {
159             super(raf, thisRowIndex, index, extra);
160         }
161
162         Row(final int referenceIndex, final int thisRowIndex,
163             final Index index) {
164             super(referenceIndex, thisRowIndex, index);
165         }
166
167         @Override
168         public String toString() {
169             return getRawText(false);
170         }
171
172         public HtmlEntry getEntry() {
173             return index.dict.htmlEntries.get(referenceIndex);
174         }
175
176         @Override
177         public void print(PrintStream out) {
178             final HtmlEntry entry = getEntry();
179             out.println("See also HtmlEntry:" + entry.title);
180         }
181
182         @Override
183         public String getRawText(boolean compact) {
184             final HtmlEntry entry = getEntry();
185             return entry.getRawText(compact);
186         }
187
188         @Override
189         public RowMatchType matches(final List<String> searchTokens,
190                                     final Pattern orderedMatchPattern, final Transliterator normalizer,
191                                     final boolean swapPairEntries) {
192             final String text = normalizer.transform(getRawText(false));
193             if (orderedMatchPattern.matcher(text).find()) {
194                 return RowMatchType.ORDERED_MATCH;
195             }
196             for (int i = searchTokens.size() - 1; i >= 0; --i) {
197                 final String searchToken = searchTokens.get(i);
198                 if (!text.contains(searchToken)) {
199                     return RowMatchType.NO_MATCH;
200                 }
201             }
202             return RowMatchType.BAG_OF_WORDS_MATCH;
203         }
204     }
205
206     public static String htmlBody(final List<HtmlEntry> htmlEntries, final String indexShortName) {
207         final StringBuilder result = new StringBuilder();
208         for (final HtmlEntry htmlEntry : htmlEntries) {
209             final String titleEscaped = StringUtil.escapeUnicodeToPureHtml(htmlEntry.title);
210             result.append(String.format("<h1><a href=\"%s\">%s</a></h1>\n<p>%s\n",
211                                         formatQuickdicUrl(indexShortName, htmlEntry.title), titleEscaped,
212                                         htmlEntry.getHtml()));
213         }
214         return result.toString();
215     }
216
217     @SuppressWarnings("WeakerAccess")
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));
222     }
223
224     public static boolean isQuickdicUrl(String url) {
225         return url.startsWith("q://d?");
226     }
227
228     // --------------------------------------------------------------------
229
230     @SuppressWarnings("WeakerAccess")
231     public static final class LazyHtmlLoader {
232         final DataInput raf;
233         final FileChannel ch;
234         final long offset;
235         final int numBytes;
236         final int numZipBytes;
237         final List<byte[]> data;
238         final int index;
239
240         // Not sure this volatile is right, but oh well.
241         volatile SoftReference<String> htmlRef = new SoftReference<>(null);
242
243         private LazyHtmlLoader(FileChannel ch, final DataInput inp, List<byte[]> data, int index) throws IOException {
244             this.data = data;
245             this.index = index;
246             if (data != null) {
247                 this.raf = null;
248                 this.ch = null;
249                 this.offset = 0;
250                 this.numBytes = -1;
251                 this.numZipBytes = -1;
252                 return;
253             }
254             raf = inp;
255             this.ch = ch;
256             numBytes = Math.min(raf.readInt(), 20 * 1024 * 1024);
257             numZipBytes = Math.min(raf.readInt(), 20 * 1024 * 1024);
258             offset = ch.position();
259             raf.skipBytes(numZipBytes);
260         }
261
262         String getHtml() {
263             String html = htmlRef.get();
264             if (html != null) {
265                 return html;
266             }
267             if (data != null) {
268                 html = new String(data.get(index), StandardCharsets.UTF_8);
269                 htmlRef = new SoftReference<>(html);
270                 return html;
271             }
272             System.out.println("Loading Html: numBytes=" + numBytes + ", numZipBytes="
273                                + numZipBytes);
274             final byte[] zipBytes = new byte[numZipBytes];
275             synchronized (ch) {
276                 try {
277                     ch.position(offset);
278                     raf.readFully(zipBytes);
279                 } catch (IOException e) {
280                     throw new RuntimeException("Failed to read HTML data from dictionary", e);
281                 }
282             }
283             try {
284                 final byte[] bytes = StringUtil.unzipFully(zipBytes, numBytes);
285                 html = new String(bytes, StandardCharsets.UTF_8);
286             } catch (IOException e) {
287                 throw new RuntimeException("Dictionary HTML data corrupted", e);
288             }
289             htmlRef = new SoftReference<>(html);
290             return html;
291         }
292     }
293
294 }