Example usage for org.springframework.util StringUtils countOccurrencesOf

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

Introduction

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

Prototype

public static int countOccurrencesOf(String str, String sub) 

Source Link

Document

Count the occurrences of the substring sub in string str .

Usage

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

public static boolean matchExtenderVersionRange(Bundle bundle, String header, Version versionToMatch) {
    Assert.notNull(bundle);/*from   w w w.j  a  v a  2 s  .  c  o  m*/
    // 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");
}

From source file:org.springframework.web.context.support.ServletContextResourcePatternResolver.java

/**
 * Recursively retrieve ServletContextResources that match the given pattern,
 * adding them to the given result set./*from   w  w  w  .j  av a  2s .  c om*/
 * @param servletContext the ServletContext to work on
 * @param fullPattern the pattern to match against,
 * with preprended root directory path
 * @param dir the current directory
 * @param result the Set of matching Resources to add to
 * @throws IOException if directory contents could not be retrieved
 * @see ServletContextResource
 * @see javax.servlet.ServletContext#getResourcePaths
 */
protected void doRetrieveMatchingServletContextResources(ServletContext servletContext, String fullPattern,
        String dir, Set<Resource> result) throws IOException {

    Set<String> candidates = servletContext.getResourcePaths(dir);
    if (candidates != null) {
        boolean dirDepthNotFixed = fullPattern.contains("**");
        int jarFileSep = fullPattern.indexOf(ResourceUtils.JAR_URL_SEPARATOR);
        String jarFilePath = null;
        String pathInJarFile = null;
        if (jarFileSep > 0 && jarFileSep + ResourceUtils.JAR_URL_SEPARATOR.length() < fullPattern.length()) {
            jarFilePath = fullPattern.substring(0, jarFileSep);
            pathInJarFile = fullPattern.substring(jarFileSep + ResourceUtils.JAR_URL_SEPARATOR.length());
        }
        for (String currPath : candidates) {
            if (!currPath.startsWith(dir)) {
                // Returned resource path does not start with relative directory:
                // assuming absolute path returned -> strip absolute path.
                int dirIndex = currPath.indexOf(dir);
                if (dirIndex != -1) {
                    currPath = currPath.substring(dirIndex);
                }
            }
            if (currPath.endsWith("/") && (dirDepthNotFixed || StringUtils.countOccurrencesOf(currPath,
                    "/") <= StringUtils.countOccurrencesOf(fullPattern, "/"))) {
                // Search subdirectories recursively: ServletContext.getResourcePaths
                // only returns entries for one directory level.
                doRetrieveMatchingServletContextResources(servletContext, fullPattern, currPath, result);
            }
            if (jarFilePath != null && getPathMatcher().match(jarFilePath, currPath)) {
                // Base pattern matches a jar file - search for matching entries within.
                String absoluteJarPath = servletContext.getRealPath(currPath);
                if (absoluteJarPath != null) {
                    doRetrieveMatchingJarEntries(absoluteJarPath, pathInJarFile, result);
                }
            }
            if (getPathMatcher().match(fullPattern, currPath)) {
                result.add(new ServletContextResource(servletContext, currPath));
            }
        }
    }
}