1 package edu.berkeley.sbp.util;
5 public interface ToHTML {
6 public void toHTML(HTML h);
8 public static class HTML {
9 private final StringBuffer sb;
10 public HTML(StringBuffer sb) { this.sb = sb; }
12 public void appendLiterally(String s) { sb.append(s); }
13 public void appendLiterally(char c) { sb.append(c); }
15 public void appendText(String s) {
16 sb.append(escapify(s));
19 public String escapify(String s) {
20 StringBuffer sb = new StringBuffer();
21 for(int i=0; i<s.length(); i++) {
24 case '&': sb.append("&"); break;
25 case '<': sb.append("<"); break;
26 case '>': sb.append(">"); break;
27 case '\'': sb.append("'"); break;
28 case '\"': sb.append("""); break;
30 if (c < 32 || c >= 127) {
31 sb.append("&#x" + Integer.toString((int)(c & 0xffff), 16) + ";");
40 public void entity(int entity) { appendLiterally("&#"+entity+";"); }
41 public void entity(String entity) { appendLiterally("&"+entity+";"); }
43 public void append(Object o) {
44 if (o==null) appendLiterally("<tt><font color=red>null</font></tt>");
45 else if (o instanceof ToHTML) ((ToHTML)o).toHTML(this);
46 else if (o instanceof Object[]) append((Object[])o);
47 else appendText(o.toString());
49 public void append(int i) { sb.append(i); }
50 public void append(char c) { append(""+c); }
52 public void append(Object[] o) {
53 for(int i=0; i<o.length; i++) {
59 public void tag(String s) {
62 appendLiterally("/>");
64 public void openTag(String s) { openTag(s, null); }
65 public void openTag(String s, Object[] attrs) {
69 for(int i=0; i<attrs.length; i+=2) {
72 appendLiterally("=\'");
74 appendLiterally("\'");
78 public void closeTag(String s) {
79 appendLiterally("</");
83 public void tag(String s, Object o) { tag(s, null, o); }
84 public void tag(String s, Object[] attrs, Object o) {
85 if (s != null) openTag(s, attrs);
88 appendLiterally("</");