Example usage for java.lang String isEmpty

List of usage examples for java.lang String isEmpty

Introduction

In this page you can find the example usage for java.lang String isEmpty.

Prototype

public boolean isEmpty() 

Source Link

Document

Returns true if, and only if, #length() is 0 .

Usage

From source file:Main.java

/**
 * Get the first word (pure alphabet word) from the String
 * //  w w  w.java 2  s. c  o  m
 * @param source
 *          the string where the first word is to be extracted
 * @return the extracted first word or null if there is no first word
 */
public static String getFirstWord(String source) {
    if (source == null) {
        return null;
    }

    source = source.trim();

    if (source.isEmpty()) {
        return null;
    }

    if (containsSpace(source)) {

        final int FIRST_WORD_GROUP = 1;
        String firstWordRegex = "^[^A-z]*([A-z]+)";
        Pattern pattern = Pattern.compile(firstWordRegex);
        Matcher matcher = pattern.matcher(source);
        if (matcher.find()) {
            return matcher.group(FIRST_WORD_GROUP);
        } else {
            return null;
        }
    } else {
        return source;
    }
}

From source file:zipkin.sparkstreaming.autoconfigure.stream.kafka.ZipkinKafkaStreamFactoryProperties.java

private static String emptyToNull(String s) {
    return (s != null && !s.isEmpty()) ? s : null;
}

From source file:de.dfki.resc28.ole.bootstrap.Util.java

public static String appendSegmentToPath(String path, String segment) {
    boolean segmentStartsWithSlash = !segment.isEmpty() && segment.charAt(0) == '/';

    if (path == null || path.isEmpty()) {
        return segmentStartsWithSlash ? segment : "/" + segment;
    }/*from w  w  w  .  j av a  2  s  .  c om*/

    if (path.charAt(path.length() - 1) == '/') {
        return segmentStartsWithSlash ? path + segment.substring(1) : path + segment;
    }

    return segmentStartsWithSlash ? path + segment : path + "/" + segment;
}

From source file:Main.java

/**
 * This method ensures that the output String has only valid XML unicode
 * characters as specified by the XML 1.0 standard. For reference, please
 * see/*  w w w.  ja  va 2s .  c o m*/
 * <a href="http://www.w3.org/TR/2000/REC-xml-20001006#NT-Char">the
 * standard</a>. This method will return an empty String if the input is
 * null or empty.
 *
 * @param in The String whose non-valid characters we want to remove.
 * @return The in String, stripped of non-valid characters.
 */
public static String cleanInvalidXmlChars(String text) {

    if (null == text || text.isEmpty()) {
        return text;
    }

    final int len = text.length();
    char current = 0;
    int codePoint = 0;
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < len; i++) {
        current = text.charAt(i);
        boolean surrogate = false;
        if (Character.isHighSurrogate(current) && i + 1 < len && Character.isLowSurrogate(text.charAt(i + 1))) {
            surrogate = true;
            codePoint = text.codePointAt(i++);
        } else {
            codePoint = current;
        }
        if ((codePoint == 0x9) || (codePoint == 0xA) || (codePoint == 0xD)
                || ((codePoint >= 0x20) && (codePoint <= 0xD7FF))
                || ((codePoint >= 0xE000) && (codePoint <= 0xFFFD))
                || ((codePoint >= 0x10000) && (codePoint <= 0x10FFFF))) {
            sb.append(current);

            if (surrogate) {
                sb.append(text.charAt(i));
            }
        } else {

            // 
            // Invalid Char at index transformed into hex 
            //System.err.println("Index=["+ i +"] Char=["+ String.format("%04x", (int)text.charAt(i)) +"] CodePoint=[" + codePoint + "]");
            //sb.append("hex"+String.format("%04x", (int)text.charAt(i)));
        }
    }

    return sb.toString();
}

From source file:com.jaeksoft.searchlib.util.NetworksUtils.java

