Java XML Encode toXMLCharData(String javaString)

Here you can find the source of toXMLCharData(String javaString)

Description

to XML Char Data

License

Open Source License

Declaration

public static String toXMLCharData(String javaString) 

Method Source Code

//package com.java2s;
/**//from   w  ww . 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 String toXMLCharData(String javaString) {
        char[] charArray = javaString.toCharArray();
        StringBuilder buffer = new StringBuilder();

        for (int i = 0; i < charArray.length; i++) {
            final char ch = charArray[i];

            switch (ch) {
            case ' ':
                buffer.append(' ');
                break;
            case '<':
                buffer.append("&lt;");
                break;
            case '>':
                buffer.append("&gt;");
                break;
            case '\'':
                buffer.append("&apos;");
                break;
            case '"':
                buffer.append("&quot;");
                break;
            case '&':
                buffer.append("&amp;");
                break;
            case '\b':
                buffer.append("&#8;");
                break;
            case '\t':
            case '\n':
                buffer.append(ch);
                break;
            case '\f':
                buffer.append("&#12;");
                break;
            case '\r':
                buffer.append("&#13;");
                break;
            default:
                if (Character.isISOControl(ch) || Character.isWhitespace(ch)) {
                    buffer.append("&#x");
                    buffer.append(charToHex(ch));
                    buffer.append(';');
                } else
                    buffer.append(ch);
                break;
            }
        }
        return buffer.toString();
    }

    public static boolean isWhitespace(CharSequence cs) {
        int count = 0, length = cs.length();
        while (count < length && Character.isWhitespace(cs.charAt(count)))
            count++;
        return count == length;
    }

    public static String charToHex(char c) {
        // Returns hex String representation of char c
        byte hi = (byte) (c >>> 8);
        byte lo = (byte) (c & 0xff);
        return byteToHex(hi) + byteToHex(lo);
    }

    public static String byteToHex(byte b) {
        // Returns hex String representation of byte b
        char hexDigit[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
        char[] array = { hexDigit[(b >> 4) & 0x0f], hexDigit[b & 0x0f] };
        return new String(array);
    }
}

Related

  1. encodeXML(String text)
  2. textToXml(String text)
  3. textToXML(String text)
  4. toXMLCData(String cdata)
  5. toXMLEscapedTextExpandingWhitespace(String text)
  6. unwrapCdata(String s)
  7. unwrapCdata(String sText)
  8. unwrapCDATA(String str)