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