00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 #include <iostream>
00014 #include <xercesc/sax2/SAX2XMLReader.hpp>
00015 #include <xercesc/sax2/XMLReaderFactory.hpp>
00016 #include <xercesc/util/XMLString.hpp>
00017 #include <xercesc/util/PlatformUtils.hpp>
00018 #include <xercesc/util/Janitor.hpp>
00019 #include <xercesc/framework/URLInputSource.hpp>
00020 #ifndef __MSD_SAX_2_PARSER__
00021 #include "msdSax2Parser.h"
00022 #endif
00023
00024
00025
00026
00027 using namespace std;
00028 using namespace xercesc;
00029
00030
00031
00032
00033 void msdSax2Handler::error(const SAXParseException& e)
00034 {
00035 fSawErrors = true;
00036 errors << "[Error] " << XMLString::transcode(e.getSystemId())
00037 << ": line " << e.getLineNumber()
00038 << ", col " << e.getColumnNumber()
00039 << ": " << XMLString::transcode(e.getMessage()) << std::endl;
00040 }
00041
00042 void msdSax2Handler::fatalError(const SAXParseException& e)
00043 {
00044 fSawErrors = true;
00045 errors << "[Fatal Error] " << XMLString::transcode(e.getSystemId())
00046 << ": line " << e.getLineNumber()
00047 << ", col " << e.getColumnNumber()
00048 << ": " << XMLString::transcode(e.getMessage()) << std::endl;
00049 }
00050
00051 void msdSax2Handler::warning(const SAXParseException& e)
00052 {
00053 fSawErrors = true;
00054 errors << "[Warning] " << XMLString::transcode(e.getSystemId())
00055 << ": line " << e.getLineNumber()
00056 << ", col " << e.getColumnNumber()
00057 << ": " << XMLString::transcode(e.getMessage()) << std::endl;
00058 }
00059
00060 const char* msdSax2Handler::getErrors()
00061 {
00062 return errors.str().c_str();
00063 }
00064
00065
00066
00067
00068 const char* msdSax2Parser::validate(char* xmlFile) {
00069
00070 std::ostringstream result;
00071
00072
00073
00074 try {
00075 XMLPlatformUtils::Initialize();
00076 }
00077 catch (const XMLException& toCatch) {
00078
00079 char* message = XMLString::transcode(toCatch.getMessage());
00080 result << "Error during initialization! :\n";
00081 result << "XMLException message is: \n"
00082 << message << "\n";
00083
00084 XMLString::release(&message);
00085 return result.str().c_str();
00086 }
00087
00088 SAX2XMLReader* parser = XMLReaderFactory::createXMLReader();
00089
00090
00091
00092 XMLCh* externalSchemaLocation = XMLString::transcode("http://www.geodesy.org/river# River.xsd");
00093
00094
00095 ArrayJanitor<XMLCh> janValue(externalSchemaLocation);
00096
00097
00098
00099
00100 parser->setProperty(XMLUni::fgXercesSchemaExternalSchemaLocation, externalSchemaLocation);
00101
00102
00103 parser->setFeature(XMLUni::fgSAX2CoreValidation, true);
00104
00105
00106 parser->setFeature(XMLUni::fgSAX2CoreNameSpaces, true);
00107
00108
00109 parser->setFeature(XMLUni::fgXercesSchema, true);
00110
00111
00112 parser->setFeature(XMLUni::fgXercesSchemaFullChecking, true);
00113
00114
00115 msdSax2Handler* errorHandler = new msdSax2Handler();
00116 parser->setErrorHandler(errorHandler);
00117
00118 try {
00119
00120
00121
00122 parser->parse(xmlFile);
00123 result << errorHandler->getErrors();
00124 }
00125 catch (const XMLException& toCatch) {
00126 char* message = XMLString::transcode(toCatch.getMessage());
00127 result << "XMLException message is: \n"
00128 << message << "\n";
00129 XMLString::release(&message);
00130 return result.str().c_str();
00131 }
00132 catch (const SAXParseException& toCatch) {
00133 char* message = XMLString::transcode(toCatch.getMessage());
00134 result << "SAXParseException message is: \n"
00135 << message << "\n";
00136 XMLString::release(&message);
00137 return result.str().c_str();
00138 }
00139
00140 delete parser;
00141 delete errorHandler;
00142 return result.str().c_str();
00143 }
00144