2
* =========================================
3
* LibXML : a free Java layouting library
4
* =========================================
6
* Project Info: http://reporting.pentaho.org/libxml/
8
* (C) Copyright 2006, by Object Refinery Ltd, Pentaho Corporation and Contributors.
10
* This library is free software; you can redistribute it and/or modify it under the terms
11
* of the GNU Lesser General Public License as published by the Free Software Foundation;
12
* either version 2.1 of the License, or (at your option) any later version.
14
* This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
15
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16
* See the GNU Lesser General Public License for more details.
18
* You should have received a copy of the GNU Lesser General Public License along with this
19
* library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20
* Boston, MA 02111-1307, USA.
22
* [Java is a trademark or registered trademark of Sun Microsystems, Inc.
23
* in the United States and other countries.]
27
* $Id: AttributeList.java,v 1.9 2007/06/03 17:07:08 taqua Exp $
29
* (C) Copyright 2006, by Pentaho Corporation.
32
package org.jfree.xmlns.common;
34
import java.util.ArrayList;
35
import java.util.Iterator;
36
import java.util.List;
38
import org.jfree.util.ObjectUtilities;
41
* The attribute list is used by a writer to specify the attributes of an XML
42
* element in a certain order.
44
* @author Thomas Morgner
46
public class AttributeList
48
/** A constant containing the XML-Namespace namespace identifier. */
49
public static final String XMLNS_NAMESPACE = "http://www.w3.org/2000/xmlns/";
50
/** A constant containing the XML namespace identifier. */
51
public static final String XML_NAMESPACE = "http://www.w3.org/XML/1998/namespace";
54
* A name/value pair of the attribute list.
56
public static class AttributeEntry
59
* The namespace of the attribute entry.
61
private String namespace;
64
* The name of the attribute entry.
69
* The value of the attribute entry.
74
* Creates a new attribute entry for the given name and value.
76
* @param namespace the namespace of the attribute.
77
* @param name the attribute name (<code>null</code> not permitted).
78
* @param value the attribute value (<code>null</code> not permitted).
80
public AttributeEntry(final String namespace, final String name,
85
throw new NullPointerException("Name must not be null. ["
86
+ name + ", " + value + "]");
90
throw new NullPointerException("Value must not be null. ["
91
+ name + ", " + value + "]");
93
this.namespace = namespace;
99
* Returns the attribute name.
103
public String getName()
109
* Returns the value of this attribute entry.
111
* @return the value of the entry.
113
public String getValue()
119
* Returns the attribute namespace (which can be null).
121
* @return the namespace.
123
public String getNamespace()
129
* Compares this attribute entry for equality with an other object.
131
* @param o the other object.
132
* @return true, if this object is equal, false otherwise.
134
public boolean equals(Object o)
140
if (o == null || getClass() != o.getClass())
145
final AttributeEntry that = (AttributeEntry) o;
147
if (!name.equals(that.name))
151
if (namespace != null ? !namespace.equals(
152
that.namespace) : that.namespace != null)
161
* Computes a hashcode for this attribute entry.
163
* @return the attribute entry's hashcode.
165
public int hashCode()
168
result = (namespace != null ? namespace.hashCode() : 0);
169
result = 29 * result + name.hashCode();
175
* The storage for all entries of this list.
177
private List entryList;
180
* Creates an empty attribute list with no default values.
182
public AttributeList()
184
this.entryList = new ArrayList();
188
* Returns an iterator over the entry list. The iterator returns
189
* AttributeList.AttributeEntry objects.
191
* @return the iterator over the entries contained in this list.
193
public Iterator iterator()
195
return entryList.iterator();
199
* Defines an attribute.
201
* @param namespace the namespace of the attribute.
202
* @param name the name of the attribute to be defined
203
* @param value the value of the attribute.
205
public void setAttribute(final String namespace,
209
final AttributeEntry entry = new AttributeEntry(namespace, name, value);
210
final int pos = this.entryList.indexOf(entry);
213
this.entryList.remove(pos);
215
this.entryList.add(entry);
219
* Returns the attribute value for the given attribute name or null, if the
220
* attribute is not defined in this list.
222
* @param namespace the namespace of the attribute.
223
* @param name the name of the attribute
224
* @return the attribute value or null.
226
public String getAttribute(final String namespace,
229
return getAttribute(namespace, name, null);
233
* Returns the attribute value for the given attribute name or the given
234
* defaultvalue, if the attribute is not defined in this list.
236
* @param namespace the namespace of the attribute.
237
* @param name the name of the attribute.
238
* @param defaultValue the default value.
239
* @return the attribute value or the defaultValue.
241
public String getAttribute(final String namespace,
243
final String defaultValue)
245
for (int i = 0; i < this.entryList.size(); i++)
247
final AttributeEntry ae = (AttributeEntry) this.entryList.get(i);
248
if (ae.getName().equals(name) && namespace.equals(name))
250
return ae.getValue();
257
* Removes the attribute with the given name from the list.
259
* @param namespace the namespace of the attribute that should be removed.
260
* @param name the name of the attribute which should be removed..
262
public void removeAttribute(final String namespace,
265
for (int i = 0; i < this.entryList.size(); i++)
267
final AttributeEntry ae = (AttributeEntry) this.entryList.get(i);
268
if (ae.getName().equals(name) && ae.getNamespace().equals(namespace))
270
this.entryList.remove(ae);
277
* Checks, whether this list is empty.
279
* @return true, if the list is empty, false otherwise.
281
public boolean isEmpty()
283
return this.entryList.isEmpty();
287
* Adds a namespace declaration. In XML, Namespaces are declared by
288
* using a special attribute-syntax. As this syntax is confusing and
289
* complicated, this method encapsulates this and make defining
290
* namespaces less confusing.
292
* @param prefix the desired namespace prefix (can be null or empty to
293
* define the default namespace.
294
* @param namespaceUri the URI of the namespace.
296
public void addNamespaceDeclaration(final String prefix,
297
final String namespaceUri)
299
if (namespaceUri == null)
301
throw new NullPointerException();
304
if (prefix == null || "".equals(prefix))
306
setAttribute(AttributeList.XMLNS_NAMESPACE, "", namespaceUri);
310
setAttribute(AttributeList.XMLNS_NAMESPACE, prefix, namespaceUri);
315
* Removes a namespace declaration from this attribute list.
317
* @param prefix the declared namespace prefix.
319
public void removeNamespaceDeclaration (final String prefix)
321
if (prefix == null || "".equals(prefix))
323
removeAttribute(AttributeList.XMLNS_NAMESPACE, "");
327
removeAttribute(AttributeList.XMLNS_NAMESPACE, prefix);
332
* Checks, whether the given prefix is defined.
334
* @param prefix the namespace prefix.
335
* @return true, if the prefix is defined, false otherwise.
337
public boolean isNamespacePrefixDefined(final String prefix)
339
return getAttribute(AttributeList.XMLNS_NAMESPACE, prefix) != null;
343
* Checks, whether the given namespace URI has a defined prefix.
346
* @param uri the uri.
347
* @return true, if there is at least one namespace declaration matching
348
* the given URI, false otherwise.
350
public boolean isNamespaceUriDefined(final String uri)
352
for (int i = 0; i < this.entryList.size(); i++)
354
final AttributeEntry ae = (AttributeEntry) this.entryList.get(i);
355
if (ObjectUtilities.equal(ae.getValue(), uri) &&
356
AttributeList.XMLNS_NAMESPACE.equals(ae.getNamespace()))