Example usage for org.apache.commons.lang StringUtils substringAfterLast

List of usage examples for org.apache.commons.lang StringUtils substringAfterLast

Introduction

In this page you can find the example usage for org.apache.commons.lang StringUtils substringAfterLast.

Prototype

public static String substringAfterLast(String str, String separator) 

Source Link

Document

Gets the substring after the last occurrence of a separator.

Usage

From source file:org.gradle.build.docs.dsl.docbook.LinkRenderer.java

private Node addType(String className, GenerationListener listener) {
    if (model.isKnownType(className)) {
        Element linkElement = document.createElement("apilink");
        linkElement.setAttribute("class", className);
        return linkElement;
    }/*from w  w w  .  j ava2s  . c  o m*/

    if (primitiveTypes.contains(className)) {
        Element classNameElement = document.createElement("classname");
        classNameElement.appendChild(document.createTextNode(className));
        return classNameElement;
    }

    if (className.startsWith("java.")) {
        Element linkElement = document.createElement("ulink");
        linkElement.setAttribute("url", String.format(
                "http://download.oracle.com/javase/1.5.0/docs/api/%s.html", className.replace(".", "/")));
        Element classNameElement = document.createElement("classname");
        classNameElement.appendChild(document.createTextNode(StringUtils.substringAfterLast(className, ".")));
        linkElement.appendChild(classNameElement);
        return linkElement;
    }

    if (className.startsWith("groovy.")) {
        Element linkElement = document.createElement("ulink");
        linkElement.setAttribute("url",
                String.format("http://groovy.codehaus.org/gapi/%s.html", className.replace(".", "/")));
        Element classNameElement = document.createElement("classname");
        classNameElement.appendChild(document.createTextNode(StringUtils.substringAfterLast(className, ".")));
        linkElement.appendChild(classNameElement);
        return linkElement;
    }

    //this if is a bit cheesy but 1-letter classname surely means a generic type and the warning will be useless
    if (className.length() > 1) {
        listener.warning(String.format("Could not generate link for unknown class '%s'", className));
    }
    Element element = document.createElement("classname");
    element.appendChild(document.createTextNode(className));
    return element;
}

From source file:org.gradle.build.docs.dsl.JavadocLinkConverter.java

private String doResolve(String link, ClassMetaData classMetaData) {
    if (link.contains(".")) {
        return link;
    }//  w ww.  j  a  v a 2s  .c o m

    for (String importedClass : classMetaData.getImports()) {
        String baseName = StringUtils.substringAfterLast(importedClass, ".");
        if (baseName.equals("*")) {
            String candidateClassName = StringUtils.substringBeforeLast(importedClass, ".") + "." + link;
            if (metaDataRepository.findClass(candidateClassName) != null) {
                return candidateClassName;
            }
        } else if (link.equals(baseName)) {
            return importedClass;
        }
    }

    String candidateClassName = StringUtils.substringBeforeLast(classMetaData.getClassName(), ".") + "." + link;
    if (metaDataRepository.findClass(candidateClassName) != null) {
        return candidateClassName;
    }

    return null;
}

From source file:org.gradle.execution.TaskNameResolvingBuildExecuter.java

