]> gitweb.fperrin.net Git - DictionaryPC.git/blob - src/com/hughes/android/dictionary/parser/enwiktionary/AppendAndIndexWikiCallback.java
zipSize, overrideStoplist-> special isMainEntry, tagalog, trying to
[DictionaryPC.git] / src / com / hughes / android / dictionary / parser / enwiktionary / AppendAndIndexWikiCallback.java
1 // Copyright 2012 Google Inc. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package com.hughes.android.dictionary.parser.enwiktionary;
16
17 import java.util.LinkedHashMap;
18 import java.util.List;
19 import java.util.Map;
20 import java.util.concurrent.atomic.AtomicInteger;
21
22 import com.hughes.android.dictionary.engine.EntryTypeName;
23 import com.hughes.android.dictionary.engine.IndexBuilder;
24 import com.hughes.android.dictionary.engine.IndexedEntry;
25 import com.hughes.android.dictionary.parser.WikiTokenizer;
26 import com.hughes.util.EnumUtil;
27
28 final class AppendAndIndexWikiCallback implements WikiTokenizer.Callback {
29
30   final EnWiktionaryXmlParser parser;
31   StringBuilder builder;
32   IndexedEntry indexedEntry;
33   IndexBuilder indexBuilder;
34   final Map<String,FunctionCallback> functionCallbacks = new LinkedHashMap<String, FunctionCallback>();
35   
36   boolean entryTypeNameSticks = false;
37   EntryTypeName entryTypeName = null;
38   
39   final Map<String,AtomicInteger> langCodeToTCount = new LinkedHashMap<String, AtomicInteger>();
40   
41   public AppendAndIndexWikiCallback(final EnWiktionaryXmlParser parser) {
42     this.parser = parser;
43   }
44   
45   public void reset(final StringBuilder builder, final IndexedEntry indexedEntry) {
46     this.builder = builder;
47     this.indexedEntry = indexedEntry;
48     this.indexBuilder = null;
49     entryTypeName = null;
50     entryTypeNameSticks = false;
51   }
52   
53   public void dispatch(final String wikiText, final IndexBuilder indexBuilder, final EntryTypeName entryTypeName) {
54     final IndexBuilder oldIndexBuilder = this.indexBuilder;
55     final EntryTypeName oldEntryTypeName = this.entryTypeName;
56     this.indexBuilder = indexBuilder;
57     if (!entryTypeNameSticks) {
58       this.entryTypeName = EnumUtil.min(entryTypeName, this.entryTypeName);
59     }
60     if (entryTypeName == null) this.entryTypeName = null;
61     WikiTokenizer.dispatch(wikiText, false, this);
62     this.indexBuilder = oldIndexBuilder;
63     this.entryTypeName = oldEntryTypeName;
64   }
65   
66   public void dispatch(final String wikiText, final EntryTypeName entryTypeName) {
67     dispatch(wikiText, this.indexBuilder, entryTypeName);
68   }
69
70   
71   @Override
72   public void onPlainText(final String plainText) {
73     // The only non-recursive callback.  Just appends to the builder, and indexes.
74     builder.append(plainText);
75     if (indexBuilder != null && entryTypeName != null && indexedEntry != null) {
76       indexBuilder.addEntryWithString(indexedEntry, plainText, entryTypeName);
77     }
78   }
79
80   @Override
81   public void onWikiLink(WikiTokenizer wikiTokenizer) {
82     final String text = wikiTokenizer.wikiLinkText();
83     final String link = wikiTokenizer.wikiLinkDest();
84     if (link != null) {
85       if (link.contains("#English")) {
86         dispatch(text, parser.enIndexBuilder, EntryTypeName.WIKTIONARY_ENGLISH_DEF_WIKI_LINK);
87       } else if (link.contains("#") && parser.langPattern.matcher(link).find()) {
88         dispatch(text, parser.foreignIndexBuilder, EntryTypeName.WIKTIONARY_ENGLISH_DEF_OTHER_LANG);
89       } else if (link.equals("plural")) {
90         builder.append(text);
91       } else {
92         //LOG.warning("Special link: " + englishTokenizer.token());
93         dispatch(text, EntryTypeName.WIKTIONARY_ENGLISH_DEF_WIKI_LINK);
94       }
95     } else {
96       // link == null
97       final EntryTypeName entryTypeName;
98       switch (parser.state) {
99       case TRANSLATION_LINE:
100         entryTypeName = EntryTypeName.WIKTIONARY_TRANSLATION_WIKI_TEXT;
101         break;
102       case ENGLISH_DEF_OF_FOREIGN:
103         entryTypeName = EntryTypeName.WIKTIONARY_ENGLISH_DEF_WIKI_LINK;
104         break;
105         default:
106           throw new IllegalStateException("Invalid enum value: " + parser.state);
107       }
108       dispatch(text, entryTypeName);
109     }
110   }
111
112   @Override
113   public void onFunction(
114       final WikiTokenizer wikiTokenizer,
115       final String name,
116       final List<String> args, 
117       final Map<String, String> namedArgs) {
118     
119     FunctionCallback functionCallback = functionCallbacks.get(name);
120     if (functionCallback == null || !functionCallback.onWikiFunction(wikiTokenizer, name, args, namedArgs, parser, this)) {
121       // Default function handling:
122       namedArgs.keySet().removeAll(EnWiktionaryXmlParser.USELESS_WIKI_ARGS);
123       final boolean single = args.isEmpty() && namedArgs.isEmpty();
124       builder.append(single ? "{" : "{{");
125
126       final IndexBuilder oldIndexBuilder = indexBuilder;
127       indexBuilder = null;
128       FunctionCallbacksDefault.NAME_AND_ARGS.onWikiFunction(wikiTokenizer, name, args, namedArgs, parser, this);
129       indexBuilder = oldIndexBuilder;
130
131       builder.append(single ? "}" : "}}");
132     }
133   }
134
135   @Override
136   public void onHtml(WikiTokenizer wikiTokenizer) {
137     // Unindexed for now.
138     builder.append(wikiTokenizer.token());
139   }
140
141   @Override
142   public void onMarkup(WikiTokenizer wikiTokenizer) {
143     // Do nothing.
144   }
145
146   @Override
147   public void onComment(WikiTokenizer wikiTokenizer) {
148     // Do nothing.
149   }
150
151   @Override
152   public void onNewline(WikiTokenizer wikiTokenizer) {
153     assert false;
154   }
155
156   @Override
157   public void onHeading(WikiTokenizer wikiTokenizer) {
158     assert false;
159   }
160
161   @Override
162   public void onListItem(WikiTokenizer wikiTokenizer) {
163     assert false;
164   }
165
166 }