class HTMLUtilities {

	public static String escapeHTML(String text) {

		StringBuilder buffer = new StringBuilder(text.length());
		text.codePoints().forEach(cp -> {
			switch (cp) {
				case '&':
					buffer.append("&amp;");
					break;
				case '<':
					buffer.append("&lt;");
					break;
				case '>':
					buffer.append("&gt;");
					break;
				default:
					if (charNeedsHTMLEscaping(cp)) {
						buffer.append("&#x");
						buffer.append(Integer.toString(cp, 16).toUpperCase());
						buffer.append(";");
					}
					else {
						buffer.appendCodePoint(cp);
					}
					break;
			}
		});

		return buffer.toString();
	}

}