KickJava   Java API By Example, From Geeks To Geeks.

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


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.StringWriter JavaDoc;
24 import java.io.Writer JavaDoc;
25
26 import org.apache.velocity.context.InternalContextAdapter;
27 import org.apache.velocity.exception.MethodInvocationException;
28 import org.apache.velocity.exception.ParseErrorException;
29 import org.apache.velocity.exception.ResourceNotFoundException;
30 import org.apache.velocity.runtime.directive.Directive;
31 import org.apache.velocity.runtime.parser.node.ASTDirective;
32 import org.apache.velocity.runtime.parser.node.Node;
33
34 /**
35  * A custom Velocity directive to conditionally join a number of {@link ChunkDirective chunks}.
36  * Usage of chain is the following:
37  *
38  * <pre>
39  * #chain(operator) - e.g. #chain(' AND ')
40  * #chain(operator prefix) - e.g. #chain(' AND ' 'WHERE ')</pre>
41  *
42  * <p><code>operator</code> (e.g. AND, OR, etc.) is used to join chunks that are included
43  * in a chain. <code>prefix</code> is inserted if a chain contains at least one chunk.
44  * </p>
45  *
46  * @since 1.1
47  * @author Andrus Adamchik
48  */

49 public class ChainDirective extends Directive {
50
51     public String JavaDoc getName() {
52         return "chain";
53     }
54
55     public int getType() {
56         return BLOCK;
57     }
58
59     public boolean render(InternalContextAdapter context, Writer JavaDoc writer, Node node)
60         throws
61             IOException JavaDoc,
62             ResourceNotFoundException,
63             ParseErrorException,
64             MethodInvocationException {
65
66         int size = node.jjtGetNumChildren();
67         if (size == 0) {
68             return true;
69         }
70
71         // BLOCK is the last child
72
Node block = node.jjtGetChild(node.jjtGetNumChildren() - 1);
73         String JavaDoc join = (size > 1) ? (String JavaDoc) node.jjtGetChild(0).value(context) : "";
74         String JavaDoc prefix = (size > 2) ? (String JavaDoc) node.jjtGetChild(1).value(context) : "";
75
76         // if there is a conditional prefix, use a separate buffer ofr children
77
StringWriter JavaDoc childWriter = new StringWriter JavaDoc(30);
78
79         int len = block.jjtGetNumChildren();
80         int includedChunks = 0;
81         for (int i = 0; i < len; i++) {
82             Node child = block.jjtGetChild(i);
83
84             // if this is a "chunk", evaluate its expression and prepend join if included...
85
if (child instanceof ASTDirective
86                 && "chunk".equals(((ASTDirective) child).getDirectiveName())) {
87
88                 if (child.jjtGetNumChildren() < 2
89                     || child.jjtGetChild(0).evaluate(context)) {
90
91                     if (includedChunks > 0) {
92                         childWriter.write(join);
93                     }
94
95                     includedChunks++;
96                 }
97             }
98
99             child.render(context, childWriter);
100         }
101
102         if (includedChunks > 0) {
103             childWriter.flush();
104             writer.write(prefix);
105             writer.write(childWriter.toString());
106         }
107
108         return true;
109     }
110 }
111
Popular Tags