1 package com.hughes.android.dictionary.engine;
3 import java.io.IOException;
4 import java.io.PrintStream;
5 import java.io.RandomAccessFile;
7 import java.util.regex.Pattern;
9 import com.hughes.android.dictionary.engine.PairEntry.Pair;
10 import com.hughes.util.raf.RAFSerializable;
11 import com.hughes.util.raf.RAFSerializer;
12 import com.ibm.icu.text.Transliterator;
14 public class HtmlEntry extends AbstractEntry implements RAFSerializable<HtmlEntry>, Comparable<HtmlEntry> {
21 public HtmlEntry(final EntrySource entrySource, String title, String html) {
27 public HtmlEntry(Dictionary dictionary, RandomAccessFile raf) throws IOException {
28 super(dictionary, raf);
29 title = raf.readUTF();
33 public void write(RandomAccessFile raf) throws IOException {
40 public int addToDictionary(Dictionary dictionary) {
41 dictionary.htmlEntries.add(this);
42 return dictionary.htmlEntries.size() - 1;
46 public RowBase CreateRow(int entryIndex, int rowIndex, Index dictionaryIndex) {
47 return new Row(entryIndex, rowIndex, dictionaryIndex);
51 static final class Serializer implements RAFSerializer<HtmlEntry> {
53 final Dictionary dictionary;
55 Serializer(Dictionary dictionary) {
56 this.dictionary = dictionary;
60 public HtmlEntry read(RandomAccessFile raf) throws IOException {
61 return new HtmlEntry(dictionary, raf);
65 public void write(RandomAccessFile raf, HtmlEntry t) throws IOException {
70 public String getRawText(final boolean compact) {
71 return title + ": " + html;
76 public int compareTo(HtmlEntry another) {
77 if (title.compareTo(another.title) != 0) {
78 return title.compareTo(another.title);
80 return html.compareTo(another.html);
84 public String toString() {
85 return getRawText(false);
88 // --------------------------------------------------------------------
91 public static class Row extends RowBase {
93 Row(final RandomAccessFile raf, final int thisRowIndex,
94 final Index index) throws IOException {
95 super(raf, thisRowIndex, index);
98 Row(final int referenceIndex, final int thisRowIndex,
100 super(referenceIndex, thisRowIndex, index);
104 public String toString() {
105 return getRawText(false);
108 public HtmlEntry getEntry() {
109 return index.dict.htmlEntries.get(referenceIndex);
113 public void print(PrintStream out) {
114 final HtmlEntry entry = getEntry();
119 public String getRawText(boolean compact) {
120 final HtmlEntry entry = getEntry();
121 return entry.getRawText(compact);
125 public RowMatchType matches(final List<String> searchTokens, final Pattern orderedMatchPattern, final Transliterator normalizer, final boolean swapPairEntries) {
126 final String text = normalizer.transform(getRawText(false));
127 if (orderedMatchPattern.matcher(text).find()) {
128 return RowMatchType.ORDERED_MATCH;
130 for (int i = searchTokens.size() - 1; i >= 0; --i) {
131 final String searchToken = searchTokens.get(i);
132 if (!text.contains(searchToken)) {
133 return RowMatchType.NO_MATCH;
136 return RowMatchType.BAG_OF_WORDS_MATCH;