optimizations to IntPairMap.java
[sbp.git] / src / edu / berkeley / sbp / util / ToHTML.java
index 88e4f57..67b8761 100644 (file)
@@ -1,3 +1,5 @@
+// Copyright 2006-2007 all rights reserved; see LICENSE file for BSD-style license
+
 package edu.berkeley.sbp.util;
 import java.io.*;
 import java.util.*;
@@ -9,30 +11,45 @@ public interface ToHTML {
         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 appendLiterally(String s) { sb.append(s); }
+        public void appendLiterally(char c) { sb.append(c); }
 
         public void appendText(String s) {
-            /* FIXME: escapify this!!! */
-            sb.append(s);
+            sb.append(escapify(s));
+        }
+
+        public String escapify(String s) {
+            StringBuffer sb = new StringBuffer();
+            for(int i=0; i<s.length(); i++) {
+                char c = s.charAt(i);
+                switch(c) {
+                    case '&':  sb.append("&amp;"); break;
+                    case '<':  sb.append("&lt;"); break;
+                    case '>':  sb.append("&gt;"); break;
+                    case '\'': sb.append("&apos;"); break;
+                    case '\"': sb.append("&quot;"); 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) { sb.append("&#"+entity+";"); }
-        public void entity(String entity) { sb.append("&"+entity+";"); }
+        public void entity(int entity) { appendLiterally("&#"+entity+";"); }
+        public void entity(String entity) { appendLiterally("&"+entity+";"); }
 
         public void append(Object o) {
-            if (o==null)                    append("<tt><font color=red>null</font></tt>");
+            if (o==null)                    appendLiterally("<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());
+            else                            appendText(o.toString());
         }
         public void append(int i) { sb.append(i); }
-        public void append(char c) { sb.append(c); }
+        public void append(char c) { append(""+c); }
 
         public void append(Object[] o) {
             for(int i=0; i<o.length; i++) {
@@ -42,36 +59,37 @@ public interface ToHTML {
         }
 
         public void tag(String s) {
-            sb.append("<");
-            append(s);
-            sb.append("/>");
+            appendLiterally("<");
+            appendLiterally(s);
+            appendLiterally("/>");
         }
+        public void openTag(String s) { openTag(s, null); }
         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(">");
+            appendLiterally("<");
+            appendLiterally(s);
+            if (attrs != null)
+                for(int i=0; i<attrs.length; i+=2) {
+                    appendLiterally(' ');
+                    append(attrs[i]);
+                    appendLiterally("=\'");
+                    append(attrs[i+1]);
+                    appendLiterally("\'");
+                }
+            appendLiterally(">");
         }
         public void closeTag(String s) {
-            sb.append("<");
-            append(s);
-            sb.append(">");
+            appendLiterally("</");
+            appendLiterally(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) {
-                sb.append("</");
-                append(s);
-                sb.append(">");
+                appendLiterally("</");
+                appendLiterally(s);
+                appendLiterally(">");
             }
         }