Example usage for org.apache.commons.lang StringUtils isWhitespace

List of usage examples for org.apache.commons.lang StringUtils isWhitespace

Introduction

In this page you can find the example usage for org.apache.commons.lang StringUtils isWhitespace.

Prototype

public static boolean isWhitespace(String str) 

Source Link

Document

Checks if the String contains only whitespace.

Usage

From source file:airlift.util.AirliftUtil.java

/**
 * Convert to integer.//from  w  ww.ja va 2 s .c o m
 *
 * @param _byteArray the _byte array
 * @return the integer
 */
public static Integer convertToInteger(byte[] _byteArray) {
    String convertedBytes = convertToString(_byteArray);
    return (StringUtils.isNumeric(convertedBytes) == true && StringUtils.isWhitespace(convertedBytes) == false)
            ? Integer.parseInt(convertedBytes)
            : null;
}

From source file:airlift.util.AirliftUtil.java

/**
 * Convert to double.//from   www.java  2s .c  o  m
 *
 * @param _byteArray the _byte array
 * @return the double
 */
public static Double convertToDouble(byte[] _byteArray) {
    String convertedBytes = convertToString(_byteArray);
    return (StringUtils.isNumeric(convertedBytes) == true && StringUtils.isWhitespace(convertedBytes) == false)
            ? Double.parseDouble(convertedBytes)
            : null;
}

From source file:airlift.util.AirliftUtil.java

/**
 * Convert to float./*from   ww w. j a v  a 2  s. c o  m*/
 *
 * @param _byteArray the _byte array
 * @return the float
 */
public static Float convertToFloat(byte[] _byteArray) {
    String convertedBytes = convertToString(_byteArray);
    return (StringUtils.isNumeric(convertedBytes) == true && StringUtils.isWhitespace(convertedBytes) == false)
            ? Float.parseFloat(convertedBytes)
            : null;
}

From source file:airlift.util.AirliftUtil.java

/**
 * Convert to date.//  www .j  ava2s  .c o  m
 *
 * @param _byteArray the _byte array
 * @return the java.util. date
 */
public static java.util.Date convertToDate(byte[] _byteArray) {
    String convertedBytes = convertToString(_byteArray);
    return (StringUtils.isNumeric(convertedBytes) == true && StringUtils.isWhitespace(convertedBytes) == false)
            ? new java.util.Date(Long.parseLong(convertedBytes))
            : null;
}

From source file:com.ms.commons.test.BaseTestCase.java

/**
 * Assert string contains only whitespace.
 */
protected void assertWhitespace(String str) {
    assertTrue(StringUtils.isWhitespace(str));
}

From source file:org.apache.accumulo.core.util.shell.commands.SetIterCommand.java

