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

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

Introduction

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

Prototype

public static String removeStart(String str, String remove) 

Source Link

Document

Removes a substring only if it is at the begining of a source string, otherwise returns the source string.

Usage

From source file:org.sonar.ide.eclipse.internal.jdt.JavaElementsAdapterFactory.java

public Object getAdapter(Object adaptableObject, Class adapterType) {
    if (ISonarResource.class.equals(adapterType) || ISonarFile.class.equals(adapterType)
            || ISonarProject.class.equals(adapterType)) {
        return getSonarResource(adaptableObject);
    }/*from  w  w w.jav  a 2s . co m*/

    if (adapterType == Resource.class) {
        if (adaptableObject instanceof IJavaProject) {
            IJavaProject javaProject = (IJavaProject) adaptableObject;
            String key = SonarUiPlugin.getSonarProject(javaProject.getProject()).getKey();
            return new Resource().setKey(key);
        }
    } else if (adapterType == IFile.class) {
        if (adaptableObject instanceof Resource) {
            Resource resource = (Resource) adaptableObject;
            String key = resource.getKey();
            String keyWithoutBranch = key.replaceFirst("([^:]*):([^:]*):([^:]*):([^:]*)", "$1:$2:$4");
            String[] parts = StringUtils.split(keyWithoutBranch, SonarKeyUtils.PROJECT_DELIMITER);
            String groupId = parts.length > 0 ? parts[0] : "";
            String artifactId = parts.length > 1 ? parts[1] : "";
            String className = StringUtils.removeStart(parts.length > 2 ? parts[2] : "", "[default]."); //$NON-NLS-1$

            IWorkspace root = ResourcesPlugin.getWorkspace();
            // TODO this is not optimal
            for (IProject project : root.getRoot().getProjects()) {
                if (project.isAccessible()) {
                    ISonarProject sonarProject = SonarUiPlugin.getSonarProject(project);
                    if ((sonarProject != null) && StringUtils.equals(sonarProject.getGroupId(), groupId)
                            && StringUtils.equals(sonarProject.getArtifactId(), artifactId)) {
                        IJavaProject javaProject = JavaCore.create(project);
                        try {
                            IType type = javaProject.findType(className);
                            if (type == null) {
                                return null;
                            }
                            IResource result = type.getCompilationUnit().getResource();
                            return result instanceof IFile ? result : null;
                        } catch (JavaModelException e) {
                            LoggerFactory.getLogger(getClass()).warn(e.getMessage(), e);
                        }
                    }
                }
            }
            return null;
        }
    }
    return null;
}

From source file:org.sonar.ide.eclipse.jdt.internal.JavaResourceResolver.java

@Override
public IResource locate(IProject project, String resourceKey) {
    IJavaProject javaProject = JavaCore.create(project);
    if (javaProject != null) {
        try {//from  w w  w  .  j  a  v  a 2  s .  c om
            String className = StringUtils.removeStart(resourceKey, "[default]."); //$NON-NLS-1$

            IType type = javaProject.findType(className);
            if (type == null) {
                return null;
            }
            IResource result = type.getCompilationUnit().getResource();
            return result instanceof IFile ? result : null;
        } catch (JavaModelException e) {
            return null;
        }
    }
    return null;
}

From source file:org.sonar.plugins.groovy.codenarc.Converter.java

