KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > access > jdbc > ChunkDirective


1 /*****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. 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,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  ****************************************************************/

19
20 package org.apache.cayenne.access.jdbc;
21
22 import java.io.IOException JavaDoc;
23 import java.io.Writer JavaDoc;
24
25 import org.apache.velocity.context.InternalContextAdapter;
26 import org.apache.velocity.exception.MethodInvocationException;
27 import org.apache.velocity.exception.ParseErrorException;
28 import org.apache.velocity.exception.ResourceNotFoundException;
29 import org.apache.velocity.runtime.directive.Directive;
30 import org.apache.velocity.runtime.parser.node.Node;
31
32 /**
33  * A custom Velocity directive to describe a conditional chunk of a {@link ChainDirective chain}.
34  * Usage of chunk is the following:
35  *
36  * <pre>
37  * #chunk()...#end - e.g. #chunk()A = 5#end
38  * #chunk(condition)...#end - e.g. #chunk($a)A = $a#end</pre>
39  *
40  * <p>If condition is evaluated to false, chunk is not included in the chain,
41  * if it is true, chunk is included, and if it is not the first chunk, it is
42  * prefixed with chain join.
43  *
44  * @since 1.1
45  * @author Andrus Adamchik
46  */

47 public class ChunkDirective extends Directive {
48
49     public String JavaDoc getName() {
50         return "chunk";
51     }
52
53     public int getType() {
54         return BLOCK;
55     }
56
57     public boolean render(InternalContextAdapter context, Writer JavaDoc writer, Node node)
58         throws
59             IOException JavaDoc,
60             ResourceNotFoundException,
61             ParseErrorException,
62             MethodInvocationException {
63
64         // first child is an expression, second is BLOCK
65
if (node.jjtGetNumChildren() > 1 && !node.jjtGetChild(0).evaluate(context)) {
66             // return value is really meaningless in Velocity...whatever
67
return false;
68         }
69
70         // BLOCK is the last child
71
Node block = node.jjtGetChild(node.jjtGetNumChildren() - 1);
72         block.render(context, writer);
73         return true;
74     }
75
76 }
77
Popular Tags