You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ComponentKeys.java 3.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2019 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.core.component;
  21. import javax.annotation.Nullable;
  22. import org.apache.commons.lang.StringUtils;
  23. import static com.google.common.base.Preconditions.checkArgument;
  24. public final class ComponentKeys {
  25. public static final int MAX_COMPONENT_KEY_LENGTH = 400;
  26. /*
  27. * Must not be blank or empty
  28. */
  29. private static final String VALID_PROJECT_KEY_REGEXP = "[^\\p{javaWhitespace}]+";
  30. /*
  31. * Allowed characters are alphanumeric, '-', '_', '.' and '/'
  32. */
  33. private static final String VALID_BRANCH_REGEXP = "[\\p{Alnum}\\-_./]*";
  34. private static final String KEY_WITH_BRANCH_FORMAT = "%s:%s";
  35. private ComponentKeys() {
  36. // only static stuff
  37. }
  38. public static String createEffectiveKey(String projectKey, @Nullable String path) {
  39. StringBuilder sb = new StringBuilder(MAX_COMPONENT_KEY_LENGTH);
  40. sb.append(projectKey);
  41. if (path != null) {
  42. sb.append(':').append(path);
  43. }
  44. return sb.toString();
  45. }
  46. /**
  47. * Test if given parameter is valid for a project. A key is valid if it doesn't contain whitespaces.
  48. *
  49. * @return <code>true</code> if <code>keyCandidate</code> can be used for a project
  50. */
  51. public static boolean isValidProjectKey(String keyCandidate) {
  52. return keyCandidate.matches(VALID_PROJECT_KEY_REGEXP);
  53. }
  54. /**
  55. * Checks if given parameter is valid for a project following {@link #isValidProjectKey(String)} contract.
  56. *
  57. * @throws IllegalArgumentException if the format is incorrect
  58. */
  59. public static void checkProjectKey(String keyCandidate) {
  60. checkArgument(isValidProjectKey(keyCandidate), "Malformed key for '%s'. %s", keyCandidate, "Project key cannot be empty nor contain whitespaces.");
  61. }
  62. /**
  63. * Return the project key with potential branch
  64. */
  65. public static String createKey(String keyWithoutBranch, @Nullable String branch) {
  66. if (StringUtils.isNotBlank(branch)) {
  67. return String.format(KEY_WITH_BRANCH_FORMAT, keyWithoutBranch, branch);
  68. } else {
  69. return keyWithoutBranch;
  70. }
  71. }
  72. public static String createKey(String projectKey, @Nullable String path, @Nullable String branch) {
  73. String key = createKey(projectKey, branch);
  74. return createEffectiveKey(key, path);
  75. }
  76. }