xml-copy-editor-code/src/wraplibxml.cpp

837 lines
20 KiB
C++
Raw Normal View History

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; either version 2
* of the License, or (at your option) any later version.
2009-11-06 18:06:12 +01: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.
*
* 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>
#include <wx/filesys.h>
#include <wx/uri.h>
2009-11-06 18:06:12 +01:00
2014-03-25 16:14:45 +01:00
// Convert wxString to const char *
#ifdef __WXMSW__ // Libxml supports utf8 file name on windows
2014-03-25 16:14:45 +01:00
#define CONV(s) ( ( const char * ) ( s ).utf8_str() )
#else
2014-03-25 16:14:45 +01:00
#define CONV(s) ( ( const char * ) ( s ).mb_str ( wxConvLocal ) )
#endif
static xmlCatalogPtr catalog = NULL;
class Initializer
{
public:
Initializer ( const wxString &catalogPath ) throw ()
{
xmlSetGenericErrorFunc ( xmlGenericErrorContext,
&Initializer::OnXmlGenericError );
LIBXML_TEST_VERSION
xmlInitializeCatalog();
2014-03-25 16:14:45 +01:00
xmlLoadCatalog ( CONV ( catalogPath ) );
::catalog = xmlLoadACatalog ( CONV ( catalogPath ) );
initGenericErrorDefaultFunc ( NULL );
}
~Initializer ()
{
xmlFreeCatalog ( ::catalog );
::catalog = NULL;
xsltCleanupGlobals();
xmlCatalogCleanup();
xmlCleanupParser();
}
static void XMLCDECL OnXmlGenericError (void *ctx, const char *msg, ...) throw()
{
va_list args;
size_t size = 128;
std::string buffer;
int chars;
for (;;)
{
buffer.resize ( size );
if ( buffer.size() < size )
throw std::runtime_error ( "Out of memory" );
va_start(args, msg);
chars = vsnprintf( (char *) buffer.c_str(), size, msg, args);
va_end(args);
if ( chars >= 0 && ( size_t ) chars < size )
{
buffer.resize ( chars );
throw std::runtime_error ( buffer );
}
if ( chars >= 0 )
size = chars + 1;
else
throw std::runtime_error (
std::string ( "Can't format message: " ) + msg );
}
}
};
void WrapLibxml::Init ( const wxString &catalogPath ) throw()
{
static Initializer dummy ( catalogPath );
}
WrapLibxml::WrapLibxml ( bool netAccessParameter )
: netAccess ( netAccessParameter )
2009-11-06 18:06:12 +01:00
{
WrapLibxml::Init();
2009-11-06 18:06:12 +01:00
}
WrapLibxml::~WrapLibxml()
{
}
bool WrapLibxml::validate ( const std::string& utf8DocBuf,
2014-03-25 16:14:45 +01:00
const wxString &docFileName )
2009-11-06 18:06:12 +01:00
{
output = "";
xmlParserCtxtPtr ctxt = NULL;
xmlDocPtr docPtr = NULL;
2009-11-06 18:06:12 +01:00
ctxt = xmlNewParserCtxt();
if ( ctxt == NULL )
{
2013-12-22 16:24:00 +01:00
nonParserError = _("Cannot create a parser context");
2009-11-06 18:06:12 +01:00
return false;
}
int flags = XML_PARSE_DTDVALID;
if ( !netAccess )
flags |= XML_PARSE_NONET;
xmlChar *url = xmlFileNameToURL ( docFileName );
docPtr = xmlCtxtReadMemory ( ctxt, utf8DocBuf.c_str(), utf8DocBuf.length(),
( const char * ) url, "UTF-8", flags);
xmlFree ( url );
2013-11-12 17:30:07 +01:00
bool returnValue = docPtr != NULL && ctxt->valid != 0;
2009-11-06 18:06:12 +01:00
xmlFreeDoc ( docPtr );
xmlFreeParserCtxt ( ctxt );
return returnValue;
}
bool WrapLibxml::validateRelaxNG (
const wxString &schemaUrl,
const std::string &utf8DocBuf,
2014-03-25 16:14:45 +01:00
const wxString &docFileName )
2009-11-06 18:06:12 +01:00
{
output = "";
bool returnValue = false;
xmlParserCtxtPtr ctxt = NULL;
xmlDocPtr docPtr = NULL;
xmlRelaxNGValidCtxtPtr ctxtPtr = NULL;
xmlRelaxNGParserCtxtPtr rngParserCtxt = NULL;
xmlRelaxNGPtr schemaPtr = NULL;
2009-11-06 18:06:12 +01:00
do {
rngParserCtxt = xmlRelaxNGNewParserCtxt ( schemaUrl.utf8_str() );
if ( rngParserCtxt == NULL )
2013-12-22 16:24:00 +01:00
{
nonParserError = _("Cannot create an RNG parser context");
return false;
2013-12-22 16:24:00 +01:00
}
schemaPtr = xmlRelaxNGParse ( rngParserCtxt );
if ( schemaPtr == NULL )
break;
2009-11-06 18:06:12 +01:00
ctxtPtr = xmlRelaxNGNewValidCtxt ( schemaPtr );
if ( ctxtPtr == NULL )
2013-12-22 16:24:00 +01:00
{
nonParserError = _("Cannot create an RNG validation context");
break;
2013-12-22 16:24:00 +01:00
}
ctxt = xmlNewParserCtxt();
if ( ctxt == NULL )
2013-12-22 16:24:00 +01:00
{
nonParserError = _("Cannot create a parser context");
break;
2013-12-22 16:24:00 +01:00
}
2009-11-06 18:06:12 +01:00
int flags = XML_PARSE_DTDVALID;
if ( !netAccess )
flags |= XML_PARSE_NONET;
xmlChar *url = xmlFileNameToURL ( docFileName );
docPtr = xmlCtxtReadMemory ( ctxt, utf8DocBuf.c_str(),
utf8DocBuf.length(), ( const char * ) url, "UTF-8", flags );
xmlFree ( url );
if ( docPtr == NULL )
break;
int err = xmlRelaxNGValidateDoc ( ctxtPtr, docPtr );
returnValue = ( err ) ? false : true;
2009-11-06 18:06:12 +01:00
} while ( false );
2009-11-06 18:06:12 +01:00
xmlFreeDoc ( docPtr );
xmlFreeParserCtxt ( ctxt );
2009-11-06 18:06:12 +01:00
xmlRelaxNGFreeValidCtxt ( ctxtPtr );
xmlRelaxNGFree ( schemaPtr );
2009-11-06 18:06:12 +01:00
return returnValue;
}
bool WrapLibxml::validateW3CSchema (
const wxString &schemaFileName,
const std::string &utf8DocBuf,
2014-03-25 16:14:45 +01:00
const wxString &docFileName )
2009-11-06 18:06:12 +01:00
{
output = "";
bool returnValue = false;
2009-11-06 18:06:12 +01:00
xmlParserCtxtPtr ctxt = NULL;
xmlDocPtr docPtr = NULL;
xmlSchemaValidCtxtPtr ctxtPtr = NULL;
xmlSchemaParserCtxtPtr rngParserCtxt = NULL;
xmlSchemaPtr schemaPtr = NULL;
2009-11-06 18:06:12 +01:00
do {
xmlChar *url = xmlFileNameToURL ( schemaFileName );
rngParserCtxt = xmlSchemaNewParserCtxt ( ( const char * ) url );
xmlFree ( url );
if ( rngParserCtxt == NULL )
return false;
2009-11-06 18:06:12 +01:00
schemaPtr = xmlSchemaParse ( rngParserCtxt );
if ( schemaPtr == NULL )
break;
2009-11-06 18:06:12 +01:00
ctxtPtr = xmlSchemaNewValidCtxt ( schemaPtr );
if ( ctxtPtr == NULL )
2013-12-22 16:24:00 +01:00
{
nonParserError = _("Cannot create a schema validation context");
break;
2013-12-22 16:24:00 +01:00
}
ctxt = xmlNewParserCtxt();
if ( ctxt == NULL )
2013-12-22 16:24:00 +01:00
{
nonParserError = _("Cannot create a parser context");
break;
2013-12-22 16:24:00 +01:00
}
int flags = XML_PARSE_DTDLOAD;
if ( !netAccess )
flags |= XML_PARSE_NONET;
url = xmlFileNameToURL ( docFileName );
docPtr = xmlCtxtReadMemory ( ctxt, utf8DocBuf.c_str(),
utf8DocBuf.length(), ( const char * ) url, "UTF-8", flags );
xmlFree ( url );
if ( docPtr == NULL )
break;
int res = xmlSchemaValidateDoc ( ctxtPtr, docPtr );
2009-11-06 18:06:12 +01:00
returnValue = ( res ) ? false : true;
} while ( false );
2009-11-06 18:06:12 +01:00
xmlFreeDoc ( docPtr );
xmlFreeParserCtxt ( ctxt );
2009-11-06 18:06:12 +01:00
xmlSchemaFree ( schemaPtr );
xmlSchemaFreeValidCtxt ( ctxtPtr );
2009-11-06 18:06:12 +01:00
return returnValue;
}
bool WrapLibxml::parse (
const std::string& utf8DocBuf,
2014-03-25 16:14:45 +01:00
const wxString &docFileName,
bool indent,
bool resolveEntities )
{
2014-03-25 16:14:45 +01:00
return parse ( utf8DocBuf.c_str(), utf8DocBuf.length(), docFileName,
indent, resolveEntities );
}
bool WrapLibxml::parse (
2014-03-25 16:14:45 +01:00
const wxString &docFileName,
bool indent,
bool resolveEntities )
{
2014-03-25 16:14:45 +01:00
return parse ( NULL, 0, docFileName, indent, resolveEntities );
}
bool WrapLibxml::parse (
const char *utf8DocBuf,
size_t utf8DocBufSize,
2014-03-25 16:14:45 +01:00
const wxString &docFileName,
2009-11-06 18:06:12 +01:00
bool indent,
bool resolveEntities )
{
output = "";
xmlParserCtxtPtr ctxt = NULL;
xmlDocPtr docPtr = NULL;
2009-11-06 18:06:12 +01:00
ctxt = xmlNewParserCtxt();
if ( ctxt == NULL )
{
2013-12-22 16:24:00 +01:00
nonParserError = _("Cannot create a parser context");
2009-11-06 18:06:12 +01:00
return false;
}
int flags = XML_PARSE_DTDLOAD;
if ( resolveEntities )
flags |= XML_PARSE_NOENT;
if ( !netAccess )
flags |= XML_PARSE_NONET;
if ( utf8DocBuf != NULL)
{
xmlChar *url = xmlFileNameToURL ( docFileName );
docPtr = xmlCtxtReadMemory ( ctxt, utf8DocBuf, utf8DocBufSize,
( const char * ) url, "UTF-8", flags );
xmlFree ( url );
}
else
2014-03-25 16:14:45 +01:00
docPtr = xmlCtxtReadFile ( ctxt, CONV ( docFileName ), NULL, flags );
2009-11-06 18:06:12 +01:00
if ( docPtr == NULL )
{
xmlFreeParserCtxt ( ctxt );
return false;
}
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 wxString &xpath, const std::string &utf8DocBuf,
2014-03-25 16:14:45 +01:00
const wxString &docFileName )
2009-11-06 18:06:12 +01:00
{
output = "";
xmlParserCtxtPtr ctxt = NULL;
xmlDocPtr docPtr = NULL;
2009-11-06 18:06:12 +01:00
xmlKeepBlanksDefault ( 0 );
2009-11-06 18:06:12 +01:00
ctxt = xmlNewParserCtxt();
if ( ctxt == NULL )
{
2013-12-22 16:24:00 +01:00
nonParserError = _("Cannot create a parser context");
2009-11-06 18:06:12 +01:00
return false;
}
//(netAccess) ? XML_PARSE_DTDLOAD | XML_PARSE_NOENT : XML_PARSE_DTDLOAD | XML_PARSE_NONET | XML_PARSE_NOENT
const static int flags = XML_PARSE_NOENT | XML_PARSE_NONET | XML_PARSE_NSCLEAN;
xmlChar *url = xmlFileNameToURL ( docFileName );
docPtr = xmlCtxtReadMemory ( ctxt, utf8DocBuf.c_str(), utf8DocBuf.length(),
( const char * ) url, "UTF-8", flags );
xmlFree ( url );
2009-11-06 18:06:12 +01:00
if ( docPtr == NULL )
{
xmlFreeParserCtxt ( ctxt );
return false;
}
xmlXPathContextPtr context = NULL;
xmlXPathObjectPtr result = NULL;
2009-11-06 18:06:12 +01:00
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 (
// Since the encoding of the buffer is UTF-8
( const xmlChar * ) ( const char * ) xpath.utf8_str (),
context );
2009-11-06 18:06:12 +01:00
bool xpathIsValid = ( result ) ? true : false;
2014-05-20 14:51:28 +02:00
output = dumpXPathObject ( result );
xmlXPathFreeObject ( result );
xmlXPathFreeContext ( context );
xmlFreeDoc ( docPtr );
xmlFreeParserCtxt ( ctxt );
return xpathIsValid;
}
std::string WrapLibxml::dumpXPathObject ( xmlXPathObjectPtr obj )
{
std::stringstream sstream;
if ( !obj )
return sstream.str();
switch ( obj->type )
{
case XPATH_NODESET:
2009-11-06 18:06:12 +01:00
{
2014-05-20 14:51:28 +02:00
if ( xmlXPathNodeSetIsEmpty ( obj->nodesetval ) )
2009-11-06 18:06:12 +01:00
break;
2014-05-20 14:51:28 +02:00
xmlBufferPtr bufferPtr = xmlBufferCreate();
if ( bufferPtr == NULL )
break;
2014-05-20 14:51:28 +02:00
xmlNodeSetPtr nodeset = obj->nodesetval;
2009-11-06 18:06:12 +01:00
for ( int i = 0; i < nodeset->nodeNr; i++ )
{
xmlNodePtr node = nodeset->nodeTab[i];
if ( !node )
2009-11-06 18:06:12 +01:00
break;
2014-05-20 14:51:28 +02:00
xmlNodeDump ( bufferPtr, NULL, node, 0/*level*/, TRUE/*format*/ );
2009-11-06 18:06:12 +01:00
2014-05-20 14:51:28 +02:00
sstream << ( const char * ) xmlBufferContent ( bufferPtr )
<< '\n';
2013-07-20 20:07:39 +02:00
xmlBufferEmpty ( bufferPtr );
2009-11-06 18:06:12 +01:00
}
xmlBufferFree ( bufferPtr );
2009-11-06 18:06:12 +01:00
break;
}
2014-05-20 14:51:28 +02:00
case XPATH_BOOLEAN:
sstream << !!obj->boolval;
break;
2009-11-06 18:06:12 +01:00
2014-05-20 14:51:28 +02:00
case XPATH_NUMBER:
switch ( xmlXPathIsInf ( obj->floatval ) )
{
case 1:
sstream << wxString ( _("Infinity") ).utf8_str();
break;
case -1:
sstream << wxString ( _("-Infinity") ).utf8_str();
break;
default:
if ( xmlXPathIsNaN ( obj->floatval ) )
sstream << wxString ( _("NaN") ).utf8_str();
else
sstream << obj->floatval;
break;
}
break;
case XPATH_STRING:
sstream << obj->stringval;
break;
case XPATH_POINT:
xmlBufferPtr bufferPtr;
bufferPtr = xmlBufferCreate();
if ( bufferPtr == NULL )
break;
xmlNodeDump ( bufferPtr, NULL, ( xmlNodePtr ) obj->user,
1/*level*/, TRUE/*format*/ );
sstream << xmlBufferContent ( bufferPtr );
xmlBufferFree ( bufferPtr );
break;
case XPATH_RANGE:
case XPATH_LOCATIONSET:
case XPATH_XSLT_TREE:
default:
FILE *fp = tmpfile();
xmlXPathDebugDumpObject ( fp, obj, 0/*depth*/ );
std::string str;
2014-06-01 11:16:02 +02:00
long size = ftell ( fp );
if ( size == -1 )
return str;
str.resize ( size + 1 );
if ( str.capacity() != ( size_t ) size + 1 )
return str;
rewind ( fp );
size_t read = fread ( ( char * ) str.c_str(), 1, str.capacity(), fp );
str[size] = '\0';
2014-05-20 14:51:28 +02:00
fclose ( fp );
2014-06-01 11:16:02 +02:00
return read == str.capacity() ? str : sstream.str();
2014-05-20 14:51:28 +02:00
}
return sstream.str();
2009-11-06 18:06:12 +01:00
}
bool WrapLibxml::xslt (
const wxString &styleFileName,
const std::string &utf8DocBuf,
2014-03-25 16:14:45 +01:00
const wxString &docFileName
)
{
return xslt ( styleFileName, utf8DocBuf.c_str(), utf8DocBuf.length(),
2014-03-25 16:14:45 +01:00
docFileName );
}
bool WrapLibxml::xslt (
const wxString &styleFileName,
2014-03-25 16:14:45 +01:00
const wxString &docFileName
)
{
2014-03-25 16:14:45 +01:00
return xslt ( styleFileName, NULL, 0, docFileName );
}
bool WrapLibxml::xslt (
const wxString &styleFileName,
const char *utf8DocBuf,
size_t utf8DocBufSize,
2014-03-25 16:14:45 +01:00
const wxString &docFileName
2009-11-06 18:06:12 +01:00
)
{
output = "";
bool ret = false;
2009-11-06 18:06:12 +01:00
xsltStylesheetPtr cur = NULL;
xmlParserCtxtPtr ctxt = NULL;
xmlDocPtr doc = NULL, res = NULL;
2009-11-06 18:06:12 +01:00
do {
cur = xsltParseStylesheetFile ( ( const xmlChar * )
CONV ( styleFileName ) );
if ( !cur )
{
nonParserError = _("Cannot parse stylesheet");
return false;
}
2009-11-06 18:06:12 +01:00
ctxt = xmlNewParserCtxt();
if ( !ctxt )
{
2013-12-22 16:24:00 +01:00
nonParserError = _("Cannot create a parser context");
break;
}
2009-11-06 18:06:12 +01:00
int flags = XML_PARSE_NOENT | XML_PARSE_DTDLOAD;
if ( !netAccess )
flags |= XML_PARSE_NONET;
if ( utf8DocBuf != NULL )
{
xmlChar *url = xmlFileNameToURL ( docFileName );
doc = xmlCtxtReadMemory ( ctxt, utf8DocBuf, utf8DocBufSize,
( const char * ) url, "UTF-8", flags );
xmlFree ( url );
}
else
2014-03-25 16:14:45 +01:00
doc = xmlCtxtReadFile ( ctxt, CONV ( docFileName ), NULL, flags );
if ( !doc )
break;
res = xsltApplyStylesheet ( cur, doc, NULL );
if ( !res )
{
nonParserError = _("Cannot apply stylesheet");
break;
}
xmlChar *buf = NULL;
int size;
xmlDocDumpFormatMemoryEnc ( res, &buf, &size, "UTF-8", 1 );
if ( buf )
{
output.append ( ( char * ) buf, size );
xmlFree ( buf );
}
2013-12-22 16:24:00 +01:00
// ensure entity warnings are treated as errors
ret = !xmlGetLastError();
} while ( false );
2009-11-06 18:06:12 +01:00
xmlFreeDoc ( doc );
xmlFreeParserCtxt ( ctxt );
2013-12-15 15:21:31 +01:00
xmlFreeDoc ( res );
xsltFreeStylesheet ( cur );
2009-11-06 18:06:12 +01:00
return ret;
2009-11-06 18:06:12 +01:00
}
bool WrapLibxml::bufferWellFormed ( const std::string& buffer )
{
xmlParserCtxtPtr ctxt = xmlNewParserCtxt();
if ( !ctxt )
2013-12-22 16:24:00 +01:00
{
nonParserError = _("Cannot create a parser context");
2009-11-06 18:06:12 +01:00
return false;
2013-12-22 16:24:00 +01:00
}
2009-11-06 18:06:12 +01:00
int flags = XML_PARSE_DTDLOAD;
if ( !netAccess )
flags |= XML_PARSE_NONET;
xmlDocPtr docPtr = xmlCtxtReadMemory ( ctxt, buffer.c_str(), buffer.size(),
"", "UTF-8", flags );
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 &utf8Buffer,
const wxString &fileNameSource,
const wxString &fileNameDestination,
wxMemoryBuffer *outputBuffer,
const wxString &encoding )
2009-11-06 18:06:12 +01:00
{
return saveEncoding ( utf8Buffer.c_str(), utf8Buffer.length(),
fileNameSource, fileNameDestination, outputBuffer, encoding );
}
2009-11-06 18:06:12 +01:00
int WrapLibxml::saveEncoding (
const wxString &fileNameSource,
const wxString &fileNameDestination,
const wxString &encoding )
{
return saveEncoding ( NULL, 0, fileNameSource, fileNameDestination,
NULL, encoding );
2009-11-06 18:06:12 +01:00
}
int WrapLibxml::saveEncoding (
const char *utf8Buffer,
size_t utf8BufferSize,
const wxString &fileNameSource,
const wxString &fileNameDestination,
wxMemoryBuffer *outputBuffer,
const wxString &encoding )
2009-11-06 18:06:12 +01:00
{
xmlParserCtxtPtr ctxt = xmlNewParserCtxt();
if ( !ctxt )
2013-12-22 16:24:00 +01:00
{
nonParserError = _("Cannot create a parser context");
2009-11-06 18:06:12 +01:00
return -1;
2013-12-22 16:24:00 +01:00
}
2009-11-06 18:06:12 +01:00
xmlDocPtr docPtr;
// Don't load DTD because additional namespace declarations will be added
// to every element when processing a docbook XML
int flags = XML_PARSE_PEDANTIC /*| XML_PARSE_DTDLOAD | XML_PARSE_DTDVALID*/;
if ( !netAccess )
flags |= XML_PARSE_NONET;
if ( utf8Buffer != NULL )
{
xmlChar *url = xmlFileNameToURL ( fileNameSource );
docPtr = xmlCtxtReadMemory ( ctxt, utf8Buffer, utf8BufferSize,
( const char * ) url, "UTF-8", flags );
xmlFree ( url );
}
else
docPtr = xmlCtxtReadFile ( ctxt, CONV ( fileNameSource ), NULL, flags );
2009-11-06 18:06:12 +01:00
if ( !docPtr )
{
xmlFreeParserCtxt ( ctxt );
return -1;
}
int result;
if ( outputBuffer == NULL )
{
result = xmlSaveFileEnc (
CONV ( fileNameDestination ),
docPtr,
encoding.utf8_str() );
}
else
{
xmlChar *buffer;
xmlDocDumpFormatMemoryEnc ( docPtr, &buffer, &result,
encoding.utf8_str(), 1 );
outputBuffer->AppendData ( buffer, result );
xmlFree ( buffer );
}
2009-11-06 18:06:12 +01:00
xmlFreeDoc ( docPtr );
xmlFreeParserCtxt ( ctxt );
2013-12-15 15:21:31 +01:00
2009-11-06 18:06:12 +01:00
return result;
}
2013-12-15 15:21:31 +01:00
wxString WrapLibxml::getLastError()
2009-11-06 18:06:12 +01:00
{
xmlErrorPtr err = xmlGetLastError();
if ( !err )
return nonParserError;
2009-11-06 18:06:12 +01:00
2013-12-15 15:21:31 +01:00
wxString error ( err->message, wxConvLocal );
2009-11-06 18:06:12 +01:00
if ( err->int2 )
2013-12-15 15:21:31 +01:00
return wxString::Format ( _("Error at line %d, column %d: %s"),
err->line, err->int2, error.c_str() );
return wxString::Format ( _("Error at line %d: %s"),
err->line, error.c_str() );
2009-11-06 18:06:12 +01:00
}
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;
}
2013-10-26 15:15:42 +02:00
wxString WrapLibxml::catalogResolve
( const wxString &publicId
, const wxString &systemId
)
2009-11-06 18:06:12 +01:00
{
// According to 7.1.2. Resolution of External Identifiers
// from http://www.oasis-open.org/committees/entity/spec-2001-08-06.html,
// our catalog may not be used if the system catalog, which is specified
// in a delegateSystem entry, is out of date, such as the catalog for
// resolving public ID "-//OASIS//DTD DocBook XML V5.0//EN"
char *s = ( char * ) xmlACatalogResolve ( ::catalog,
( const xmlChar * ) ( const char *) publicId.utf8_str(),
( const xmlChar * ) ( const char *) systemId.utf8_str() );
if ( s == NULL )
2013-11-06 12:43:04 +01:00
{
#ifndef __WXMSW__
s = ( char * ) xmlCatalogResolve (
( const xmlChar * ) ( const char *) publicId.utf8_str(),
( const xmlChar * ) ( const char *) systemId.utf8_str() );
2013-11-06 12:43:04 +01:00
if ( s == NULL )
#endif
2013-11-06 12:43:04 +01:00
return wxEmptyString;
}
2009-11-06 18:06:12 +01:00
wxString url ( s, wxConvUTF8 );
2013-10-26 15:15:42 +02:00
xmlFree ( s );
wxFileName file = URLToFileName ( url );
if ( file.IsFileReadable() )
2013-10-26 15:15:42 +02:00
return file.GetFullPath();
2009-11-06 18:06:12 +01:00
2013-10-26 15:15:42 +02:00
return url;
2009-11-06 18:06:12 +01:00
}
wxString WrapLibxml::FileNameToURL ( const wxString &fileName )
{
xmlChar *s = xmlFileNameToURL ( fileName );
if ( !s )
return wxEmptyString;
wxString url = wxString::FromUTF8 ( ( char * ) s );
xmlFree ( s );
return url;
}
xmlChar *WrapLibxml::xmlFileNameToURL ( const wxString &fileName )
{
if ( fileName.empty() )
return NULL;
wxFileName fn ( fileName );
fn.Normalize();
wxString url = fn.GetFullPath();
return xmlPathToURI ( ( xmlChar * ) ( const char * ) url.utf8_str() );
}
wxFileName WrapLibxml::URLToFileName ( const wxString &url )
{
#if wxCHECK_VERSION(2,9,0)
return wxFileSystem::URLToFileName ( url );
#else
xmlURIPtr uri = xmlParseURI ( url.utf8_str() );
if ( !uri )
return wxFileName ( url );
do {
if ( uri->scheme != NULL && strcmp (uri->scheme, "file" ) )
break;
2014-05-04 02:40:05 +02:00
#ifdef _MSC_VER
if ( uri->server && stricmp ( uri->server, "localhost") )
break;
2014-05-04 02:40:05 +02:00
#else
if ( uri->server && strcasecmp ( uri->server, "localhost") )
break;
#endif
if ( uri->path == NULL || !*uri->path )
break;
char *path = uri->path;
// Does it begin with "/C:" ?
if ( *path == '/' && wxIsalpha ( path[1] ) && path[2] == ':')
path++;
wxFileName file ( wxString ( path, wxConvUTF8 ) );
xmlFreeURI ( uri );
return file;
} while ( false );
xmlFreeURI ( uri );
return wxFileName(url);
#endif // wxCHECK_VERSION(2,9,0)
}