~ubuntu-branches/ubuntu/oneiric/libxml-java/oneiric

« back to all changes in this revision

Viewing changes to source/org/jfree/xmlns/common/AttributeList.java

  • Committer: Bazaar Package Importer
  • Author(s): Rene Engelhard
  • Date: 2007-08-21 00:19:22 UTC
  • Revision ID: james.westby@ubuntu.com-20070821001922-9khxrso03tk3c5i2
Tags: upstream-0.9.5
ImportĀ upstreamĀ versionĀ 0.9.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 * =========================================
 
3
 * LibXML : a free Java layouting library
 
4
 * =========================================
 
5
 *
 
6
 * Project Info:  http://reporting.pentaho.org/libxml/
 
7
 *
 
8
 * (C) Copyright 2006, by Object Refinery Ltd, Pentaho Corporation and Contributors.
 
9
 *
 
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.
 
13
 *
 
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.
 
17
 *
 
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.
 
21
 *
 
22
 * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
 
23
 * in the United States and other countries.]
 
24
 *
 
25
 *
 
26
 * ------------
 
27
 * $Id: AttributeList.java,v 1.9 2007/06/03 17:07:08 taqua Exp $
 
28
 * ------------
 
29
 * (C) Copyright 2006, by Pentaho Corporation.
 
30
 */
 
31
 
 
32
package org.jfree.xmlns.common;
 
33
 
 
34
import java.util.ArrayList;
 
35
import java.util.Iterator;
 
36
import java.util.List;
 
37
 
 
38
import org.jfree.util.ObjectUtilities;
 
39
 
 
40
/**
 
41
 * The attribute list is used by a writer to specify the attributes of an XML
 
42
 * element in a certain order.
 
43
 *
 
44
 * @author Thomas Morgner
 
45
 */
 
46
public class AttributeList
 
