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: ResourceFileProcessorTask.java 5135 2016-04-08 13:53:07Z schulte $
29 *
30 */
31 package org.jomc.ant;
32
33 import java.util.Locale;
34 import org.apache.tools.ant.BuildException;
35 import org.apache.tools.ant.Project;
36 import org.jomc.tools.ResourceFileProcessor;
37
38 /**
39 * Base class for executing resource file processor based tasks.
40 *
41 * @author <a href="mailto:cs@schulte.it">Christian Schulte</a>
42 * @version $JOMC: ResourceFileProcessorTask.java 5135 2016-04-08 13:53:07Z schulte $
43 * @see #processResourceFiles()
44 */
45 public class ResourceFileProcessorTask extends JomcToolTask
46 {
47
48 /**
49 * The language of the default language properties file of generated resource bundle resources.
50 */
51 private String resourceBundleDefaultLanguage;
52
53 /**
54 * Controls processing of resource files.
55 */
56 private boolean resourceProcessingEnabled = true;
57
58 /**
59 * Class of the {@code ResourceFileProcessor} backing the task.
60 */
61 private Class<? extends ResourceFileProcessor> resourceFileProcessorClass;
62
63 /**
64 * Creates a new {@code ResourceFileProcessorTask} instance.
65 */
66 public ResourceFileProcessorTask()
67 {
68 super();
69 }
70
71 /**
72 * Gets a flag indicating the processing of resources is enabled.
73 *
74 * @return {@code true}, if processing of resources is enabled; {@code false}, else.
75 *
76 * @see #setResourceProcessingEnabled(boolean)
77 */
78 public final boolean isResourceProcessingEnabled()
79 {
80 return this.resourceProcessingEnabled;
81 }
82
83 /**
84 * Sets the flag indicating the processing of resources is enabled.
85 *
86 * @param value {@code true}, to enable processing of resources; {@code false}, to disable processing of resources.
87 *
88 * @see #isResourceProcessingEnabled()
89 */
90 public final void setResourceProcessingEnabled( final boolean value )
91 {
92 this.resourceProcessingEnabled = value;
93 }
94
95 /**
96 * Gets the language of the default language properties file of generated resource bundle resource files.
97 *
98 * @return The language of the default language properties file of generated resource bundle resource files or
99 * {@code null}.
100 *
101 * @see #setResourceBundleDefaultLanguage(java.lang.String)
102 */
103 public final String getResourceBundleDefaultLanguage()
104 {
105 return this.resourceBundleDefaultLanguage;
106 }
107
108 /**
109 * Sets the language of the default language properties file of generated resource bundle resource files.
110 *
111 * @param value The new language of the default language properties file of generated resource bundle resource files
112 * or {@code null}.
113 *
114 * @see #getResourceBundleDefaultLanguage()
115 */
116 public final void setResourceBundleDefaultLanguage( final String value )
117 {
118 this.resourceBundleDefaultLanguage = value;
119 }
120
121 /**
122 * Gets the class of the {@code ResourceFileProcessor} backing the task.
123 *
124 * @return The class of the {@code ResourceFileProcessor} backing the task.
125 *
126 * @see #setResourceFileProcessorClass(java.lang.Class)
127 */
128 public final Class<? extends ResourceFileProcessor> getResourceFileProcessorClass()
129 {
130 if ( this.resourceFileProcessorClass == null )
131 {
132 this.resourceFileProcessorClass = ResourceFileProcessor.class;
133 }
134
135 return this.resourceFileProcessorClass;
136 }
137
138 /**
139 * Sets the class of the {@code ResourceFileProcessor} backing the task.
140 *
141 * @param value The new class of the {@code ResourceFileProcessor} backing the task or {@code null}.
142 *
143 * @see #getResourceFileProcessorClass()
144 */
145 public final void setResourceFileProcessorClass( final Class<? extends ResourceFileProcessor> value )
146 {
147 this.resourceFileProcessorClass = value;
148 }
149
150 /**
151 * Creates a new {@code ResourceFileProcessor} instance setup using the properties of the instance.
152 *
153 * @return A new {@code ResourceFileProcessor} instance.
154 *
155 * @throws BuildException if creating a new {@code ResourceFileProcessor} instance fails.
156 *
157 * @see #getResourceFileProcessorClass()
158 * @see #configureResourceFileProcessor(org.jomc.tools.ResourceFileProcessor)
159 */
160 public ResourceFileProcessor newResourceFileProcessor() throws BuildException
161 {
162 try
163 {
164 final ResourceFileProcessor resourceFileProcessor = this.getResourceFileProcessorClass().newInstance();
165 this.configureResourceFileProcessor( resourceFileProcessor );
166 return resourceFileProcessor;
167 }
168 catch ( final InstantiationException e )
169 {
170 throw new BuildException( Messages.getMessage( "failedCreatingObject",
171 this.getResourceFileProcessorClass().getName() ),
172 e, this.getLocation() );
173
174 }
175 catch ( final IllegalAccessException e )
176 {
177 throw new BuildException( Messages.getMessage( "failedCreatingObject",
178 this.getResourceFileProcessorClass().getName() ),
179 e, this.getLocation() );
180
181 }
182 }
183
184 /**
185 * Configures a given {@code ResourceFileProcessor} instance using the properties of the instance.
186 *
187 * @param resourceFileProcessor The resource file processor to configure.
188 *
189 * @throws NullPointerException if {@code resourceFileProcessor} is {@code null}.
190 * @throws BuildException if configuring {@code resourceFileProcessor} fails.
191 *
192 * @see #configureJomcTool(org.jomc.tools.JomcTool)
193 */
194 public void configureResourceFileProcessor( final ResourceFileProcessor resourceFileProcessor )
195 throws BuildException
196 {
197 if ( resourceFileProcessor == null )
198 {
199 throw new NullPointerException( "resourceFileProcessor" );
200 }
201
202 this.configureJomcTool( resourceFileProcessor );
203
204 if ( this.getResourceBundleDefaultLanguage() != null )
205 {
206 resourceFileProcessor.setResourceBundleDefaultLocale(
207 new Locale( this.getResourceBundleDefaultLanguage() ) );
208
209 }
210 }
211
212 /**
213 * Calls the {@code processResourceFiles} method if resource processing is enabled.
214 *
215 * @throws BuildException if processing resource files fails.
216 *
217 * @see #processResourceFiles()
218 */
219 @Override
220 public final void executeTask() throws BuildException
221 {
222 if ( this.isResourceProcessingEnabled() )
223 {
224 this.processResourceFiles();
225 this.log( Messages.getMessage( "resourceProcessingSuccess" ) );
226 }
227 else
228 {
229 this.log( Messages.getMessage( "resourceProcessingDisabled" ) );
230 }
231 }
232
233 /**
234 * Processes resource files.
235 *
236 * @throws BuildException if processing resource files fails.
237 *
238 * @see #executeTask()
239 */
240 public void processResourceFiles() throws BuildException
241 {
242 this.log( Messages.getMessage( "unimplementedTask", this.getClass().getName(), "processResourceFiles" ),
243 Project.MSG_ERR );
244
245 }
246
247 /**
248 * {@inheritDoc}
249 */
250 @Override
251 public ResourceFileProcessorTask clone()
252 {
253 return (ResourceFileProcessorTask) super.clone();
254 }
255
256 }