X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=src%2Fedu%2Fberkeley%2Fsbp%2Futil%2FToHTML.java;h=3d52562e172a4c316efc2557fd2d5541a495f2f8;hb=aaa5d101e054dc548e7ef7831b1fcb7913a4d4d4;hp=4415e3d95b7729141b1ed6fd56ba644fc1c2cdb6;hpb=373f281f7a7988773f6e469717ce6f972cb07e96;p=sbp.git diff --git a/src/edu/berkeley/sbp/util/ToHTML.java b/src/edu/berkeley/sbp/util/ToHTML.java index 4415e3d..3d52562 100644 --- a/src/edu/berkeley/sbp/util/ToHTML.java +++ b/src/edu/berkeley/sbp/util/ToHTML.java @@ -3,5 +3,93 @@ import java.io.*; import java.util.*; public interface ToHTML { - public void ToHTML(PrintWriter p); + public void toHTML(HTML h); + + public static class HTML { + private final StringBuffer sb; + public HTML(StringBuffer sb) { this.sb = sb; } + + public void appendLiterally(String s) { sb.append(s); } + public void appendLiterally(char c) { sb.append(c); } + + public void appendText(String s) { + sb.append(escapify(s)); + } + + public String escapify(String s) { + StringBuffer sb = new StringBuffer(); + for(int i=0; i': sb.append(">"); break; + case '\'': sb.append("'"); break; + case '\"': sb.append("""); break; + default: + if (c < 32 || c >= 127) { + sb.append("&#x" + Integer.toString((int)(c & 0xffff), 16) + ";"); + } else { + sb.append(c); + } + } + } + return sb.toString(); + } + + public void entity(int entity) { appendLiterally("&#"+entity+";"); } + public void entity(String entity) { appendLiterally("&"+entity+";"); } + + public void append(Object o) { + if (o==null) appendLiterally("null"); + else if (o instanceof ToHTML) ((ToHTML)o).toHTML(this); + else if (o instanceof Object[]) append((Object[])o); + else appendText(o.toString()); + } + public void append(int i) { sb.append(i); } + public void append(char c) { append(""+c); } + + public void append(Object[] o) { + for(int i=0; i0) append(' '); + append(o[i]); + } + } + + public void tag(String s) { + appendLiterally("<"); + appendLiterally(s); + appendLiterally("/>"); + } + public void openTag(String s) { openTag(s, null); } + public void openTag(String s, Object[] attrs) { + appendLiterally("<"); + appendLiterally(s); + if (attrs != null) + for(int i=0; i"); + } + public void closeTag(String s) { + appendLiterally(""); + } + public void tag(String s, Object o) { tag(s, null, o); } + public void tag(String s, Object[] attrs, Object o) { + if (s != null) openTag(s, attrs); + append(o); + if (s != null) { + appendLiterally(""); + } + } + + } }