libjava/ChangeLog:
[official-gcc.git] / libjava / classpath / tools / gnu / classpath / tools / doclets / PackageMatcher.java
blob23da79cb387e565ce37929af976017a00e05920d
1 /* gnu.classpath.tools.doclets.PackageMatcher
2 Copyright (C) 2004 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
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.
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.
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. */
21 package gnu.classpath.tools.doclets;
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;
29 import java.util.regex.Matcher;
30 import java.util.regex.Pattern;
32 import com.sun.javadoc.PackageDoc;
34 /**
35 * Filters a set of packages according to a set of wildcards.
37 public class PackageMatcher
39 private Set patterns = new HashSet();
41 /**
42 * Add a wildcard to be matched. Wildcards can contain asterisk
43 * characters which match zero or more characters.
45 * @throw InvalidPackageWildcardException if the wildcard cannot
46 * match any valid package name.
48 public void addWildcard(String wildcard)
49 throws InvalidPackageWildcardException
51 final int STATE_ID_START = 0;
52 final int STATE_ID = 1;
54 int state = STATE_ID_START;
56 char[] wildcardChars = wildcard.toCharArray();
57 StringBuffer regexString = new StringBuffer();
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(".*");
66 else if (Character.isJavaIdentifierStart(c)) {
67 regexString.append(c);
69 else {
70 throw new InvalidPackageWildcardException(wildcard);
72 state = STATE_ID;
73 break;
75 case STATE_ID:
76 if ('.' == c) {
77 regexString.append("\\.");
78 state = STATE_ID_START;
80 else if ('*' == c) {
81 regexString.append(".*");
83 else if (Character.isJavaIdentifierPart(c)) {
84 regexString.append(c);
86 else {
87 throw new InvalidPackageWildcardException(wildcard);
91 if (STATE_ID_START == state) {
92 throw new InvalidPackageWildcardException(wildcard);
95 patterns.add(Pattern.compile(regexString.toString()));
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.
103 public SortedSet filter(PackageDoc[] packageDocs)
105 SortedSet result = new TreeSet();
106 for (int i=0; i<packageDocs.length; ++i) {
107 if (match(packageDocs[i])) {
108 result.add(packageDocs[i]);
111 return result;
115 * Return true when the given PackageDoc matches one or more of
116 * the wildcard added to this PackageMatcher before.
118 public boolean match(PackageDoc packageDoc)
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;
128 return false;
131 public String toString()
133 return "PackageMatcher{patterns=" + patterns + "}";