001/* 002 * Copyright (C) 2005 Christian Schulte <cs@schulte.it> 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: SourceFileProcessorTask.java 5135 2016-04-08 13:53:07Z schulte $ 029 * 030 */ 031package org.jomc.ant; 032 033import org.apache.tools.ant.BuildException; 034import org.apache.tools.ant.Project; 035import org.jomc.tools.SourceFileProcessor; 036 037/** 038 * Base class for executing source file processor based tasks. 039 * 040 * @author <a href="mailto:cs@schulte.it">Christian Schulte</a> 041 * @version $JOMC: SourceFileProcessorTask.java 5135 2016-04-08 13:53:07Z schulte $ 042 * @see #processSourceFiles() 043 */ 044public class SourceFileProcessorTask extends JomcToolTask 045{ 046 047 /** 048 * Controls processing of source files. 049 */ 050 private boolean sourceProcessingEnabled = true; 051 052 /** 053 * Class of the {@code SourceFileProcessor} backing the task. 054 */ 055 private Class<? extends SourceFileProcessor> sourceFileProcessorClass; 056 057 /** 058 * Creates a new {@code SourceFileProcessorTask} instance. 059 */ 060 public SourceFileProcessorTask() 061 { 062 super(); 063 } 064 065 /** 066 * Gets a flag indicating the processing of source files is enabled. 067 * 068 * @return {@code true}, if processing of source files is enabled; {@code false}, else. 069 * 070 * @see #setSourceProcessingEnabled(boolean) 071 */ 072 public final boolean isSourceProcessingEnabled() 073 { 074 return this.sourceProcessingEnabled; 075 } 076 077 /** 078 * Sets the flag indicating the processing of source files is enabled. 079 * 080 * @param value {@code true}, to enable processing of source files; {@code false}, to disable processing of source 081 * files. 082 * 083 * @see #isSourceProcessingEnabled() 084 */ 085 public final void setSourceProcessingEnabled( final boolean value ) 086 { 087 this.sourceProcessingEnabled = value; 088 } 089 090 /** 091 * Gets the class of the {@code SourceFileProcessor} backing the task. 092 * 093 * @return The class of the {@code SourceFileProcessor} backing the task. 094 * 095 * @see #setSourceFileProcessorClass(java.lang.Class) 096 */ 097 public final Class<? extends SourceFileProcessor> getSourceFileProcessorClass() 098 { 099 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}