~ubuntu-branches/ubuntu/quantal/libxml-java/quantal

« back to all changes in this revision

Viewing changes to source/org/pentaho/reporting/libraries/xmlns/writer/DeclaredNamespaces.java

  • Committer: Package Import Robot
  • Author(s): tony mancill, Miguel Landaeta, tony mancill
  • Date: 2011-12-23 09:07:02 UTC
  • mfrom: (3.1.5 sid)
  • Revision ID: package-import@ubuntu.com-20111223090702-wvpt3s2m696owb5d
Tags: 1.1.6.dfsg-3
[Miguel Landaeta]
* Team upload.
* Add Build-Depends on ant-optional. (Closes: #652753).
* Bump Standards-Version to 3.9.2. No changes were required.
* Switch to source format 3.0 (quilt).
* Remove deprecated simple-patchsys management patch system.

[tony mancill]
* Add Vcs-Git and Vcs-Browser to d/control. (Closes: #653038)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
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
 
4
 * Foundation.
 
5
 *
 
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.
 
10
 *
 
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.
 
14
 *
 
15
 * Copyright (c) 2006 - 2009 Object Refinery Ltd, Pentaho Corporation and Contributors.  All rights reserved.
 
16
 */
 
17
 
 
18
package org.pentaho.reporting.libraries.xmlns.writer;
 
19
 
 
20
import java.util.Collections;
 
21
import java.util.HashMap;
 
22
import java.util.Iterator;
 
23
import java.util.Map;
 
24
 
 
25
import org.pentaho.reporting.libraries.xmlns.common.AttributeList;
 
26
 
 
27
/**
 
28
 * A immutable namespace collection. Any attempt to modify the declared namespaces creates a new copy of the map.
 
29
 *
 
30
 * @author Thomas Morgner
 
31
 */
 
32
public final class DeclaredNamespaces
 
33
{
 
34
  private HashMap namespaces;
 
35
 
 
36
  /**
 
37
   * Creates a new namespaces collection.
 
38
   */
 
39
  public DeclaredNamespaces()
 
40
  {
 
41
  }
 
42
 
 
43
  /**
 
44
   * Creates a new namespaces collection using the given namespaces as initial values.
 
45
   *
 
46
   * @param namespaces the namespaces, never null.
 
47
   */
 
48
  public DeclaredNamespaces(final DeclaredNamespaces namespaces)
 
49
  {
 
50
    if (namespaces == null)
 
51
    {
 
52
      throw new NullPointerException();
 
53
    }
 
54
 
 
55
    if (namespaces.namespaces != null)
 
56
    {
 
57
      this.namespaces = (HashMap) namespaces.namespaces.clone();
 
58
    }
 
59
  }
 
60
 
 
61
  /**
 
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.
 
64
   *
 
65
   * @param newNamespaces the namespaces collection.
 
66
   * @return the created namespaces object.
 
67
   */
 
68
  public DeclaredNamespaces add(final HashMap newNamespaces)
 
69
  {
 
70
    if (newNamespaces == null)
 
71
    {
 
72
      throw new NullPointerException();
 
73
    }
 
74
 
 
75
    final DeclaredNamespaces retval = new DeclaredNamespaces();
 
76
    if (this.namespaces == null)
 
77
    {
 
78
      retval.namespaces = new HashMap();
 
79
    }
 
80
    else
 
81
    {
 
82
      retval.namespaces = (HashMap) this.namespaces.clone();
 
83
    }
 
84
 
 
85
    final Iterator iterator = newNamespaces.entrySet().iterator();
 
86
    while (iterator.hasNext())
 
87
    {
 
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)
 
92
      {
 
93
        throw new NullPointerException();
 
94
      }
 
95
      retval.namespaces.put(o, value);
 
96
 
 
97
    }
 
98
    return retval;
 
99
  }
 
100
 
 
101
  /**
 
102
   * Adds all declared namespaces from the given attribute-list into the namespaces collection.
 
103
   *
 
104
   * @param attributes the attribute list containing namespace definitions.
 
105
   * @return the new namespaces collection.
 
106
   */
 
107
  public DeclaredNamespaces add(final AttributeList attributes)
 
108
  {
 
109
    if (attributes == null)
 
110
    {
 
111
      throw new NullPointerException();
 
112
    }
 
113
 
 
114
    DeclaredNamespaces retval = null;
 
115
    final AttributeList.AttributeEntry[] entries = attributes.toArray();
 
116
    for (int i = 0; i < entries.length; i++)
 
117
    {
 
118
      final AttributeList.AttributeEntry entry = entries[i];
 
119
      final String prefix = entry.getName();
 
120
      if ("xmlns".equals(prefix))
 
121
      {
 
122
        if (entry.getNamespace() == null || "".equals(entry.getNamespace()))
 
123
        {
 
124
          if (retval == null)
 
125
          {
 
126
            retval = new DeclaredNamespaces();
 
127
            if (namespaces != null)
 
128
            {
 
129
              retval.namespaces = (HashMap) namespaces.clone();
 
130
            }
 
131
            else
 
132
            {
 
133
              retval.namespaces = new HashMap();
 
134
            }
 
135
          }
 
136
          retval.namespaces.put(entry.getValue(), "");
 
137
        }
 
138
      }
 
139
      else if (AttributeList.XMLNS_NAMESPACE.equals(entry.getNamespace()))
 
140
      {
 
141
        if (retval == null)
 
142
        {
 
143
          retval = new DeclaredNamespaces();
 
144
          if (namespaces != null)
 
145
          {
 
146
            retval.namespaces = (HashMap) namespaces.clone();
 
147
          }
 
148
          else
 
149
          {
 
150
            retval.namespaces = new HashMap();
 
151
          }
 
152
        }
 
153
        retval.namespaces.put(entry.getValue(), prefix);
 
154
      }
 
155
    }
 
156
 
 
157
    if (retval == null)
 
158
    {
 
159
      return this;
 
160
    }
 
161
    return retval;
 
162
  }
 
163
 
 
164
  /**
 
165
   * Adds a single namespace definition to the collection.
 
166
   *
 
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.
 
170
   */
 
171
  public DeclaredNamespaces add(final String uri, final String prefix)
 
172
  {
 
173
    if (uri == null)
 
174
    {
 
175
      throw new NullPointerException();
 
176
    }
 
177
    if (prefix == null)
 
178
    {
 
179
      throw new NullPointerException();
 
180
    }
 
181
    final DeclaredNamespaces retval = new DeclaredNamespaces();
 
182
    if (namespaces == null)
 
183
    {
 
184
      retval.namespaces = new HashMap();
 
185
    }
 
186
    else
 
187
    {
 
188
      retval.namespaces = (HashMap) namespaces.clone();
 
189
    }
 
190
    retval.namespaces.put(uri, prefix);
 
191
    return retval;
 
192
  }
 
193
 
 
194
  /**
 
195
   * Looksup the prefix for the given URI. This returns null if the URI is not a declared namespace.
 
196
   *
 
197
   * @param uri the URI.
 
198
   * @return the prefix for the given URI or null, if the URI is not part of this collection.
 
199
   */
 
200
  public String getPrefix(final String uri)
 
201
  {
 
202
    if (uri == null)
 
203
    {
 
204
      throw new NullPointerException();
 
205
    }
 
206
 
 
207
    if (namespaces == null)
 
208
    {
 
209
      return null;
 
210
    }
 
211
    return (String) namespaces.get(uri);
 
212
  }
 
213
 
 
214
  /**
 
215
   * Checks, whether the namespace marked by the given URI is defined in this collection.
 
216
   *
 
217
   * @param uri the URI to be checked.
 
218
   * @return true, if the URI is known, false otherwise.
 
219
   */
 
220
  public boolean isNamespaceDefined(final String uri)
 
221
  {
 
222
    if (uri == null)
 
223
    {
 
224
      throw new NullPointerException();
 
225
    }
 
226
 
 
227
    if (namespaces == null)
 
228
    {
 
229
      return false;
 
230
    }
 
231
    return namespaces.containsKey(uri);
 
232
  }
 
233
 
 
234
  /**
 
235
   * Returns all known namespaces as unmodifiable map.
 
236
   *
 
237
   * @return the namespaces.
 
238
   */
 
239
  public Map getNamespaces()
 
240
  {
 
241
    return Collections.unmodifiableMap(namespaces);
 
242
  }
 
243
 
 
244
  /**
 
245
   * Checks whether the given prefix is already defined in the collection.
 
246
   *
 
247
   * @param prefix the prefix.
 
248
   * @return true, if the prefix is already used, false otherwise.
 
249
   */
 
250
  public boolean isPrefixDefined(final String prefix)
 
251
  {
 
252
    return namespaces.containsValue(prefix);
 
253
  }
 
254
}