Example usage for org.springframework.util StringUtils trimWhitespace

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

Introduction

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

Prototype

public static String trimWhitespace(String str) 

Source Link

Document

Trim leading and trailing whitespace from the given String .

Usage

From source file:org.entitypedia.games.common.api.handlers.DefaultExceptionDetailsResolver.java

private ExceptionDetails toExceptionDetails(String exceptionConfig) {
    String[] values = StringUtils.commaDelimitedListToStringArray(exceptionConfig);
    if (values == null || values.length == 0) {
        throw new IllegalStateException(
                "Invalid config mapping.  Exception names must map to a string configuration.");
    }/* ww w.j a  v  a  2  s . co  m*/

    ExceptionDetails result = new ExceptionDetails();

    for (String value : values) {
        String trimmedVal = StringUtils.trimWhitespace(value);

        //check to see if the value is an explicitly named key/value pair:
        String[] pair = StringUtils.split(trimmedVal, "=");
        if (pair != null) {
            //explicit attribute set:
            String pairKey = StringUtils.trimWhitespace(pair[0]);
            if (!StringUtils.hasText(pairKey)) {
                pairKey = null;
            }
            String pairValue = StringUtils.trimWhitespace(pair[1]);
            if (!StringUtils.hasText(pairValue)) {
                pairValue = null;
            }
            if ("status".equalsIgnoreCase(pairKey)) {
                result.setStatus(getRequiredInt(pairKey, pairValue));
            } else if ("msg".equalsIgnoreCase(pairKey)) {
                result.setErrorMessage(pairValue);
            } else if ("emsg".equalsIgnoreCase(pairKey)) {
                result.setExplanationMessage(pairValue);
            } else if ("wmsg".equalsIgnoreCase(pairKey)) {
                result.setWhatToDoMessage(pairValue);
            } else if ("infoUrl".equalsIgnoreCase(pairKey)) {
                result.setMoreInfoUrl(pairValue);
            } else if ("target".equalsIgnoreCase(pairKey)) {
                result.setExceptionClass(pairValue);
            }
        }
    }

    return result;
}

From source file:org.hdiv.config.xml.ConfigBeanDefinitionParser.java

private List convertToList(String data) {
    String[] result = data.split(",");
    List list = new ArrayList();
    // clean the edges of the item - spaces/returns/tabs etc may be used for readability in the configs
    for (int i = 0; i < result.length; i++) {
        // trims leading and trailing whitespace
        list.add(StringUtils.trimWhitespace(result[i]));
    }// w ww .j av a2s.  c o  m
    return list;
}

From source file:se.alingsas.alfresco.repo.utils.byggreda.ByggRedaUtil.java

/**
 * Create new or return existing folder/*from   w w w. ja  va2 s  . c  om*/
 * 
 * @param filepath
 * @param site
 * @return
 */
private NodeRef createFolder(final String basePath, final String folderPath, final String folderTitles,
        final SiteInfo site) {
    NodeRef rootNodeRef = fileFolderService.searchSimple(site.getNodeRef(), SiteService.DOCUMENT_LIBRARY);

    String[] parts = StringUtils.delimitedListToStringArray(basePath, "/");

    for (String part : parts) {
        part = StringUtils.trimWhitespace(part);

        NodeRef folder = fileFolderService.searchSimple(rootNodeRef, part);

        while (folder == null) {
            folder = fileFolderService.create(rootNodeRef, part, ContentModel.TYPE_FOLDER).getNodeRef();
            nodeService.setProperty(folder, ContentModel.PROP_TITLE, part);
        }

        rootNodeRef = folder;
    }
    if (folderPath != null) {
        parts = StringUtils.delimitedListToStringArray(folderPath, "/");
        String[] titles;
        if (folderTitles != null) {
            titles = StringUtils.delimitedListToStringArray(folderTitles, "#/#");
        } else {
            titles = new String[0];
        }
        for (int i = 0; i < parts.length; i++) {
            String part = parts[i];
            String title;
            if (titles.length >= i + 1) {
                title = titles[i];
            } else {
                title = part;
            }
            part = StringUtils.trimWhitespace(part);

            NodeRef folder = fileFolderService.searchSimple(rootNodeRef, part);

            while (folder == null) {
                folder = fileFolderService.create(rootNodeRef, part, ContentModel.TYPE_FOLDER).getNodeRef();
                nodeService.setProperty(folder, ContentModel.PROP_TITLE, title);
            }

            rootNodeRef = folder;
        }
    }
    return rootNodeRef;
}