private static Map<String, Collection<Task>> doSelect(GradleInternal gradle, Iterable<String> paths) {
    Project defaultProject = gradle.getDefaultProject();

    Map<String, Collection<Task>> allProjectsTasksByName = null;

    Map<String, Collection<Task>> matches = new LinkedHashMap<String, Collection<Task>>();
    for (String path : paths) {
        Map<String, Collection<Task>> tasksByName;
        String baseName;/*  w  ww . ja va  2  s.co  m*/
        String prefix;

        if (path.contains(Project.PATH_SEPARATOR)) {
            prefix = StringUtils.substringBeforeLast(path, Project.PATH_SEPARATOR);
            prefix = prefix.length() == 0 ? Project.PATH_SEPARATOR : prefix;
            Project project = defaultProject.findProject(prefix);
            if (project == null) {
                throw new TaskSelectionException(
                        String.format("Project '%s' not found in %s.", prefix, defaultProject));
            }
            baseName = StringUtils.substringAfterLast(path, Project.PATH_SEPARATOR);
            Task match = project.getTasks().findByName(baseName);
            if (match != null) {
                matches.put(path, Collections.singleton(match));
                continue;
            }

            tasksByName = new HashMap<String, Collection<Task>>();
            for (Task task : project.getTasks().getAll()) {
                tasksByName.put(task.getName(), Collections.singleton(task));
            }
            prefix = prefix + Project.PATH_SEPARATOR;

        } else {
            Set<Task> tasks = defaultProject.getTasksByName(path, true);
            if (!tasks.isEmpty()) {
                matches.put(path, tasks);
                continue;
            }
            if (allProjectsTasksByName == null) {
                allProjectsTasksByName = buildTaskMap(defaultProject);
            }
            tasksByName = allProjectsTasksByName;
            baseName = path;
            prefix = "";
        }

        Pattern pattern = getPatternForName(baseName);
        Set<String> patternCandidates = new TreeSet<String>();
        Set<String> typoCandidates = new TreeSet<String>();
        for (String candidate : tasksByName.keySet()) {
            if (pattern.matcher(candidate).matches()) {
                patternCandidates.add(candidate);
            }
            if (StringUtils.getLevenshteinDistance(baseName.toUpperCase(), candidate.toUpperCase()) <= Math
                    .min(3, baseName.length() / 2)) {
                typoCandidates.add(candidate);
            }
        }
        if (patternCandidates.size() == 1) {
            String actualName = patternCandidates.iterator().next();
            matches.put(prefix + actualName, tasksByName.get(actualName));
            continue;
        }

        if (!patternCandidates.isEmpty()) {
            throw new TaskSelectionException(String.format("Task '%s' is ambiguous in %s. Candidates are: %s.",
                    baseName, defaultProject, GUtil.toString(patternCandidates)));
        }
        if (!typoCandidates.isEmpty()) {
            throw new TaskSelectionException(
                    String.format("Task '%s' not found in %s. Some candidates are: %s.", baseName,
                            defaultProject, GUtil.toString(typoCandidates)));
        }
        throw new TaskSelectionException(String.format("Task '%s' not found in %s.", baseName, defaultProject));
    }

    return matches;
}

From source file:org.gradle.execution.taskpath.TaskPathResolver.java

/**
 * @param path the task path, e.g. 'someTask', 'sT', ':sT', ':foo:bar:sT'
 * @param startFrom the starting project the task should be found recursively
 * @return resolved task path/*from   w  w  w .  jav a  2  s .c  o  m*/
 */
public ResolvedTaskPath resolvePath(String path, ProjectInternal startFrom) {
    ProjectInternal project;
    String taskName; //eg. 'someTask' or 'sT'
    String prefix; //eg. '', ':' or ':foo:bar'

    if (ResolvedTaskPath.isQualified(path)) {
        project = projectFinder.findProject(path, startFrom);
        taskName = StringUtils.substringAfterLast(path, Project.PATH_SEPARATOR);
        prefix = project.getPath() + Project.PATH_SEPARATOR;
    } else {
        project = startFrom;
        taskName = path;
        prefix = "";
    }
    return new ResolvedTaskPath(path, prefix, taskName, project);
}

From source file:org.gradle.execution.TaskSelector.java

public void selectTasks(GradleInternal gradle, String path) {
    SetMultimap<String, Task> tasksByName;
    String baseName;/*ww w .j  av a 2 s. c  om*/
    String prefix;

    ProjectInternal project = gradle.getDefaultProject();

    if (path.contains(Project.PATH_SEPARATOR)) {
        String projectPath = StringUtils.substringBeforeLast(path, Project.PATH_SEPARATOR);
        projectPath = projectPath.length() == 0 ? Project.PATH_SEPARATOR : projectPath;
        project = findProject(project, projectPath);
        baseName = StringUtils.substringAfterLast(path, Project.PATH_SEPARATOR);
        prefix = project.getPath() + Project.PATH_SEPARATOR;

        tasksByName = taskNameResolver.select(baseName, project);
    } else {
        baseName = path;
        prefix = "";

        tasksByName = taskNameResolver.selectAll(path, project);
    }

    Set<Task> tasks = tasksByName.get(baseName);
    if (!tasks.isEmpty()) {
        // An exact match
        this.tasks = tasks;
        this.taskName = path;
        return;
    }

    NameMatcher matcher = new NameMatcher();
    String actualName = matcher.find(baseName, tasksByName.keySet());

    if (actualName != null) {
        // A partial match
        this.tasks = tasksByName.get(actualName);
        this.taskName = prefix + actualName;
        return;
    }

    throw new TaskSelectionException(matcher.formatErrorMessage("task", project));
}

