*/
public void validate(Source source, Result result)
throws SAXException, IOException {
if (result instanceof SAXResult || result == null) {
final SAXSource saxSource = (SAXSource) source;
final SAXResult saxResult = (SAXResult) result;
LexicalHandler lh = null;
if (result != null) {
ContentHandler ch = saxResult.getHandler();
lh = saxResult.getLexicalHandler();
/** If the lexical handler is not set try casting the ContentHandler. **/
if (lh == null && ch instanceof LexicalHandler) {
lh = (LexicalHandler) ch;
}
setContentHandler(ch);
}
XMLReader reader = null;
try {
reader = saxSource.getXMLReader();
if (reader == null) {
// create one now
SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setNamespaceAware(true);
try {
reader = spf.newSAXParser().getXMLReader();
// If this is a Xerces SAX parser, set the security manager if there is one
if (reader instanceof mf.org.apache.xerces.parsers.SAXParser) {
Object securityManager = fComponentManager.getProperty(SECURITY_MANAGER);
if (securityManager != null) {
try {
reader.setProperty(SECURITY_MANAGER, securityManager);
}
// Ignore the exception if the security manager cannot be set.
catch (SAXException exc) {}
}
}
}
catch (Exception e) {
// this is impossible, but better safe than sorry
throw new FactoryConfigurationError(e);
}
}
// If XML names and Namespace URIs are already internalized we
// can avoid running them through the SymbolTable.
try {
fStringsInternalized = reader.getFeature(STRING_INTERNING);
}
catch (SAXException exc) {
// The feature isn't recognized or getting it is not supported.
// In either case, assume that strings are not internalized.
fStringsInternalized = false;
}
ErrorHandler errorHandler = fComponentManager.getErrorHandler();
reader.setErrorHandler(errorHandler != null ? errorHandler : DraconianErrorHandler.getInstance());
reader.setEntityResolver(fResolutionForwarder);
fResolutionForwarder.setEntityResolver(fComponentManager.getResourceResolver());
reader.setContentHandler(this);
reader.setDTDHandler(this);
try {
reader.setProperty(LEXICAL_HANDLER, lh);
}
// Ignore the exception if the lexical handler cannot be set.
catch (SAXException exc) {}
InputSource is = saxSource.getInputSource();
reader.parse(is);
}
finally {
// Release the reference to user's ContentHandler ASAP
setContentHandler(null);