com.careerly.utils.TextUtils.java Source code

Java tutorial

Introduction

Here is the source code for com.careerly.utils.TextUtils.java

Source

/*
 * Copyright 2012 Alibaba.com All right reserved. This software is the confidential and proprietary information of
 * Alibaba.com ("Confidential Information"). You shall not disclose such Confidential Information and shall use it only
 * in accordance with the terms of the license agreement you entered into with Alibaba.com.
 */
package com.careerly.utils;

import org.apache.commons.lang.StringEscapeUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.http.Consts;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.regex.Pattern;

/**
 * ???
 */
public class TextUtils {

    private static final Logger logger = LoggerFactory.getLogger(TextUtils.class);

    public static final String WHITESPACE = " ";

    /**
     * ?  
     *
     * @param text ??
     * @return ??
     */
    public static String convertTraditionalChineseNumber2ArabicNumber(String text) {
        if (StringUtils.isBlank(text)) {
            return StringUtils.EMPTY;
        }

        return StringUtils.replaceEach(text,
                new String[] { "", "", "", "??", "", "?", "", "", "?", "" },
                new String[] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" });
    }

    /**
     *   
     *
     * @param number ??
     * @return ??
     */
    public static String convertSimplifiedArabicNumber2ChineseNumber(int number) {
        return StringUtils.replaceEach(number + "",
                new String[] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" },
                new String[] { "", "", "", "", "", "", "", "", "", "?" });
    }

    /**
     * xssHtml?
     *
     * @param text ??
     * @return ??
     */
    public static String escapeHtml(String text) {
        return StringUtils.isBlank(text) ? StringUtils.EMPTY : StringEscapeUtils.escapeHtml(text);
    }

    /**
     * xssjavascript
     *
     * @param text ??
     * @return ??
     */
    public static String escapeJavaScript(String text) {
        return StringUtils.isBlank(text) ? StringUtils.EMPTY : StringEscapeUtils.escapeJavaScript(text);
    }

    /**
     * ???
     *
     * @param text
     * @return
     */
    public static String escapeNotNormalString(String text) {

        if (StringUtils.isBlank(text)) {
            return StringUtils.EMPTY;
        }
        return StringUtils.replaceEach(text, new String[] { "&", "<", ">", "\"", "\\", "\'" },
                new String[] { "&amp;", "&lt;", "&gt;", "&quot;", "\\\\", "&quot;" });
    }

    public static String revertNotNormalString(String text) {

        if (StringUtils.isBlank(text)) {
            return StringUtils.EMPTY;
        }

        return StringUtils.replaceEach(text, new String[] { "&amp;", "&lt;", "&gt;", "&quot;", "\\\\", "&quot;" },
                new String[] { "&", "<", ">", "\"", "\\", "\'" });
    }

    public static String escapeNormalString(String text) {

        if (StringUtils.isBlank(text)) {
            return StringUtils.EMPTY;
        }

        return StringUtils.replaceEach(text, new String[] { "&lt;", "&gt;", "&quot;", "\\\\", "&quot;" },
                new String[] { "<", ">", "\"", "\\", "\'" });
    }

    /**
     * ???
     *
     * @param text
     * @return
     */
    public static String reductionNotNormalString(String text) {

        if (StringUtils.isBlank(text)) {
            return StringUtils.EMPTY;
        }

        return StringUtils.replaceEach(text,
                new String[] { "&amp;", "&lt;", "&gt;", "&quot;", "\\\\", "&quot;", "[", "]" },
                new String[] { "&", "<", ">", "\"", "\\", "\'", "<", ">" });
    }

    /**
     * ???
     *
     * @param text
     * @return
     */
    public static boolean hasAnyNormalString(String text) {

        if (StringUtils.isBlank(text)) {
            return false;
        }

        for (int i = 0; i < text.length(); i++) {
            String str = String.valueOf(text.charAt(i));
            if (StringUtils.isNotBlank(str) && TextUtils.isNormalString(str)) {
                return true;
            }
        }
        return false;
    }

    /**
     * ?
     *
     * @param text
     * @return
     */
    public static boolean hasEnglishChar(String text) {

        if (StringUtils.isBlank(text)) {
            return false;
        }

        for (int i = 0; i < text.length(); i++) {
            if (TextUtils.isEnglishChar(text.charAt(i))) {
                return true;
            }
        }
        return false;
    }

    /**
     * ??????5??*?
     *
     * @param telephone
     * @return
     */
    public static String blurMoblieNumber(String telephone) {
        if (StringUtils.isBlank(telephone)) {
            return StringUtils.EMPTY;
        }
        return telephone.substring(0, 3) + "****" + telephone.substring(7, 11);
    }

    /**
     * ?
     *
     * @param str
     * @param maxLength
     * @param tailPadding
     * @return
     */
    public static String limitString(String str, int maxLength, String tailPadding) {
        if (StringUtils.isBlank(str)) {
            return StringUtils.EMPTY;
        }
        if (str.length() <= maxLength) {
            return str;
        }
        return str.substring(0, maxLength) + tailPadding;
    }

    /**
     * ????
     *
     * @param ch ?
     * @return true
     */
    public static boolean isChineseChar(char ch) {
        Character.UnicodeBlock ub = Character.UnicodeBlock.of(ch);
        return ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
                || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
                || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A;
    }

    /**
     * ?????
     *
     * @param text
     * @return
     */
    public static boolean isChineseStr(String text) {
        if (StringUtils.isBlank(text)) {
            return false;
        }
        for (int i = 0; i < text.length(); i++) {
            if (!TextUtils.isChineseChar(text.charAt(i))) {
                return false;
            }
        }
        return true;
    }

    /**
     * ?
     *
     * @param ch
     * @return
     */
    public static boolean isDigit(char ch) {
        return ch >= '0' && ch <= '9';
    }

    /**
     * ?a-zA-Z0-9
     *
     * @param ch ?
     * @return  true
     */
    public static boolean isDigitOrEngilishChar(char ch) {
        return Character.isLowerCase(ch) || Character.isUpperCase(ch) || Character.isDigit(ch);
    }

    /**
     * ?a-zA-Z
     *
     * @param ch ?
     * @return true
     */
    public static boolean isEnglishChar(char ch) {
        return Character.isLowerCase(ch) || Character.isUpperCase(ch);
    }

/**
 * ????
 *
 * @param text
 * @return
 */
public static boolean isNormalString(String text) {

    if (StringUtils.isBlank(text)) {
        return false;
    }

    for (int i = 0; i < text.length(); i++) {
        char ch = text.charAt(i);
        if (!TextUtils.isChineseChar(ch) && !TextUtils.isEnglishChar(ch) && !TextUtils.isDigit(ch) && ch != ' '
                && ch != '') {
            return false;
        }
    }

    return true;
}

    /**
     * ?
     *
     * @param ch ?
     * @return true
     */
    public static boolean isPunctuationChar(char ch) {
        if (ch > '\u0020' && ch <= '\u002F') {
            return true;
        }
        if (ch >= '\u003A' && ch <= '\u0040') {
            return true;
        }
        if (ch >= '\u005B' && ch <= '\u0060') {
            return true;
        }
        if (ch >= '\u007B' && ch <= '\u007E') {
            return true;
        }
        Character.UnicodeBlock ub = Character.UnicodeBlock.of(ch);
        return ub == Character.UnicodeBlock.GENERAL_PUNCTUATION
                || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION
                || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS;
    }

    /**
     * ?
     *
     * @param text ??
     * @return ??
     */
    public static String normalizeWhitespace(String text) {
        if (StringUtils.isBlank(text)) {
            return StringUtils.EMPTY;
        }

        return text.replaceAll("[ ]+", TextUtils.WHITESPACE);
    }

    /**
     * ?
     *
     * @param text
     * @return
     */
    public static String removeDoubleByte(String text) {

        if (StringUtils.isBlank(text)) {
            return StringUtils.EMPTY;
        }

        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < text.codePointCount(0, text.length()); i++) {
            char[] chars = Character.toChars(text.codePointAt(i));
            if (chars.length == 1) {
                stringBuilder.append(String.valueOf(chars));
            }
        }
        return stringBuilder.toString();
    }

