Example usage for org.springframework.util StringUtils hasText

List of usage examples for org.springframework.util StringUtils hasText

Introduction

In this page you can find the example usage for org.springframework.util StringUtils hasText.

Prototype

public static boolean hasText(@Nullable String str) 

Source Link

Document

Check whether the given String contains actual text.

Usage

From source file:com.seajas.search.utilities.web.WebResourceLocators.java

/**
 * Gets a resource locator.//  w  ww .  j a v a  2s .c o  m
 * 
 * @param uri
 * @throws URISyntaxException
 */
public static URI parseURI(String uri) throws URISyntaxException {
    if (!StringUtils.hasText(uri))
        throw new URISyntaxException("", "No content");
    uri = uri.trim();

    // Escape illegal characters

    for (int i = 0; i < uri.length(); ++i) {
        char c = uri.charAt(i);
        if (ASCII_ESCAPE_SET.indexOf(c) >= 0)
            uri = String.format("%s%%%02X%s", uri.substring(0, i), c & 0xFF, uri.substring(i + 1));
    }

    // Parse XML entities

    for (int start = uri.indexOf('&'); start >= 0; start = uri.indexOf('&', start + 1)) {
        int end = uri.indexOf(';', start);
        if (end < 0)
            break;
        String entity = uri.substring(start + 1, end);

        String replacement = null;
        if (entity.startsWith("#")) {
            try {
                int codepoint = entity.startsWith("#x") ? Integer.parseInt(entity.substring(2), 16)
                        : Integer.parseInt(entity.substring(1));
                if (codepoint <= 0xFF)
                    replacement = String.format("%%%02X", codepoint);
                else
                    replacement = new String(Character.toChars(codepoint));
            } catch (Exception e) {
                logger.trace("Unparseable numeric entity.", e);
            }
        } else {
            replacement = xmlEntityPercentEscapes.get(entity);
        }

        if (replacement != null) {
            if (logger.isDebugEnabled())
                logger.debug(String.format("Replaced entity %s with %s for %s", entity, replacement, uri));
            uri = uri.substring(0, start) + replacement + uri.substring(end + 1);
        } else if (logger.isDebugEnabled()) {
            logger.debug(String.format("Skiped entity %s for %s", entity, uri));
        }
    }

    // Multiple fragments

    while (uri.indexOf('#') != uri.lastIndexOf('#')) {
        int i = uri.lastIndexOf('#');

        // Escape last hash mark

        uri = uri.substring(0, i) + "%23" + uri.substring(i + 1);
    }

    return new URI(uri).normalize();
}

From source file:org.terasoluna.gfw.web.util.JspTagUtils.java

/**
 * Convert to the Boolean value.// w  ww .  j av a  2  s  .c  om
 * @param attributeValue string value of attribute
 * @param defaultValue If attribute value is not text(null or blank or whitespace only), apply this value.
 * @param attributeName attribute name (If attribute value is not true or false, sets in exception message)
 * @return converted Boolean value
 * @throws JspTagException If value that is not true or false is specified.
 */
public static boolean toBoolean(String attributeValue, boolean defaultValue, String attributeName)
        throws JspTagException {
    if (StringUtils.hasText(attributeValue)) {
        if ("true".equalsIgnoreCase(attributeValue) || "false".equalsIgnoreCase(attributeValue)) {
            return Boolean.parseBoolean(attributeValue);
        } else {
            throw new JspTagException("The value of " + attributeName + " must be either true or false.");
        }
    }
    return defaultValue;
}

From source file:com.ge.predix.uaa.token.lib.JsonUtils.java

public static <T> T readValue(final String s, final Class<T> clazz) throws JsonUtilException {
    try {/*from  w ww.  j  a va  2 s .  c  o  m*/
        if (StringUtils.hasText(s)) {
            return objectMapper.readValue(s, clazz);
        } else {
            return null;
        }
    } catch (IOException e) {
        throw new JsonUtilException(e);
    }
}

From source file:fr.mby.saml2.sp.api.core.SamlBindingEnum.java

/**
 * Load the enum from a bidning URI./*w w  w .  j ava2 s  . c o  m*/
 * 
 * @param samlUri the binding URI
 * @return the matching enum
 */
public static SamlBindingEnum fromSamlUri(final String samlUri) {
    SamlBindingEnum result = null;

    if (StringUtils.hasText(samlUri)) {
        for (SamlBindingEnum val : SamlBindingEnum.values()) {
            if (samlUri.equals(val.getUri())) {
                result = val;
            }
        }
    }

    return result;
}

From source file:io.pivotal.gemfire.main.GemFireServerApplication.java

static String systemProperty(String propertyName, String defaultValue) {
    String propertyValue = System.getProperty(propertyName);
    return (StringUtils.hasText(propertyValue) ? propertyValue : defaultValue);
}

From source file:org.openmrs.module.fbregistrationapp.converter.StringToPersonAddressConverter.java

@Override
public PersonAddress convert(String uuid) {
    if (StringUtils.hasText(uuid))
        return Context.getPersonService().getPersonAddressByUuid(uuid);

    return null;//from w w  w .  j a va2 s  .  c  o m
}

From source file:io.pivotal.spring.xd.jdbcgpfdist.support.SqlUtils.java

