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.VelocityException;
25 import org.apache.velocity.runtime.RuntimeServices;
26 import org.apache.velocity.runtime.log.Log;
27 import org.apache.velocity.runtime.parser.node.Node;
28
29 /**
30 * Break directive used for interrupting scopes.
31 *
32 * @author <a href="mailto:wyla@removethis.sci.fi">Jarkko Viinamaki</a>
33 * @author Nathan Bubna
34 * @version $Id$
35 */
36 public class Break extends Directive
37 {
38 private boolean scoped = false;
39
40 /**
41 * Return name of this directive.
42 * @return The name of this directive.
43 */
44 public String getName()
45 {
46 return "break";
47 }
48
49 /**
50 * Return type of this directive.
51 * @return The type of this directive.
52 */
53 public int getType()
54 {
55 return LINE;
56 }
57
58 /**
59 * Since there is no processing of content,
60 * there is never a need for an internal scope.
61 */
62 public boolean isScopeProvided()
63 {
64 return false;
65 }
66
67 /**
68 * simple init - init the tree and get the elementKey from
69 * the AST
70 * @param rs
71 * @param context
72 * @param node
73 * @throws TemplateInitException
74 */
75 public void init(RuntimeServices rs, InternalContextAdapter context, Node node)
76 {
77 super.init(rs, context, node);
78
79 int kids = node.jjtGetNumChildren();
80 if (kids > 1)
81 {
82 throw new VelocityException("The #stop directive only accepts a single scope object at "
83 + Log.formatFileString(this));
84 }
85 else
86 {
87 this.scoped = (kids == 1);
88 }
89 }
90
91 /**
92 * Break directive does not actually do any rendering.
93 *
94 * This directive throws a StopCommand which signals either
95 * the nearest Scope or the specified scope to stop rendering
96 * its content.
97 *
98 * @param context
99 * @param writer
100 * @param node
101 * @return never, always throws a StopCommand
102 */
103 public boolean render(InternalContextAdapter context, Writer writer, Node node)
104 {
105 if (!scoped)
106 {
107 throw new StopCommand();
108 }
109
110 Object argument = node.jjtGetChild(0).value(context);
111 if (argument instanceof Scope)
112 {
113 ((Scope)argument).stop();
114 }
115 else
116 {
117 throw new VelocityException(node.jjtGetChild(0).literal()+
118 " is not a valid " + Scope.class.getName() + " instance at "
119 + Log.formatFileString(this));
120 }
121 return false;
122 }
123
124 }