Java XML Encode xmlEncode(String text)

Here you can find the source of xmlEncode(String text)

Description

xml Encode

License

Open Source License

Declaration

public static String xmlEncode(String text) 

Method Source Code

//package com.java2s;
/*/* w ww .  j a v a  2s.  c o m*/
 * JasperReports - Free Java Reporting Library.
 * Copyright (C) 2001 - 2014 TIBCO Software Inc. All rights reserved.
 * http://www.jaspersoft.com
 *
 * Unless you have purchased a commercial license agreement from Jaspersoft,
 * the following license terms apply:
 *
 * This program is part of JasperReports.
 *
 * JasperReports 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.
 *
 * JasperReports 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 JasperReports. If not, see <http://www.gnu.org/licenses/>.
 */

public class Main {
    /**
     *
     */
    public static String xmlEncode(String text) {
        return xmlEncode(text, null);
    }

    /**
     *
     */
    public static String xmlEncode(String text,
            String invalidCharReplacement) {
        if (text == null || text.length() == 0) {
            return text;
        }

        int length = text.length();
        StringBuffer ret = new StringBuffer(length * 12 / 10);
        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') {
                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);
                }
            } else {
                switch (c) {
                case '&':
                    last = appendText(text, ret, i, last);
                    ret.append("&amp;");
                    break;
                case '>':
                    last = appendText(text, ret, i, last);
                    ret.append("&gt;");
                    break;
                case '<':
                    last = appendText(text, ret, i, last);
                    ret.append("&lt;");
                    break;
                case '\"':
                    last = appendText(text, ret, i, last);
                    ret.append("&quot;");
                    break;
                case '\'':
                    last = appendText(text, ret, i, last);
                    ret.append("&apos;");
                    break;
                default:
                    break;
                }
            }
        }
        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;
    }
}

Related

  1. xmlEncode(String s)
  2. xmlEncode(String s)
  3. xmlEncode(String str)
  4. xmlEncode(String str)
  5. xmlEncode(String string)
  6. xmlEncode(String text)
  7. xmlEncode(String text)
  8. xmlEncode(String text, String invalidCharReplacement)
  9. XMLEncode(String value)