mirror of
https://github.com/yacy/yacy_search_server.git
synced 2024-09-21 00:00:13 +02:00
e97b6f0458
git-svn-id: https://svn.berlios.de/svnroot/repos/yacy/trunk@3386 6c8d7289-2bf4-0310-a012-ef5d649a1542
126 lines
3.3 KiB
Java
126 lines
3.3 KiB
Java
|
|
package de.anomic.data.wiki.tokens;
|
|
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.regex.MatchResult;
|
|
import java.util.regex.Matcher;
|
|
import java.util.regex.Pattern;
|
|
|
|
public class SimpleToken extends AbstractToken {
|
|
|
|
protected String content = null;
|
|
protected int grade = 0;
|
|
|
|
protected final Pattern[] pattern;
|
|
protected MatchResult mresult = null;
|
|
private final String[][] definitionList;
|
|
private final String[] blockElements;
|
|
|
|
public SimpleToken(char firstChar, char lastChar, String[][] definitionList, boolean isBlockElements) {
|
|
this.definitionList = definitionList;
|
|
int i;
|
|
if (isBlockElements) {
|
|
ArrayList r = new ArrayList();
|
|
int j;
|
|
for (i=0; i<definitionList.length; i++)
|
|
if (definitionList[i] != null)
|
|
for (j=0; j<definitionList[i].length; j++)
|
|
r.add(definitionList[i][j]);
|
|
this.blockElements = (String[])r.toArray(new String[r.size()]);
|
|
} else {
|
|
this.blockElements = null;
|
|
}
|
|
|
|
for (i=0; i<definitionList.length; i++)
|
|
if (definitionList[i] != null) {
|
|
i++;
|
|
break;
|
|
}
|
|
this.pattern = new Pattern[] { Pattern.compile(String.format(
|
|
"([\\%s]{%d,%d})(.*?)([\\%s]{%d,%d})",
|
|
new Object[] {
|
|
new Character(firstChar),
|
|
new Integer(i),
|
|
new Integer(definitionList.length),
|
|
new Character(lastChar),
|
|
new Integer(i),
|
|
new Integer(definitionList.length)
|
|
} ))};
|
|
}
|
|
|
|
public String getMarkup() {
|
|
if (this.content == null) {
|
|
if (this.text == null) {
|
|
throw new IllegalArgumentException();
|
|
} else {
|
|
setText(this.text, 0);
|
|
}
|
|
}
|
|
if (!this.parsed && !parse()) return this.text;
|
|
return this.markup;
|
|
}
|
|
|
|
protected boolean parse() {
|
|
String[] e;
|
|
if ((e = definitionList[this.grade]) == null || definitionList.length <= this.grade) {
|
|
System.err.println("token not defined for grade: " + this.grade);
|
|
return false;
|
|
}
|
|
this.markup = getMarkup(e);
|
|
this.parsed = true;
|
|
return true;
|
|
}
|
|
|
|
protected String getMarkup(String[] es) {
|
|
return getMarkup(es, false) + this.content + getMarkup(es, true);
|
|
}
|
|
|
|
protected String getMarkup(String[] es, boolean closing) {
|
|
StringBuffer result = new StringBuffer();
|
|
// backwards if closing
|
|
for (
|
|
int i = (closing) ? es.length - 1 : 0, j;
|
|
(closing && i >= 0) ^ (!closing && i < es.length);
|
|
i += (closing) ? -1 : +1
|
|
) {
|
|
result.append("<");
|
|
if (closing) {
|
|
result.append("/");
|
|
if ((j = es[i].indexOf(' ')) > -1) {
|
|
result.append(es[i].substring(0, j));
|
|
} else {
|
|
result.append(es[i]);
|
|
}
|
|
} else {
|
|
result.append(es[i]);
|
|
}
|
|
result.append(">");
|
|
}
|
|
return new String(result);
|
|
}
|
|
|
|
public boolean setText(String text, int patternNr) {
|
|
this.text = text;
|
|
this.markup = null;
|
|
this.parsed = false;
|
|
if (text != null) {
|
|
Matcher m = getRegex()[0].matcher(text);
|
|
if (
|
|
(m.matches()) &&
|
|
(m.group(1).length() == m.group(3).length()) &&
|
|
(definitionList.length >= m.group(1).length()) &&
|
|
(definitionList[m.group(1).length() - 1] != null)
|
|
) {
|
|
this.grade = m.group(1).length() - 1;
|
|
this.content = m.group(2);
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public Pattern[] getRegex() { return this.pattern; }
|
|
public String[] getBlockElementNames() { return this.blockElements; }
|
|
}
|