All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.arch.util.UnicodeUtils Maven / Gradle / Ivy

There is a newer version: 18.12.0
Show newest version
package com.arch.util;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by wagner.araujo on 04/04/16.
 */
public final class UnicodeUtils {

    public static final int TWO = 2;
    public static final int THREE = 3;

    private UnicodeUtils() {

    }

    public static String convertSpecialCharacter(String value) {
        StringBuilder returnValue = new StringBuilder();

        for (char c : value.toCharArray()) {
            returnValue.append(converte(c));
        }

        return returnValue.toString();
    }

    private static String converte(char c) {
        Map mapa = new HashMap<>();

        mapa.put("á", "\u00e1");
        mapa.put("à", "\u00e0");
        mapa.put("â", "\u00e2");
        mapa.put("ã", "\u00e3");
        mapa.put("ä", "\u00e4");
        mapa.put("Á", "\u00c1");
        mapa.put("À", "\u00c0");
        mapa.put("Â", "\u00c2");
        mapa.put("Ã", "\u00c3");
        mapa.put("Ä", "\u00c4");
        mapa.put("é", "\u00e9");
        mapa.put("è", "\u00e8");
        mapa.put("ê", "\u00ea");
        mapa.put("ê", "\u00ea");
        mapa.put("É", "\u00c9");
        mapa.put("È", "\u00c8");
        mapa.put("Ê", "\u00ca");
        mapa.put("Ë", "\u00cb");
        mapa.put("í", "\u00ed");
        mapa.put("ì", "\u00ec");
        mapa.put("î", "\u00ee");
        mapa.put("ï", "\u00ef");
        mapa.put("Í", "\u00cd");
        mapa.put("Ì", "\u00cc");
        mapa.put("Î", "\u00ce");
        mapa.put("Ï", "\u00cf");
        mapa.put("ó", "\u00f3");
        mapa.put("ò", "\u00f2");
        mapa.put("ô", "\u00f4");
        mapa.put("õ", "\u00f5");
        mapa.put("ö", "\u00f6");
        mapa.put("Ó", "\u00d3");
        mapa.put("Ò", "\u00d2");
        mapa.put("Ô", "\u00d4");
        mapa.put("Õ", "\u00d5");
        mapa.put("Ö", "\u00d6");
        mapa.put("ú", "\u00fa");
        mapa.put("ù", "\u00f9");
        mapa.put("û", "\u00fb");
        mapa.put("ü", "\u00fc");
        mapa.put("Ú", "\u00da");
        mapa.put("Ù", "\u00d9");
        mapa.put("Û", "\u00db");
        mapa.put("ç", "\u00e7");
        mapa.put("Ç", "\u00c7");
        mapa.put("ñ", "\u00f1");
        mapa.put("Ñ", "\u00d1");
        mapa.put("&", "\u0026");
        mapa.put("\"", "\u0027");

        String retorno = mapa.get(Character.toString(c));

        if (retorno == null) {
            return Character.toString(c);
        }
        String hexa = Integer.toHexString((int) c);

        String prefix;
        if (hexa.length() == 1) {
            prefix = "\\u000";
        } else if (hexa.length() == TWO) {
            prefix = "\\u00";
        } else if (hexa.length() == THREE) {
            prefix = "\\u0";
        } else {
            prefix = "\\u";
        }

        retorno = prefix + hexa;

        return retorno;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy