View Javadoc

1   /*
2    * Copyright 2011 - Doxia :: Include Macro - Juergen Kellerer
3    *
4    *    Licensed under the Apache License, Version 2.0 (the "License");
5    *    you may not use this file except in compliance with the License.
6    *    You may obtain a copy of the License at
7    *
8    *        http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *    Unless required by applicable law or agreed to in writing, software
11   *    distributed under the License is distributed on an "AS IS" BASIS,
12   *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *    See the License for the specific language governing permissions and
14   *    limitations under the License.
15   */
16  
17  package org.tinyjee.maven.dim.extensions;
18  
19  import org.apache.maven.doxia.logging.Log;
20  import org.tinyjee.maven.dim.spi.Globals;
21  import org.tinyjee.maven.dim.spi.RequestParameterTransformer;
22  
23  import java.util.HashMap;
24  import java.util.Map;
25  
26  import static org.tinyjee.maven.dim.IncludeMacroSignature.PARAM_SOURCE;
27  import static org.tinyjee.maven.dim.IncludeMacroSignature.PARAM_SOURCE_CLASS;
28  
29  /**
30   * Implements a base for bundled request parameter transformers.
31   *
32   * @author Juergen_Kellerer, 2011-11-08
33   */
34  public abstract class AbstractParameterTransformer implements RequestParameterTransformer {
35  
36  	protected abstract boolean doTransformParameters(Map<String, Object> requestParams);
37  
38  	protected boolean isSourceSet(Map<String, Object> requestParams) {
39  		return requestParams.containsKey(PARAM_SOURCE) && requestParams.containsKey(PARAM_SOURCE_CLASS);
40  	}
41  
42  	public final void transformParameters(Map<String, Object> requestParams) {
43  		final Log log = Globals.getLog();
44  		if (!isSourceSet(requestParams)) {
45  			final Map<String, Object> originalParams = new HashMap<String, Object>(requestParams);
46  			if (doTransformParameters(requestParams)) {
47  				if (log.isDebugEnabled()) {
48  					log.debug("The parameter transformer " + this + " changed the request parameters from " + originalParams +
49  							" to " + requestParams);
50  				}
51  			} else if (!originalParams.equals(requestParams)) {
52  				log.warn("The parameter transformer " + this + " changed the request parameters from " + originalParams +
53  						" to " + requestParams + " without reporting it.");
54  			}
55  		}
56  	}
57  }