Example usage for org.apache.commons.lang CharUtils isAsciiAlphanumeric

List of usage examples for org.apache.commons.lang CharUtils isAsciiAlphanumeric

Introduction

In this page you can find the example usage for org.apache.commons.lang CharUtils isAsciiAlphanumeric.

Prototype

public static boolean isAsciiAlphanumeric(char ch) 

Source Link

Document

Checks whether the character is ASCII 7 bit numeric.

Usage

From source file:org.nuxeo.ecm.core.storage.ExpressionEvaluator.java

/**
 * Turns a NXQL LIKE pattern into a regex.
 * <p>/*from  ww  w  . ja  v  a 2 s.  co  m*/
 * % and _ are standard wildcards, and \ escapes them.
 *
 * @since 7.4
 */
public static String likeToRegex(String like) {
    StringBuilder regex = new StringBuilder();
    char[] chars = like.toCharArray();
    boolean escape = false;
    for (int i = 0; i < chars.length; i++) {
        char c = chars[i];
        boolean escapeNext = false;
        switch (c) {
        case '%':
            if (escape) {
                regex.append(c);
            } else {
                regex.append(".*");
            }
            break;
        case '_':
            if (escape) {
                regex.append(c);
            } else {
                regex.append(".");
            }
            break;
        case '\\':
            if (escape) {
                regex.append("\\\\"); // backslash escaped for regexp
            } else {
                escapeNext = true;
            }
            break;
        default:
            // escape mostly everything just in case
            if (!CharUtils.isAsciiAlphanumeric(c)) {
                regex.append("\\");
            }
            regex.append(c);
            break;
        }
        escape = escapeNext;
    }
    if (escape) {
        // invalid string terminated by escape character, ignore
    }
    return regex.toString();
}

From source file:org.rapidcontext.core.data.XmlSerializer.java

/**
 * Writes an XML tag name.//from  w  ww  .j  a  v  a 2 s.  co m
 *
 * @param id             the tag name (identifier)
 * @param buffer         the string buffer to append into
 */
private static void tagName(String id, StringBuilder buffer) {
    for (int i = 0; i < id.length(); i++) {
        char c = id.charAt(i);
        if (i == 0 && !CharUtils.isAsciiAlpha(c)) {
            c = '_';
        } else if (!CharUtils.isAsciiAlphanumeric(c)) {
            c = '_';
        }
        buffer.append(c);
    }
}

From source file:org.yccheok.jstock.gui.Utils.java

/**
 * Returns list of Han Yu Pin Yin's prefix of every characters. If the
 * character is an alphabet or numerical, the original character will be
 * used. If there is any error occur during conversion, that particular
 * character will be ignored./*from   w w  w. ja v  a2s  . c om*/
 *
 * @param chinese String to be converted
 * @return List of Han Yu Pin Yin's prefix of every characters.
 */
public static List<String> toHanyuPinyin(String chinese) {
    // Is this an empty string?
    if (chinese.isEmpty()) {
        return new ArrayList<String>();
    }

    // Use StringBuilder instead of String during processing for speed
    // optimization.
    List<StringBuilder> stringBuilders = null;

    for (int i = 0, length = chinese.length(); i < length; i++) {
        final char c = chinese.charAt(i);

        String[] pinyins = null;
        final java.util.Set<Character> set = new java.util.HashSet<Character>();
        // Is this Chinese character?
        if (CharUtils.isAscii(c)) {
            if (CharUtils.isAsciiAlphanumeric(c)) {
                // We are only interested in 'abc' and '123'.
                set.add(c);
            }
        } else {
            // This is possible a Chinese character.
            try {
                pinyins = PinyinHelper.toHanyuPinyinStringArray(c, DEFAULT_HANYU_PINYIN_OUTPUT_FORMAT);
                if (pinyins != null) {
                    for (String pinyin : pinyins) {
                        set.add(pinyin.charAt(0));
                    }
                }
            } catch (BadHanyuPinyinOutputFormatCombination ex) {
                log.error(null, ex);
                // No. This is not Chinese character.
                // Just ignore the error. Continue for the rest of characters.
                // return new ArrayList<String>();
            }
        }
        final List<StringBuilder> tmps = stringBuilders;
        stringBuilders = new ArrayList<StringBuilder>();

        if (tmps == null) {
            // This will be the first converted character.
            for (Character character : set) {
                final StringBuilder me = new StringBuilder();
                me.append(character);
                stringBuilders.add(me);
            }
        } else {
            for (Character character : set) {
                for (StringBuilder tmp : tmps) {
                    final StringBuilder me = new StringBuilder();
                    me.append(tmp);
                    me.append(character);
                    stringBuilders.add(me);
                }
            }
        }
    }

    List<String> result = new ArrayList<String>();
    // Do we have any converted characters?
    if (stringBuilders != null) {
        for (StringBuilder stringBuilder : stringBuilders) {
            result.add(stringBuilder.toString());
        }
    }

    return result;
}

From source file:stirling.fix.messages.fix42.DefaultMessageFactory.java

private boolean isValidSingle(String msgType) {
    char first = msgType.charAt(0);
    return CharUtils.isAsciiAlphanumeric(first);
}