001    /*
002     *   Copyright (C) Christian Schulte, 2005-206
003     *   All rights reserved.
004     *
005     *   Redistribution and use in source and binary forms, with or without
006     *   modification, are permitted provided that the following conditions
007     *   are met:
008     *
009     *     o Redistributions of source code must retain the above copyright
010     *       notice, this list of conditions and the following disclaimer.
011     *
012     *     o Redistributions in binary form must reproduce the above copyright
013     *       notice, this list of conditions and the following disclaimer in
014     *       the documentation and/or other materials provided with the
015     *       distribution.
016     *
017     *   THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
018     *   INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
019     *   AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
020     *   THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY DIRECT, INDIRECT,
021     *   INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
022     *   NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
023     *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
024     *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
025     *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
026     *   THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
027     *
028     *   $JOMC: SourceFileProcessorTask.java 3838 2011-10-08 20:15:41Z schulte2005 $
029     *
030     */
031    package org.jomc.ant;
032    
033    import org.apache.tools.ant.BuildException;
034    import org.apache.tools.ant.Project;
035    import org.jomc.tools.SourceFileProcessor;
036    
037    /**
038     * Base class for executing source file processor based tasks.
039     *
040     * @author <a href="mailto:schulte2005@users.sourceforge.net">Christian Schulte</a>
041     * @version $JOMC: SourceFileProcessorTask.java 3838 2011-10-08 20:15:41Z schulte2005 $
042     * @see #processSourceFiles()
043     */
044    public class SourceFileProcessorTask extends JomcToolTask
045    {
046    
047        /** Controls processing of source files. */
048        private boolean sourceProcessingEnabled = true;
049    
050        /** Class of the {@code SourceFileProcessor} backing the task. */
051        private Class<? extends SourceFileProcessor> sourceFileProcessorClass;
052    
053        /** Creates a new {@code SourceFileProcessorTask} instance. */
054        public SourceFileProcessorTask()
055        {
056            super();
057        }
058    
059        /**
060         * Gets a flag indicating the processing of source files is enabled.
061         *
062         * @return {@code true}, if processing of source files is enabled; {@code false}, else.
063         *
064         * @see #setSourceProcessingEnabled(boolean)
065         */
066        public final boolean isSourceProcessingEnabled()
067        {
068            return this.sourceProcessingEnabled;
069        }
070    
071        /**
072         * Sets the flag indicating the processing of source files is enabled.
073         *
074         * @param value {@code true}, to enable processing of source files; {@code false}, to disable processing of source
075         * files.
076         *
077         * @see #isSourceProcessingEnabled()
078         */
079        public final void setSourceProcessingEnabled( final boolean value )
080        {
081            this.sourceProcessingEnabled = value;
082        }
083    
084        /**
085         * Gets the class of the {@code SourceFileProcessor} backing the task.
086         *
087         * @return The class of the {@code SourceFileProcessor} backing the task.
088         *
089         * @see #setSourceFileProcessorClass(java.lang.Class)
090         */
091        public final Class<? extends SourceFileProcessor> getSourceFileProcessorClass()
092        {
093            if ( this.sourceFileProcessorClass == null )
094            {
095                this.sourceFileProcessorClass = SourceFileProcessor.class;
096            }
097    
098            return this.sourceFileProcessorClass;
099        }
100    
101        /**
102         * Sets the class of the {@code SourceFileProcessor} backing the task.
103         *
104         * @param value The new class of the {@code SourceFileProcessor} backing the task or {@code null}.
105         *
106         * @see #getSourceFileProcessorClass()
107         */
108        public final void setSourceFileProcessorClass( final Class<? extends SourceFileProcessor> value )
109        {
110            this.sourceFileProcessorClass = value;
111        }
112    
113        /**
114         * Creates a new {@code SourceFileProcessor} instance setup using the properties of the instance.
115         *
116         * @return A new {@code SourceFileProcessor} instance.
117         *
118         * @throws BuildException if creating a new {@code SourceFileProcessor} instance fails.
119         *
120         * @see #getSourceFileProcessorClass()
121         * @see #configureSourceFileProcessor(org.jomc.tools.SourceFileProcessor)
122         */
123        public SourceFileProcessor newSourceFileProcessor() throws BuildException
124        {
125            try
126            {
127                final SourceFileProcessor sourceFileProcessor = this.getSourceFileProcessorClass().newInstance();
128                this.configureSourceFileProcessor( sourceFileProcessor );
129                return sourceFileProcessor;
130            }
131            catch ( final InstantiationException e )
132            {
133                throw new BuildException( Messages.getMessage( "failedCreatingObject",
134                                                               this.getSourceFileProcessorClass().getName() ),
135                                          e, this.getLocation() );
136    
137            }
138            catch ( final IllegalAccessException e )
139            {
140                throw new BuildException( Messages.getMessage( "failedCreatingObject",
141                                                               this.getSourceFileProcessorClass().getName() ),
142                                          e, this.getLocation() );
143    
144            }
145        }
146    
147        /**
148         * Configures a given {@code SourceFileProcessor} instance using the properties of the instance.
149         *
150         * @param sourceFileProcessor The source file processor to configure.
151         *
152         * @throws NullPointerException if {@code sourceFileProcessor} is {@code null}.
153         * @throws BuildException if configuring {@code sourceFileProcessor} fails.
154         *
155         * @see #configureJomcTool(org.jomc.tools.JomcTool)
156         */
157        public void configureSourceFileProcessor( final SourceFileProcessor sourceFileProcessor ) throws BuildException
158        {
159            if ( sourceFileProcessor == null )
160            {
161                throw new NullPointerException( "sourceFileProcessor" );
162            }
163    
164            this.configureJomcTool( sourceFileProcessor );
165        }
166    
167        /**
168         * Calls the {@code processSourceFiles} method if source processing is enabled.
169         *
170         * @throws BuildException if processing source files fails.
171         *
172         * @see #processSourceFiles()
173         */
174        @Override
175        public final void executeTask() throws BuildException
176        {
177            if ( this.isSourceProcessingEnabled() )
178            {
179                this.processSourceFiles();
180                this.log( Messages.getMessage( "sourceProcessingSuccess" ) );
181            }
182            else
183            {
184                this.log( Messages.getMessage( "sourceProcessingDisabled" ) );
185            }
186        }
187    
188        /**
189         * Processes source files.
190         *
191         * @throws BuildException if processing source files fails.
192         *
193         * @see #executeTask()
194         */
195        public void processSourceFiles() throws BuildException
196        {
197            this.log( Messages.getMessage( "unimplementedTask", this.getClass().getName(), "processSourceFiles" ),
198                      Project.MSG_ERR );
199    
200        }
201    
202        /** {@inheritDoc} */
203        @Override
204        public SourceFileProcessorTask clone()
205        {
206            return (SourceFileProcessorTask) super.clone();
207        }
208    
209    }