Example usage for org.apache.commons.lang3 CharUtils isAsciiPrintable

List of usage examples for org.apache.commons.lang3 CharUtils isAsciiPrintable

Introduction

In this page you can find the example usage for org.apache.commons.lang3 CharUtils isAsciiPrintable.

Prototype

public static boolean isAsciiPrintable(final char ch) 

Source Link

Document

Checks whether the character is ASCII 7 bit printable.

 CharUtils.isAsciiPrintable('a')  = true CharUtils.isAsciiPrintable('A')  = true CharUtils.isAsciiPrintable('3')  = true CharUtils.isAsciiPrintable('-')  = true CharUtils.isAsciiPrintable('\n') = false CharUtils.isAsciiPrintable('©') = false 

Usage

From source file:com.joyent.manta.client.crypto.EncryptedMetadataUtils.java

/**
 * Parses a plaintext metadata string and converts it into a {@link Map} of
 * keys and values./*from  www .jav a  2  s  .  c o m*/
 *
 * @param plaintext Plaintext binary data in US-ASCII encoding
 * @return headers as map
 */
public static Map<String, String> plaintextMetadataAsMap(final byte[] plaintext) {
    Map<String, String> map = new CaseInsensitiveMap<>();

    boolean parsingKey = true;
    boolean parsingVal = false;

    final int initialSize = 24;
    StringBuilder key = new StringBuilder(initialSize);
    StringBuilder val = new StringBuilder(initialSize);

    for (int i = 0; i <= plaintext.length; i++) {
        // Done parsing a line, now we add it to the map
        if (i == plaintext.length || (char) plaintext[i] == CharUtils.LF) {
            if (key.length() > 0) {
                map.put(key.toString(), val.toString());
            }

            key.setLength(0);
            val.setLength(0);
            parsingKey = true;
            parsingVal = false;
            continue;
        }

        char c = (char) plaintext[i];

        if (!CharUtils.isAsciiPrintable(c)) {
            String msg = "Encrypted metadata contained a " + "non-ascii or unprintable character";
            throw new MantaClientEncryptionException(msg);
        }

        if (c == ':' && parsingKey) {
            parsingKey = false;
            continue;
        }

        if (c == ' ' && !parsingKey && !parsingVal) {
            continue;
        } else if (!parsingKey && !parsingVal) {
            parsingVal = true;
        }

        if (parsingKey) {
            key.append(c);
        }

        if (parsingVal) {
            val.append(c);
        }
    }

    return map;
}

From source file:org.apache.commons.lang3.StringUtils.java

/**
 * <p>Checks if the CharSequence contains only ASCII printable characters.</p>
 *
 * <p>{@code null} will return {@code false}.
 * An empty CharSequence (length()=0) will return {@code true}.</p>
 *
 * <pre>//  w ww .  j  a va 2  s .c o  m
 * StringUtils.isAsciiPrintable(null)     = false
 * StringUtils.isAsciiPrintable("")       = true
 * StringUtils.isAsciiPrintable(" ")      = true
 * StringUtils.isAsciiPrintable("Ceki")   = true
 * StringUtils.isAsciiPrintable("ab2c")   = true
 * StringUtils.isAsciiPrintable("!ab-c~") = true
 * StringUtils.isAsciiPrintable("\u0020") = true
 * StringUtils.isAsciiPrintable("\u0021") = true
 * StringUtils.isAsciiPrintable("\u007e") = true
 * StringUtils.isAsciiPrintable("\u007f") = false
 * StringUtils.isAsciiPrintable("Ceki G\u00fclc\u00fc") = false
 * </pre>
 *
 * @param cs the CharSequence to check, may be null
 * @return {@code true} if every character is in the range
 *  32 thru 126
 * @since 2.1
 * @since 3.0 Changed signature from isAsciiPrintable(String) to isAsciiPrintable(CharSequence)
 */
public static boolean isAsciiPrintable(CharSequence cs) {
    if (cs == null) {
        return false;
    }
    int sz = cs.length();
    for (int i = 0; i < sz; i++) {
        if (CharUtils.isAsciiPrintable(cs.charAt(i)) == false) {
            return false;
        }
    }
    return true;
}

From source file:bfile.util.StringUtils.java

/**
 * <p>Checks if the CharSequence contains only ASCII printable characters.</p>
 *
 * <p>{@code null} will return {@code false}.
 * An empty CharSequence (length()=0) will return {@code true}.</p>
 *
 * <pre>/* w w w  .  j  ava2 s  .  c o  m*/
 * StringUtils.isAsciiPrintable(null)     = false
 * StringUtils.isAsciiPrintable("")       = true
 * StringUtils.isAsciiPrintable(" ")      = true
 * StringUtils.isAsciiPrintable("Ceki")   = true
 * StringUtils.isAsciiPrintable("ab2c")   = true
 * StringUtils.isAsciiPrintable("!ab-c~") = true
 * StringUtils.isAsciiPrintable("\u0020") = true
 * StringUtils.isAsciiPrintable("\u0021") = true
 * StringUtils.isAsciiPrintable("\u007e") = true
 * StringUtils.isAsciiPrintable("\u007f") = false
 * StringUtils.isAsciiPrintable("Ceki G\u00fclc\u00fc") = false
 * </pre>
 *
 * @param cs the CharSequence to check, may be null
 * @return {@code true} if every character is in the range
 *  32 thru 126
 * @since 2.1
 * @since 3.0 Changed signature from isAsciiPrintable(String) to isAsciiPrintable(CharSequence)
 */