private static String setUpOptions(ClassLoader classloader, final ConsoleReader reader, final String className,
        final Map<String, String> options) throws IOException, ShellCommandException {
    String input;//from  w w w . j av  a 2  s  . c  o m
    @SuppressWarnings("rawtypes")
    SortedKeyValueIterator untypedInstance;
    @SuppressWarnings("rawtypes")
    Class<? extends SortedKeyValueIterator> clazz;
    try {
        clazz = classloader.loadClass(className).asSubclass(SortedKeyValueIterator.class);
        untypedInstance = clazz.newInstance();
    } catch (ClassNotFoundException e) {
        StringBuilder msg = new StringBuilder("Unable to load ").append(className);
        if (className.indexOf('.') < 0) {
            msg.append("; did you use a fully qualified package name?");
        } else {
            msg.append("; class not found.");
        }
        throw new ShellCommandException(ErrorCode.INITIALIZATION_FAILURE, msg.toString());
    } catch (InstantiationException e) {
        throw new IllegalArgumentException(e.getMessage());
    } catch (IllegalAccessException e) {
        throw new IllegalArgumentException(e.getMessage());
    } catch (ClassCastException e) {
        StringBuilder msg = new StringBuilder(50);
        msg.append(className).append(" loaded successfully but does not implement SortedKeyValueIterator.");
        msg.append(" This class cannot be used with this command.");
        throw new ShellCommandException(ErrorCode.INITIALIZATION_FAILURE, msg.toString());
    }

    @SuppressWarnings("unchecked")
    SortedKeyValueIterator<Key, Value> skvi = (SortedKeyValueIterator<Key, Value>) untypedInstance;
    OptionDescriber iterOptions = null;
    if (OptionDescriber.class.isAssignableFrom(skvi.getClass())) {
        iterOptions = (OptionDescriber) skvi;
    }

    String iteratorName;
    if (null != iterOptions) {
        final IteratorOptions itopts = iterOptions.describeOptions();
        iteratorName = itopts.getName();

        if (iteratorName == null) {
            throw new IllegalArgumentException(
                    className + " described its default distinguishing name as null");
        }
        String shortClassName = className;
        if (className.contains(".")) {
            shortClassName = className.substring(className.lastIndexOf('.') + 1);
        }
        final Map<String, String> localOptions = new HashMap<String, String>();
        do {
            // clean up the overall options that caused things to fail
            for (String key : localOptions.keySet()) {
                options.remove(key);
            }
            localOptions.clear();

            reader.println(itopts.getDescription());

            String prompt;
            if (itopts.getNamedOptions() != null) {
                for (Entry<String, String> e : itopts.getNamedOptions().entrySet()) {
                    prompt = Shell.repeat("-", 10) + "> set " + shortClassName + " parameter " + e.getKey()
                            + ", " + e.getValue() + ": ";
                    reader.flush();
                    input = reader.readLine(prompt);
                    if (input == null) {
                        reader.println();
                        throw new IOException("Input stream closed");
                    }
                    // Places all Parameters and Values into the LocalOptions, even if the value is "".
                    // This allows us to check for "" values when setting the iterators and allows us to remove
                    // the parameter and value from the table property.
                    localOptions.put(e.getKey(), input);
                }
            }

            if (itopts.getUnnamedOptionDescriptions() != null) {
                for (String desc : itopts.getUnnamedOptionDescriptions()) {
                    reader.println(Shell.repeat("-", 10) + "> entering options: " + desc);
                    input = "start";
                    prompt = Shell.repeat("-", 10) + "> set " + shortClassName
                            + " option (<name> <value>, hit enter to skip): ";
                    while (true) {
                        reader.flush();
                        input = reader.readLine(prompt);
                        if (input == null) {
                            reader.println();
                            throw new IOException("Input stream closed");
                        } else {
                            input = new String(input);
                        }

                        if (input.length() == 0)
                            break;

                        String[] sa = input.split(" ", 2);
                        localOptions.put(sa[0], sa[1]);
                    }
                }
            }

            options.putAll(localOptions);
            if (!iterOptions.validateOptions(options))
                reader.println("invalid options for " + clazz.getName());

        } while (!iterOptions.validateOptions(options));
    } else {
        reader.flush();
        reader.println(
                "The iterator class does not implement OptionDescriber. Consider this for better iterator configuration using this setiter command.");
        iteratorName = reader.readLine("Name for iterator (enter to skip): ");
        if (null == iteratorName) {
            reader.println();
            throw new IOException("Input stream closed");
        } else if (StringUtils.isWhitespace(iteratorName)) {
            // Treat whitespace or empty string as no name provided
            iteratorName = null;
        }

        reader.flush();
        reader.println("Optional, configure name-value options for iterator:");
        String prompt = Shell.repeat("-", 10) + "> set option (<name> <value>, hit enter to skip): ";
        final HashMap<String, String> localOptions = new HashMap<String, String>();

        while (true) {
            reader.flush();
            input = reader.readLine(prompt);
            if (input == null) {
                reader.println();
                throw new IOException("Input stream closed");
            } else if (StringUtils.isWhitespace(input)) {
                break;
            }

            String[] sa = input.split(" ", 2);
            localOptions.put(sa[0], sa[1]);
        }

        options.putAll(localOptions);
    }

    return iteratorName;
}

From source file:org.apache.accumulo.shell.commands.SetIterCommand.java

