View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      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://commons.apache.org'>Apache 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, suppressedPackage.length()-2);
43          }
44          else
45          {
46              exact =true;
47          }
48          this.suppressedPackage = suppressedPackage;
49      }
50      
51      public boolean suppressProperty(Class classContainingTheProperty, 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                  }
60                  else if (packageName != null)
61                  {
62                      result = packageName.startsWith(suppressedPackage);
63                  }
64              }
65          }
66          return result;
67      }
68  
69      public String toString() {
70          StringBuffer buffer = new StringBuffer("Suppressing package " );
71          buffer.append(suppressedPackage);
72          if (exact) {
73              buffer.append("(exact)");
74          }
75          return buffer.toString();
76      }
77  }