47
{
 
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";
 
52
 
 
53
  /**
 
54
   * A name/value pair of the attribute list.
 
55
   */
 
56
  public static class AttributeEntry
 
57
  {
 
58
    /**
 
59
     * The namespace of the attribute entry.
 
60
     */
 
61
    private String namespace;
 
62
 
 
63
    /**
 
64
     * The name of the attribute entry.
 
65
     */
 
66
    private String name;
 
67
 
 
68
    /**
 
69
     * The value of the attribute entry.
 
70
     */
 
71
    private String value;
 
72
 
 
73
    /**
 
74
     * Creates a new attribute entry for the given name and value.
 
75
     *
 
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).
 
79
     */
 
80
    public AttributeEntry(final String namespace, final String name,
 
81
                          final String value)
 
82
    {
 
83
      if (name == null)
 
84
      {
 
85
        throw new NullPointerException("Name must not be null. ["
 
86
                                       + name + ", " + value + "]");
 
87
      }
 
88
      if (value == null)
 
89
      {
 
90
        throw new NullPointerException("Value must not be null. ["
 
91
                                       + name + ", " + value + "]");
 
92
      }
 
93
      this.namespace = namespace;
 
94
      this.name = name;
 
95
      this.value = value;
 
96
    }
 
97
 
 
98
    /**
 
99
     * Returns the attribute name.
 
100
     *
 
101
     * @return the name.
 
102
     */
 
103
    public String getName()
 
104
    {
 
105
      return this.name;
 
106
    }
 
107
 
 
108
    /**
 
109
     * Returns the value of this attribute entry.
 
110
     *
 
111
     * @return the value of the entry.
 
112
     */
 
113
    public String getValue()
 
114
    {
 
115
      return this.value;
 
116
    }
 
117
 
 
118
    /**
 
119
     * Returns the attribute namespace (which can be null).
 
120
     *
 
121
     * @return the namespace.
 
122
     */
 
123
    public String getNamespace()
 
124
    {
 
125
      return namespace;
 
126
    }
 
127
 
 
128
    /**
 
129
     * Compares this attribute entry for equality with an other object.
 
130
     *
 
131
     * @param o the other object.
 
132
     * @return true, if this object is equal, false otherwise.
 
133
     */
 
134
    public boolean equals(Object o)
 
135
    {
 
136
      if (this == o)
 
137
      {
 
138
        return true;
 
139
      }
 
140
      if (o == null || getClass() != o.getClass())
 
141
      {
 
142
        return false;
 
143
      }
 
144
 
 
145
      final AttributeEntry that = (AttributeEntry) o;
 
146
 
 
147
      if (!name.equals(that.name))
 
148
      {
 
149
        return false;
 
150
      }
 
151
      if (namespace != null ? !namespace.equals(
 
152
          that.namespace) : that.namespace != null)
 
153
      {
 
154
        return false;
 
155
      }
 
156
 
 
157
      return true;
 
158
    }
 
159
 
 
160
    /**
 
161
     * Computes a hashcode for this attribute entry.
 
162
     *
 
163
     * @return the attribute entry's hashcode.
 
164
     */
 
165
    public int hashCode()
 
166
    {
 
167
      int result;
 
168
      result = (namespace != null ? namespace.hashCode() : 0);
 
169
      result = 29 * result + name.hashCode();
 
170
      return result;
 
171
    }
 
172
  }
 
173
 
 
174
  /**
 
175
   * The storage for all entries of this list.
 
176
   */
 
177
  private List entryList;
 
178
 
 
179
  /**
 
180
   * Creates an empty attribute list with no default values.
 
181
   */
 
182
  public AttributeList()
 
183
  {
 
184
    this.entryList = new ArrayList();
 
185
  }
 
186
 
 
187
  /**
 
188
   * Returns an iterator over the entry list. The iterator returns
 
189
   * AttributeList.AttributeEntry objects.
 
190
   *
 
191
   * @return the iterator over the entries contained in this list.
 
192
   */
 
193
  public Iterator iterator()
 
194
  {
 
195
    return entryList.iterator();
 
196
  }
 
197
 
 
198
  /**
 
199
   * Defines an attribute.
 
200
   *
 
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.
 
204
   */
 
205
  public void setAttribute(final String namespace,
 
206
                           final String name,
 
207
                           final String value)
 
208
  {
 
209
    final AttributeEntry entry = new AttributeEntry(namespace, name, value);
 
210
    final int pos = this.entryList.indexOf(entry);
 
211
    if (pos != -1)
 
212
    {
 
213
      this.entryList.remove(pos);
 
214
    }
 
215
    this.entryList.add(entry);
 
216
  }
 
217
 
 
218
  /**
 
219
   * Returns the attribute value for the given attribute name or null, if the
 
220
   * attribute is not defined in this list.
 
221
   *
 
222
   * @param namespace the namespace of the attribute.
 
223
   * @param name      the name of the attribute
 
224
   * @return the attribute value or null.
 
225
   */
 
226
  public String getAttribute(final String namespace,
 
227
                             final String name)
 
228
  {
 
229
    return getAttribute(namespace, name, null);
 
230
  }
 
231
 
 
232
  /**
 
233
   * Returns the attribute value for the given attribute name or the given
 
234
   * defaultvalue, if the attribute is not defined in this list.
 
235
   *
 
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.
 
240
   */
 
241
  public String getAttribute(final String namespace,
 
242
                             final String name,
 
243
                             final String defaultValue)
 
244
  {
 
245
    for (int i = 0; i < this.entryList.size(); i++)
 
246
    {
 
247
      final AttributeEntry ae = (AttributeEntry) this.entryList.get(i);
 
248
      if (ae.getName().equals(name) && namespace.equals(name))
 
249
      {
 
250
        return ae.getValue();
 
251
      }
 
252
    }
 
253
    return defaultValue;
 
254
  }
 
255
 
 
256
  /**
 
257
   * Removes the attribute with the given name from the list.
 
258
   *
 
259
   * @param namespace the namespace of the attribute that should be removed.
 
260
   * @param name      the name of the attribute which should be removed..
 
261
   */
 
262
  public void removeAttribute(final String namespace,
 
263
                              final String name)
 
264
  {
 
265
    for (int i = 0; i < this.entryList.size(); i++)
 
266
    {
 
267
      final AttributeEntry ae = (AttributeEntry) this.entryList.get(i);
 
268
      if (ae.getName().equals(name) && ae.getNamespace().equals(namespace))
 
269
      {
 
270
        this.entryList.remove(ae);
 
271
        return;
 
272
      }
 
273
    }
 
274
  }
 
275
 
 
276
  /**
 
277
   * Checks, whether this list is empty.
 
278
   *
 
279
   * @return true, if the list is empty, false otherwise.
 
280
   */
 
281
  public boolean isEmpty()
 
282
  {
 
283
    return this.entryList.isEmpty();
 
284
  }
 
285
 
 
286
  /**
 
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.
 
291
   *
 
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.
 
295
   */
 
296
  public void addNamespaceDeclaration(final String prefix,
 
297
                                      final String namespaceUri)
 
298
  {
 
299
    if (namespaceUri == null)
 
300
    {
 
301
      throw new NullPointerException();
 
302
    }
 
303
 
 
304
    if (prefix == null || "".equals(prefix))
 
305
    {
 
306
      setAttribute(AttributeList.XMLNS_NAMESPACE, "", namespaceUri);
 
307
    }
 
308
    else
 
309
    {
 
310
      setAttribute(AttributeList.XMLNS_NAMESPACE, prefix, namespaceUri);
 
311
    }
 
312
  }
 
313
 
 
314
  /**
 
315
   * Removes a namespace declaration from this attribute list.
 
316
   *
 
317
   * @param prefix the declared namespace prefix.
 
318
   */
 
319
  public void removeNamespaceDeclaration (final String prefix)
 
320
  {
 
321
    if (prefix == null || "".equals(prefix))
 
322
    {
 
323
      removeAttribute(AttributeList.XMLNS_NAMESPACE, "");
 
324
    }
 
325
    else
 
326
    {
 
327
      removeAttribute(AttributeList.XMLNS_NAMESPACE, prefix);
 
328
    }
 
329
  }
 
330
 
 
331
  /**
 
332
   * Checks, whether the given prefix is defined.
 
333
   *
 
334
   * @param prefix the namespace prefix.
 
335
   * @return true, if the prefix is defined, false otherwise.
 
336
   */
 
337
  public boolean isNamespacePrefixDefined(final String prefix)
 
338
  {
 
339
    return getAttribute(AttributeList.XMLNS_NAMESPACE, prefix) != null;
 
340
  }
 
341
 
 
342
  /**
 
343
   * Checks, whether the given namespace URI has a defined prefix.
 
344
   *
 
345
   *
 
346
   * @param uri the uri.
 
347
   * @return true, if there is at least one namespace declaration matching
 
348
   * the given URI, false otherwise.
 
349
   */
 
350
  public boolean isNamespaceUriDefined(final String uri)
 
351
  {
 
352
    for (int i = 0; i < this.entryList.size(); i++)
 
353
    {
 
354
      final AttributeEntry ae = (AttributeEntry) this.entryList.get(i);
 
355
      if (ObjectUtilities.equal(ae.getValue(), uri) &&
 
356
          AttributeList.XMLNS_NAMESPACE.equals(ae.getNamespace()))
 
357
      {
 
358
        return true;
 
359
      }
 
360
    }
 
361
    return false;
 
362
  }
 
363
}