View Javadoc

1   /*
2    * Copyright 2010 - org.tinyjee.maven
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.utils;
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.Map;
24  
25  import static org.tinyjee.maven.dim.IncludeMacroSignature.PARAM_SOURCE_CLASS;
26  
27  /**
28   * Implements an handler that allows the definition of aliases for request parameters.
29   */
30  public abstract class AbstractAliasHandler implements RequestParameterTransformer {
31  
32  	final String triggerParameter, targetParameter, sourceClass;
33  
34  	/**
35  	 * Constructs a new AbstractAliasHandler for the given values.
36  	 *
37  	 * @param triggerParameter the parameter to trigger.
38  	 * @param targetParameter  the parameter to transfer the trigger value to.
39  	 * @param sourceClass      the source class to set if the trigger was present.
40  	 */
41  	protected AbstractAliasHandler(String triggerParameter, String targetParameter, String sourceClass) {
42  		this.triggerParameter = triggerParameter;
43  		this.targetParameter = targetParameter;
44  		this.sourceClass = sourceClass;
45  	}
46  
47  	/**
48  	 * {@inheritDoc}
49  	 */
50  	public void transformParameters(Map<String, Object> requestParams) {
51  		Log logger = Globals.getLog();
52  		Object parameterValue = requestParams.get(triggerParameter);
53  		if (parameterValue != null) {
54  			if (sourceClass != null) {
55  				if (requestParams.containsKey(PARAM_SOURCE_CLASS)) {
56  					logger.warn("The request parameter '" + PARAM_SOURCE_CLASS + "' is already set to '" +
57  							requestParams.get(PARAM_SOURCE_CLASS) + "'. Will not set the parameter to '" + sourceClass + "' " +
58  							"which may break subsequent template execution and may lead to unexpected results.");
59  				} else
60  					requestParams.put(PARAM_SOURCE_CLASS, sourceClass);
61  			}
62  
63  			if (requestParams.containsKey(targetParameter)) {
64  				logger.warn("The request parameter '" + targetParameter + "' is already set to '" +
65  						requestParams.get(targetParameter) + "', overwriting it with '" + parameterValue + "'. " +
66  						"This may may lead to unexpected results.");
67  			}
68  
69  			requestParams.put(targetParameter, parameterValue);
70  		}
71  	}
72  }