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