View Javadoc

1   /*
2    * Copyright 2004-2010 the Seasar Foundation and the Others.
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,
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  
17  package org.seasar.cubby.tags;
18  
19  import static org.seasar.cubby.internal.util.LogMessages.format;
20  
21  import java.io.IOException;
22  import java.io.StringWriter;
23  
24  import javax.servlet.jsp.JspException;
25  import javax.servlet.jsp.tagext.SimpleTagSupport;
26  
27  /**
28   * パラメータを指定するためのカスタムタグです。
29   * 
30   * @author baba
31   */
32  public class ParamTag extends SimpleTagSupport {
33  
34  	/** パラメータ名。 */
35  	private String name;
36  
37  	/** 値。 */
38  	private String value;
39  
40  	/**
41  	 * パラメータ名を設定します。
42  	 * 
43  	 * @param name
44  	 *            パラメータ名
45  	 */
46  	public void setName(final String name) {
47  		this.name = name;
48  	}
49  
50  	/**
51  	 * 値を設定します。
52  	 * 
53  	 * @param value
54  	 *            値
55  	 */
56  	public void setValue(final String value) {
57  		this.value = value;
58  	}
59  
60  	/**
61  	 * {@inheritDoc}
62  	 */
63  	@Override
64  	public void doTag() throws JspException, IOException {
65  		final ParamParent parent = (ParamParent) findAncestorWithClass(this,
66  				ParamParent.class);
67  		if (parent == null) {
68  			throw new JspException(format("ECUB1004"));
69  		}
70  		final String value;
71  		if (this.value == null) {
72  			final StringWriter writer = new StringWriter();
73  			getJspBody().invoke(writer);
74  			value = writer.toString().trim();
75  		} else {
76  			value = this.value;
77  		}
78  		parent.addParameter(name, value);
79  	}
80  
81  }