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();
34 public void write(RandomAccessFile raf) throws IOException {
41 public void addToDictionary(Dictionary dictionary) {
43 dictionary.htmlEntries.add(this);
44 index = dictionary.htmlEntries.size() - 1;
48 public RowBase CreateRow(int rowIndex, Index dictionaryIndex) {
49 return new Row(this.index, rowIndex, dictionaryIndex);
53 static final class Serializer implements RAFListSerializer<HtmlEntry> {
55 final Dictionary dictionary;
57 Serializer(Dictionary dictionary) {
58 this.dictionary = dictionary;
62 public HtmlEntry read(RandomAccessFile raf, final int index) throws IOException {
63 return new HtmlEntry(dictionary, raf, index);
67 public void write(RandomAccessFile raf, HtmlEntry t) throws IOException {
72 public String getRawText(final boolean compact) {
73 return title + ":\n" + html;
78 public int compareTo(HtmlEntry another) {
79 if (title.compareTo(another.title) != 0) {
80 return title.compareTo(another.title);
82 return html.compareTo(another.html);
86 public String toString() {
87 return getRawText(false);
90 // --------------------------------------------------------------------
93 public static class Row extends RowBase {
95 boolean isExpanded = false;
97 Row(final RandomAccessFile raf, final int thisRowIndex,
98 final Index index) throws IOException {
99 super(raf, thisRowIndex, index);
102 Row(final int referenceIndex, final int thisRowIndex,
104 super(referenceIndex, thisRowIndex, index);
108 public String toString() {
109 return getRawText(false);
112 public HtmlEntry getEntry() {
113 return index.dict.htmlEntries.get(referenceIndex);
117 public void print(PrintStream out) {
118 final HtmlEntry entry = getEntry();
119 out.println("See also HtmlEntry:" + entry.title);
123 public String getRawText(boolean compact) {
124 final HtmlEntry entry = getEntry();
125 return entry.getRawText(compact);
129 public RowMatchType matches(final List<String> searchTokens, final Pattern orderedMatchPattern, final Transliterator normalizer, final boolean swapPairEntries) {
130 final String text = normalizer.transform(getRawText(false));
131 if (orderedMatchPattern.matcher(text).find()) {
132 return RowMatchType.ORDERED_MATCH;
134 for (int i = searchTokens.size() - 1; i >= 0; --i) {
135 final String searchToken = searchTokens.get(i);
136 if (!text.contains(searchToken)) {
137 return RowMatchType.NO_MATCH;
140 return RowMatchType.BAG_OF_WORDS_MATCH;
144 public static String htmlBody(final List<HtmlEntry> htmlEntries, final String indexShortName) {
145 final StringBuilder result = new StringBuilder();
146 for (final HtmlEntry htmlEntry : htmlEntries) {
147 final String titleEscaped = StringUtil.escapeToPureHtmlUnicode(htmlEntry.title);
148 result.append(String.format("<h1><a href=\"%s\">%s</a></h1>\n(%s)\n<p>%s\n",
149 formatQuickdicUrl(indexShortName, titleEscaped), titleEscaped, htmlEntry.entrySource.name,
152 return result.toString();
155 public static String formatQuickdicUrl(final String indexShortName, final String text) {
156 assert !indexShortName.contains(":");
157 return String.format("qd:%s:%s", indexShortName, text);
160 public static boolean isQuickdicUrl(String url) {
161 return url.startsWith("qd:");
164 public static void quickdicUrlToIntent(final String url, final Intent intent) {
165 int firstColon = url.indexOf(":");
166 if (firstColon == -1) return;
167 int secondColon = url.indexOf(":", firstColon + 1);
168 if (secondColon == -1) return;
169 intent.putExtra(C.SEARCH_TOKEN, url.substring(secondColon + 1));