Use memory rather than temporary files to pass XML content

This commit is contained in:
Zane U. Ji 2013-12-15 22:19:08 +08:00
parent e516b13d5e
commit dff2513b0d
4 changed files with 268 additions and 249 deletions

View File

@ -103,11 +103,9 @@ bool WrapDaisy::run (
return false; return false;
} }
std::string output, stdStylesheet, stdFileIn; std::string output;
stdStylesheet = stylesheet.mb_str ( wxConvUTF8 );
stdFileIn = fileIn.mb_str ( wxConvUTF8 );
if ( !stdStylesheet.empty() ) // stylesheet found if ( !stylesheet.empty() ) // stylesheet found
{ {
// #1: convert to canonical XHTML // #1: convert to canonical XHTML
#if wxCHECK_VERSION(2,9,0) #if wxCHECK_VERSION(2,9,0)
@ -126,7 +124,7 @@ bool WrapDaisy::run (
WrapLibxml wrapLibxml; WrapLibxml wrapLibxml;
bool success = wrapLibxml.xslt ( stdStylesheet, stdFileIn ); bool success = wrapLibxml.xslt ( stylesheet, fileIn );
if ( !success ) if ( !success )
{ {

View File

@ -29,6 +29,13 @@
#include <wx/filesys.h> #include <wx/filesys.h>
#include <wx/uri.h> #include <wx/uri.h>
#ifdef __WXMSW__ // Libxml supports utf8 file name on windows
#define CONV(url) ( ( const char * ) ( url ).utf8_str() )
#else
#define CONV(url) ( ( const char * ) ( url ).mb_str ( wxConvLocal ) )
#endif
static xmlCatalogPtr catalog = NULL; static xmlCatalogPtr catalog = NULL;
class Initializer class Initializer
@ -105,12 +112,13 @@ WrapLibxml::~WrapLibxml()
{ {
} }
bool WrapLibxml::validate ( const std::string& fileName ) bool WrapLibxml::validate ( const std::string& utf8DocBuf,
const wxString &docUrl )
{ {
output = ""; output = "";
xmlParserCtxtPtr ctxt; xmlParserCtxtPtr ctxt = NULL;
xmlDocPtr docPtr; xmlDocPtr docPtr = NULL;
ctxt = xmlNewParserCtxt(); ctxt = xmlNewParserCtxt();
if ( ctxt == NULL ) if ( ctxt == NULL )
@ -118,11 +126,11 @@ bool WrapLibxml::validate ( const std::string& fileName )
return false; return false;
} }
docPtr = xmlCtxtReadFile ( int flags = XML_PARSE_DTDVALID;
ctxt, if ( !netAccess )
fileName.c_str(), flags |= XML_PARSE_NONET;
NULL, docPtr = xmlCtxtReadMemory ( ctxt, utf8DocBuf.c_str(), utf8DocBuf.length(),
( netAccess ) ? XML_PARSE_DTDVALID : XML_PARSE_DTDVALID | XML_PARSE_NONET ); CONV ( docUrl ), "UTF-8", flags);
bool returnValue = docPtr != NULL && ctxt->valid != 0; bool returnValue = docPtr != NULL && ctxt->valid != 0;
@ -133,112 +141,140 @@ bool WrapLibxml::validate ( const std::string& fileName )
} }
bool WrapLibxml::validateRelaxNG ( bool WrapLibxml::validateRelaxNG (
const std::string& schemaFileName, const wxString &schemaFileName,
const std::string& fileName ) const std::string &utf8DocBuf,
const wxString &docUrl )
{ {
output = ""; output = "";
xmlRelaxNGValidCtxtPtr ctxtPtr; bool returnValue = false;
xmlDocPtr docPtr; xmlParserCtxtPtr ctxt = NULL;
xmlRelaxNGParserCtxtPtr schemaParserCtxtPtr; xmlDocPtr docPtr = NULL;
xmlRelaxNGPtr schemaPtr; xmlRelaxNGValidCtxtPtr ctxtPtr = NULL;
xmlRelaxNGParserCtxtPtr rngParserCtxt = NULL;
xmlRelaxNGPtr schemaPtr = NULL;
schemaParserCtxtPtr = xmlRelaxNGNewParserCtxt ( schemaFileName.c_str() ); do {
if ( schemaParserCtxtPtr == NULL ) rngParserCtxt = xmlRelaxNGNewParserCtxt ( CONV ( schemaFileName ) );
return false; if ( rngParserCtxt == NULL )
return false;
schemaPtr = xmlRelaxNGParse ( schemaParserCtxtPtr ); schemaPtr = xmlRelaxNGParse ( rngParserCtxt );
if ( schemaPtr == NULL ) if ( schemaPtr == NULL )
{ break;
xmlRelaxNGFreeParserCtxt ( schemaParserCtxtPtr );
return false;
}
ctxtPtr = xmlRelaxNGNewValidCtxt ( schemaPtr ); ctxtPtr = xmlRelaxNGNewValidCtxt ( schemaPtr );
if ( ctxtPtr == NULL ) if ( ctxtPtr == NULL )
{ break;
xmlRelaxNGFree ( schemaPtr );
xmlRelaxNGFreeParserCtxt ( schemaParserCtxtPtr );
return false;
}
docPtr = xmlReadFile ( fileName.c_str(), NULL, ctxt = xmlNewParserCtxt();
( netAccess ) ? XML_PARSE_DTDLOAD : XML_PARSE_DTDLOAD | XML_PARSE_NONET ); if ( ctxt == NULL )
if ( docPtr == NULL ) break;
{
xmlRelaxNGFree ( schemaPtr );
xmlRelaxNGFreeValidCtxt ( ctxtPtr );
return false;
}
int res = xmlRelaxNGValidateDoc ( ctxtPtr, docPtr ); int flags = XML_PARSE_DTDVALID;
if ( !netAccess )
flags |= XML_PARSE_NONET;
docPtr = xmlCtxtReadMemory ( ctxt, utf8DocBuf.c_str(),
utf8DocBuf.length(), CONV ( docUrl ), "UTF-8", flags );
if ( docPtr == NULL )
break;
bool returnValue = ( res ) ? false : true; int err = xmlRelaxNGValidateDoc ( ctxtPtr, docPtr );
returnValue = ( err ) ? false : true;
} while ( false );
xmlFreeDoc ( docPtr ); xmlFreeDoc ( docPtr );
xmlRelaxNGFree ( schemaPtr ); xmlFreeParserCtxt ( ctxt );
xmlRelaxNGFreeValidCtxt ( ctxtPtr ); xmlRelaxNGFreeValidCtxt ( ctxtPtr );
xmlRelaxNGFree ( schemaPtr );
return returnValue; return returnValue;
} }
bool WrapLibxml::validateW3CSchema ( bool WrapLibxml::validateW3CSchema (
const std::string& schemaFileName, const wxString &schemaFileName,
const std::string& fileName ) const std::string &utf8DocBuf,
const wxString &docUrl )
{ {
output = ""; output = "";
xmlSchemaValidCtxtPtr ctxtPtr; bool returnValue = false;
xmlDocPtr docPtr;
xmlSchemaParserCtxtPtr schemaParserCtxtPtr;
xmlSchemaPtr schemaPtr;
schemaParserCtxtPtr = xmlSchemaNewParserCtxt ( schemaFileName.c_str() ); xmlParserCtxtPtr ctxt = NULL;
if ( schemaParserCtxtPtr == NULL ) xmlDocPtr docPtr = NULL;
return false; xmlSchemaValidCtxtPtr ctxtPtr = NULL;
xmlSchemaParserCtxtPtr rngParserCtxt = NULL;
xmlSchemaPtr schemaPtr = NULL;
schemaPtr = xmlSchemaParse ( schemaParserCtxtPtr ); do {
if ( schemaPtr == NULL ) rngParserCtxt = xmlSchemaNewParserCtxt ( CONV ( schemaFileName ) );
{ if ( rngParserCtxt == NULL )
xmlSchemaFreeParserCtxt ( schemaParserCtxtPtr ); return false;
return false;
}
ctxtPtr = xmlSchemaNewValidCtxt ( schemaPtr ); schemaPtr = xmlSchemaParse ( rngParserCtxt );
if ( ctxtPtr == NULL ) if ( schemaPtr == NULL )
{ break;
xmlSchemaFree ( schemaPtr );
xmlSchemaFreeParserCtxt ( schemaParserCtxtPtr );
return false;
}
docPtr = xmlReadFile ( fileName.c_str(), NULL, ctxtPtr = xmlSchemaNewValidCtxt ( schemaPtr );
( netAccess ) ? XML_PARSE_DTDLOAD : XML_PARSE_DTDLOAD | XML_PARSE_NONET ); if ( ctxtPtr == NULL )
if ( docPtr == NULL ) break;
{
xmlSchemaFree ( schemaPtr );
xmlSchemaFreeValidCtxt ( ctxtPtr );
return false;
}
int res = xmlSchemaValidateDoc ( ctxtPtr, docPtr ); ctxt = xmlNewParserCtxt();
if ( ctxt == NULL )
break;
bool returnValue = ( res ) ? false : true; int flags = XML_PARSE_DTDLOAD;
if ( !netAccess )
flags |= XML_PARSE_NONET;
docPtr = xmlCtxtReadMemory ( ctxt, utf8DocBuf.c_str(),
utf8DocBuf.length(), CONV ( docUrl ), "UTF-8", flags );
if ( docPtr == NULL )
break;
int res = xmlSchemaValidateDoc ( ctxtPtr, docPtr );
returnValue = ( res ) ? false : true;
} while ( false );
xmlFreeDoc ( docPtr ); xmlFreeDoc ( docPtr );
xmlFreeParserCtxt ( ctxt );
xmlSchemaFree ( schemaPtr ); xmlSchemaFree ( schemaPtr );
xmlSchemaFreeValidCtxt ( ctxtPtr ); xmlSchemaFreeValidCtxt ( ctxtPtr );
return returnValue; return returnValue;
} }
bool WrapLibxml::parse ( bool WrapLibxml::parse (
const std::string& fileName, const std::string& utf8DocBuf,
const wxString &docUrl,
bool indent,
bool resolveEntities )
{
return parse ( utf8DocBuf.c_str(), utf8DocBuf.length(), docUrl,
indent, resolveEntities );
}
bool WrapLibxml::parse (
const wxString &docUrl,
bool indent,
bool resolveEntities )
{
return parse ( NULL, 0, docUrl, indent, resolveEntities );
}
bool WrapLibxml::parse (
const char *utf8DocBuf,
size_t utf8DocBufSize,
const wxString &docUrl,
bool indent, bool indent,
bool resolveEntities ) bool resolveEntities )
{ {
output = ""; output = "";
xmlParserCtxtPtr ctxt; xmlParserCtxtPtr ctxt = NULL;
xmlDocPtr docPtr; xmlDocPtr docPtr = NULL;
ctxt = xmlNewParserCtxt(); ctxt = xmlNewParserCtxt();
if ( ctxt == NULL ) if ( ctxt == NULL )
@ -252,12 +288,11 @@ bool WrapLibxml::parse (
if ( !netAccess ) if ( !netAccess )
flags |= XML_PARSE_NONET; flags |= XML_PARSE_NONET;
docPtr = xmlCtxtReadFile ( if ( utf8DocBuf != NULL)
ctxt, docPtr = xmlCtxtReadMemory ( ctxt, utf8DocBuf, utf8DocBufSize,
fileName.c_str(), CONV ( docUrl ), "UTF-8", flags );
NULL, else
flags ); docPtr = xmlCtxtReadFile ( ctxt, CONV ( docUrl ), NULL, flags );
if ( docPtr == NULL ) if ( docPtr == NULL )
{ {
xmlFreeParserCtxt ( ctxt ); xmlFreeParserCtxt ( ctxt );
@ -291,12 +326,13 @@ bool WrapLibxml::parse (
return returnValue; return returnValue;
} }
bool WrapLibxml::xpath ( const std::string& path, const std::string& fileName ) bool WrapLibxml::xpath ( const wxString &xpath, const std::string &utf8DocBuf,
const wxString &docUrl )
{ {
output = ""; output = "";
xmlParserCtxtPtr ctxt; xmlParserCtxtPtr ctxt = NULL;
xmlDocPtr docPtr; xmlDocPtr docPtr = NULL;
xmlKeepBlanksDefault ( 0 ); xmlKeepBlanksDefault ( 0 );
@ -306,9 +342,11 @@ bool WrapLibxml::xpath ( const std::string& path, const std::string& fileName )
return false; return false;
} }
docPtr = xmlCtxtReadFile ( docPtr = xmlCtxtReadMemory (
ctxt, ctxt,
fileName.c_str(), utf8DocBuf.c_str(),
utf8DocBuf.length(),
CONV ( docUrl ),
"UTF-8", "UTF-8",
//(netAccess) ? XML_PARSE_DTDLOAD | XML_PARSE_NOENT : XML_PARSE_DTDLOAD | XML_PARSE_NONET | XML_PARSE_NOENT //(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 XML_PARSE_NOENT | XML_PARSE_NONET | XML_PARSE_NSCLEAN
@ -320,9 +358,9 @@ bool WrapLibxml::xpath ( const std::string& path, const std::string& fileName )
return false; return false;
} }
xmlXPathContextPtr context; xmlXPathContextPtr context = NULL;
xmlXPathObjectPtr result; xmlXPathObjectPtr result = NULL;
xmlNodeSetPtr nodeset; xmlNodeSetPtr nodeset = NULL;
context = xmlXPathNewContext ( docPtr ); context = xmlXPathNewContext ( docPtr );
if ( !context ) if ( !context )
@ -336,7 +374,10 @@ bool WrapLibxml::xpath ( const std::string& path, const std::string& fileName )
xmlXPathRegisterNs ( context, ( xmlChar * ) "xhtml", ( xmlChar * ) "http://www.w3.org/1999/xhtml" ); xmlXPathRegisterNs ( context, ( xmlChar * ) "xhtml", ( xmlChar * ) "http://www.w3.org/1999/xhtml" );
// add others as necessary! // add others as necessary!
result = xmlXPathEvalExpression ( ( const xmlChar * ) path.c_str(), context ); result = xmlXPathEvalExpression (
// Since the encoding of the buffer is UTF-8
( const xmlChar * ) ( const char * ) xpath.utf8_str (),
context );
bool xpathIsValid = ( result ) ? true : false; bool xpathIsValid = ( result ) ? true : false;
@ -362,8 +403,8 @@ bool WrapLibxml::xpath ( const std::string& path, const std::string& fileName )
xmlBufferFree ( bufferPtr ); xmlBufferFree ( bufferPtr );
break; break;
} }
if ( result )
xmlXPathFreeObject ( result ); xmlXPathFreeObject ( result );
xmlXPathFreeContext ( context ); xmlXPathFreeContext ( context );
xmlFreeDoc ( docPtr ); xmlFreeDoc ( docPtr );
xmlFreeParserCtxt ( ctxt ); xmlFreeParserCtxt ( ctxt );
@ -372,62 +413,98 @@ bool WrapLibxml::xpath ( const std::string& path, const std::string& fileName )
} }
bool WrapLibxml::xslt ( bool WrapLibxml::xslt (
const std::string& styleFileName, const wxString &styleFileName,
const std::string& fileName const std::string &utf8DocBuf,
const wxString &docUrl
)
{
return xslt ( styleFileName, utf8DocBuf.c_str(), utf8DocBuf.length(),
docUrl );
}
bool WrapLibxml::xslt (
const wxString &styleFileName,
const wxString &docUrl
)
{
return xslt ( styleFileName, NULL, 0, docUrl );
}
bool WrapLibxml::xslt (
const wxString &styleFileName,
const char *utf8DocBuf,
size_t utf8DocBufSize,
const wxString &docUrl
) )
{ {
output = ""; output = "";
xsltStylesheetPtr cur; bool ret = false;
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() ); xsltStylesheetPtr cur = NULL;
if ( !doc ) xmlParserCtxtPtr ctxt = NULL;
{ xmlDocPtr doc = NULL, res = NULL;
nonParserError = "Cannot parse file";
xsltFreeStylesheet ( cur );
return false;
}
// ensure entity warnings are treated as errors do {
if ( !getLastError().empty() ) cur = xsltParseStylesheetFile ( ( const xmlChar * )
{ CONV ( styleFileName ) );
xmlFreeDoc ( doc ); if ( !cur )
xsltFreeStylesheet ( cur ); {
return false; nonParserError = "Cannot parse stylesheet";
} return false;
}
res = xsltApplyStylesheet ( cur, doc, NULL ); ctxt = xmlNewParserCtxt();
if ( !res ) if ( !ctxt )
{ {
nonParserError = "Cannot apply stylesheet"; nonParserError = _("Cannot create parser context");
xmlFreeDoc ( doc ); break;
xsltFreeStylesheet ( cur ); }
return false;
}
xmlChar *buf = NULL; int flags = XML_PARSE_NOENT | XML_PARSE_DTDLOAD;
int size; if ( !netAccess )
xmlDocDumpFormatMemoryEnc ( res, &buf, &size, "UTF-8", 1 ); flags |= XML_PARSE_NONET;
if ( buf ) if ( utf8DocBuf != NULL )
{ doc = xmlCtxtReadMemory ( ctxt, utf8DocBuf, utf8DocBufSize,
output.append ( ( char * ) buf, size ); CONV ( docUrl ), "UTF-8", flags );
xmlFree ( buf ); else
} doc = xmlCtxtReadFile ( ctxt, CONV ( docUrl ), NULL, flags );
if ( !doc )
{
nonParserError = "Cannot parse file";
break;
}
// ensure entity warnings are treated as errors
if ( !getLastError().empty() )
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 );
}
ret = true;
} while ( false );
xsltFreeStylesheet ( cur );
xmlFreeDoc ( doc ); xmlFreeDoc ( doc );
xmlFreeDoc ( res ); xmlFreeDoc ( res );
xmlFreeParserCtxt ( ctxt );
xsltFreeStylesheet ( cur );
return true; return ret;
} }
bool WrapLibxml::bufferWellFormed ( const std::string& buffer ) bool WrapLibxml::bufferWellFormed ( const std::string& buffer )
@ -436,13 +513,11 @@ bool WrapLibxml::bufferWellFormed ( const std::string& buffer )
if ( !ctxt ) if ( !ctxt )
return false; return false;
xmlDocPtr docPtr = xmlCtxtReadMemory ( int flags = XML_PARSE_DTDLOAD;
ctxt, if ( !netAccess )
buffer.c_str(), flags |= XML_PARSE_NONET;
buffer.size(), xmlDocPtr docPtr = xmlCtxtReadMemory ( ctxt, buffer.c_str(), buffer.size(),
"", "", "UTF-8", flags );
"UTF-8",
( netAccess ) ? XML_PARSE_DTDLOAD : XML_PARSE_DTDLOAD | XML_PARSE_NONET );
bool returnValue = ( docPtr ) ? true : false; bool returnValue = ( docPtr ) ? true : false;
xmlFreeDoc ( docPtr ); xmlFreeDoc ( docPtr );
@ -530,8 +605,8 @@ std::string WrapLibxml::getLastError()
if ( !err ) if ( !err )
{ {
return ( nonParserError.empty() ) ? "" : nonParserError; return nonParserError;
} }
std::stringstream ss; std::stringstream ss;
ss << "Error at line "; ss << "Error at line ";

