Here you can find the source of quoteCharacterData(String s)
Parameter | Description |
---|---|
s | character data to be quoted. |
public static String quoteCharacterData(String s)
//package com.java2s; /**************************************************************************** Copyright (c) 2006, Colorado School of Mines and others. All rights reserved. This program and accompanying materials are made available under the terms of the Common Public License - v1.0, which accompanies this distribution, and is available at http://www.eclipse.org/legal/cpl-v10.html ****************************************************************************/ public class Main { /**/*from ww w . ja v a2s .c o m*/ * Quotes character data. * Replaces special characters and XML entities. * Encloses string in double quotes if it contains whitespace. * @param s character data to be quoted. * @return quoted character data. */ public static String quoteCharacterData(String s) { final char space = '\u0020'; if (s == null) return null; s = replaceAll("&", "&", s); s = replaceAll("<", "<", s); s = replaceAll("\\", "\\\\", s); s = replaceAll("\r", "\\r", s); s = replaceAll("\n", "\\n", s); s = replaceAll("\t", "\\t", s); s = replaceAll("\"", "\\\"", s); s = replaceAll("\'", "\\\'", s); String quote = ""; if (s.length() == 0) { quote = "\""; } else { for (int i = 0; i < s.length(); ++i) { if (s.charAt(i) <= space) { quote = "\""; break; } } } return quote + s + quote; } /** * Replaces all occurences in string s of string x with string y. * @param x string to be replaced. * @param y string replacing each occurence of x. * @param s string containing zero or more x to be replaced. * @return string s with all x replaced with y. */ private static String replaceAll(String x, String y, String s) { if (s == null) return null; int from = 0; int to = s.indexOf(x, from); if (to < 0) return s; StringBuilder d = new StringBuilder(s.length() + 32); while (to >= 0) { d.append(s.substring(from, to)); d.append(y); from = to + x.length(); to = s.indexOf(x, from); } return d.append(s.substring(from)).toString(); } }