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