SourceFileProcessorTask.java

  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. import org.apache.tools.ant.BuildException;
  33. import org.apache.tools.ant.Project;
  34. import org.jomc.tools.SourceFileProcessor;

  35. /**
  36.  * Base class for executing source file processor based tasks.
  37.  *
  38.  * @author <a href="mailto:cs@schulte.it">Christian Schulte</a>
  39.  * @version $JOMC: SourceFileProcessorTask.java 5135 2016-04-08 13:53:07Z schulte $
  40.  * @see #processSourceFiles()
  41.  */
  42. public class SourceFileProcessorTask extends JomcToolTask
  43. {

  44.     /**
  45.      * Controls processing of source files.
  46.      */
  47.     private boolean sourceProcessingEnabled = true;

  48.     /**
  49.      * Class of the {@code SourceFileProcessor} backing the task.
  50.      */
  51.     private Class<? extends SourceFileProcessor> sourceFileProcessorClass;

  52.     /**
  53.      * Creates a new {@code SourceFileProcessorTask} instance.
  54.      */
  55.     public SourceFileProcessorTask()
  56.     {
  57.         super();
  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.      * Sets the flag indicating the processing of source files is enabled.
  72.      *
  73.      * @param value {@code true}, to enable processing of source files; {@code false}, to disable processing of source
  74.      * files.
  75.      *
  76.      * @see #isSourceProcessingEnabled()
  77.      */
  78.     public final void setSourceProcessingEnabled( final boolean value )
  79.     {
  80.         this.sourceProcessingEnabled = value;
  81.     }

  82.     /**
  83.      * Gets the class of the {@code SourceFileProcessor} backing the task.
  84.      *
  85.      * @return The class of the {@code SourceFileProcessor} backing the task.
  86.      *
  87.      * @see #setSourceFileProcessorClass(java.lang.Class)
  88.      */
  89.     public final Class<? extends SourceFileProcessor> getSourceFileProcessorClass()
  90.     {
  91.         if ( this.sourceFileProcessorClass == null )
  92.         {
  93.             this.sourceFileProcessorClass = SourceFileProcessor.class;
  94.         }

  95.         return this.sourceFileProcessorClass;
  96.     }

  97.     /**
  98.      * Sets the class of the {@code SourceFileProcessor} backing the task.
  99.      *
  100.      * @param value The new class of the {@code SourceFileProcessor} backing the task or {@code null}.
  101.      *
  102.      * @see #getSourceFileProcessorClass()
  103.      */
  104.     public final void setSourceFileProcessorClass( final Class<? extends SourceFileProcessor> value )
  105.     {
  106.         this.sourceFileProcessorClass = value;
  107.     }

  108.     /**
  109.      * Creates a new {@code SourceFileProcessor} instance setup using the properties of the instance.
  110.      *
  111.      * @return A new {@code SourceFileProcessor} instance.
  112.      *
  113.      * @throws BuildException if creating a new {@code SourceFileProcessor} instance fails.
  114.      *
  115.      * @see #getSourceFileProcessorClass()
  116.      * @see #configureSourceFileProcessor(org.jomc.tools.SourceFileProcessor)
  117.      */
  118.     public SourceFileProcessor newSourceFileProcessor() throws BuildException
  119.     {
  120.         try
  121.         {
  122.             final SourceFileProcessor sourceFileProcessor = this.getSourceFileProcessorClass().newInstance();
  123.             this.configureSourceFileProcessor( sourceFileProcessor );
  124.             return sourceFileProcessor;
  125.         }
  126.         catch ( final InstantiationException e )
  127.         {
  128.             throw new BuildException( Messages.getMessage( "failedCreatingObject",
  129.                                                            this.getSourceFileProcessorClass().getName() ),
  130.                                       e, this.getLocation() );

  131.         }
  132.         catch ( final IllegalAccessException e )
  133.         {
  134.             throw new BuildException( Messages.getMessage( "failedCreatingObject",
  135.                                                            this.getSourceFileProcessorClass().getName() ),
  136.                                       e, this.getLocation() );

  137.         }
  138.     }

  139.     /**
  140.      * Configures a given {@code SourceFileProcessor} instance using the properties of the instance.
  141.      *
  142.      * @param sourceFileProcessor The source file processor to configure.
  143.      *
  144.      * @throws NullPointerException if {@code sourceFileProcessor} is {@code null}.
  145.      * @throws BuildException if configuring {@code sourceFileProcessor} fails.
  146.      *
  147.      * @see #configureJomcTool(org.jomc.tools.JomcTool)
  148.      */
  149.     public void configureSourceFileProcessor( final SourceFileProcessor sourceFileProcessor ) throws BuildException
  150.     {
  151.         if ( sourceFileProcessor == null )
  152.         {
  153.             throw new NullPointerException( "sourceFileProcessor" );
  154.         }

  155.         this.configureJomcTool( sourceFileProcessor );
  156.     }

  157.     /**
  158.      * Calls the {@code processSourceFiles} method if source processing is enabled.
  159.      *
  160.      * @throws BuildException if processing source files fails.
  161.      *
  162.      * @see #processSourceFiles()
  163.      */
  164.     @Override
  165.     public final void executeTask() throws BuildException
  166.     {
  167.         if ( this.isSourceProcessingEnabled() )
  168.         {
  169.             this.processSourceFiles();
  170.             this.log( Messages.getMessage( "sourceProcessingSuccess" ) );
  171.         }
  172.         else
  173.         {
  174.             this.log( Messages.getMessage( "sourceProcessingDisabled" ) );
  175.         }
  176.     }

  177.     /**
  178.      * Processes source files.
  179.      *
  180.      * @throws BuildException if processing source files fails.
  181.      *
  182.      * @see #executeTask()
  183.      */
  184.     public void processSourceFiles() throws BuildException
  185.     {
  186.         this.log( Messages.getMessage( "unimplementedTask", this.getClass().getName(), "processSourceFiles" ),
  187.                   Project.MSG_ERR );

  188.     }

  189.     /**
  190.      * {@inheritDoc}
  191.      */
  192.     @Override
  193.     public SourceFileProcessorTask clone()
  194.     {
  195.         return (SourceFileProcessorTask) super.clone();
  196.     }

  197. }