Here you can find the source of htmlEscape(String tag)
private static String htmlEscape(String tag)
//package com.java2s; /*// w w w . j a v a 2 s .c om * Title : AlchemiXmlUtil.java * Package : org.gridbus.alchemi.client.util * Project : AlchemiJavaAPI * Description : * Created on : 4/08/2005 * Author : Krishna Nadiminti (kna@cs.mu.oz.au) * Copyright : (c) 2005, Grid Computing and Distributed Systems Laboratory, * Dept. of Computer Science and Software Engineering, * University of Melbourne, Australia. * * This program is free software; you can redistribute it and/or modify it under * the terms of the GNU General Public License as published by the Free Software * Foundation; either version 2 of the License, or (at your option) any later version. * See the GNU General Public License (http://www.gnu.org/copyleft/gpl.html)for more details. * */ public class Main { /** * Replace characters having special meaning <em>inside</em> HTML tags * with their escaped equivalents, using character entities such as <tt>'&'</tt>. * * <P>The escaped characters are : * <ul> * <li> < * <li> > * <li> " * <li> ' * <li> \ * <li> & * </ul> * * <P>This method ensures that arbitrary text appearing inside a tag does not "confuse" * the tag. For example, <tt>HREF='Blah.do?Page=1&Sort=ASC'</tt> * does not comply with strict HTML because of the ampersand, and should be changed to * <tt>HREF='Blah.do?Page=1&Sort=ASC'</tt>. This is commonly seen in building * query strings. (In JSTL, the c:url tag performs this task automatically.) */ //adapted from: http://www.javapractices.com/Topic96.cjp private static String htmlEscape(String tag) { final StringBuffer result = new StringBuffer(); if (tag == null) { return null; } else { for (int i = 0; i < tag.length(); i++) { char character = tag.charAt(i); if (character == '<') { result.append("<"); } else if (character == '>') { result.append(">"); } else if (character == '\"') { result.append("""); } else if (character == '\'') { result.append("'"); } else if (character == '\\') { result.append("\"); } else if (character == '&') { result.append("&"); } else { //the char is not a special one //add it to the result as is result.append(character); } } //return Base64.encodeBytes(result.toString().getBytes()); return result.toString(); } } }