final XMLReader reader = parser.getXMLReader();
final XmlFactoryModule[] rootHandlers = getModules();
if (rootHandlers.length == 0)
{
throw new ResourceCreationException("There are no root-handlers registered for the factory for type " + getFactoryType());
}
final ResourceDataInputSource input = new ResourceDataInputSource(data, manager);
final ResourceKey contextKey;
final long version;
final ResourceKey targetKey = data.getKey();
if (context == null)
{
contextKey = targetKey;
version = data.getVersion(manager);
}
else
{
contextKey = context;
version = -1;
}
final MultiplexRootElementHandler handler =
new MultiplexRootElementHandler(manager, targetKey,
contextKey, version, rootHandlers);
final DefaultConfiguration parserConfiguration = handler.getParserConfiguration();
final URL value = manager.toURL(contextKey);
if (value != null)
{
parserConfiguration.setConfigProperty(CONTENTBASE_KEY, value.toExternalForm());
}
configureReader(reader, handler);
reader.setContentHandler(handler);
reader.setDTDHandler(handler);
reader.setEntityResolver(handler.getEntityResolver());
reader.setErrorHandler(getErrorHandler());
final Map parameters = targetKey.getFactoryParameters();
final Iterator it = parameters.keySet().iterator();
while (it.hasNext())
{
final Object o = it.next();
if (o instanceof FactoryParameterKey)
{
final FactoryParameterKey fpk = (FactoryParameterKey) o;
handler.setHelperObject(fpk.getName(), parameters.get(fpk));
}
}
reader.parse(input);
final Object createdProduct = finishResult
(handler.getResult(), manager, data, contextKey);
handler.getDependencyCollector().add(targetKey, data.getVersion(manager));
return createResource(targetKey, handler, createdProduct);
}
catch (ParserConfigurationException e)
{
throw new ResourceCreationException
("Unable to initialize the XML-Parser", e);
}
catch (SAXException e)
{
throw new ResourceCreationException("Unable to parse the document: " + data.getKey(), e);
}
catch (IOException e)
{
throw new ResourceLoadingException("Unable to read the stream from document: " + data.getKey(), e);
}