Java tutorial
//package com.java2s; /* * Qizx/open 4.1 * * This code is the open-source version of Qizx. * Copyright (C) 2004-2009 Axyana Software -- All rights reserved. * * The contents of this file are subject to the Mozilla Public License * Version 1.1 (the "License"); you may not use this file except in * compliance with the License. You may obtain a copy of the License at * http://www.mozilla.org/MPL/ * * Software distributed under the License is distributed on an "AS IS" basis, * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License * for the specific language governing rights and limitations under the * License. * * The Initial Developer of the Original Code is Xavier Franc - Axyana Software. * */ public class Main { /** * Escapes specified string (that is, <tt>'<'</tt> is replaced by "<tt>&#60</tt>;", * <tt>'&'</tt> is replaced by "<tt>&#38;</tt>", etc). * * @param string string to be escaped * @return escaped string */ public static final String escapeXML(String string) { StringBuffer escaped = new StringBuffer(); escapeXML(string, escaped); return escaped.toString(); } /** * Escapes specified string (that is, <tt>'<'</tt> is replaced by "<tt>&#60</tt>;", * <tt>'&'</tt> is replaced by "<tt>&#38;</tt>", etc). * * @param string string to be escaped * @param escaped buffer used to store escaped string (characters are * appended to this buffer) */ public static final void escapeXML(String string, StringBuffer escaped) { char[] chars = string.toCharArray(); escapeXML(chars, 0, chars.length, escaped); } /** * Escapes specified character array (that is, <tt>'<'</tt> is * replaced by "<tt>&#60</tt>;", <tt>'&'</tt> is replaced by "<tt>&#38;</tt>", * etc). * * @param chars character array to be escaped * @param offset specifies first character in array to be escaped * @param length number of characters in array to be escaped * @param escaped buffer used to store escaped string (characters are * appended to this buffer) */ public static final void escapeXML(char[] chars, int offset, int length, StringBuffer escaped) { escapeXML(chars, offset, length, escaped, false); } /** * Escapes specified character array (that is, <tt>'<'</tt> is * replaced by "<tt>&#60</tt>;", <tt>'&'</tt> is replaced by "<tt>&#38;</tt>", * etc). * * @param chars character array to be escaped * @param offset specifies first character in array to be escaped * @param length number of characters in array to be escaped * @param escaped buffer used to store escaped string (characters are * appended to this buffer) * @param ascii if true, characters with code > 127 are escaped as * <tt>&#<i>code</i>;</tt> */ public static final void escapeXML(char[] chars, int offset, int length, StringBuffer escaped, boolean ascii) { int end = offset + length; for (int i = offset; i < end; ++i) { char c = chars[i]; switch (c) { case '\'': escaped.append("'"); break; case '\"': escaped.append("""); break; case '<': escaped.append("<"); break; case '>': escaped.append(">"); break; case '&': escaped.append("&"); break; case 0x00A0: // escaped.append(" "); break; default: if (ascii && c > 127) { escaped.append("&#"); escaped.append(Integer.toString((int) c)); escaped.append(';'); } else { escaped.append(c); } } } } }