comparison hg4j/src/main/java/org/tmatesoft/hg/internal/PathGlobMatcher.java @ 213:6ec4af642ba8 gradle

Project uses Gradle for build - actual changes
author Alexander Kitaev <kitaev@gmail.com>
date Tue, 10 May 2011 10:52:53 +0200
parents
children
comparison
equal deleted inserted replaced
212:edb2e2829352 213:6ec4af642ba8
1 /*
2 * Copyright (c) 2011 TMate Software Ltd
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * For information on how to redistribute this software under
14 * the terms of a license other than GNU General Public License
15 * contact TMate Software at support@hg4j.com
16 */
17 package org.tmatesoft.hg.internal;
18
19 import java.util.regex.PatternSyntaxException;
20
21 import org.tmatesoft.hg.util.Path;
22
23 /**
24 *
25 * @author Artem Tikhomirov
26 * @author TMate Software Ltd.
27 */
28 public class PathGlobMatcher implements Path.Matcher {
29
30 private final PathRegexpMatcher delegate;
31
32 /**
33 *
34 * @param globPatterns
35 * @throws NullPointerException if argument is null
36 * @throws IllegalArgumentException if any of the patterns is not valid
37 */
38 public PathGlobMatcher(String... globPatterns) {
39 String[] regexp = new String[globPatterns.length]; //deliberately let fail with NPE
40 int i = 0;
41 for (String s : globPatterns) {
42 regexp[i] = glob2regexp(s);
43 }
44 try {
45 delegate = new PathRegexpMatcher(regexp);
46 } catch (PatternSyntaxException ex) {
47 ex.printStackTrace();
48 throw new IllegalArgumentException(ex);
49 }
50 }
51
52
53 // HgIgnore.glob2regex is similar, but IsIgnore solves slightly different task
54 // (need to match partial paths, e.g. for glob 'bin' shall match not only 'bin' folder, but also any path below it,
55 // which is not generally the case
56 private static String glob2regexp(String glob) {
57 int end = glob.length() - 1;
58 boolean needLineEndMatch = glob.charAt(end) != '*';
59 while (end > 0 && glob.charAt(end) == '*') end--; // remove trailing * that are useless for Pattern.find()
60 StringBuilder sb = new StringBuilder(end*2);
61 if (glob.charAt(0) != '*') {
62 sb.append('^');
63 }
64 for (int i = 0; i <= end; i++) {
65 char ch = glob.charAt(i);
66 if (ch == '*') {
67 if (glob.charAt(i+1) == '*') { // i < end because we've stripped any trailing * earlier
68 // any char, including path segment separator
69 sb.append(".*?");
70 i++;
71 } else {
72 // just path segments
73 sb.append("[^/]*?");
74 }
75 continue;
76 } else if (ch == '?') {
77 sb.append("[^/]");
78 continue;
79 } else if (ch == '.' || ch == '\\') {
80 sb.append('\\');
81 }
82 sb.append(ch);
83 }
84 if (needLineEndMatch) {
85 sb.append('$');
86 }
87 return sb.toString();
88 }
89
90 public boolean accept(Path path) {
91 return delegate.accept(path);
92 }
93
94 }