    /**
     * 
     *
     * @param text
     * @return
     */
    public static String removeUselessSpace(String text) {

        if (StringUtils.isBlank(text)) {
            return StringUtils.EMPTY;
        }

        StringBuilder sb = new StringBuilder(text.length());
        for (int i = 0; i < text.length(); i++) {
            if (text.charAt(i) == ' ') {
                if (i > 0 && TextUtils.isDigitOrEngilishChar(text.charAt(i - 1)) && i + 1 < text.length()
                        && TextUtils.isDigitOrEngilishChar(text.charAt(i + 1))) {
                    sb.append(text.charAt(i));
                }
            } else {
                sb.append(text.charAt(i));
            }
        }
        return sb.toString();
    }

    /**
     * c2a0(unicode - '\u00A0')
     *
     * @param text
     * @return
     */
    public static String replaceC2A0(String text) {

        if (StringUtils.isBlank(text)) {
            return StringUtils.EMPTY;
        }

        StringBuilder sb = new StringBuilder(text.length());
        for (int i = 0; i < text.length(); i++) {
            if (text.charAt(i) == '\u00A0') {
                sb.append('\u0020');
            } else {
                sb.append(text.charAt(i));
            }
        }
        return sb.toString();
    }

    /**
     * ?replaceChar
     *
     * @param text        
     * @param replaceChar ??
     * @return ??string
     */
    public static String replacePunctuation(String text, char replaceChar) {

        if (StringUtils.isBlank(text)) {
            return StringUtils.EMPTY;
        }

        StringBuilder sb = new StringBuilder(text.length());
        for (int i = 0; i < text.length(); i++) {
            char c = text.charAt(i);
            if (TextUtils.isPunctuationChar(c)) {
                sb.append(replaceChar);
            } else {
                sb.append(c);
            }
        }
        return sb.toString();
    }

