01: /**
02: * Copyright 2006 Webmedia Group Ltd.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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: **/package org.araneaframework.jsp.tag.basic;
16:
17: import java.io.IOException;
18: import java.io.Writer;
19: import javax.servlet.jsp.JspException;
20: import org.araneaframework.jsp.tag.BaseTag;
21: import org.araneaframework.jsp.tag.PresentationTag;
22:
23: /**
24: * Attribute tag, tries to define attribute of the containing tag (element).
25: * First searches for HTML element from <code>PageContext</code> under the
26: * {@link org.araneaframework.jsp.tag.basic.AttributedTagInterface#HTML_ELEMENT_KEY}
27: * and if it is found writes out javascript that should set elements attribute.
28: *
29: * When this does not succeed, searches {@link org.araneaframework.jsp.tag.basic.AttributedTagInterface#ATTRIBUTED_TAG_KEY}
30: * and tries to attach attribute to found {@link org.araneaframework.jsp.tag.basic.AttributedTagInterface}, if any.
31: * This only works when {@link org.araneaframework.jsp.tag.basic.AttributedTagInterface} has been implemented
32: * correctly by containing tag and whole HTML for containing tag is written out in <code>doEndTag()</code> method.
33: *
34: * @author Oleg Mürk
35: *
36: * @jsp.tag
37: * name = "attribute"
38: * body-content = "empty"
39: * description = "Defines an attribute of the containing element."
40: */
41: public class AttributeHtmlTag extends BaseTag {
42: protected String name;
43: protected String value;
44:
45: protected int doStartTag(Writer out) throws Exception {
46: super .doStartTag(out);
47:
48: String elementKey = (String) getContextEntry(AttributedTagInterface.HTML_ELEMENT_KEY);
49: if (elementKey != null) {
50: writeAttributeScript(out, elementKey);
51: return SKIP_BODY;
52: }
53:
54: AttributedTagInterface attributedTag = (AttributedTagInterface) requireContextEntry(PresentationTag.ATTRIBUTED_TAG_KEY);
55: attributedTag.addAttribute(name, value);
56:
57: return SKIP_BODY;
58: }
59:
60: public void writeAttributeScript(Writer out, String elementKey)
61: throws IOException {
62: out.write("<script type=\"text/javascript\">");
63: out.write("setElementAttr(\"");
64: out.write(elementKey);
65: out.write("\", \"");
66: out.write(name);
67: out.write("\", \"");
68: out.write(value);
69: out.write("\");");
70: out.write("</script>");
71: }
72:
73: /* ***********************************************************************************
74: * Tag attributes
75: * ***********************************************************************************/
76:
77: /**
78: * @jsp.attribute
79: * type = "java.lang.String"
80: * required = "true"
81: * description = "Attribute name."
82: */
83: public void setName(String name) throws JspException {
84: this .name = (String) evaluateNotNull("name", name, String.class);
85: }
86:
87: /**
88: * @jsp.attribute
89: * type = "java.lang.String"
90: * required = "true"
91: * description = "Attribute value."
92: */
93: public void setValue(String value) throws JspException {
94: this .value = (String) evaluate("value", value, String.class);
95: }
96: }
|