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.parser.ParseException;
33 import org.apache.velocity.runtime.parser.Parser;
34 import org.apache.velocity.runtime.parser.ParserVisitor;
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
57
58
59
60 public ASTDirective(int id)
61 {
62 super(id);
63 }
64
65
66
67
68
69 public ASTDirective(Parser p, int id)
70 {
71 super(p, id);
72 }
73
74
75
76
77
78 public Object jjtAccept(ParserVisitor visitor, Object data)
79 {
80 return visitor.visit(this, data);
81 }
82
83
84
85
86 public Object init( InternalContextAdapter context, Object data)
87 throws TemplateInitException
88 {
89 super.init( context, data );
90
91
92
93
94
95 if (parser.isDirective( directiveName ))
96 {
97 isDirective = true;
98
99 try
100 {
101 directive = (Directive) parser.getDirective( directiveName )
102 .getClass().newInstance();
103 }
104 catch (InstantiationException e)
105 {
106 throw ExceptionUtils.createRuntimeException("Couldn't initialize " +
107 "directive of class " +
108 parser.getDirective(directiveName).getClass().getName(),
109 e);
110 }
111 catch (IllegalAccessException e)
112 {
113 throw ExceptionUtils.createRuntimeException("Couldn't initialize " +
114 "directive of class " +
115 parser.getDirective(directiveName).getClass().getName(),
116 e);
117 }
118
119 directive.init(rsvc, context,this);
120
121 directive.setLocation( getLine(), getColumn() );
122 }
123 else if (rsvc.isVelocimacro( directiveName, context.getCurrentTemplateName() ))
124 {
125
126
127
128
129 isDirective = true;
130 directive = rsvc.getVelocimacro( directiveName, context.getCurrentTemplateName());
131
132 try
133 {
134 directive.init( rsvc, context, this );
135 }
136
137
138
139
140 catch (TemplateInitException die)
141 {
142 throw new TemplateInitException(die.getMessage(),
143 (ParseException) die.getWrappedThrowable(),
144 die.getTemplateName(),
145 die.getColumnNumber() + getColumn(),
146 die.getLineNumber() + getLine());
147 }
148 directive.setLocation( getLine(), getColumn() );
149 }
150 else
151 {
152 isDirective = false;
153 }
154
155 return data;
156 }
157
158
159
160
161 public boolean render( InternalContextAdapter context, Writer writer)
162 throws IOException,MethodInvocationException, ResourceNotFoundException, ParseErrorException
163 {
164
165
166
167
168 if (isDirective)
169 {
170 directive.render(context, writer, this);
171 }
172 else
173 {
174 if (context.getAllowRendering())
175 {
176 writer.write( "#");
177 writer.write( directiveName );
178 }
179 }
180
181 return true;
182 }
183
184
185
186
187
188
189 public void setDirectiveName( String str )
190 {
191 directiveName = str;
192 }
193
194
195
196
197
198 public String getDirectiveName()
199 {
200 return directiveName;
201 }
202
203 public String toString()
204 {
205 return new ToStringBuilder(this)
206 .appendSuper(super.toString())
207 .append("directiveName", getDirectiveName())
208 .toString();
209 }
210
211 }
212
213