private static String setUpOptions(ClassLoader classloader, final ConsoleReader reader, final String className,
        final Map<String, String> options) throws IOException, ShellCommandException {
    String input;/*from  ww  w.  j ava2  s.  co  m*/
    @SuppressWarnings("rawtypes")
    SortedKeyValueIterator untypedInstance;
    @SuppressWarnings("rawtypes")
    Class<? extends SortedKeyValueIterator> clazz;
    try {
        clazz = classloader.loadClass(className).asSubclass(SortedKeyValueIterator.class);
        untypedInstance = clazz.newInstance();
    } catch (ClassNotFoundException e) {
        StringBuilder msg = new StringBuilder("Unable to load ").append(className);
        if (className.indexOf('.') < 0) {
            msg.append("; did you use a fully qualified package name?");
        } else {
            msg.append("; class not found.");
        }
        throw new ShellCommandException(ErrorCode.INITIALIZATION_FAILURE, msg.toString());
    } catch (InstantiationException e) {
        throw new IllegalArgumentException(e.getMessage());
    } catch (IllegalAccessException e) {
        throw new IllegalArgumentException(e.getMessage());
    } catch (ClassCastException e) {
        StringBuilder msg = new StringBuilder(50);
        msg.append(className).append(" loaded successfully but does not implement SortedKeyValueIterator.");
        msg.append(" This class cannot be used with this command.");
        throw new ShellCommandException(ErrorCode.INITIALIZATION_FAILURE, msg.toString());
    }

    @SuppressWarnings("unchecked")
    SortedKeyValueIterator<Key, Value> skvi = untypedInstance;
    OptionDescriber iterOptions = null;
    if (OptionDescriber.class.isAssignableFrom(skvi.getClass())) {
        iterOptions = (OptionDescriber) skvi;
    }

    String iteratorName;
    if (null != iterOptions) {
        final IteratorOptions itopts = iterOptions.describeOptions();
        iteratorName = itopts.getName();

        if (iteratorName == null) {
            throw new IllegalArgumentException(
                    className + " described its default distinguishing name as null");
        }
        String shortClassName = className;
        if (className.contains(".")) {
            shortClassName = className.substring(className.lastIndexOf('.') + 1);
        }
        final Map<String, String> localOptions = new HashMap<String, String>();
        do {
            // clean up the overall options that caused things to fail
            for (String key : localOptions.keySet()) {
                options.remove(key);
            }
            localOptions.clear();

            reader.println(itopts.getDescription());

            String prompt;
            if (itopts.getNamedOptions() != null) {
                for (Entry<String, String> e : itopts.getNamedOptions().entrySet()) {
                    prompt = Shell.repeat("-", 10) + "> set " + shortClassName + " parameter " + e.getKey()
                            + ", " + e.getValue() + ": ";
                    reader.flush();
                    input = reader.readLine(prompt);
                    if (input == null) {
                        reader.println();
                        throw new IOException("Input stream closed");
                    }
                    // Places all Parameters and Values into the LocalOptions, even if the value is "".
                    // This allows us to check for "" values when setting the iterators and allows us to remove
                    // the parameter and value from the table property.
                    localOptions.put(e.getKey(), input);
                }
            }

            if (itopts.getUnnamedOptionDescriptions() != null) {
                for (String desc : itopts.getUnnamedOptionDescriptions()) {
                    reader.println(Shell.repeat("-", 10) + "> entering options: " + desc);
                    input = "start";
                    prompt = Shell.repeat("-", 10) + "> set " + shortClassName
                            + " option (<name> <value>, hit enter to skip): ";
                    while (true) {
                        reader.flush();
                        input = reader.readLine(prompt);
                        if (input == null) {
                            reader.println();
                            throw new IOException("Input stream closed");
                        } else {
                            input = new String(input);
                        }

                        if (input.length() == 0)
                            break;

                        String[] sa = input.split(" ", 2);
                        localOptions.put(sa[0], sa[1]);
                    }
                }
            }

            options.putAll(localOptions);
            if (!iterOptions.validateOptions(options))
                reader.println("invalid options for " + clazz.getName());

        } while (!iterOptions.validateOptions(options));
    } else {
        reader.flush();
        reader.println(
                "The iterator class does not implement OptionDescriber. Consider this for better iterator configuration using this setiter command.");
        iteratorName = reader.readLine("Name for iterator (enter to skip): ");
        if (null == iteratorName) {
            reader.println();
            throw new IOException("Input stream closed");
        } else if (StringUtils.isWhitespace(iteratorName)) {
            // Treat whitespace or empty string as no name provided
            iteratorName = null;
        }

        reader.flush();
        reader.println("Optional, configure name-value options for iterator:");
        String prompt = Shell.repeat("-", 10) + "> set option (<name> <value>, hit enter to skip): ";
        final HashMap<String, String> localOptions = new HashMap<String, String>();

        while (true) {
            reader.flush();
            input = reader.readLine(prompt);
            if (input == null) {
                reader.println();
                throw new IOException("Input stream closed");
            } else if (StringUtils.isWhitespace(input)) {
                break;
            }

            String[] sa = input.split(" ", 2);
            localOptions.put(sa[0], sa[1]);
        }

        options.putAll(localOptions);
    }

    return iteratorName;
}

