| 1 | /* |
| 2 | * Copyright (C) Christian Schulte, 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 4613 2012-09-22 10:07:08Z schulte $ |
| 29 | * |
| 30 | */ |
| 31 | package org.jomc.ant; |
| 32 | |
| 33 | import java.util.Locale; |
| 34 | import org.apache.tools.ant.BuildException; |
| 35 | import org.apache.tools.ant.Project; |
| 36 | import org.jomc.tools.ResourceFileProcessor; |
| 37 | |
| 38 | /** |
| 39 | * Base class for executing resource file processor based tasks. |
| 40 | * |
| 41 | * @author <a href="mailto:cs@schulte.it">Christian Schulte</a> |
| 42 | * @version $JOMC: ResourceFileProcessorTask.java 4613 2012-09-22 10:07:08Z schulte $ |
| 43 | * @see #processResourceFiles() |
| 44 | */ |
| 45 | public class ResourceFileProcessorTask extends JomcToolTask |
| 46 | { |
| 47 | |
| 48 | /** The language of the default language properties file of generated resource bundle resources. */ |
| 49 | private String resourceBundleDefaultLanguage; |
| 50 | |
| 51 | /** Controls processing of resource files. */ |
| 52 | private boolean resourceProcessingEnabled = true; |
| 53 | |
| 54 | /** Class of the {@code ResourceFileProcessor} backing the task. */ |
| 55 | private Class<? extends ResourceFileProcessor> resourceFileProcessorClass; |
| 56 | |
| 57 | /** Creates a new {@code ResourceFileProcessorTask} instance. */ |
| 58 | public ResourceFileProcessorTask() |
| 59 | { |
| 60 | super(); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Gets a flag indicating the processing of resources is enabled. |
| 65 | * |
| 66 | * @return {@code true}, if processing of resources is enabled; {@code false}, else. |
| 67 | * |
| 68 | * @see #setResourceProcessingEnabled(boolean) |
| 69 | */ |
| 70 | public final boolean isResourceProcessingEnabled() |
| 71 | { |
| 72 | return this.resourceProcessingEnabled; |
| 73 | } |
| 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 | /** |
| 88 | * Gets the language of the default language properties file of generated resource bundle resource files. |
| 89 | * |
| 90 | * @return The language of the default language properties file of generated resource bundle resource files or |
| 91 | * {@code null}. |
| 92 | * |
| 93 | * @see #setResourceBundleDefaultLanguage(java.lang.String) |
| 94 | */ |
| 95 | public final String getResourceBundleDefaultLanguage() |
| 96 | { |
| 97 | return this.resourceBundleDefaultLanguage; |
| 98 | } |
| 99 | |
| 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 | } |