From source file:org.gradle.groovy.scripts.AbstractUriScriptSource.java

/**
 * Returns the class name for use for this script source.  The name is intended to be unique to support mapping
 * class names to source files even if many sources have the same file name (e.g. build.gradle).
 *//*from   w ww.  ja va 2s  .  c o  m*/
public String getClassName() {
    if (className == null) {
        URI sourceUri = getResource().getURI();
        String name = StringUtils.substringBeforeLast(StringUtils.substringAfterLast(sourceUri.toString(), "/"),
                ".");
        StringBuilder className = new StringBuilder(name.length());
        for (int i = 0; i < name.length(); i++) {
            char ch = name.charAt(i);
            if (Character.isJavaIdentifierPart(ch)) {
                className.append(ch);
            } else {
                className.append('_');
            }
        }
        if (!Character.isJavaIdentifierStart(className.charAt(0))) {
            className.insert(0, '_');
        }
        className.setLength(Math.min(className.length(), 30));
        className.append('_');
        String path = sourceUri.toString();
        className.append(HashUtil.createCompactMD5(path));

        this.className = className.toString();
    }

    return className;
}

From source file:org.gradle.groovy.scripts.UriScriptSource.java

/**
 * Returns the class name for use for this script source.  The name is intended to be unique to support mapping
 * class names to source files even if many sources have the same file name (e.g. build.gradle).
 *//*from   ww w .  ja  v  a 2  s  .c o  m*/
public String getClassName() {
    if (className == null) {
        URI sourceUri = resource.getURI();
        String name = StringUtils.substringBeforeLast(StringUtils.substringAfterLast(sourceUri.toString(), "/"),
                ".");
        StringBuilder className = new StringBuilder(name.length());
        for (int i = 0; i < name.length(); i++) {
            char ch = name.charAt(i);
            if (Character.isJavaIdentifierPart(ch)) {
                className.append(ch);
            } else {
                className.append('_');
            }
        }
        if (!Character.isJavaIdentifierStart(className.charAt(0))) {
            className.insert(0, '_');
        }
        className.setLength(Math.min(className.length(), 30));
        className.append('_');
        String path = sourceUri.toString();
        className.append(HashUtil.createCompactMD5(path));

        this.className = className.toString();
    }

    return className;
}

From source file:org.gradle.performance.results.ReportGenerator.java

private void copyResource(String resourceName, File outputDirectory) {
    URL resource = getClass().getClassLoader().getResource("org/gradle/reporting/" + resourceName);
    String dir = StringUtils.substringAfterLast(resourceName, ".");
    GFileUtils.copyURLToFile(resource, new File(outputDirectory, dir + "/" + resourceName));
}

From source file:org.gradle.test.fixtures.file.AbstractTestDirectoryProvider.java

private String determinePrefix() {
    StackTraceElement[] stackTrace = new RuntimeException().getStackTrace();
    for (StackTraceElement element : stackTrace) {
        if (element.getClassName().endsWith("Test") || element.getClassName().endsWith("Spec")) {
            return StringUtils.substringAfterLast(element.getClassName(), ".") + "/unknown-test";
        }//  www.ja v a 2s . c  o m
    }
    return "unknown-test-class";
}

From source file:org.gradle.test.fixtures.file.TestNameTestDirectoryProvider.java

private String determinePrefix() {
    StackTraceElement[] stackTrace = new RuntimeException().getStackTrace();
    for (StackTraceElement element : stackTrace) {
        if (element.getClassName().endsWith("Test") || element.getClassName().endsWith("Spec")) {
            return StringUtils.substringAfterLast(element.getClassName(), ".") + "/unknown-test-"
                    + testCounter.getAndIncrement();
        }//ww w  .j a  va  2  s  . c om
    }
    return "unknown-test-class-" + testCounter.getAndIncrement();
}