2008-01-06 23:13:01 +01:00
|
|
|
/*
|
2007-09-12 01:14:06 +02:00
|
|
|
* Copyright 2005-2007 Gerald Schmidt.
|
2008-01-06 23:13:01 +01:00
|
|
|
*
|
2007-09-12 01:14:06 +02:00
|
|
|
* This file is part of Xml Copy Editor.
|
2008-01-06 23:13:01 +01:00
|
|
|
*
|
2007-09-12 01:14:06 +02:00
|
|
|
* Xml Copy Editor is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; version 2 of the License.
|
2008-01-06 23:13:01 +01:00
|
|
|
*
|
2007-09-12 01:14:06 +02:00
|
|
|
* Xml Copy Editor is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2008-01-06 23:13:01 +01:00
|
|
|
*
|
2007-09-12 01:14:06 +02:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with Xml Copy Editor; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
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/sax2/XMLReaderFactory.hpp>
|
|
|
|
#include <xercesc/sax2/SAX2XMLReader.hpp>
|
|
|
|
#include <xercesc/sax2/DefaultHandler.hpp>
|
|
|
|
#include <xercesc/util/XMLUni.hpp>
|
2008-07-09 00:54:48 +02:00
|
|
|
#include <xercesc/framework/MemBufInputSource.hpp>
|
2007-09-07 23:17:30 +02:00
|
|
|
#include <sstream>
|
|
|
|
#include <utility>
|
|
|
|
#include <stdexcept>
|
|
|
|
|
|
|
|
using namespace xercesc;
|
|
|
|
|
2009-01-17 23:52:54 +01:00
|
|
|
WrapXerces::WrapXerces( std::string catalogPath, std::string catalogUtilityPath )
|
2007-09-07 23:17:30 +02:00
|
|
|
{
|
2008-01-06 23:13:01 +01:00
|
|
|
try
|
|
|
|
{
|
|
|
|
XMLPlatformUtils::Initialize();
|
|
|
|
}
|
|
|
|
catch ( XMLException& e )
|
|
|
|
{
|
|
|
|
throw std::runtime_error ( "Cannot initialize Xerces" );
|
|
|
|
}
|
|
|
|
errorPosition = std::make_pair ( 1, 1 );
|
2009-01-17 23:52:54 +01:00
|
|
|
catalogResolver = new XercesCatalogResolver( catalogPath, catalogUtilityPath );
|
2007-09-07 23:17:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
WrapXerces::~WrapXerces()
|
|
|
|
{
|
2009-01-17 23:52:54 +01:00
|
|
|
delete catalogResolver;
|
|
|
|
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
|
|
|
{
|
2008-01-06 23:13:01 +01:00
|
|
|
SAX2XMLReader *parser = XMLReaderFactory::createXMLReader();
|
2007-09-07 23:17:30 +02:00
|
|
|
|
2008-01-06 23:13:01 +01: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 );
|
2008-07-09 00:54:48 +02:00
|
|
|
parser->setFeature ( XMLUni::fgXercesLoadExternalDTD, true );
|
2007-09-07 23:17:30 +02:00
|
|
|
|
2009-01-17 23:52:54 +01:00
|
|
|
|
2008-01-06 23:13:01 +01:00
|
|
|
DefaultHandler handler;
|
|
|
|
MySAX2Handler mySAX2Handler;
|
|
|
|
parser->setContentHandler ( &handler );
|
|
|
|
parser->setErrorHandler ( &mySAX2Handler );
|
2009-01-17 23:52:54 +01:00
|
|
|
|
|
|
|
//DefaultHandler handler;
|
|
|
|
//parser->setEntityResolver ( &handler );
|
|
|
|
parser->setEntityResolver ( catalogResolver );
|
2007-09-07 23:17:30 +02:00
|
|
|
|
2008-01-06 23:13:01 +01: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;
|
|
|
|
}
|
|
|
|
delete parser;
|
|
|
|
return true;
|
2007-09-07 23:17:30 +02:00
|
|
|
}
|
|
|
|
|
2008-07-09 00:54:48 +02:00
|
|
|
// tbd: cache grammar
|
|
|
|
bool WrapXerces::validateMemory (
|
|
|
|
const char *buffer,
|
|
|
|
const char *system,
|
|
|
|
unsigned len )
|
|
|
|
{
|
|
|
|
SAX2XMLReader *parser = XMLReaderFactory::createXMLReader();
|
|
|
|
|
|
|
|
parser->setFeature ( XMLUni::fgSAX2CoreNameSpaces, true );
|
|
|
|
parser->setFeature ( XMLUni::fgSAX2CoreValidation, true );
|
|
|
|
parser->setFeature ( XMLUni::fgXercesDynamic, true );
|
|
|
|
parser->setFeature ( XMLUni::fgXercesSchema, true );
|
2009-01-17 23:52:54 +01:00
|
|
|
//parser->setFeature ( XMLUni::fgXercesSchemaFullChecking, true );
|
2008-07-09 00:54:48 +02:00
|
|
|
parser->setFeature ( XMLUni::fgXercesValidationErrorAsFatal, true );
|
|
|
|
parser->setFeature ( XMLUni::fgXercesLoadExternalDTD, true );
|
|
|
|
|
|
|
|
DefaultHandler handler;
|
|
|
|
MySAX2Handler mySAX2Handler;
|
|
|
|
parser->setContentHandler ( &handler );
|
|
|
|
parser->setErrorHandler ( &mySAX2Handler );
|
2009-01-17 23:52:54 +01:00
|
|
|
//parser->setEntityResolver ( &handler );
|
|
|
|
parser->setEntityResolver ( catalogResolver );
|
2008-07-09 00:54:48 +02:00
|
|
|
|
|
|
|
XMLByte* xmlBuffer = (XMLByte*) buffer;
|
|
|
|
MemBufInputSource source (
|
|
|
|
xmlBuffer,
|
|
|
|
len,
|
|
|
|
system );
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
parser->parse ( source );
|
|
|
|
}
|
|
|
|
catch ( XMLException& e )
|
|
|
|
{
|
|
|
|
delete parser;
|
|
|
|
lastError = "";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
catch ( SAXParseException& e )
|
|
|
|
{
|
|
|
|
delete parser;
|
|
|
|
char *err = XMLString::transcode ( e.getMessage() );
|
|
|
|
std::stringstream ss;
|
|
|
|
ss << "Ln " << e.getLineNumber() << " Col " << e.getColumnNumber() << ": " << err;
|
|
|
|
lastError = ss.str();
|
|
|
|
errorPosition = std::make_pair ( e.getLineNumber(), e.getColumnNumber() );
|
|
|
|
XMLString::release ( &err );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
catch ( ... )
|
|
|
|
{
|
|
|
|
delete parser;
|
|
|
|
lastError = "";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
delete parser;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2007-09-07 23:17:30 +02:00
|
|
|
std::string WrapXerces::getLastError()
|
|
|
|
{
|
2008-07-09 00:54:48 +02:00
|
|
|
char *rawError, *it;
|
|
|
|
rawError = (char *)lastError.c_str();
|
|
|
|
it = strstr ( rawError, "Message:" );
|
|
|
|
if ( it )
|
|
|
|
{
|
|
|
|
lastError = it + 8;
|
|
|
|
}
|
|
|
|
|
2008-01-06 23:13:01 +01:00
|
|
|
return lastError;
|
2007-09-07 23:17:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
std::pair<int, int> WrapXerces::getErrorPosition()
|
|
|
|
{
|
2008-01-06 23:13:01 +01:00
|
|
|
return errorPosition;
|
2007-09-07 23:17:30 +02:00
|
|
|
}
|