public static boolean isAsciiPrintable(final CharSequence cs) {
    if (cs == null) {
        return false;
    }
    final int sz = cs.length();
    for (int i = 0; i < sz; i++) {
        if (CharUtils.isAsciiPrintable(cs.charAt(i)) == false) {
            return false;
        }
    }
    return true;
}

From source file:org.apache.openmeetings.screenshare.job.OmKeyEvent.java

public OmKeyEvent(Map<String, Object> obj) {
     alt = TRUE.equals(obj.get("alt"));
     ctrl = TRUE.equals(obj.get("ctrl"));
     shift = TRUE.equals(obj.get("shift")) || isUpperCase(ch);
     ch = (char) getInt(obj, "char");
     key = inKey = getInt(obj, "key");
     Integer _key = null;/*from   w  w w .  ja va  2  s  .  c om*/
     if (CharUtils.isAsciiPrintable(ch)) {
         boolean alpha = Character.isAlphabetic(ch);
         if (alpha) { // can't be combined due to different types
             key = getKeyStroke(toUpperCase(ch), 0).getKeyCode();
         } else {
             key = getKeyStroke(Character.valueOf(ch), 0).getKeyCode();
         }
         if (key == 0) {
             _key = CHAR_MAP.get(ch);
             if (_key == null) {
                 // fallback
                 key = inKey;
             }
         }
         if (!alpha && _key == null) {
             _key = KEY_MAP.get(key);
         }
     } else {
         _key = KEY_MAP.get(key);
     }
     this.key = _key == null ? key : _key;
     log.debug("sequence:: shift {}, ch {}, orig {} -> key {}({}), map {}", shift, ch == 0 ? ' ' : ch, inKey,
             key, Integer.toHexString(key), _key);
 }

From source file:org.finra.dm.service.helper.Hive13DdlGenerator.java

/**
 * Gets the DDL character value based on the specified configured character value. This method supports UTF-8 encoded strings and will "Hive" escape any
 * non-ASCII printable characters using '\(value)'.
 *
 * @param string the configured character value.
 * @param escapeSingleBackslash specifies if we need to escape a single backslash character with an extra backslash
 *
 * @return the DDL character value.//  w  ww. j  a v a 2s.  co m
 */
public String getDdlCharacterValue(String string, boolean escapeSingleBackslash) {
    // Assume the empty string for the return value.
    StringBuilder returnValueStringBuilder = new StringBuilder();

    // If we have an actual character, set the return value based on our rules.
    if (StringUtils.isNotEmpty(string)) {
        // Convert the string to UTF-8 so we can the proper characters that were sent via XML.
        String utf8String = new String(string.getBytes(Charsets.UTF_8), Charsets.UTF_8);

        // Loop through each character and add each one to the return value.
        for (int i = 0; i < utf8String.length(); i++) {
            // Default to the character itself.
            Character character = string.charAt(i);
            String nextValue = character.toString();

            // If the character isn't ASCII printable, then "Hive" escape it.
            if (!CharUtils.isAsciiPrintable(character)) {
                // If the character is unprintable, then display it as the ASCII octal value in \000 format.
                nextValue = String.format("\\%03o", (int) character);
            }

            // Add this character to the return value.
            returnValueStringBuilder.append(nextValue);
        }

        // Check if we need to escape a single backslash character with an extra backslash.
        if (escapeSingleBackslash && returnValueStringBuilder.toString().equals("\\")) {
            returnValueStringBuilder.append('\\');
        }
    }

    // Return the value.
    return returnValueStringBuilder.toString();
}

From source file:org.formiz.core.input.csv.AbstractCsvInputSource.java

/**
 * Create a map with header/row data./*from  w  w  w. j a v a 2  s. c  om*/
 *
 * @param header
 *            header line
 * @param data
 *            data line.
 * @return
 */
protected static Map<String, String> toMap(String[] header, String[] data) {
    Map<String, String> ret = new HashMap<String, String>();
    StringBuilder firstColumn = new StringBuilder();

    if (header.length == 0) {
        return ret;
    }

    // Prevent encoding issues, Skip UTF8 BOM.
    for (char c : header[0].toCharArray()) {
        if (CharUtils.isAsciiPrintable(c)) {
            firstColumn.append(c);
        }
    }
    ret.put(firstColumn.toString(), data[0]);

    for (int i = 1; i < header.length; i++) {
        ret.put(header[i], data[i]);
    }
    return ret;
}

From source file:org.guvnor.common.services.backend.validation.ValidationUtils.java

public static boolean isJavaIdentifier(final String value) {
    if (StringUtils.isBlank(value)) {
        return false;
    }/*from   ww  w  .  j av  a2s.c om*/
    if (!SourceVersion.isIdentifier(value) || SourceVersion.isKeyword(value)) {
        return false;
    }
    for (int i = 0; i < value.length(); i++) {
        if (!CharUtils.isAsciiPrintable(value.charAt(i))) {
            return false;
        }
    }
    return true;
}

From source file:org.kalypso.commons.databinding.validation.FileIsAsciiPrintable.java

private void validateName(final String name) throws CoreException {
    final char[] chars = name.toCharArray();
    for (final char c : chars) {
        if (!CharUtils.isAsciiPrintable(c))
            fail();/*from   w w  w.ja v  a 2  s.c o  m*/
    }
}