1 package org.apache.velocity.runtime.parser.node;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.IOException;
23 import java.io.Writer;
24
25 import org.apache.commons.lang.builder.ToStringBuilder;
26 import org.apache.velocity.context.InternalContextAdapter;
27 import org.apache.velocity.exception.MethodInvocationException;
28 import org.apache.velocity.exception.ParseErrorException;
29 import org.apache.velocity.exception.ResourceNotFoundException;
30 import org.apache.velocity.exception.TemplateInitException;
31 import org.apache.velocity.runtime.directive.Directive;
32 import org.apache.velocity.runtime.directive.RuntimeMacro;
33 import org.apache.velocity.runtime.parser.ParseException;
34 import org.apache.velocity.runtime.parser.Parser;
35 import org.apache.velocity.util.ExceptionUtils;
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51 public class ASTDirective extends SimpleNode
52 {
53 private Directive directive = null;
54 private String directiveName = "";
55 private boolean isDirective;
56 private boolean isInitialized;
57
58
59
60
61 public ASTDirective(int id)
62 {
63 super(id);
64 }
65
66
67
68
69
70 public ASTDirective(Parser p, int id)
71 {
72 super(p, id);
73 }
74
75
76
77
78
79 public Object jjtAccept(ParserVisitor visitor, Object data)
80 {
81 return visitor.visit(this, data);
82 }
83
84
85
86
87 public synchronized Object init( InternalContextAdapter context, Object data)
88 throws TemplateInitException
89 {
90
91
92 if (!isInitialized)
93 {
94 super.init( context, data );
95
96
97
98
99
100 if (parser.isDirective( directiveName ))
101 {
102 isDirective = true;
103
104 try
105 {
106 directive = (Directive) parser.getDirective( directiveName )
107 .getClass().newInstance();
108 }
109 catch (InstantiationException e)
110 {
111 throw ExceptionUtils.createRuntimeException("Couldn't initialize " +
112 "directive of class " +
113 parser.getDirective(directiveName).getClass().getName(),
114 e);
115 }
116 catch (IllegalAccessException e)
117 {
118 throw ExceptionUtils.createRuntimeException("Couldn't initialize " +
119 "directive of class " +
120 parser.getDirective(directiveName).getClass().getName(),
121 e);
122 }
123
124 directive.setLocation(getLine(), getColumn(), getTemplateName());
125 directive.init(rsvc, context,this);
126 }
127 else
128 {
129
130
131
132 directive = new RuntimeMacro(directiveName, getTemplateName());
133 directive.setLocation(getLine(), getColumn(), getTemplateName());
134
135
136
137
138 try
139 {
140 directive.init( rsvc, context, this );
141 }
142
143
144
145
146 catch (TemplateInitException die)
147 {
148 throw new TemplateInitException(die.getMessage(),
149 (ParseException) die.getWrappedThrowable(),
150 die.getTemplateName(),
151 die.getColumnNumber() + getColumn(),
152 die.getLineNumber() + getLine());
153 }
154 isDirective = true;
155 }
156
157 isInitialized = true;
158 }
159
160 return data;
161 }
162
163
164
165
166 public boolean render( InternalContextAdapter context, Writer writer)
167 throws IOException,MethodInvocationException, ResourceNotFoundException, ParseErrorException
168 {
169
170
171
172
173 if (isDirective)
174 {
175 directive.render(context, writer, this);
176 }
177 else
178 {
179 if (context.getAllowRendering())
180 {
181 writer.write( "#");
182 writer.write( directiveName );
183 }
184 }
185
186 return true;
187 }
188
189
190
191
192
193
194 public void setDirectiveName( String str )
195 {
196 directiveName = str;
197 }
198
199
200
201
202
203 public String getDirectiveName()
204 {
205 return directiveName;
206 }
207
208
209
210
211 public String toString()
212 {
213 return new ToStringBuilder(this)
214 .appendSuper(super.toString())
215 .append("directiveName", getDirectiveName())
216 .toString();
217 }
218
219 }
220
221
222