1 package com.hughes.android.dictionary.parser;
3 import java.util.ArrayList;
5 import java.util.regex.Matcher;
6 import java.util.regex.Pattern;
8 public class WikiLineReader {
10 private final List<String> lineStack = new ArrayList<String>();
12 private final String wikiText;
13 private int lineStart = 0;
15 private static final Pattern wikiLineEvent = Pattern.compile("$|\\{\\{|\\[\\[|\\}\\}|\\]\\]|<!--|<pre>", Pattern.MULTILINE);
17 private static final Pattern whitespace = Pattern.compile("\\s+");
19 public WikiLineReader(final String wikiText) {
20 this.wikiText = wikiText;
23 public String readLine() {
24 while (lineStart < wikiText.length() &&
25 Character.isWhitespace(wikiText.charAt(lineStart)) &&
26 wikiText.charAt(lineStart) != '\n') {
29 if (lineStart >= wikiText.length()) {
33 int lineEnd = lineStart;
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();
42 lineEnd = matcher.end();
43 if (lineEnd == wikiText.length()) {
46 if (matcher.group().equals("")) {
47 assert (wikiText.charAt(matcher.start()) == '\n');
49 if (lineStack.size() == 0) {
52 if (firstNewline == -1) {
53 firstNewline = matcher.end();
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");
66 if (removed.equals("[[") && !matcher.group().equals("]]")) {
67 System.err.println("Error");
70 System.err.println("Error");
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");
78 if (lineStack.size() > 0 && firstNewline != -1) {
79 lineEnd = firstNewline + 1;
81 final String result = wikiText.substring(lineStart, lineEnd);
87 static int safeIndexOf(final String s, final int start, final String target, final String backup) {
88 int close = s.indexOf(target, start);
90 return close + target.length();
92 close = s.indexOf(backup, start);
94 return close + backup.length();
99 public static String cleanUpLine(String line) {
101 while ((pos = line.indexOf("<!--")) != -1) {
102 int end = line.indexOf("-->");
104 line = line.substring(0, pos) + line.substring(end + 3);
107 final Matcher matcher = whitespace.matcher(line);
108 line = matcher.replaceAll(" ");