1
/* ========================================================================
2
* JCommon : a free general purpose class library for the Java(tm) platform
3
* ========================================================================
5
* (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
7
* Project Info: http://www.jfree.org/jcommon/index.html
9
* This library is free software; you can redistribute it and/or modify it
10
* under the terms of the GNU Lesser General Public License as published by
11
* the Free Software Foundation; either version 2.1 of the License, or
12
* (at your option) any later version.
14
* This library is distributed in the hope that it will be useful, but
15
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
17
* License for more details.
19
* You should have received a copy of the GNU Lesser General Public
20
* License along with this library; if not, write to the Free Software
21
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
24
* [Java is a trademark or registered trademark of Sun Microsystems, Inc.
25
* in the United States and other countries.]
30
* (C)opyright 2003-2005, by Thomas Morgner and Contributors.
32
* Original Author: Thomas Morgner;
33
* Contributor(s): David Gilbert (for Object Refinery Limited);
35
* $Id: AttributeList.java,v 1.3 2005/10/18 13:35:06 mungady Exp $
39
* 25-Sep-2003 : Initial version (TM);
40
* 26-Nov-2003 : Javadoc updates (DG);
44
package org.jfree.xml.writer;
46
import java.util.Iterator;
47
import java.util.List;
50
* The attribute list is used by a writer to specify the attributes
51
* of an XML element in a certain order.
53
* @author Thomas Morgner
55
public class AttributeList {
58
* A name/value pair of the attribute list.
60
private static class AttributeEntry {
62
/** The name of the attribute entry. */
65
/** The value of the attribute entry. */
69
* Creates a new attribute entry for the given name and value.
71
* @param name the attribute name (<code>null</code> not permitted).
72
* @param value the attribute value (<code>null</code> not permitted).
74
public AttributeEntry(final String name, final String value) {
76
throw new NullPointerException("Name must not be null. ["
77
+ name + ", " + value + "]");
80
throw new NullPointerException("Value must not be null. ["
81
+ name + ", " + value + "]");
88
* Returns the attribute name.
92
public String getName() {
97
* Returns the value of this attribute entry.
99
* @return the value of the entry.
101
public String getValue() {
106
* Checks whether the given object is an attribute entry with the same name.
108
* @param o the suspected other attribute entry.
110
* @return <code>true</code> if the given object is equal, <code>false</code> otherwise.
112
public boolean equals(final Object o) {
116
if (!(o instanceof AttributeEntry)) {
120
final AttributeEntry attributeEntry = (AttributeEntry) o;
121
if (!this.name.equals(attributeEntry.name)) {
128
* Computes an hashcode for this entry.
130
* @return the hashcode.
132
public int hashCode() {
133
return this.name.hashCode();
138
* An iterator over the attribute names of this list.
140
private static class AttributeIterator implements Iterator {
142
/** The backend is an iterator over the attribute entries. */
143
private Iterator backend;
146
* Creates a new attribute iterator using the given iterator as backend.
148
* @param backend an iterator over the attribute entries (<code>null</code> not permitted).
150
public AttributeIterator(final Iterator backend) {
151
if (backend == null) {
152
throw new NullPointerException();
154
this.backend = backend;
158
* Returns <tt>true</tt> if the iteration has more elements. (In other
159
* words, returns <tt>true</tt> if <tt>next</tt> would return an element
160
* rather than throwing an exception.)
162
* @return <tt>true</tt> if the iterator has more elements.
164
public boolean hasNext() {
165
return this.backend.hasNext();
169
* Returns the next element in the iteration.
171
* @return the next element in the iteration.
173
public Object next() {
174
final AttributeEntry entry = (AttributeEntry) this.backend.next();
176
return entry.getName();
183
* Removes from the underlying collection the last element returned by the
184
* iterator (optional operation). This method can be called only once per
185
* call to <tt>next</tt>. The behavior of an iterator is unspecified if
186
* the underlying collection is modified while the iteration is in
187
* progress in any way other than by calling this method.
189
public void remove() {
190
this.backend.remove();
194
/** The storage for all entries of this list. */
195
private List entryList;
198
* Creates an empty attribute list with no default values.
200
public AttributeList() {
201
this.entryList = new java.util.ArrayList();
205
* Returns an iterator over all attribute names. The names are returned
206
* in their oder of addition to the list. The iterator contains strings.
208
* @return the iterator over all attribute names.
210
public Iterator keys() {
211
return new AttributeIterator(this.entryList.iterator());
215
* Defines an attribute.
217
* @param name the name of the attribute to be defined
218
* @param value the value of the attribute.
220
public synchronized void setAttribute(final String name, final String value) {
221
final AttributeEntry entry = new AttributeEntry(name, value);
222
final int pos = this.entryList.indexOf(entry);
224
this.entryList.remove(pos);
226
this.entryList.add(entry);
230
* Returns the attribute value for the given attribute name or null,
231
* if the attribute is not defined in this list.
233
* @param name the name of the attribute
234
* @return the attribute value or null.
236
public synchronized String getAttribute(final String name) {
237
return getAttribute(name, null);
241
* Returns the attribute value for the given attribute name or the given
242
* defaultvalue, if the attribute is not defined in this list.
244
* @param name the name of the attribute.
245
* @param defaultValue the default value.
247
* @return the attribute value or the defaultValue.
249
public synchronized String getAttribute(final String name, final String defaultValue) {
250
for (int i = 0; i < this.entryList.size(); i++) {
251
final AttributeEntry ae = (AttributeEntry) this.entryList.get(i);
252
if (ae.getName().equals(name)) {
253
return ae.getValue();
260
* Removes the attribute with the given name from the list.
262
* @param name the name of the attribute which should be removed..
264
public synchronized void removeAttribute(final String name) {
265
for (int i = 0; i < this.entryList.size(); i++) {
266
final AttributeEntry ae = (AttributeEntry) this.entryList.get(i);
267
if (ae.getName().equals(name)) {
268
this.entryList.remove(ae);