01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package org.apache.commons.betwixt.strategy.impl.propertysuppression;
18:
19: import org.apache.commons.betwixt.strategy.PropertySuppressionStrategy;
20:
21: /**
22: * Suppresses properties based on the package of their type.
23: * Limited regex is supported. If the package name ends in <code>.*</code>
24: * them all child packages will be suppressed.
25: * @author <a href='http://jakarta.apache.org/commons'>Jakarta Commons Team</a> of the <a href='http://www.apache.org'>Apache Software Foundation</a>
26: * @since 0.8
27: */
28: public class PackageSuppressor extends PropertySuppressionStrategy {
29:
30: /** Package to be suppressed */
31: private final String suppressedPackage;
32: private final boolean exact;
33:
34: /**
35: * Constructs a suppressor for packages.
36: * @param suppressedPackage package name (for exact match)
37: * or base package and <code>.*</code>to suppress children
38: */
39: public PackageSuppressor(String suppressedPackage) {
40: if (suppressedPackage.endsWith(".*")) {
41: exact = false;
42: suppressedPackage = suppressedPackage.substring(0,
43: suppressedPackage.length() - 2);
44: } else {
45: exact = true;
46: }
47: this .suppressedPackage = suppressedPackage;
48: }
49:
50: public boolean suppressProperty(Class classContainingTheProperty,
51: Class propertyType, String propertyName) {
52: boolean result = false;
53: if (propertyType != null) {
54: Package propertyTypePackage = propertyType.getPackage();
55: if (propertyTypePackage != null) {
56: String packageName = propertyTypePackage.getName();
57: if (exact) {
58: result = suppressedPackage.equals(packageName);
59: } else if (packageName != null) {
60: result = packageName.startsWith(suppressedPackage);
61: }
62: }
63: }
64: return result;
65: }
66:
67: public String toString() {
68: StringBuffer buffer = new StringBuffer("Suppressing package ");
69: buffer.append(suppressedPackage);
70: if (exact) {
71: buffer.append("(exact)");
72: }
73: return buffer.toString();
74: }
75: }
|