001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: * $Header:$
018: */
019:
020: package org.apache.beehive.controls.system.jdbc.parser;
021:
022: import org.apache.beehive.controls.api.context.ControlBeanContext;
023:
024: import java.lang.reflect.Method;
025: import java.util.ArrayList;
026:
027: /**
028: * The abstract base class for fragment's which may contain child fragments.
029: */
030: public abstract class SqlFragmentContainer extends SqlFragment {
031:
032: /** Child fragments of this container. */
033: protected ArrayList<SqlFragment> _children;
034:
035: /**
036: * Construct a new SqlFragmentContainer instance.
037: */
038: SqlFragmentContainer() {
039: _children = new ArrayList<SqlFragment>();
040: }
041:
042: /**
043: * Does this fragment contain a parameter value for a prepared statement?
044: * @return true If this fragment contains a parameter value for a PreparedStatement.
045: */
046: boolean hasParamValue() {
047: for (SqlFragment f : _children) {
048: if (f.hasParamValue()) {
049: return true;
050: }
051: }
052: return false;
053: }
054:
055: /**
056: * Add a child.
057: * @param child Child to add.
058: */
059: void addChild(SqlFragment child) {
060: _children.add(child);
061: }
062:
063: /**
064: * Return the array of children.
065: * @return An array of SqlFragments.
066: */
067: SqlFragment[] getChildren() {
068: SqlFragment[] fragments = new SqlFragment[_children.size()];
069: return _children.toArray(fragments);
070: }
071:
072: /**
073: * Must be implemented for JUnit testing.
074: * @return The String value of this fragment and all of its child fragments.
075: */
076: public String toString() {
077: StringBuilder s = new StringBuilder();
078: for (SqlFragment f : _children) {
079: s.append(f.toString());
080: }
081: return s.toString();
082: }
083:
084: /**
085: * builds the text of the prepared statement
086: *
087: * @param context A ControlBeanContext instance.
088: * @param m The annotated method.
089: * @param args The method's parameters.
090: * @return The PreparedStatement text generated by this fragment and its children.
091: */
092: String getPreparedStatementText(ControlBeanContext context,
093: Method m, Object[] args) {
094: StringBuilder sb = new StringBuilder();
095: for (SqlFragment sf : _children) {
096: sb.append(sf.getPreparedStatementText(context, m, args));
097: }
098: return sb.toString();
099: }
100: }
|