}
this.resource = resource;
is = inputStream;
this.options = options;
XMLParserPool pool = (XMLParserPool)options.get(XMLResource.OPTION_USE_PARSER_POOL);
@SuppressWarnings("unchecked") Map<String, Boolean> parserFeatures = (Map<String, Boolean>)options.get(XMLResource.OPTION_PARSER_FEATURES);
@SuppressWarnings("unchecked") Map<String, ?> parserProperties = (Map<String, ?>)options.get(XMLResource.OPTION_PARSER_PROPERTIES);
parserFeatures = (parserFeatures == null) ? Collections.<String, Boolean>emptyMap() : parserFeatures;
parserProperties = (parserProperties == null) ? Collections.<String, Object>emptyMap() : parserProperties;
// HACK: reading encoding
String encoding = null;
if (!Boolean.FALSE.equals(options.get(XMLResource.OPTION_USE_DEPRECATED_METHODS)))
{
encoding = getEncoding();
resource.setEncoding(encoding);
}
try
{
SAXParser parser;
DefaultHandler handler;
if (pool != null)
{
// use the pool to retrieve the parser
parser = pool.get(parserFeatures, parserProperties, Boolean.TRUE.equals(options.get(XMLResource.OPTION_USE_LEXICAL_HANDLER)));
handler = (DefaultHandler)pool.getDefaultHandler(resource, this, helper, options);
}
else
{
parser = makeParser();
handler = makeDefaultHandler();
// set features and properties
if (parserFeatures != null)
{
for (Map.Entry<String, Boolean> entry : parserFeatures.entrySet())
{
parser.getXMLReader().setFeature(entry.getKey(), entry.getValue());
}
}
if (parserProperties !=null)
{
for (Map.Entry<String, ?> entry : parserProperties.entrySet())
{
parser.getXMLReader().setProperty(entry.getKey(), entry.getValue());
}
}
}
InputSource inputSource = new InputSource(is);
if (resource.getURI() != null)
{
String resourceURI = resource.getURI().toString();
inputSource.setPublicId(resourceURI);
inputSource.setSystemId(resourceURI);
inputSource.setEncoding(encoding);
}
// set lexical handler
if (Boolean.TRUE.equals(options.get(XMLResource.OPTION_USE_LEXICAL_HANDLER)))
{
if (parserProperties == null || parserProperties.get(SAX_LEXICAL_PROPERTY) == null)
{
parser.setProperty(SAX_LEXICAL_PROPERTY, handler);
}
}
parser.parse(inputSource, handler);
// release parser back to the pool
if (pool != null)
{
pool.release(parser, parserFeatures, parserProperties, Boolean.TRUE.equals(options.get(XMLResource.OPTION_USE_LEXICAL_HANDLER)));
pool.releaseDefaultHandler((XMLDefaultHandler)handler, options);
}
helper = null;
handleErrors();
}