1 package com.hughes.android.dictionary.engine;
3 import android.content.Intent;
4 import android.net.Uri;
6 import com.hughes.android.dictionary.C;
7 import com.hughes.util.StringUtil;
8 import com.hughes.util.raf.RAFListSerializer;
9 import com.hughes.util.raf.RAFSerializable;
10 import com.ibm.icu.text.Transliterator;
12 import java.io.IOException;
13 import java.io.PrintStream;
14 import java.io.RandomAccessFile;
15 import java.io.UnsupportedEncodingException;
17 import java.net.URLDecoder;
18 import java.net.URLEncoder;
19 import java.util.List;
20 import java.util.regex.Pattern;
22 public class HtmlEntry extends AbstractEntry implements RAFSerializable<HtmlEntry>, Comparable<HtmlEntry> {
24 // Title is not HTML escaped.
25 public final String title;
28 public HtmlEntry(final EntrySource entrySource, String title) {
33 public HtmlEntry(Dictionary dictionary, RandomAccessFile raf, final int index) throws IOException {
34 super(dictionary, raf, index);
35 title = raf.readUTF();
37 final byte[] bytes = new byte[raf.readInt()];
38 final byte[] zipBytes = new byte[raf.readInt()];
40 StringUtil.unzipFully(zipBytes, bytes);
41 html = new String(bytes, "UTF-8");
44 public void write(RandomAccessFile raf) throws IOException {
48 final byte[] bytes = html.getBytes("UTF-8");
49 final byte[] zipBytes = StringUtil.zipBytes(bytes);
50 raf.writeInt(bytes.length);
51 raf.writeInt(zipBytes.length);
56 public void addToDictionary(Dictionary dictionary) {
58 dictionary.htmlEntries.add(this);
59 index = dictionary.htmlEntries.size() - 1;
63 public RowBase CreateRow(int rowIndex, Index dictionaryIndex) {
64 return new Row(this.index, rowIndex, dictionaryIndex);
68 static final class Serializer implements RAFListSerializer<HtmlEntry> {
70 final Dictionary dictionary;
72 Serializer(Dictionary dictionary) {
73 this.dictionary = dictionary;
77 public HtmlEntry read(RandomAccessFile raf, final int index) throws IOException {
78 return new HtmlEntry(dictionary, raf, index);
82 public void write(RandomAccessFile raf, HtmlEntry t) throws IOException {
87 public String getRawText(final boolean compact) {
88 return title + ":\n" + html;
93 public int compareTo(HtmlEntry another) {
94 if (title.compareTo(another.title) != 0) {
95 return title.compareTo(another.title);
97 return html.compareTo(another.html);
101 public String toString() {
102 return getRawText(false);
105 // --------------------------------------------------------------------
108 public static class Row extends RowBase {
110 boolean isExpanded = false;
112 Row(final RandomAccessFile raf, final int thisRowIndex,
113 final Index index) throws IOException {
114 super(raf, thisRowIndex, index);
117 Row(final int referenceIndex, final int thisRowIndex,
119 super(referenceIndex, thisRowIndex, index);
123 public String toString() {
124 return getRawText(false);
127 public HtmlEntry getEntry() {
128 return index.dict.htmlEntries.get(referenceIndex);
132 public void print(PrintStream out) {
133 final HtmlEntry entry = getEntry();
134 out.println("See also HtmlEntry:" + entry.title);
138 public String getRawText(boolean compact) {
139 final HtmlEntry entry = getEntry();
140 return entry.getRawText(compact);
144 public RowMatchType matches(final List<String> searchTokens, final Pattern orderedMatchPattern, final Transliterator normalizer, final boolean swapPairEntries) {
145 final String text = normalizer.transform(getRawText(false));
146 if (orderedMatchPattern.matcher(text).find()) {
147 return RowMatchType.ORDERED_MATCH;
149 for (int i = searchTokens.size() - 1; i >= 0; --i) {
150 final String searchToken = searchTokens.get(i);
151 if (!text.contains(searchToken)) {
152 return RowMatchType.NO_MATCH;
155 return RowMatchType.BAG_OF_WORDS_MATCH;
159 public static String htmlBody(final List<HtmlEntry> htmlEntries, final String indexShortName) {
160 final StringBuilder result = new StringBuilder();
161 for (final HtmlEntry htmlEntry : htmlEntries) {
162 final String titleEscaped = StringUtil.escapeToPureHtmlUnicode(htmlEntry.title);
163 result.append(String.format("<h1><a href=\"%s\">%s</a></h1>\n(%s)\n<p>%s\n",
164 formatQuickdicUrl(indexShortName, titleEscaped), titleEscaped, htmlEntry.entrySource.name,
167 return result.toString();
170 public static String formatQuickdicUrl(final String indexShortName, final String text) {
171 assert !indexShortName.contains(":");
172 assert text.length() > 0;
174 return String.format("qd:%s:%s", indexShortName, URLEncoder.encode(text, "UTF-8"));
175 } catch (UnsupportedEncodingException e) {
176 throw new RuntimeException(e);
180 public static boolean isQuickdicUrl(String url) {
181 return url.startsWith("qd:");
184 public static void quickdicUrlToIntent(final String url, final Intent intent) {
185 int firstColon = url.indexOf(":");
186 if (firstColon == -1) return;
187 int secondColon = url.indexOf(":", firstColon + 1);
188 if (secondColon == -1) return;
189 intent.putExtra(C.SEARCH_TOKEN, Uri.decode(url.substring(secondColon + 1)));