Here you can find the source of xmlEncode(String text, String invalidCharReplacement)
public static String xmlEncode(String text, String invalidCharReplacement)
//package com.java2s; /******************************************************************************* * Copyright (C) 2005 - 2014 TIBCO Software Inc. All rights reserved. * http://www.jaspersoft.com./*from w ww . ja v a2 s .com*/ * * Unless you have purchased a commercial license agreement from Jaspersoft, * the following license terms apply: * * 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 ******************************************************************************/ public class Main { public static String xmlEncode(String text, String invalidCharReplacement) { if (text == null || text.length() == 0) { return text; } int length = text.length(); StringBuffer ret = new StringBuffer(); int last = 0; for (int i = 0; i < length; i++) { char c = text.charAt(i); if (Character.isISOControl(c) && c != '\t' && c != '\r' && c != '\n' && c != 0) { last = appendText(text, ret, i, last); if (invalidCharReplacement == null) { // the invalid character is preserved ret.append(c); } else if ("".equals(invalidCharReplacement)) { // the invalid character is removed continue; } else { // the invalid character is replaced ret.append(invalidCharReplacement); } } } appendText(text, ret, length, last); return ret.toString(); } private static int appendText(String text, StringBuffer ret, int current, int old) { if (old < current) { ret.append(text.substring(old, current)); } return current + 1; } }