001: //WebOnSwing - Web Application Framework
002: //Copyright (C) 2003 Fernando Damian Petrola
003: //
004: //This library is free software; you can redistribute it and/or
005: //modify it under the terms of the GNU Lesser General Public
006: //License as published by the Free Software Foundation; either
007: //version 2.1 of the License, or (at your option) any later version.
008: //
009: //This library is distributed in the hope that it will be useful,
010: //but WITHOUT ANY WARRANTY; without even the implied warranty of
011: //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: //Lesser General Public License for more details.
013: //
014: //You should have received a copy of the GNU Lesser General Public
015: //License along with this library; if not, write to the Free Software
016: //Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017:
018: package net.ar.webonswing.render.markup;
019:
020: import java.util.*;
021:
022: import net.ar.webonswing.visitor.*;
023:
024: import org.apache.commons.logging.*;
025:
026: public class Tag extends MarkupElement {
027: String theName;
028: Vector theAttributes;
029: MarkupContainer theMarkupContainer;
030: boolean needsClosure;
031:
032: public Tag(String aName) {
033: theName = aName;
034: theAttributes = new Vector();
035: theMarkupContainer = new MarkupContainer();
036: needsClosure = true;
037: }
038:
039: public Tag addAttribute(TagAttribute aAttribute) {
040: theAttributes.add(aAttribute);
041: return this ;
042: }
043:
044: public Tag addAttribute(String aKey, String aValue) {
045: addAttribute(new TagAttribute(aKey, aValue));
046: return this ;
047: }
048:
049: public Tag addAttribute(String aKey, int aValue) {
050: addAttribute(new TagAttribute(aKey, aValue));
051: return this ;
052: }
053:
054: public void removeAttribute(String anAttributeName) {
055: theAttributes.remove(new TagAttribute(anAttributeName, ""));
056: }
057:
058: public Tag addElementToContainer(MarkupElement anElement) {
059: getTheMarkupContainer().addElement(anElement);
060: return this ;
061: }
062:
063: public Tag addTextToContainer(String aText) {
064: getTheMarkupContainer().addElement(new TagContent(aText));
065: return this ;
066: }
067:
068: public Tag addTextToContainer(StringBuffer aText) {
069: addTextToContainer(aText.toString());
070: return this ;
071: }
072:
073: public boolean isNeedsClosure() {
074: return needsClosure;
075: }
076:
077: public Tag setNeedsClosure(boolean isNeedsClosure) {
078: this .needsClosure = isNeedsClosure;
079: return this ;
080: }
081:
082: public List getTheAttributes() {
083: return theAttributes;
084: }
085:
086: public String getAttributeValueForKey(String anAttributeKey) {
087: for (Iterator i = theAttributes.iterator(); i.hasNext();) {
088: TagAttribute theAttribute = (TagAttribute) i.next();
089: if (theAttribute.getKey().equals(anAttributeKey))
090: return theAttribute.getValue();
091: }
092:
093: return null;
094: }
095:
096: public String getTheName() {
097: return theName;
098: }
099:
100: public void setTheAttributes(Vector anAttributes) {
101: this .theAttributes = anAttributes;
102: }
103:
104: public void setTheName(String aName) {
105: this .theName = aName;
106: }
107:
108: public MarkupContainer getTheMarkupContainer() {
109: return theMarkupContainer;
110: }
111:
112: public void setTheMarkupContainer(MarkupContainer aMarkupContainer) {
113: this .theMarkupContainer = aMarkupContainer;
114: }
115:
116: public Tag addNewTagAttributes(Tag aTag) {
117: for (Iterator i = aTag.getTheAttributes().iterator(); i
118: .hasNext();) {
119: TagAttribute theAttribute = (TagAttribute) i.next();
120:
121: if (!getTheAttributes().contains(theAttribute))
122: addAttribute(theAttribute);
123: }
124:
125: return this ;
126: }
127:
128: public Tag copyNewTagContainer(Tag aTag) {
129: try {
130: setTheMarkupContainer((MarkupContainer) aTag
131: .getTheMarkupContainer().clone());
132: } catch (CloneNotSupportedException e) {
133: LogFactory.getLog(Tag.class).error("", e);
134: }
135:
136: return this ;
137: }
138:
139: public Tag addNewTagAttributesAndName(Tag aTag) {
140: addNewTagAttributes(aTag);
141: theName = aTag.getTheName();
142: return this ;
143: }
144:
145: public Tag addAllTagAttributes(Tag aTag) {
146: for (Iterator i = aTag.getTheAttributes().iterator(); i
147: .hasNext();) {
148: TagAttribute theAttribute = (TagAttribute) i.next();
149:
150: if (getTheAttributes().contains(theAttribute))
151: getTheAttributes().remove(theAttribute);
152:
153: addAttribute(theAttribute);
154: }
155:
156: return this ;
157: }
158:
159: public Tag addAllTagAttributesAndName(Tag aTag) {
160: addAllTagAttributes(aTag);
161: theName = aTag.getTheName();
162: return this ;
163: }
164:
165: public void accept(Visitor aMarkupVisitor) {
166: MarkupVisitor theMarkupVisitor = (MarkupVisitor) aMarkupVisitor;
167:
168: theMarkupVisitor.visitTagBegin(this );
169:
170: for (Iterator i = getTheAttributes().iterator(); i.hasNext();)
171: ((TagAttribute) i.next()).accept(aMarkupVisitor);
172:
173: theMarkupVisitor.visitAllTagAttributesEnd(this );
174: getTheMarkupContainer().accept(aMarkupVisitor);
175: theMarkupVisitor.visitTagEnd(this );
176: }
177:
178: public Object clone() throws CloneNotSupportedException {
179: Tag theTag = (Tag) super .clone();
180:
181: theTag.theAttributes = new Vector();
182: for (Iterator i = theAttributes.iterator(); i.hasNext();)
183: theTag.theAttributes.add(i.next());
184:
185: theTag.theMarkupContainer = (MarkupContainer) theMarkupContainer
186: .clone();
187: theTag.theName = theName;
188: theTag.needsClosure = needsClosure;
189:
190: return theTag;
191: }
192:
193: }
|