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.IOException;
23 import java.io.Writer;
24 import java.util.Iterator;
25 import java.util.List;
26
27 import org.apache.velocity.app.event.EventCartridge;
28 import org.apache.velocity.context.Context;
29 import org.apache.velocity.context.InternalContextAdapter;
30 import org.apache.velocity.exception.MethodInvocationException;
31 import org.apache.velocity.exception.ParseErrorException;
32 import org.apache.velocity.exception.ResourceNotFoundException;
33 import org.apache.velocity.exception.TemplateInitException;
34 import org.apache.velocity.runtime.RuntimeConstants;
35 import org.apache.velocity.runtime.RuntimeServices;
36 import org.apache.velocity.runtime.parser.node.ASTReference;
37 import org.apache.velocity.runtime.parser.node.Node;
38 import org.apache.velocity.runtime.parser.node.SimpleNode;
39 import org.apache.velocity.runtime.resource.Resource;
40 import org.apache.velocity.util.introspection.Info;
41 import org.apache.velocity.util.introspection.IntrospectionCacheData;
42
43 /**
44 * Break directive used for interrupting foreach loops.
45 *
46 * @author <a href="mailto:wyla@removethis.sci.fi">Jarkko Viinamaki</a>
47 * @version $Id$
48 */
49 public class Break extends Directive
50 {
51 /**
52 * Return name of this directive.
53 * @return The name of this directive.
54 */
55 public String getName()
56 {
57 return "break";
58 }
59
60 /**
61 * Return type of this directive.
62 * @return The type of this directive.
63 */
64 public int getType()
65 {
66 return LINE;
67 }
68
69 /**
70 * simple init - init the tree and get the elementKey from
71 * the AST
72 * @param rs
73 * @param context
74 * @param node
75 * @throws TemplateInitException
76 */
77 public void init(RuntimeServices rs, InternalContextAdapter context, Node node)
78 throws TemplateInitException
79 {
80 super.init(rs, context, node);
81 }
82
83 /**
84 * Break directive does not actually do any rendering.
85 *
86 * This directive throws a BreakException (RuntimeException) which
87 * signals foreach directive to break out of the loop. Note that this
88 * directive does not verify that it is being called inside a foreach
89 * loop.
90 *
91 * @param context
92 * @param writer
93 * @param node
94 * @return true if the directive rendered successfully.
95 * @throws IOException
96 * @throws MethodInvocationException
97 * @throws ResourceNotFoundException
98 * @throws ParseErrorException
99 */
100 public boolean render(InternalContextAdapter context,
101 Writer writer, Node node)
102 throws IOException, MethodInvocationException, ResourceNotFoundException,
103 ParseErrorException
104 {
105 throw new BreakException();
106 }
107
108 public static class BreakException extends RuntimeException
109 {
110
111 }
112 }