2007-09-07 23:17:30 +02:00
|
|
|
#include "wrapxerces.h"
|
2007-09-08 00:25:30 +02:00
|
|
|
#define XERCES_TMPLSINC
|
2007-09-07 23:17:30 +02:00
|
|
|
|
|
|
|
//#include <xercesc/util/PlatformUtils.hpp>
|
|
|
|
#include <xercesc/sax2/XMLReaderFactory.hpp>
|
|
|
|
#include <xercesc/sax2/SAX2XMLReader.hpp>
|
|
|
|
#include <xercesc/sax2/DefaultHandler.hpp>
|
|
|
|
#include <xercesc/util/XMLUni.hpp>
|
|
|
|
#include <sstream>
|
|
|
|
#include <utility>
|
|
|
|
#include <stdexcept>
|
|
|
|
|
|
|
|
using namespace xercesc;
|
|
|
|
|
|
|
|
WrapXerces::WrapXerces()
|
|
|
|
{
|
2007-09-08 00:25:30 +02:00
|
|
|
try
|
|
|
|
{
|
|
|
|
XMLPlatformUtils::Initialize();
|
|
|
|
}
|
|
|
|
catch ( XMLException& e )
|
|
|
|
{
|
|
|
|
throw std::runtime_error ( "Cannot initialize Xerces" );
|
|
|
|
}
|
|
|
|
errorPosition = std::make_pair ( 1, 1 );
|
2007-09-07 23:17:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
WrapXerces::~WrapXerces()
|
|
|
|
{
|
2007-09-08 00:25:30 +02:00
|
|
|
XMLPlatformUtils::Terminate();
|
2007-09-07 23:17:30 +02:00
|
|
|
}
|
|
|
|
|
2007-09-08 00:25:30 +02:00
|
|
|
bool WrapXerces::validate ( const std::string& fileName )
|
2007-09-07 23:17:30 +02:00
|
|
|
{
|
2007-09-08 00:25:30 +02:00
|
|
|
SAX2XMLReader *parser = XMLReaderFactory::createXMLReader();
|
2007-09-07 23:17:30 +02:00
|
|
|
|
2007-09-08 00:25:30 +02:00
|
|
|
parser->setFeature ( XMLUni::fgSAX2CoreNameSpaces, true );
|
|
|
|
parser->setFeature ( XMLUni::fgSAX2CoreValidation, true );
|
|
|
|
parser->setFeature ( XMLUni::fgXercesDynamic, false );
|
|
|
|
parser->setFeature ( XMLUni::fgXercesSchema, true );
|
|
|
|
parser->setFeature ( XMLUni::fgXercesSchemaFullChecking, true );
|
|
|
|
parser->setFeature ( XMLUni::fgXercesValidationErrorAsFatal, true );
|
2007-09-07 23:17:30 +02:00
|
|
|
|
2007-09-08 00:25:30 +02:00
|
|
|
DefaultHandler handler;
|
|
|
|
MySAX2Handler mySAX2Handler;
|
|
|
|
parser->setContentHandler ( &handler );
|
|
|
|
parser->setErrorHandler ( &mySAX2Handler );
|
|
|
|
parser->setEntityResolver ( &handler );
|
2007-09-07 23:17:30 +02:00
|
|
|
|
2007-09-08 00:25:30 +02:00
|
|
|
try
|
|
|
|
{
|
|
|
|
parser->parse ( fileName.c_str() );
|
|
|
|
}
|
|
|
|
catch ( XMLException& e )
|
|
|
|
{
|
|
|
|
delete parser;
|
|
|
|
char *err = XMLString::transcode ( e.getMessage() );
|
|
|
|
lastError = err;
|
|
|
|
XMLString::release ( &err );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
catch ( SAXParseException& e )
|
|
|
|
{
|
|
|
|
delete parser;
|
|
|
|
char *err = XMLString::transcode ( e.getMessage() );
|
|
|
|
std::stringstream ss;
|
|
|
|
ss << "Validation stopped at line " << e.getLineNumber() << ", column " << e.getColumnNumber() << ": " << err;
|
|
|
|
lastError = ss.str();
|
|
|
|
errorPosition = std::make_pair ( e.getLineNumber(), e.getColumnNumber() );
|
|
|
|
XMLString::release ( &err );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
catch ( ... )
|
|
|
|
{
|
|
|
|
delete parser;
|
|
|
|
lastError = "Unexpected validation error";
|
|
|
|
return false;
|
|
|
|
}
|
2007-09-07 23:17:30 +02:00
|
|
|
delete parser;
|
2007-09-08 00:25:30 +02:00
|
|
|
return true;
|
2007-09-07 23:17:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string WrapXerces::getLastError()
|
|
|
|
{
|
2007-09-08 00:25:30 +02:00
|
|
|
return lastError;
|
2007-09-07 23:17:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
std::pair<int, int> WrapXerces::getErrorPosition()
|
|
|
|
{
|
2007-09-08 00:25:30 +02:00
|
|
|
return errorPosition;
|
2007-09-07 23:17:30 +02:00
|
|
|
}
|