~ubuntu-branches/ubuntu/utopic/libxml-java/utopic

« back to all changes in this revision

Viewing changes to source/org/pentaho/reporting/libraries/xmlns/parser/AbstractReadHandlerFactory.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.parser;
 
19
 
 
20
import java.util.HashMap;
 
21
import java.util.Iterator;
 
22
 
 
23
import org.pentaho.reporting.libraries.base.config.Configuration;
 
24
import org.pentaho.reporting.libraries.base.util.ObjectUtilities;
 
25
 
 
26
/**
 
27
 * The AbstractReadHandlerFactory provides a base implementation for all
 
28
 * read-handler factories. A read-handler factory decouples the tag-handlers
 
29
 * of a SAX parser and allows to configure alternate parser configuations
 
30
 * at runtime, resulting in a more flexible parsing process.
 
31
 *
 
32
 * @author Thomas Morgner
 
33
 */
 
34
public abstract class AbstractReadHandlerFactory
 
35
{
 
36
  /**
 
37
   * The TagDefinitionKey is a compund key to lookup handler implementations
 
38
   * using a namespace and tagname.
 
39
   */
 
40
  private static class TagDefinitionKey
 
41
  {
 
42
    private String namespace;
 
43
    private String tagName;
 
44
 
 
45
    /**
 
46
     * Creates a new key.
 
47
     *
 
48
     * @param namespace the namespace (can be null for undefined).
 
49
     * @param tagName   the tagname (can be null for undefined).
 
50
     */
 
51
    private TagDefinitionKey(final String namespace, final String tagName)
 
52
    {
 
53
      this.namespace = namespace;
 
54
      this.tagName = tagName;
 
55
    }
 
56
 
 
57
    /**
 
58
     * Compares this key for equality with an other object.
 
59
     *
 
60
     * @param o the other object.
 
61
     * @return true, if this key is the same as the given object, false otherwise.
 
62
     */
 
63
    public boolean equals(final Object o)
 
64
    {
 
65
      if (this == o)
 
66
      {
 
67
        return true;
 
68
      }
 
69
      if (o == null || getClass() != o.getClass())
 
70
      {
 
71
        return false;
 
72
      }
 
73
 
 
74
      final TagDefinitionKey that = (TagDefinitionKey) o;
 
75
 
 
76
      if (namespace != null ? !namespace.equals(that.namespace) : that.namespace != null)
 
77
      {
 
78
        return false;
 
79
      }
 
80
      if (tagName != null ? !tagName.equals(that.tagName) : that.tagName != null)
 
81
      {
 
82
        return false;
 
83
      }
 
84
 
 
85
      return true;
 
86
    }
 
87
 
 
88
    /**
 
89
     * Computes the hashcode for this key.
 
90
     *
 
91
     * @return the hashcode.
 
92
     */
 
93
    public int hashCode()
 
94
    {
 
95
      int result = (namespace != null ? namespace.hashCode() : 0);
 
96
      result = 29 * result + (tagName != null ? tagName.hashCode() : 0);
 
97
      return result;
 
98
    }
 
99
  }
 
100
 
 
101
  private HashMap defaultDefinitions;
 
102
  private HashMap tagData;
 
103
  private String defaultNamespace;
 
104
 
 
105
  /**
 
106
   * A default-constructor.
 
107
   */
 
108
  protected AbstractReadHandlerFactory()
 
109
  {
 
110
    defaultDefinitions = new HashMap();
 
111
    tagData = new HashMap();
 
112
  }
 
113
 
 
114
  /**
 
115
   * Configures this factory from the given configuration using the speoified
 
116
   * prefix as filter.
 
117
   *
 
118
   * @param conf   the configuration.
 
119
   * @param prefix the key-prefix.
 
120
   * @noinspection ObjectAllocationInLoop as this method configures the factory.
 
121
   */
 
122
  public void configure(final Configuration conf, final String prefix)
 
123
  {
 
124
    final HashMap knownNamespaces = new HashMap();
 
125
 
 
126
    final String nsConfPrefix = prefix + "namespace.";
 
127
    final Iterator namespaces = conf.findPropertyKeys(nsConfPrefix);
 
128
    while (namespaces.hasNext())
 
129
    {
 
130
      final String key = (String) namespaces.next();
 
131
      final String nsPrefix = key.substring(nsConfPrefix.length());
 
132
      final String nsUri = conf.getConfigProperty(key);
 
133
      knownNamespaces.put(nsPrefix, nsUri);
 
134
    }
 
135
 
 
136
    defaultNamespace = (String) knownNamespaces.get
 
137
        (conf.getConfigProperty(prefix + "namespace"));
 
138
 
 
139
    final String globalDefaultKey = prefix + "default";
 
140
    final String globalValue = conf.getConfigProperty(globalDefaultKey);
 
141
    if (isValidHandler(globalValue))
 
142
    {
 
143
      defaultDefinitions.put(null, globalValue);
 
144
    }
 
145
    else
 
146
    {
 
147
      // let the loading fail ..
 
148
      if (defaultDefinitions.containsKey(null) == false)
 
149
      {
 
150
        defaultDefinitions.put(null, "");
 
151
      }
 
152
    }
 
153
 
 
154
    final String nsDefaultPrefix = prefix + "default.";
 
155
    final Iterator defaults = conf.findPropertyKeys(nsDefaultPrefix);
 
156
    while (defaults.hasNext())
 
157
    {
 
158
      final String key = (String) defaults.next();
 
159
      final String nsPrefix = key.substring(nsDefaultPrefix.length());
 
160
      final String nsUri = (String) knownNamespaces.get(nsPrefix);
 
161
      if (nsUri == null)
 
162
      {
 
163
        continue;
 
164
      }
 
165
 
 
166
      final String tagData = conf.getConfigProperty(key);
 
167
      if (tagData == null)
 
168
      {
 
169
        continue;
 
170
      }
 
171
      if (isValidHandler(tagData))
 
172
      {
 
173
        defaultDefinitions.put(nsUri, tagData);
 
174
      }
 
175
      else
 
176
      {
 
177
        // let the loading fail .. to indicate we want no parsing ..
 
178
        defaultDefinitions.put(nsUri, "");
 
179
      }
 
180
    }
 
181
 
 
182
    final String nsTagsPrefix = prefix + "tag.";
 
183
    final Iterator tags = conf.findPropertyKeys(nsTagsPrefix);
 
184
    while (tags.hasNext())
 
185
    {
 
186
      final String key = (String) tags.next();
 
187
      final String tagDef = key.substring(nsTagsPrefix.length());
 
188
      final String tagData = conf.getConfigProperty(key);
 
189
      if (tagData == null)
 
190
      {
 
191
        continue;
 
192
      }
 
193
      if (isValidHandler(tagData) == false)
 
194
      {
 
195
        continue;
 
196
      }
 
197
 
 
198
      final int delim = tagDef.indexOf('.');
 
199
      if (delim == -1)
 
200
      {
 
201
        this.tagData.put(new TagDefinitionKey(null, tagDef), tagData);
 
202
      }
 
203
      else
 
204
      {
 
205
        final String nsPrefix = tagDef.substring(0, delim);
 
206
        final String nsUri = (String) knownNamespaces.get(nsPrefix);
 
207
        if (nsUri == null)
 
208
        {
 
209
          continue;
 
210
        }
 
211
 
 
212
        final String tagName = tagDef.substring(delim + 1);
 
213
        this.tagData.put(new TagDefinitionKey(nsUri, tagName), tagData);
 
214
      }
 
215
    }
 
216
  }
 
217
 
 
218
  /**
 
219
   * Checks, whether the given handler classname can be instantiated
 
220
   * and is in fact an object of the required target-type.
 
221
   *
 
222
   * @param className the classname that should be checked.
 
223
   * @return true, if the handler is valid, false otherwise.
 
224
   */
 
225
  private boolean isValidHandler(final String className)
 
226
  {
 
227
    if (className == null)
 
228
    {
 
229
      return false;
 
230
    }
 
231
    final Object o = ObjectUtilities.loadAndInstantiate
 
232
        (className, getClass(), getTargetClass());
 
233
    return o != null;
 
234
  }
 
235
 
 
236
  /**
 
237
   * Returns the implementation class for this read-handler factory.
 
238
   *
 
239
   * @return the implementation class.
 
240
   */
 
241
  protected abstract Class getTargetClass();
 
242
 
 
243
  /**
 
244
   * The returned handler can be null, in case no handler is registered.
 
245
   *
 
246
   * @param namespace the namespace of the xml-tag for which a handler should be returned.
 
247
   * @param tagname   the tagname of the xml-tag.
 
248
   * @return the instantiated read handler, or null if there is no handler registered.
 
249
   */
 
250
  public XmlReadHandler getHandler(String namespace, final String tagname)
 
251
  {
 
252
    if (namespace == null)
 
253
    {
 
254
      namespace = defaultNamespace;
 
255
    }
 
256
 
 
257
    final TagDefinitionKey key = new TagDefinitionKey(namespace, tagname);
 
258
    final String tagVal = (String) tagData.get(key);
 
259
    if (tagVal != null)
 
260
    {
 
261
      final Object o = ObjectUtilities.loadAndInstantiate
 
262
          (tagVal, getClass(), getTargetClass());
 
263
      return (XmlReadHandler) o;
 
264
    }
 
265
 
 
266
    final String className = (String) defaultDefinitions.get(namespace);
 
267
    if (className != null)
 
268
    {
 
269
      final Object o = ObjectUtilities.loadAndInstantiate
 
270
          (className, getClass(), getTargetClass());
 
271
      return (XmlReadHandler) o;
 
272
    }
 
273
 
 
274
    final String fallbackName = (String) defaultDefinitions.get(null);
 
275
    final Object fallbackValue = ObjectUtilities.loadAndInstantiate
 
276
        (fallbackName, getClass(), getTargetClass());
 
277
    if (fallbackValue != null)
 
278
    {
 
279
      return (XmlReadHandler) fallbackValue;
 
280
    }
 
281
    return null;
 
282
  }
 
283
 
 
284
 
 
285
}