1 | package org.apache.velocity.runtime.parser.node; |
2 | |
3 | /* |
4 | * Copyright 2000-2001,2004 The Apache Software Foundation. |
5 | * |
6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
7 | * you may not use this file except in compliance with the License. |
8 | * You may obtain a copy of the License at |
9 | * |
10 | * http://www.apache.org/licenses/LICENSE-2.0 |
11 | * |
12 | * Unless required by applicable law or agreed to in writing, software |
13 | * distributed under the License is distributed on an "AS IS" BASIS, |
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
15 | * See the License for the specific language governing permissions and |
16 | * limitations under the License. |
17 | */ |
18 | |
19 | import java.io.Writer; |
20 | import java.io.IOException; |
21 | |
22 | import org.apache.velocity.context.InternalContextAdapter; |
23 | import org.apache.velocity.runtime.directive.Directive; |
24 | import org.apache.velocity.runtime.parser.Parser; |
25 | |
26 | import org.apache.velocity.exception.MethodInvocationException; |
27 | import org.apache.velocity.exception.ParseErrorException; |
28 | import org.apache.velocity.exception.ResourceNotFoundException; |
29 | |
30 | /** |
31 | * This class is responsible for handling the pluggable |
32 | * directives in VTL. ex. #foreach() |
33 | * |
34 | * Please look at the Parser.jjt file which is |
35 | * what controls the generation of this class. |
36 | * |
37 | * @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a> |
38 | * @author <a href="mailto:geirm@optonline.net">Geir Magnusson Jr.</a> |
39 | * @author <a href="mailto:kav@kav.dk">Kasper Nielsen</a> |
40 | * @version $Id: ASTDirective.java,v 1.21.4.1 2004/03/03 23:22:58 geirm Exp $ |
41 | */ |
42 | public class ASTDirective extends SimpleNode |
43 | { |
44 | private Directive directive; |
45 | private String directiveName = ""; |
46 | private boolean isDirective; |
47 | |
48 | public ASTDirective(int id) |
49 | { |
50 | super(id); |
51 | } |
52 | |
53 | public ASTDirective(Parser p, int id) |
54 | { |
55 | super(p, id); |
56 | } |
57 | |
58 | |
59 | /** Accept the visitor. **/ |
60 | public Object jjtAccept(ParserVisitor visitor, Object data) |
61 | { |
62 | return visitor.visit(this, data); |
63 | } |
64 | |
65 | public Object init( InternalContextAdapter context, Object data) |
66 | throws Exception |
67 | { |
68 | super.init( context, data ); |
69 | |
70 | /* |
71 | * only do things that are not context dependant |
72 | */ |
73 | |
74 | if (parser.isDirective( directiveName )) |
75 | { |
76 | isDirective = true; |
77 | |
78 | directive = (Directive) parser.getDirective( directiveName ) |
79 | .getClass().newInstance(); |
80 | |
81 | directive.init(rsvc, context,this); |
82 | |
83 | directive.setLocation( getLine(), getColumn() ); |
84 | } |
85 | else if (rsvc.isVelocimacro( directiveName, context.getCurrentTemplateName() )) |
86 | { |
87 | /* |
88 | * we seem to be a Velocimacro. |
89 | */ |
90 | |
91 | isDirective = true; |
92 | directive = (Directive) rsvc.getVelocimacro( directiveName, context.getCurrentTemplateName() ); |
93 | |
94 | directive.init( rsvc, context, this ); |
95 | directive.setLocation( getLine(), getColumn() ); |
96 | } |
97 | else |
98 | { |
99 | isDirective = false; |
100 | } |
101 | |
102 | return data; |
103 | } |
104 | |
105 | public boolean render( InternalContextAdapter context, Writer writer) |
106 | throws IOException,MethodInvocationException, ResourceNotFoundException, ParseErrorException |
107 | { |
108 | /* |
109 | * normal processing |
110 | */ |
111 | |
112 | if (isDirective) |
113 | { |
114 | directive.render(context, writer, this); |
115 | } |
116 | else |
117 | { |
118 | writer.write( "#"); |
119 | writer.write( directiveName ); |
120 | } |
121 | |
122 | return true; |
123 | } |
124 | |
125 | /** |
126 | * Sets the directive name. Used by the parser. This keeps us from having to |
127 | * dig it out of the token stream and gives the parse the change to override. |
128 | */ |
129 | public void setDirectiveName( String str ) |
130 | { |
131 | directiveName = str; |
132 | return; |
133 | } |
134 | |
135 | /** |
136 | * Gets the name of this directive. |
137 | */ |
138 | public String getDirectiveName() |
139 | { |
140 | return directiveName; |
141 | } |
142 | } |
143 | |
144 | |