diff options
author | Jean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com> | 2013-10-02 11:32:00 +0200 |
---|---|---|
committer | Jean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com> | 2013-10-02 11:32:00 +0200 |
commit | 7a8f6c251214670b5bdfba3ac94bf8c866af1933 (patch) | |
tree | 8f327acc67b0b3f56238454ecb694f0d53450cbe /sonar-core | |
parent | 59afc718cbc197ed4f67f80af9ca40df4edbf51b (diff) | |
download | sonarqube-7a8f6c251214670b5bdfba3ac94bf8c866af1933.tar.gz sonarqube-7a8f6c251214670b5bdfba3ac94bf8c866af1933.zip |
Move project key validation to core
Diffstat (limited to 'sonar-core')
-rw-r--r-- | sonar-core/src/main/java/org/sonar/core/component/ComponentKeys.java | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/sonar-core/src/main/java/org/sonar/core/component/ComponentKeys.java b/sonar-core/src/main/java/org/sonar/core/component/ComponentKeys.java index 25748625070..985b64c7699 100644 --- a/sonar-core/src/main/java/org/sonar/core/component/ComponentKeys.java +++ b/sonar-core/src/main/java/org/sonar/core/component/ComponentKeys.java @@ -27,10 +27,21 @@ import org.sonar.api.resources.Scopes; public final class ComponentKeys { + /* + * Allowed characters are alphanumeric, '-', '_', '.' and ':', with at least one non-digit + */ + private static final String VALID_MODULE_KEY_REGEXP = "[\\p{Alnum}\\-_.:]*[\\p{Alpha}\\-_.:]+[\\p{Alnum}\\-_.:]*"; + private ComponentKeys() { // only static stuff } + /** + * + * @param project + * @param resource + * @return the full key of a component, based on its parent projects' key and own key + */ public static String createKey(Project project, Resource resource) { String key = resource.getKey(); if (!StringUtils.equals(Scopes.PROJECT, resource.getScope())) { @@ -44,4 +55,23 @@ public final class ComponentKeys { return key; } + /** + * <p>Test if given parameter is valid for a project/module. Valid format is:</p> + * <ul> + * <li>Allowed characters: + * <ul> + * <li>Uppercase ASCII letters A-Z</li> + * <li>Lowercase ASCII letters a-z</li> + * <li>ASCII digits 0-9</li> + * <li>Punctuation signs dash '-', underscore '_', period '.' and colon ':'</li> + * </ul> + * </li> + * <li>At least one non-digit</li> + * </ul> + * @param keyCandidate + * @return <code>true</code> if <code>keyCandidate</code> can be used for a project/module + */ + public static boolean isValidModuleKey(String keyCandidate) { + return keyCandidate.matches(VALID_MODULE_KEY_REGEXP); + } } |