View File

@ -43,20 +43,41 @@ class WrapLibxml
WrapLibxml ( bool netAccessParameter = false ); WrapLibxml ( bool netAccessParameter = false );
virtual ~WrapLibxml(); virtual ~WrapLibxml();
bool validate ( const std::string& fileName ); bool validate ( const std::string &utf8DocBuf, const wxString &docUrl );
bool validateRelaxNG ( bool validateRelaxNG (
const std::string& schemaFileName, const wxString &schemaFileName,
const std::string& fileName ); const std::string &utf8DocBuf,
const wxString &docUrl );
bool validateW3CSchema ( bool validateW3CSchema (
const std::string& schemaFileName, const wxString &schemaFileName,
const std::string& fileName ); const std::string &utf8DocBuf,
const wxString &docUrl );
bool parse ( bool parse (
const std::string& fileName, const std::string &utf8DocBuf,
const wxString &docUrl,
bool indent = false, bool indent = false,
bool resolveEntities = false ); bool resolveEntities = false );
bool bufferWellFormed ( const std::string& buffer ); bool parse (
bool xpath ( const std::string& path, const std::string& fileName ); const wxString &fileName,
bool xslt ( const std::string& styleFileName, const std::string& fileName ); bool indent = false,
bool resolveEntities = false );
bool parse (
const char *utf8DocBuf,
size_t utf8DocBufSize,
const wxString &fileName,
bool indent = false,
bool resolveEntities = false );
bool bufferWellFormed ( const std::string& utf8Buffer );
bool xpath ( const wxString &xpath, const std::string &utf8DocBuf,
const wxString &docUrl );
bool xslt ( const wxString &styleFileName,
const std::string &utf8DocBuf,
const wxString &docUrl );
bool xslt ( const wxString &styleFileName, const wxString &docUrl );
bool xslt ( const wxString &styleFileName,
const char *utf8DocBuf,
size_t utf8DocBufSize,
const wxString &docUrl );
std::string getLastError(); std::string getLastError();
std::pair<int, int> getErrorPosition(); std::pair<int, int> getErrorPosition();
std::string getOutput(); std::string getOutput();

