Example usage for java.lang Character isLetterOrDigit

List of usage examples for java.lang Character isLetterOrDigit

Introduction

In this page you can find the example usage for java.lang Character isLetterOrDigit.

Prototype

public static boolean isLetterOrDigit(int codePoint) 

Source Link

Document

Determines if the specified character (Unicode code point) is a letter or digit.

Usage

From source file:org.apache.myfaces.renderkit.html.util.ReducedHTMLParser.java

/**
 * Eat up a sequence of chars which form a valid XML element name.
 * <p>//  w ww  .j a v  a 2s.c  o  m
 * TODO: implement this properly in compliance with spec
 */
String consumeElementName() {
    consumeWhitespace();
    int nameStart = _offset;
    while (!isFinished()) {
        boolean ok = false;
        char c = _seq.charAt(_offset);
        if (Character.isLetterOrDigit(_seq.charAt(_offset))) {
            ok = true;
        } else if (c == '_') {
            ok = true;
        } else if (c == '-') {
            ok = true;
        } else if (c == ':') {
            ok = true;
        }

        if (!ok) {
            break;
        }

        ++_offset;
    }

    if (nameStart == _offset) {
        return null;
    } else {
        return _seq.subSequence(nameStart, _offset).toString();
    }
}

From source file:org.ajax4jsf.templatecompiler.utils.ReducedHTMLParser.java

/**
 * Eat up a sequence of chars which form a valid XML element name.
 * <p>//  w  w w . j a v a2s  . c om
 * TODO: implement this properly in compliance with spec
 */
String consumeElementName() {
    consumeWhitespace();
    int nameStart = this._offset;
    while (!isFinished()) {
        boolean ok = false;
        char c = this._seq.charAt(this._offset);
        if (Character.isLetterOrDigit(this._seq.charAt(this._offset))) {
            ok = true;
        } else if (c == '_') {
            ok = true;
        } else if (c == '-') {
            ok = true;
        } else if (c == ':') {
            ok = true;
        }

        if (!ok) {
            break;
        }

        ++this._offset;
    }

    if (nameStart == this._offset) {
        return null;
    } else {
        return this._seq.subSequence(nameStart, this._offset).toString();
    }
}

From source file:nl.strohalm.cyclos.utils.StringHelper.java

/**
 * Removes the given mask from the given value, optionally trimming at mask max length (ex: mask is ###, 12345 would result 123 it true, and 12345
 * if false)/*from  www  .  j  ava 2  s.c om*/
 */
public static String removeMask(final String mask, final String value, final boolean trimToMask) {
    if (StringUtils.isEmpty(mask) || value == null) {
        return value;
    }
    final StringBuilder sb = new StringBuilder();
    boolean nextIsLiteral = false;
    int pos = 0;
    for (int i = 0; i < mask.length(); i++) {
        final char c = mask.charAt(i);
        if (c == '\\') {
            nextIsLiteral = true;
        } else if (!nextIsLiteral && MASK_VARIABLES.indexOf(c) >= 0) {
            try {
                while (!Character.isLetterOrDigit(value.charAt(pos))) {
                    pos++;
                }
                sb.append(value.charAt(pos++));
            } catch (final StringIndexOutOfBoundsException e) {
                break;
            }
        } else if (nextIsLiteral) {
            nextIsLiteral = false;
            sb.append(c);
            pos++;
        }
    }
    if (!trimToMask) {
        sb.append(value.substring(pos));
    }
    return sb.toString();
}

From source file:org.exoplatform.wcm.webui.formgenerator.UIFormGeneratorTabPane.java

/**
 * Clean string.//from w w  w  . j  a va 2 s .  c  o m
 *
 * @param str the str
 *
 * @return the string
 */
private static String cleanString(String str) {
    Transliterator accentsconverter = Transliterator
            .getInstance("Latin; NFD; [:Nonspacing Mark:] Remove; NFC;");
    str = accentsconverter.transliterate(str);
    //the character ? seems to not be changed to d by the transliterate function
    StringBuffer cleanedStr = new StringBuffer(str.trim());
    // delete special character
    for (int i = 0; i < cleanedStr.length(); i++) {
        char c = cleanedStr.charAt(i);
        if (c == ' ') {
            if (i > 0 && cleanedStr.charAt(i - 1) == '-') {
                cleanedStr.deleteCharAt(i--);
            } else {
                c = '_';
                cleanedStr.setCharAt(i, c);
            }
            continue;
        }
        if (i > 0 && !(Character.isLetterOrDigit(c) || c == '-')) {
            cleanedStr.deleteCharAt(i--);
            continue;
        }
        if (i > 0 && c == '-' && cleanedStr.charAt(i - 1) == '-')
            cleanedStr.deleteCharAt(i--);
    }

    if (!Character.isLetterOrDigit(cleanedStr.charAt(0))) {
        cleanedStr.deleteCharAt(0);
    }

    if (cleanedStr.length() > 0 && !Character.isLetterOrDigit(cleanedStr.charAt(cleanedStr.length() - 1))) {
        cleanedStr.deleteCharAt(cleanedStr.length() - 1);
    }

    return cleanedStr.toString().toLowerCase().replaceAll("-", "_");
}

From source file:org.sakaiproject.vm.VelocityServlet.java

/**
 * Change any characters that Velocity doesn't like in the name to '_' to make a valid Velocity name
 * /* ww w .  j  a va  2  s  .  c  o  m*/
 * @param name
 *        The name to convert.
 * @return The name converted to a valid Velocity name.
 */
