001: /**
002: * Copyright 2006 Webmedia Group Ltd.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: **/package org.araneaframework.jsp.tag.basic;
016:
017: import java.io.Writer;
018: import java.util.HashMap;
019: import java.util.Map;
020: import javax.servlet.jsp.JspException;
021: import javax.servlet.jsp.PageContext;
022: import org.araneaframework.jsp.tag.BaseTag;
023: import org.araneaframework.jsp.tag.PresentationTag;
024: import org.araneaframework.jsp.util.JspUtil;
025:
026: /**
027: * Element tag.
028: *
029: * @author Oleg Mürk
030: *
031: * @jsp.tag
032: * name = "element"
033: * body-content = "JSP"
034: * description = "Defines an HTML element."
035: */
036: public class ElementHtmlTag extends BaseTag implements
037: AttributedTagInterface {
038: public final static String KEY = "org.araneaframework.jsp.tag.basic.ElementHtmlTag.KEY";
039:
040: protected String name = null;
041: protected Map attributes = new HashMap();
042: protected boolean hasContent = false;
043:
044: public void setPageContext(PageContext pageContext) {
045: attributes = new HashMap();
046: hasContent = false;
047: name = null;
048:
049: super .setPageContext(pageContext);
050: }
051:
052: protected int doStartTag(Writer out) throws Exception {
053: super .doStartTag(out);
054:
055: addContextEntry(KEY, this );
056: addContextEntry(PresentationTag.ATTRIBUTED_TAG_KEY, this );
057:
058: JspUtil.writeOpenStartTag(out, name);
059:
060: // Continue
061: return EVAL_BODY_INCLUDE;
062: }
063:
064: /**
065: * After tag.
066: */
067: protected int doEndTag(Writer out) throws Exception {
068: if (hasContent)
069: JspUtil.writeEndTag_SS(out, name);
070: else {
071: JspUtil.writeAttributes(out, attributes);
072: JspUtil.writeCloseStartEndTag_SS(out);
073: }
074: return super .doEndTag(out);
075: }
076:
077: public void addAttribute(String name, String value)
078: throws JspException {
079: value = (String) evaluate("value", value, String.class);
080: attributes.put(name, value);
081: }
082:
083: protected void onContent() {
084: this .hasContent = true;
085: }
086:
087: protected void writeAttributes(Writer out) throws Exception {
088: JspUtil.writeAttributes(out, attributes);
089: }
090:
091: /* ***********************************************************************************
092: * Tag attributes
093: * ***********************************************************************************/
094:
095: /**
096: * @jsp.attribute
097: * type = "java.lang.String"
098: * required = "true"
099: * description = "HTML element name."
100: */
101: public void setName(String name) throws JspException {
102: this .name = (String) evaluateNotNull("name", name, String.class);
103: }
104: }
|