1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
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
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 }