1 /*
2 * Copyright (C) 2005 Christian Schulte <cs@schulte.it>
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 5135 2016-04-08 13:53:07Z 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 5135 2016-04-08 13:53:07Z schulte $
42 * @see #processSourceFiles()
43 */
44 public class SourceFileProcessorTask extends JomcToolTask
45 {
46
47 /**
48 * Controls processing of source files.
49 */
50 private boolean sourceProcessingEnabled = true;
51
52 /**
53 * Class of the {@code SourceFileProcessor} backing the task.
54 */
55 private Class<? extends SourceFileProcessor> sourceFileProcessorClass;
56
57 /**
58 * Creates a new {@code SourceFileProcessorTask} instance.
59 */
60 public SourceFileProcessorTask()
61 {
62 super();
63 }
64
65 /**
66 * Gets a flag indicating the processing of source files is enabled.
67 *
68 * @return {@code true}, if processing of source files is enabled; {@code false}, else.
69 *
70 * @see #setSourceProcessingEnabled(boolean)
71 */
72 public final boolean isSourceProcessingEnabled()
73 {
74 return this.sourceProcessingEnabled;
75 }
76
77 /**
78 * Sets the flag indicating the processing of source files is enabled.
79 *
80 * @param value {@code true}, to enable processing of source files; {@code false}, to disable processing of source
81 * files.
82 *
83 * @see #isSourceProcessingEnabled()
84 */
85 public final void setSourceProcessingEnabled( final boolean value )
86 {
87 this.sourceProcessingEnabled = value;
88 }
89
90 /**
91 * Gets the class of the {@code SourceFileProcessor} backing the task.
92 *
93 * @return The class of the {@code SourceFileProcessor} backing the task.
94 *
95 * @see #setSourceFileProcessorClass(java.lang.Class)
96 */
97 public final Class<? extends SourceFileProcessor> getSourceFileProcessorClass()
98 {
99 if ( this.sourceFileProcessorClass == null )
100 {
101 this.sourceFileProcessorClass = SourceFileProcessor.class;
102 }
103
104 return this.sourceFileProcessorClass;
105 }
106
107 /**
108 * Sets the class of the {@code SourceFileProcessor} backing the task.
109 *
110 * @param value The new class of the {@code SourceFileProcessor} backing the task or {@code null}.
111 *
112 * @see #getSourceFileProcessorClass()
113 */
114 public final void setSourceFileProcessorClass( final Class<? extends SourceFileProcessor> value )
115 {
116 this.sourceFileProcessorClass = value;
117 }
118
119 /**
120 * Creates a new {@code SourceFileProcessor} instance setup using the properties of the instance.
121 *
122 * @return A new {@code SourceFileProcessor} instance.
123 *
124 * @throws BuildException if creating a new {@code SourceFileProcessor} instance fails.
125 *
126 * @see #getSourceFileProcessorClass()
127 * @see #configureSourceFileProcessor(org.jomc.tools.SourceFileProcessor)
128 */
129 public SourceFileProcessor newSourceFileProcessor() throws BuildException
130 {
131 try
132 {
133 final SourceFileProcessor sourceFileProcessor = this.getSourceFileProcessorClass().newInstance();
134 this.configureSourceFileProcessor( sourceFileProcessor );
135 return sourceFileProcessor;
136 }
137 catch ( final InstantiationException e )
138 {
139 throw new BuildException( Messages.getMessage( "failedCreatingObject",
140 this.getSourceFileProcessorClass().getName() ),
141 e, this.getLocation() );
142
143 }
144 catch ( final IllegalAccessException e )
145 {
146 throw new BuildException( Messages.getMessage( "failedCreatingObject",
147 this.getSourceFileProcessorClass().getName() ),
148 e, this.getLocation() );
149
150 }
151 }
152
153 /**
154 * Configures a given {@code SourceFileProcessor} instance using the properties of the instance.
155 *
156 * @param sourceFileProcessor The source file processor to configure.
157 *
158 * @throws NullPointerException if {@code sourceFileProcessor} is {@code null}.
159 * @throws BuildException if configuring {@code sourceFileProcessor} fails.
160 *
161 * @see #configureJomcTool(org.jomc.tools.JomcTool)
162 */
163 public void configureSourceFileProcessor( final SourceFileProcessor sourceFileProcessor ) throws BuildException
164 {
165 if ( sourceFileProcessor == null )
166 {
167 throw new NullPointerException( "sourceFileProcessor" );
168 }
169
170 this.configureJomcTool( sourceFileProcessor );
171 }
172
173 /**
174 * Calls the {@code processSourceFiles} method if source processing is enabled.
175 *
176 * @throws BuildException if processing source files fails.
177 *
178 * @see #processSourceFiles()
179 */
180 @Override
181 public final void executeTask() throws BuildException
182 {
183 if ( this.isSourceProcessingEnabled() )
184 {
185 this.processSourceFiles();
186 this.log( Messages.getMessage( "sourceProcessingSuccess" ) );
187 }
188 else
189 {
190 this.log( Messages.getMessage( "sourceProcessingDisabled" ) );
191 }
192 }
193
194 /**
195 * Processes source files.
196 *
197 * @throws BuildException if processing source files fails.
198 *
199 * @see #executeTask()
200 */
201 public void processSourceFiles() throws BuildException
202 {
203 this.log( Messages.getMessage( "unimplementedTask", this.getClass().getName(), "processSourceFiles" ),
204 Project.MSG_ERR );
205
206 }
207
208 /**
209 * {@inheritDoc}
210 */
211 @Override
212 public SourceFileProcessorTask clone()
213 {
214 return (SourceFileProcessorTask) super.clone();
215 }
216
217 }