]> gitweb.fperrin.net Git - Dictionary.git/blob - src/com/hughes/android/dictionary/engine/HtmlEntry.java
WebView links starting to work (still timing problem).
[Dictionary.git] / src / com / hughes / android / dictionary / engine / HtmlEntry.java
1 package com.hughes.android.dictionary.engine;
2
3 import android.content.Intent;
4
5 import com.hughes.android.dictionary.C;
6 import com.hughes.util.StringUtil;
7 import com.hughes.util.raf.RAFListSerializer;
8 import com.hughes.util.raf.RAFSerializable;
9 import com.ibm.icu.text.Transliterator;
10
11 import java.io.IOException;
12 import java.io.PrintStream;
13 import java.io.RandomAccessFile;
14 import java.util.List;
15 import java.util.regex.Pattern;
16
17 public class HtmlEntry extends AbstractEntry implements RAFSerializable<HtmlEntry>, Comparable<HtmlEntry> {
18   
19   // Title is not HTML escaped.
20   public final String title;
21   public String html;
22   
23   public HtmlEntry(final EntrySource entrySource, String title) {
24     super(entrySource);
25     this.title = title;
26   }
27   
28   public HtmlEntry(Dictionary dictionary, RandomAccessFile raf, final int index) throws IOException {
29     super(dictionary, raf, index);
30     title = raf.readUTF();
31     html = raf.readUTF();
32   }
33   @Override
34   public void write(RandomAccessFile raf) throws IOException {
35     super.write(raf);
36     raf.writeUTF(title);
37     raf.writeUTF(html);
38   }
39
40   @Override
41   public void addToDictionary(Dictionary dictionary) {
42     assert index == -1;
43     dictionary.htmlEntries.add(this);
44     index = dictionary.htmlEntries.size() - 1;
45   }
46   
47   @Override
48   public RowBase CreateRow(int rowIndex, Index dictionaryIndex) {
49     return new Row(this.index, rowIndex, dictionaryIndex);
50   }
51
52   
53   static final class Serializer implements RAFListSerializer<HtmlEntry> {
54     
55     final Dictionary dictionary;
56     
57     Serializer(Dictionary dictionary) {
58       this.dictionary = dictionary;
59     }
60
61     @Override
62     public HtmlEntry read(RandomAccessFile raf, final int index) throws IOException {
63       return new HtmlEntry(dictionary, raf, index);
64     }
65
66     @Override
67     public void write(RandomAccessFile raf, HtmlEntry t) throws IOException {
68       t.write(raf);
69     }
70   };
71
72   public String getRawText(final boolean compact) {
73     return title + ":\n" + html;
74   }
75
76   
77   @Override
78   public int compareTo(HtmlEntry another) {
79     if (title.compareTo(another.title) != 0) {
80       return title.compareTo(another.title);
81     }
82     return html.compareTo(another.html);
83   }
84   
85   @Override
86   public String toString() {
87     return getRawText(false);
88   }
89   
90   // --------------------------------------------------------------------
91   
92
93   public static class Row extends RowBase {
94     
95     boolean isExpanded = false;
96     
97     Row(final RandomAccessFile raf, final int thisRowIndex,
98         final Index index) throws IOException {
99       super(raf, thisRowIndex, index);
100     }
101
102     Row(final int referenceIndex, final int thisRowIndex,
103         final Index index) {
104       super(referenceIndex, thisRowIndex, index);
105     }
106     
107     @Override
108     public String toString() {
109       return getRawText(false);
110     }
111
112     public HtmlEntry getEntry() {
113       return index.dict.htmlEntries.get(referenceIndex);
114     }
115     
116     @Override
117     public void print(PrintStream out) {
118       final HtmlEntry entry = getEntry();
119       out.println("See also HtmlEntry:" + entry.title);
120     }
121
122     @Override
123     public String getRawText(boolean compact) {
124       final HtmlEntry entry = getEntry();
125       return entry.getRawText(compact);
126     }
127
128     @Override
129     public RowMatchType matches(final List<String> searchTokens, final Pattern orderedMatchPattern, final Transliterator normalizer, final boolean swapPairEntries) {
130       final String text = normalizer.transform(getRawText(false));
131       if (orderedMatchPattern.matcher(text).find()) {
132         return RowMatchType.ORDERED_MATCH;
133       }
134       for (int i = searchTokens.size() - 1; i >= 0; --i) {
135         final String searchToken = searchTokens.get(i);
136         if (!text.contains(searchToken)) {
137           return RowMatchType.NO_MATCH;
138         }
139       }
140       return RowMatchType.BAG_OF_WORDS_MATCH;
141     }
142   }
143
144     public static String htmlBody(final List<HtmlEntry> htmlEntries, final String indexShortName) {
145         final StringBuilder result = new StringBuilder();
146         for (final HtmlEntry htmlEntry : htmlEntries) {
147             final String titleEscaped = StringUtil.escapeToPureHtmlUnicode(htmlEntry.title);
148             result.append(String.format("<h1><a href=\"%s\">%s</a></h1>\n(%s)\n<p>%s\n", 
149                     formatQuickdicUrl(indexShortName, titleEscaped), titleEscaped, htmlEntry.entrySource.name,
150                     htmlEntry.html));
151         }
152         return result.toString();
153     }
154     
155     public static String formatQuickdicUrl(final String indexShortName, final String text) {
156         assert !indexShortName.contains(":");
157         return String.format("qd:%s:%s", indexShortName, text);
158     }
159
160     public static boolean isQuickdicUrl(String url) {
161         return url.startsWith("qd:");
162     }
163     
164     public static void quickdicUrlToIntent(final String url, final Intent intent) {
165         int firstColon = url.indexOf(":");
166         if (firstColon == -1) return;
167         int secondColon = url.indexOf(":", firstColon + 1);
168         if (secondColon == -1) return;
169         intent.putExtra(C.SEARCH_TOKEN, url.substring(secondColon + 1));
170     }
171
172 }