From source file:org.apache.samza.sql.testutil.SqlFileParser.java

public static List<String> parseSqlFile(String fileName) {
    Validate.notEmpty(fileName, "fileName cannot be empty.");
    List<String> sqlLines;
    try {/*from  w w  w  . jav  a  2s . c o  m*/
        sqlLines = Files.lines(Paths.get(fileName)).collect(Collectors.toList());
    } catch (IOException e) {
        String msg = String.format("Unable to parse the sql file %s", fileName);
        LOG.error(msg, e);
        throw new SamzaException(msg, e);
    }
    List<String> sqlStmts = new ArrayList<>();
    String lastStatement = "";
    for (String sqlLine : sqlLines) {
        String sql = sqlLine.trim();
        if (sql.toLowerCase().startsWith(INSERT_CMD)) {
            if (StringUtils.isNotEmpty(lastStatement)) {
                sqlStmts.add(lastStatement);
            }

            lastStatement = sql;
        } else if (StringUtils.isNotBlank(sql) && !sql.startsWith(SQL_COMMENT_PREFIX)) {
            lastStatement = String.format("%s %s", lastStatement, sql);
        }
    }

    if (!StringUtils.isWhitespace(lastStatement)) {
        sqlStmts.add(lastStatement);
    }
    return sqlStmts;
}

From source file:org.apache.shindig.gadgets.parse.AbstractSocialMarkupHtmlParserTest.java

private void assertEmpty(Node n) {
    if (n.getChildNodes().getLength() != 0) {
        assertTrue(StringUtils.isEmpty(n.getTextContent()) || StringUtils.isWhitespace(n.getTextContent()));
    }//  ww w  .  j  ava  2 s. c o m
}

From source file:org.apache.shindig.gadgets.rewrite.CssRequestRewriter.java

/**
 * Rewrite the CSS content in a style DOM node.
 * @param styleNode Rewrite the CSS content of this node
 * @param source Uri of content//  w  w  w  .j  ava 2  s  .c om
 * @param rewriter Rewrite urls
 * @param extractImports If true remove the import statements from the output and return their
 *            referenced URIs.
 * @return Empty list of extracted import URIs.
 */
public List<String> rewrite(Element styleNode, Uri source, LinkRewriter rewriter, boolean extractImports) {
    try {
        List<Object> stylesheet = cssParser.parse(styleNode.getTextContent());
        List<String> imports = rewrite(stylesheet, source, rewriter, extractImports);
        // Write the rewritten CSS back into the element
        String content = cssParser.serialize(stylesheet);
        if (StringUtils.isEmpty(content) || StringUtils.isWhitespace(content)) {
            // Remove the owning node
            styleNode.getParentNode().removeChild(styleNode);
        } else {
            styleNode.setTextContent(content);
        }
        return imports;
    } catch (GadgetException ge) {
        if (ge.getCause() instanceof ParseException) {
            logger.log(Level.WARNING,
                    "Caja CSS parse failure: " + ge.getCause().getMessage() + " for " + source);
            return Collections.emptyList();
        } else {
            throw new RuntimeException(ge);
        }
    }
}