Java XML Unescape unescapeXML(String text)

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

Description

Unescapes specified string.

License

Open Source License

Parameter

Parameter Description
text string to be unescaped

Return

unescaped string

Declaration

public static final String unescapeXML(String text) 

Method Source Code

//package com.java2s;
/*/*from   w ww .j  a v  a2s  .co m*/
 *    Qizx/open 4.1
 *
 * This code is the open-source version of Qizx.
 * Copyright (C) 2004-2009 Axyana Software -- All rights reserved.
 *
 * The contents of this file are subject to the Mozilla Public License 
 *  Version 1.1 (the "License"); you may not use this file except in 
 *  compliance with the License. You may obtain a copy of the License at
 *  http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 *  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 *  for the specific language governing rights and limitations under the
 *  License.
 *
 * The Initial Developer of the Original Code is Xavier Franc - Axyana Software.
 *
 */

public class Main {
    /**
     * Unescapes specified string. Inverse operation of escapeXML(...).
     * 
     * @param text string to be unescaped
     * @return unescaped string
     */
    public static final String unescapeXML(String text) {
        StringBuffer unescaped = new StringBuffer();
        unescapeXML(text, 0, text.length(), unescaped);
        return unescaped.toString();
    }

    /**
     * Unescapes specified string. Inverse operation of escapeXML().
     * 
     * @param text string to be unescaped
     * @param offset specifies first character in string to be unescaped
     * @param length number of characters in string to be unescaped
     * @param unescaped buffer used to store unescaped string (characters are
     *        appended to this buffer)
     */
    public static final void unescapeXML(String text, int offset, int length, StringBuffer unescaped) {
        int end = offset + length;

        for (int i = offset; i < end; ++i) {
            char c = text.charAt(i);

            if (c == '&') {
                StringBuffer charRef = new StringBuffer();

                ++i;
                while (i < end) {
                    c = text.charAt(i);
                    if (c == ';')
                        break;
                    charRef.append(c);
                    ++i;
                }

                c = parseCharRef(charRef.toString());
            }

            unescaped.append(c);
        }
    }

    private static char parseCharRef(String charRef) {
        if (charRef.length() >= 2 && charRef.charAt(0) == '#') {
            int i;

            try {
                if (charRef.charAt(1) == 'x')
                    i = Integer.parseInt(charRef.substring(2), 16);
                else
                    i = Integer.parseInt(charRef.substring(1));
            } catch (NumberFormatException e) {
                i = -1;
            }

            if (i < 0 || i > Character.MAX_VALUE)
                return '?';
            else
                return (char) i;
        }
        if (charRef.equals("amp")) {
            return '&';
        }
        if (charRef.equals("apos")) {
            return '\'';
        }
        if (charRef.equals("quot")) {
            return '\"';
        }
        if (charRef.equals("lt")) {
            return '<';
        }
        if (charRef.equals("gt")) {
            return '>';
        } else {
            return '?';
        }
    }
}

Related

  1. unescapeXML(String s)
  2. unescapeXml(String src)
  3. unescapeXml(String str)
  4. unescapeXML(String str)
  5. unescapeXml(String str)
  6. unescapeXML(String text)
  7. unescapeXml(String value)
  8. unescapeXml(String value)
  9. unescapeXml(String xml)