2009-11-06 18:06:12 +01:00
|
|
|
/*
|
|
|
|
* Copyright 2005-2007 Gerald Schmidt.
|
|
|
|
*
|
|
|
|
* This file is part of Xml Copy Editor.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "wrapxerces.h"
|
|
|
|
|
|
|
|
#include <xercesc/sax2/XMLReaderFactory.hpp>
|
|
|
|
#include <xercesc/sax2/SAX2XMLReader.hpp>
|
|
|
|
#include <xercesc/sax2/DefaultHandler.hpp>
|
|
|
|
#include <xercesc/util/XMLUni.hpp>
|
|
|
|
#include <xercesc/framework/MemBufInputSource.hpp>
|
|
|
|
#include <sstream>
|
|
|
|
#include <utility>
|
|
|
|
#include <stdexcept>
|
|
|
|
|
|
|
|
using namespace xercesc;
|
|
|
|
|
2012-08-15 14:45:24 +02:00
|
|
|
void WrapXerces::Init() throw()
|
|
|
|
{
|
|
|
|
static class Initializer
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Initializer()
|
|
|
|
{
|
|
|
|
XMLPlatformUtils::Initialize();
|
|
|
|
}
|
|
|
|
~Initializer()
|
|
|
|
{
|
|
|
|
XMLPlatformUtils::Terminate();
|
|
|
|
}
|
|
|
|
} dummy;
|
|
|
|
}
|
|
|
|
|
2012-08-30 16:37:01 +02:00
|
|
|
WrapXerces::WrapXerces()
|
2009-11-06 18:06:12 +01:00
|
|
|
{
|
2012-08-15 14:45:24 +02:00
|
|
|
WrapXerces::Init();
|
|
|
|
|
2009-11-06 18:06:12 +01:00
|
|
|
errorPosition = std::make_pair ( 1, 1 );
|
2012-08-30 16:37:01 +02:00
|
|
|
catalogResolver = new XercesCatalogResolver();
|
2009-11-06 18:06:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
WrapXerces::~WrapXerces()
|
|
|
|
{
|
|
|
|
delete catalogResolver;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool WrapXerces::validate ( const std::string& fileName )
|
|
|
|
{
|
|
|
|
SAX2XMLReader *parser = XMLReaderFactory::createXMLReader();
|
|
|
|
|
|
|
|
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 );
|
|
|
|
parser->setFeature ( XMLUni::fgXercesLoadExternalDTD, true );
|
|
|
|
|
|
|
|
|
|
|
|
DefaultHandler handler;
|
|
|
|
MySAX2Handler mySAX2Handler;
|
|
|
|
parser->setContentHandler ( &handler );
|
|
|
|
parser->setErrorHandler ( &mySAX2Handler );
|
|
|
|
|
|
|
|
//DefaultHandler handler;
|
|
|
|
//parser->setEntityResolver ( &handler );
|
|
|
|
parser->setEntityResolver ( catalogResolver );
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
parser->parse ( fileName.c_str() );
|
|
|
|
}
|
|
|
|
catch ( XMLException& e )
|
|
|
|
{
|
|
|
|
delete parser;
|
2012-08-10 15:34:13 +02:00
|
|
|
lastError = toString ( e.getMessage() );
|
2009-11-06 18:06:12 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
catch ( SAXParseException& e )
|
|
|
|
{
|
|
|
|
delete parser;
|
2012-08-10 15:34:13 +02:00
|
|
|
lastError << _T ( "Validation stopped at line " )
|
|
|
|
<< e.getLineNumber() << _T ( ", column " ) << e.getColumnNumber()
|
2012-08-18 17:41:27 +02:00
|
|
|
<< _T ( ": " ) << toString ( e.getMessage() );
|
2009-11-06 18:06:12 +01:00
|
|
|
errorPosition = std::make_pair ( e.getLineNumber(), e.getColumnNumber() );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
catch ( ... )
|
|
|
|
{
|
|
|
|
delete parser;
|
2012-08-10 15:34:13 +02:00
|
|
|
lastError = _T ( "Unexpected validation error" );
|
2009-11-06 18:06:12 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
delete parser;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// tbd: cache grammar
|
|
|
|
bool WrapXerces::validateMemory (
|
|
|
|
const char *buffer,
|
2012-08-27 14:25:08 +02:00
|
|
|
size_t len,
|
2009-11-06 18:06:12 +01:00
|
|
|
const char *system,
|
2012-08-27 14:25:08 +02:00
|
|
|
wxThread *thread /*= NULL*/ )
|
2009-11-06 18:06:12 +01:00
|
|
|
{
|
2012-08-15 14:45:24 +02:00
|
|
|
std::auto_ptr<SAX2XMLReader> parser ( XMLReaderFactory::createXMLReader() );
|
2009-11-06 18:06:12 +01:00
|
|
|
|
|
|
|
parser->setFeature ( XMLUni::fgSAX2CoreNameSpaces, true );
|
|
|
|
parser->setFeature ( XMLUni::fgSAX2CoreValidation, true );
|
|
|
|
parser->setFeature ( XMLUni::fgXercesDynamic, true );
|
|
|
|
parser->setFeature ( XMLUni::fgXercesSchema, true );
|
|
|
|
//parser->setFeature ( XMLUni::fgXercesSchemaFullChecking, true );
|
|
|
|
parser->setFeature ( XMLUni::fgXercesValidationErrorAsFatal, true );
|
|
|
|
parser->setFeature ( XMLUni::fgXercesLoadExternalDTD, true );
|
2012-08-27 14:25:08 +02:00
|
|
|
|
2009-11-06 18:06:12 +01:00
|
|
|
DefaultHandler handler;
|
|
|
|
MySAX2Handler mySAX2Handler;
|
|
|
|
parser->setContentHandler ( &handler );
|
|
|
|
parser->setErrorHandler ( &mySAX2Handler );
|
|
|
|
//parser->setEntityResolver ( &handler );
|
|
|
|
parser->setEntityResolver ( catalogResolver );
|
|
|
|
|
|
|
|
XMLByte* xmlBuffer = (XMLByte*) buffer;
|
2012-08-15 14:45:24 +02:00
|
|
|
MemBufInputSource source ( xmlBuffer, len, system );
|
|
|
|
|
2009-11-06 18:06:12 +01:00
|
|
|
try
|
|
|
|
{
|
2012-08-27 14:25:08 +02:00
|
|
|
if ( thread == NULL )
|
|
|
|
{
|
|
|
|
parser->parse ( source );
|
|
|
|
}
|
|
|
|
else if ( !thread->TestDestroy() )
|
|
|
|
{
|
|
|
|
XMLPScanToken token;
|
|
|
|
if ( parser->parseFirst ( source, token ) )
|
|
|
|
while ( (!thread->TestDestroy()) && parser->parseNext ( token ) )
|
|
|
|
continue;
|
|
|
|
}
|
2009-11-06 18:06:12 +01:00
|
|
|
}
|
|
|
|
catch ( XMLException& e )
|
|
|
|
{
|
2012-08-10 15:34:13 +02:00
|
|
|
lastError = wxEmptyString;
|
2009-11-06 18:06:12 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
catch ( SAXParseException& e )
|
|
|
|
{
|
2012-08-10 15:34:13 +02:00
|
|
|
lastError << _T ( "Ln " ) << e.getLineNumber() << _T ( " Col " )
|
|
|
|
<< e.getColumnNumber() << _T ( ": " ) << toString ( e.getMessage() );
|
2009-11-06 18:06:12 +01:00
|
|
|
errorPosition = std::make_pair ( e.getLineNumber(), e.getColumnNumber() );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
catch ( ... )
|
|
|
|
{
|
2012-08-30 16:37:01 +02:00
|
|
|
if ( thread != NULL && thread->TestDestroy() )
|
|
|
|
throw;
|
2012-08-10 15:34:13 +02:00
|
|
|
lastError = wxEmptyString;
|
2009-11-06 18:06:12 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-08-10 15:34:13 +02:00
|
|
|
const wxString &WrapXerces::getLastError()
|
2009-11-06 18:06:12 +01:00
|
|
|
{
|
2012-08-10 15:34:13 +02:00
|
|
|
int i = lastError.Find( _T ( "Message:" ) );
|
|
|
|
if ( i != wxNOT_FOUND )
|
2009-11-06 18:06:12 +01:00
|
|
|
{
|
2012-08-10 15:34:13 +02:00
|
|
|
lastError = lastError.substr( i );
|
2009-11-06 18:06:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return lastError;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::pair<int, int> WrapXerces::getErrorPosition()
|
|
|
|
{
|
|
|
|
return errorPosition;
|
|
|
|
}
|
2012-08-10 15:34:13 +02:00
|
|
|
|
|
|
|
wxString WrapXerces::toString ( const XMLCh *str )
|
|
|
|
{
|
|
|
|
static wxMBConvUTF16 conv;
|
|
|
|
|
|
|
|
return wxString ( ( const char * ) str, conv );
|
|
|
|
}
|