Example usage for org.apache.commons.lang3 StringUtils trimToNull

List of usage examples for org.apache.commons.lang3 StringUtils trimToNull

Introduction

In this page you can find the example usage for org.apache.commons.lang3 StringUtils trimToNull.

Prototype

public static String trimToNull(final String str) 

Source Link

Document

Removes control characters (char <= 32) from both ends of this String returning null if the String is empty ("") after the trim or if it is null .

Usage

From source file:org.openestate.io.core.NumberUtils.java

/**
 * Convert a string value into a number.
 *
 * @param value/*from ww  w. j  a v a 2  s  . c om*/
 * the string value to convert
 *
 * @param integerOnly
 * wether only the integer part of the value is parsed
 *
 * @param locales
 * locales, against which the value is parsed
 *
 * @return
 * numeric value for the string
 *
 * @throws NumberFormatException
 * if the string is not a valid number
 */
public static Number parseNumber(String value, boolean integerOnly, Locale... locales)
        throws NumberFormatException {
    value = StringUtils.trimToNull(value);
    if (value == null)
        return null;

    // ignore leading plus sign
    if (value.startsWith("+"))
        value = StringUtils.trimToNull(value.substring(1));

    // remove any spaces
    value = StringUtils.replace(value, StringUtils.SPACE, StringUtils.EMPTY);

    if (ArrayUtils.isEmpty(locales))
        locales = new Locale[] { Locale.getDefault() };

    for (Locale locale : locales) {
        // check, if the value is completely numeric for the locale
        if (!isNumeric(value, locale))
            continue;
        try {
            NumberFormat format = NumberFormat.getNumberInstance(locale);
            format.setMinimumFractionDigits(0);
            format.setGroupingUsed(false);
            format.setParseIntegerOnly(integerOnly);
            return format.parse(value);
        } catch (ParseException ex) {
        }
    }
    throw new NumberFormatException("The provided value '" + value + "' is not numeric!");
}

From source file:org.openestate.io.core.XmlUtils.java

/**
 * Recursively remove any comments and unnecessary white spaces from a
 * {@link Node} and its children.//from   www. j  ava 2  s .  co m
 *
 * @param node
 * the node to clean
 */
public static void clean(Node node) {
    NodeList childNodes = node.getChildNodes();
    for (int n = childNodes.getLength() - 1; n >= 0; n--) {
        Node child = childNodes.item(n);
        short nodeType = child.getNodeType();
        if (nodeType == Node.ELEMENT_NODE) {
            XmlUtils.clean(child);
        } else if (nodeType == Node.COMMENT_NODE) {
            node.removeChild(child);
        } else if (nodeType == Node.TEXT_NODE) {
            String value = StringUtils.trimToNull(child.getNodeValue());
            if (value == null)
                node.removeChild(child);
            else
                child.setNodeValue(value);
        }
    }
}

From source file:org.openestate.io.core.XmlUtils.java

private static Calendar parseDate(String value, boolean tryDateTimeOnError) {
    value = StringUtils.trimToNull(value);
    if (value == null)
        return null;
    try {/* ww w  .  j a  v  a  2s. c o m*/
        return DatatypeConverter.parseDate(value);
    } catch (IllegalArgumentException ex) {
        //LOGGER.warn( "Can't parse value '" + value + "' as date!" );
        //LOGGER.warn( "> " + ex.getLocalizedMessage(), ex );
    }
    try {
        Date date = DateUtils.parseDateStrictly(value,
                new String[] { "dd.MM.yyyy", "dd.MM.yy", "dd/MM/yyyy", "dd/MM/yy", "dd-MM-yyyy", "dd-MMM-yyyy",
                        "yyyy-MM-dd", "yyyy/MM/dd", "yyyy-D", "MM/yyyy", "MMM yyyy", "MMMMM yyyy", "yyyy" });
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return cal;
    } catch (ParseException ex) {
        //LOGGER.warn( "Can't parse value '" + value + "' as date!" );
        //LOGGER.warn( "> " + ex.getLocalizedMessage(), ex );
    }

    // try to parse the value as xsd:dateTime instead
    if (tryDateTimeOnError) {
        try {
            return XmlUtils.parseDateTime(value, false);
        } catch (IllegalArgumentException ex) {
            //LOGGER.warn( "Can't parse value '" + value + "' as datetime!" );
            //LOGGER.warn( "> " + ex.getLocalizedMessage(), ex );
        }
    }

    throw new IllegalArgumentException("Can't parse date value '" + value + "'!");
}

