Fixed an encoding problem of file paths in prompt generator

This commit is contained in:
Zane U. Ji 2013-10-17 01:28:39 +08:00
parent 7b9701f75b
commit e366b6ec48
9 changed files with 56 additions and 55 deletions

View File

@ -72,6 +72,8 @@ InputSource *XercesCatalogResolver::resolveEntity (
if ( publicID == NULL || *publicID == '\0' ) if ( publicID == NULL || *publicID == '\0' )
return NULL; return NULL;
// Since public IDs consist of ANSII characters, it doesn't matter how
// publicID is encoded as long as it's a multibyte string
char *narrowPublicId = XMLString::transcode ( publicID ); char *narrowPublicId = XMLString::transcode ( publicID );
if ( narrowPublicId == NULL ) if ( narrowPublicId == NULL )
return NULL; return NULL;

View File

@ -2920,25 +2920,23 @@ void MyFrame::OnNew ( wxCommandEvent& WXUNUSED ( event ) )
ReadFile::run ( templateFileLocal, buffer ); ReadFile::run ( templateFileLocal, buffer );
wxString documentContents = wxString ( buffer.c_str(), wxConvUTF8, buffer.size() ); wxString documentContents = wxString ( buffer.c_str(), wxConvUTF8, buffer.size() );
newDocument ( documentContents, newDocument ( buffer, templateFile );
wxString ( templateFileLocal.c_str(), wxConvUTF8, templateFileLocal.size() ) );
} }
void MyFrame::newDocument ( const wxString& s, const wxString& path, bool canSave ) void MyFrame::newDocument ( const wxString& s, const wxString& path, bool canSave )
{ {
std::string bufferUtf8 = ( const char * ) s.mb_str ( wxConvUTF8 ); std::string bufferUtf8 = ( const char * ) s.mb_str ( wxConvUTF8 );
std::string pathUtf8 = ( const char * ) path.mb_str ( wxConvUTF8 ); newDocument ( bufferUtf8, path, canSave );
newDocument ( bufferUtf8, pathUtf8, canSave );
} }
void MyFrame::newDocument ( const std::string& s, const std::string& path, bool canSave ) void MyFrame::newDocument ( const std::string& s, const wxString& path, bool canSave )
{ {
XmlDoc *doc; XmlDoc *doc;
wxString documentLabel; wxString documentLabel;
documentLabel.Printf ( _ ( "Document%i" ), documentCount++ ); documentLabel.Printf ( _ ( "Document%i" ), documentCount++ );
std::string auxPath = getAuxPath ( path ); wxString auxPath = getAuxPath ( path );
Freeze(); Freeze();
doc = ( s.empty() ) ? doc = ( s.empty() ) ?
@ -3068,7 +3066,7 @@ bool MyFrame::openFile ( wxString& fileName, bool largeFile )
XmlDoc *doc; XmlDoc *doc;
int type = getFileType ( fileName ); int type = getFileType ( fileName );
std::string auxPath = getAuxPath ( ( const char * ) fileName.mb_str ( wxConvLocal ) ); wxString auxPath = getAuxPath ( fileName );
char *docBuffer = NULL; char *docBuffer = NULL;
size_t docBufferLen = 0; size_t docBufferLen = 0;
@ -3267,7 +3265,7 @@ bool MyFrame::openFile ( wxString& fileName, bool largeFile )
wxID_ANY, wxID_ANY,
finalBuffer, finalBuffer,
finalBufferLen, finalBufferLen,
( const char * ) fileName.mb_str ( wxConvLocal ), fileName,
auxPath ); auxPath );
#ifdef __WXMSW__ #ifdef __WXMSW__
doc->SetUndoCollection ( false ); doc->SetUndoCollection ( false );
@ -6183,26 +6181,26 @@ void MyFrame::OnDropFiles ( wxDropFilesEvent& event )
} }
#endif #endif
std::string MyFrame::getAuxPath ( const std::string& fileName ) wxString MyFrame::getAuxPath ( const wxString& fileName )
{ {
if ( fileName.find ( ".xsl" ) != std::string::npos || if ( fileName.Find ( _T ( ".xsl" ) ) != wxNOT_FOUND ||
fileName.find ( ".XSL" ) != std::string::npos ) fileName.Find ( _T ( ".XSL" ) ) != wxNOT_FOUND )
return xslDtdPath; return wxString ( xslDtdPath.c_str(), wxConvUTF8 );
else if ( fileName.find ( ".rss" ) != std::string::npos || else if ( fileName.Find ( _T ( ".rss" ) ) != wxNOT_FOUND ||
fileName.find ( ".RSS" ) != std::string::npos ) fileName.Find ( _T ( ".RSS" ) ) != wxNOT_FOUND )
return rssDtdPath; return wxString ( rssDtdPath.c_str(), wxConvUTF8 );
else if ( fileName.find ( ".xtm" ) != std::string::npos || else if ( fileName.Find ( _T ( ".xtm" ) ) != wxNOT_FOUND ||
fileName.find ( ".xtmm" ) != std::string::npos || fileName.Find ( _T ( ".xtmm" ) ) != wxNOT_FOUND ||
fileName.find ( ".XTM" ) != std::string::npos || fileName.Find ( _T ( ".XTM" ) ) != wxNOT_FOUND ||
fileName.find ( ".XTMM" ) != std::string::npos ) fileName.Find ( _T ( ".XTMM" ) ) != wxNOT_FOUND )
return xtmDtdPath; return wxString ( xtmDtdPath.c_str(), wxConvUTF8 );
else if ( fileName.find ( ".lzx" ) != std::string::npos || else if ( fileName.Find ( _T ( ".lzx" ) ) != wxNOT_FOUND ||
fileName.find ( ".LZX" ) != std::string::npos ) fileName.Find ( _T ( ".LZX" ) ) != wxNOT_FOUND )
return lzxDtdPath; return wxString ( lzxDtdPath.c_str(), wxConvUTF8 );
else if ( fileName.find ( ".xlf" ) != std::string::npos || else if ( fileName.Find ( _T ( ".xlf" ) ) != wxNOT_FOUND ||
fileName.find ( ".XLF" ) != std::string::npos ) fileName.Find ( _T ( ".XLF" ) ) != wxNOT_FOUND )
return xliffDtdPath; return wxString ( xliffDtdPath.c_str(), wxConvUTF8 );
return ""; return wxEmptyString;
} }
void MyFrame::OnActivateApp ( wxActivateEvent& event ) void MyFrame::OnActivateApp ( wxActivateEvent& event )