From source file:ch.rasc.extclassgenerator.ModelGenerator.java

static String trimToNull(String str) {
    String trimmedStr = StringUtils.trimWhitespace(str);
    if (StringUtils.hasLength(trimmedStr)) {
        return trimmedStr;
    }//from  ww  w .  j a v  a  2  s . c  o  m
    return null;
}

From source file:org.cloudfoundry.identity.uaa.scim.bootstrap.ScimGroupBootstrap.java

private void setCombinedGroups() {
    this.groups = new HashMap<>();
    this.groups.putAll(this.defaultUserGroups);
    this.groups.putAll(this.nonDefaultUserGroups);

    this.configuredGroups.entrySet().stream()
            .filter(e -> StringUtils.hasText(e.getValue()) || !groups.containsKey(e.getKey()))
            .forEach(e -> groups.put(e.getKey(), e.getValue()));

    this.groups = this.groups.entrySet().stream().collect(new MapCollector<>(
            e -> StringUtils.trimWhitespace(e.getKey()), e -> StringUtils.trimWhitespace(e.getValue())));
}

From source file:org.eclipse.gemini.blueprint.extender.support.internal.ConfigUtils.java

public static boolean matchExtenderVersionRange(Bundle bundle, String header, Version versionToMatch) {
    Assert.notNull(bundle);//from ww w .j  a v  a2 s. c  o  m
    // get version range
    String range = bundle.getHeaders().get(header);

    boolean trace = log.isTraceEnabled();

    // empty value = empty version = *
    if (!StringUtils.hasText(range))
        return true;

    if (trace)
        log.trace("discovered " + header + " header w/ value=" + range);

    // do we have a range or not ?
    range = StringUtils.trimWhitespace(range);

    // a range means one comma
    int commaNr = StringUtils.countOccurrencesOf(range, COMMA);

    // no comma, no intervals
    if (commaNr == 0) {
        Version version = Version.parseVersion(range);

        return versionToMatch.equals(version);
    }

    if (commaNr == 1) {

        // sanity check
        if (!((range.startsWith(LEFT_CLOSED_INTERVAL) || range.startsWith(LEFT_OPEN_INTERVAL))
                && (range.endsWith(RIGHT_CLOSED_INTERVAL) || range.endsWith(RIGHT_OPEN_INTERVAL)))) {
            throw new IllegalArgumentException("range [" + range + "] is invalid");
        }

        boolean equalMin = range.startsWith(LEFT_CLOSED_INTERVAL);
        boolean equalMax = range.endsWith(RIGHT_CLOSED_INTERVAL);

        // remove interval brackets
        range = range.substring(1, range.length() - 1);

        // split the remaining string in two pieces
        String[] pieces = StringUtils.split(range, COMMA);

        if (trace)
            log.trace("discovered low/high versions : " + ObjectUtils.nullSafeToString(pieces));

        Version minVer = Version.parseVersion(pieces[0]);
        Version maxVer = Version.parseVersion(pieces[1]);

        if (trace)
            log.trace("comparing version " + versionToMatch + " w/ min=" + minVer + " and max=" + maxVer);

        boolean result = true;

        int compareMin = versionToMatch.compareTo(minVer);

        if (equalMin)
            result = (result && (compareMin >= 0));
        else
            result = (result && (compareMin > 0));

        int compareMax = versionToMatch.compareTo(maxVer);

        if (equalMax)
            result = (result && (compareMax <= 0));
        else
            result = (result && (compareMax < 0));

        return result;
    }

    // more then one comma means incorrect range

    throw new IllegalArgumentException("range [" + range + "] is invalid");
}

From source file:org.springframework.data.gemfire.support.SpringContextBootstrappingInitializer.java

/**
 * Initializes a Spring ApplicationContext with the given parameters specified with a GemFire &lt;initializer&gt;
 * block in cache.xml.//from  www . j av a 2s  .  c  o  m
 *
 * @param parameters a Properties object containing the configuration parameters and settings defined in the
 * GemFire cache.xml &lt;initializer&gt; block for the declared SpringContextBootstrappingInitializer
 * GemFire Declarable object.
 * @throws org.springframework.context.ApplicationContextException if the Spring ApplicationContext could not be
 * successfully created, configured and initialized.
 * @see #createApplicationContext(String[], String[])
 * @see #initApplicationContext(org.springframework.context.ConfigurableApplicationContext)
 * @see #refreshApplicationContext(org.springframework.context.ConfigurableApplicationContext)
 * @see java.util.Properties
 */
