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.casa_it.CasaItUtils.java

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

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

/**
 * Helper function to replace line breaks in a string with a custom value
 * before printing.//from w w  w.j  av  a  2 s. c o  m
 * <p>
 * This method may be used by inheriting classes, if the particular format
 * does not support line breaks.
 *
 * @param value
 * value to replace
 *
 * @param lineBreak
 * value, that is used for replacement of line breaks - if null, &lt;br/&gt;
 * is used
 *
 * @return
 * value with replaced line breaks
 */
protected static String replaceLineBreaks(String value, String lineBreak) {
    value = StringUtils.trimToNull(value);
    if (value == null)
        return null;
    if (lineBreak == null)
        lineBreak = "<br/>";
    Matcher m = LINES.matcher(value);
    StringBuilder out = new StringBuilder();
    while (m.find()) {
        out.append(StringUtils.trimToEmpty(m.group()));
        if (!m.hitEnd())
            out.append(lineBreak);
    }
    return out.toString();
}

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

/**
 * Returns the value of the record at a certain index position.
 *
 * @param pos//from ww  w .  ja  va  2  s . c om
 * index position
 *
 * @param defaultValue
 * returned default value, if no value is available at the index position
 *
 * @return
 * value at index position or the provided defaultValue, if not available
 */
protected final String get(int pos, String defaultValue) {
    String value = StringUtils.trimToNull(this.values.get(pos));
    return (value != null) ? value : defaultValue;
}

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

/**
 * Sets the value of this record at a certain index position.
 *
 * @param pos//from   w ww.jav  a 2s  .c  o m
 * index position
 *
 * @param value
 * the value to set
 */
protected final void set(int pos, String value) {
    value = StringUtils.trimToNull(value);
    if (value != null)
        this.values.put(pos, value);
    else if (this.values.containsKey(pos))
        this.values.remove(pos);
}

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

/**
 * Return an ISO-2 country code from a country name.
 *
 * @param country//from  w  w  w  .ja v a  2  s .com
 * country name
 *
 * @return
 * ISO-2 country code or null, if no code was found
 */
public static String getCountryISO2(String country) {
    country = StringUtils.trimToNull(country);
    if (country == null)
        return null;
    if (country.length() == 2)
        return country;

    String[] iso2Codes = Locale.getISOCountries();
    if (country.length() == 3) {
        String iso2Code = LocaleUtils.getCountryISO2FromISO3(country);
        if (iso2Code != null)
            return iso2Code;
    }

    for (String iso2Code : iso2Codes) {
        Locale countryLocale = new Locale(iso2Code, iso2Code);
        for (Locale translationLocale : LocaleUtils.availableLocaleList()) {
            String name = StringUtils.trimToNull(countryLocale.getDisplayCountry(translationLocale));
            if (name != null && name.equalsIgnoreCase(country))
                return iso2Code;
        }
    }
    return null;
}

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

/**
 * Create an ISO-2 country code from an ISO-3 country code.
 *
 * @param iso3Code//from   w  w  w .  j a  va 2  s  .  com
 * ISO-3 country code
 *
 * @return
 * ISO-2 country code or null, if no code was found
 */
public static String getCountryISO2FromISO3(String iso3Code) {
    iso3Code = StringUtils.trimToNull(iso3Code);
    if (iso3Code == null)
        return null;
    if (iso3Code.length() == 3) {
        for (String iso2Code : Locale.getISOCountries()) {
            Locale countryLocale = new Locale(iso2Code, iso2Code);
            String countryISO3 = StringUtils.trimToNull(countryLocale.getISO3Country());
            if (countryISO3 != null && countryISO3.equalsIgnoreCase(iso3Code)) {
                return iso2Code;
            }
        }
    }
    return null;
}

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

/**
 * Return an ISO-3 country code from a country name.
 *
 * @param country//from ww w.  ja  v  a  2  s.  co m
 * country name
 *
 * @return
 * ISO-3 country code or null, if no code was found
 */
public static String getCountryISO3(String country) {
    country = StringUtils.trimToNull(country);
    if (country == null)
        return null;
    if (country.length() == 3)
        return country;

    String[] iso2Codes = Locale.getISOCountries();
    if (country.length() == 2) {
        String iso3code = LocaleUtils.getCountryISO3FromISO2(country);
        if (iso3code != null)
            return iso3code;
    }

    for (String iso2Code : iso2Codes) {
        Locale countryLocale = new Locale(iso2Code, iso2Code);
        String iso3Code = StringUtils.trimToNull(countryLocale.getISO3Country());
        if (iso3Code == null)
            continue;
        for (Locale translationLocale : LocaleUtils.availableLocaleList()) {
            String name = StringUtils.trimToNull(countryLocale.getDisplayCountry(translationLocale));
            if (name != null && name.equalsIgnoreCase(country))
                return iso3Code;
        }
    }
    return null;
}

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

/**
 * Create an ISO-3 country code from an ISO-2 country code.
 *
 * @param iso2Code/*from   w  w w  .  j a  v a  2  s .com*/
 * ISO-2 country code
 *
 * @return
 * ISO-3 country code or null, if no code was found
 */
public static String getCountryISO3FromISO2(String iso2Code) {
    iso2Code = StringUtils.trimToNull(iso2Code);
    if (iso2Code == null)
        return null;
    if (iso2Code.length() == 2) {
        Locale countryLocale = new Locale(iso2Code, iso2Code);
        String iso3Code = StringUtils.trimToNull(countryLocale.getISO3Country());
        if (iso3Code != null)
            return iso3Code;
    }
    return null;
}

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

/**
 * Return a country name in another language.
 *
 * @param country//from w ww .java  2 s.c o  m
 * country name
 *
 * @param language
 * language to translate
 *
 * @return
 * translated country name or null, if no translation was found
 */
public static String getCountryName(String country, Locale language) {
    country = StringUtils.trimToNull(country);
    if (country == null)
        return null;

    String iso2Code = LocaleUtils.getCountryISO2(country);
    if (iso2Code != null) {
        String name = StringUtils.trimToNull(new Locale(iso2Code, iso2Code).getDisplayCountry(language));
        if (name != null)
            return name;
    }
    return null;
}

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

/**
 * Translate a country name into another language.
 *
 * @param country//from  w  w w. ja v a  2s  .  c  o  m
 * country name
 *
 * @param language
 * language to translate
 *
 * @return
 * translated country name or null, if no translation was found
 */
public static String translateCountryName(String country, Locale language) {
    country = StringUtils.trimToNull(country);
    if (country == null)
        return null;
    for (String iso2Code : Locale.getISOCountries()) {
        Locale countryLocale = new Locale(iso2Code, iso2Code);
        for (Locale translationLocale : LocaleUtils.availableLocaleList()) {
            String name = StringUtils.trimToNull(countryLocale.getDisplayCountry(translationLocale));
            if (name != null && name.equalsIgnoreCase(country)) {
                name = StringUtils.trimToNull(countryLocale.getDisplayCountry(language));
                if (name != null)
                    return name;
            }
        }
    }
    return null;
}