protected String escapeVmName(String name) {
    char[] chars = name.toCharArray();

    // make sure first character is valid (alpha)
    if (!Character.isLetter(chars[0])) {
        chars[0] = 'X';
    }

    // replace any other invalid characters
    for (int i = 1; i < chars.length; i++) {
        char c = chars[i];
        if (!(Character.isLetterOrDigit(c) || (c == '_') || (c == '-'))) {
            chars[i] = '_';
        }
    }

    return new String(chars);

}

From source file:org.openhealthtools.openatna.syslog.bsd.BsdMessageFactory.java

public SyslogMessage read(InputStream in) throws SyslogException {
    try {/* w w w  .ja  v  a 2  s .com*/
        PushbackInputStream pin = new PushbackInputStream(in, 5);
        int priority = readPriority(pin);
        int facility;
        int severity;
        byte c;
        int spaces = 4;
        int count = 0;
        boolean spaceBefore = false;
        ByteBuffer buff = ByteBuffer.wrap(new byte[256]);

        String timestamp;
        String month = null;
        String date = null;
        String time = null;
        String host = "";
        int max = 256;
        int curr = 0;

        while (count < spaces && curr < max) {
            c = (byte) pin.read();
            curr++;
            if (c == ' ') {
                if (!spaceBefore) {
                    count++;
                    String currHeader = new String(buff.array(), 0, buff.position(), Constants.ENC_UTF8);
                    buff.clear();
                    switch (count) {
                    case 1:
                        month = currHeader;
                        break;
                    case 2:
                        date = currHeader;
                        break;
                    case 3:
                        time = currHeader;
                        break;
                    case 4:
                        host = currHeader;
                        break;

                    }
                }
                spaceBefore = true;

            } else {
                spaceBefore = false;
                buff.put(c);
            }
        }
        if (month == null || date == null || time == null) {
            timestamp = createDate(new Date());
        } else {
            String gap = " ";
            if (date.length() == 1) {
                gap = "  ";
            }
            timestamp = (month + gap + date + " " + time);
            try {
                formatDate(timestamp);
            } catch (Exception e) {
                timestamp = createDate(new Date());
            }
        }
        String tag = null;
        int tagLen = 32;
        buff.clear();
        for (int i = 0; i < tagLen; i++) {
            c = (byte) pin.read();
            curr++;
            if (!Character.isLetterOrDigit((char) (c & 0xff))) {
                pin.unread(c);
                break;
            }
            buff.put(c);
        }
        if (buff.position() > 0) {
            tag = new String(buff.array(), 0, buff.position(), Constants.ENC_UTF8);
        }

        LogMessage logMessage = getLogMessage(tag);
        String encoding = readBom(pin, logMessage.getExpectedEncoding());
        logMessage.read(pin, encoding);
        facility = priority / 8;
        severity = priority % 8;
        return new BsdMessage(facility, severity, timestamp, host, logMessage, tag);
    } catch (IOException e) {
        e.printStackTrace();
        throw new SyslogException(e);
    }
}

From source file:util.Seeks.java

/**
 * Prepares a String for comparison: converts
 * string to lower case, resolves  to ss and removes
 * all non letter and non digit characters.
 *//* www .j  av a  2 s .c o  m*/
private String prepareCompare(String input) {

    if (input == null) {
        return "";
    }

    input = input.toLowerCase();
    input = input.replaceAll("", "ss");

    // remove all non-letter characters (including spaces)
    final StringBuffer strBuff = new StringBuffer();
    char c;

    final int max = input.length();
    for (int i = 0; i < max; i++) {
        c = input.charAt(i);

        if (Character.isLetterOrDigit(c)) {
            strBuff.append(c);
        }
    }

    return strBuff.toString();
}

From source file:com.github.jferard.pgloaderutils.sniffer.csv.HeaderRowAnalyzer.java

private int getFieldDelimiterIndex(String expectedField, String line, int firstIndex, int firstLetterIndex)
        throws IOException {
    int delimiterBlockIndex;
    if (firstLetterIndex == firstIndex) {
        delimiterBlockIndex = this.getDelimiterBlockIndex(expectedField, line, expectedField.length(),
                firstLetterIndex);/*from  www.j a  v  a 2  s .c o m*/
    } else if (firstLetterIndex == firstIndex + 1) {
        char maybeQuote = line.charAt(firstIndex);
        if (Character.isLetterOrDigit(maybeQuote))
            throw new IOException("Missing start of field:" + expectedField + " (" + line + ")");

        int len = this.getLen(expectedField, maybeQuote);
        delimiterBlockIndex = this.getDelimiterBlockIndex(expectedField, line, len, firstLetterIndex);

        for (int i = 0; i < len; i++) {
            if (line.charAt(firstLetterIndex + i) == maybeQuote)
                this.escapeCounter.put(line.charAt(firstLetterIndex + i - 1));
        }

        if (line.charAt(delimiterBlockIndex) != maybeQuote)
            throw new IOException("Missing quote:" + expectedField + " (" + line + ")");
        this.quoteCounter.put(maybeQuote);
        delimiterBlockIndex++;
    } else {
        throw new IOException("Missing start of field:" + expectedField + " (" + line + ")");
    }
    return delimiterBlockIndex;
}

From source file:com.sun.socialsite.util.Utilities.java

/**
 * Replaces occurences of non-alphanumeric characters with a
 * supplied char./* www  . ja  va 2s  .  c  o  m*/
 */
public static String replaceNonAlphanumeric(String str, char subst) {
    StringBuffer ret = new StringBuffer(str.length());
    char[] testChars = str.toCharArray();
    for (int i = 0; i < testChars.length; i++) {
        if (Character.isLetterOrDigit(testChars[i])) {
            ret.append(testChars[i]);
        } else {
            ret.append(subst);
        }
    }
    return ret.toString();
}