1 | package org.apache.velocity.runtime.directive; |
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.IOException; |
20 | import java.io.Writer; |
21 | |
22 | import org.apache.velocity.context.InternalContextAdapter; |
23 | |
24 | import org.apache.velocity.Template; |
25 | import org.apache.velocity.runtime.RuntimeConstants; |
26 | import org.apache.velocity.runtime.parser.node.Node; |
27 | import org.apache.velocity.runtime.parser.node.SimpleNode; |
28 | |
29 | import org.apache.velocity.exception.MethodInvocationException; |
30 | import org.apache.velocity.exception.ParseErrorException; |
31 | import org.apache.velocity.exception.ResourceNotFoundException; |
32 | |
33 | /** |
34 | * Pluggable directive that handles the <code>#parse()</code> |
35 | * statement in VTL. |
36 | * |
37 | * <pre> |
38 | * Notes: |
39 | * ----- |
40 | * 1) The parsed source material can only come from somewhere in |
41 | * the TemplateRoot tree for security reasons. There is no way |
42 | * around this. If you want to include content from elsewhere on |
43 | * your disk, use a link from somwhere under Template Root to that |
44 | * content. |
45 | * |
46 | * 2) There is a limited parse depth. It is set as a property |
47 | * "parse_directive.maxdepth = 10" for example. There is a 20 iteration |
48 | * safety in the event that the parameter isn't set. |
49 | * </pre> |
50 | * |
51 | * @author <a href="mailto:geirm@optonline.net">Geir Magnusson Jr.</a> |
52 | * @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a> |
53 | * @author <a href="mailto:Christoph.Reck@dlr.de">Christoph Reck</a> |
54 | * @version $Id: Parse.java,v 1.27.4.1 2004/03/03 23:22:56 geirm Exp $ |
55 | */ |
56 | public class Parse extends InputBase |
57 | { |
58 | private boolean ready = false; |
59 | |
60 | /** |
61 | * Return name of this directive. |
62 | */ |
63 | public String getName() |
64 | { |
65 | return "parse"; |
66 | } |
67 | |
68 | /** |
69 | * Return type of this directive. |
70 | */ |
71 | public int getType() |
72 | { |
73 | return LINE; |
74 | } |
75 | |
76 | /** |
77 | * iterates through the argument list and renders every |
78 | * argument that is appropriate. Any non appropriate |
79 | * arguments are logged, but render() continues. |
80 | */ |
81 | public boolean render( InternalContextAdapter context, |
82 | Writer writer, Node node) |
83 | throws IOException, ResourceNotFoundException, ParseErrorException, |
84 | MethodInvocationException |
85 | { |
86 | /* |
87 | * did we get an argument? |
88 | */ |
89 | if ( node.jjtGetChild(0) == null) |
90 | { |
91 | rsvc.error( "#parse() error : null argument" ); |
92 | return false; |
93 | } |
94 | |
95 | /* |
96 | * does it have a value? If you have a null reference, then no. |
97 | */ |
98 | Object value = node.jjtGetChild(0).value( context ); |
99 | |
100 | if ( value == null) |
101 | { |
102 | rsvc.error( "#parse() error : null argument" ); |
103 | return false; |
104 | } |
105 | |
106 | /* |
107 | * get the path |
108 | */ |
109 | String arg = value.toString(); |
110 | |
111 | /* |
112 | * see if we have exceeded the configured depth. |
113 | * If it isn't configured, put a stop at 20 just in case. |
114 | */ |
115 | |
116 | Object[] templateStack = context.getTemplateNameStack(); |
117 | |
118 | if ( templateStack.length >= |
119 | rsvc.getInt(RuntimeConstants.PARSE_DIRECTIVE_MAXDEPTH, 20) ) |
120 | { |
121 | StringBuffer path = new StringBuffer(); |
122 | |
123 | for( int i = 0; i < templateStack.length; ++i) |
124 | { |
125 | path.append( " > " + templateStack[i] ); |
126 | } |
127 | |
128 | rsvc.error( "Max recursion depth reached (" + |
129 | templateStack.length + ")" + " File stack:" + path ); |
130 | return false; |
131 | } |
132 | |
133 | /* |
134 | * now use the Runtime resource loader to get the template |
135 | */ |
136 | |
137 | Template t = null; |
138 | |
139 | try |
140 | { |
141 | t = rsvc.getTemplate( arg, getInputEncoding(context) ); |
142 | } |
143 | catch ( ResourceNotFoundException rnfe ) |
144 | { |
145 | /* |
146 | * the arg wasn't found. Note it and throw |
147 | */ |
148 | |
149 | rsvc.error("#parse(): cannot find template '" + arg + |
150 | "', called from template " + |
151 | context.getCurrentTemplateName() + " at (" + |
152 | getLine() + ", " + getColumn() + ")" ); |
153 | throw rnfe; |
154 | } |
155 | catch ( ParseErrorException pee ) |
156 | { |
157 | /* |
158 | * the arg was found, but didn't parse - syntax error |
159 | * note it and throw |
160 | */ |
161 | |
162 | rsvc.error("#parse(): syntax error in #parse()-ed template '" + |
163 | arg + "', called from template " + |
164 | context.getCurrentTemplateName() + " at (" + |
165 | getLine() + ", " + getColumn() + ")" ); |
166 | |
167 | throw pee; |
168 | } |
169 | catch ( Exception e) |
170 | { |
171 | rsvc.error("#parse() : arg = " + arg + ". Exception : " + e); |
172 | return false; |
173 | } |
174 | |
175 | /* |
176 | * and render it |
177 | */ |
178 | try |
179 | { |
180 | context.pushCurrentTemplateName(arg); |
181 | ((SimpleNode) t.getData()).render( context, writer ); |
182 | } |
183 | catch ( Exception e ) |
184 | { |
185 | /* |
186 | * if it's a MIE, it came from the render.... throw it... |
187 | */ |
188 | |
189 | if ( e instanceof MethodInvocationException) |
190 | { |
191 | throw (MethodInvocationException) e; |
192 | } |
193 | |
194 | rsvc.error( "Exception rendering #parse( " + arg + " ) : " + e ); |
195 | return false; |
196 | } |
197 | finally |
198 | { |
199 | context.popCurrentTemplateName(); |
200 | } |
201 | |
202 | return true; |
203 | } |
204 | } |
205 | |