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

« back to all changes in this revision

Viewing changes to source/org/jfree/xmlns/parser/MultiplexRootElementHandler.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
 
 * =========================================
3
 
 * LibXML : a free Java layouting library
4
 
 * =========================================
5
 
 *
6
 
 * Project Info:  http://reporting.pentaho.org/libxml/
7
 
 *
8
 
 * (C) Copyright 2006-2007, 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: MultiplexRootElementHandler.java 3518 2007-10-16 10:26:53Z tmorgner $
28
 
 * ------------
29
 
 * (C) Copyright 2006-2007, by Pentaho Corporation.
30
 
 */
31
 
package org.jfree.xmlns.parser;
32
 
 
33
 
import java.io.IOException;
34
 
 
35
 
import org.jfree.resourceloader.ResourceKey;
36
 
import org.jfree.resourceloader.ResourceManager;
37
 
import org.xml.sax.Attributes;
38
 
import org.xml.sax.EntityResolver;
39
 
import org.xml.sax.InputSource;
40
 
import org.xml.sax.SAXException;
41
 
 
42
 
/**
43
 
 * Creation-Date: 08.04.2006, 13:29:54
44
 
 *
45
 
 * @author Thomas Morgner
46
 
 */
47
 
public class MultiplexRootElementHandler extends RootXmlReadHandler
48
 
