ResourceFileProcessorTask.java

  1. /*
  2.  *   Copyright (C) Christian Schulte <cs@schulte.it>, 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: ResourceFileProcessorTask.java 5043 2015-05-27 07:03:39Z schulte $
  29.  *
  30.  */
  31. package org.jomc.ant;

  32. import java.util.Locale;
  33. import org.apache.tools.ant.BuildException;
  34. import org.apache.tools.ant.Project;
  35. import org.jomc.tools.ResourceFileProcessor;

  36. /**
  37.  * Base class for executing resource file processor based tasks.
  38.  *
  39.  * @author <a href="mailto:cs@schulte.it">Christian Schulte</a>
  40.  * @version $JOMC: ResourceFileProcessorTask.java 5043 2015-05-27 07:03:39Z schulte $
  41.  * @see #processResourceFiles()
  42.  */
  43. public class ResourceFileProcessorTask extends JomcToolTask
  44. {

  45.     /**
  46.      * The language of the default language properties file of generated resource bundle resources.
  47.      */
  48.     private String resourceBundleDefaultLanguage;

  49.     /**
  50.      * Controls processing of resource files.
  51.      */
  52.     private boolean resourceProcessingEnabled = true;

  53.     /**
  54.      * Class of the {@code ResourceFileProcessor} backing the task.
  55.      */
  56.     private Class<? extends ResourceFileProcessor> resourceFileProcessorClass;

  57.     /**
  58.      * Creates a new {@code ResourceFileProcessorTask} instance.
  59.      */
  60.     public ResourceFileProcessorTask()
  61.     {
  62.         super();
  63.     }

  64.     /**
  65.      * Gets a flag indicating the processing of resources is enabled.
  66.      *
  67.      * @return {@code true}, if processing of resources is enabled; {@code false}, else.
  68.      *
  69.      * @see #setResourceProcessingEnabled(boolean)
  70.      */
  71.     public final boolean isResourceProcessingEnabled()
  72.     {
  73.         return this.resourceProcessingEnabled;
  74.     }

  75.     /**
  76.      * Sets the flag indicating the processing of resources is enabled.
  77.      *
  78.      * @param value {@code true}, to enable processing of resources; {@code false}, to disable processing of resources.
  79.      *
  80.      * @see #isResourceProcessingEnabled()
  81.      */
  82.     public final void setResourceProcessingEnabled( final boolean value )
  83.     {
  84.         this.resourceProcessingEnabled = value;
  85.     }

  86.     /**
  87.      * Gets the language of the default language properties file of generated resource bundle resource files.
  88.      *
  89.      * @return The language of the default language properties file of generated resource bundle resource files or
  90.      * {@code null}.
  91.      *
  92.      * @see #setResourceBundleDefaultLanguage(java.lang.String)
  93.      */
  94.     public final String getResourceBundleDefaultLanguage()
  95.     {
  96.         return this.resourceBundleDefaultLanguage;
  97.     }

  98.     /**
  99.      * Sets the language of the default language properties file of generated resource bundle resource files.
  100.      *
  101.      * @param value The new language of the default language properties file of generated resource bundle resource files
  102.      * or {@code null}.
  103.      *
  104.      * @see #getResourceBundleDefaultLanguage()
  105.      */
  106.     public final void setResourceBundleDefaultLanguage( final String value )
  107.     {
  108.         this.resourceBundleDefaultLanguage = value;
  109.     }

  110.     /**
  111.      * Gets the class of the {@code ResourceFileProcessor} backing the task.
  112.      *
  113.      * @return The class of the {@code ResourceFileProcessor} backing the task.
  114.      *
  115.      * @see #setResourceFileProcessorClass(java.lang.Class)
  116.      */
  117.     public final Class<? extends ResourceFileProcessor> getResourceFileProcessorClass()
  118.     {
  119.         if ( this.resourceFileProcessorClass == null )
  120.         {
  121.             this.resourceFileProcessorClass = ResourceFileProcessor.class;
  122.         }

  123.         return this.resourceFileProcessorClass;
  124.     }

  125.     /**
  126.      * Sets the class of the {@code ResourceFileProcessor} backing the task.
  127.      *
  128.      * @param value The new class of the {@code ResourceFileProcessor} backing the task or {@code null}.
  129.      *
  130.      * @see #getResourceFileProcessorClass()
  131.      */
  132.     public final void setResourceFileProcessorClass( final Class<? extends ResourceFileProcessor> value )
  133.     {
  134.         this.resourceFileProcessorClass = value;
  135.     }

  136.     /**
  137.      * Creates a new {@code ResourceFileProcessor} instance setup using the properties of the instance.
  138.      *
  139.      * @return A new {@code ResourceFileProcessor} instance.
  140.      *
  141.      * @throws BuildException if creating a new {@code ResourceFileProcessor} instance fails.
  142.      *
  143.      * @see #getResourceFileProcessorClass()
  144.      * @see #configureResourceFileProcessor(org.jomc.tools.ResourceFileProcessor)
  145.      */
  146.     public ResourceFileProcessor newResourceFileProcessor() throws BuildException
  147.     {
  148.         try
  149.         {
  150.             final ResourceFileProcessor resourceFileProcessor = this.getResourceFileProcessorClass().newInstance();
  151.             this.configureResourceFileProcessor( resourceFileProcessor );
  152.             return resourceFileProcessor;
  153.         }
  154.         catch ( final InstantiationException e )
  155.         {
  156.             throw new BuildException( Messages.getMessage( "failedCreatingObject",
  157.                                                            this.getResourceFileProcessorClass().getName() ),
  158.                                       e, this.getLocation() );

  159.         }
  160.         catch ( final IllegalAccessException e )
  161.         {
  162.             throw new BuildException( Messages.getMessage( "failedCreatingObject",
  163.                                                            this.getResourceFileProcessorClass().getName() ),
  164.                                       e, this.getLocation() );

  165.         }
  166.     }

  167.     /**
  168.      * Configures a given {@code ResourceFileProcessor} instance using the properties of the instance.
  169.      *
  170.      * @param resourceFileProcessor The resource file processor to configure.
  171.      *
  172.      * @throws NullPointerException if {@code resourceFileProcessor} is {@code null}.
  173.      * @throws BuildException if configuring {@code resourceFileProcessor} fails.
  174.      *
  175.      * @see #configureJomcTool(org.jomc.tools.JomcTool)
  176.      */
  177.     public void configureResourceFileProcessor( final ResourceFileProcessor resourceFileProcessor )
  178.         throws BuildException
  179.     {
  180.         if ( resourceFileProcessor == null )
  181.         {
  182.             throw new NullPointerException( "resourceFileProcessor" );
  183.         }

  184.         this.configureJomcTool( resourceFileProcessor );

  185.         if ( this.getResourceBundleDefaultLanguage() != null )
  186.         {
  187.             resourceFileProcessor.setResourceBundleDefaultLocale(
  188.                 new Locale( this.getResourceBundleDefaultLanguage() ) );

  189.         }
  190.     }

  191.     /**
  192.      * Calls the {@code processResourceFiles} method if resource processing is enabled.
  193.      *
  194.      * @throws BuildException if processing resource files fails.
  195.      *
  196.      * @see #processResourceFiles()
  197.      */
  198.     @Override
  199.     public final void executeTask() throws BuildException
  200.     {
  201.         if ( this.isResourceProcessingEnabled() )
  202.         {
  203.             this.processResourceFiles();
  204.             this.log( Messages.getMessage( "resourceProcessingSuccess" ) );
  205.         }
  206.         else
  207.         {
  208.             this.log( Messages.getMessage( "resourceProcessingDisabled" ) );
  209.         }
  210.     }

  211.     /**
  212.      * Processes resource files.
  213.      *
  214.      * @throws BuildException if processing resource files fails.
  215.      *
  216.      * @see #executeTask()
  217.      */
  218.     public void processResourceFiles() throws BuildException
  219.     {
  220.         this.log( Messages.getMessage( "unimplementedTask", this.getClass().getName(), "processResourceFiles" ),
  221.                   Project.MSG_ERR );

  222.     }

  223.     /**
  224.      * {@inheritDoc}
  225.      */
  226.     @Override
  227.     public ResourceFileProcessorTask clone()
  228.     {
  229.         return (ResourceFileProcessorTask) super.clone();
  230.     }

  231. }