001: // ListToken.java
002: // ---------
003: // part of YaCy
004: // (C) by Michael Peter Christen; mc@anomic.de
005: // first published on http://www.anomic.de
006: // Frankfurt, Germany, 2007
007: // Created 22.02.2007
008: //
009: // This file is contributed by Franz Brausze
010: //
011: // $LastChangedDate: $
012: // $LastChangedRevision: $
013: // $LastChangedBy: $
014: //
015: // This program is free software; you can redistribute it and/or modify
016: // it under the terms of the GNU General Public License as published by
017: // the Free Software Foundation; either version 2 of the License, or
018: // (at your option) any later version.
019: //
020: // This program is distributed in the hope that it will be useful,
021: // but WITHOUT ANY WARRANTY; without even the implied warranty of
022: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
023: // GNU General Public License for more details.
024: //
025: // You should have received a copy of the GNU General Public License
026: // along with this program; if not, write to the Free Software
027: // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
028: //
029: // Using this software in any meaning (reading, learning, copying, compiling,
030: // running) means that you agree that the Author(s) is (are) not responsible
031: // for cost, loss of data or any harm that may be caused directly or indirectly
032: // by usage of this softare or this documentation. The usage of this software
033: // is on your own risk. The installation and usage (starting/running) of this
034: // software may allow other people or application to access your computer and
035: // any attached devices and is highly dependent on the configuration of the
036: // software which must be done by the user of the software; the author(s) is
037: // (are) also not responsible for proper configuration and usage of the
038: // software, even if provoked by documentation provided together with
039: // the software.
040: //
041: // Any changes to this file according to the GPL as documented in the file
042: // gpl.txt aside this file in the shipment you received can be done to the
043: // lines that follows this copyright notice here, but changes must not be
044: // done inside the copyright notive above. A re-distribution must contain
045: // the intact and unchanged copyright notice.
046: // Contributions and changes to the program code must be marked as such.
047:
048: package de.anomic.data.wiki.tokens;
049:
050: import java.util.ArrayList;
051: import java.util.regex.Pattern;
052:
053: public class ListToken extends AbstractToken {
054:
055: protected final String[] blockElements;
056:
057: protected final char firstChar;
058: protected final String listBlockElement;
059: protected final String listElement;
060: protected final boolean recursion;
061: protected final Pattern[] pattern;
062:
063: protected int aktline = 0;
064:
065: public ListToken(char firstChar, String listBlockElement) {
066: this .firstChar = firstChar;
067: this .listBlockElement = listBlockElement;
068: this .listElement = "li";
069: this .recursion = true;
070: this .pattern = new Pattern[] { Pattern.compile("^[" + firstChar
071: + "]([^\n]|\n[" + firstChar + "])*", Pattern.MULTILINE) };
072: ArrayList<String> r = new ArrayList<String>();
073: if (this .listBlockElement != null) {
074: if (this .recursion)
075: r.add(this .listBlockElement);
076: if (this .listElement != null)
077: r.add(this .listElement);
078: }
079: blockElements = (String[]) r.toArray(new String[r.size()]);
080: }
081:
082: public ListToken(char firstChar, String listBlockElement,
083: String listElement) {
084: this .firstChar = firstChar;
085: this .listBlockElement = listBlockElement;
086: this .listElement = listElement;
087: this .recursion = true;
088: this .pattern = new Pattern[] { Pattern.compile("^[" + firstChar
089: + "]([^\n]|\n[" + firstChar + "])*", Pattern.MULTILINE) };
090: ArrayList<String> r = new ArrayList<String>();
091: if (this .listBlockElement != null) {
092: if (this .recursion)
093: r.add(this .listBlockElement);
094: if (this .listElement != null)
095: r.add(this .listElement);
096: }
097: blockElements = (String[]) r.toArray(new String[r.size()]);
098: }
099:
100: public ListToken(char firstChar, String listBlockElement,
101: String listElement, boolean recursion) {
102: this .firstChar = firstChar;
103: this .listBlockElement = listBlockElement;
104: this .listElement = listElement;
105: this .recursion = recursion;
106: this .pattern = new Pattern[] { Pattern.compile("^[" + firstChar
107: + "]([^\n]|\n[" + firstChar + "])*", Pattern.MULTILINE) };
108: ArrayList<String> r = new ArrayList<String>();
109: if (this .listBlockElement != null) {
110: if (this .recursion)
111: r.add(this .listBlockElement);
112: if (this .listElement != null)
113: r.add(this .listElement);
114: }
115: blockElements = (String[]) r.toArray(new String[r.size()]);
116: }
117:
118: protected void parse() {
119: StringBuffer sb = new StringBuffer(this .text.length());
120: parse(this .text.split("\n"), 0, sb);
121: this .markup = new String(sb);
122: this .parsed = true;
123: }
124:
125: protected StringBuffer parse(String[] t, int depth, StringBuffer sb) {
126: if (this .listBlockElement != null)
127: sb.append("<").append(this .listBlockElement).append(">\n");
128: while (this .aktline < t.length
129: && getGrade(t[this .aktline]) >= depth) {
130: if (recursion)
131: for (int j = 0; j < depth + 1; j++)
132: sb.append("\t");
133: if (this .listElement != null)
134: sb.append("<").append(this .listElement).append(">");
135:
136: if (this .recursion && getGrade(t[this .aktline]) > depth) {
137: parse(t, depth + 1, sb);
138: } else {
139: sb.append(t[this .aktline].substring(depth + 1));
140: }
141:
142: if (this .listElement != null)
143: sb.append("</").append(this .listElement).append(">");
144: sb.append("\n");
145: this .aktline++;
146: }
147: if (this .recursion)
148: for (int j = 0; j < depth; j++)
149: sb.append("\t");
150: if (this .listBlockElement != null)
151: sb.append("</").append(this .listBlockElement).append(">");
152: this .aktline--;
153: return sb;
154: }
155:
156: protected int getGrade(String t) {
157: int i = 0;
158: for (i = 0; i < t.length(); i++)
159: if (t.charAt(i) != this .firstChar)
160: break;
161: return i - 1;
162: }
163:
164: public String[] getBlockElementNames() {
165: return blockElements;
166: }
167:
168: public Pattern[] getRegex() {
169: return this .pattern;
170: }
171:
172: public char getFirstChar() {
173: return this .firstChar;
174: }
175:
176: public boolean setText(String text, int patternNr) {
177: this .text = text;
178: this .markup = null;
179: this .parsed = false;
180: this .aktline = 0;
181: return true;
182: }
183: }
|