View File

@ -328,7 +328,7 @@ class MyFrame : public wxFrame
// public to allow access from CommandPanel // public to allow access from CommandPanel
XmlDoc *getActiveDocument(); XmlDoc *getActiveDocument();
void newDocument ( const wxString& s, const wxString& path = wxEmptyString, bool canSave = false ); void newDocument ( const wxString& s, const wxString& path = wxEmptyString, bool canSave = false );
void newDocument ( const std::string& s, const std::string& path = "", bool canSave = false ); void newDocument ( const std::string& s, const wxString& path = wxEmptyString, bool canSave = false );
void statusProgress ( const wxString& s ); void statusProgress ( const wxString& s );
// public to allow InsertPanel access // public to allow InsertPanel access
@ -523,7 +523,7 @@ class MyFrame : public wxFrame
bool ignoreEncoding = false, bool ignoreEncoding = false,
bool isXml = true ); bool isXml = true );
void removeUtf8Bom ( std::string& buffer ); void removeUtf8Bom ( std::string& buffer );
std::string getAuxPath ( const std::string& fileName ); wxString getAuxPath ( const wxString& fileName );
wxMenuBar *getMenuBar(); wxMenuBar *getMenuBar();
wxToolBar *getToolBar(); wxToolBar *getToolBar();

View File

@ -55,8 +55,8 @@ XmlCtrl::XmlCtrl (
wxWindowID id, wxWindowID id,
const char *buffer, // could be NULL const char *buffer, // could be NULL
size_t bufferLen, size_t bufferLen,
const std::string& basePathParameter, const wxString& basePathParameter,
const std::string& auxPathParameter, const wxString& auxPathParameter,
const wxPoint& position, const wxPoint& position,
const wxSize& size, const wxSize& size,
long style long style
@ -1954,7 +1954,7 @@ bool XmlCtrl::backgroundValidate()
return backgroundValidate ( return backgroundValidate (
bufferUtf8.c_str(), bufferUtf8.c_str(),
basePath.c_str(), basePath.mb_str(),
bufferUtf8.size() ); bufferUtf8.size() );
} }

View File