private void rule(Class<? extends AbstractRule> ruleClass, String since) throws Exception {
    if (duplications.contains(ruleClass)) {
        System.out.println("Duplicated rule " + ruleClass.getName());
    } else {/*from ww  w.  java 2s.  c o  m*/
        duplications.add(ruleClass);
    }
    AbstractRule rule = ruleClass.newInstance();
    String key = ruleClass.getCanonicalName();
    String configKey = StringUtils.removeEnd(ruleClass.getSimpleName(), "Rule");
    String name = StringUtils.join(StringUtils.splitByCharacterTypeCamelCase(configKey), ' ');
    String priorityStr = priority(rule.getPriority());
    String description = props.getProperty(configKey + ".description.html");

    SortedSet<String> params = new TreeSet<String>();

    // extract params
    String[] params1 = StringUtils.substringsBetween(description, "${", "}");
    if (params1 != null) {
        for (String param : params1) {
            description = StringUtils.remove(description, " (${" + param + "})");
            param = StringUtils.removeStart(param, "rule.");
            params.add(param);
        }
    }

    String[] params2 = StringUtils.substringsBetween(description, "<em>", "</em> property");
    if (params2 != null) {
        params.addAll(Arrays.asList(params2));
    }

    String[] params3 = StringUtils.substringsBetween(description, "configured in <em>", "</em>");
    if (params3 != null) {
        params.addAll(Arrays.asList(params3));
    }

    if (StringUtils.contains(description, "length property")) {
        params.add("length");
    }
    if (StringUtils.contains(description, "sameLine property")) {
        params.add("sameLine");
    }

    // output
    if (since != null) {
        out.println("  <!-- since " + since + " -->");
    }
    out.println("  <rule key=\"" + key + "\"" + " priority=\"" + priorityStr + "\">");
    out.println("    <name><![CDATA[" + name + "]]></name>");
    out.println("    <configKey><![CDATA[" + configKey + "]]></configKey>");
    out.println("    <description><![CDATA[" + description + "]]></description>");

    if (params != null) {
        for (String param : params) {
            out.println("    <param key=\"" + param + "\"/>");
        }
    }

    out.println("  </rule>");
    out.println();
    count++;
}

From source file:org.sonar.plugins.jira.reviews.JiraIssueCreator.java

protected RemoteIssue sendRequest(JiraSoapService jiraSoapService, String authToken, RemoteIssue issue,
        String jiraUrl, String userName) {
    try {//  ww  w. j  a va2s. co  m
        return jiraSoapService.createIssue(authToken, issue);
    } catch (RemoteAuthenticationException e) {
        throw new IllegalStateException("Impossible to connect to the JIRA server (" + jiraUrl
                + ") because of invalid credentials for user " + userName, e);
    } catch (RemotePermissionException e) {
        throw new IllegalStateException("Impossible to create the issue on the JIRA server (" + jiraUrl
                + ") because user " + userName + " does not have enough rights.", e);
    } catch (RemoteValidationException e) {
        // Unfortunately the detailed cause of the error is not in fault details (ie stack) but only in fault string
        String message = StringUtils
                .removeStart(e.getFaultString(), "com.atlassian.jira.rpc.exception.RemoteValidationException:")
                .trim();
        throw new IllegalStateException(
                "Impossible to create the issue on the JIRA server (" + jiraUrl + "): " + message, e);
    } catch (RemoteException e) {
        throw new IllegalStateException("Impossible to create the issue on the JIRA server (" + jiraUrl + ")",
                e);
    }
}

From source file:org.sonar.plugins.jira.soap.JiraSoapServiceWrapper.java

@Override
public BasicIssue createIssue(String authToken, Issue sonarIssue) {
    try {/*from  www  . j a va 2 s  . c  o  m*/
        return convertIssue(jiraSoapService.createIssue(authToken, convertIssue(sonarIssue)));
    } catch (RemoteAuthenticationException e) {
        throw new IllegalStateException(
                "Impossible to connect to the JIRA server because of invalid credentials.", e);
    } catch (RemotePermissionException e) {
        throw new IllegalStateException(
                "Impossible to create the issue on the JIRA server because user does not have enough rights.",
                e);
    } catch (RemoteValidationException e) {
        // Unfortunately the detailed cause of the error is not in fault
        // details (ie stack) but only in fault string
        String message = StringUtils
                .removeStart(e.getFaultString(), "com.atlassian.jira.rpc.exception.RemoteValidationException:")
                .trim();
        throw new IllegalStateException("Impossible to create the issue on the JIRA server: " + message, e);
    } catch (RemoteException e) {
        throw new IllegalStateException("Impossible to create the issue on the JIRA server", e);
    }
}

From source file:org.sonar.server.plugins.ClassLoaderUtils.java

