001    /*
002     * Copyright 2005 Stephen J. McConnell
003     *
004     * Licensed  under the  Apache License,  Version 2.0  (the "License");
005     * you may not use  this file  except in  compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     *   http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed  under the  License is distributed on an "AS IS" BASIS,
012     * WITHOUT  WARRANTIES OR CONDITIONS  OF ANY KIND, either  express  or
013     * implied.
014     *
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    
019    package net.dpml.tools.tasks;
020    
021    import org.apache.tools.ant.BuildException;
022    import org.apache.tools.ant.taskdefs.Filter;
023    
024    /**
025     * Construct a pattern filter using a supplied feature and token.
026     *
027     * @author <a href="http://www.dpml.net">Digital Product Meta Library</a>
028     * @version 1.1.3
029     */
030    public class FilterTask extends FeatureTask
031    {
032        private String m_token;
033        private String m_default;
034        
035       /**
036        * Set a default filter value.
037        * @param value the default value
038        */
039        public void setDefault( final String value )
040        {
041            m_default = value;
042        }
043    
044       /**
045        * Set the filter token value.
046        * @param token the token value
047        */
048        public void setToken( final String token )
049        {
050            m_token = token;
051        }
052    
053       /**
054        * Execute the task.
055        * @exception BuildException if a build error occurs
056        */
057        public void execute() throws BuildException
058        {
059            if( null == m_token )
060            {
061                final String error =
062                  "Missing token attribute.";
063                throw new BuildException( error );
064            }
065    
066            final String value = resolveValue();
067            if( null != value )
068            {
069                final Filter filter = (Filter) getProject().createTask( "filter" );
070                filter.setTaskName( getTaskName() );
071                filter.init();
072                filter.setToken( m_token );
073                filter.setValue( value );
074                filter.execute();
075            }
076            else
077            {
078                final String error =
079                  "Unrecognized or unsupported feature ["
080                  + getFeature()
081                  + "].";
082                throw new BuildException( error );
083            }
084        }
085        
086        private String resolveValue()
087        {
088            String value = super.resolve();
089            if( null == value )
090            {
091                return m_default;
092            }
093            else
094            {
095                return value;
096            }
097        }
098    }