{
49
 
  private static class RootEntityResolver implements EntityResolver
50
 
  {
51
 
    private ParserEntityResolver entityResolver;
52
 
    private String publicId;
53
 
    private String systemId;
54
 
 
55
 
    private RootEntityResolver()
56
 
    {
57
 
      entityResolver = ParserEntityResolver.getDefaultResolver();
58
 
    }
59
 
 
60
 
    public InputSource resolveEntity(final String publicId, final String systemId)
61
 
        throws SAXException, IOException
62
 
    {
63
 
      this.publicId = publicId;
64
 
      this.systemId = systemId;
65
 
      return entityResolver.resolveEntity(publicId, systemId);
66
 
    }
67
 
 
68
 
    public String getPublicId()
69
 
    {
70
 
      return publicId;
71
 
    }
72
 
 
73
 
    public String getSystemId()
74
 
    {
75
 
      return systemId;
76
 
    }
77
 
 
78
 
    public ParserEntityResolver getEntityResolver()
79
 
    {
80
 
      return entityResolver;
81
 
    }
82
 
  }
83
 
 
84
 
  private XmlFactoryModule[] rootHandlers;
85
 
  private RootEntityResolver entityResolver;
86
 
  private boolean xmlnsUrisNotAvailable;
87
 
 
88
 
  public MultiplexRootElementHandler
89
 
      (final ResourceManager manager,
90
 
       final ResourceKey source,
91
 
       final ResourceKey context,
92
 
       final long version,
93
 
       final XmlFactoryModule[] rootHandlers)
94
 
  {
95
 
    super(manager, source, context, version);
96
 
    this.entityResolver = new RootEntityResolver();
97
 
    this.rootHandlers = (XmlFactoryModule[]) rootHandlers.clone();
98
 
  }
99
 
 
100
 
  public boolean isXmlnsUrisNotAvailable()
101
 
  {
102
 
    return xmlnsUrisNotAvailable;
103
 
  }
104
 
 
105
 
  public void setXmlnsUrisNotAvailable(final boolean xmlnsUrisNotAvailable)
106
 
  {
107
 
    this.xmlnsUrisNotAvailable = xmlnsUrisNotAvailable;
108
 
  }
109
 
 
110
 
  public EntityResolver getEntityResolver()
111
 
  {
112
 
    return entityResolver;
113
 
  }
114
 
 
115
 
  public ParserEntityResolver getParserEntityResolver()
116
 
  {
117
 
    return entityResolver.getEntityResolver();
118
 
  }
119
 
 
120
 
  protected XmlFactoryModule[] getRootHandlers()
121
 
  {
122
 
    return rootHandlers;
123
 
  }
124
 
 
125
 
  /**
126
 
   * Starts processing an element.
127
 
   *
128
 
   * @param originalUri the URI.
129
 
   * @param localName   the local name.
130
 
   * @param qName       the qName.
131
 
   * @param attributes  the attributes.
132
 
   * @throws SAXException if there is a parsing problem.
133
 
   */
134
 
  protected void interceptFirstStartElement(final String originalUri,
135
 
                                            final String localName,
136
 
                                            final String qName,
137
 
                                            Attributes attributes)
138
 
      throws SAXException
139
 
  {
140
 
    // build the document info and select the root handler that will
141
 
    // deal with the document content.
142
 
    final DefaultXmlDocumentInfo documentInfo = new DefaultXmlDocumentInfo();
143
 
    documentInfo.setPublicDTDId(entityResolver.getPublicId());
144
 
    documentInfo.setSystemDTDId(entityResolver.getSystemId());
145
 
    documentInfo.setRootElement(localName);
146
 
    documentInfo.setRootElementNameSpace(originalUri);
147
 
 
148
 
    final String nsuri = attributes.getValue("xmlns");
149
 
    if (nsuri != null)
150
 
    {
151
 
      documentInfo.setDefaultNameSpace(nsuri);
152
 
    }
153
 
    else
154
 
    {
155
 
      documentInfo.setDefaultNameSpace("");
156
 
    }
157
 
 
158
 
    // ok, now find the best root handler and start parsing ...
159
 
    XmlFactoryModule bestRootHandler = null;
160
 
    int bestRootHandlerWeight = -1;
161
 
    for (int i = 0; i < rootHandlers.length; i++)
162
 
    {
163
 
      final XmlFactoryModule rootHandler = rootHandlers[i];
164
 
      final int weight = rootHandler.getDocumentSupport(documentInfo);
165
 
      if (weight > bestRootHandlerWeight)
166
 
      {
167
 
        bestRootHandler = rootHandler;
168
 
        bestRootHandlerWeight = weight;
169
 
      }
170
 
    }
171
 
    if (bestRootHandlerWeight < 0 || bestRootHandler == null)
172
 
    {
173
 
      throw new SAXException("No suitable root handler known for this document: " + documentInfo);
174
 
    }
175
 
    final XmlReadHandler readHandler =
176
 
        bestRootHandler.createReadHandler(documentInfo);
177
 
    if (readHandler == null)
178
 
    {
179
 
      throw new SAXException("Unable to create the root handler." + bestRootHandler);
180
 
    }
181
 
 
182
 
    String defaultNamespace = documentInfo.getDefaultNameSpace();
183
 
    if (defaultNamespace == null || "".equals(defaultNamespace))
184
 
    {
185
 
      // Now correct the namespace ..
186
 
      defaultNamespace = bestRootHandler.getDefaultNamespace(documentInfo);
187
 
      if (defaultNamespace != null && "".equals(defaultNamespace) == false)
188
 
      {
189
 
        documentInfo.setRootElementNameSpace(defaultNamespace);
190
 
      }
191
 
    }
192
 
 
193
 
    pushDefaultNamespace(defaultNamespace);
194
 
 
195
 
    final String uri;
196
 
    if ((originalUri == null || "".equals(originalUri)) &&
197
 
        defaultNamespace != null)
198
 
    {
199
 
      uri = defaultNamespace;
200
 
    }
201
 
    else
202
 
    {
203
 
      uri = originalUri;
204
 
    }
205
 
 
206
 
    attributes = new FixNamespaceUriAttributes(uri, attributes);
207
 
    installRootHandler(readHandler, uri, localName, attributes);
208
 
  }
209
 
 
210
 
  /**
211
 
   * Returns the object for this element or null, if this element does not
212
 
   * create an object.
213
 
   *
214
 
   * @return the object.
215
 
   */
216
 
  public Object getObject()
217
 
  {
218
 
    return null;
219
 
  }
220
 
}