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();
31 final boolean compressed = raf.readBoolean();
32 final int length = raf.readInt();
33 final byte[] bytes = new byte[length];
35 html = new String(bytes, "UTF-8");
38 public void write(RandomAccessFile raf) throws IOException {
41 raf.writeBoolean(false);
42 final byte[] bytes = html.getBytes("UTF-8");
43 raf.writeInt(bytes.length);
48 public void addToDictionary(Dictionary dictionary) {
50 dictionary.htmlEntries.add(this);
51 index = dictionary.htmlEntries.size() - 1;
55 public RowBase CreateRow(int rowIndex, Index dictionaryIndex) {
56 return new Row(this.index, rowIndex, dictionaryIndex);
60 static final class Serializer implements RAFListSerializer<HtmlEntry> {
62 final Dictionary dictionary;
64 Serializer(Dictionary dictionary) {
65 this.dictionary = dictionary;
69 public HtmlEntry read(RandomAccessFile raf, final int index) throws IOException {
70 return new HtmlEntry(dictionary, raf, index);
74 public void write(RandomAccessFile raf, HtmlEntry t) throws IOException {
79 public String getRawText(final boolean compact) {
80 return title + ":\n" + html;
85 public int compareTo(HtmlEntry another) {
86 if (title.compareTo(another.title) != 0) {
87 return title.compareTo(another.title);
89 return html.compareTo(another.html);
93 public String toString() {
94 return getRawText(false);
97 // --------------------------------------------------------------------
100 public static class Row extends RowBase {
102 boolean isExpanded = false;
104 Row(final RandomAccessFile raf, final int thisRowIndex,
105 final Index index) throws IOException {
106 super(raf, thisRowIndex, index);
109 Row(final int referenceIndex, final int thisRowIndex,
111 super(referenceIndex, thisRowIndex, index);
115 public String toString() {
116 return getRawText(false);
119 public HtmlEntry getEntry() {
120 return index.dict.htmlEntries.get(referenceIndex);
124 public void print(PrintStream out) {
125 final HtmlEntry entry = getEntry();
126 out.println("See also HtmlEntry:" + entry.title);
130 public String getRawText(boolean compact) {
131 final HtmlEntry entry = getEntry();
132 return entry.getRawText(compact);
136 public RowMatchType matches(final List<String> searchTokens, final Pattern orderedMatchPattern, final Transliterator normalizer, final boolean swapPairEntries) {
137 final String text = normalizer.transform(getRawText(false));
138 if (orderedMatchPattern.matcher(text).find()) {
139 return RowMatchType.ORDERED_MATCH;
141 for (int i = searchTokens.size() - 1; i >= 0; --i) {
142 final String searchToken = searchTokens.get(i);
143 if (!text.contains(searchToken)) {
144 return RowMatchType.NO_MATCH;
147 return RowMatchType.BAG_OF_WORDS_MATCH;
151 public static String htmlBody(final List<HtmlEntry> htmlEntries, final String indexShortName) {
152 final StringBuilder result = new StringBuilder();
153 for (final HtmlEntry htmlEntry : htmlEntries) {
154 final String titleEscaped = StringUtil.escapeToPureHtmlUnicode(htmlEntry.title);
155 result.append(String.format("<h1><a href=\"%s\">%s</a></h1>\n(%s)\n<p>%s\n",
156 formatQuickdicUrl(indexShortName, titleEscaped), titleEscaped, htmlEntry.entrySource.name,
159 return result.toString();
162 public static String formatQuickdicUrl(final String indexShortName, final String text) {
163 assert !indexShortName.contains(":");
164 return String.format("qd:%s:%s", indexShortName, text);
167 public static boolean isQuickdicUrl(String url) {
168 return url.startsWith("qd:");
171 public static void quickdicUrlToIntent(final String url, final Intent intent) {
172 int firstColon = url.indexOf(":");
173 if (firstColon == -1) return;
174 int secondColon = url.indexOf(":", firstColon + 1);
175 if (secondColon == -1) return;
176 intent.putExtra(C.SEARCH_TOKEN, url.substring(secondColon + 1));