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