2
* This program is free software; you can redistribute it and/or modify it under the
3
* terms of the GNU Lesser General Public License, version 2.1 as published by the Free Software
6
* You should have received a copy of the GNU Lesser General Public License along with this
7
* program; if not, you can obtain a copy at http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html
8
* or from the Free Software Foundation, Inc.,
9
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
11
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
12
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
* See the GNU Lesser General Public License for more details.
15
* Copyright (c) 2006 - 2009 Object Refinery Ltd, Pentaho Corporation and Contributors. All rights reserved.
18
package org.pentaho.reporting.libraries.xmlns.writer;
20
import java.util.Collections;
21
import java.util.HashMap;
22
import java.util.Iterator;
25
import org.pentaho.reporting.libraries.xmlns.common.AttributeList;
28
* A immutable namespace collection. Any attempt to modify the declared namespaces creates a new copy of the map.
30
* @author Thomas Morgner
32
public final class DeclaredNamespaces
34
private HashMap namespaces;
37
* Creates a new namespaces collection.
39
public DeclaredNamespaces()
44
* Creates a new namespaces collection using the given namespaces as initial values.
46
* @param namespaces the namespaces, never null.
48
public DeclaredNamespaces(final DeclaredNamespaces namespaces)
50
if (namespaces == null)
52
throw new NullPointerException();
55
if (namespaces.namespaces != null)
57
this.namespaces = (HashMap) namespaces.namespaces.clone();
62
* Adds all namespaces from the given hashmap into this map. The namespaces map must only contain string keys and
63
* string values and must not contain either null-keys or null-values.
65
* @param newNamespaces the namespaces collection.
66
* @return the created namespaces object.
68
public DeclaredNamespaces add(final HashMap newNamespaces)
70
if (newNamespaces == null)
72
throw new NullPointerException();
75
final DeclaredNamespaces retval = new DeclaredNamespaces();
76
if (this.namespaces == null)
78
retval.namespaces = new HashMap();
82
retval.namespaces = (HashMap) this.namespaces.clone();
85
final Iterator iterator = newNamespaces.entrySet().iterator();
86
while (iterator.hasNext())
88
final Map.Entry entry = (Map.Entry) iterator.next();
89
final String value = (String) entry.getValue();
90
final String o = (String) entry.getKey();
91
if (value == null || o == null)
93
throw new NullPointerException();
95
retval.namespaces.put(o, value);
102
* Adds all declared namespaces from the given attribute-list into the namespaces collection.
104
* @param attributes the attribute list containing namespace definitions.
105
* @return the new namespaces collection.
107
public DeclaredNamespaces add(final AttributeList attributes)
109
if (attributes == null)
111
throw new NullPointerException();
114
DeclaredNamespaces retval = null;
115
final AttributeList.AttributeEntry[] entries = attributes.toArray();
116
for (int i = 0; i < entries.length; i++)
118
final AttributeList.AttributeEntry entry = entries[i];
119
final String prefix = entry.getName();
120
if ("xmlns".equals(prefix))
122
if (entry.getNamespace() == null || "".equals(entry.getNamespace()))
126
retval = new DeclaredNamespaces();
127
if (namespaces != null)
129
retval.namespaces = (HashMap) namespaces.clone();
133
retval.namespaces = new HashMap();
136
retval.namespaces.put(entry.getValue(), "");
139
else if (AttributeList.XMLNS_NAMESPACE.equals(entry.getNamespace()))
143
retval = new DeclaredNamespaces();
144
if (namespaces != null)
146
retval.namespaces = (HashMap) namespaces.clone();
150
retval.namespaces = new HashMap();
153
retval.namespaces.put(entry.getValue(), prefix);
165
* Adds a single namespace definition to the collection.
167
* @param uri the URI of the namespace.
168
* @param prefix the prefix to be used for the namespace.
169
* @return the new namespaces collection.
171
public DeclaredNamespaces add(final String uri, final String prefix)
175
throw new NullPointerException();
179
throw new NullPointerException();
181
final DeclaredNamespaces retval = new DeclaredNamespaces();
182
if (namespaces == null)
184
retval.namespaces = new HashMap();
188
retval.namespaces = (HashMap) namespaces.clone();
190
retval.namespaces.put(uri, prefix);
195
* Looksup the prefix for the given URI. This returns null if the URI is not a declared namespace.
197
* @param uri the URI.
198
* @return the prefix for the given URI or null, if the URI is not part of this collection.
200
public String getPrefix(final String uri)
204
throw new NullPointerException();
207
if (namespaces == null)
211
return (String) namespaces.get(uri);
215
* Checks, whether the namespace marked by the given URI is defined in this collection.
217
* @param uri the URI to be checked.
218
* @return true, if the URI is known, false otherwise.
220
public boolean isNamespaceDefined(final String uri)
224
throw new NullPointerException();
227
if (namespaces == null)
231
return namespaces.containsKey(uri);
235
* Returns all known namespaces as unmodifiable map.
237
* @return the namespaces.
239
public Map getNamespaces()
241
return Collections.unmodifiableMap(namespaces);
245
* Checks whether the given prefix is already defined in the collection.
247
* @param prefix the prefix.
248
* @return true, if the prefix is already used, false otherwise.
250
public boolean isPrefixDefined(final String prefix)
252
return namespaces.containsValue(prefix);