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> {
16 public final String title;
21 public HtmlEntry(final EntrySource entrySource, String title) {
26 public HtmlEntry(Dictionary dictionary, RandomAccessFile raf) throws IOException {
27 super(dictionary, raf);
28 title = raf.readUTF();
32 public void write(RandomAccessFile raf) throws IOException {
39 public int addToDictionary(Dictionary dictionary) {
40 dictionary.htmlEntries.add(this);
41 return dictionary.htmlEntries.size() - 1;
45 public RowBase CreateRow(int entryIndex, int rowIndex, Index dictionaryIndex) {
46 return new Row(entryIndex, rowIndex, dictionaryIndex);
50 static final class Serializer implements RAFSerializer<HtmlEntry> {
52 final Dictionary dictionary;
54 Serializer(Dictionary dictionary) {
55 this.dictionary = dictionary;
59 public HtmlEntry read(RandomAccessFile raf) throws IOException {
60 return new HtmlEntry(dictionary, raf);
64 public void write(RandomAccessFile raf, HtmlEntry t) throws IOException {
69 public String getRawText(final boolean compact) {
70 return title + ":\n" + html;
75 public int compareTo(HtmlEntry another) {
76 if (title.compareTo(another.title) != 0) {
77 return title.compareTo(another.title);
79 return html.compareTo(another.html);
83 public String toString() {
84 return getRawText(false);
87 // --------------------------------------------------------------------
90 public static class Row extends RowBase {
92 boolean isExpanded = false;
94 Row(final RandomAccessFile raf, final int thisRowIndex,
95 final Index index) throws IOException {
96 super(raf, thisRowIndex, index);
99 Row(final int referenceIndex, final int thisRowIndex,
101 super(referenceIndex, thisRowIndex, index);
105 public String toString() {
106 return getRawText(false);
109 public HtmlEntry getEntry() {
110 return index.dict.htmlEntries.get(referenceIndex);
114 public void print(PrintStream out) {
115 final HtmlEntry entry = getEntry();
120 public String getRawText(boolean compact) {
121 final HtmlEntry entry = getEntry();
122 return entry.getRawText(compact);
126 public RowMatchType matches(final List<String> searchTokens, final Pattern orderedMatchPattern, final Transliterator normalizer, final boolean swapPairEntries) {
127 final String text = normalizer.transform(getRawText(false));
128 if (orderedMatchPattern.matcher(text).find()) {
129 return RowMatchType.ORDERED_MATCH;
131 for (int i = searchTokens.size() - 1; i >= 0; --i) {
132 final String searchToken = searchTokens.get(i);
133 if (!text.contains(searchToken)) {
134 return RowMatchType.NO_MATCH;
137 return RowMatchType.BAG_OF_WORDS_MATCH;