1 /*
2 * Copyright (C) Christian Schulte, 2005-206
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * o Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * o Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
15 * distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
18 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
19 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 * $JOMC: SourceFileProcessorTask.java 4613 2012-09-22 10:07:08Z schulte $
29 *
30 */
31 package org.jomc.ant;
32
33 import org.apache.tools.ant.BuildException;
34 import org.apache.tools.ant.Project;
35 import org.jomc.tools.SourceFileProcessor;
36
37 /**
38 * Base class for executing source file processor based tasks.
39 *
40 * @author <a href="mailto:cs@schulte.it">Christian Schulte</a>
41 * @version $JOMC: SourceFileProcessorTask.java 4613 2012-09-22 10:07:08Z schulte $
42 * @see #processSourceFiles()
43 */
44 public class SourceFileProcessorTask extends JomcToolTask
45 {
46
47 /** Controls processing of source files. */
48 private boolean sourceProcessingEnabled = true;
49
50 /** Class of the {@code SourceFileProcessor} backing the task. */
51 private Class<? extends SourceFileProcessor> sourceFileProcessorClass;
52
53 /** Creates a new {@code SourceFileProcessorTask} instance. */
54 public SourceFileProcessorTask()
55 {
56 super();
57 }
58
59 /**
60 * Gets a flag indicating the processing of source files is enabled.
61 *
62 * @return {@code true}, if processing of source files is enabled; {@code false}, else.
63 *
64 * @see #setSourceProcessingEnabled(boolean)
65 */
66 public final boolean isSourceProcessingEnabled()
67 {
68 return this.sourceProcessingEnabled;
69 }
70
71 /**
72 * Sets the flag indicating the processing of source files is enabled.
73 *
74 * @param value {@code true}, to enable processing of source files; {@code false}, to disable processing of source
75 * files.
76 *
77 * @see #isSourceProcessingEnabled()
78 */
79 public final void setSourceProcessingEnabled( final boolean value )
80 {
81 this.sourceProcessingEnabled = value;
82 }
83
84 /**
85 * Gets the class of the {@code SourceFileProcessor} backing the task.
86 *
87 * @return The class of the {@code SourceFileProcessor} backing the task.
88 *
89 * @see #setSourceFileProcessorClass(java.lang.Class)
90 */
91 public final Class<? extends SourceFileProcessor> getSourceFileProcessorClass()
92 {
93 if ( this.sourceFileProcessorClass == null )
94 {
95 this.sourceFileProcessorClass = SourceFileProcessor.class;
96 }
97
98 return this.sourceFileProcessorClass;
99 }
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 }