package org.xins.common.xml;
import java.io.ByteArrayInputStream;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.parsers.SAXParser;
import org.xins.common.Log;
import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;
public class SAXParserProvider {
private static final SAXParserFactory SAX_PARSER_FACTORY;
private static ThreadLocal CACHE;
static {
SAX_PARSER_FACTORY = SAXParserFactory.newInstance();
SAX_PARSER_FACTORY.setNamespaceAware(true);
SAX_PARSER_FACTORY.setValidating(false);
CACHE = new ThreadLocal();
}
private SAXParserProvider() {
}
public static SAXParser get() {
Object o = CACHE.get();
SAXParser parser;
if (o == null) {
parser = create();
CACHE.set(parser);
} else {
parser = (SAXParser) o;
}
return parser;
}
private static SAXParser create() {
SAXParser parser;
try {
parser = SAX_PARSER_FACTORY.newSAXParser();
parser.getXMLReader().setEntityResolver(new EntityResolver() {
public InputSource resolveEntity(String publicId, String systemId) {
return new InputSource(new ByteArrayInputStream(new byte[0]));
}
});
} catch (Exception exception) {
Log.log_1550(exception);
String exceptionMessage = exception.getMessage();
String message;
if (exceptionMessage == null) {
message = "Error when creating a SAX parser.";
} else {
message = "Error when creating a SAX parser: \""
+ exceptionMessage
+ "\".";
}
RuntimeException e = new RuntimeException(message, exception);
throw e;
}
return parser;
}
}