KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ccvs > core > util > FileNameMatcher


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.team.internal.ccvs.core.util;
12
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.List JavaDoc;
16
17 import org.eclipse.core.runtime.Assert;
18
19 /**
20  * A FileNameMatcher associates a String with a String pattern.
21  */

22 public class FileNameMatcher {
23     
24     private List JavaDoc matchers = new ArrayList JavaDoc();
25     private List JavaDoc results = new ArrayList JavaDoc();
26     private static final String JavaDoc TRUE = "true"; //$NON-NLS-1$
27

28     public FileNameMatcher() {
29     }
30     
31     public FileNameMatcher(String JavaDoc[] patterns) {
32         register(patterns);
33     }
34         
35     void register(String JavaDoc[] patterns) {
36         for (int i = 0; i < patterns.length; i++) {
37             register(patterns[i],TRUE);
38         }
39     }
40     
41     public void register(String JavaDoc pattern, String JavaDoc result) {
42         
43         Assert.isTrue(matchers.size() == results.size());
44         
45         pattern = pattern.trim();
46         
47         // The empty pattern matches everything, but we want to match
48
// nothing with it, so we just do not register anything
49
if (pattern.length() == 0) {
50             return;
51         }
52     
53         matchers.add(new StringMatcher(pattern,false,false));
54         results.add(result);
55         
56     }
57     
58     public String JavaDoc getMatch(String JavaDoc name) {
59         StringMatcher stringMatcher;
60         
61         for (int i = 0; i < matchers.size(); i++) {
62             stringMatcher = (StringMatcher) matchers.get(i);
63             if (stringMatcher.match(name)) {
64                 return (String JavaDoc)results.get(i);
65             }
66         }
67         
68         return null;
69     }
70     
71     public boolean match(String JavaDoc name) {
72         return getMatch(name) != null;
73     }
74 }
75
Popular Tags