Java XML Unescape unescapeXml(String value)

Here you can find the source of unescapeXml(String value)

Description

unescape Xml

License

Open Source License

Declaration

public static String unescapeXml(String value) 

Method Source Code

//package com.java2s;
/**//from www.  j a va  2s  .co m
 * Copyright 2013-2014 Guoqiang Chen, Shanghai, China. All rights reserved.
 *
 * Email: subchen@gmail.com
 * URL: http://subchen.github.io/
 *
 * Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

public class Main {
    public static String unescapeXml(String value) {
        if (value == null || value.length() == 0) {
            return value;
        }
        StringBuilder buf = null;
        int len = value.length();
        int len3 = len - 3;
        for (int i = 0; i < len; i++) {
            char ch = value.charAt(i);
            if (ch == '&' && i < len3) {
                int j = i;
                char ch1 = value.charAt(i + 1);
                switch (ch1) {
                case 'l':
                    if (value.charAt(i + 2) == 't' && value.charAt(i + 3) == ';') {
                        i += 3;
                        if (buf == null) {
                            buf = new StringBuilder(len3);
                            if (j > 0) {
                                buf.append(value.substring(0, j));
                            }
                        }
                        buf.append('<');
                    } else if (buf != null) {
                        buf.append('&');
                    }
                    break;
                case 'g':
                    if (value.charAt(i + 2) == 't' && value.charAt(i + 3) == ';') {
                        i += 3;
                        if (buf == null) {
                            buf = new StringBuilder(len3);
                            if (j > 0) {
                                buf.append(value.substring(0, j));
                            }
                        }
                        buf.append('>');
                    } else if (buf != null) {
                        buf.append('&');
                    }
                    break;
                case 'a':
                    int len4 = len - 4;
                    int len5 = len - 5;
                    if (i < len4 && value.charAt(i + 2) == 'm' && value.charAt(i + 3) == 'p'
                            && value.charAt(i + 4) == ';') {
                        i += 4;
                        if (buf == null) {
                            buf = new StringBuilder(len4);
                            if (j > 0) {
                                buf.append(value.substring(0, j));
                            }
                        }
                        buf.append('&');
                    } else if (i < len5 && value.charAt(i + 2) == 'p' && value.charAt(i + 3) == 'o'
                            && value.charAt(i + 4) == 's' && value.charAt(i + 5) == ';') {
                        i += 5;
                        if (buf == null) {
                            buf = new StringBuilder(len5);
                            if (j > 0) {
                                buf.append(value.substring(0, j));
                            }
                        }
                        buf.append('\'');
                    } else if (buf != null) {
                        buf.append('&');
                    }
                    break;
                case 'q':
                    int len_5 = len - 5;
                    if (i < len_5 && value.charAt(i + 2) == 'u' && value.charAt(i + 3) == 'o'
                            && value.charAt(i + 4) == 't' && value.charAt(i + 5) == ';') {
                        i += 5;
                        if (buf == null) {
                            buf = new StringBuilder(len_5);
                            if (j > 0) {
                                buf.append(value.substring(0, j));
                            }
                        }
                        buf.append('\"');
                    } else if (buf != null) {
                        buf.append('&');
                    }
                    break;
                default:
                    if (buf != null) {
                        buf.append('&');
                    }
                    break;
                }
            } else if (buf != null) {
                buf.append(ch);
            }
        }
        if (buf != null) {
            return buf.toString();
        }
        return value;
    }
}

Related

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