static bool SAMLLoadSchema(XercesDOMParser &parser, const SAMLGlibString &schemaDir, const char *filename) { SAMLGlibString schemaPath(g_build_filename(schemaDir.c_str(), filename, NULL)); Grammar *g = parser.loadGrammar(schemaPath.c_str(), Grammar::SchemaGrammarType, true); if (g == NULL) { /* * The parser complains even with official schemas, so we don't * normally set an error handler. However, this should not fail since * we control these files, so try again with logging, so we can see * what went wrong. */ SAMLErrorHandler errorHandler; parser.setErrorHandler(&errorHandler); g = parser.loadGrammar(schemaPath.c_str(), Grammar::SchemaGrammarType, true); Warning("Failed to load XML Schema from %s.\n", schemaPath.c_str()); return false; } return true; }
// --------------------------------------------------------------------------- int main(int argc, const char** argv) { try { XMLPlatformUtils::Initialize(); } catch(const XMLException& e) { StrX tmp_e(e.getMessage()); cerr << "Xerces initialization error: " << tmp_e.localForm() << endl; return 2; } // check command line for arguments if ( argc < 1 ) { cerr << "usage: schema-check <xml-file> [<schema-file> ...]" << endl; return 3; } XercesDOMParser *parser = new XercesDOMParser; DOMTreeErrorReporter *errReporter = new DOMTreeErrorReporter(); parser->setErrorHandler(errReporter); parser->setDoNamespaces(true); parser->setCreateEntityReferenceNodes(true); parser->useCachedGrammarInParse(true); if ( argc > 2 ) { parser->setDoSchema(true); parser->setDoValidation(true); parser->setValidationSchemaFullChecking(true); for ( int i = 2; i < argc; i++ ) { if ( parser->loadGrammar(argv[i], Grammar::SchemaGrammarType, true) == 0 ) { cerr << "Error loading grammar " << std::string(argv[i]) << endl; return 4; } } } bool errorsOccured = true; try { parser->parse(argv[1]); errorsOccured = false; } catch ( const OutOfMemoryException& ) { cerr << "Out of memory exception." << endl; } catch ( const XMLException& e ) { cerr << "An error occurred during parsing" << endl << " Message: " << StrX(e.getMessage()) << endl; } catch ( const DOMException& e ) { const unsigned int maxChars = 2047; XMLCh errText[maxChars + 1]; cerr << endl << "DOM Error during parsing: '" << std::string(argv[1]) << "'" << endl << "DOM Exception code is: " << e.code << endl; if ( DOMImplementation::loadDOMExceptionMsg(e.code, errText, maxChars) ) cerr << "Message is: " << StrX(errText) << endl; } catch (...) { cerr << "An error occurred during parsing." << endl; } return errorsOccured ? 1 : 0; }