@Override
public void init(final Properties parameters) {
    try {
        synchronized (SpringContextBootstrappingInitializer.class) {
            if (applicationContext == null || !applicationContext.isActive()) {
                String basePackages = parameters.getProperty(BASE_PACKAGES_PARAMETER);
                String contextConfigLocations = parameters.getProperty(CONTEXT_CONFIG_LOCATIONS_PARAMETER);

                String[] basePackagesArray = StringUtils.delimitedListToStringArray(
                        StringUtils.trimWhitespace(basePackages), COMMA_DELIMITER, CHARS_TO_DELETE);

                String[] contextConfigLocationsArray = StringUtils.delimitedListToStringArray(
                        StringUtils.trimWhitespace(contextConfigLocations), COMMA_DELIMITER, CHARS_TO_DELETE);

                ConfigurableApplicationContext localApplicationContext = refreshApplicationContext(
                        initApplicationContext(
                                createApplicationContext(basePackagesArray, contextConfigLocationsArray)));

                Assert.state(localApplicationContext.isRunning(), String.format(
                        "The Spring ApplicationContext (%1$s) failed to be properly initialized with the context config files (%2$s) or base packages (%3$s)!",
                        nullSafeGetApplicationContextId(localApplicationContext),
                        Arrays.toString(contextConfigLocationsArray), Arrays.toString(basePackagesArray)));

                applicationContext = localApplicationContext;
            }
        }
    } catch (Throwable cause) {
        String message = "Failed to bootstrap the Spring ApplicationContext!";
        logger.error(message, cause);
        throw new ApplicationContextException(message, cause);
    }
}

From source file:org.springframework.osgi.extender.support.internal.ConfigUtils.java

public static boolean matchExtenderVersionRange(Bundle bundle, String header, Version versionToMatch) {
    Assert.notNull(bundle);/* w  w  w.ja v  a  2 s  . c  om*/
    // get version range
    String range = (String) bundle.getHeaders().get(header);

    boolean trace = log.isTraceEnabled();

    // empty value = empty version = *
    if (!StringUtils.hasText(range))
        return true;

    if (trace)
        log.trace("discovered " + header + " header w/ value=" + range);

    // do we have a range or not ?
    range = StringUtils.trimWhitespace(range);

    // a range means one comma
    int commaNr = StringUtils.countOccurrencesOf(range, COMMA);

    // no comma, no intervals
    if (commaNr == 0) {
        Version version = Version.parseVersion(range);

        return versionToMatch.equals(version);
    }

    if (commaNr == 1) {

        // sanity check
        if (!((range.startsWith(LEFT_CLOSED_INTERVAL) || range.startsWith(LEFT_OPEN_INTERVAL))
                && (range.endsWith(RIGHT_CLOSED_INTERVAL) || range.endsWith(RIGHT_OPEN_INTERVAL)))) {
            throw new IllegalArgumentException("range [" + range + "] is invalid");
        }

        boolean equalMin = range.startsWith(LEFT_CLOSED_INTERVAL);
        boolean equalMax = range.endsWith(RIGHT_CLOSED_INTERVAL);

        // remove interval brackets
        range = range.substring(1, range.length() - 1);

        // split the remaining string in two pieces
        String[] pieces = StringUtils.split(range, COMMA);

        if (trace)
            log.trace("discovered low/high versions : " + ObjectUtils.nullSafeToString(pieces));

        Version minVer = Version.parseVersion(pieces[0]);
        Version maxVer = Version.parseVersion(pieces[1]);

        if (trace)
            log.trace("comparing version " + versionToMatch + " w/ min=" + minVer + " and max=" + maxVer);

        boolean result = true;

        int compareMin = versionToMatch.compareTo(minVer);

        if (equalMin)
            result = (result && (compareMin >= 0));
        else
            result = (result && (compareMin > 0));

        int compareMax = versionToMatch.compareTo(maxVer);

        if (equalMax)
            result = (result && (compareMax <= 0));
        else
            result = (result && (compareMax < 0));

        return result;
    }

    // more then one comma means incorrect range

    throw new IllegalArgumentException("range [" + range + "] is invalid");
}