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 "wraplibxml.h"
|
|
|
|
#include <sstream>
|
|
|
|
#include <stdexcept>
|
|
|
|
#include <wx/wx.h>
|
|
|
|
|
2007-09-08 00:25:30 +02:00
|
|
|
WrapLibxml::WrapLibxml ( bool netAccessParameter, const std::string& catalogPathParameter )
|
2008-01-06 23:13:01 +01:00
|
|
|
: netAccess ( netAccessParameter ), catalogPath ( catalogPathParameter )
|
2007-09-07 23:17:30 +02:00
|
|
|
{
|
2008-01-06 23:13:01 +01:00
|
|
|
LIBXML_TEST_VERSION
|
|
|
|
xmlInitializeCatalog();
|
2007-09-07 23:17:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
WrapLibxml::~WrapLibxml()
|
|
|
|
{
|
2008-01-06 23:13:01 +01:00
|
|
|
xsltCleanupGlobals();
|
|
|
|
xmlCleanupParser();
|
|
|
|
xmlCatalogCleanup();
|
2007-09-07 23:17:30 +02:00
|
|
|
}
|
|
|
|
|
2007-09-08 00:25:30 +02:00
|
|
|
bool WrapLibxml::validate ( const std::string& fileName )
|
2007-09-07 23:17:30 +02:00
|
|
|
{
|
2008-01-06 23:13:01 +01:00
|
|
|
output = "";
|
|
|
|
|
|
|
|
xmlCleanupParser();
|
|
|
|
xmlParserCtxtPtr ctxt;
|
|
|
|
xmlDocPtr docPtr;
|
|
|
|
|
|
|
|
ctxt = xmlNewParserCtxt();
|
|
|
|
if ( ctxt == NULL )
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool returnValue = false;
|
|
|
|
|
|
|
|
loadCatalog();
|
|
|
|
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;
|
2007-09-07 23:17:30 +02:00
|
|
|
}
|
|
|
|
|
2007-09-08 00:25:30 +02:00
|
|
|
bool WrapLibxml::validateRelaxNG (
|
|
|
|
const std::string& schemaFileName,
|
|
|
|
const std::string& fileName )
|
2007-09-07 23:17:30 +02:00
|
|
|
{
|
2008-01-06 23:13:01 +01:00
|
|
|
output = "";
|
|
|
|
|
|
|
|
xmlCleanupParser();
|
|
|
|
|
|
|
|
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;
|
2007-09-07 23:17:30 +02:00
|
|
|
}
|
|
|
|
|
2007-09-08 00:25:30 +02:00
|
|
|
bool WrapLibxml::validateW3CSchema (
|
|
|
|
const std::string& schemaFileName,
|
|
|
|
const std::string& fileName )
|
2007-09-07 23:17:30 +02:00
|
|
|
{
|
2008-01-06 23:13:01 +01:00
|
|
|
output = "";
|
|
|
|
|
|
|
|
xmlCleanupParser();
|
|
|
|
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;
|
2007-09-07 23:17:30 +02:00
|
|
|
}
|
|
|
|
|
2007-09-08 00:25:30 +02:00
|
|
|
bool WrapLibxml::parse (
|
|
|
|
const std::string& fileName,
|
|
|
|
bool indent,
|
|
|
|
bool resolveEntities )
|
2007-09-07 23:17:30 +02:00
|
|
|
{
|
2008-01-06 23:13:01 +01:00
|
|
|
output = "";
|
|
|
|
|
|
|
|
xmlCleanupParser();
|
|
|
|
xmlParserCtxtPtr ctxt;
|
|
|
|
xmlDocPtr docPtr;
|
|
|
|
|
|
|
|
ctxt = xmlNewParserCtxt();
|
|
|
|
if ( ctxt == NULL )
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
loadCatalog();
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( indent )
|
|
|
|
xmlKeepBlanksDefault ( 0 );
|
|
|
|
|
|
|
|
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;
|
2007-09-07 23:17:30 +02:00
|
|
|
}
|
|
|
|
|
2007-09-08 00:25:30 +02:00
|
|
|
bool WrapLibxml::xpath ( const std::string& path, const std::string& fileName )
|
2007-09-07 23:17:30 +02:00
|
|
|
{
|
2008-01-06 23:13:01 +01:00
|
|
|
output = "";
|
|
|
|
|
|
|
|
xmlCleanupParser();
|
|
|
|
|
|
|
|
xmlParserCtxtPtr ctxt;
|
|
|
|
xmlDocPtr docPtr;
|
|
|
|
|
|
|
|
ctxt = xmlNewParserCtxt();
|
|
|
|
if ( ctxt == NULL )
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
loadCatalog();
|
|
|
|
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 );
|
|
|
|
|
|
|
|
xmlKeepBlanksDefault ( 0 );
|
|
|
|
|
|
|
|
bool xpathIsValid = ( result ) ? true : false;
|
|
|
|
|
|
|
|
while ( result != NULL )
|
|
|
|
{
|
|
|
|
if ( xmlXPathNodeSetIsEmpty ( result->nodesetval ) )
|
|
|
|
break;
|
|
|
|
nodeset = result->nodesetval;
|
|
|
|
for ( int i = 0; i < nodeset->nodeNr; i++ )
|
|
|
|
{
|
|
|
|
xmlNodePtr node = nodeset->nodeTab[i];
|
|
|
|
xmlBufferPtr bufferPtr = xmlBufferCreate();
|
|
|
|
if ( !node || !bufferPtr )
|
|
|
|
break;
|
|
|
|
xmlNodeDump ( bufferPtr, NULL, node, 0, 1 );
|
|
|
|
std::string nodeBuffer = ( const char * ) xmlBufferContent ( bufferPtr );
|
|
|
|
|
|
|
|
output += nodeBuffer;
|
|
|
|
output += '\n';
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if ( result )
|
|
|
|
xmlXPathFreeObject ( result );
|
|
|
|
xmlXPathFreeContext ( context );
|
|
|
|
xmlFreeDoc ( docPtr );
|
|
|
|
xmlFreeParserCtxt ( ctxt );
|
|
|
|
|
|
|
|
xmlCleanupParser();
|
|
|
|
|
|
|
|
return xpathIsValid;
|
2007-09-07 23:17:30 +02:00
|
|
|
}
|
|
|
|
|
2007-09-08 00:25:30 +02:00
|
|
|
bool WrapLibxml::xslt (
|
|
|
|
const std::string& styleFileName,
|
|
|
|
const std::string& fileName
|
2007-09-07 23:17:30 +02:00
|
|
|
)
|
|
|
|
{
|
2008-01-06 23:13:01 +01:00
|
|
|
xmlCleanupParser();
|
|
|
|
output = "";
|
|
|
|
|
|
|
|
xsltStylesheetPtr cur;
|
|
|
|
xmlDocPtr doc, res;
|
|
|
|
xmlSubstituteEntitiesDefault ( 1 );
|
|
|
|
xmlLoadExtDtdDefaultValue = 1;
|
|
|
|
cur = xsltParseStylesheetFile ( ( const xmlChar * ) styleFileName.c_str() );
|
|
|
|
if ( !cur )
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
loadCatalog();
|
|
|
|
doc = xmlParseFile ( fileName.c_str() );
|
|
|
|
if ( !doc )
|
|
|
|
{
|
|
|
|
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 )
|
|
|
|
{
|
|
|
|
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 );
|
|
|
|
free ( buf ); // as above?
|
|
|
|
}
|
|
|
|
|
|
|
|
xsltFreeStylesheet ( cur );
|
|
|
|
xmlFreeDoc ( doc );
|
|
|
|
xmlFreeDoc ( res );
|
|
|
|
|
|
|
|
return true;
|
2007-09-07 23:17:30 +02:00
|
|
|
}
|
|
|
|
|
2007-09-08 00:25:30 +02:00
|
|
|
bool WrapLibxml::bufferWellFormed ( const std::string& buffer )
|
2007-09-07 23:17:30 +02:00
|
|
|
{
|
2008-01-06 23:13:01 +01:00
|
|
|
xmlCleanupParser();
|
|
|
|
|
|
|
|
xmlParserCtxtPtr ctxt = xmlNewParserCtxt();
|
|
|
|
if ( !ctxt )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
loadCatalog();
|
|
|
|
xmlDocPtr docPtr = xmlCtxtReadMemory (
|
|
|
|
ctxt,
|
|
|
|
buffer.c_str(),
|
|
|
|
buffer.size(),
|
|
|
|
"",
|
|
|
|
"UTF-8",
|
|
|
|
0 );
|
|
|
|
bool returnValue = ( docPtr ) ? true : false;
|
|
|
|
|
|
|
|
xmlFreeDoc ( docPtr );
|
|
|
|
xmlFreeParserCtxt ( ctxt );
|
|
|
|
|
|
|
|
return returnValue;
|
2007-09-07 23:17:30 +02:00
|
|
|
}
|
|
|
|
|
2007-09-08 00:25:30 +02:00
|
|
|
int WrapLibxml::saveEncoding (
|
|
|
|
const std::string& buffer,
|
|
|
|
const std::string& fileName,
|
|
|
|
const std::string& encoding )
|
2007-09-07 23:17:30 +02:00
|
|
|
{
|
2008-01-06 23:13:01 +01:00
|
|
|
xmlCleanupParser();
|
|
|
|
|
|
|
|
xmlParserCtxtPtr ctxt = xmlNewParserCtxt();
|
|
|
|
if ( !ctxt )
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
xmlSubstituteEntitiesDefault ( 0 );
|
|
|
|
|
|
|
|
loadCatalog();
|
|
|
|
|
|
|
|
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;
|
2007-09-07 23:17:30 +02:00
|
|
|
}
|
|
|
|
|
2007-09-08 00:25:30 +02:00
|
|
|
int WrapLibxml::saveEncodingFromFile (
|
|
|
|
const std::string& fileNameSource,
|
|
|
|
const std::string& fileNameDestination,
|
|
|
|
const std::string& encoding )
|
2007-09-07 23:17:30 +02:00
|
|
|
{
|
2008-01-06 23:13:01 +01:00
|
|
|
xmlCleanupParser();
|
2007-09-08 00:25:30 +02:00
|
|
|
|
2008-01-06 23:13:01 +01:00
|
|
|
xmlParserCtxtPtr ctxt = xmlNewParserCtxt();
|
|
|
|
if ( !ctxt )
|
|
|
|
return -1;
|
2007-09-08 00:25:30 +02:00
|
|
|
|
2008-01-06 23:13:01 +01:00
|
|
|
xmlSubstituteEntitiesDefault ( 0 );
|
2007-09-08 00:25:30 +02:00
|
|
|
|
2008-01-06 23:13:01 +01:00
|
|
|
loadCatalog();
|
2007-09-08 00:25:30 +02:00
|
|
|
|
2008-01-06 23:13:01 +01:00
|
|
|
xmlKeepBlanksDefault ( 1 ); // prevents single-line output
|
2007-09-08 00:25:30 +02:00
|
|
|
|
2008-01-06 23:13:01 +01:00
|
|
|
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;
|
|
|
|
}
|
2007-09-08 00:25:30 +02:00
|
|
|
|
2008-01-06 23:13:01 +01:00
|
|
|
int result = xmlSaveFileEnc (
|
|
|
|
fileNameDestination.c_str(),
|
|
|
|
docPtr,
|
|
|
|
encoding.c_str() );
|
2007-09-08 00:25:30 +02:00
|
|
|
|
2008-01-06 23:13:01 +01:00
|
|
|
// ensure entity warnings are treated as errors!
|
|
|
|
if ( !getLastError().empty() )
|
|
|
|
return -1;
|
2007-09-08 00:25:30 +02:00
|
|
|
|
2008-01-06 23:13:01 +01:00
|
|
|
xmlFreeDoc ( docPtr );
|
|
|
|
xmlFreeParserCtxt ( ctxt );
|
|
|
|
return result;
|
2007-09-07 23:17:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string WrapLibxml::getLastError()
|
|
|
|
{
|
2008-01-06 23:13:01 +01:00
|
|
|
xmlErrorPtr err = xmlGetLastError();
|
|
|
|
|
|
|
|
if ( !err )
|
|
|
|
return "";
|
|
|
|
|
|
|
|
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();
|
2007-09-07 23:17:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
std::pair<int, int> WrapLibxml::getErrorPosition()
|
|
|
|
{
|
2008-01-06 23:13:01 +01:00
|
|
|
xmlErrorPtr err = xmlGetLastError();
|
|
|
|
if ( !err )
|
|
|
|
return std::make_pair ( 1, 1 );
|
2007-09-07 23:17:30 +02:00
|
|
|
|
2008-01-06 23:13:01 +01:00
|
|
|
return std::make_pair (
|
|
|
|
err->line,
|
|
|
|
err->int2 );
|
2007-09-07 23:17:30 +02:00
|
|
|
}
|
|
|
|
|
2007-09-08 00:25:30 +02:00
|
|
|
std::string WrapLibxml::getOutput()
|
|
|
|
{
|
2008-01-06 23:13:01 +01:00
|
|
|
return output;
|
2007-09-08 00:25:30 +02:00
|
|
|
}
|
2007-09-07 23:17:30 +02:00
|
|
|
|
|
|
|
void WrapLibxml::loadCatalog()
|
2007-09-08 00:25:30 +02:00
|
|
|
{
|
2007-09-07 23:17:30 +02:00
|
|
|
#ifdef __WXMSW__
|
2008-01-06 23:13:01 +01:00
|
|
|
xmlLoadCatalog ( catalogPath.c_str() );
|
2007-09-07 23:17:30 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2007-09-08 00:25:30 +02:00
|
|
|
std::string WrapLibxml::lookupPublicId ( const std::string& id )
|
2007-09-07 23:17:30 +02:00
|
|
|
{
|
2008-01-06 23:13:01 +01:00
|
|
|
loadCatalog();
|
|
|
|
std::string ret;
|
|
|
|
char *s = ( char * ) xmlCatalogResolvePublic ( ( const xmlChar * ) id.c_str() );
|
|
|
|
if ( !s )
|
|
|
|
return ret;
|
|
|
|
ret = s;
|
|
|
|
free ( s );
|
|
|
|
return ret;
|
2007-09-07 23:17:30 +02:00
|
|
|
}
|