Here you can find the source of forHTMLTag(String aTagFragment)
Parameter | Description |
---|---|
aTagFragment | some HTML to be escaped |
private static String forHTMLTag(String aTagFragment)
//package com.java2s; /*// w w w . j a v a2s. c om * RHQ Management Platform * Copyright (C) 2005-2012 Red Hat, Inc. * All rights reserved. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License, version 2, as * published by the Free Software Foundation, and/or the GNU Lesser * General Public License, version 2.1, also as published by the Free * Software Foundation. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License and the GNU Lesser General Public License * for more details. * * You should have received a copy of the GNU General Public License * and the GNU Lesser General Public License along with this program; * if not, write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ import java.text.CharacterIterator; import java.text.StringCharacterIterator; 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.) * * forHTMLTag is copy-n-pasted from: http://www.javapractices.com/Topic96.cjp * used to be in our util.StringUtil, we should really use jakarta's <code>StringEscapeUtils.escapeHTML()</code> * method, however, at this time we do not want to pull in the entire * Commons Lang API dependency for just one method. * * @param aTagFragment * some HTML to be escaped * @return escaped HTML */ private static String forHTMLTag(String aTagFragment) { final StringBuffer result = new StringBuffer(); final StringCharacterIterator iterator = new StringCharacterIterator( aTagFragment); for (char character = iterator.current(); character != CharacterIterator.DONE; character = iterator .next()) { switch (character) { case '<': result.append("<"); break; case '>': result.append(">"); break; case '\"': result.append("""); break; case '\'': result.append("'"); break; case '\\': result.append("\"); break; case '&': result.append("&"); break; case '|': result.append("|"); break; case ',': result.append(","); break; default: // the char is not a special one add it to the result as is result.append(character); break; } } return result.toString(); } }