KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > taglibs > xtags > xpath > AttributeTag


1 /*
2  * Copyright 1999,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
17 package org.apache.taglibs.xtags.xpath;
18
19 import java.io.IOException JavaDoc;
20
21 import javax.servlet.ServletRequest JavaDoc;
22 import javax.servlet.jsp.JspTagException JavaDoc;
23 import javax.servlet.jsp.JspWriter JavaDoc;
24 import javax.servlet.jsp.PageContext JavaDoc;
25 import javax.servlet.jsp.tagext.Tag JavaDoc;
26
27 /** Adds an XML attribute to the parent element tag like
28   * the <code>&lt;xsl:attribute&gt;</code> tag.
29   *
30   * @author James Strachan
31   * @version $Revision: 1.2 $
32   */

33 public class AttributeTag extends AbstractBodyTag {
34     
35     /** Holds attributeValue of property name. */
36     private String JavaDoc name;
37     private ElementTag elementTag;
38     
39
40     public AttributeTag() {
41     }
42     
43     // BodyTag interface
44
//-------------------------------------------------------------------------
45
public void release() {
46         super.release();
47         name = null;
48         elementTag = null;
49     }
50
51     public int doStartTag() throws JspTagException JavaDoc {
52         getElementTag().addAttribute( getName() );
53         return EVAL_BODY_TAG;
54     }
55     
56     public int doAfterBody() throws JspTagException JavaDoc {
57         getElementTag().setAttributeValue( getName(), bodyContent.getString() );
58         bodyContent.clearBody();
59         return SKIP_BODY;
60     }
61     
62     // Properties
63
//-------------------------------------------------------------------------
64

65     /** Getter for property name.
66      * @return AttributeValue of property name.
67      */

68     public String JavaDoc getName() {
69         return name;
70     }
71     /** Setter for property name.
72      * @param name New attributeValue of property name.
73      */

74     public void setName(String JavaDoc name) {
75         this.name = name;
76     }
77     
78     // Implementation methods
79
//-------------------------------------------------------------------------
80
protected ElementTag getElementTag() throws JspTagException JavaDoc {
81         if ( elementTag != null ) {
82             return elementTag;
83         }
84         Tag JavaDoc tag = findAncestorWithClass( this, ElementTag.class );
85         if ( tag != null ) {
86             if ( tag instanceof ElementTag ) {
87                 elementTag = (ElementTag) tag;
88                 return elementTag;
89             }
90         }
91         throw new JspTagException JavaDoc( "<attribute> tag must be enclosed inside an <element> tag" );
92     }
93 }
94
Popular Tags