/**
 * Finds directories and files within a given directory and its subdirectories.
 *
 * @param classLoader/*from ww w . j  av  a  2s  . co  m*/
 * @param rootPath    the root directory, for example org/sonar/sqale, or a file in this root directory, for example org/sonar/sqale/index.txt
 * @param
 * @return a list of relative paths, for example {"org/sonar/sqale", "org/sonar/sqale/foo", "org/sonar/sqale/foo/bar.txt}. Never null.
 */
public static Collection<String> listResources(ClassLoader classLoader, String rootPath,
        Predicate<String> predicate) {
    String jarPath = null;
    JarFile jar = null;
    try {
        Collection<String> paths = Lists.newArrayList();
        rootPath = StringUtils.removeStart(rootPath, "/");

        URL root = classLoader.getResource(rootPath);
        if (root != null) {
            checkJarFile(root);

            // Path of the root directory
            // Examples :
            // org/sonar/sqale/index.txt  -> rootDirectory is org/sonar/sqale
            // org/sonar/sqale/  -> rootDirectory is org/sonar/sqale
            // org/sonar/sqale  -> rootDirectory is org/sonar/sqale
            String rootDirectory = rootPath;
            if (StringUtils.substringAfterLast(rootPath, "/").indexOf('.') >= 0) {
                rootDirectory = StringUtils.substringBeforeLast(rootPath, "/");
            }
            jarPath = root.getPath().substring(5, root.getPath().indexOf("!")); //strip out only the JAR file
            jar = new JarFile(URLDecoder.decode(jarPath, CharEncoding.UTF_8));
            Enumeration<JarEntry> entries = jar.entries();
            while (entries.hasMoreElements()) {
                String name = entries.nextElement().getName();
                if (name.startsWith(rootDirectory) && predicate.apply(name)) {
                    paths.add(name);
                }
            }
        }
        return paths;
    } catch (Exception e) {
        throw Throwables.propagate(e);
    } finally {
        closeJar(jar, jarPath);
    }
}

From source file:org.sonar.server.startup.GenerateBootstrapIndex.java

public static List<String> getLibs(ServletContext servletContext) {
    List<String> libs = Lists.newArrayList();
    Set<String> paths = servletContext.getResourcePaths("/WEB-INF/lib");
    for (String path : paths) {
        if (StringUtils.endsWith(path, ".jar")) {
            String filename = StringUtils.removeStart(path, "/WEB-INF/lib/");
            if (!isIgnored(filename)) {
                libs.add(filename);/*  w ww  .j  a  v  a 2 s.  c o m*/
            }
        }
    }
    return libs;
}

From source file:org.sonar.updatecenter.common.PluginKeyUtils.java

public static String sanitize(String mavenArtifactId) {
    if (mavenArtifactId == null) {
        return null;
    }/*from ww w  .ja  v  a  2s  .c om*/

    String key = mavenArtifactId;
    if (StringUtils.startsWith(mavenArtifactId, "sonar-") && StringUtils.endsWith(mavenArtifactId, "-plugin")) {
        key = StringUtils.removeEnd(StringUtils.removeStart(mavenArtifactId, "sonar-"), "-plugin");
    } else if (StringUtils.endsWith(mavenArtifactId, "-sonar-plugin")) {
        key = StringUtils.removeEnd(mavenArtifactId, "-sonar-plugin");
    }
    return keepLettersAndDigits(key);
}

From source file:org.sonarsource.pluginpackaging.PluginKeyUtils.java

public static String sanitize(String mavenArtifactId) {
    String key = mavenArtifactId;
    if (StringUtils.startsWith(mavenArtifactId, SONAR_PREFIX)
            && StringUtils.endsWith(mavenArtifactId, PLUGIN_SUFFIX)) {
        key = StringUtils.removeEnd(StringUtils.removeStart(mavenArtifactId, SONAR_PREFIX), PLUGIN_SUFFIX);
    } else if (StringUtils.endsWith(mavenArtifactId, SONAR_PLUGIN_SUFFIX)) {
        key = StringUtils.removeEnd(mavenArtifactId, SONAR_PLUGIN_SUFFIX);
    }//from   w  ww. j  a v a  2 s  .  c o m
    return keepLettersAndDigits(key);
}