/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/gridfs/AntPath.java

https://github.com/spring-projects/spring-data-mongodb · Java · 105 lines · 50 code · 19 blank · 36 comment · 11 complexity · e4ddc0c1fab267cdded4635860293cc7 MD5 · raw file

  1. /*
  2. * Copyright 2011-2022 the original author or authors.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * https://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.springframework.data.mongodb.gridfs;
  17. import java.util.regex.Matcher;
  18. import java.util.regex.Pattern;
  19. import org.springframework.util.Assert;
  20. /**
  21. * Value object to abstract Ant paths.
  22. *
  23. * @author Oliver Gierke
  24. * @author Mark Paluch
  25. */
  26. class AntPath {
  27. private static final String PREFIX_DELIMITER = ":";
  28. private static final Pattern WILDCARD_PATTERN = Pattern.compile("\\?|\\*\\*|\\*");
  29. private final String path;
  30. /**
  31. * Creates a new {@link AntPath} from the given path.
  32. *
  33. * @param path must not be {@literal null}.
  34. */
  35. public AntPath(String path) {
  36. Assert.notNull(path, "Path must not be null");
  37. this.path = path;
  38. }
  39. /**
  40. * Returns whether the path is a pattern.
  41. *
  42. * @return
  43. */
  44. public boolean isPattern() {
  45. String path = stripPrefix(this.path);
  46. return (path.indexOf('*') != -1 || path.indexOf('?') != -1);
  47. }
  48. private static String stripPrefix(String path) {
  49. int index = path.indexOf(PREFIX_DELIMITER);
  50. return (index > -1 ? path.substring(index + 1) : path);
  51. }
  52. /**
  53. * Returns the regular expression equivalent of this Ant path.
  54. *
  55. * @return
  56. */
  57. public String toRegex() {
  58. StringBuilder patternBuilder = new StringBuilder();
  59. Matcher m = WILDCARD_PATTERN.matcher(path);
  60. int end = 0;
  61. while (m.find()) {
  62. patternBuilder.append(quote(path, end, m.start()));
  63. String match = m.group();
  64. if ("?".equals(match)) {
  65. patternBuilder.append('.');
  66. } else if ("**".equals(match)) {
  67. patternBuilder.append(".*");
  68. } else if ("*".equals(match)) {
  69. patternBuilder.append("[^/]*");
  70. }
  71. end = m.end();
  72. }
  73. patternBuilder.append(quote(path, end, path.length()));
  74. return patternBuilder.toString();
  75. }
  76. private static String quote(String s, int start, int end) {
  77. if (start == end) {
  78. return "";
  79. }
  80. return Pattern.quote(s.substring(start, end));
  81. }
  82. @Override
  83. public String toString() {
  84. return path;
  85. }
  86. }