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