From source file:org.openestate.io.core.XmlUtils.java

private static Calendar parseDateTime(String value, boolean tryDateOnError) {
    value = StringUtils.trimToNull(value);
    if (value == null)
        return null;
    try {/*from ww w.ja  v a2 s . c  o  m*/
        return DatatypeConverter.parseDateTime(value);
    } catch (IllegalArgumentException ex) {
        //LOGGER.warn( "Can't parse value '" + value + "' as datetime!" );
        //LOGGER.warn( "> " + ex.getLocalizedMessage(), ex );
    }
    try {
        Date date = DateUtils.parseDateStrictly(value, new String[] { "yyyy-MM-dd'T'HH:mm:ss",
                "yyyy-MM-dd'T'HH:mm", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm" });
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return cal;
    } catch (ParseException ex) {
        //LOGGER.warn( "Can't parse value '" + value + "' as datetime!" );
        //LOGGER.warn( "> " + ex.getLocalizedMessage(), ex );
    }

    // try to parse the value as xsd:date instead
    if (tryDateOnError) {
        try {
            return XmlUtils.parseDate(value, false);
        } catch (IllegalArgumentException ex) {
            //LOGGER.warn( "Can't parse value '" + value + "' as datetime!" );
            //LOGGER.warn( "> " + ex.getLocalizedMessage(), ex );
        }
    }

    throw new IllegalArgumentException("Can't parse date-time value '" + value + "'!");
}

From source file:org.openestate.io.daft_ie.DaftIeDocument.java

@Override
public DaftIeVersion getDocumentVersion() {
    String version;/*  w ww . j a v a  2 s. c o  m*/
    try {
        Document doc = this.getDocument();
        version = StringUtils.trimToNull(XmlUtils.newXPath("/io:daft/@version", doc).stringValueOf(doc));
        if (version == null) {
            LOGGER.warn("Can't find version informations in the XML document!");
            //System.out.println( "----------------------------" );
            //try
            //{
            //  DocumentUtils.write( doc, System.out );
            //}
            //catch (Exception ex)
            //{
            //  LOGGER.error( "Can't write XML document!" );
            //  LOGGER.error( "> " + ex.getLocalizedMessage(), ex );
            //}
            //System.out.println( "----------------------------" );
            return null;
        }
    } catch (JaxenException ex) {
        LOGGER.error("Can't evaluate XPath expression!");
        LOGGER.error("> " + ex.getLocalizedMessage(), ex);
        return null;
    }

    DaftIeVersion v = DaftIeVersion.detectFromString(version);
    if (v != null)
        return v;

    LOGGER.warn("The provided version (" + version + ") is not supported!");
    return null;
}

From source file:org.openestate.io.daft_ie.DaftIeUtils.java

public static String parseCountry(String value) {
    return StringUtils.trimToNull(value);
}

From source file:org.openestate.io.daft_ie.DaftIeUtils.java

public static BigDecimal parsePositiveDecimal(String value) {
    value = StringUtils.trimToNull(value);
    return (value != null) ? DatatypeConverter.parseDecimal(value) : null;
}

From source file:org.openestate.io.daft_ie.DaftIeUtils.java

public static BigInteger parsePositiveInteger(String value) {
    value = StringUtils.trimToNull(value);
    return (value != null) ? DatatypeConverter.parseInteger(value) : null;
}

From source file:org.openestate.io.daft_ie.DaftIeUtils.java

public static URL parseURL(String value) {
    value = StringUtils.trimToNull(value);
    if (value == null)
        return null;
    try {/*from   w w w.  j a v  a 2s  . c  om*/
        if (!StringUtils.startsWithIgnoreCase(value, "http://")
                && !StringUtils.startsWithIgnoreCase(value, "https://"))
            return new URL("http://" + value);
        else
            return new URL(value);
    } catch (MalformedURLException ex) {
        throw new IllegalArgumentException("Can't parse URL value '" + value + "'!", ex);
    }
}

From source file:org.openestate.io.daft_ie.DaftIeUtils.java

public static String printCountry(String value) {
    value = StringUtils.trimToNull(value);
    if (value == null)
        throw new IllegalArgumentException("Can't print country value!");

    String country = StringUtils.trimToNull(DaftIeUtils.getCountryName(value));
    if (country == null) {
        LOGGER.warn("Can't convert country '" + value + "' to its english name!");
        return value;
    } else {// ww w . ja v a2  s  . com
        return country;
    }
}