checkpoint
[sbp.git] / src / edu / berkeley / sbp / util / ToHTML.java
1 package edu.berkeley.sbp.util;
2 import java.io.*;
3 import java.util.*;
4
5 public interface ToHTML {
6     public void toHTML(HTML h);
7
8     public static class HTML {
9         private final StringBuffer sb;
10         public HTML(StringBuffer sb) { this.sb = sb; }
11
12         public void appendLiterally(String s) { sb.append(s); }
13         public void appendLiterally(char c) { sb.append(c); }
14
15         public void appendText(String s) {
16             sb.append(escapify(s));
17         }
18
19         public String escapify(String s) {
20             StringBuffer sb = new StringBuffer();
21             for(int i=0; i<s.length(); i++) {
22                 char c = s.charAt(i);
23                 switch(c) {
24                     case '&':  sb.append("&amp;"); break;
25                     case '<':  sb.append("&lt;"); break;
26                     case '>':  sb.append("&gt;"); break;
27                     case '\'': sb.append("&apos;"); break;
28                     case '\"': sb.append("&quot;"); break;
29                     default:
30                         if (c < 32 || c >= 127) {
31                             sb.append("&#x" + Integer.toString((int)(c & 0xffff), 16) + ";");
32                         } else {
33                             sb.append(c);
34                         }
35                 }
36             }
37             return sb.toString();
38         }
39
40         public void entity(int entity) { appendLiterally("&#"+entity+";"); }
41         public void entity(String entity) { appendLiterally("&"+entity+";"); }
42
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());
48         }
49         public void append(int i) { sb.append(i); }
50         public void append(char c) { append(""+c); }
51
52         public void append(Object[] o) {
53             for(int i=0; i<o.length; i++) {
54                 if (i>0) append(' ');
55                 append(o[i]);
56             }
57         }
58
59         public void tag(String s) {
60             appendLiterally("<");
61             appendLiterally(s);
62             appendLiterally("/>");
63         }
64         public void openTag(String s) { openTag(s, null); }
65         public void openTag(String s, Object[] attrs) {
66             appendLiterally("<");
67             appendLiterally(s);
68             if (attrs != null)
69                 for(int i=0; i<attrs.length; i+=2) {
70                     appendLiterally(' ');
71                     append(attrs[i]);
72                     appendLiterally("=\'");
73                     append(attrs[i+1]);
74                     appendLiterally("\'");
75                 }
76             appendLiterally(">");
77         }
78         public void closeTag(String s) {
79             appendLiterally("</");
80             appendLiterally(s);
81             appendLiterally(">");
82         }
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);
86             append(o);
87             if (s != null) {
88                 appendLiterally("</");
89                 appendLiterally(s);
90                 appendLiterally(">");
91             }
92         }
93
94     }
95 }