// TODO: need to unset the parser property when it is returned to the cache?
}
// if (config.isTiming()) {
// System.err.println("Using SAX parser " + parser);
// }
StandardErrorHandler errorHandler = new StandardErrorHandler(pipe.getErrorListener());
// TODO: what about the ErrorListener in the ParseOptions?
parser.setErrorHandler(errorHandler);
receiver = makeValidator(receiver, source.getSystemId(), options);
// Reuse the previous ReceivingContentHandler if possible (it contains a useful cache of names)
ReceivingContentHandler ce;
final ContentHandler ch = parser.getContentHandler();
if (ch instanceof ReceivingContentHandler && config.isCompatible(((ReceivingContentHandler)ch).getConfiguration())) {
ce = (ReceivingContentHandler)ch;
ce.reset();
} else {
ce = new ReceivingContentHandler();
parser.setContentHandler(ce);
parser.setDTDHandler(ce);
try {
parser.setProperty("http://xml.org/sax/properties/lexical-handler", ce);
} catch (SAXNotSupportedException err) { // this just means we won't see the comments
// ignore the error
} catch (SAXNotRecognizedException err) {
// ignore the error
}
}
// TracingFilter tf = new TracingFilter();
// tf.setUnderlyingReceiver(receiver);
// tf.setPipelineConfiguration(pipe);
// receiver = tf;
ce.setReceiver(receiver);
ce.setPipelineConfiguration(pipe);
// if (stripSpace == Whitespace.IGNORABLE) {
// ce.setIgnoreIgnorableWhitespace(true);
// } else if (stripSpace == Whitespace.NONE) {
// ce.setIgnoreIgnorableWhitespace(false);
// }
boolean xqj = options.sourceIsXQJ();
try {
if (xqj) {
parser.parse("dummy");
} else {
parser.parse(source.getInputSource());
}
} catch (SAXException err) {
Exception nested = err.getException();
if (nested instanceof XPathException) {
throw (XPathException)nested;
} else if (nested instanceof RuntimeException) {
throw (RuntimeException)nested;
} else {
if (errorHandler.getErrorCount() == 0) {
// The built-in parser for JDK 1.6 has a nasty habit of not notifying errors to the ErrorHandler
throw new XPathException("Error reported by XML parser processing " +
source.getSystemId() + ": " + err.getMessage());
} else {
XPathException de = new XPathException(err);
de.setHasBeenReported();
throw de;
}
}
} catch (java.io.IOException err) {
throw new XPathException(err);
}
if (errorHandler.getErrorCount() > 0) {
throw new XPathException("The XML parser reported one or more errors");
}
if (reuseParser) {
config.reuseSourceParser(parser);
}