]> gitweb.fperrin.net Git - Dictionary.git/blob - src/com/hughes/android/dictionary/engine/HtmlEntry.java
Added WholeSection entry type.
[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   
20
21   public HtmlEntry(final EntrySource entrySource, String title, String html) {
22     super(entrySource);
23     this.title = title;
24     this.html = html;
25   }
26   
27   public HtmlEntry(Dictionary dictionary, RandomAccessFile raf) throws IOException {
28     super(dictionary, raf);
29     title = raf.readUTF();
30     html = raf.readUTF();
31   }
32   @Override
33   public void write(RandomAccessFile raf) throws IOException {
34     super.write(raf);
35     raf.writeUTF(title);
36     raf.writeUTF(html);
37   }
38
39   @Override
40   public int addToDictionary(Dictionary dictionary) {
41     dictionary.htmlEntries.add(this);
42     return dictionary.htmlEntries.size() - 1;
43   }
44   
45   @Override
46   public RowBase CreateRow(int entryIndex, int rowIndex, Index dictionaryIndex) {
47     return new Row(entryIndex, rowIndex, dictionaryIndex);
48   }
49
50   
51   static final class Serializer implements RAFSerializer<HtmlEntry> {
52     
53     final Dictionary dictionary;
54     
55     Serializer(Dictionary dictionary) {
56       this.dictionary = dictionary;
57     }
58
59     @Override
60     public HtmlEntry read(RandomAccessFile raf) throws IOException {
61       return new HtmlEntry(dictionary, raf);
62     }
63
64     @Override
65     public void write(RandomAccessFile raf, HtmlEntry t) throws IOException {
66       t.write(raf);
67     }
68   };
69
70   public String getRawText(final boolean compact) {
71     return title + ": " + html;
72   }
73
74   
75   @Override
76   public int compareTo(HtmlEntry another) {
77     if (title.compareTo(another.title) != 0) {
78       return title.compareTo(another.title);
79     }
80     return html.compareTo(another.html);
81   }
82   
83   @Override
84   public String toString() {
85     return getRawText(false);
86   }
87   
88   // --------------------------------------------------------------------
89   
90
91   public static class Row extends RowBase {
92     
93     Row(final RandomAccessFile raf, final int thisRowIndex,
94         final Index index) throws IOException {
95       super(raf, thisRowIndex, index);
96     }
97
98     Row(final int referenceIndex, final int thisRowIndex,
99         final Index index) {
100       super(referenceIndex, thisRowIndex, index);
101     }
102     
103     @Override
104     public String toString() {
105       return getRawText(false);
106     }
107
108     public HtmlEntry getEntry() {
109       return index.dict.htmlEntries.get(referenceIndex);
110     }
111     
112     @Override
113     public void print(PrintStream out) {
114       final HtmlEntry entry = getEntry();
115       out.println(entry);
116     }
117
118     @Override
119     public String getRawText(boolean compact) {
120       final HtmlEntry entry = getEntry();
121       return entry.getRawText(compact);
122     }
123
124     @Override
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;
129       }
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;
134         }
135       }
136       return RowMatchType.BAG_OF_WORDS_MATCH;
137     }
138     
139   }
140
141 }