public final static SubnetInfo[] getSubnetArray(String ips) throws IOException {
    if (ips == null)
        return null;
    StringReader sr = new StringReader(ips);
    try {/*from  www.j a v  a  2 s.  c  o  m*/
        List<String> lines = IOUtils.readLines(sr);
        if (lines == null)
            return null;
        SubnetInfo[] subnetArray = new SubnetInfo[lines.size()];
        int i = 0;
        for (String line : lines) {
            line = line.trim();
            if (line.isEmpty())
                continue;
            if (line.indexOf('/') == -1)
                line = line + "/32";
            SubnetUtils subnetUtils = new SubnetUtils(line);
            subnetUtils.setInclusiveHostCount(true);
            subnetArray[i++] = subnetUtils.getInfo();
        }
        return subnetArray;
    } finally {
        IOUtils.closeQuietly(sr);
    }
}

From source file:grails.plugins.sitemapper.ValidationUtils.java

public static void assertVideoTitle(String title) {
    if (title == null || title.isEmpty())
        throw new SitemapperException("Title not specified");
}

From source file:Main.java

/**
 * Creates an XIInclude element with a link to a file
 *
 * @param doc  The DOM Document to create the xi:include for.
 * @param file The file name/path to link to.
 * @return An xi:include element that can be used to include content from another file.
 *//*from w  ww  .  j av  a 2 s.c  o  m*/
public static Element createXIInclude(final Document doc, final String file) {
    try {
        // Encode the file reference
        final StringBuilder fixedFileRef = new StringBuilder();
        final String[] split = file.split("/");
        for (int i = 0; i < split.length; i++) {
            if (i != 0) {
                fixedFileRef.append("/");
            }

            final String uriPart = split[i];
            if (uriPart.isEmpty() || uriPart.equals("..") || uriPart.equals(".")) {
                fixedFileRef.append(uriPart);
            } else {
                fixedFileRef.append(URLEncoder.encode(uriPart, "UTF-8"));
            }
        }

        // If the file ref ended with "/" then it wouldn't have been added as their was no content after it
        if (file.endsWith("/")) {
            fixedFileRef.append("/");
        }

        final Element xiInclude = doc.createElementNS("http://www.w3.org/2001/XInclude", "xi:include");
        xiInclude.setAttribute("href", fixedFileRef.toString());
        xiInclude.setAttribute("xmlns:xi", "http://www.w3.org/2001/XInclude");

        return xiInclude;
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static SimpleDateFormat getStartDateTimeInTimezone(String timezone) {
    SimpleDateFormat format = new SimpleDateFormat("EEEE, MMMM d, h:mm a");
    if (timezone == null || timezone.isEmpty()) {
        format.setTimeZone(TimeZone.getDefault());
    } else//  w w w  .  jav a 2s  .c  o  m
        format.setTimeZone(TimeZone.getTimeZone("GMT+" + timezone));
    return format;
}

From source file:Main.java

public static SimpleDateFormat getServerDateTimeInTimezone(String timezone) {

    SimpleDateFormat format = new SimpleDateFormat("EEE, d MMM yyyy");
    if (timezone == null || timezone.isEmpty()) {
        format.setTimeZone(TimeZone.getDefault());
    } else {//from  w  w  w. j a v a2  s . co m
        format.setTimeZone(TimeZone.getTimeZone("GMT+" + timezone));
    }
    return format;
}

From source file:Main.java

/**
 * Get nodelist from XML document using xpath expression
 * //w ww. j av a2 s  . c o m
 * @param doc
 * @param expression
 * @return
 * @throws XPathExpressionException
 */
public static NodeList getNodeList(final Document doc, final String expression)
        throws XPathExpressionException {
    if (null == expression || expression.isEmpty() || null == doc) {
        return null;
    }
    log.finer("Executing xpath xpression " + expression);
    XPath xPath = XPathFactory.newInstance().newXPath();
    XPathExpression expr = xPath.compile(expression);
    Object result = expr.evaluate(doc, XPathConstants.NODESET);
    return (NodeList) result;
}