View File

@ -2319,7 +2319,7 @@ void MyFrame::importMSWord ( const wxString& path )
if ( result != 5 ) // Word 2003 or later if ( result != 5 ) // Word 2003 or later
{ {
std::auto_ptr<WrapLibxml> prettyPrinter ( new WrapLibxml ( libxmlNetAccess ) ); std::auto_ptr<WrapLibxml> prettyPrinter ( new WrapLibxml ( libxmlNetAccess ) );
prettyPrinter->parse ( tempFileName.name(), true ); prettyPrinter->parse ( tempFileName.wideName(), true );
buffer = prettyPrinter->getOutput(); buffer = prettyPrinter->getOutput();
displayBuffer = wxString ( buffer.c_str(), wxConvUTF8, buffer.size() ); displayBuffer = wxString ( buffer.c_str(), wxConvUTF8, buffer.size() );
} }
@ -3773,34 +3773,13 @@ void MyFrame::OnValidateDTD ( wxCommandEvent& event )
if ( ( doc = getActiveDocument() ) == NULL ) if ( ( doc = getActiveDocument() ) == NULL )
return; return;
wxString fname = doc->getFullFileName();
WrapTempFileName wtfn ( fname );
if ( fname.empty() || doc->GetModify() )
{
wxString wideBuffer = doc->GetText();
std::string buffer = ( const char * ) wideBuffer.mb_str ( wxConvUTF8 );
if ( !saveRawUtf8 (
wtfn.name(),
buffer ) )
{
messagePane (
_ ( "Cannot save temporary copy for validation; please save or discard changes" ),
CONST_STOP );
return;
}
fname = wtfn.wideName();
}
std::string fnameLocal = ( const char * ) fname.mb_str ( wxConvLocal );
doc->clearErrorIndicators(); doc->clearErrorIndicators();
statusProgress ( _ ( "DTD Validation in progress..." ) ); statusProgress ( _ ( "DTD Validation in progress..." ) );
auto_ptr<WrapLibxml> wl ( new WrapLibxml ( libxmlNetAccess ) ); auto_ptr<WrapLibxml> wl ( new WrapLibxml ( libxmlNetAccess ) );
wxString fname = doc->getFullFileName();
if ( !wl->validate ( fnameLocal ) ) if ( !wl->validate ( doc->myGetTextRaw(), fname ) )
{ {
std::string error = wl->getLastError(); std::string error = wl->getLastError();
wxString wideError = wxString ( error.c_str(), wxConvUTF8, error.size() ); wxString wideError = wxString ( error.c_str(), wxConvUTF8, error.size() );
@ -3871,32 +3850,13 @@ void MyFrame::validateRelaxNG (
if ( !doc ) if ( !doc )
return; return;
WrapTempFileName wtfn ( fileName );
if ( fileName.empty() || doc->GetModify() )
{
wxString wideBuffer = doc->GetText();
std::string buffer = ( const char * ) wideBuffer.mb_str ( wxConvUTF8 );
if ( !saveRawUtf8 (
wtfn.name(),
buffer ) )
{
messagePane (
_ ( "Cannot save temporary copy for validation; please save or discard changes" ),
CONST_STOP );
return;
}
fileName = wtfn.wideName();
}
doc->clearErrorIndicators(); doc->clearErrorIndicators();
statusProgress ( _ ( "RELAX NG validation in progress..." ) ); statusProgress ( _ ( "RELAX NG validation in progress..." ) );
auto_ptr<WrapLibxml> wl ( new WrapLibxml ( libxmlNetAccess ) ); auto_ptr<WrapLibxml> wl ( new WrapLibxml ( libxmlNetAccess ) );
std::string schemaFileNameLocal = ( const char * ) schemaName.mb_str ( wxConvLocal ); if ( !wl->validateRelaxNG ( schemaName, doc->myGetTextRaw(), fileName ) )
std::string fileNameLocal = ( const char * ) fileName.mb_str ( wxConvLocal );
if ( !wl->validateRelaxNG ( schemaFileNameLocal, fileNameLocal ) )
{ {
std::string error = wl->getLastError(); std::string error = wl->getLastError();
wxString wideError = wxString ( error.c_str(), wxConvUTF8, error.size() ); wxString wideError = wxString ( error.c_str(), wxConvUTF8, error.size() );
@ -4089,28 +4049,14 @@ void MyFrame::OnXPath ( wxCommandEvent& event )
if ( ret == wxID_CANCEL ) if ( ret == wxID_CANCEL )
return; return;
xpathExpression = dlg->GetValue(); xpathExpression = dlg->GetValue();
std::string valUtf8 = ( const char * ) xpathExpression.mb_str ( wxConvUTF8 );
// fetch document contents // fetch document contents
std::string rawBufferUtf8; std::string utf8Buffer;
getRawText ( doc, rawBufferUtf8 ); getRawText ( doc, utf8Buffer );
if ( !XmlEncodingHandler::setUtf8 ( rawBufferUtf8 ) )
{
encodingMessage();
return;
}
WrapTempFileName tempFileName ( doc->getFullFileName() );
ofstream rawBufferStream ( tempFileName.name().c_str() );
if ( !rawBufferStream )
return;
rawBufferStream << rawBufferUtf8;
rawBufferStream.close();
auto_ptr<WrapLibxml> wl ( new WrapLibxml ( libxmlNetAccess ) ); auto_ptr<WrapLibxml> wl ( new WrapLibxml ( libxmlNetAccess ) );
bool success = wl->xpath ( xpathExpression, utf8Buffer,
bool success = wl->xpath ( valUtf8, tempFileName.name() ); doc->getFullFileName() );
if ( !success ) if ( !success )
{ {
@ -4145,24 +4091,10 @@ void MyFrame::OnXslt ( wxCommandEvent& event )
XmlDoc *doc; XmlDoc *doc;
if ( ( doc = getActiveDocument() ) == NULL ) if ( ( doc = getActiveDocument() ) == NULL )
return; return;
std::string rawBufferUtf8;
getRawText ( doc, rawBufferUtf8 );
if ( !XmlEncodingHandler::setUtf8 ( rawBufferUtf8 ) )
{
encodingMessage();
return;
}
WrapTempFileName tempFileName ( doc->getFullFileName() ); std::string rawBufferUtf8 = doc->myGetTextRaw();
ofstream rawBufferStream ( tempFileName.name().c_str() );
if ( !rawBufferStream )
return;
rawBufferStream << rawBufferUtf8;
rawBufferStream.close();
wxString path; wxString path;
int id = event.GetId(); int id = event.GetId();
if ( id == ID_XSLT ) if ( id == ID_XSLT )
{ {
@ -4254,7 +4186,8 @@ void MyFrame::OnXslt ( wxCommandEvent& event )
std::string stylefnameLocal = ( const char * ) path.mb_str ( wxConvLocal ); std::string stylefnameLocal = ( const char * ) path.mb_str ( wxConvLocal );
auto_ptr<WrapLibxml> wl ( new WrapLibxml ( libxmlNetAccess ) ); auto_ptr<WrapLibxml> wl ( new WrapLibxml ( libxmlNetAccess ) );
if ( !wl->xslt ( stylefnameLocal, tempFileName.name() ) ) wxString fileName = doc->getFullFileName();
if ( !wl->xslt ( path, rawBufferUtf8, fileName ) )
{ {
std::string error = wl->getLastError(); std::string error = wl->getLastError();
wxString wideError = wxString ( error.c_str(), wxConvUTF8, error.size() ); wxString wideError = wxString ( error.c_str(), wxConvUTF8, error.size() );
@ -4298,20 +4231,12 @@ void MyFrame::OnPrettyPrint ( wxCommandEvent& event )
statusProgress ( _ ( "Pretty-printing in progress..." ) ); statusProgress ( _ ( "Pretty-printing in progress..." ) );
wxString fileName = doc->getFullFileName();
auto_ptr<WrapLibxml> wl ( new WrapLibxml ( libxmlNetAccess ) );
for ( int i = 0; i < 2; i++ ) // perform two iterations for ( int i = 0; i < 2; i++ ) // perform two iterations
{ {
WrapTempFileName tempFileName ( doc->getFullFileName() );
ofstream rawBufferStream ( tempFileName.name().c_str() ); if ( !wl->parse ( rawBufferUtf8, fileName, true ) )
if ( !rawBufferStream )
return;
rawBufferStream << rawBufferUtf8;
rawBufferStream.close();
auto_ptr<WrapLibxml> wl ( new WrapLibxml ( libxmlNetAccess ) );
bool success = wl->parse ( tempFileName.name(), true );
if ( !success )
{ {
std::string error = wl->getLastError(); std::string error = wl->getLastError();
wxString wideError = wxString ( error.c_str(), wxConvUTF8, error.size() ); wxString wideError = wxString ( error.c_str(), wxConvUTF8, error.size() );