2
* =========================================
3
* LibXML : a free Java layouting library
4
* =========================================
6
* Project Info: http://reporting.pentaho.org/libxml/
8
* (C) Copyright 2006-2007, by Object Refinery Ltd, Pentaho Corporation and Contributors.
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.
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.
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.
22
* [Java is a trademark or registered trademark of Sun Microsystems, Inc.
23
* in the United States and other countries.]
27
* $Id: MultiplexRootElementHandler.java 3518 2007-10-16 10:26:53Z tmorgner $
29
* (C) Copyright 2006-2007, by Pentaho Corporation.
31
package org.jfree.xmlns.parser;
33
import java.io.IOException;
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;
43
* Creation-Date: 08.04.2006, 13:29:54
45
* @author Thomas Morgner
47
public class MultiplexRootElementHandler extends RootXmlReadHandler
49
private static class RootEntityResolver implements EntityResolver
51
private ParserEntityResolver entityResolver;
52
private String publicId;
53
private String systemId;
55
private RootEntityResolver()
57
entityResolver = ParserEntityResolver.getDefaultResolver();
60
public InputSource resolveEntity(final String publicId, final String systemId)
61
throws SAXException, IOException
63
this.publicId = publicId;
64
this.systemId = systemId;
65
return entityResolver.resolveEntity(publicId, systemId);
68
public String getPublicId()
73
public String getSystemId()
78
public ParserEntityResolver getEntityResolver()
80
return entityResolver;
84
private XmlFactoryModule[] rootHandlers;
85
private RootEntityResolver entityResolver;
86
private boolean xmlnsUrisNotAvailable;
88
public MultiplexRootElementHandler
89
(final ResourceManager manager,
90
final ResourceKey source,
91
final ResourceKey context,
93
final XmlFactoryModule[] rootHandlers)
95
super(manager, source, context, version);
96
this.entityResolver = new RootEntityResolver();
97
this.rootHandlers = (XmlFactoryModule[]) rootHandlers.clone();
100
public boolean isXmlnsUrisNotAvailable()
102
return xmlnsUrisNotAvailable;
105
public void setXmlnsUrisNotAvailable(final boolean xmlnsUrisNotAvailable)
107
this.xmlnsUrisNotAvailable = xmlnsUrisNotAvailable;
110
public EntityResolver getEntityResolver()
112
return entityResolver;
115
public ParserEntityResolver getParserEntityResolver()
117
return entityResolver.getEntityResolver();
120
protected XmlFactoryModule[] getRootHandlers()
126
* Starts processing an element.
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.
134
protected void interceptFirstStartElement(final String originalUri,
135
final String localName,
137
Attributes attributes)
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);
148
final String nsuri = attributes.getValue("xmlns");
151
documentInfo.setDefaultNameSpace(nsuri);
155
documentInfo.setDefaultNameSpace("");
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++)
163
final XmlFactoryModule rootHandler = rootHandlers[i];
164
final int weight = rootHandler.getDocumentSupport(documentInfo);
165
if (weight > bestRootHandlerWeight)
167
bestRootHandler = rootHandler;
168
bestRootHandlerWeight = weight;
171
if (bestRootHandlerWeight < 0 || bestRootHandler == null)
173
throw new SAXException("No suitable root handler known for this document: " + documentInfo);
175
final XmlReadHandler readHandler =
176
bestRootHandler.createReadHandler(documentInfo);
177
if (readHandler == null)
179
throw new SAXException("Unable to create the root handler." + bestRootHandler);
182
String defaultNamespace = documentInfo.getDefaultNameSpace();
183
if (defaultNamespace == null || "".equals(defaultNamespace))
185
// Now correct the namespace ..
186
defaultNamespace = bestRootHandler.getDefaultNamespace(documentInfo);
187
if (defaultNamespace != null && "".equals(defaultNamespace) == false)
189
documentInfo.setRootElementNameSpace(defaultNamespace);
193
pushDefaultNamespace(defaultNamespace);
196
if ((originalUri == null || "".equals(originalUri)) &&
197
defaultNamespace != null)
199
uri = defaultNamespace;
206
attributes = new FixNamespaceUriAttributes(uri, attributes);
207
installRootHandler(readHandler, uri, localName, attributes);
211
* Returns the object for this element or null, if this element does not
214
* @return the object.
216
public Object getObject()