Kawai Wiki

Compare Revisions

Ignore whitespace Rev 92 → Rev 93

/trunk/src/main/java/org/kawai/tag/KawaiTagInterpreter.java
File deleted
\ No newline at end of file
/trunk/src/main/java/org/kawai/tag/kcode/ContextPath.java
New file
0,0 → 1,27
package org.kawai.tag.kcode;
 
import java.util.Map;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
 
public class ContextPath implements KCode {
@Override
public String getTag() {
return "contextPath";
}
@Override
public boolean hasBody() {
return false;
}
@Override
public String process(PageContext pageContext, Map<String, String> attributes, String body) throws JspException {
if (body != null) throw new JspException("contextPath tag cannot have a body!");
HttpServletRequest req = (HttpServletRequest) pageContext.getRequest();
return req.getContextPath();
}
}
/trunk/src/main/java/org/kawai/tag/kcode/KCodeHandler.java
New file
0,0 → 1,182
package org.kawai.tag.kcode;
 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
import javax.servlet.jsp.JspException;
 
import org.kawai.model.Page;
import org.mentawai.tag.util.PrintTag;
 
public class KCodeHandler extends PrintTag {
private static final Map<String, String> EMPTY_MAP = new HashMap<String, String>();
private static final List<KCode> codes = new ArrayList<KCode>();
public static void add(KCode code) {
codes.add(code);
}
static {
add(new ContextPath());
add(new Code());
}
private String name;
public void setName(String name) {
this.name = name;
}
@Override
public String getStringToPrint() throws JspException {
Page page = (Page) pageContext.findAttribute(name);
if (page == null) {
throw new JspException("Cannot find page with name: " + name);
}
String body = page.getBody();
for(KCode code : codes) {
String tag = code.getTag();
while(checkForTag(tag, body)) {
String[] parts = splitCodeBlock(tag, body, code.hasBody());
if (parts == null) {
break;
}
Map<String, String> attributes = parseAttributes(parts[0]);
StringBuilder sb = new StringBuilder(1024 * 10);
sb.append(parts[1]);
sb.append(code.process(pageContext, attributes, parts[2]));
sb.append(parts[3]);
body = sb.toString();
System.out.println("\n{\n" + body + "\n}\n");
}
}
return body;
}
private static final boolean checkForTag(String tag, String body) {
int start = body.indexOf("[" + tag);
if (start == -1) return false;
int end = body.indexOf(']', start);
return end != -1;
}
private static String[] splitCodeBlock(String tag, String body, boolean hasBody) {
if (hasBody) {
int start = body.indexOf("[" + tag);
if (start == -1) return null;
int closingBracketIndex = body.indexOf("]", start);
if (closingBracketIndex == -1) return null;
int end = body.indexOf("[/" + tag + "]");
if (end > start) {
// body tag...
String part1 = "";
if (start > 0) {
part1 = body.substring(0, start);
}
String part2 = "";
int len = ("[/" + tag + "]").length();
if (end < body.length() - len - 1) {
part2 = body.substring(end + len, body.length());
}
String contents = "";
int contentsStart = body.indexOf("]", start) + 1;
int contentsEnd = end;
if (contentsEnd > contentsStart) {
contents = body.substring(contentsStart, contentsEnd);
}
String tagAttributes = "";
len = ("[" + tag).length();
if (closingBracketIndex > start + len) {
tagAttributes = body.substring(start + len, closingBracketIndex).trim();
}
return new String[] { tagAttributes, part1, contents, part2 };
}
return null;
} else {
int start = body.indexOf("[" + tag);
if (start == -1) return null;
int closingBracketIndex = body.indexOf("/]", start);
if (closingBracketIndex == -1) return null;
int end = closingBracketIndex + 2;
if (end > start) {
String part1 = "";
if (start > 0) {
part1 = body.substring(0, start);
}
String part2 = "";
if (end < body.length()) {
part2 = body.substring(end, body.length());
}
String contents = null;
String tagAttributes = "";
int len = ("[" + tag).length();
if (closingBracketIndex > start + len) {
tagAttributes = body.substring(start + len, closingBracketIndex).trim();
}
return new String[] { tagAttributes, part1, contents, part2 };
}
return null;
}
}
private static Map<String, String> parseAttributes(String attrs) {
if (attrs.trim().equals("")) return EMPTY_MAP;
Map<String, String> map = new HashMap<String, String>();
String[] temp = attrs.split(" +");
for(String s : temp) {
String[] temp2 = s.split("\\=");
if (temp2[1].startsWith("\"") && temp2[1].endsWith("\"")) {
map.put(temp2[0], temp2[1].substring(1, temp2[1].length() - 1));
} else {
map.put(temp2[0], temp2[1]);
}
}
return map;
}
public static void main(String[] args) {
String x1 = "house";
String x2 = "home";
String s = "aaaaaa [code a=\"asfadf\" bbb=\"asfasfffff\"]" + x1 + "[/code] bbbbb [code ]" + x2 + "[/code] cccccccccccc";
String[] blocks = splitCodeBlock("code", s, true);
for(String x : blocks) System.out.println("{" + x + "}");
blocks = splitCodeBlock("code", blocks[3], true);
for(String x : blocks) System.out.println("{" + x + "}");
s = "aaaaaa [code flip=\"1\" flop=\"2\" /]";
blocks = splitCodeBlock("code", s, false);
for(String x : blocks) System.out.println("{" + x + "}");
 
s = "aaaaaa [code /]";
blocks = splitCodeBlock("code", s, false);
for(String x : blocks) System.out.println("{" + x + "}");
 
}
}
/trunk/src/main/java/org/kawai/tag/kcode/KCode.java
New file
0,0 → 1,15
package org.kawai.tag.kcode;
 
import java.util.Map;
 
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
 
public interface KCode {
public String getTag();
public boolean hasBody();
public String process(PageContext pageContext, Map<String, String> attributes, String body) throws JspException;
}
/trunk/src/main/java/org/kawai/tag/kcode/Code.java
New file
0,0 → 1,62
package org.kawai.tag.kcode;
 
import java.util.Iterator;
import java.util.Map;
 
import javax.servlet.jsp.PageContext;
 
public class Code implements KCode {
@Override
public boolean hasBody() {
return true;
}
@Override
public String getTag() {
return "code";
}
@Override
public String process(PageContext context, Map<String, String> attrs, String html) {
StringBuilder sb = new StringBuilder(1024 * 10);
boolean indent = true;
if (attrs.containsKey("ul")) {
indent = attrs.get("ul").equals("true");
}
 
if (indent) sb.append("<ul>");
sb.append(buildOpenShTag(attrs));
sb.append(html);
sb.append("</pre>");
if (indent) sb.append("</ul>");
return sb.toString();
}
private static String buildOpenShTag(Map<String, String> map) {
StringBuilder sb = new StringBuilder(256);
sb.append("<pre class=\"");
if (map.containsKey("brush")) {
sb.append("brush: ").append(map.get("brush")).append(";");
} else {
sb.append("brush: java;");
}
map.remove("brush");
if (map.containsKey("highlight")) {
sb.append(" highlight: [").append(map.get("highlight")).append("];");
}
map.remove("highlight");
Iterator<String> iter = map.keySet().iterator();
while(iter.hasNext()) {
String name = iter.next();
if (name.equals("ul")) continue;
String value = map.get(name);
sb.append(" ").append(name).append(": ").append(value).append(";");
}
sb.append("\">");
return sb.toString();
}
}