checkpoint
[sbp.git] / src / edu / berkeley / sbp / util / ToHTML.java
index 4415e3d..88e4f57 100644 (file)
@@ -3,5 +3,77 @@ import java.io.*;
 import java.util.*;
 
 public interface ToHTML {
 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 append(String s) {
+            /* FIXME */
+            sb.append(s);
+        }
+
+        public void appendText(String s) {
+            /* FIXME: escapify this!!! */
+            sb.append(s);
+        }
+
+        public void entity(int entity) { sb.append("&#"+entity+";"); }
+        public void entity(String entity) { sb.append("&"+entity+";"); }
+
+        public void append(Object o) {
+            if (o==null)                    append("<tt><font color=red>null</font></tt>");
+            else if (o instanceof ToHTML)   ((ToHTML)o).toHTML(this);
+            else if (o instanceof Object[]) append((Object[])o);
+            else                            append(o.toString());
+        }
+        public void append(int i) { sb.append(i); }
+        public void append(char c) { sb.append(c); }
+
+        public void append(Object[] o) {
+            for(int i=0; i<o.length; i++) {
+                if (i>0) append(' ');
+                append(o[i]);
+            }
+        }
+
+        public void tag(String s) {
+            sb.append("<");
+            append(s);
+            sb.append("/>");
+        }
+        public void openTag(String s, Object[] attrs) {
+                sb.append("<");
+                append(s);
+                if (attrs != null)
+                    for(int i=0; i<attrs.length; i+=2) {
+                        sb.append(' ');
+                        append(attrs[i]);
+                        sb.append("=\'");
+                        append(attrs[i+1]);
+                        sb.append("\'");
+                    }
+                sb.append(">");
+        }
+        public void closeTag(String s) {
+            sb.append("<");
+            append(s);
+            sb.append(">");
+        }
+        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) {
+                sb.append("</");
+                append(s);
+                sb.append(">");
+            }
+        }
+
+    }
 }
 }