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.

FileNameMatcher.java 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. /*
  2. * Copyright (C) 2008, Florian Koeberle <florianskarten@web.de>
  3. * Copyright (C) 2008, Florian Köberle <florianskarten@web.de>
  4. * and other copyright owners as documented in the project's IP log.
  5. *
  6. * This program and the accompanying materials are made available
  7. * under the terms of the Eclipse Distribution License v1.0 which
  8. * accompanies this distribution, is reproduced below, and is
  9. * available at http://www.eclipse.org/org/documents/edl-v10.php
  10. *
  11. * All rights reserved.
  12. *
  13. * Redistribution and use in source and binary forms, with or
  14. * without modification, are permitted provided that the following
  15. * conditions are met:
  16. *
  17. * - Redistributions of source code must retain the above copyright
  18. * notice, this list of conditions and the following disclaimer.
  19. *
  20. * - Redistributions in binary form must reproduce the above
  21. * copyright notice, this list of conditions and the following
  22. * disclaimer in the documentation and/or other materials provided
  23. * with the distribution.
  24. *
  25. * - Neither the name of the Eclipse Foundation, Inc. nor the
  26. * names of its contributors may be used to endorse or promote
  27. * products derived from this software without specific prior
  28. * written permission.
  29. *
  30. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  31. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  32. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  33. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  34. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  35. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  36. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  37. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  38. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  39. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  40. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  41. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  42. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  43. */
  44. package org.eclipse.jgit.fnmatch;
  45. import java.util.ArrayList;
  46. import java.util.Collections;
  47. import java.util.List;
  48. import java.util.ListIterator;
  49. import java.util.regex.Matcher;
  50. import java.util.regex.Pattern;
  51. import org.eclipse.jgit.errors.InvalidPatternException;
  52. import org.eclipse.jgit.errors.NoClosingBracketException;
  53. /**
  54. * This class can be used to match filenames against fnmatch like patterns. It
  55. * is not thread save.
  56. * <p>
  57. * Supported are the wildcard characters * and ? and groups with:
  58. * <ul>
  59. * <li>characters e.g. [abc]</li>
  60. * <li>ranges e.g. [a-z]</li>
  61. * <li>the following character classes
  62. * <ul>
  63. * <li>[:alnum:]</li>
  64. * <li>[:alpha:]</li>
  65. * <li>[:blank:]</li>
  66. * <li>[:cntrl:]</li>
  67. * <li>[:digit:]</li>
  68. * <li>[:graph:]</li>
  69. * <li>[:lower:]</li>
  70. * <li>[:print:]</li>
  71. * <li>[:punct:]</li>
  72. * <li>[:space:]</li>
  73. * <li>[:upper:]</li>
  74. * <li>[:word:]</li>
  75. * <li>[:xdigit:]</li>
  76. * </ul>
  77. * e. g. [[:xdigit:]]</li>
  78. * </ul>
  79. * Any character can be escaped by prepending it with a \
  80. */
  81. public class FileNameMatcher {
  82. static final List<Head> EMPTY_HEAD_LIST = Collections.emptyList();
  83. private static final Pattern characterClassStartPattern = Pattern
  84. .compile("\\[[.:=]"); //$NON-NLS-1$
  85. private List<Head> headsStartValue;
  86. private List<Head> heads;
  87. /**
  88. * {{@link #extendStringToMatchByOneCharacter(char)} needs a list for the
  89. * new heads, allocating a new array would be bad for the performance, as
  90. * the method gets called very often.
  91. *
  92. */
  93. private List<Head> listForLocalUseage;
  94. /**
  95. *
  96. * @param headsStartValue
  97. * must be a list which will never be modified.
  98. */
  99. private FileNameMatcher(List<Head> headsStartValue) {
  100. this(headsStartValue, headsStartValue);
  101. }
  102. /**
  103. *
  104. * @param headsStartValue
  105. * must be a list which will never be modified.
  106. * @param heads
  107. * a list which will be cloned and then used as current head
  108. * list.
  109. */
  110. private FileNameMatcher(final List<Head> headsStartValue,
  111. final List<Head> heads) {
  112. this.headsStartValue = headsStartValue;
  113. this.heads = new ArrayList<>(heads.size());
  114. this.heads.addAll(heads);
  115. this.listForLocalUseage = new ArrayList<>(heads.size());
  116. }
  117. /**
  118. * Constructor for FileNameMatcher
  119. *
  120. * @param patternString
  121. * must contain a pattern which fnmatch would accept.
  122. * @param invalidWildgetCharacter
  123. * if this parameter isn't null then this character will not
  124. * match at wildcards(* and ? are wildcards).
  125. * @throws org.eclipse.jgit.errors.InvalidPatternException
  126. * if the patternString contains a invalid fnmatch pattern.
  127. */
  128. public FileNameMatcher(final String patternString,
  129. final Character invalidWildgetCharacter)
  130. throws InvalidPatternException {
  131. this(createHeadsStartValues(patternString, invalidWildgetCharacter));
  132. }
  133. /**
  134. * A Copy Constructor which creates a new
  135. * {@link org.eclipse.jgit.fnmatch.FileNameMatcher} with the same state and
  136. * reset point like <code>other</code>.
  137. *
  138. * @param other
  139. * another {@link org.eclipse.jgit.fnmatch.FileNameMatcher}
  140. * instance.
  141. */
  142. public FileNameMatcher(FileNameMatcher other) {
  143. this(other.headsStartValue, other.heads);
  144. }
  145. private static List<Head> createHeadsStartValues(
  146. final String patternString, final Character invalidWildgetCharacter)
  147. throws InvalidPatternException {
  148. final List<AbstractHead> allHeads = parseHeads(patternString,
  149. invalidWildgetCharacter);
  150. List<Head> nextHeadsSuggestion = new ArrayList<>(2);
  151. nextHeadsSuggestion.add(LastHead.INSTANCE);
  152. for (int i = allHeads.size() - 1; i >= 0; i--) {
  153. final AbstractHead head = allHeads.get(i);
  154. // explanation:
  155. // a and * of the pattern "a*b"
  156. // need *b as newHeads
  157. // that's why * extends the list for it self and it's left neighbor.
  158. if (head.isStar()) {
  159. nextHeadsSuggestion.add(head);
  160. head.setNewHeads(nextHeadsSuggestion);
  161. } else {
  162. head.setNewHeads(nextHeadsSuggestion);
  163. nextHeadsSuggestion = new ArrayList<>(2);
  164. nextHeadsSuggestion.add(head);
  165. }
  166. }
  167. return nextHeadsSuggestion;
  168. }
  169. private static int findGroupEnd(final int indexOfStartBracket,
  170. final String pattern) throws InvalidPatternException {
  171. int firstValidCharClassIndex = indexOfStartBracket + 1;
  172. int firstValidEndBracketIndex = indexOfStartBracket + 2;
  173. if (indexOfStartBracket + 1 >= pattern.length())
  174. throw new NoClosingBracketException(indexOfStartBracket, "[", "]", //$NON-NLS-1$ //$NON-NLS-2$
  175. pattern);
  176. if (pattern.charAt(firstValidCharClassIndex) == '!') {
  177. firstValidCharClassIndex++;
  178. firstValidEndBracketIndex++;
  179. }
  180. final Matcher charClassStartMatcher = characterClassStartPattern
  181. .matcher(pattern);
  182. int groupEnd = -1;
  183. while (groupEnd == -1) {
  184. final int possibleGroupEnd = indexOfUnescaped(pattern, ']',
  185. firstValidEndBracketIndex);
  186. if (possibleGroupEnd == -1)
  187. throw new NoClosingBracketException(indexOfStartBracket, "[", //$NON-NLS-1$
  188. "]", pattern); //$NON-NLS-1$
  189. final boolean foundCharClass = charClassStartMatcher
  190. .find(firstValidCharClassIndex);
  191. if (foundCharClass
  192. && charClassStartMatcher.start() < possibleGroupEnd) {
  193. final String classStart = charClassStartMatcher.group(0);
  194. final String classEnd = classStart.charAt(1) + "]"; //$NON-NLS-1$
  195. final int classStartIndex = charClassStartMatcher.start();
  196. final int classEndIndex = pattern.indexOf(classEnd,
  197. classStartIndex + 2);
  198. if (classEndIndex == -1)
  199. throw new NoClosingBracketException(classStartIndex,
  200. classStart, classEnd, pattern);
  201. firstValidCharClassIndex = classEndIndex + 2;
  202. firstValidEndBracketIndex = firstValidCharClassIndex;
  203. } else {
  204. groupEnd = possibleGroupEnd;
  205. }
  206. }
  207. return groupEnd;
  208. }
  209. private static List<AbstractHead> parseHeads(final String pattern,
  210. final Character invalidWildgetCharacter)
  211. throws InvalidPatternException {
  212. int currentIndex = 0;
  213. List<AbstractHead> heads = new ArrayList<>();
  214. while (currentIndex < pattern.length()) {
  215. final int groupStart = indexOfUnescaped(pattern, '[', currentIndex);
  216. if (groupStart == -1) {
  217. final String patternPart = pattern.substring(currentIndex);
  218. heads.addAll(createSimpleHeads(patternPart,
  219. invalidWildgetCharacter));
  220. currentIndex = pattern.length();
  221. } else {
  222. final String patternPart = pattern.substring(currentIndex,
  223. groupStart);
  224. heads.addAll(createSimpleHeads(patternPart,
  225. invalidWildgetCharacter));
  226. final int groupEnd = findGroupEnd(groupStart, pattern);
  227. final String groupPart = pattern.substring(groupStart + 1,
  228. groupEnd);
  229. heads.add(new GroupHead(groupPart, pattern));
  230. currentIndex = groupEnd + 1;
  231. }
  232. }
  233. return heads;
  234. }
  235. private static List<AbstractHead> createSimpleHeads(
  236. final String patternPart, final Character invalidWildgetCharacter) {
  237. final List<AbstractHead> heads = new ArrayList<>(
  238. patternPart.length());
  239. boolean escaped = false;
  240. for (int i = 0; i < patternPart.length(); i++) {
  241. final char c = patternPart.charAt(i);
  242. if (escaped) {
  243. final CharacterHead head = new CharacterHead(c);
  244. heads.add(head);
  245. escaped = false;
  246. } else {
  247. switch (c) {
  248. case '*': {
  249. final AbstractHead head = createWildCardHead(
  250. invalidWildgetCharacter, true);
  251. heads.add(head);
  252. break;
  253. }
  254. case '?': {
  255. final AbstractHead head = createWildCardHead(
  256. invalidWildgetCharacter, false);
  257. heads.add(head);
  258. break;
  259. }
  260. case '\\':
  261. escaped = true;
  262. break;
  263. default:
  264. final CharacterHead head = new CharacterHead(c);
  265. heads.add(head);
  266. }
  267. }
  268. }
  269. return heads;
  270. }
  271. private static AbstractHead createWildCardHead(
  272. final Character invalidWildgetCharacter, final boolean star) {
  273. if (invalidWildgetCharacter != null) {
  274. return new RestrictedWildCardHead(invalidWildgetCharacter
  275. .charValue(), star);
  276. }
  277. return new WildCardHead(star);
  278. }
  279. /**
  280. * @param c new character to append
  281. * @return true to continue, false if the matcher can stop appending
  282. */
  283. private boolean extendStringToMatchByOneCharacter(char c) {
  284. final List<Head> newHeads = listForLocalUseage;
  285. newHeads.clear();
  286. List<Head> lastAddedHeads = null;
  287. for (int i = 0; i < heads.size(); i++) {
  288. final Head head = heads.get(i);
  289. final List<Head> headsToAdd = head.getNextHeads(c);
  290. // Why the next performance optimization isn't wrong:
  291. // Some times two heads return the very same list.
  292. // We save future effort if we don't add these heads again.
  293. // This is the case with the heads "a" and "*" of "a*b" which
  294. // both can return the list ["*","b"]
  295. if (headsToAdd != lastAddedHeads) {
  296. if (!headsToAdd.isEmpty())
  297. newHeads.addAll(headsToAdd);
  298. lastAddedHeads = headsToAdd;
  299. }
  300. }
  301. listForLocalUseage = heads;
  302. heads = newHeads;
  303. return !newHeads.isEmpty();
  304. }
  305. private static int indexOfUnescaped(final String searchString,
  306. final char ch, final int fromIndex) {
  307. for (int i = fromIndex; i < searchString.length(); i++) {
  308. char current = searchString.charAt(i);
  309. if (current == ch)
  310. return i;
  311. if (current == '\\')
  312. i++; // Skip the next char as it is escaped }
  313. }
  314. return -1;
  315. }
  316. /**
  317. * Append to the string which is matched against the patterns of this class
  318. *
  319. * @param stringToMatch
  320. * extends the string which is matched against the patterns of
  321. * this class.
  322. */
  323. public void append(String stringToMatch) {
  324. for (int i = 0; i < stringToMatch.length(); i++) {
  325. final char c = stringToMatch.charAt(i);
  326. if (!extendStringToMatchByOneCharacter(c))
  327. break;
  328. }
  329. }
  330. /**
  331. * Resets this matcher to it's state right after construction.
  332. */
  333. public void reset() {
  334. heads.clear();
  335. heads.addAll(headsStartValue);
  336. }
  337. /**
  338. * Create a {@link org.eclipse.jgit.fnmatch.FileNameMatcher} instance which
  339. * uses the same pattern like this matcher, but has the current state of
  340. * this matcher as reset and start point
  341. *
  342. * @return a {@link org.eclipse.jgit.fnmatch.FileNameMatcher} instance which
  343. * uses the same pattern like this matcher, but has the current
  344. * state of this matcher as reset and start point.
  345. */
  346. public FileNameMatcher createMatcherForSuffix() {
  347. final List<Head> copyOfHeads = new ArrayList<>(heads.size());
  348. copyOfHeads.addAll(heads);
  349. return new FileNameMatcher(copyOfHeads);
  350. }
  351. /**
  352. * Whether the matcher matches
  353. *
  354. * @return whether the matcher matches
  355. */
  356. public boolean isMatch() {
  357. if (heads.isEmpty())
  358. return false;
  359. final ListIterator<Head> headIterator = heads
  360. .listIterator(heads.size());
  361. while (headIterator.hasPrevious()) {
  362. final Head head = headIterator.previous();
  363. if (head == LastHead.INSTANCE) {
  364. return true;
  365. }
  366. }
  367. return false;
  368. }
  369. /**
  370. * Whether a match can be appended
  371. *
  372. * @return a boolean.
  373. */
  374. public boolean canAppendMatch() {
  375. for (int i = 0; i < heads.size(); i++) {
  376. if (heads.get(i) != LastHead.INSTANCE) {
  377. return true;
  378. }
  379. }
  380. return false;
  381. }
  382. }