1 // Copyright 2006-2007 all rights reserved; see LICENSE file for BSD-style license
3 package edu.berkeley.sbp.util;
7 public interface ToHTML {
8 public void toHTML(HTML h);
10 public static class HTML {
11 private final StringBuffer sb;
12 public HTML(StringBuffer sb) { this.sb = sb; }
14 public void appendLiterally(String s) { sb.append(s); }
15 public void appendLiterally(char c) { sb.append(c); }
17 public void appendText(String s) {
18 sb.append(escapify(s));
21 public String escapify(String s) {
22 StringBuffer sb = new StringBuffer();
23 for(int i=0; i<s.length(); i++) {
26 case '&': sb.append("&"); break;
27 case '<': sb.append("<"); break;
28 case '>': sb.append(">"); break;
29 case '\'': sb.append("'"); break;
30 case '\"': sb.append("""); break;
32 if (c < 32 || c >= 127) {
33 sb.append("&#x" + Integer.toString((int)(c & 0xffff), 16) + ";");
42 public void entity(int entity) { appendLiterally("&#"+entity+";"); }
43 public void entity(String entity) { appendLiterally("&"+entity+";"); }
45 public void append(Object o) {
46 if (o==null) appendLiterally("<tt><font color=red>null</font></tt>");
47 else if (o instanceof ToHTML) ((ToHTML)o).toHTML(this);
48 else if (o instanceof Object[]) append((Object[])o);
49 else appendText(o.toString());
51 public void append(int i) { sb.append(i); }
52 public void append(char c) { append(""+c); }
54 public void append(Object[] o) {
55 for(int i=0; i<o.length; i++) {
61 public void tag(String s) {
64 appendLiterally("/>");
66 public void openTag(String s) { openTag(s, null); }
67 public void openTag(String s, Object[] attrs) {
71 for(int i=0; i<attrs.length; i+=2) {
74 appendLiterally("=\'");
76 appendLiterally("\'");
80 public void closeTag(String s) {
81 appendLiterally("</");
85 public void tag(String s, Object o) { tag(s, null, o); }
86 public void tag(String s, Object[] attrs, Object o) {
87 if (s != null) openTag(s, attrs);
90 appendLiterally("</");