Mercurial > hg4j
comparison src/org/tmatesoft/hg/internal/PathGlobMatcher.java @ 114:46291ec605a0
Filters to read and initialize according to configuration files
author | Artem Tikhomirov <tikhomirov.artem@gmail.com> |
---|---|
date | Thu, 03 Feb 2011 22:13:55 +0100 |
parents | |
children | 68ba22a2133a |
comparison
equal
deleted
inserted
replaced
113:67ae317408c9 | 114:46291ec605a0 |
---|---|
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@svnkit.com | |
16 */ | |
17 package org.tmatesoft.hg.internal; | |
18 | |
19 import java.util.regex.PatternSyntaxException; | |
20 | |
21 import org.tmatesoft.hg.core.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 for (int i = 0; i <= end; i++) { | |
62 char ch = glob.charAt(i); | |
63 if (ch == '*') { | |
64 if (glob.charAt(i+1) == '*') { // i < end because we've stripped any trailing * earlier | |
65 // any char, including path segment separator | |
66 sb.append(".*?"); | |
67 } else { | |
68 // just path segments | |
69 sb.append("[^/]*?"); | |
70 } | |
71 continue; | |
72 } else if (ch == '?') { | |
73 sb.append("[^/]"); | |
74 continue; | |
75 } else if (ch == '.' || ch == '\\') { | |
76 sb.append('\\'); | |
77 } | |
78 sb.append(ch); | |
79 } | |
80 if (needLineEndMatch) { | |
81 sb.append('$'); | |
82 } | |
83 return sb.toString(); | |
84 } | |
85 | |
86 public boolean accept(Path path) { | |
87 return delegate.accept(path); | |
88 } | |
89 | |
90 } |