001: // Copyright (C) 2003,2004,2005 by Object Mentor, Inc. All rights reserved.
002: // Released under the terms of the GNU General Public License version 2 or later.
003: package fitnesse.html;
004:
005: import java.util.LinkedList;
006: import java.util.List;
007:
008: public class HtmlTag extends HtmlElement {
009: public LinkedList<HtmlElement> childTags = new LinkedList<HtmlElement>();
010: protected List<Attribute> attributes = new LinkedList<Attribute>();
011: protected String tagName = "youreIt";
012: public String tail;
013: public String head;
014:
015: public HtmlTag(String tagName) {
016: this .tagName = tagName;
017: }
018:
019: public HtmlTag(String tagName, String content) {
020: this (tagName);
021: add(content);
022: }
023:
024: public HtmlTag(String tagName, HtmlElement child) {
025: this (tagName);
026: add(child);
027: }
028:
029: public String tagName() {
030: return tagName;
031: }
032:
033: public String html() throws Exception {
034: return html(0);
035: }
036:
037: public String html(int depth) throws Exception {
038: StringBuffer buffer = new StringBuffer();
039: addTabs(depth, buffer);
040:
041: if (head != null)
042: buffer.append(head);
043:
044: buffer.append("<").append(tagName());
045: addAttributes(buffer);
046:
047: if (hasChildren()) {
048: buffer.append(">");
049: boolean tagWasAdded = addChildHtml(buffer, depth);
050: if (tagWasAdded)
051: addTabs(depth, buffer);
052: buffer.append("</").append(tagName()).append(">");
053: } else
054: buffer.append("/>");
055:
056: if (tail != null)
057: buffer.append(tail);
058:
059: buffer.append(endl);
060:
061: return buffer.toString();
062: }
063:
064: private void addAttributes(StringBuffer buffer) {
065: for (Attribute attribute : attributes) {
066: buffer.append(" ").append(attribute.name).append("=\"")
067: .append(attribute.value).append("\"");
068: }
069: }
070:
071: protected void addTabs(int depth, StringBuffer buffer) {
072: for (int i = 0; i < depth; i++)
073: buffer.append('\t');
074: }
075:
076: private boolean addChildHtml(StringBuffer buffer, int depth)
077: throws Exception {
078: boolean addedTag = false;
079: boolean lastAddedWasNonTag = false;
080: int i = 0;
081: for (HtmlElement element : childTags) {
082: if (element instanceof HtmlTag) {
083: if (i == 0 || lastAddedWasNonTag)
084: buffer.append(endl);
085: buffer.append(((HtmlTag) element).html(depth + 1));
086: addedTag = true;
087: lastAddedWasNonTag = false;
088: } else {
089: buffer.append(element.html());
090: lastAddedWasNonTag = true;
091: }
092: i++;
093: }
094:
095: return addedTag;
096: }
097:
098: private boolean hasChildren() {
099: return childTags.size() > 0;
100: }
101:
102: public void add(String s) {
103: add(new RawHtml(s));
104: }
105:
106: public void add(HtmlElement element) {
107: childTags.add(element);
108: }
109:
110: public void addAttribute(String key, String value) {
111: attributes.add(new Attribute(key, value));
112: }
113:
114: public void use(String s) {
115: use(new RawHtml(s));
116: }
117:
118: public void use(HtmlElement element) {
119: childTags.clear();
120: add(element);
121: }
122:
123: public String getAttribute(String key) {
124: for (Attribute attribute : attributes) {
125: if (key != null && key.equals(attribute.name))
126: return attribute.value;
127: }
128: return null;
129: }
130:
131: public static class Attribute {
132: public String name;
133: public String value;
134:
135: public Attribute(String name, String value) {
136: this.name = name;
137: this.value = value;
138: }
139: }
140: }
|