KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > controls > system > jdbc > parser > SqlFragmentContainer


1 /*
2  * Copyright 2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * $Header:$
17  */

18
19 package org.apache.beehive.controls.system.jdbc.parser;
20
21 import org.apache.beehive.controls.api.context.ControlBeanContext;
22
23 import java.lang.reflect.Method JavaDoc;
24 import java.util.ArrayList JavaDoc;
25
26 /**
27  * The abstract base class for fragment's which may contain child fragments.
28  */

29 public abstract class SqlFragmentContainer extends SqlFragment {
30
31     /** Child fragments of this container. */
32     protected ArrayList JavaDoc<SqlFragment> _children;
33
34     /**
35      * Construct a new SqlFragmentContainer instance.
36      */

37     SqlFragmentContainer() {
38         _children = new ArrayList JavaDoc<SqlFragment>();
39     }
40
41     /**
42      * Does this fragment contain a parameter value for a prepared statement?
43      * @return true If this fragment contains a parameter value for a PreparedStatement.
44      */

45     boolean hasParamValue() {
46         for (SqlFragment f : _children) {
47             if (f.hasParamValue()) {
48                 return true;
49             }
50         }
51         return false;
52     }
53
54     /**
55      * Add a child.
56      * @param child Child to add.
57      */

58     void addChild(SqlFragment child) {
59         _children.add(child);
60     }
61
62     /**
63      * Return the array of children.
64      * @return An array of SqlFragments.
65      */

66     SqlFragment[] getChildren() {
67         SqlFragment[] fragments = new SqlFragment[_children.size()];
68         return _children.toArray(fragments);
69     }
70
71     /**
72      * Must be implemented for JUnit testing.
73      * @return The String value of this fragment and all of its child fragments.
74      */

75     public String JavaDoc toString() {
76         StringBuilder JavaDoc s = new StringBuilder JavaDoc();
77         for (SqlFragment f : _children) {
78             s.append(f.toString());
79         }
80         return s.toString();
81     }
82
83     /**
84      * builds the text of the prepared statement
85      *
86      * @param context A ControlBeanContext instance.
87      * @param m The annotated method.
88      * @param args The method's parameters.
89      * @return The PreparedStatement text generated by this fragment and its children.
90      */

91     String JavaDoc getPreparedStatementText(ControlBeanContext context, Method JavaDoc m, Object JavaDoc[] args) {
92         StringBuilder JavaDoc sb = new StringBuilder JavaDoc();
93         for (SqlFragment sf : _children) {
94             sb.append(sf.getPreparedStatementText(context, m, args));
95         }
96         return sb.toString();
97     }
98 }
99
Popular Tags