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: ResourceFileProcessorTask.java 3838 2011-10-08 20:15:41Z schulte2005 $
029     *
030     */
031    package org.jomc.ant;
032    
033    import java.util.Locale;
034    import org.apache.tools.ant.BuildException;
035    import org.apache.tools.ant.Project;
036    import org.jomc.tools.ResourceFileProcessor;
037    
038    /**
039     * Base class for executing resource file processor based tasks.
040     *
041     * @author <a href="mailto:schulte2005@users.sourceforge.net">Christian Schulte</a>
042     * @version $JOMC: ResourceFileProcessorTask.java 3838 2011-10-08 20:15:41Z schulte2005 $
043     * @see #processResourceFiles()
044     */
045    public class ResourceFileProcessorTask extends JomcToolTask
046    {
047    
048        /** The language of the default language properties file of generated resource bundle resources. */
049        private String resourceBundleDefaultLanguage;
050    
051        /** Controls processing of resource files. */
052        private boolean resourceProcessingEnabled = true;
053    
054        /** Class of the {@code ResourceFileProcessor} backing the task. */
055        private Class<? extends ResourceFileProcessor> resourceFileProcessorClass;
056    
057        /** Creates a new {@code ResourceFileProcessorTask} instance. */
058        public ResourceFileProcessorTask()
059        {
060            super();
061        }
062    
063        /**
064         * Gets a flag indicating the processing of resources is enabled.
065         *
066         * @return {@code true}, if processing of resources is enabled; {@code false}, else.
067         *
068         * @see #setResourceProcessingEnabled(boolean)
069         */
070        public final boolean isResourceProcessingEnabled()
071        {
072            return this.resourceProcessingEnabled;
073        }
074    
075        /**
076         * Sets the flag indicating the processing of resources is enabled.
077         *
078         * @param value {@code true}, to enable processing of resources; {@code false}, to disable processing of resources.
079         *
080         * @see #isResourceProcessingEnabled()
081         */
082        public final void setResourceProcessingEnabled( final boolean value )
083        {
084            this.resourceProcessingEnabled = value;
085        }
086    
087        /**
088         * Gets the language of the default language properties file of generated resource bundle resource files.
089         *
090         * @return The language of the default language properties file of generated resource bundle resource files or
091         * {@code null}.
092         *
093         * @see #setResourceBundleDefaultLanguage(java.lang.String)
094         */
095        public final String getResourceBundleDefaultLanguage()
096        {
097            return this.resourceBundleDefaultLanguage;
098        }
099    
100        /**
101         * Sets the language of the default language properties file of generated resource bundle resource files.
102         *
103         * @param value The new language of the default language properties file of generated resource bundle resource files
104         * or {@code null}.
105         *
106         * @see #getResourceBundleDefaultLanguage()
107         */
108        public final void setResourceBundleDefaultLanguage( final String value )
109        {
110            this.resourceBundleDefaultLanguage = value;
111        }
112    
113        /**
114         * Gets the class of the {@code ResourceFileProcessor} backing the task.
115         *
116         * @return The class of the {@code ResourceFileProcessor} backing the task.
117         *
118         * @see #setResourceFileProcessorClass(java.lang.Class)
119         */
120        public final Class<? extends ResourceFileProcessor> getResourceFileProcessorClass()
121        {
122            if ( this.resourceFileProcessorClass == null )
123            {
124                this.resourceFileProcessorClass = ResourceFileProcessor.class;
125            }
126    
127            return this.resourceFileProcessorClass;
128        }
129    
130        /**
131         * Sets the class of the {@code ResourceFileProcessor} backing the task.
132         *
133         * @param value The new class of the {@code ResourceFileProcessor} backing the task or {@code null}.
134         *
135         * @see #getResourceFileProcessorClass()
136         */
137        public final void setResourceFileProcessorClass( final Class<? extends ResourceFileProcessor> value )
138        {
139            this.resourceFileProcessorClass = value;
140        }
141    
142        /**
143         * Creates a new {@code ResourceFileProcessor} instance setup using the properties of the instance.
144         *
145         * @return A new {@code ResourceFileProcessor} instance.
146         *
147         * @throws BuildException if creating a new {@code ResourceFileProcessor} instance fails.
148         *
149         * @see #getResourceFileProcessorClass()
150         * @see #configureResourceFileProcessor(org.jomc.tools.ResourceFileProcessor)
151         */
152        public ResourceFileProcessor newResourceFileProcessor() throws BuildException
153        {
154            try
155            {
156                final ResourceFileProcessor resourceFileProcessor = this.getResourceFileProcessorClass().newInstance();
157                this.configureResourceFileProcessor( resourceFileProcessor );
158                return resourceFileProcessor;
159            }
160            catch ( final InstantiationException e )
161            {
162                throw new BuildException( Messages.getMessage( "failedCreatingObject",
163                                                               this.getResourceFileProcessorClass().getName() ),
164                                          e, this.getLocation() );
165    
166            }
167            catch ( final IllegalAccessException e )
168            {
169                throw new BuildException( Messages.getMessage( "failedCreatingObject",
170                                                               this.getResourceFileProcessorClass().getName() ),
171                                          e, this.getLocation() );
172    
173            }
174        }
175    
176        /**
177         * Configures a given {@code ResourceFileProcessor} instance using the properties of the instance.
178         *
179         * @param resourceFileProcessor The resource file processor to configure.
180         *
181         * @throws NullPointerException if {@code resourceFileProcessor} is {@code null}.
182         * @throws BuildException if configuring {@code resourceFileProcessor} fails.
183         *
184         * @see #configureJomcTool(org.jomc.tools.JomcTool)
185         */
186        public void configureResourceFileProcessor( final ResourceFileProcessor resourceFileProcessor )
187            throws BuildException
188        {
189            if ( resourceFileProcessor == null )
190            {
191                throw new NullPointerException( "resourceFileProcessor" );
192            }
193    
194            this.configureJomcTool( resourceFileProcessor );
195    
196            if ( this.getResourceBundleDefaultLanguage() != null )
197            {
198                resourceFileProcessor.setResourceBundleDefaultLocale(
199                    new Locale( this.getResourceBundleDefaultLanguage() ) );
200    
201            }
202        }
203    
204        /**
205         * Calls the {@code processResourceFiles} method if resource processing is enabled.
206         *
207         * @throws BuildException if processing resource files fails.
208         *
209         * @see #processResourceFiles()
210         */
211        @Override
212        public final void executeTask() throws BuildException
213        {
214            if ( this.isResourceProcessingEnabled() )
215            {
216                this.processResourceFiles();
217                this.log( Messages.getMessage( "resourceProcessingSuccess" ) );
218            }
219            else
220            {
221                this.log( Messages.getMessage( "resourceProcessingDisabled" ) );
222            }
223        }
224    
225        /**
226         * Processes resource files.
227         *
228         * @throws BuildException if processing resource files fails.
229         *
230         * @see #executeTask()
231         */
232        public void processResourceFiles() throws BuildException
233        {
234            this.log( Messages.getMessage( "unimplementedTask", this.getClass().getName(), "processResourceFiles" ),
235                      Project.MSG_ERR );
236    
237        }
238    
239        /** {@inheritDoc} */
240        @Override
241        public ResourceFileProcessorTask clone()
242        {
243            return (ResourceFileProcessorTask) super.clone();
244        }
245    
246    }