package de.anomic.data.wiki.tokens;
import java.util.HashMap;
import java.util.Iterator;
import java.util.regex.Pattern;
public class TableToken extends AbstractToken {
private static final Pattern[] pattern = new Pattern[] {
Pattern.compile(
"\\{\\|" + // "{|"
"([^\n]|\n\\|[|-])*\n" + // new line must start with "||" or "|-"
"\\|\\}") // "|}"
};
private static final String[] blockElementNames = new String[] { "table", "tr", "td" };
@Override
protected boolean parse() {
String[] t = text.split("\n");
String[] tds;
StringBuffer sb = new StringBuffer();
sb.append("
2) sb.append(parseTableProperties(t[0].substring(2)));
sb.append(">\n");
boolean trOpen = false;
for (int i=1, j, a; i\n");
if (trOpen = (i < t.length - 2)) sb.append("\t\n");
} else if (t[i].startsWith("||")) {
tds = t[i].split("\\|\\|");
for (j=0; j (a = tds[j].indexOf('|')) + 1) { // don't print empty td's
sb.append("\t\t -1) sb.append(parseTableProperties(tds[j].substring(0, a)));
sb.append(">").append(tds[j].substring(a + 1)).append(" | \n");
}
}
}
}
if (trOpen) sb.append("\t
\n");
this.markup = new String(sb.append("
"));
this.parsed = true;
return true;
}
// from de.anomic.data.wikiCode.java.parseTableProperties, modified by [FB]
private static final String[] tps = { "rowspan", "colspan", "vspace", "hspace", "cellspacing", "cellpadding", "border" };
private static final HashMap ps = new HashMap();
static {
ps.put("frame", new String[] { "void", "above", "below", "hsides", "lhs", "rhs", "vsides", "box", "border" });
ps.put("rules", new String[] { "none", "groups", "rows", "cols", "all" });
ps.put("valign", new String[] { "top", "middle", "bottom", "baseline" });
ps.put("align", new String[] { "left", "right", "center" });
}
// contributed by [MN]
/** This method takes possible table properties and tests if they are valid.
* Valid in this case means if they are a property for the table, tr or td
* tag as stated in the HTML Pocket Reference by Jennifer Niederst (1st edition)
* The method is important to avoid XSS attacks on the wiki via table properties.
* @param str A string that may contain several table properties and/or junk.
* @return A string that only contains table properties.
*/
private static StringBuffer parseTableProperties(final String properties){
String[] values = properties.replaceAll(""", "").split("[= ]"); //splitting the string at = and blanks
StringBuffer sb = new StringBuffer(properties.length());
Iterator it;
String key, valkey, value;
int numberofvalues = values.length;
main: for (int i=0; i-1; i--)
if (array[i].equals(find)) return true;
return false;
}
public Pattern[] getRegex() { return pattern; }
public String[] getBlockElementNames() { return blockElementNames; }
public boolean setText(String text, int patternNr) {
this.text = text;
this.parsed = false;
this.markup = null;
return true;
}
}