    /**
     * ??
     *
     * @param text
     * @return
     */
    public static String swapNotNormalString(String text) {

        if (StringUtils.isBlank(text)) {
            return StringUtils.EMPTY;
        }

        StringBuffer buffer = new StringBuffer();
        for (int i = 0; i < text.length(); i++) {
            String str = String.valueOf(text.charAt(i));
            if (TextUtils.isNormalString(str)) {
                buffer.append(str);
            }
        }
        return buffer.toString();
    }

    /**
     * xssHtml?
     *
     * @param text ??
     * @return ??
     */
    public static String unescapeHtml(String text) {
        return StringUtils.isBlank(text) ? StringUtils.EMPTY : StringEscapeUtils.unescapeHtml(text);
    }

    /**
     * xssjavascript
     *
     * @param text ??
     * @return ??
     */
    public static String unescapeJavaScript(String text) {
        return StringUtils.isBlank(text) ? StringUtils.EMPTY : StringEscapeUtils.unescapeJavaScript(text);
    }

    public static String urlDecodeGBK(String text) {

        String result = StringUtils.EMPTY;

        if (StringUtils.isBlank(text)) {
            return result;
        }
        try {
            result = URLDecoder.decode(text, "GBK");
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
        return result;
    }

    public static String urlDecodeUTF8(String text) {
        String result = StringUtils.EMPTY;

        if (StringUtils.isBlank(text)) {
            return result;
        }

        try {
            result = URLDecoder.decode(text, Consts.UTF_8.name());
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
        return result;
    }

    public static String urlEncodeGBK(String text) {

        String result = StringUtils.EMPTY;

        if (StringUtils.isBlank(text)) {
            return result;
        }
        try {
            result = URLEncoder.encode(text, "GBK");
        } catch (UnsupportedEncodingException e) {
            logger.error("urlEncodeGBK occur error, text {}, execption", text, e);
        }
        return result;
    }

    public static String urlEncodeUTF8(String text) {

        String result = StringUtils.EMPTY;

        if (StringUtils.isBlank(text)) {
            return result;
        }
        try {
            result = URLEncoder.encode(text, Consts.UTF_8.name());
        } catch (UnsupportedEncodingException e) {
            logger.error("urlEncodeUTF8 occur error, text {}, execption", text, e);
        }
        return result;
    }

    /**
     * ?
     *
     * @param text
     * @return
     */
    public static String escapeQuote(String text) {
        if (StringUtils.isBlank(text)) {
            return StringUtils.EMPTY;
        }
        return StringUtils.replace(StringUtils.replace(text, "\\", "\\\\"), "\"", "\\\"");
    }

    /**
     * ????html
     *
     * @param inputString
     * @return
     */
    public static boolean validateStructureHtml(String inputString) {
        Pattern pattern = Pattern.compile("<[a-zA-Z]+[1-9]?[^><]*>|</[a-zA-Z]+[1-9]?>|\\&[a-zA-Z]{1,10};",
                Pattern.CASE_INSENSITIVE);
        return pattern.matcher(inputString).find();
    }

}