]> gitweb.fperrin.net Git - DictionaryPC.git/blob - src/com/hughes/android/dictionary/parser/wiktionary/ItFunctionCallbacks.java
22d3f9f6a86c4ad1ad3154f2559d819761928eec
[DictionaryPC.git] / src / com / hughes / android / dictionary / parser / wiktionary / ItFunctionCallbacks.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.wiktionary;
16
17 import java.util.List;
18 import java.util.Map;
19
20 import com.hughes.android.dictionary.parser.WikiTokenizer;
21 import com.hughes.android.dictionary.parser.wiktionary.AbstractWiktionaryParser.AppendAndIndexWikiCallback;
22 import com.hughes.android.dictionary.parser.wiktionary.AbstractWiktionaryParser.NameAndArgs;
23
24 class ItFunctionCallbacks {
25
26     static <T extends AbstractWiktionaryParser> void addGenericCallbacks(
27         Map<String, FunctionCallback<T>> callbacks) {
28         callbacks.put("-hyph-", new Redispatch<T>("\n==== Sillabazione ====\n"));
29         callbacks.put("-pron-", new Redispatch<T>("\n==== Pronuncia ====\n"));
30         callbacks.put("-etim-", new Redispatch<T>("\n==== Etimologia / Derivazione ====\n"));
31         callbacks.put("-syn-", new Redispatch<T>("\n==== Sinonimi ====\n"));
32         callbacks.put("-ant-", new Redispatch<T>("\n==== Antonimi/Contrari ====\n"));
33         callbacks.put("-drv-", new Redispatch<T>("\n==== Parole derivate ====\n"));
34         callbacks.put("-prov-", new Redispatch<T>("\n==== Proverbi e modi di dire ====\n"));
35         callbacks.put("-ref-", new Redispatch<T>("\n==== Note / Riferimenti ====\n"));
36         callbacks.put("-rel-", new Redispatch<T>("\n==== Termini correlati ====\n"));
37         callbacks.put("-var-", new Redispatch<T>("\n==== Varianti ====\n"));
38
39         callbacks.put("-trans1-", new SkipSection<T>());
40         callbacks.put("-trans2-", new SkipSection<T>());
41         callbacks.put("-ref-", new SkipSection<T>());
42     }
43
44     static final NameAndArgs<EnParser> NAME_AND_ARGS = new NameAndArgs<EnParser>();
45
46     static final class Redispatch<T extends AbstractWiktionaryParser> implements
47         FunctionCallback<T> {
48         final String newText;
49
50         public Redispatch(String newText) {
51             this.newText = newText;
52         }
53
54         @Override
55         public boolean onWikiFunction(final WikiTokenizer wikiTokenizer, final String name,
56                                       final List<String> args,
57                                       final Map<String, String> namedArgs,
58                                       final T parser,
59                                       final AppendAndIndexWikiCallback<T> appendAndIndexWikiCallback) {
60             if (!namedArgs.isEmpty() || args.size() != 0) {
61                 return false;
62             }
63             appendAndIndexWikiCallback.dispatch(newText, null);
64             return true;
65         }
66     }
67
68     static final class SkipSection<T extends AbstractWiktionaryParser> implements
69         FunctionCallback<T> {
70         public SkipSection() {
71         }
72
73         @Override
74         public boolean onWikiFunction(final WikiTokenizer wikiTokenizer, final String name,
75                                       final List<String> args,
76                                       final Map<String, String> namedArgs,
77                                       final T parser,
78                                       final AppendAndIndexWikiCallback<T> appendAndIndexWikiCallback) {
79             while (wikiTokenizer.nextToken() != null) {
80                 if (wikiTokenizer.isFunction()
81                         && wikiTokenizer.functionName().startsWith("-")
82                         && wikiTokenizer.functionName().endsWith("-")
83                         // Hack to prevent infinite-looping, would be better to check that this func was at the start of the line.
84                         && !wikiTokenizer.functionName().contains("trans")) {
85                     wikiTokenizer.returnToLineStart();
86                     return true;
87                 }
88             }
89             return true;
90         }
91     }
92
93 }