Here you can find the source of toXMLCData(String cdata)
public static String toXMLCData(String cdata)
//package com.java2s; /**//from w w w . j a v a 2 s .c o m * Copyright 2004-2014 Riccardo Solmi. All rights reserved. * This file is part of the Whole Platform. * * The Whole Platform 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 3 of the License, or * (at your option) any later version. * * The Whole Platform 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 the Whole Platform. If not, see <http://www.gnu.org/licenses/>. */ public class Main { public static byte[] cdataFilter; public static String toXMLCData(String cdata) { char[] charArray = cdata.toCharArray(); StringBuilder buffer = new StringBuilder(); for (int i = 0; i < charArray.length; i++) { final char ch = charArray[i]; switch (cdataFilter(ch)) { case 0: buffer.append(ch); break; case 1: break; case 2: if (charArray[i + 1] == ']' && charArray[i + 2] == '>') { buffer.append("]]]]><![CDATA[>"); i += 2; } else buffer.append(ch); break; case 3: if (Character.isSurrogatePair(ch, charArray[i + 1]) && Character.isSupplementaryCodePoint(Character.toCodePoint(ch, charArray[i + 1]))) { buffer.append(ch); buffer.append(charArray[++i]); } } } return buffer.toString(); } public static byte cdataFilter(char ch) { if (cdataFilter == null) { cdataFilter = new byte[65536]; for (int i = 0; i < cdataFilter.length; i++) { if (i < 0x0020) switch (i) { case '\t': case '\n': case '\r': cdataFilter[i] = 0; break; default: cdataFilter[i] = 1; break; } else if (i <= 0xd7ff) { if (i == ']') cdataFilter[i] = 2; else cdataFilter[i] = 0; } else if (i < 0xe000) { cdataFilter[i] = 3; } else if (i <= 0xfffd) cdataFilter[i] = 0; else cdataFilter[i] = 1; } } return cdataFilter[ch]; } }