Java Unicode Unescape unescapeHTMLUnicodeEntity(final String text)

Here you can find the source of unescapeHTMLUnicodeEntity(final String text)

Description

unescape HTML Unicode Entity

License

Open Source License

Declaration

public static String unescapeHTMLUnicodeEntity(final String text) 

Method Source Code

//package com.java2s;
/*/*  w  w w .  j  a  v a  2s . c  o  m*/
 *  Freeplane - mind map editor
 *  Copyright (C) 2008 Joerg Mueller, Daniel Polansky, Christian Foltin, Dimitry Polivaev
 *
 *  This file is modified by Dimitry Polivaev in 2008.
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program 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 General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

public class Main {
    public static String unescapeHTMLUnicodeEntity(final String text) {
        final StringBuilder resultBuilder = new StringBuilder(text.length());
        final StringBuilder entity = new StringBuilder();
        boolean readingEntity = false;
        char myChar;
        for (int i = 0; i < text.length(); ++i) {
            myChar = text.charAt(i);
            if (readingEntity) {
                if (myChar == ';') {
                    if (entity.charAt(0) == '#') {
                        try {
                            final char c;
                            if (entity.charAt(1) == 'x') {
                                c = (char) Integer.parseInt(entity.substring(2), 16);
                            } else {
                                c = (char) Integer.parseInt(entity.substring(1), 10);
                            }
                            if (c >= ' ' || c == '\t' || c == '\r' || c == '\n') {
                                resultBuilder.append(c);
                            } else {
                                resultBuilder.append(' ');
                            }
                        } catch (final NumberFormatException e) {
                            resultBuilder.append('&').append(entity).append(';');
                        }
                    } else {
                        resultBuilder.append('&').append(entity).append(';');
                    }
                    entity.setLength(0);
                    readingEntity = false;
                } else {
                    entity.append(myChar);
                }
            } else {
                if (myChar == '&') {
                    readingEntity = true;
                } else {
                    resultBuilder.append(myChar);
                }
            }
        }
        if (entity.length() > 0) {
            resultBuilder.append('&').append(entity);
        }
        final String result = resultBuilder.toString();
        return result;
    }
}

Related

  1. unescapeUnicode(String encoded)
  2. unescapeUnicode(String s)
  3. unescapeUnicode(String s)
  4. unescapeUnicodeChars(String string)