public static String createExternalReadableTable(LoadConfiguration config, String prefix,
        List<String> overrideLocations) {

    // TODO: this function needs a cleanup
    StringBuilder buf = new StringBuilder();

    // unique table name

    String name = quotesEscape(config.getTable() + "_ext_" + prefix);
    buf.append("CREATE READABLE EXTERNAL TABLE ");
    buf.append(name);//  ww  w .  j av  a 2s .  c  o  m
    buf.append(" ( ");

    // column types or like
    ReadableTable externalTable = config.getExternalTable();
    if (externalTable.getLike() != null) {
        buf.append("LIKE ");
        buf.append(quotesEscape(config.getTable()));
    } else if (StringUtils.hasText(externalTable.getColumns())) {
        buf.append(externalTable.getColumns());
    } else {
        buf.append("LIKE ");
        buf.append(quotesEscape(config.getTable()));
    }
    buf.append(" ) ");

    // locations
    buf.append("LOCATION (");
    if (overrideLocations != null && !overrideLocations.isEmpty()) {
        buf.append(createLocationString(overrideLocations.toArray(new String[0])));
    } else {
        buf.append(createLocationString(externalTable.getLocations().toArray(new String[0])));
    }
    buf.append(") ");

    // format type
    if (externalTable.getFormat() == Format.TEXT) {
        buf.append("FORMAT 'TEXT'");
    } else {
        buf.append("FORMAT 'CSV'");
    }

    // format parameters
    buf.append(" ( ");
    buf.append("DELIMITER '");
    if (externalTable.getDelimiter() != null) {
        buf.append(unicodeEscaped(externalTable.getDelimiter().charValue()));
    } else {
        buf.append("|");
    }
    buf.append("'");

    if (externalTable.getNullString() != null) {
        buf.append(" NULL '");
        buf.append(externalTable.getNullString());
        buf.append("'");
    }

    if (externalTable.getEscape() != null) {
        buf.append(" ESCAPE '");
        buf.append(externalTable.getEscape());
        buf.append("'");
    }

    if (externalTable.getQuote() != null) {
        buf.append(" QUOTE '");
        buf.append(externalTable.getQuote());
        buf.append("'");
    }

    if (externalTable.getForceQuote() != null) {
        buf.append(" FORCE QUOTE ");
        buf.append(StringUtils.arrayToCommaDelimitedString(externalTable.getForceQuote()));
    }

    if (externalTable.isFormatHeader()) {
        buf.append(" HEADER ");
    }

    buf.append(" )");

    if (externalTable.getEncoding() != null) {
        buf.append(" ENCODING '");
        buf.append(externalTable.getEncoding());
        buf.append("'");
    }

    if (externalTable.getSegmentRejectLimit() != null) {
        if (externalTable.getLogErrorsInto() != null) {
            buf.append(" LOG ERRORS INTO ");
            buf.append(externalTable.getLogErrorsInto());
        }
        buf.append(" SEGMENT REJECT LIMIT ");
        buf.append(externalTable.getSegmentRejectLimit());
        buf.append(" ");

    }

    return buf.toString();
}

From source file:org.kmnet.com.fw.web.util.JspTagUtils.java

/**
 * Convert to the Boolean value./* www  . j  ava2  s . co  m*/
 * @param attributeValue string value of attribute
 * @param defaultValue If attribute value is not text(null or blank or whitespace only), apply this value.
 * @param attributeName attribute name (If attribute value is not true or false, sets in exception message)
 * @return converted Boolean value
 * @throws JspTagException If value that is not true or false is specified.
 */
public static boolean toBoolean(String attributeValue, boolean defaultValue, String attributeName)
        throws JspTagException {
    if (StringUtils.hasText(attributeValue)) {
        if (attributeValue.equalsIgnoreCase("true") || attributeValue.equalsIgnoreCase("false")) {
            return Boolean.parseBoolean(attributeValue);
        } else {
            throw new JspTagException("The value of " + attributeName + " must be either true or false.");
        }
    }
    return defaultValue;
}

From source file:spring.osgi.utils.OsgiHeaderUtils.java

private static String[] getHeaderAsTrimmedStringArray(Bundle bundle, String header) {
    if (bundle == null || !StringUtils.hasText(header))
        return new String[0];

    String headerContent = bundle.getHeaders().get(header);
    String[] entries = StringUtils.commaDelimitedListToStringArray(headerContent);
    for (int i = 0; i < entries.length; i++) {
        entries[i] = entries[i].trim();//from  w ww. j a  v  a2 s  .c  o  m
    }

    return entries;
}

From source file:org.zilverline.util.Utils.java

/**
 * Does an 'ELF-proof' on given ISBN number.
 * /*from   w w  w  . ja v a 2  s  .c  o m*/
 * @param ISBNnumber the givenNumber
 * 
 * @return true if so
 */
public static boolean isValidISBNNumber(final String ISBNnumber) {
    // check whether this is a valid ISBN number: see http://www.isbn-international.org/en/userman/chapter4.html
    if (!StringUtils.hasText(ISBNnumber)) {
        log.debug("invalid ISBN: " + ISBNnumber);
        return false;
    }
    // remove all non-valid ISBN characters (0-9xX and - seem valid) and the -
    String number = ISBNnumber.replaceAll("[^0-9xX]", "");
    if (number.length() != 10) {
        log.debug("invalid ISBN: " + number);
        return false;
    }
    char checkDigit = number.charAt(9);
    int checkInt;
    if (checkDigit == 'X') {
        checkInt = 10;
    } else {
        checkInt = Integer.parseInt("" + checkDigit);
    }
    String ISBNnumberPrefix = number.substring(0, 9);

    // elf proof
    int total = checkInt;
    for (int i = 0; i < 9; i++) {
        int k = Integer.parseInt("" + ISBNnumberPrefix.charAt(i));
        total += (10 - i) * k;
    }
    if ((total % 11) != 0) {
        log.debug("invalid ISBN, not ELF proof: " + number);
        return false;
    }
    log.debug("valid ISBN: " + number);

    return true;
}