* @param schemaURLDeque The mapping of schema URLs to other URLs that will
* be used as the actual locations of those schemas, in the schema parse order.
*/
private static void parse(XMLInputSource source, Object handler, EntityResolver resolver, LookupDeque schemaURLDeque)
{
SAXParser parser = null;
XMLParserConfiguration config = null;
// Try to get a parser for the given grammar from the pool
synchronized (s_saxParserMap)
{
List holderList = (List)s_saxParserMap.get(getKey(schemaURLDeque));
if (holderList != null)
{
SAXHolder holder = null;
while (holderList.size() > 0 && holder == null)
{
holder = (SAXHolder)((SoftReference)holderList.remove(holderList.size() - 1)).get();
}
if (holder != null)
{
parser = holder.getParser();
config = holder.getConfig();
}
}
}
ClassLoader classLoaderSaved = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(XMLUtil.class.getClassLoader());
// Parse the source stream
try
{
// If the attempt to retrieve a parser failed, create a new one
if (parser == null)
{
config = getParserConfiguration(schemaURLDeque);
parser = new SAXParser(config);
parser.setProperty(BUFFER_SIZE_PROPERTY, BUFFER_SIZE);
}
parser.setContentHandler((handler instanceof ContentHandler) ? (ContentHandler)handler : DEFAULT_HANDLER);
parser.setErrorHandler((handler instanceof ErrorHandler) ? (ErrorHandler)handler : DEFAULT_HANDLER);
parser.setDTDHandler((handler instanceof DTDHandler) ? (DTDHandler)handler : DEFAULT_HANDLER);
parser.setEntityResolver((resolver != null) ? resolver : DEFAULT_HANDLER);
if (handler instanceof LexicalHandler)
{
parser.setProperty(LEXICAL_HANDLER_PROPERTY, handler);
}
if (handler instanceof IncrementalHandler && config instanceof XMLPullParserConfiguration)
{
IncrementalHandler incrementalHandler = (IncrementalHandler)handler;
XMLPullParserConfiguration pullConfig = (XMLPullParserConfiguration)config;
pullConfig.setInputSource(source);
parser.reset();
while (!incrementalHandler.isComplete() && pullConfig.parse(false)) ;
}
else
{
parser.parse(source);
}
}
catch (XMLParseException e)
{
throw convertException(e);
}
catch (Exception e)
{
throw new XMLException("err.xml.parse", e);
}
finally
{
// Put the parser back into the pool, using a soft reference
if (parser != null)
{
try
{
parser.setContentHandler(DEFAULT_HANDLER);
parser.setEntityResolver(DEFAULT_HANDLER);
parser.setErrorHandler(DEFAULT_HANDLER);
parser.setDTDHandler(DEFAULT_HANDLER);
parser.setProperty(LEXICAL_HANDLER_PROPERTY, null);
parser.reset();
if (config instanceof XMLPullParserConfiguration)
{
((XMLPullParserConfiguration)config).cleanup();
}