]> gitweb.fperrin.net Git - Dictionary.git/blob - src/com/hughes/android/dictionary/engine/HtmlEntry.java
Non-final html text.
[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   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     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 }