]> gitweb.fperrin.net Git - DictionaryPC.git/blob - src/com/hughes/android/dictionary/parser/WikiFunction.java
e52a47065cf22f7d4942af567f26f9dcdc37d73b
[DictionaryPC.git] / src / com / hughes / android / dictionary / parser / WikiFunction.java
1 package com.hughes.android.dictionary.parser;
2
3 import java.util.ArrayList;
4 import java.util.LinkedHashMap;
5 import java.util.List;
6 import java.util.Map;
7 import java.util.regex.Matcher;
8 import java.util.regex.Pattern;
9
10 import com.sun.org.apache.bcel.internal.generic.NamedAndTyped;
11
12 public class WikiFunction {
13   
14   public int start;
15   public int end;
16   public String name = "";
17   public final List<String> args = new ArrayList<String>();;
18   public final Map<String,String> namedArgs = new LinkedHashMap<String, String>();
19   
20   private static final Pattern functionEvent = Pattern.compile("\\{\\{|\\[\\[|\\}\\}|\\]\\]|=|\\|");
21
22   public static WikiFunction getFunction(String line) {
23     final int start = line.indexOf("{{"); 
24     if (start == -1) {
25       return null;
26     }
27     final WikiFunction result = new WikiFunction();
28     result.start = start;
29     
30     final Matcher matcher = functionEvent.matcher(line);
31     int depth = 1;
32     int end = start + 2;
33     int lastPipe = end;
34     int lastEquals = -1;
35     while (end < line.length() && matcher.find(end)) {
36       end = matcher.end();
37       if (matcher.group().equals("{{") || matcher.group().equals("[[")) {
38         ++depth;
39       } else if (matcher.group().equals("}}") || matcher.group().equals("]]")) {
40         --depth;
41         if (depth == 0) {
42           break;
43         }
44       } else if (matcher.group().equals("|") && depth == 1) {
45         if (lastEquals != -1) {
46           result.namedArgs.put(line.substring(lastPipe, lastEquals), line.substring(lastEquals + 1, matcher.start()));
47         } else {
48           result.args.add(line.substring(lastPipe, matcher.start()));
49         }
50         lastPipe = matcher.end();
51         lastEquals = -1;
52       } else if (matcher.group().equals("=") && depth == 1) {
53         lastEquals = matcher.start();
54       }
55     }
56     if (depth > 0) {
57       System.err.println("Invalid function: " + line);
58       return null;
59     }
60     
61     if (lastEquals != -1) {
62       result.namedArgs.put(line.substring(lastPipe, lastEquals), line.substring(lastEquals + 1, matcher.start()));
63     } else {
64       result.args.add(line.substring(lastPipe, matcher.start()));
65     }
66     result.end = matcher.end();
67     if (result.args.size() > 0) {
68       result.name = result.args.remove(0);
69     } else {
70       System.err.println("Funnction unnamed: " + line);
71     }
72    
73     return result;
74   }
75   
76   public String getArg(final int pos) {
77     return (pos < args.size()) ? args.get(pos) : null;
78   }
79   
80   public String getNamedArg(final String name) {
81     return namedArgs.get(name);
82   }
83
84   public String replaceWith(final String line, final String sub) {
85     return line.substring(0, start) + sub + line.substring(end);
86   }
87   
88   
89
90 }