1 package org.apache.velocity.runtime.directive;
2
3 /*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21
22 import java.io.Writer;
23 import org.apache.velocity.context.InternalContextAdapter;
24 import org.apache.velocity.exception.TemplateInitException;
25 import org.apache.velocity.exception.VelocityException;
26 import org.apache.velocity.runtime.log.Log;
27 import org.apache.velocity.runtime.RuntimeConstants;
28 import org.apache.velocity.runtime.RuntimeServices;
29 import org.apache.velocity.runtime.parser.node.Node;
30
31 /**
32 * Directive that puts an unrendered AST block in the context
33 * under the specified key, postponing rendering until the
34 * reference is used and rendered.
35 *
36 * @author Andrew Tetlaw
37 * @author Nathan Bubna
38 * @version $Id: Define.java 686842 2008-08-18 18:29:31Z nbubna $
39 */
40 public class Define extends Block
41 {
42 /**
43 * Return name of this directive.
44 */
45 public String getName()
46 {
47 return "define";
48 }
49
50 /**
51 * simple init - get the key
52 */
53 public void init(RuntimeServices rs, InternalContextAdapter context, Node node)
54 throws TemplateInitException
55 {
56 super.init(rs, context, node);
57
58 // the first child is the block name (key), the second child is the block AST body
59 if ( node.jjtGetNumChildren() != 2 )
60 {
61 throw new VelocityException("parameter missing: block name at "
62 + Log.formatFileString(this));
63 }
64
65 /*
66 * first token is the name of the block. We don't even check the format,
67 * just assume it looks like this: $block_name. Should we check if it has
68 * a '$' or not?
69 */
70 key = node.jjtGetChild(0).getFirstToken().image.substring(1);
71
72 /*
73 * default max depth of two is used because intentional recursion is
74 * unlikely and discouraged, so make unintentional ones end fast
75 */
76 maxDepth = rs.getInt(RuntimeConstants.DEFINE_DIRECTIVE_MAXDEPTH, 2);
77 }
78
79 /**
80 * directive.render() simply makes an instance of the Block inner class
81 * and places it into the context as indicated.
82 */
83 public boolean render(InternalContextAdapter context, Writer writer, Node node)
84 {
85 /* put a Block.Reference instance into the context,
86 * using the user-defined key, for later inline rendering.
87 */
88 context.put(key, new Reference(context, this));
89 return true;
90 }
91
92 }