1 /*
2 * Copyright 2002,2004 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 package org.apache.commons.jelly.tags.sql;
17
18 import javax.servlet.jsp.jstl.sql.SQLExecutionTag;
19
20 import org.apache.commons.jelly.JellyTagException;
21 import org.apache.commons.jelly.TagSupport;
22 import org.apache.commons.jelly.XMLOutput;
23 import org.apache.commons.jelly.tags.Resources;
24
25 /***
26 * <p>Tag handler for <Param> in JSTL, used to set
27 * parameter values for a SQL statement.</p>
28 *
29 * @author Hans Bergsten
30 */
31
32 public class ParamTag extends TagSupport {
33 protected Object value;
34
35
36 public void setValue(Object value) {
37 this.value = value;
38 }
39
40 //**********************************************************************
41 // Tag logic
42
43 public void doTag(XMLOutput output) throws JellyTagException {
44 SQLExecutionTag parent =
45 (SQLExecutionTag) findAncestorWithClass(this, SQLExecutionTag.class);
46 if (parent == null) {
47 throw new JellyTagException(Resources.getMessage("SQL_PARAM_OUTSIDE_PARENT"));
48 }
49
50 Object paramValue = value;
51 if (value != null) {
52 paramValue = value;
53 }
54 else {
55 String bodyContent = getBodyText();
56 if (bodyContent != null) {
57 bodyContent = bodyContent.trim();
58 if (bodyContent.length() > 0) {
59 paramValue = bodyContent;
60 }
61 }
62 }
63
64 parent.addSQLParameter(paramValue);
65 }
66 }