001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     * 
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     * 
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */ 
017    package org.apache.commons.betwixt.strategy.impl.propertysuppression;
018    
019    import org.apache.commons.betwixt.strategy.PropertySuppressionStrategy;
020    
021    /**
022     * Suppresses properties based on the package of their type.
023     * Limited regex is supported. If the package name ends in <code>.*</code>
024     * them all child packages will be suppressed.
025     * @author <a href='http://commons.apache.org'>Apache Commons Team</a> of the <a href='http://www.apache.org'>Apache Software Foundation</a>
026     * @since 0.8
027     */
028    public class PackageSuppressor extends PropertySuppressionStrategy {
029    
030        /** Package to be suppressed */
031        private final String suppressedPackage;
032        private final boolean exact;
033        
034        /**
035         * Constructs a suppressor for packages.
036         * @param suppressedPackage package name (for exact match)
037         * or base package and <code>.*</code>to suppress children
038         */
039        public  PackageSuppressor(String suppressedPackage) {
040            if (suppressedPackage.endsWith(".*")) {
041                exact = false;
042                suppressedPackage = suppressedPackage.substring(0, suppressedPackage.length()-2);
043            }
044            else
045            {
046                exact =true;
047            }
048            this.suppressedPackage = suppressedPackage;
049        }
050        
051        public boolean suppressProperty(Class classContainingTheProperty, Class propertyType, String propertyName) {
052            boolean result = false;
053            if (propertyType != null) {
054                Package propertyTypePackage = propertyType.getPackage();
055                if (propertyTypePackage != null) {
056                    String packageName = propertyTypePackage.getName();
057                    if (exact) {
058                        result = suppressedPackage.equals(packageName);
059                    }
060                    else if (packageName != null)
061                    {
062                        result = packageName.startsWith(suppressedPackage);
063                    }
064                }
065            }
066            return result;
067        }
068    
069        public String toString() {
070            StringBuffer buffer = new StringBuffer("Suppressing package " );
071            buffer.append(suppressedPackage);
072            if (exact) {
073                buffer.append("(exact)");
074            }
075            return buffer.toString();
076        }
077    }