KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > jelly > impl > StaticTag


1 /*
2  * Copyright 2002,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 package org.apache.commons.jelly.impl;
17
18 import org.apache.commons.jelly.DynaTagSupport;
19 import org.apache.commons.jelly.JellyTagException;
20 import org.apache.commons.jelly.XMLOutput;
21
22 import org.xml.sax.SAXException JavaDoc;
23 import org.xml.sax.helpers.AttributesImpl JavaDoc;
24
25 /**
26  * <p><code>StaticTag</code> represents a static XML element
27  * which echos itself to XMLOutput when it is invoked.</p>
28  *
29  * @author <a HREF="mailto:jstrachan@apache.org">James Strachan</a>
30  * @version $Revision: 231483 $
31  */

32
33 public class StaticTag extends DynaTagSupport {
34
35     /** The namespace URI */
36     private String JavaDoc uri;
37
38     /** The qualified name */
39     private String JavaDoc qname;
40
41     /** The local name */
42     private String JavaDoc localName;
43
44     /** The XML Attributes */
45     private AttributesImpl JavaDoc attributes = new AttributesImpl JavaDoc();
46
47     public StaticTag() {
48     }
49
50     public StaticTag(String JavaDoc uri, String JavaDoc localName, String JavaDoc qname) {
51         this.uri = uri;
52         this.localName = localName;
53         this.qname = qname;
54     }
55
56     public String JavaDoc toString() {
57         return super.toString() + "[qname=" + qname + ";attributes=" + attributes + "]";
58     }
59
60     // Tag interface
61
//-------------------------------------------------------------------------
62
public void doTag(XMLOutput output) throws JellyTagException {
63         try {
64             output.startElement(uri, localName, qname, attributes);
65             invokeBody(output);
66             output.endElement(uri, localName, qname);
67         } catch (SAXException JavaDoc e) {
68             throw new JellyTagException(e);
69         } finally {
70             attributes.clear();
71         }
72     }
73
74     public void setAttribute(String JavaDoc name, String JavaDoc prefix, String JavaDoc nsURI, Object JavaDoc value) {
75         if(value==null)
76             return;
77         if(prefix!=null && prefix.length()>0)
78             attributes.addAttribute(nsURI,name,prefix+":"+name,"CDATA",value.toString());
79         else
80             attributes.addAttribute("",name,name,"CDATA",value.toString());
81     }
82
83     // DynaTag interface
84
//-------------------------------------------------------------------------
85
public void setAttribute(String JavaDoc name, Object JavaDoc value) throws JellyTagException {
86         // ### we'll assume that all attributes are in no namespace!
87
// ### this is severely limiting!
88
// ### - Tag attributes should allow for namespace aware
89
int index = attributes.getIndex("", name);
90         if (index >= 0) {
91             attributes.removeAttribute(index);
92         }
93         // treat null values as no attribute
94
if (value != null) {
95             attributes.addAttribute("", name, name, "CDATA", value.toString());
96         }
97     }
98
99     // Properties
100
//-------------------------------------------------------------------------
101
public String JavaDoc getUri() {
102         return uri;
103     }
104
105     public void setUri(String JavaDoc uri) {
106         this.uri = uri;
107     }
108
109     public String JavaDoc getQName() {
110         return qname;
111     }
112
113     public void setQName(String JavaDoc qname) {
114         this.qname = qname;
115         int idx = qname.indexOf(':');
116         if (idx >= 0) {
117             this.localName = qname.substring(idx + 1);
118         }
119         else {
120             this.localName = qname;
121         }
122     }
123
124     public String JavaDoc getLocalName() {
125         return localName;
126     }
127
128     public void setLocalName(String JavaDoc localName) {
129         this.localName = localName;
130         // FIXME This just doesn't seem right or work...
131
if (qname == null || !qname.endsWith(localName)) {
132             localName = qname;
133         }
134     }
135 }
136
Popular Tags