Here you can find the source of quoteSpecialCharacters(final String oldString)
Parameter | Description |
---|---|
oldString | A string that needs its XML special characters quoted. |
public static String quoteSpecialCharacters(final String oldString)
//package com.java2s; /*/*from w ww .j a v a 2s.com*/ * Created on 16/07/2004 * YAWLEditor v1.01 * * @author Lindsay Bradford * * * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library 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 * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ public class Main { /** * Quotes the special XML characters present in oldString so the * return string can be embedded within an XML document without * problems. * @param oldString A string that needs its XML special characters quoted. * @return A string safe for inclusing within XML. */ public static String quoteSpecialCharacters(final String oldString) { // small mod of method from http://www.javapractices.com/Topic96.cjp // TODO: Fix string quoting between editor and engine. // needed now only by initial value special character quoting. final StringBuffer quotedString = new StringBuffer(); if (oldString == null) { return null; } for (int i = 0; i < oldString.length(); i++) { if (oldString.charAt(i) == '<') { quotedString.append("<"); } else if (oldString.charAt(i) == '>') { quotedString.append(">"); } else if (oldString.charAt(i) == '\"') { quotedString.append("""); } else if (oldString.charAt(i) == '\'') { quotedString.append("'"); } else if (oldString.charAt(i) == '\\') { quotedString.append("\"); } else if (oldString.charAt(i) == '&') { quotedString.append("&"); } else { //the char is not a special one //add it to the result as is quotedString.append(oldString.charAt(i)); } } return quotedString.toString(); } }