Java tutorial
//package com.java2s; import java.io.IOException; import java.io.Writer; public class Main { /** * Appends str to <code>out</code> while performing HTML attribute escaping. * * @param out * @param str * @throws IOException */ public static void escapeHTMLAttribute(Writer out, String str) throws IOException { if (out == null) { throw new IllegalArgumentException("The Writer must not be null"); } if (str == null) { return; } int sz; sz = str.length(); for (int i = 0; i < sz; i++) { char ch = str.charAt(i); switch (ch) { case '&': out.write("&"); break; case '"': out.write("""); break; case '\t': out.write("	"); break; case '\n': out.write(" "); break; case '\r': out.write(" "); break; case '<': out.write("<"); break; case '>': out.write(">"); break; default: out.write(ch); break; } } } }