]> gitweb.fperrin.net Git - Dictionary.git/blob - src/com/hughes/android/dictionary/engine/HtmlEntry.java
First decent implementation of HtmlEntry attached to TokenRow.
[Dictionary.git] / src / com / hughes / android / dictionary / engine / HtmlEntry.java
1 package com.hughes.android.dictionary.engine;
2
3 import com.hughes.util.raf.RAFListSerializer;
4 import com.hughes.util.raf.RAFSerializable;
5 import com.ibm.icu.text.Transliterator;
6
7 import java.io.IOException;
8 import java.io.PrintStream;
9 import java.io.RandomAccessFile;
10 import java.util.List;
11 import java.util.regex.Pattern;
12
13 public class HtmlEntry extends AbstractEntry implements RAFSerializable<HtmlEntry>, Comparable<HtmlEntry> {
14   
15   public final String title;
16   public String html;
17   
18   public HtmlEntry(final EntrySource entrySource, String title) {
19     super(entrySource);
20     this.title = title;
21   }
22   
23   public HtmlEntry(Dictionary dictionary, RandomAccessFile raf, final int index) throws IOException {
24     super(dictionary, raf, index);
25     title = raf.readUTF();
26     html = raf.readUTF();
27   }
28   @Override
29   public void write(RandomAccessFile raf) throws IOException {
30     super.write(raf);
31     raf.writeUTF(title);
32     raf.writeUTF(html);
33   }
34
35   @Override
36   public void addToDictionary(Dictionary dictionary) {
37     assert index == -1;
38     dictionary.htmlEntries.add(this);
39     index = dictionary.htmlEntries.size() - 1;
40   }
41   
42   @Override
43   public RowBase CreateRow(int rowIndex, Index dictionaryIndex) {
44     return new Row(this.index, rowIndex, dictionaryIndex);
45   }
46
47   
48   static final class Serializer implements RAFListSerializer<HtmlEntry> {
49     
50     final Dictionary dictionary;
51     
52     Serializer(Dictionary dictionary) {
53       this.dictionary = dictionary;
54     }
55
56     @Override
57     public HtmlEntry read(RandomAccessFile raf, final int index) throws IOException {
58       return new HtmlEntry(dictionary, raf, index);
59     }
60
61     @Override
62     public void write(RandomAccessFile raf, HtmlEntry t) throws IOException {
63       t.write(raf);
64     }
65   };
66
67   public String getRawText(final boolean compact) {
68     return title + ":\n" + html;
69   }
70
71   
72   @Override
73   public int compareTo(HtmlEntry another) {
74     if (title.compareTo(another.title) != 0) {
75       return title.compareTo(another.title);
76     }
77     return html.compareTo(another.html);
78   }
79   
80   @Override
81   public String toString() {
82     return getRawText(false);
83   }
84   
85   // --------------------------------------------------------------------
86   
87
88   public static class Row extends RowBase {
89     
90     boolean isExpanded = false;
91     
92     Row(final RandomAccessFile raf, final int thisRowIndex,
93         final Index index) throws IOException {
94       super(raf, thisRowIndex, index);
95     }
96
97     Row(final int referenceIndex, final int thisRowIndex,
98         final Index index) {
99       super(referenceIndex, thisRowIndex, index);
100     }
101     
102     @Override
103     public String toString() {
104       return getRawText(false);
105     }
106
107     public HtmlEntry getEntry() {
108       return index.dict.htmlEntries.get(referenceIndex);
109     }
110     
111     @Override
112     public void print(PrintStream out) {
113       final HtmlEntry entry = getEntry();
114       out.println(entry);
115     }
116
117     @Override
118     public String getRawText(boolean compact) {
119       final HtmlEntry entry = getEntry();
120       return entry.getRawText(compact);
121     }
122
123     @Override
124     public RowMatchType matches(final List<String> searchTokens, final Pattern orderedMatchPattern, final Transliterator normalizer, final boolean swapPairEntries) {
125       final String text = normalizer.transform(getRawText(false));
126       if (orderedMatchPattern.matcher(text).find()) {
127         return RowMatchType.ORDERED_MATCH;
128       }
129       for (int i = searchTokens.size() - 1; i >= 0; --i) {
130         final String searchToken = searchTokens.get(i);
131         if (!text.contains(searchToken)) {
132           return RowMatchType.NO_MATCH;
133         }
134       }
135       return RowMatchType.BAG_OF_WORDS_MATCH;
136     }
137     
138   }
139
140 }