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)
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
21 package gnu
.classpath
.tools
.doclets
;
23 import java
.util
.Iterator
;
24 import java
.util
.HashSet
;
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
;
35 * Filters a set of packages according to a set of wildcards.
37 public class PackageMatcher
39 private Set patterns
= new HashSet();
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
];
64 regexString
.append(".*");
66 else if (Character
.isJavaIdentifierStart(c
)) {
67 regexString
.append(c
);
70 throw new InvalidPackageWildcardException(wildcard
);
77 regexString
.append("\\.");
78 state
= STATE_ID_START
;
81 regexString
.append(".*");
83 else if (Character
.isJavaIdentifierPart(c
)) {
84 regexString
.append(c
);
87 throw new InvalidPackageWildcardException(wildcard
);
91 if (STATE_ID_START
== state
) {
92 throw new InvalidPackageWildcardException(wildcard
);
95 patterns
.add(Pattern
.compile(regexString
.toString()));
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
]);
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()) {
131 public String
toString()
133 return "PackageMatcher{patterns=" + patterns
+ "}";