1 package com.hughes.android.dictionary.engine;
3 import android.content.Intent;
5 import com.hughes.android.dictionary.C;
6 import com.hughes.util.StringUtil;
7 import com.hughes.util.raf.RAFListSerializer;
8 import com.hughes.util.raf.RAFSerializable;
9 import com.ibm.icu.text.Transliterator;
11 import java.io.IOException;
12 import java.io.PrintStream;
13 import java.io.RandomAccessFile;
14 import java.util.List;
15 import java.util.regex.Pattern;
17 public class HtmlEntry extends AbstractEntry implements RAFSerializable<HtmlEntry>, Comparable<HtmlEntry> {
19 // Title is not HTML escaped.
20 public final String title;
23 public HtmlEntry(final EntrySource entrySource, String title) {
28 public HtmlEntry(Dictionary dictionary, RandomAccessFile raf, final int index) throws IOException {
29 super(dictionary, raf, index);
30 title = raf.readUTF();
32 final byte[] bytes = new byte[raf.readInt()];
33 final byte[] zipBytes = new byte[raf.readInt()];
35 StringUtil.unzipFully(zipBytes, bytes);
36 html = new String(bytes, "UTF-8");
39 public void write(RandomAccessFile raf) throws IOException {
43 final byte[] bytes = html.getBytes("UTF-8");
44 final byte[] zipBytes = StringUtil.zipBytes(bytes);
45 raf.writeInt(bytes.length);
46 raf.writeInt(zipBytes.length);
51 public void addToDictionary(Dictionary dictionary) {
53 dictionary.htmlEntries.add(this);
54 index = dictionary.htmlEntries.size() - 1;
58 public RowBase CreateRow(int rowIndex, Index dictionaryIndex) {
59 return new Row(this.index, rowIndex, dictionaryIndex);
63 static final class Serializer implements RAFListSerializer<HtmlEntry> {
65 final Dictionary dictionary;
67 Serializer(Dictionary dictionary) {
68 this.dictionary = dictionary;
72 public HtmlEntry read(RandomAccessFile raf, final int index) throws IOException {
73 return new HtmlEntry(dictionary, raf, index);
77 public void write(RandomAccessFile raf, HtmlEntry t) throws IOException {
82 public String getRawText(final boolean compact) {
83 return title + ":\n" + html;
88 public int compareTo(HtmlEntry another) {
89 if (title.compareTo(another.title) != 0) {
90 return title.compareTo(another.title);
92 return html.compareTo(another.html);
96 public String toString() {
97 return getRawText(false);
100 // --------------------------------------------------------------------
103 public static class Row extends RowBase {
105 boolean isExpanded = false;
107 Row(final RandomAccessFile raf, final int thisRowIndex,
108 final Index index) throws IOException {
109 super(raf, thisRowIndex, index);
112 Row(final int referenceIndex, final int thisRowIndex,
114 super(referenceIndex, thisRowIndex, index);
118 public String toString() {
119 return getRawText(false);
122 public HtmlEntry getEntry() {
123 return index.dict.htmlEntries.get(referenceIndex);
127 public void print(PrintStream out) {
128 final HtmlEntry entry = getEntry();
129 out.println("See also HtmlEntry:" + entry.title);
133 public String getRawText(boolean compact) {
134 final HtmlEntry entry = getEntry();
135 return entry.getRawText(compact);
139 public RowMatchType matches(final List<String> searchTokens, final Pattern orderedMatchPattern, final Transliterator normalizer, final boolean swapPairEntries) {
140 final String text = normalizer.transform(getRawText(false));
141 if (orderedMatchPattern.matcher(text).find()) {
142 return RowMatchType.ORDERED_MATCH;
144 for (int i = searchTokens.size() - 1; i >= 0; --i) {
145 final String searchToken = searchTokens.get(i);
146 if (!text.contains(searchToken)) {
147 return RowMatchType.NO_MATCH;
150 return RowMatchType.BAG_OF_WORDS_MATCH;
154 public static String htmlBody(final List<HtmlEntry> htmlEntries, final String indexShortName) {
155 final StringBuilder result = new StringBuilder();
156 for (final HtmlEntry htmlEntry : htmlEntries) {
157 final String titleEscaped = StringUtil.escapeToPureHtmlUnicode(htmlEntry.title);
158 result.append(String.format("<h1><a href=\"%s\">%s</a></h1>\n(%s)\n<p>%s\n",
159 formatQuickdicUrl(indexShortName, titleEscaped), titleEscaped, htmlEntry.entrySource.name,
162 return result.toString();
165 public static String formatQuickdicUrl(final String indexShortName, final String text) {
166 assert !indexShortName.contains(":");
167 assert text.length() > 0;
168 return String.format("qd:%s:%s", indexShortName, text);
171 public static boolean isQuickdicUrl(String url) {
172 return url.startsWith("qd:");
175 public static void quickdicUrlToIntent(final String url, final Intent intent) {
176 int firstColon = url.indexOf(":");
177 if (firstColon == -1) return;
178 int secondColon = url.indexOf(":", firstColon + 1);
179 if (secondColon == -1) return;
180 intent.putExtra(C.SEARCH_TOKEN, url.substring(secondColon + 1));