Java tutorial
//package com.java2s; /******************************************************************************* * Copyright (c) 2014 Karlsruhe Institute of Technology, Germany * Technical University Darmstadt, Germany * Chalmers University of Technology, Sweden * 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: * Technical University Darmstadt - initial API and implementation and/or initial documentation *******************************************************************************/ public class Main { /** * <p> * Encodes the given text in a way that it contains no XML elements * and can be used for instance as plain text or attribute value. * </p> * <p> * The following signs are replaced: * <pre> * " => "quot; * & => "amp; * ' => "apos; * < => "lt; * > => "gt; * </pre> * </p> * @param text The text to encode. * @return The encoded text. */ public static String encodeText(String text) { if (text != null) { char[] signs = text.toCharArray(); StringBuffer sb = new StringBuffer(); for (char sign : signs) { switch (sign) { case '"': sb.append("""); break; case '&': sb.append("&"); break; case '\'': sb.append("'"); break; case '<': sb.append("<"); break; case '>': sb.append(">"); break; default: sb.append(sign); break; } } return sb.toString(); } else { return null; } } }