KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.util.ArrayList JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.List JavaDoc;
23 import java.util.Map JavaDoc;
24
25 import javax.servlet.ServletRequest JavaDoc;
26 import javax.servlet.jsp.JspException JavaDoc;
27 import javax.servlet.jsp.JspWriter JavaDoc;
28 import javax.servlet.jsp.PageContext JavaDoc;
29
30
31 /** A tag to produce an XML element which can contain other attributes
32   * or elements like the <code>&lt;xsl:element&gt;</code> tag.
33   *
34   * @author James Strachan
35   * @version $Revision: 1.2 $
36   */

37 public class ElementTag extends AbstractBodyTag {
38
39     /** Should attribute values be trimmed of whitespace? */
40     protected static final boolean TRIM_VALUES = true;
41     
42     /** Holds value of property name. */
43     private String JavaDoc name;
44     private List JavaDoc attributeNames;
45     private Map JavaDoc attributeValues;
46     
47     //-------------------------------------------------------------------------
48
public ElementTag() {
49     }
50
51     public void addAttribute( String JavaDoc name ) {
52         if ( attributeNames == null ) {
53             attributeNames = new ArrayList JavaDoc();
54         }
55         attributeNames.add( name );
56     }
57
58     public void setAttributeValue( String JavaDoc name, String JavaDoc value ) {
59         if ( attributeValues == null ) {
60             attributeValues = new HashMap JavaDoc();
61         }
62         attributeValues.put( name, value );
63     }
64
65     // BodyTag interface
66
//-------------------------------------------------------------------------
67
public void release() {
68         super.release();
69         name = null;
70         attributeNames = null;
71         attributeValues = null;
72     }
73
74     public int doStartTag() throws JspException JavaDoc {
75         return EVAL_BODY_TAG;
76     }
77     
78     public int doAfterBody() throws JspException JavaDoc {
79         JspWriter JavaDoc out = bodyContent.getEnclosingWriter();
80         //JspWriter out = pageContext.getOut();
81
try {
82             out.print( "<" + getName() );
83             printAttributes( out );
84             
85             String JavaDoc content = bodyContent.getString();
86             if ( content == null || content.length() <= 0 ) {
87                 out.print( "/>" );
88             }
89             else {
90                 out.print( ">" );
91                 out.print( content );
92                 out.print( "</" + getName() + ">" );
93             }
94             bodyContent.clearBody();
95         }
96         catch ( IOException JavaDoc e ) {
97             handleException( e );
98         }
99         return SKIP_BODY;
100     }
101     
102     // Properties
103
//-------------------------------------------------------------------------
104

105     /** Getter for property name.
106      * @return Value of property name.
107      */

108     public String JavaDoc getName() {
109         return name;
110     }
111     /** Setter for property name.
112      * @param name New value of property name.
113      */

114     public void setName(String JavaDoc name) {
115         this.name = name;
116     }
117     
118     // Implementation methods
119
//-------------------------------------------------------------------------
120
protected void printAttributes( JspWriter JavaDoc out ) throws IOException JavaDoc {
121         if ( attributeNames != null ) {
122             int size = attributeNames.size();
123             for ( int i = 0; i < size; i++ ) {
124                 String JavaDoc attributeName = (String JavaDoc) attributeNames.get( i );
125                 printAttribute( out, attributeName );
126             }
127         }
128     }
129     
130     protected void printAttribute( JspWriter JavaDoc out, String JavaDoc attributeName ) throws IOException JavaDoc {
131         Object JavaDoc value = null;
132         if ( attributeValues != null ) {
133             value = attributeValues.get( attributeName );
134         }
135         String JavaDoc text = attributeName;
136         if ( value != null ) {
137             if ( TRIM_VALUES ) {
138                 text = value.toString().trim();
139             }
140             else {
141                 text = value.toString();
142             }
143         }
144         out.print( " " + attributeName + "=\"" + text + "\"" );
145     }
146 }
147
Popular Tags