@ -102,8 +102,8 @@ class XmlCtrl: public wxStyledTextCtrl
wxWindowID id = wxID_ANY, wxWindowID id = wxID_ANY,
const char *buffer = NULL, const char *buffer = NULL,
size_t bufferLen = 0, size_t bufferLen = 0,
const std::string& basePath = "", const wxString& basePath = wxEmptyString,
const std::string& auxPath = "", const wxString& auxPath = wxEmptyString,
const wxPoint &position = wxDefaultPosition, const wxPoint &position = wxDefaultPosition,
const wxSize& size = wxDefaultSize, const wxSize& size = wxDefaultSize,
long style = 0 ); long style = 0 );
@ -161,7 +161,7 @@ class XmlCtrl: public wxStyledTextCtrl
std::map<wxString, std::set<wxString> > elementMap; std::map<wxString, std::set<wxString> > elementMap;
std::set<wxString> entitySet; std::set<wxString> entitySet;
std::map<wxString, wxString> elementStructureMap; std::map<wxString, wxString> elementStructureMap;
std::string basePath, auxPath; wxString basePath, auxPath;
XmlCtrlProperties properties; XmlCtrlProperties properties;
wxString getLastAttributeName ( int pos ); wxString getLastAttributeName ( int pos );
int getAttributeStartPos ( int pos ); int getAttributeStartPos ( int pos );

View File

@ -30,8 +30,8 @@ XmlDoc::XmlDoc (
//const std::string& buffer, //const std::string& buffer,
const char *buffer, const char *buffer,
size_t bufferLen, size_t bufferLen,
const std::string& basePath, const wxString& basePath,
const std::string& auxPath, const wxString& auxPath,
const wxPoint &position, const wxPoint &position,
const wxSize& size, const wxSize& size,
long style ) long style )

View File

@ -38,8 +38,8 @@ class XmlDoc : public XmlCtrl
//const std::string& buffer = DEFAULT_XML_DECLARATION_UTF8, //const std::string& buffer = DEFAULT_XML_DECLARATION_UTF8,
const char *buffer = NULL, const char *buffer = NULL,
size_t bufferLen = 0, size_t bufferLen = 0,
const std::string& basePath = "", const wxString& basePath = wxEmptyString,
const std::string& auxPath = "", const wxString& auxPath = wxEmptyString,
const wxPoint& position = wxDefaultPosition, const wxPoint& position = wxDefaultPosition,
const wxSize& size = wxDefaultSize, const wxSize& size = wxDefaultSize,
long style = 0 ); long style = 0 );

View File

