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