br.com.manish.ahy.kernel.util.TextUtil.java Source code

Java tutorial

Introduction

Here is the source code for br.com.manish.ahy.kernel.util.TextUtil.java

Source

//  Ahy - A pure java CMS.
//  Copyright (C) 2010 Sidney Leal (manish.com.br)
//
//  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 3 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/>.

package br.com.manish.ahy.kernel.util;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public final class TextUtil {
    private static Log log = LogFactory.getLog(TextUtil.class);

    private TextUtil() {
        super();
    }

    public static String removeAccentuation(String text) {

        String[][] chars = { { "", "a" }, { "?", "A" }, { "", "e" }, { "", "E" }, { "", "i" },
                { "?", "I" }, { "", "o" }, { "", "O" }, { "", "u" }, { "", "U" }, { "", "a" },
                { "", "A" }, { "", "e" }, { "", "E" }, { "", "o" }, { "", "O" }, { "", "c" },
                { "", "C" }, { "", "a" }, { "", "A" }, { "", "o" }, { "", "O" }, { "", "a" },
                { "", "A" } };

        for (String[] item : chars) {
            text = text.replaceAll(item[0], item[1]);
        }

        return text;
    }

    public static String formatDOSLike(String text) {

        String ret = "";
        String preProcessedText = removeAccentuation(text);
        preProcessedText = preProcessedText.replaceAll(" ", "-");

        for (byte b : preProcessedText.getBytes()) {
            if ((b == 95) || (b == 46) || (b > 47 && b < 58) || (b > 64 && b < 91) || (b > 64 && b < 91)
                    || (b > 96 && b < 123)) {
                ret += (char) b;
            }
        }
        return ret;
    }

    public static String tinyFirstLetter(String str) {
        String firstLetter = str.substring(0, 1);
        String ret = str.substring(1, str.length());
        ret = firstLetter.toLowerCase() + ret;
        return ret;
    }

    public static String capFirstLetter(String str) {
        String firstLetter = str.substring(0, 1);
        String ret = str.substring(1, str.length());
        ret = firstLetter.toUpperCase() + ret;
        return ret;
    }

}