@ -44,8 +44,8 @@
using namespace xercesc; using namespace xercesc;
XmlPromptGenerator::XmlPromptGenerator ( XmlPromptGenerator::XmlPromptGenerator (
const std::string& basePath, const wxString& basePath,
const std::string& auxPath ) : d ( new PromptGeneratorData() ) const wxString& auxPath ) : d ( new PromptGeneratorData() )
{ {
XML_SetUserData ( p, d.get() ); XML_SetUserData ( p, d.get() );
d->p = p; d->p = p;
@ -62,7 +62,7 @@ XmlPromptGenerator::XmlPromptGenerator (
XML_SetEntityDeclHandler ( p, entitydeclhandler ); XML_SetEntityDeclHandler ( p, entitydeclhandler );
XML_SetExternalEntityRefHandlerArg ( p, d.get() ); XML_SetExternalEntityRefHandlerArg ( p, d.get() );
XML_SetExternalEntityRefHandler ( p, externalentityrefhandler ); XML_SetExternalEntityRefHandler ( p, externalentityrefhandler );
XML_SetBase ( p, basePath.c_str() ); XML_SetBase ( p, d->basePath.utf8_str() );
if ( !auxPath.empty() ) if ( !auxPath.empty() )
XML_UseForeignDTD ( p, true ); XML_UseForeignDTD ( p, true );
@ -303,17 +303,17 @@ int XMLCALL XmlPromptGenerator::externalentityrefhandler (
// auxPath req'd? // auxPath req'd?
if ( !systemId && !publicId ) if ( !systemId && !publicId )
{ {
ReadFile::run ( d->auxPath, buffer ); ReadFile::run ( ( const char * ) d->auxPath.mb_str(), buffer );
if ( buffer.empty() ) if ( buffer.empty() )
{ {
return XML_STATUS_ERROR; return XML_STATUS_ERROR;
} }
std::string encoding = XmlEncodingHandler::get ( buffer ); d->encoding = XmlEncodingHandler::get ( buffer );
XML_Parser dtdParser = XML_ExternalEntityParserCreate ( d->p, context, encoding.c_str() ); XML_Parser dtdParser = XML_ExternalEntityParserCreate ( d->p, context, d->encoding.c_str() );
if ( !dtdParser ) if ( !dtdParser )
return XML_STATUS_ERROR; return XML_STATUS_ERROR;
XML_SetBase ( dtdParser, d->auxPath.c_str() ); XML_SetBase ( dtdParser, d->auxPath.utf8_str() );
ret = XML_Parse ( dtdParser, buffer.c_str(), buffer.size(), true ); ret = XML_Parse ( dtdParser, buffer.c_str(), buffer.size(), true );
XML_ParserFree ( dtdParser ); XML_ParserFree ( dtdParser );
return ret; return ret;
@ -395,13 +395,13 @@ void XmlPromptGenerator::handleSchema (
return; return;
// first check for XML Schema association // first check for XML Schema association
const char **schemaAttr = ( const char ** ) attr; // now redundant; could use attr const char **schemaAttr = ( const char ** ) attr; // now redundant; could use attr
std::string path; wxString path;
for ( ; *schemaAttr; schemaAttr += 2 ) for ( ; *schemaAttr; schemaAttr += 2 )
{ {
// no namespace // no namespace
if ( !strcmp ( *schemaAttr, "xsi:noNamespaceSchemaLocation" ) ) if ( !strcmp ( *schemaAttr, "xsi:noNamespaceSchemaLocation" ) )
{ {
path = * ( schemaAttr + 1 ); path = wxString ( schemaAttr[1], wxConvUTF8 );
break; break;
} }
// with namespace -- check if this works // with namespace -- check if this works
@ -412,7 +412,7 @@ void XmlPromptGenerator::handleSchema (
searchIterator++; searchIterator++;
if ( *searchIterator ) if ( *searchIterator )
{ {
path = searchIterator + 1; path = wxString ( searchIterator + 1, wxConvUTF8 );
break; break;
} }
} }
@ -422,16 +422,16 @@ void XmlPromptGenerator::handleSchema (
{ {
return; return;
} }
std::string schemaPath = PathResolver::run ( path, ( d->auxPath.empty() ) ? d->basePath : d->auxPath);
wxString schemaPath = PathResolver::run ( path, ( d->auxPath.empty() ) ? d->basePath : d->auxPath);
std::auto_ptr<XercesDOMParser> parser ( new XercesDOMParser() ); std::auto_ptr<XercesDOMParser> parser ( new XercesDOMParser() );
parser->setDoNamespaces ( true ); parser->setDoNamespaces ( true );
parser->setDoSchema ( true ); parser->setDoSchema ( true );
parser->setValidationSchemaFullChecking ( true ); parser->setValidationSchemaFullChecking ( true );
Grammar *rootGrammar = parser->loadGrammar ( schemaPath.c_str(), Grammar::SchemaGrammarType ); Grammar *rootGrammar = parser->loadGrammar ( schemaPath.mb_str(), Grammar::SchemaGrammarType );
if ( !rootGrammar ) if ( !rootGrammar )
{ {
return; return;

View File

@ -38,7 +38,8 @@ struct PromptGeneratorData : public ParserData
std::map<wxString, std::set<wxString> > requiredAttributeMap; std::map<wxString, std::set<wxString> > requiredAttributeMap;
std::map<wxString, wxString> elementStructureMap; std::map<wxString, wxString> elementStructureMap;
std::set<wxString> entitySet; std::set<wxString> entitySet;
std::string basePath, auxPath, rootElement; wxString basePath, auxPath;
std::string encoding, rootElement;
bool isRootElement, grammarFound; bool isRootElement, grammarFound;
unsigned attributeValueCutoff; unsigned attributeValueCutoff;
XML_Parser p; XML_Parser p;
@ -51,8 +52,8 @@ class XmlPromptGenerator : public WrapExpat
{ {
public: public:
XmlPromptGenerator ( XmlPromptGenerator (
const std::string& basePath = "", const wxString& basePath = wxEmptyString,
const std::string& auxPath = "" ); const wxString& auxPath = wxEmptyString );
virtual ~XmlPromptGenerator(); virtual ~XmlPromptGenerator();
void getAttributeMap ( void getAttributeMap (
std::map<wxString, std::map<wxString, std::set<wxString> > > std::map<wxString, std::map<wxString, std::set<wxString> > >