001: /*
002: * Copyright 2002,2004 The Apache Software Foundation.
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: */
016: package org.apache.commons.jelly.impl;
017:
018: import org.apache.commons.jelly.DynaTagSupport;
019: import org.apache.commons.jelly.JellyTagException;
020: import org.apache.commons.jelly.XMLOutput;
021:
022: import org.xml.sax.SAXException;
023: import org.xml.sax.helpers.AttributesImpl;
024:
025: /**
026: * <p><code>StaticTag</code> represents a static XML element
027: * which echos itself to XMLOutput when it is invoked.</p>
028: *
029: * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
030: * @version $Revision: 155420 $
031: */
032:
033: public class StaticTag extends DynaTagSupport {
034:
035: /** The namespace URI */
036: private String uri;
037:
038: /** The qualified name */
039: private String qname;
040:
041: /** The local name */
042: private String localName;
043:
044: /** The XML Attributes */
045: private AttributesImpl attributes = new AttributesImpl();
046:
047: public StaticTag() {
048: }
049:
050: public StaticTag(String uri, String localName, String qname) {
051: this .uri = uri;
052: this .localName = localName;
053: this .qname = qname;
054: }
055:
056: public String toString() {
057: return super .toString() + "[qname=" + qname + ";attributes="
058: + attributes + "]";
059: }
060:
061: // Tag interface
062: //-------------------------------------------------------------------------
063: public void doTag(XMLOutput output) throws JellyTagException {
064: try {
065: output.startElement(uri, localName, qname, attributes);
066: invokeBody(output);
067: output.endElement(uri, localName, qname);
068: } catch (SAXException e) {
069: throw new JellyTagException(e);
070: }
071: }
072:
073: // DynaTag interface
074: //-------------------------------------------------------------------------
075: public void setAttribute(String name, Object value)
076: throws JellyTagException {
077: // ### we'll assume that all attributes are in no namespace!
078: // ### this is severely limiting!
079: // ### - Tag attributes should allow for namespace aware
080: int index = attributes.getIndex("", name);
081: if (index >= 0) {
082: attributes.removeAttribute(index);
083: }
084: // treat null values as no attribute
085: if (value != null) {
086: attributes.addAttribute("", name, name, "CDATA", value
087: .toString());
088: }
089: }
090:
091: // Properties
092: //-------------------------------------------------------------------------
093: public String getUri() {
094: return uri;
095: }
096:
097: public void setUri(String uri) {
098: this .uri = uri;
099: }
100:
101: public String getQName() {
102: return qname;
103: }
104:
105: public void setQName(String qname) {
106: this .qname = qname;
107: int idx = qname.indexOf(':');
108: if (idx >= 0) {
109: this .localName = qname.substring(idx + 1);
110: } else {
111: this .localName = qname;
112: }
113: }
114:
115: public String getLocalName() {
116: return localName;
117: }
118:
119: public void setLocalName(String localName) {
120: this .localName = localName;
121: // FIXME This just doesn't seem right or work...
122: if (qname == null || !qname.endsWith(localName)) {
123: localName = qname;
124: }
125: }
126: }
|