Subversion Repositories Integrator Subversion

Rev

Blame | Last modification | View Log | Download | RSS feed

package br.com.kronus.core;

import java.text.DecimalFormat;
import java.text.Normalizer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;

public class StringUtil {
       
        public static String setarUpperCase(String campo) {
                return VerificadorUtil.naoEstaNulo(campo)? campo.toUpperCase() : campo;
        }
       
        public static String setarLowerCase(String campo) {
                return VerificadorUtil.naoEstaNulo(campo)? campo.toLowerCase() : campo;
        }
       
        public static String setarUpperCaseComTrim(String campo) {
                return VerificadorUtil.naoEstaNulo(campo)? campo.toUpperCase().trim() : campo;
        }
       
        public static String setarValorAhEsquerdaAteCompletarAhQuantidadeDeCaracteres(String string, String valor, Integer quantidadeCaracteres) {
                StringBuilder retorno = new StringBuilder();  
                while (retorno.length() + string.length() < quantidadeCaracteres) {  
                        retorno.append(valor);  
                }  
                retorno.append(string);
                return retorno.toString();  
        }
       
        public static String formatarCpf(String cpfSemFormatacao) {
                String cpfComFormatacao = null;
                Pattern pattern = Pattern.compile("(\\d{3})(\\d{3})(\\d{3})(\\d{2})");
                Matcher matcher = pattern.matcher(cpfSemFormatacao);
                if (matcher.matches()) {
                        cpfComFormatacao = matcher.replaceAll("$1.$2.$3-$4");
                }
                return cpfComFormatacao;       
        }
       
        public static String formatarCnpj(String cnpjSemFormatacao) {
                String cnpjComFormatacao = null;
                Pattern pattern = Pattern.compile("(\\d{2})(\\d{3})(\\d{3})(\\d{4})(\\d{2})");
                Matcher matcher = pattern.matcher(cnpjSemFormatacao);
                if (matcher.matches()) {
//                      cnpjComFormatacao = matcher.replaceAll("$1.$2.$3-$4");
                        cnpjComFormatacao = matcher.replaceAll("$1.$2.$3/$4-$5");
                }
                return cnpjComFormatacao;      
        }
       
        public static String removerAcentos(String str) {
            str = Normalizer.normalize(str, Normalizer.Form.NFD);
            str = str.replaceAll("[^\\p{ASCII}]", "");
            return str;
        }
       
        public static String retornarApenasNumeros(String valor) {
                return VerificadorUtil.naoEstaNulo(valor)? valor.replaceAll("[^0-9]", "") : "";
        }
       
        public static String formatarValor(Double valor) {
                DecimalFormat doisDigitos = new DecimalFormat("###,##0.00");
                return doisDigitos.format(valor);
        }
       
        public static String formatarPercentual(Double valor) {
                DecimalFormat percentual = new DecimalFormat("###,##0%");
                return percentual.format(valor);
        }
       
        public static String lpadTo(String input, int width, char ch) {  
        String strPad = "";
        StringBuffer sb = new StringBuffer(input.trim());
        while (sb.length() < width) sb.insert(0,ch);
        strPad = sb.toString();
        if (strPad.length() > width) {
            strPad = strPad.substring(0,width);
        }
        return strPad;
    }
       
        public static String formatarValorComDoisDigitos(String valor) throws Exception {
                try {
                        return formatarValorComDigitos(valor, 2);
                } catch (Exception e) {
                        e.printStackTrace();
                        throw new Exception(e.getMessage());
                }
        }
       
        public static String formatarValorComDezDigitos(String valor) throws Exception {
                try {
                        return formatarValorComDigitos(valor, 10);
                } catch (Exception e) {
                        e.printStackTrace();
                        throw new Exception(e.getMessage());
                }
        }
       
        public static String formatarValorComDigitos(String valor, Integer digitos) throws Exception {
                try {
                        DecimalFormat df = new DecimalFormat("0.##");
                        if (valor.contains(".") && valor.contains(",")) {
                                valor = valor.replace(".", "");
                        }
                        valor = valor.replace(",", ".");
                        valor = df.format(new Double(valor)).replace(",", ".");
                        if (!valor.contains(".")) {
                                valor = valor + ".00";
                        }
                        String valorFormatar = valor.substring(valor.indexOf(".") + 1);
                        return valor.substring(0, valor.indexOf(".") + 1) + StringUtils.rightPad(valorFormatar, digitos, "0");
                } catch (Exception e) {
                        e.printStackTrace();
                        throw new Exception(e.getMessage());
                }
        }
       
        public static int modulo11(String chave) {
        int total = 0;
        int peso = 2;
        for (int i = 0; i < chave.length(); i++) {
            total += (chave.charAt((chave.length()-1) - i) - '0') * peso;
            peso ++;
            if (peso == 10) {
                peso = 2;
            }
        }
        int resto = total % 11;
        return (resto == 0 || resto == 1) ? 0 : (11 - resto);
        }
       
}