001: /* ========================================================================
002: * JCommon : a free general purpose class library for the Java(tm) platform
003: * ========================================================================
004: *
005: * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
006: *
007: * Project Info: http://www.jfree.org/jcommon/index.html
008: *
009: * This library is free software; you can redistribute it and/or modify it
010: * under the terms of the GNU Lesser General Public License as published by
011: * the Free Software Foundation; either version 2.1 of the License, or
012: * (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful, but
015: * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
016: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
017: * License for more details.
018: *
019: * You should have received a copy of the GNU Lesser General Public
020: * License along with this library; if not, write to the Free Software
021: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
022: * USA.
023: *
024: * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
025: * in the United States and other countries.]
026: *
027: * ------------------
028: * AttributeList.java
029: * ------------------
030: * (C)opyright 2003-2005, by Thomas Morgner and Contributors.
031: *
032: * Original Author: Thomas Morgner;
033: * Contributor(s): David Gilbert (for Object Refinery Limited);
034: *
035: * $Id: AttributeList.java,v 1.3 2005/10/18 13:35:06 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 25-Sep-2003 : Initial version (TM);
040: * 26-Nov-2003 : Javadoc updates (DG);
041: *
042: */
043:
044: package org.jfree.xml.writer;
045:
046: import java.util.Iterator;
047: import java.util.List;
048:
049: /**
050: * The attribute list is used by a writer to specify the attributes
051: * of an XML element in a certain order.
052: *
053: * @author Thomas Morgner
054: */
055: public class AttributeList {
056:
057: /**
058: * A name/value pair of the attribute list.
059: */
060: private static class AttributeEntry {
061:
062: /** The name of the attribute entry. */
063: private String name;
064:
065: /** The value of the attribute entry. */
066: private String value;
067:
068: /**
069: * Creates a new attribute entry for the given name and value.
070: *
071: * @param name the attribute name (<code>null</code> not permitted).
072: * @param value the attribute value (<code>null</code> not permitted).
073: */
074: public AttributeEntry(final String name, final String value) {
075: if (name == null) {
076: throw new NullPointerException(
077: "Name must not be null. [" + name + ", "
078: + value + "]");
079: }
080: if (value == null) {
081: throw new NullPointerException(
082: "Value must not be null. [" + name + ", "
083: + value + "]");
084: }
085: this .name = name;
086: this .value = value;
087: }
088:
089: /**
090: * Returns the attribute name.
091: *
092: * @return the name.
093: */
094: public String getName() {
095: return this .name;
096: }
097:
098: /**
099: * Returns the value of this attribute entry.
100: *
101: * @return the value of the entry.
102: */
103: public String getValue() {
104: return this .value;
105: }
106:
107: /**
108: * Checks whether the given object is an attribute entry with the same name.
109: *
110: * @param o the suspected other attribute entry.
111: *
112: * @return <code>true</code> if the given object is equal, <code>false</code> otherwise.
113: */
114: public boolean equals(final Object o) {
115: if (this == o) {
116: return true;
117: }
118: if (!(o instanceof AttributeEntry)) {
119: return false;
120: }
121:
122: final AttributeEntry attributeEntry = (AttributeEntry) o;
123: if (!this .name.equals(attributeEntry.name)) {
124: return false;
125: }
126: return true;
127: }
128:
129: /**
130: * Computes an hashcode for this entry.
131: *
132: * @return the hashcode.
133: */
134: public int hashCode() {
135: return this .name.hashCode();
136: }
137: }
138:
139: /**
140: * An iterator over the attribute names of this list.
141: */
142: private static class AttributeIterator implements Iterator {
143:
144: /** The backend is an iterator over the attribute entries. */
145: private Iterator backend;
146:
147: /**
148: * Creates a new attribute iterator using the given iterator as backend.
149: *
150: * @param backend an iterator over the attribute entries (<code>null</code> not permitted).
151: */
152: public AttributeIterator(final Iterator backend) {
153: if (backend == null) {
154: throw new NullPointerException();
155: }
156: this .backend = backend;
157: }
158:
159: /**
160: * Returns <tt>true</tt> if the iteration has more elements. (In other
161: * words, returns <tt>true</tt> if <tt>next</tt> would return an element
162: * rather than throwing an exception.)
163: *
164: * @return <tt>true</tt> if the iterator has more elements.
165: */
166: public boolean hasNext() {
167: return this .backend.hasNext();
168: }
169:
170: /**
171: * Returns the next element in the iteration.
172: *
173: * @return the next element in the iteration.
174: */
175: public Object next() {
176: final AttributeEntry entry = (AttributeEntry) this .backend
177: .next();
178: if (entry != null) {
179: return entry.getName();
180: }
181: return entry;
182: }
183:
184: /**
185: *
186: * Removes from the underlying collection the last element returned by the
187: * iterator (optional operation). This method can be called only once per
188: * call to <tt>next</tt>. The behavior of an iterator is unspecified if
189: * the underlying collection is modified while the iteration is in
190: * progress in any way other than by calling this method.
191: */
192: public void remove() {
193: this .backend.remove();
194: }
195: }
196:
197: /** The storage for all entries of this list. */
198: private List entryList;
199:
200: /**
201: * Creates an empty attribute list with no default values.
202: */
203: public AttributeList() {
204: this .entryList = new java.util.ArrayList();
205: }
206:
207: /**
208: * Returns an iterator over all attribute names. The names are returned
209: * in their oder of addition to the list. The iterator contains strings.
210: *
211: * @return the iterator over all attribute names.
212: */
213: public Iterator keys() {
214: return new AttributeIterator(this .entryList.iterator());
215: }
216:
217: /**
218: * Defines an attribute.
219: *
220: * @param name the name of the attribute to be defined
221: * @param value the value of the attribute.
222: */
223: public synchronized void setAttribute(final String name,
224: final String value) {
225: final AttributeEntry entry = new AttributeEntry(name, value);
226: final int pos = this .entryList.indexOf(entry);
227: if (pos != -1) {
228: this .entryList.remove(pos);
229: }
230: this .entryList.add(entry);
231: }
232:
233: /**
234: * Returns the attribute value for the given attribute name or null,
235: * if the attribute is not defined in this list.
236: *
237: * @param name the name of the attribute
238: * @return the attribute value or null.
239: */
240: public synchronized String getAttribute(final String name) {
241: return getAttribute(name, null);
242: }
243:
244: /**
245: * Returns the attribute value for the given attribute name or the given
246: * defaultvalue, if the attribute is not defined in this list.
247: *
248: * @param name the name of the attribute.
249: * @param defaultValue the default value.
250: *
251: * @return the attribute value or the defaultValue.
252: */
253: public synchronized String getAttribute(final String name,
254: final String defaultValue) {
255: for (int i = 0; i < this .entryList.size(); i++) {
256: final AttributeEntry ae = (AttributeEntry) this .entryList
257: .get(i);
258: if (ae.getName().equals(name)) {
259: return ae.getValue();
260: }
261: }
262: return defaultValue;
263: }
264:
265: /**
266: * Removes the attribute with the given name from the list.
267: *
268: * @param name the name of the attribute which should be removed..
269: */
270: public synchronized void removeAttribute(final String name) {
271: for (int i = 0; i < this .entryList.size(); i++) {
272: final AttributeEntry ae = (AttributeEntry) this.entryList
273: .get(i);
274: if (ae.getName().equals(name)) {
275: this.entryList.remove(ae);
276: return;
277: }
278: }
279: }
280: }
|