01: /**********************************************************************************
02:
03: Feedzeo!
04: A free and open source RSS/Atom/RDF feed aggregator
05:
06: Copyright (C) 2005-2006 Anand Rao (anandrao@users.sourceforge.net)
07:
08: This library is free software; you can redistribute it and/or
09: modify it under the terms of the GNU Lesser General Public
10: License as published by the Free Software Foundation; either
11: version 2.1 of the License, or (at your option) any later version.
12:
13: This library is distributed in the hope that it will be useful,
14: but WITHOUT ANY WARRANTY; without even the implied warranty of
15: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16: Lesser General Public License for more details.
17:
18: You should have received a copy of the GNU Lesser General Public
19: License along with this library; if not, write to the Free Software
20: Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21:
22: ************************************************************************************/package util;
23:
24: import java.io.*;
25: import java.util.*;
26:
27: public class HTMLTag extends LinkedList {
28:
29: LinkedList tagAttrs;
30: String tagName;
31: boolean closeTag;
32:
33: public HTMLTag(String name) {
34: tagName = name;
35: tagAttrs = new LinkedList();
36: closeTag = true;
37: }
38:
39: public HTMLTag(String name, boolean closeTag) {
40: tagName = name;
41: tagAttrs = new LinkedList();
42: this .closeTag = closeTag;
43: }
44:
45: public void addAttribute(String attr, String value) {
46: if ((attr != null) && (value != null))
47: tagAttrs.add(new HTMLTagAttr(attr, value));
48: }
49:
50: /* removes the first attribute with the specified attrname */
51: public boolean removeAttribute(String attr) {
52: boolean elemRemoved = false;
53: ListIterator i = tagAttrs.listIterator();
54: while (i.hasNext()) {
55: HTMLTagAttr tAttr = (HTMLTagAttr) i.next();
56: if (tAttr.getName().equalsIgnoreCase(attr)) {
57: tagAttrs.remove(tAttr);
58: elemRemoved = true;
59: break;
60: }
61: }
62: return elemRemoved;
63: }
64:
65: public String toString() {
66: ListIterator i = null;
67: StringBuffer sb = new StringBuffer();
68: // add the tag name & attributes
69: sb.append("<" + tagName.toLowerCase());
70: i = tagAttrs.listIterator();
71: while (i.hasNext()) {
72: HTMLTagAttr tAttr = (HTMLTagAttr) i.next();
73: sb.append(" " + tAttr.toString());
74: }
75: sb.append(">");
76: // add the contents of this tag which may contain other tags
77: i = this .listIterator();
78: while (i.hasNext()) {
79: sb.append(i.next().toString());
80: }
81: // now close the tag if closeTag is set
82: if (closeTag)
83: sb.append("</" + tagName.toLowerCase() + ">\n");
84: return sb.toString();
85: }
86: }
|