Java tutorial
//package com.java2s; /******************************************************************************* * Copyright (c) 2010 Freemarker Team. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * Angelo Zerr <angelo.zerr@gmail.com> - initial API and implementation *******************************************************************************/ public class Main { /** * Escape the String value to returns attribute valid value. * * @param s * @return */ private static String getEscaped(String s) { StringBuffer result = new StringBuffer(s.length() + 10); for (int i = 0; i < s.length(); ++i) appendEscapedChar(result, s.charAt(i)); return result.toString(); } private static void appendEscapedChar(StringBuffer buffer, char c) { String replacement = getReplacement(c); if (replacement != null) { buffer.append('&'); buffer.append(replacement); buffer.append(';'); } else { buffer.append(c); } } private static String getReplacement(char c) { // Encode special XML characters into the equivalent character // references. // These five are defined by default for all XML documents. switch (c) { case '<': return "lt"; //$NON-NLS-1$ case '>': return "gt"; //$NON-NLS-1$ case '"': return "quot"; //$NON-NLS-1$ case '\'': return "apos"; //$NON-NLS-1$ case '&': return "amp"; //$NON-NLS-1$ } return null; } }