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
24 import org.apache.velocity.context.InternalContextAdapter;
25 import org.apache.velocity.exception.VelocityException;
26 import org.apache.velocity.runtime.RuntimeServices;
27 import org.apache.velocity.runtime.log.Log;
28 import org.apache.velocity.runtime.parser.node.Node;
29
30 /**
31 * This class implements the #stop directive which allows
32 * a user to stop the merging and rendering process. The #stop directive
33 * will accept a single message argument with info about the reason for
34 * stopping.
35 */
36 public class Stop extends Directive
37 {
38 private static final StopCommand STOP_ALL = new StopCommand("StopCommand to exit merging");
39
40 private boolean hasMessage = false;
41
42 /**
43 * Return name of this directive.
44 * @return The name of this directive.
45 */
46 public String getName()
47 {
48 return "stop";
49 }
50
51 /**
52 * Return type of this directive.
53 * @return The type of this directive.
54 */
55 public int getType()
56 {
57 return LINE;
58 }
59
60 /**
61 * Since there is no processing of content,
62 * there is never a need for an internal scope.
63 */
64 public boolean isScopeProvided()
65 {
66 return false;
67 }
68
69 public void init(RuntimeServices rs, InternalContextAdapter context, Node node)
70 {
71 super.init(rs, context, node);
72
73 int kids = node.jjtGetNumChildren();
74 if (kids > 1)
75 {
76 throw new VelocityException("The #stop directive only accepts a single message parameter at "
77 + Log.formatFileString(this));
78 }
79 else
80 {
81 hasMessage = (kids == 1);
82 }
83 }
84
85 public boolean render(InternalContextAdapter context, Writer writer, Node node)
86 {
87 if (!hasMessage)
88 {
89 throw STOP_ALL;
90 }
91
92 Object argument = node.jjtGetChild(0).value(context);
93
94 // stop all and use specified message
95 throw new StopCommand(String.valueOf(argument));
96 }
97
98 }
99