]> gitweb.fperrin.net Git - Dictionary.git/blob - src/com/hughes/android/dictionary/engine/HtmlEntry.java
Add TTS.
[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   // Both are HTML escaped already.
16   public final String title;
17   public String html;
18   
19   public HtmlEntry(final EntrySource entrySource, String title) {
20     super(entrySource);
21     this.title = title;
22   }
23   
24   public HtmlEntry(Dictionary dictionary, RandomAccessFile raf, final int index) throws IOException {
25     super(dictionary, raf, index);
26     title = raf.readUTF();
27     html = raf.readUTF();
28   }
29   @Override
30   public void write(RandomAccessFile raf) throws IOException {
31     super.write(raf);
32     raf.writeUTF(title);
33     raf.writeUTF(html);
34   }
35
36   @Override
37   public void addToDictionary(Dictionary dictionary) {
38     assert index == -1;
39     dictionary.htmlEntries.add(this);
40     index = dictionary.htmlEntries.size() - 1;
41   }
42   
43   @Override
44   public RowBase CreateRow(int rowIndex, Index dictionaryIndex) {
45     return new Row(this.index, rowIndex, dictionaryIndex);
46   }
47
48   
49   static final class Serializer implements RAFListSerializer<HtmlEntry> {
50     
51     final Dictionary dictionary;
52     
53     Serializer(Dictionary dictionary) {
54       this.dictionary = dictionary;
55     }
56
57     @Override
58     public HtmlEntry read(RandomAccessFile raf, final int index) throws IOException {
59       return new HtmlEntry(dictionary, raf, index);
60     }
61
62     @Override
63     public void write(RandomAccessFile raf, HtmlEntry t) throws IOException {
64       t.write(raf);
65     }
66   };
67
68   public String getRawText(final boolean compact) {
69     return title + ":\n" + html;
70   }
71
72   
73   @Override
74   public int compareTo(HtmlEntry another) {
75     if (title.compareTo(another.title) != 0) {
76       return title.compareTo(another.title);
77     }
78     return html.compareTo(another.html);
79   }
80   
81   @Override
82   public String toString() {
83     return getRawText(false);
84   }
85   
86   // --------------------------------------------------------------------
87   
88
89   public static class Row extends RowBase {
90     
91     boolean isExpanded = false;
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("HtmlEntry (shortened): " + entry.title);
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     public static String htmlBody(final List<HtmlEntry> htmlEntries) {
142         final StringBuilder result = new StringBuilder();
143         for (final HtmlEntry htmlEntry : htmlEntries) {
144             result.append(String.format("<h1><a href=\"%s\">%s</a></h1>\n(%s)\n<p>%s\n", 
145                     htmlEntry.title, htmlEntry.title, htmlEntry.entrySource.name,
146                     htmlEntry.html));
147         }
148         return result.toString();
149     }
150
151 }