001: /*
002: * $Id: Tag.java,v 1.8 2002/09/16 08:05:03 jkl Exp $
003: *
004: * Copyright (c) 2002 Njet Communications Ltd. All Rights Reserved.
005: *
006: * Use is subject to license terms, as defined in
007: * Anvil Sofware License, Version 1.1. See LICENSE
008: * file, or http://njet.org/license-1.1.txt
009: */
010: package anvil.parser;
011:
012: import java.util.ArrayList;
013: import java.util.Iterator;
014:
015: /**
016: * class Tag
017: *
018: * @author: Jani Lehtimäki
019: */
020: public class Tag {
021:
022: private String _namespace = null;
023: private String _name;
024: private ArrayList _attributes = null;
025: private boolean _hasEndSlash = false;
026: private boolean _isEndTag = false;
027:
028: public Tag(String name) {
029: if (name.length() > 0 && name.charAt(0) == '/') {
030: _isEndTag = true;
031: }
032:
033: int i = name.indexOf(':');
034:
035: if (i > 0) {
036: if (_isEndTag) {
037: _namespace = name.substring(1, i);
038: _name = ('/' + name.substring(i + 1));
039: } else {
040: _namespace = name.substring(0, i);
041: _name = name.substring(i + 1);
042: }
043: } else {
044: _name = name;
045: }
046: }
047:
048: void enableEndSlash() {
049: _hasEndSlash = true;
050: }
051:
052: public boolean isEndTagOf(Tag start) {
053: if (!_isEndTag || start._isEndTag) {
054: return false;
055: }
056: String namespace = start._namespace;
057: if ((_namespace == null) == (namespace == null)) {
058: if (namespace != null) {
059: if (!namespace.equalsIgnoreCase(_namespace)) {
060: return false;
061: }
062: }
063: }
064: return (_name.substring(1).equalsIgnoreCase(start._name));
065:
066: }
067:
068: void add(String name, String value) {
069: if (_attributes == null) {
070: _attributes = new ArrayList(4);
071: }
072: _attributes.add(new Attribute(name, value));
073: }
074:
075: void add(String name) {
076: if (_attributes == null) {
077: _attributes = new ArrayList(4);
078: }
079: _attributes.add(new Attribute(name));
080: }
081:
082: public boolean hasEndSlash() {
083: return _hasEndSlash;
084: }
085:
086: public boolean isEndTag() {
087: return _isEndTag;
088: }
089:
090: public String getName() {
091: return _name;
092: }
093:
094: public String getNamespace() {
095: return _namespace;
096: }
097:
098: public String getPrefixedName() {
099: if (_namespace != null) {
100: return _namespace + ':' + _name;
101: } else {
102: return _name;
103: }
104: }
105:
106: public boolean contains(String name) {
107: Attribute a = getAttribute(name);
108: return (a != null) ? true : false;
109: }
110:
111: public Attribute getAttribute(String name) {
112: int n = getLength();
113: for (int i = 0; i < n; i++) {
114: Attribute a = (Attribute) _attributes.get(i);
115: if (a.getName().equalsIgnoreCase(name)) {
116: return a;
117: }
118: }
119: return null;
120: }
121:
122: public void set(String name, String value) {
123: Attribute a = getAttribute(name);
124: if (a != null) {
125: a.setValue(value);
126: } else {
127: if (_attributes == null) {
128: _attributes = new ArrayList(4);
129: }
130: _attributes.add(new Attribute(name, value));
131: }
132: }
133:
134: public void set(String name) {
135: Attribute a = getAttribute(name);
136: if (a != null) {
137: a.setValue(null);
138: } else {
139: if (_attributes == null) {
140: _attributes = new ArrayList(4);
141: }
142: _attributes.add(new Attribute(name, ""));
143: }
144: }
145:
146: public String get(String name) {
147: Attribute a = getAttribute(name);
148: if (a != null) {
149: return a.getValue();
150: } else {
151: return null;
152: }
153: }
154:
155: public String getValue(String name) {
156: Attribute a = getAttribute(name);
157: if (a != null) {
158: return a.getValue();
159: } else {
160: return null;
161: }
162: }
163:
164: public int getLength() {
165: return ((_attributes != null) ? _attributes.size() : 0);
166: }
167:
168: public Attribute getAttribute(int index) {
169: if (index >= 0 && index <= getLength()) {
170: return (Attribute) _attributes.get(index);
171: } else {
172: return null;
173: }
174: }
175:
176: public String getName(int index) {
177: Attribute a = getAttribute(index);
178: return (a != null) ? a.getName() : null;
179: }
180:
181: public String getValue(int index) {
182: Attribute a = getAttribute(index);
183: return (a != null) ? a.getValue() : null;
184: }
185:
186: public void remove(String name) {
187: Attribute a = getAttribute(name);
188: if (a != null) {
189: _attributes.remove(a);
190: }
191: }
192:
193: public Iterator getAttributes() {
194: if (_attributes == null) {
195: _attributes = new ArrayList(4);
196: }
197: return _attributes.iterator();
198: }
199:
200: public String toString() {
201: StringBuffer image = new StringBuffer();
202: image.append('<');
203: if (_namespace != null) {
204: if (_isEndTag) {
205: image.append('/');
206: image.append(_namespace);
207: image.append(':');
208: image.append(_name.substring(1));
209: } else {
210: image.append(_namespace);
211: image.append(':');
212: image.append(_name);
213: }
214: } else {
215: image.append(_name);
216: }
217:
218: int n = getLength();
219: for (int i = 0; i < n; i++) {
220: Attribute attribute = (Attribute) _attributes.get(i);
221: image.append(' ');
222: attribute.toString(image);
223: }
224: if (_hasEndSlash) {
225: image.append('/');
226: }
227: image.append('>');
228:
229: return image.toString();
230: }
231:
232: }
|