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 "wraplibxml.h"
|
|
|
|
#include <sstream>
|
|
|
|
#include <stdexcept>
|
|
|
|
|
|
|
|
#ifdef ATTRIBUTE_PRINTF
|
|
|
|
#undef ATTRIBUTE_PRINTF
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <wx/wx.h>
|
2012-08-30 16:37:01 +02:00
|
|
|
#include <wx/filesys.h>
|
|
|
|
#include <wx/uri.h>
|
2009-11-06 18:06:12 +01:00
|
|
|
|
2012-08-30 16:37:01 +02:00
|
|
|
class Initializer
|
2012-08-15 14:45:24 +02:00
|
|
|
{
|
2012-08-30 16:37:01 +02:00
|
|
|
public:
|
2013-10-19 02:44:17 +02:00
|
|
|
Initializer ( const wxString &catalogPath ) throw ()
|
2012-08-15 14:45:24 +02:00
|
|
|
{
|
2012-08-30 16:37:01 +02:00
|
|
|
xmlSetGenericErrorFunc ( xmlGenericErrorContext,
|
|
|
|
&Initializer::OnXmlGenericError );
|
2012-08-15 14:45:24 +02:00
|
|
|
|
2012-08-30 16:37:01 +02:00
|
|
|
LIBXML_TEST_VERSION
|
2012-08-15 14:45:24 +02:00
|
|
|
|
2012-08-30 16:37:01 +02:00
|
|
|
xmlInitializeCatalog();
|
2013-10-19 02:44:17 +02:00
|
|
|
xmlLoadCatalog ( catalogPath.mb_str() );
|
2012-08-30 16:37:01 +02:00
|
|
|
|
|
|
|
initGenericErrorDefaultFunc ( NULL );
|
|
|
|
}
|
|
|
|
|
|
|
|
~Initializer ()
|
|
|
|
{
|
|
|
|
xsltCleanupGlobals();
|
|
|
|
xmlCatalogCleanup();
|
|
|
|
xmlCleanupParser();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void XMLCDECL OnXmlGenericError (void *ctx, const char *msg, ...) throw()
|
|
|
|
{
|
|
|
|
va_list args;
|
2012-08-15 14:45:24 +02:00
|
|
|
|
2012-08-30 16:37:01 +02:00
|
|
|
size_t size = 128;
|
|
|
|
std::string buffer;
|
|
|
|
int chars;
|
|
|
|
for (;;)
|
2012-08-15 14:45:24 +02:00
|
|
|
{
|
2012-08-30 16:37:01 +02:00
|
|
|
buffer.resize ( size );
|
|
|
|
if ( buffer.size() < size )
|
|
|
|
throw std::runtime_error ( "Out of memory" );
|
2012-08-15 14:45:24 +02:00
|
|
|
|
2012-08-30 16:37:01 +02:00
|
|
|
va_start(args, msg);
|
|
|
|
chars = vsnprintf( (char *) buffer.c_str(), size, msg, args);
|
|
|
|
va_end(args);
|
|
|
|
|
|
|
|
if ( chars >= 0 && ( size_t ) chars < size )
|
2012-08-15 14:45:24 +02:00
|
|
|
{
|
2012-08-30 16:37:01 +02:00
|
|
|
buffer.resize ( chars );
|
|
|
|
throw std::runtime_error ( buffer );
|
2012-08-15 14:45:24 +02:00
|
|
|
}
|
2012-08-30 16:37:01 +02:00
|
|
|
if ( chars >= 0 )
|
|
|
|
size = chars + 1;
|
|
|
|
else
|
|
|
|
throw std::runtime_error (
|
|
|
|
std::string ( "Can't format message: " ) + msg );
|
2012-08-15 14:45:24 +02:00
|
|
|
}
|
2012-08-30 16:37:01 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-10-19 02:44:17 +02:00
|
|
|
void WrapLibxml::Init ( const wxString &catalogPath ) throw()
|
2012-08-30 16:37:01 +02:00
|
|
|
{
|
|
|
|
static Initializer dummy ( catalogPath );
|
2012-08-15 14:45:24 +02:00
|
|
|
}
|
|
|
|
|
2012-08-30 16:37:01 +02:00
|
|
|
WrapLibxml::WrapLibxml ( bool netAccessParameter )
|
|
|
|
: netAccess ( netAccessParameter )
|
2009-11-06 18:06:12 +01:00
|
|
|
{
|
2012-08-15 14:45:24 +02:00
|
|
|
WrapLibxml::Init();
|
2009-11-06 18:06:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
WrapLibxml::~WrapLibxml()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool WrapLibxml::validate ( const std::string& fileName )
|
|
|
|
{
|
|
|
|
output = "";
|
|
|
|
|
|
|
|
xmlParserCtxtPtr ctxt;
|
|
|
|
xmlDocPtr docPtr;
|
|
|
|
|
|
|
|
ctxt = xmlNewParserCtxt();
|
|
|
|
if ( ctxt == NULL )
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool returnValue = false;
|
|
|
|
|
|
|
|
docPtr = xmlCtxtReadFile (
|
|
|
|
ctxt,
|
|
|
|
fileName.c_str(),
|
|
|
|
NULL,
|
|
|
|
( netAccess ) ? XML_PARSE_DTDVALID : XML_PARSE_DTDVALID | XML_PARSE_NONET );
|
|
|
|
if ( docPtr == NULL )
|
|
|
|
;
|
|
|
|
else if ( ctxt->valid == 0 )
|
|
|
|
;
|
|
|
|
else
|
|
|
|
returnValue = true;
|
|
|
|
|
|
|
|
xmlFreeDoc ( docPtr );
|
|
|
|
xmlFreeParserCtxt ( ctxt );
|
|
|
|
|
|
|
|
return returnValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool WrapLibxml::validateRelaxNG (
|
|
|
|
const std::string& schemaFileName,
|
|
|
|
const std::string& fileName )
|
|
|
|
{
|
|
|
|
output = "";
|
|
|
|
|
|
|
|
xmlRelaxNGValidCtxtPtr ctxtPtr;
|
|
|
|
xmlDocPtr docPtr;
|
|
|
|
xmlRelaxNGParserCtxtPtr schemaParserCtxtPtr;
|
|
|
|
xmlRelaxNGPtr schemaPtr;
|
|
|
|
|
|
|
|
schemaParserCtxtPtr = xmlRelaxNGNewParserCtxt ( schemaFileName.c_str() );
|
|
|
|
if ( schemaParserCtxtPtr == NULL )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
schemaPtr = xmlRelaxNGParse ( schemaParserCtxtPtr );
|
|
|
|
if ( schemaPtr == NULL )
|
|
|
|
{
|
|
|
|
xmlRelaxNGFreeParserCtxt ( schemaParserCtxtPtr );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
ctxtPtr = xmlRelaxNGNewValidCtxt ( schemaPtr );
|
|
|
|
if ( ctxtPtr == NULL )
|
|
|
|
{
|
|
|
|
xmlRelaxNGFree ( schemaPtr );
|
|
|
|
xmlRelaxNGFreeParserCtxt ( schemaParserCtxtPtr );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
docPtr = xmlReadFile ( fileName.c_str(), NULL,
|
|
|
|
( netAccess ) ? XML_PARSE_DTDLOAD : XML_PARSE_DTDLOAD | XML_PARSE_NONET );
|
|
|
|
if ( docPtr == NULL )
|
|
|
|
{
|
|
|
|
xmlRelaxNGFree ( schemaPtr );
|
|
|
|
xmlRelaxNGFreeValidCtxt ( ctxtPtr );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
int res = xmlRelaxNGValidateDoc ( ctxtPtr, docPtr );
|
|
|
|
|
|
|
|
bool returnValue = ( res ) ? false : true;
|
|
|
|
|
|
|
|
xmlFreeDoc ( docPtr );
|
|
|
|
xmlRelaxNGFree ( schemaPtr );
|
|
|
|
xmlRelaxNGFreeValidCtxt ( ctxtPtr );
|
|
|
|
return returnValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool WrapLibxml::validateW3CSchema (
|
|
|
|
const std::string& schemaFileName,
|
|
|
|
const std::string& fileName )
|
|
|
|
{
|
|
|
|
output = "";
|
|
|
|
|
|
|
|
xmlSchemaValidCtxtPtr ctxtPtr;
|
|
|
|
xmlDocPtr docPtr;
|
|
|
|
xmlSchemaParserCtxtPtr schemaParserCtxtPtr;
|
|
|
|
xmlSchemaPtr schemaPtr;
|
|
|
|
|
|
|
|
schemaParserCtxtPtr = xmlSchemaNewParserCtxt ( schemaFileName.c_str() );
|
|
|
|
if ( schemaParserCtxtPtr == NULL )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
schemaPtr = xmlSchemaParse ( schemaParserCtxtPtr );
|
|
|
|
if ( schemaPtr == NULL )
|
|
|
|
{
|
|
|
|
xmlSchemaFreeParserCtxt ( schemaParserCtxtPtr );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
ctxtPtr = xmlSchemaNewValidCtxt ( schemaPtr );
|
|
|
|
if ( ctxtPtr == NULL )
|
|
|
|
{
|
|
|
|
xmlSchemaFree ( schemaPtr );
|
|
|
|
xmlSchemaFreeParserCtxt ( schemaParserCtxtPtr );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
docPtr = xmlReadFile ( fileName.c_str(), NULL,
|
|
|
|
( netAccess ) ? XML_PARSE_DTDLOAD : XML_PARSE_DTDLOAD | XML_PARSE_NONET );
|
|
|
|
if ( docPtr == NULL )
|
|
|
|
{
|
|
|
|
xmlSchemaFree ( schemaPtr );
|
|
|
|
xmlSchemaFreeValidCtxt ( ctxtPtr );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
int res = xmlSchemaValidateDoc ( ctxtPtr, docPtr );
|
|
|
|
|
|
|
|
bool returnValue = ( res ) ? false : true;
|
|
|
|
|
|
|
|
xmlFreeDoc ( docPtr );
|
|
|
|
xmlSchemaFree ( schemaPtr );
|
|
|
|
xmlSchemaFreeValidCtxt ( ctxtPtr );
|
|
|
|
return returnValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool WrapLibxml::parse (
|
|
|
|
const std::string& fileName,
|
|
|
|
bool indent,
|
|
|
|
bool resolveEntities )
|
|
|
|
{
|
|
|
|
output = "";
|
|
|
|
|
|
|
|
xmlParserCtxtPtr ctxt;
|
|
|
|
xmlDocPtr docPtr;
|
|
|
|
|
|
|
|
ctxt = xmlNewParserCtxt();
|
|
|
|
if ( ctxt == NULL )
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
int flags = XML_PARSE_DTDLOAD;
|
|
|
|
if ( resolveEntities )
|
|
|
|
flags |= XML_PARSE_NOENT;
|
|
|
|
if ( !netAccess )
|
|
|
|
flags |= XML_PARSE_NONET;
|
|
|
|
|
|
|
|
docPtr = xmlCtxtReadFile (
|
|
|
|
ctxt,
|
|
|
|
fileName.c_str(),
|
|
|
|
NULL,
|
|
|
|
flags );
|
|
|
|
|
|
|
|
if ( docPtr == NULL )
|
|
|
|
{
|
|
|
|
xmlFreeParserCtxt ( ctxt );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-08-10 14:42:59 +02:00
|
|
|
xmlKeepBlanksDefault ( indent ? 0 : 1 );
|
2009-11-06 18:06:12 +01:00
|
|
|
|
|
|
|
xmlChar *buf = NULL;
|
|
|
|
int size;
|
|
|
|
|
|
|
|
// tbd: link output encoding to input encoding?
|
|
|
|
xmlDocDumpFormatMemoryEnc (
|
|
|
|
docPtr,
|
|
|
|
&buf,
|
|
|
|
&size,
|
|
|
|
"UTF-8",
|
|
|
|
indent );
|
|
|
|
|
|
|
|
if ( buf )
|
|
|
|
{
|
|
|
|
output.append ( ( const char * ) buf );
|
|
|
|
free ( buf );
|
|
|
|
}
|
|
|
|
|
|
|
|
bool returnValue = ( !ctxt->errNo ) ? true : false;
|
|
|
|
|
|
|
|
xmlFreeDoc ( docPtr );
|
|
|
|
xmlFreeParserCtxt ( ctxt );
|
|
|
|
|
|
|
|
return returnValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool WrapLibxml::xpath ( const std::string& path, const std::string& fileName )
|
|
|
|
{
|
|
|
|
output = "";
|
|
|
|
|
|
|
|
xmlParserCtxtPtr ctxt;
|
|
|
|
xmlDocPtr docPtr;
|
|
|
|
|
2012-08-10 14:42:59 +02:00
|
|
|
xmlKeepBlanksDefault ( 0 );
|
|
|
|
|
2009-11-06 18:06:12 +01:00
|
|
|
ctxt = xmlNewParserCtxt();
|
|
|
|
if ( ctxt == NULL )
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
docPtr = xmlCtxtReadFile (
|
|
|
|
ctxt,
|
|
|
|
fileName.c_str(),
|
|
|
|
NULL,
|
|
|
|
//(netAccess) ? XML_PARSE_DTDLOAD | XML_PARSE_NOENT : XML_PARSE_DTDLOAD | XML_PARSE_NONET | XML_PARSE_NOENT
|
|
|
|
XML_PARSE_NOENT | XML_PARSE_NONET | XML_PARSE_NSCLEAN
|
|
|
|
);
|
|
|
|
|
|
|
|
if ( docPtr == NULL )
|
|
|
|
{
|
|
|
|
xmlFreeParserCtxt ( ctxt );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
xmlXPathContextPtr context;
|
|
|
|
xmlXPathObjectPtr result;
|
|
|
|
xmlNodeSetPtr nodeset;
|
|
|
|
|
|
|
|
context = xmlXPathNewContext ( docPtr );
|
|
|
|
if ( !context )
|
|
|
|
{
|
|
|
|
xmlFreeDoc ( docPtr );
|
|
|
|
xmlFreeParserCtxt ( ctxt );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// enable namespace prefixes
|
|
|
|
xmlXPathRegisterNs ( context, ( xmlChar * ) "xhtml", ( xmlChar * ) "http://www.w3.org/1999/xhtml" );
|
|
|
|
// add others as necessary!
|
|
|
|
|
|
|
|
result = xmlXPathEvalExpression ( ( const xmlChar * ) path.c_str(), context );
|
|
|
|
|
|
|
|
bool xpathIsValid = ( result ) ? true : false;
|
|
|
|
|
|
|
|
while ( result != NULL )
|
|
|
|
{
|
|
|
|
if ( xmlXPathNodeSetIsEmpty ( result->nodesetval ) )
|
|
|
|
break;
|
2012-08-10 14:42:59 +02:00
|
|
|
xmlBufferPtr bufferPtr = xmlBufferCreate();
|
|
|
|
if ( bufferPtr == NULL )
|
|
|
|
break;
|
2009-11-06 18:06:12 +01:00
|
|
|
nodeset = result->nodesetval;
|
|
|
|
for ( int i = 0; i < nodeset->nodeNr; i++ )
|
|
|
|
{
|
|
|
|
xmlNodePtr node = nodeset->nodeTab[i];
|
2012-08-10 14:42:59 +02:00
|
|
|
if ( !node )
|
2009-11-06 18:06:12 +01:00
|
|
|
break;
|
|
|
|
xmlNodeDump ( bufferPtr, NULL, node, 0, 1 );
|
|
|
|
|
2012-08-10 14:42:59 +02:00
|
|
|
output += ( const char * ) xmlBufferContent ( bufferPtr );
|
2009-11-06 18:06:12 +01:00
|
|
|
output += '\n';
|
2013-07-20 20:07:39 +02:00
|
|
|
xmlBufferEmpty ( bufferPtr );
|
2009-11-06 18:06:12 +01:00
|
|
|
}
|
2012-08-10 14:42:59 +02:00
|
|
|
xmlBufferFree ( bufferPtr );
|
2009-11-06 18:06:12 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if ( result )
|
|
|
|
xmlXPathFreeObject ( result );
|
|
|
|
xmlXPathFreeContext ( context );
|
|
|
|
xmlFreeDoc ( docPtr );
|
|
|
|
xmlFreeParserCtxt ( ctxt );
|
|
|
|
|
|
|
|
return xpathIsValid;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool WrapLibxml::xslt (
|
|
|
|
const std::string& styleFileName,
|
|
|
|
const std::string& fileName
|
|
|
|
)
|
|
|
|
{
|
|
|
|
output = "";
|
|
|
|
|
|
|
|
xsltStylesheetPtr cur;
|
|
|
|
xmlDocPtr doc, res;
|
|
|
|
xmlSubstituteEntitiesDefault ( 1 );
|
|
|
|
xmlLoadExtDtdDefaultValue = 1;
|
|
|
|
cur = xsltParseStylesheetFile ( ( const xmlChar * ) styleFileName.c_str() );
|
|
|
|
if ( !cur )
|
|
|
|
{
|
|
|
|
nonParserError = "Cannot parse stylesheet";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
doc = xmlParseFile ( fileName.c_str() );
|
|
|
|
if ( !doc )
|
|
|
|
{
|
|
|
|
nonParserError = "Cannot parse file";
|
|
|
|
xsltFreeStylesheet ( cur );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ensure entity warnings are treated as errors
|
|
|
|
if ( !getLastError().empty() )
|
|
|
|
{
|
|
|
|
xmlFreeDoc ( doc );
|
|
|
|
xsltFreeStylesheet ( cur );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
res = xsltApplyStylesheet ( cur, doc, NULL );
|
|
|
|
if ( !res )
|
|
|
|
{
|
|
|
|
nonParserError = "Cannot apply stylesheet";
|
|
|
|
xmlFreeDoc ( doc );
|
|
|
|
xsltFreeStylesheet ( cur );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
xmlChar *buf = NULL;
|
|
|
|
int size;
|
|
|
|
xmlDocDumpFormatMemoryEnc ( res, &buf, &size, "UTF-8", 1 );
|
|
|
|
if ( buf )
|
|
|
|
{
|
|
|
|
output.append ( ( char * ) buf, size );
|
2012-08-12 05:17:00 +02:00
|
|
|
xmlFree ( buf );
|
2009-11-06 18:06:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
xsltFreeStylesheet ( cur );
|
|
|
|
xmlFreeDoc ( doc );
|
|
|
|
xmlFreeDoc ( res );
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool WrapLibxml::bufferWellFormed ( const std::string& buffer )
|
|
|
|
{
|
|
|
|
xmlParserCtxtPtr ctxt = xmlNewParserCtxt();
|
|
|
|
if ( !ctxt )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
xmlDocPtr docPtr = xmlCtxtReadMemory (
|
|
|
|
ctxt,
|
|
|
|
buffer.c_str(),
|
|
|
|
buffer.size(),
|
|
|
|
"",
|
|
|
|
"UTF-8",
|
2012-09-05 14:38:34 +02:00
|
|
|
( netAccess ) ? XML_PARSE_DTDLOAD : XML_PARSE_DTDLOAD | XML_PARSE_NONET );
|
2009-11-06 18:06:12 +01:00
|
|
|
bool returnValue = ( docPtr ) ? true : false;
|
|
|
|
|
|
|
|
xmlFreeDoc ( docPtr );
|
|
|
|
xmlFreeParserCtxt ( ctxt );
|
|
|
|
|
|
|
|
return returnValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
int WrapLibxml::saveEncoding (
|
|
|
|
const std::string& buffer,
|
|
|
|
const std::string& fileName,
|
|
|
|
const std::string& encoding )
|
|
|
|
{
|
|
|
|
xmlParserCtxtPtr ctxt = xmlNewParserCtxt();
|
|
|
|
if ( !ctxt )
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
xmlSubstituteEntitiesDefault ( 0 );
|
|
|
|
|
|
|
|
xmlKeepBlanksDefault ( 1 ); // prevents single-line output
|
|
|
|
|
|
|
|
xmlDocPtr docPtr = xmlCtxtReadMemory (
|
|
|
|
ctxt,
|
|
|
|
buffer.c_str(),
|
|
|
|
buffer.size(),
|
|
|
|
"",
|
|
|
|
NULL,
|
|
|
|
XML_PARSE_DTDLOAD | XML_PARSE_DTDVALID | XML_PARSE_PEDANTIC//XML_PARSE_NONET//XML_PARSE_DTDLOAD//0//(netAccess) ? XML_PARSE_DTDLOAD | XML_PARSE_NOENT : XML_PARSE_DTDLOAD | XML_PARSE_NONET | XML_PARSE_NOENT//0
|
|
|
|
);
|
|
|
|
if ( !docPtr )
|
|
|
|
{
|
|
|
|
xmlFreeParserCtxt ( ctxt );
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int result = xmlSaveFileEnc (
|
|
|
|
fileName.c_str(),
|
|
|
|
docPtr,
|
|
|
|
encoding.c_str() );
|
|
|
|
xmlFreeDoc ( docPtr );
|
|
|
|
xmlFreeParserCtxt ( ctxt );
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
int WrapLibxml::saveEncodingFromFile (
|
|
|
|
const std::string& fileNameSource,
|
|
|
|
const std::string& fileNameDestination,
|
|
|
|
const std::string& encoding )
|
|
|
|
{
|
|
|
|
xmlParserCtxtPtr ctxt = xmlNewParserCtxt();
|
|
|
|
if ( !ctxt )
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
xmlSubstituteEntitiesDefault ( 0 );
|
|
|
|
xmlKeepBlanksDefault ( 1 ); // prevents single-line output
|
|
|
|
|
|
|
|
xmlDocPtr docPtr = xmlCtxtReadFile (
|
|
|
|
ctxt,
|
|
|
|
fileNameSource.c_str(),
|
|
|
|
"UTF-8",
|
|
|
|
( netAccess ) ? XML_PARSE_DTDLOAD : XML_PARSE_DTDLOAD | XML_PARSE_NONET );//XML_PARSE_NONET//XML_PARSE_DTDLOAD//0//(netAccess) ? XML_PARSE_DTDLOAD | XML_PARSE_NOENT : XML_PARSE_DTDLOAD | XML_PARSE_NONET | XML_PARSE_NOENT//0
|
|
|
|
if ( !docPtr )
|
|
|
|
{
|
|
|
|
xmlFreeParserCtxt ( ctxt );
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int result = xmlSaveFileEnc (
|
|
|
|
fileNameDestination.c_str(),
|
|
|
|
docPtr,
|
|
|
|
encoding.c_str() );
|
|
|
|
|
|
|
|
// ensure entity warnings are treated as errors!
|
|
|
|
if ( !getLastError().empty() )
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
xmlFreeDoc ( docPtr );
|
|
|
|
xmlFreeParserCtxt ( ctxt );
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string WrapLibxml::getLastError()
|
|
|
|
{
|
|
|
|
xmlErrorPtr err = xmlGetLastError();
|
|
|
|
|
|
|
|
if ( !err )
|
|
|
|
{
|
|
|
|
return ( nonParserError.empty() ) ? "" : nonParserError;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::stringstream ss;
|
|
|
|
ss << "Error at line ";
|
|
|
|
ss << err->line;
|
|
|
|
if ( err->int2 )
|
|
|
|
{
|
|
|
|
ss << ", column ";
|
|
|
|
ss << err->int2;
|
|
|
|
}
|
|
|
|
ss << ": ";
|
|
|
|
ss << err->message;
|
|
|
|
return ss.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::pair<int, int> WrapLibxml::getErrorPosition()
|
|
|
|
{
|
|
|
|
xmlErrorPtr err = xmlGetLastError();
|
|
|
|
if ( !err )
|
|
|
|
return std::make_pair ( 1, 1 );
|
|
|
|
|
|
|
|
return std::make_pair (
|
|
|
|
err->line,
|
|
|
|
err->int2 );
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string WrapLibxml::getOutput()
|
|
|
|
{
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string WrapLibxml::lookupPublicId ( const std::string& id )
|
|
|
|
{
|
|
|
|
std::string ret;
|
|
|
|
char *s = ( char * ) xmlCatalogResolvePublic ( ( const xmlChar * ) id.c_str() );
|
2012-08-30 16:37:01 +02:00
|
|
|
if ( s == NULL )
|
2009-11-06 18:06:12 +01:00
|
|
|
return ret;
|
|
|
|
|
2012-08-30 16:37:01 +02:00
|
|
|
wxString url ( s, wxConvUTF8 );
|
|
|
|
wxFileName file = wxFileSystem::URLToFileName ( url );
|
|
|
|
if ( file.IsFileReadable() )
|
|
|
|
ret = file.GetFullPath().mb_str ( wxConvLocal );
|
|
|
|
else
|
|
|
|
ret = s;
|
2009-11-06 18:06:12 +01:00
|
|
|
|
2012-08-30 16:37:01 +02:00
|
|
|
xmlFree ( s );
|
2009-11-06 18:06:12 +01:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|