]> gitweb.fperrin.net Git - DictionaryPC.git/blob - src/com/hughes/android/dictionary/parser/WikiLineReader.java
4f2e0825de96adf1a27f5cd7ab5fc2f31bf44aa6
[DictionaryPC.git] / src / com / hughes / android / dictionary / parser / WikiLineReader.java
1 package com.hughes.android.dictionary.parser;
2
3 import java.util.ArrayList;
4 import java.util.List;
5 import java.util.regex.Matcher;
6 import java.util.regex.Pattern;
7
8 public class WikiLineReader {
9   
10   private final List<String> lineStack = new ArrayList<String>();
11   
12   private final String wikiText;
13   private int lineStart = 0;
14   
15   private static final Pattern wikiLineEvent = Pattern.compile("$|\\{\\{|\\[\\[|\\}\\}|\\]\\]|<!--|<pre>", Pattern.MULTILINE);
16
17   private static final Pattern whitespace = Pattern.compile("\\s+");
18   
19   public WikiLineReader(final String wikiText) {
20     this.wikiText = wikiText;
21   }
22
23   public String readLine() {
24     while (lineStart < wikiText.length() && 
25         Character.isWhitespace(wikiText.charAt(lineStart)) && 
26         wikiText.charAt(lineStart) != '\n') {
27       ++lineStart;
28     }
29     if (lineStart >= wikiText.length()) {
30       return null;
31     }
32
33     int lineEnd = lineStart;
34     lineStack.clear();
35     int firstNewline = -1;
36     final Matcher matcher = wikiLineEvent.matcher(wikiText);
37     while (lineEnd < wikiText.length()) {
38       if (!matcher.find(lineEnd)) {
39         lineEnd = wikiText.length();
40         break;
41       }
42       lineEnd = matcher.end();
43       if (lineEnd == wikiText.length()) {
44         break;
45       }
46       if (matcher.group().equals("")) {
47         assert (wikiText.charAt(matcher.start()) == '\n');
48         ++lineEnd;
49         if (lineStack.size() == 0) {
50           break;
51         } else {
52           if (firstNewline == -1) {
53             firstNewline = matcher.end();
54           }
55         }
56       }
57       
58       if (matcher.group().equals("[[") || matcher.group().equals("{{")) {
59         lineStack.add(matcher.group());
60       } else if (matcher.group().equals("}}") || matcher.group().equals("]]")) {
61         if (lineStack.size() > 0) {
62           final String removed = lineStack.remove(lineStack.size() - 1);
63           if (removed.equals("{{") && !matcher.group().equals("}}")) {
64             System.err.println("Error");
65           }
66           if (removed.equals("[[") && !matcher.group().equals("]]")) {
67             System.err.println("Error");
68           }
69         } else {
70           System.err.println("Error");
71         }
72       } else if (matcher.group().equals("<!--")) {
73         lineEnd = safeIndexOf(wikiText, lineEnd, "-->", "\n");
74       } else if (matcher.group().equals("<pre>")) {
75         lineEnd = safeIndexOf(wikiText, lineEnd, "</pre>", "\n");
76       }
77     }
78     if (lineStack.size() > 0 && firstNewline != -1) {
79       lineEnd = firstNewline + 1;
80     }
81     final String result = wikiText.substring(lineStart, lineEnd);
82     lineStart = lineEnd;
83     return result;
84   }
85     
86     
87   static int safeIndexOf(final String s, final int start, final String target, final String backup) {
88     int close = s.indexOf(target, start);
89     if (close != -1) {
90       return close + target.length();
91     }
92     close = s.indexOf(backup, start);
93     if (close != -1) {
94       return close + backup.length();
95     }
96     return s.length();
97   }
98   
99   public static String cleanUpLine(String line) {
100     int pos;
101     while ((pos = line.indexOf("<!--")) != -1) {
102       int end = line.indexOf("-->");
103       if (end != -1) {
104         line = line.substring(0, pos) + line.substring(end + 3);
105       }
106     }
107     final Matcher matcher = whitespace.matcher(line);
108     line = matcher.replaceAll(" ");
109     line = line.trim();
110     return line;
111   }
112
113 }