View Javadoc

1   /*
2    * This file is part of Pease Plate Template Engine.
3    * 
4    * Pease Plate Template Engine is free software: you can redistribute
5    * it and/or modify it under the terms of the GNU Lesser General 
6    * Public License as published by the Free Software Foundation, 
7    * either version 3 of the License, or any later version.
8    * 
9    * Pease Plate Template Engine is distributed in the hope that it 
10   * will be useful, but WITHOUT ANY WARRANTY; without even the implied
11   * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12   * See the GNU Lesser General Public License for more details.
13   * 
14   * You should have received a copy of the GNU Lesser General Public 
15   * License along with Pease Plate Template Engine. If not, see 
16   * <http://www.gnu.org/licenses/>.
17   * 
18   * Copyright (c) 2008 Manfred HANTSCHEL
19   */
20  package org.peaseplate.internal.lang.command;
21  
22  import java.lang.reflect.InvocationTargetException;
23  import java.lang.reflect.Method;
24  
25  import org.peaseplate.TemplateRuntimeException;
26  import org.peaseplate.internal.BuildContext;
27  import org.peaseplate.internal.util.ReflectionException;
28  import org.peaseplate.internal.util.ReflectionUtils;
29  import org.peaseplate.lang.TemplateParserException;
30  import org.peaseplate.locator.TemplateLocator;
31  import org.peaseplate.service.TransformerService;
32  
33  public class TransformerCommand extends AbstractNativeCallCommand {
34  
35  	private final String name;
36  	private final String extension;
37  	
38  	private final Object instance;
39  	private final Method method;
40  	
41  	public TransformerCommand(
42  		TemplateLocator locator, int line, int column, 
43  		TransformerService transformerService, String name, String extension, 
44  		ICommand command, ICommand... parameterCommands
45  	) throws TemplateParserException {
46  	    super(locator, line, column, command, parameterCommands);
47  
48  	    this.name = name;
49  	    this.extension = extension;
50  	    
51  	    instance = transformerService.getInstance(name);
52  	    
53  	    if (instance == null)
54  	    	throw new TemplateParserException(
55  	    		getLocator(), getLine(), getColumn(),
56  	    		"The transformer " + name + " is not defined"
57  	    	);
58  	    
59  	    method = transformerService.getMethod(name, extension, (parameterCommands != null) ? parameterCommands.length : 0);
60  	    
61  	    if (method == null) 
62  	    	throw new TemplateParserException(
63  	    		getLocator(), getLine(), getColumn(),
64  	    		"The transformer " + name + ((extension != null) ? "." + extension : "") + " is not defined"
65  	    	);
66      }
67  	
68  	/**
69  	 * @see org.peaseplate.internal.lang.command.ICommand#call(org.peaseplate.internal.BuildContext)
70  	 */
71  	public Object call(BuildContext context) throws TemplateRuntimeException {
72  		try {
73  			Object parameters[] = ReflectionUtils.callParameterCommands(
74  				context, getParameterCommands(), method,
75  				context, callCommand(context)
76  			);
77  
78  			return method.invoke(instance, parameters);
79          }
80  		catch (ReflectionException e) {
81          	throw new TemplateRuntimeException(
82          		getLocator(), getLine(), getColumn(),
83          		"Cound not invoke " + method, e
84          	);
85  		}
86          catch (IllegalArgumentException e) {
87          	throw new TemplateRuntimeException(
88          		getLocator(), getLine(), getColumn(),
89          		"Exception while invocing " + method, e
90          	);
91          }
92          catch (IllegalAccessException e) {
93          	throw new TemplateRuntimeException(
94          		getLocator(), getLine(), getColumn(),
95          		"Cannot access " + method, e
96          	);
97          }
98          catch (InvocationTargetException e) {
99          	throw new TemplateRuntimeException(
100         		getLocator(), getLine(), getColumn(),
101         		"Exception while invocing " + method, e
102         	);
103         }
104     }
105 	
106 	/**
107 	 * @see org.peaseplate.internal.lang.command.AbstractNativeCallCommand#toString()
108 	 */
109 	@Override
110     public String toString() {
111 		StringBuilder result = new StringBuilder();
112 		
113 		result.append(getCommand());
114 		result.append(" -> ");
115 		result.append(name);
116 		
117 		if (extension != null)
118 			result.append(".").append(extension);
119 		
120 		result.append(super.toString());
121 
122 		return result.toString();
123     }
124 	
125 }