// HTMLEncoder
// only 1 static class that converts things like:
// 3<10 & 4>1 into:
// 3&lt;10 &amp; 4&gt;1

public class HtmlEncoder {
    public static String encode(String intext) {
	if (intext == null) return "";
	StringBuffer out = null;
	char[] orig = null;
	int start = 0, len = intext.length();
	for (int i = 0; i < len; i++){
	    char c = intext.charAt(i);
	    if (c==0||c=='&'||c=='<'||c=='>'||c=='"') {
		if (out == null){
		    orig = intext.toCharArray();
		    out = new StringBuffer(len+10);
		}
		if (i > start)
		    out.append(orig, start, i-start);
		start = i + 1;
		switch (c){
		default: // case 0:
		    continue;
		case '&':
		    out.append("&amp;");
		    break;
		case '<':
		    out.append("&lt;");
		    break;
		case '>':
		    out.append("&gt;");
		    break;
		case '"':
		    out.append("&quot;");
		    break;
		}
		break;
	    }
	}
	if (out == null)
	    return intext;
	out.append(orig, start, len-start);
	return out.toString();
    }
}
