OSDN Git Service

libjava/ChangeLog:
[pf3gnuchains/gcc-fork.git] / libjava / classpath / tools / gnu / classpath / tools / doclets / PackageMatcher.java
1 /* gnu.classpath.tools.doclets.PackageMatcher
2    Copyright (C) 2004 Free Software Foundation, Inc.
3
4 This file is part of GNU Classpath.
5
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10  
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING.  If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA. */
20
21 package gnu.classpath.tools.doclets;
22
23 import java.util.Iterator;
24 import java.util.HashSet;
25 import java.util.Set;
26 import java.util.SortedSet;
27 import java.util.TreeSet;
28
29 import java.util.regex.Matcher;
30 import java.util.regex.Pattern;
31
32 import com.sun.javadoc.PackageDoc;
33
34 /**
35  *  Filters a set of packages according to a set of wildcards.
36  */
37 public class PackageMatcher
38 {
39    private Set patterns = new HashSet();
40    
41    /**
42     *  Add a wildcard to be matched. Wildcards can contain asterisk
43     *  characters which match zero or more characters.
44     *
45     *  @throw InvalidPackageWildcardException if the wildcard cannot
46     *  match any valid package name.
47     */
48    public void addWildcard(String wildcard) 
49       throws InvalidPackageWildcardException
50    {
51       final int STATE_ID_START = 0;
52       final int STATE_ID = 1;
53
54       int state = STATE_ID_START;
55
56       char[] wildcardChars = wildcard.toCharArray();
57       StringBuffer regexString = new StringBuffer();
58
59       for (int i=0; i<wildcardChars.length; ++i) {
60          char c = wildcardChars[i];
61          switch (state) {
62          case STATE_ID_START:
63             if ('*' == c) {
64                regexString.append(".*");
65             }
66             else if (Character.isJavaIdentifierStart(c)) {
67                regexString.append(c);
68             }
69             else {
70                throw new InvalidPackageWildcardException(wildcard);
71             }
72             state = STATE_ID;
73             break;
74
75          case STATE_ID:
76             if ('.' == c) {
77                regexString.append("\\.");
78                state = STATE_ID_START;
79             }
80             else if ('*' == c) {
81                regexString.append(".*");
82             }
83             else if (Character.isJavaIdentifierPart(c)) {
84                regexString.append(c);
85             }
86             else {
87                throw new InvalidPackageWildcardException(wildcard);
88             }
89          }
90       }
91       if (STATE_ID_START == state) {
92          throw new InvalidPackageWildcardException(wildcard);
93       }
94
95       patterns.add(Pattern.compile(regexString.toString()));
96    }
97
98    /**
99     *  Return a sorted, filtered set of packages. A package from the
100     *  array given will be put into the output list if it matches one
101     *  or more of the wildcards added to this PackageMatcher before.
102     */
103    public SortedSet filter(PackageDoc[] packageDocs)
104    {
105       SortedSet result = new TreeSet();
106       for (int i=0; i<packageDocs.length; ++i) {
107          if (match(packageDocs[i])) {
108             result.add(packageDocs[i]);
109          }
110       }
111       return result;
112    }
113
114    /**
115     *  Return true when the given PackageDoc matches one or more of
116     *  the wildcard added to this PackageMatcher before.
117     */
118    public boolean match(PackageDoc packageDoc)
119    {
120       Iterator it = patterns.iterator();
121       while (it.hasNext()) {
122          Pattern pattern = (Pattern)it.next();
123          Matcher matcher = pattern.matcher(packageDoc.name());
124          if (matcher.matches()) {
125             return true;
126          }
127       }
128       return false;
129    }
130
131    public String toString()
132    {
133       return "PackageMatcher{patterns=" + patterns + "}";
134    }
135 }