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