Kawai Wiki

Rev

Rev 202 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
93 soliveira 1
package org.kawai.tag.kcode;
2
 
3
import java.util.Iterator;
4
import java.util.Map;
5
 
6
import javax.servlet.jsp.PageContext;
7
 
95 soliveira 8
import org.mentaregex.Regex;
9
 
194 helio.frota 10
/**
11
 * KCode class Code.
12
 *
13
 * @author Sergio Oliveira
14
 *
15
 */
93 soliveira 16
public class Code implements KCode {
17
 
195 helio.frota 18
    /**
19
     * {@inheritDoc}.
194 helio.frota 20
     */
21
    @Override
22
    public boolean hasBody() {
23
        return true;
24
    }
25
 
26
    /**
27
     * {@inheritDoc}
28
     */
29
    @Override
30
    public String getTag() {
31
        return "code";
32
    }
33
 
34
    /**
35
     * {@inheritDoc}
36
     */
37
    @Override
38
    public String process(PageContext context, Map<String, String> attrs, String html) {
39
 
40
        StringBuilder sb = new StringBuilder(1024 * 10);
41
 
222 soliveira 42
        boolean indent = false;
194 helio.frota 43
 
44
        if (attrs.containsKey("ul")) {
45
            indent = attrs.get("ul").equals("true");
46
        }
47
 
48
        if (indent) sb.append("<ul>");
49
        sb.append(buildOpenShTag(attrs));
50
        sb.append(escapeAngleBrackets(html));
51
        sb.append("</pre>");
52
        if (indent) sb.append("</ul>");
202 soliveira 53
 
222 soliveira 54
        boolean noBR = true;
202 soliveira 55
        if (attrs.containsKey("noBR")) {
56
                noBR = attrs.get("noBR").equals("true");
57
        }
58
        if (!noBR) sb.append("<br/>");
194 helio.frota 59
        return sb.toString();
60
    }
61
 
195 helio.frota 62
    /**
63
     * Escapes the angle brackets.
64
     * @param html String
65
     * @return String
66
     */
198 soliveira 67
    public static String escapeAngleBrackets(String html) {
194 helio.frota 68
        String s = Regex.sub(html, "s/\\</\\&lt\\;/g");
69
        s = Regex.sub(s, "s/\\>/\\&gt\\;/g");
70
        return s;
71
    }
72
 
195 helio.frota 73
    /**
74
     * Builds the tag for syntax highlight.
75
     * @param map Map<String, String>
76
     * @return String
77
     */
194 helio.frota 78
    private static String buildOpenShTag(Map<String, String> map) {
79
        StringBuilder sb = new StringBuilder(256);
80
        sb.append("<pre class=\"");
81
        if (map.containsKey("brush")) {
82
            sb.append("brush: ").append(map.get("brush")).append(";");
83
        } else {
84
            sb.append("brush: java;");
85
        }
86
        map.remove("brush");
87
        if (map.containsKey("highlight")) {
88
            sb.append(" highlight: [").append(map.get("highlight")).append("];");
89
        }
90
        map.remove("highlight");
91
        Iterator<String> iter = map.keySet().iterator();
195 helio.frota 92
        while (iter.hasNext()) {
194 helio.frota 93
            String name = iter.next();
94
            if (name.equals("ul")) continue;
95
            String value = map.get(name);
96
            sb.append(" ").append(name).append(": ").append(value).append(";");
97
        }
98
        sb.append("\">");
99
        return sb.toString();
100
    }
101
}