Don't use XMLString::transcode to convert a name to native code-page Return constant references rather than objects

This commit is contained in:
Zane U. Ji 2012-08-10 21:34:13 +08:00
parent 9a9cf0e724
commit 9a3a9c6280
14 changed files with 206 additions and 297 deletions

View File

@ -100,15 +100,10 @@ void InsertPanel::update (
{
list->Clear();
lastDoc = doc;
std::set<std::string> entitySet = doc->getEntitySet();
entitySet.insert ( "amp" );
entitySet.insert ( "apos" );
entitySet.insert ( "gt" );
entitySet.insert ( "lt" );
entitySet.insert ( "quot" );
std::set<std::string>::iterator it;
const std::set<wxString> &entitySet = doc->getEntitySet();
std::set<wxString>::const_iterator it;
for ( it = entitySet.begin(); it != entitySet.end(); it++ )
list->Append ( wxString ( it->c_str(), wxConvUTF8, it->size() ) );
list->Append ( *it );
list->Show ( true );
wxSize clientSize = GetClientSize();
@ -140,14 +135,14 @@ void InsertPanel::update (
return;
}
std::set<wxString> elementSet;
elementSet = doc->getChildren ( ( type == INSERT_PANEL_TYPE_SIBLING ) ? grandparent : parent );
const std::set<wxString> &elementSet = doc->getChildren (
( type == INSERT_PANEL_TYPE_SIBLING ) ? grandparent : parent );
if ( elementSet.empty() )
{
list->Show ( false );
return;
}
std::set<wxString>::iterator it;
std::set<wxString>::const_iterator it;
for ( it = elementSet.begin(); it != elementSet.end(); it++ )
list->Append ( *it );
list->Show ( true );

View File

@ -82,15 +82,14 @@ void LocationPanel::update (
}
else
{
std::string structure = doc->getElementStructure ( parent );
wxString structure = doc->getElementStructure ( parent );
if (!structure.empty () )
{
indentStructure( structure );
structureEdit->Show ( true );
wxString wideStructure = wxString ( structure.c_str(), wxConvUTF8, structure.size() );
structureEdit->SetReadOnly ( false );
structureEdit->SetText ( wideStructure );
structureEdit->SetText ( structure );
structureEdit->SetReadOnly ( true );
wxSize clientSize = GetClientSize();
@ -115,12 +114,12 @@ void LocationPanel::update (
}
void LocationPanel::indentStructure ( std::string& structure )
void LocationPanel::indentStructure ( wxString& structure )
{
std::string indented;
char *s = (char *) structure.c_str();
wxString indented;
wxString::const_iterator s = structure.begin();
int indent = 0;
const char *indentMark = "\t";
const static wxString indentMark ( _T("\t") );
int count = 0;
bool justSeenContent = false;
@ -136,7 +135,7 @@ void LocationPanel::indentStructure ( std::string& structure )
indented += *s;
for ( int i = 0; i < indent; i++ )
{
indented += (char *)indentMark;
indented += indentMark;
}
if (justSeenContent)
indented += *s;
@ -146,7 +145,7 @@ void LocationPanel::indentStructure ( std::string& structure )
indented += '\n';
for (int i = 0; indent && i < indent; i++)
indented += (char *)indentMark;
indented += indentMark;
justSeenContent = false;
}
else if (*s == ')')
@ -157,13 +156,13 @@ void LocationPanel::indentStructure ( std::string& structure )
}
indent--;
for (int i = 0; indent && i < indent; i++)
indented += (char *)indentMark;
indented += indentMark;
indented += *s;
indented += '\n';
if (*( s + 1 ) && *(s + 1) != ')' )
{
for (int i = 0; i < indent; i++)
indented += (char *)indentMark;
indented += indentMark;
}
justSeenContent = false;
}

View File

@ -34,7 +34,7 @@ class LocationPanel : public wxPanel
XmlDoc *docParameter = NULL,
const wxString& parent = wxEmptyString );
private:
void indentStructure ( std::string& structure );
void indentStructure ( wxString& structure );
MyFrame *parentWindow;
XmlDoc *doc;
wxBoxSizer *sizer;

View File

@ -14,7 +14,7 @@ ValidationThread::ValidationThread (
bool *success,
bool *release,
std::pair<int, int> *position,
std::string *message ) : wxThread()
wxString *message ) : wxThread()
{
if (!buffer || !success || !position || !message )
{
@ -69,7 +69,7 @@ void *ValidationThread::Entry()
{
*mySuccessPtr = true;
*myPositionPtr = std::make_pair ( 0, 0 );
*myMessagePtr = "";
*myMessagePtr = wxEmptyString;
}
}
return NULL;

View File

@ -1,6 +1,7 @@
#ifndef VALIDATION_THREAD_H
#define VALIDATION_THREAD_H
#include <wx/wx.h>
#include <utility>
#include <string>
#include <wx/thread.h>
@ -16,14 +17,14 @@ public:
bool *finished,
bool *success,
bool *release, std::pair<int, int> *position,
std::string *message );
wxString *message );
virtual void *Entry();
virtual void OnExit();
private:
std::string myBuffer, mySystem, myCatalogPath, myCatalogUtilityPath;
bool *myFinishedPtr, *mySuccessPtr, *myReleasePtr;
std::pair<int, int> *myPositionPtr;
std::string *myMessagePtr;
wxString *myMessagePtr;
};
#endif

View File

@ -80,26 +80,22 @@ bool WrapXerces::validate ( const std::string& fileName )
catch ( XMLException& e )
{
delete parser;
char *err = XMLString::transcode ( e.getMessage() );
lastError = err;
XMLString::release ( &err );
lastError = toString ( e.getMessage() );
return false;
}
catch ( SAXParseException& e )
{
delete parser;
char *err = XMLString::transcode ( e.getMessage() );
std::stringstream ss;
ss << "Validation stopped at line " << e.getLineNumber() << ", column " << e.getColumnNumber() << ": " << err;
lastError = ss.str();
lastError << _T ( "Validation stopped at line " )
<< e.getLineNumber() << _T ( ", column " ) << e.getColumnNumber()
<< ": " << toString ( e.getMessage() );
errorPosition = std::make_pair ( e.getLineNumber(), e.getColumnNumber() );
XMLString::release ( &err );
return false;
}
catch ( ... )
{
delete parser;
lastError = "Unexpected validation error";
lastError = _T ( "Unexpected validation error" );
return false;
}
delete parser;
@ -142,38 +138,33 @@ bool WrapXerces::validateMemory (
catch ( XMLException& e )
{
delete parser;
lastError = "";
lastError = wxEmptyString;
return false;
}
catch ( SAXParseException& e )
{
delete parser;
char *err = XMLString::transcode ( e.getMessage() );
std::stringstream ss;
ss << "Ln " << e.getLineNumber() << " Col " << e.getColumnNumber() << ": " << err;
lastError = ss.str();
lastError << _T ( "Ln " ) << e.getLineNumber() << _T ( " Col " )
<< e.getColumnNumber() << _T ( ": " ) << toString ( e.getMessage() );
errorPosition = std::make_pair ( e.getLineNumber(), e.getColumnNumber() );
XMLString::release ( &err );
return false;
}
catch ( ... )
{
delete parser;
lastError = "";
lastError = wxEmptyString;
return false;
}
delete parser;
return true;
}
std::string WrapXerces::getLastError()
const wxString &WrapXerces::getLastError()
{
char *rawError, *it;
rawError = (char *)lastError.c_str();
it = strstr ( rawError, "Message:" );
if ( it )
int i = lastError.Find( _T ( "Message:" ) );
if ( i != wxNOT_FOUND )
{
lastError = it + 8;
lastError = lastError.substr( i );
}
return lastError;
@ -183,3 +174,10 @@ std::pair<int, int> WrapXerces::getErrorPosition()
{
return errorPosition;
}
wxString WrapXerces::toString ( const XMLCh *str )
{
static wxMBConvUTF16 conv;
return wxString ( ( const char * ) str, conv );
}

View File

@ -21,6 +21,7 @@
#define WRAP_XERCES
#define XERCES_TMPLSINC
#include <wx/wx.h>
#include <string>
#include <utility>
#include <xercesc/sax2/SAX2XMLReader.hpp>
@ -37,11 +38,12 @@ class WrapXerces
~WrapXerces();
bool validate ( const std::string& fileName );
bool validateMemory ( const char *buffer, const char *system, unsigned len );
std::string getLastError();
const wxString &getLastError();
std::pair<int, int> getErrorPosition();
static wxString toString ( const XMLCh *str );
private:
XercesCatalogResolver *catalogResolver;
std::string lastError;
wxString lastError;
std::pair<int, int> errorPosition;
};

View File

@ -1296,10 +1296,8 @@ void MyFrame::OnCheckWellformedness ( wxCommandEvent& event )
auto_ptr<WrapExpat> we ( new WrapExpat() );
if ( !we->parse ( utf8Buffer.c_str() ) )
{
std::string error = we->getLastError();
wxString werror = wxString ( error.c_str(), wxConvUTF8, error.size() );
statusProgress ( wxEmptyString );
messagePane ( werror, CONST_WARNING );
messagePane ( we->getLastError(), CONST_WARNING );
std::pair<int, int> posPair = we->getErrorPosition();
-- ( posPair.first );
int cursorPos =
@ -3944,9 +3942,6 @@ void MyFrame::OnValidateSchema ( wxCommandEvent& event )
statusProgress ( _ ( "Validation in progress..." ) );
doc->clearErrorIndicators();
std::string error;
wxString wideError;
std::auto_ptr<WrapXerces> validator (
new WrapXerces( catalogPath, catalogUtilityPath )
);
@ -3954,9 +3949,7 @@ void MyFrame::OnValidateSchema ( wxCommandEvent& event )
if ( !validator->validate ( fileNameLocal ) )
{
statusProgress ( wxEmptyString );
error = validator->getLastError();
wideError = wxString ( error.c_str(), wxConvUTF8, error.size() );
messagePane ( wideError, CONST_WARNING );
messagePane ( validator->getLastError(), CONST_WARNING );
std::pair<int, int> posPair = validator->getErrorPosition();
int cursorPos =
@ -4675,11 +4668,9 @@ bool MyFrame::saveFile ( XmlDoc *doc, wxString& fileName, bool checkLastModified
if ( !we->parse ( utf8Buffer ) )
{
std::string error = we->getLastError();
wxString werror = wxString ( error.c_str(), wxConvUTF8, error.size() );
if ( we->isEncodingError() )
;
messagePane ( werror, CONST_WARNING );
//if ( we->isEncodingError() )
// ;
messagePane ( we->getLastError(), CONST_WARNING );
}
success = saveRawUtf8 ( fileNameLocal, utf8Buffer, true, isXml );
if ( success )
@ -5842,15 +5833,12 @@ void MyFrame::OnAssociate ( wxCommandEvent& event )
std::auto_ptr<WrapExpat> wellformedparser ( new WrapExpat() );
if ( !wellformedparser->parse ( utf8Buffer ) )
{
std::string error = wellformedparser->getLastError();
wxString wideError = wxString ( error.c_str(), wxConvUTF8, error.size() );
wxString message;
message.Printf (
_ ( "Cannot associate %s: %s" ),
type.c_str(),
wideError.c_str() );
messagePane ( message,
CONST_STOP );
wellformedparser->getLastError() );
messagePane ( message, CONST_STOP );
return;
}

View File

@ -187,7 +187,7 @@ void XmlCtrl::OnIdle ( wxIdleEvent& event )
{
clearErrorIndicators ( GetLineCount() );
setErrorIndicator ( validationPosition.first - 1, 0 );
frame->statusProgress ( wxString ( validationMessage.c_str(), wxConvUTF8, validationMessage.size() ) );
frame->statusProgress ( validationMessage );
}
}
}
@ -486,15 +486,14 @@ void XmlCtrl::handleOpenAngleBracket ( wxKeyEvent& event )
if ( elementMap.find ( parent ) == elementMap.end() )
return;
wxString choice, conversion;
std::set<std::string> childSet = elementMap[parent];
std::set<std::string>::iterator it;
wxString choice;
std::set<wxString> &childSet = elementMap[parent];
std::set<wxString>::iterator it;
for ( it = childSet.begin(); it != childSet.end(); it++ )
{
if ( !choice.empty() )
choice.append ( _T ( "<" ) );
conversion = wxString ( it->c_str(), wxConvUTF8, it->size() );
choice.append ( conversion );
choice.append ( *it );
}
if ( !choice.empty() )
UserListShow ( 0, choice );
@ -520,9 +519,7 @@ void XmlCtrl::handleCloseAngleBracket ( wxKeyEvent& event )
wxString elementName = getLastElementName ( pos );
if ( !elementName.empty() )
{
std::map<std::string, std::set<std::string> > map;
std::string elementNameUtf8 = ( const char * ) elementName.mb_str ( wxConvUTF8 );
attributeMap.insert ( make_pair ( elementNameUtf8, map ) );
attributeMap[elementName]; // Just want to put it there
}
// exit condition 1
@ -571,8 +568,6 @@ void XmlCtrl::handleEquals ( wxKeyEvent& event )
if ( AutoCompActive() )
AutoCompCancel();
wxString choice, elementName, attributeName, conversion;
std::string elementNameUtf8, attributeNameUtf8;
int pos = GetCurrentPos();
if ( pos <= 0 || getLexerStyleAt ( pos - 1 ) != wxSTC_H_ATTRIBUTE )
{
@ -583,19 +578,15 @@ void XmlCtrl::handleEquals ( wxKeyEvent& event )
SetSelection ( pos + 2, pos + 2 );
// tbd: identify possible attribute values
wxString choice, elementName, attributeName;
elementName = getLastElementName ( pos );
attributeName = getLastAttributeName ( pos );
elementNameUtf8 = elementName.mb_str ( wxConvUTF8 );
attributeNameUtf8 = attributeName.mb_str ( wxConvUTF8 );
std::set<std::string> valueSet;
valueSet = attributeMap[elementNameUtf8][attributeNameUtf8];
std::set<wxString> &valueSet = attributeMap[elementName][attributeName];
if ( valueSet.empty() )
return;
std::set<std::string>::iterator valueSetIterator;
std::set<wxString>::iterator valueSetIterator;
int cutoff = BUFSIZ;
for ( valueSetIterator = valueSet.begin();
valueSetIterator != valueSet.end();
@ -605,11 +596,7 @@ void XmlCtrl::handleEquals ( wxKeyEvent& event )
break;
if ( !choice.empty() )
choice.Append ( _T ( "<" ) );
conversion = wxString (
valueSetIterator->c_str(),
wxConvUTF8,
valueSetIterator->size() );
choice.Append ( conversion );
choice.Append ( *valueSetIterator );
}
if ( !choice.empty() )
@ -621,12 +608,7 @@ void XmlCtrl::handleSpace ( wxKeyEvent& event )
if ( AutoCompActive() )
AutoCompCancel();
wxString elementName, choice, conversion;
std::string elementNameUtf8;
int pos = GetCurrentPos();
std::map<std::string, std::set<std::string> > currentAttributeMap;
std::map<std::string, std::set<std::string> >::iterator
attributeIterator;
if ( pos <= 2 )
{
event.Skip();
@ -665,30 +647,24 @@ void XmlCtrl::handleSpace ( wxKeyEvent& event )
}
AddText ( _T ( " " ) );
elementName = getLastElementName ( pos );
wxString tag = GetTextRange ( tagStartPos, pos );
elementNameUtf8 = elementName.mb_str ( wxConvUTF8 );
if ( attributeMap.find ( elementNameUtf8 ) == attributeMap.end() )
wxString elementName = getLastElementName ( pos );
if ( attributeMap.find ( elementName ) == attributeMap.end() )
return;
currentAttributeMap = attributeMap[elementNameUtf8];
for (
attributeIterator = currentAttributeMap.begin();
attributeIterator != currentAttributeMap.end();
attributeIterator++ )
{
conversion = wxString (
attributeIterator->first.c_str(),
wxConvUTF8,
attributeIterator->first.size() );
wxString choice;
wxString tag = GetTextRange ( tagStartPos, pos );
std::map<wxString, std::set<wxString> > &curAttMap = attributeMap[elementName];
std::map<wxString, std::set<wxString> >::iterator it;
for ( it = curAttMap.begin(); it != curAttMap.end(); it++ )
{
// avoid duplicate attributes
if ( tag.Contains ( conversion + _T ( "=" ) ) )
if ( tag.Contains ( it->first + _T ( "=" ) ) )
continue;
if ( !choice.empty() )
choice.Append ( _T ( "<" ) );
choice.Append ( conversion );
choice.Append ( it->first );
}
if ( !choice.empty() )
{
@ -720,14 +696,13 @@ void XmlCtrl::handleAmpersand ( wxKeyEvent& event )
{
AddText ( _T ( "&" ) );
wxString choice;
std::set<std::string>::iterator it;
it = entitySet.begin();
choice += wxString ( it->c_str(), wxConvUTF8, it->size() );
std::set<wxString>::iterator it = entitySet.begin();
choice += *it;
choice += _T ( ";" );
for ( it++; it != entitySet.end(); it++ )
{
choice += _T ( "<" );
choice += wxString ( it->c_str(), wxConvUTF8, it->size() );
choice += *it;
choice += _T ( ";" );
}
UserListShow ( 0, choice );
@ -967,26 +942,13 @@ wxString XmlCtrl::getLastElementName ( int pos )
return GetTextRange ( startPos, iteratorPos );
}
std::set<wxString> XmlCtrl::getChildren ( const wxString& parent )
const std::set<wxString> &XmlCtrl::getChildren ( const wxString& parent )
{
std::string parentUtf8 = ( const char * ) parent.mb_str ( wxConvUTF8 );
std::set<wxString> mySet;
if ( elementMap.find ( parentUtf8 ) == elementMap.end() )
static std::set<wxString> mySet;
if ( elementMap.find ( parent ) == elementMap.end() )
return mySet;
std::set<std::string> myUtf8Set = elementMap[parentUtf8];
std::string currentUtf8;
wxString currentWide;
std::set<std::string>::iterator it;
for ( it = myUtf8Set.begin(); it != myUtf8Set.end(); it++ )
{
currentUtf8 = *it;
currentWide = wxString ( currentUtf8.c_str(), wxConvUTF8, currentUtf8.size() );
mySet.insert ( currentWide );
}
return mySet;
return elementMap[parent];
}
wxString XmlCtrl::getLastAttributeName ( int pos )
@ -1095,11 +1057,11 @@ void XmlCtrl::updatePromptMaps ( const char *buffer, size_t bufferLen )
xpg->getElementStructureMap ( elementStructureMap );
xpg->getEntitySet ( entitySet );
grammarFound = xpg->getGrammarFound();
entitySet.insert ( "amp" );
entitySet.insert ( "apos" );
entitySet.insert ( "quot" );
entitySet.insert ( "lt" );
entitySet.insert ( "gt" );
entitySet.insert ( _T ( "amp" ) );
entitySet.insert ( _T ( "apos" ) );
entitySet.insert ( _T ( "quot" ) );
entitySet.insert ( _T ( "lt" ) );
entitySet.insert ( _T ( "gt" ) );
}
void XmlCtrl::applyProperties (
@ -1823,16 +1785,15 @@ wxString XmlCtrl::getOpenTag ( const wxString& element )
{
wxString openTag;
openTag = _T ( "<" ) + element;
std::set<std::string> requiredAttributeSet;
std::set<std::string>::iterator it;
std::string key = ( const char * ) element.mb_str ( wxConvUTF8 );
requiredAttributeSet = requiredAttributeMap[key];
std::set<wxString> requiredAttributeSet;
std::set<wxString>::iterator it;
requiredAttributeSet = requiredAttributeMap[element];
if ( !requiredAttributeSet.empty() )
{
for ( it = requiredAttributeSet.begin(); it != requiredAttributeSet.end(); it++ )
{
openTag += _T ( " " );
openTag += wxString ( it->c_str(), wxConvUTF8, it->size() );
openTag += *it;
openTag += _T ( "=\"\"" );
}
}
@ -1970,31 +1931,24 @@ void XmlCtrl::toggleLineBackground()
SetCaretLineBackground ( ( lineBackgroundState == BACKGROUND_STATE_NORMAL ) ? baseBackground : alternateBackground );
}
std::set<std::string> XmlCtrl::getEntitySet()
const std::set<wxString> &XmlCtrl::getEntitySet()
{
return entitySet;
}
std::set<std::string> XmlCtrl::getAttributes ( const wxString& parent )
const std::set<std::string> &XmlCtrl::getAttributes ( const wxString& parent )
{
std::set<std::string> retVal;
static std::set<std::string> retVal;
return retVal;
}
std::string XmlCtrl::getElementStructure ( const wxString& element )
wxString XmlCtrl::getElementStructure ( const wxString& element )
{
std::string stdElement, ret;
stdElement = element.mb_str ( wxConvUTF8);
if ( elementStructureMap.find ( stdElement ) == elementStructureMap.end() )
if ( elementStructureMap.find ( element ) == elementStructureMap.end() )
{
ret = "";
return wxEmptyString;
}
else
{
ret = elementStructureMap[stdElement];
}
return ret;
return elementStructureMap[element];
}
bool XmlCtrl::backgroundValidate()

View File

@ -130,10 +130,10 @@ class XmlCtrl: public wxStyledTextCtrl
void clearErrorIndicators ( int maxLine = 0 );
wxString getParent();
wxString getLastElementName ( int pos );
std::set<wxString> getChildren ( const wxString& parent );
std::set<std::string> getEntitySet();
std::set<std::string> getAttributes ( const wxString& parent );
std::string getElementStructure ( const wxString& parent );
const std::set<wxString> &getChildren ( const wxString& parent );
const std::set<wxString> &getEntitySet();
const std::set<std::string> &getAttributes ( const wxString& parent );
wxString getElementStructure ( const wxString& parent );
bool canInsertAt ( int pos );
int getTagStartPos ( int pos );
void toggleLineBackground();
@ -153,7 +153,7 @@ class XmlCtrl: public wxStyledTextCtrl
validationSuccess;
bool *validationReleasePtr;
std::pair<int, int> validationPosition;
std::string validationMessage;
wxString validationMessage;
int type;
bool *protectTags;
@ -163,12 +163,12 @@ class XmlCtrl: public wxStyledTextCtrl
int currentMaxLine;
int lineBackgroundState;
wxColour baseBackground, alternateBackground;
std::map<std::string, std::map<std::string, std::set<std::string> > >
std::map<wxString, std::map<wxString, std::set<wxString> > >
attributeMap;
std::map<std::string, std::set<std::string> > requiredAttributeMap;
std::map<std::string, std::set<std::string> > elementMap;
std::set<std::string> entitySet;
std::map<std::string, std::string> elementStructureMap;
std::map<wxString, std::set<wxString> > requiredAttributeMap;
std::map<wxString, std::set<wxString> > elementMap;
std::set<wxString> entitySet;
std::map<wxString, wxString> elementStructureMap;
std::string catalogPath, catalogUtilityPath, basePath, auxPath;
XmlCtrlProperties properties;
wxString getLastAttributeName ( int pos );

View File

@ -42,7 +42,7 @@
#include <xercesc/validators/schema/SchemaValidator.hpp>
#include <xercesc/validators/common/ContentSpecNode.hpp>
#include <xercesc/validators/schema/SchemaSymbols.hpp>
//#include "wrapxerces.h"
#include "wrapxerces.h" // Delearation of toString()
using namespace xercesc;
@ -99,34 +99,19 @@ void XMLCALL XmlPromptGenerator::starthandler (
d->push ( el );
std::string parent, element;
parent = d->getParent();
element = el;
wxString parent ( d->getParent().c_str(), wxConvUTF8 );
wxString element ( el, wxConvUTF8 );
// update elementMap
if ( d->elementMap.find ( parent ) == d->elementMap.end() )
{
std::set<std::string> childSet;
childSet.insert ( element );
d->elementMap.insert ( make_pair ( parent, childSet ) );
}
else
d->elementMap[parent].insert ( element );
d->elementMap[parent].insert ( element );
std::string attributeName, attributeValue;
wxString attributeName, attributeValue;
// update attributeMap
// case 1: element unknown, no attributes
if ( ! ( *attr ) && d->attributeMap.find ( element ) == d->attributeMap.end() )
{
std::map<std::string, std::set<std::string> > currentAttributeMap;
d->attributeMap.insert ( make_pair ( element, currentAttributeMap ) );
}
for ( ; *attr; attr += 2 )
{
attributeName = *attr;
attributeValue = * ( attr + 1 );
attributeName = wxString ( *attr, wxConvUTF8 );
attributeValue = wxString ( * ( attr + 1 ), wxConvUTF8 );
if (d->attributeMap[element][attributeName].size() < d->attributeValueCutoff)
d->attributeMap[element][attributeName].insert ( attributeValue );
@ -146,32 +131,32 @@ bool XmlPromptGenerator::getGrammarFound()
}
void XmlPromptGenerator::getAttributeMap (
std::map<std::string, std::map<std::string, std::set<std::string> > >
std::map<wxString, std::map<wxString, std::set<wxString> > >
&attributeMap )
{
attributeMap = d->attributeMap;
}
void XmlPromptGenerator::getRequiredAttributeMap (
std::map<std::string, std::set<std::string> >& requiredAttributeMap )
std::map<wxString, std::set<wxString> >& requiredAttributeMap )
{
requiredAttributeMap = d->requiredAttributeMap;
}
void XmlPromptGenerator::getElementMap (
std::map<std::string, std::set<std::string> > &elementMap )
std::map<wxString, std::set<wxString> > &elementMap )
{
elementMap = d->elementMap;
}
void XmlPromptGenerator::getEntitySet (
std::set<std::string> &entitySet )
std::set<wxString> &entitySet )
{
entitySet = d->entitySet;
}
void XmlPromptGenerator::getElementStructureMap (
std::map<std::string, std::string> &elementStructureMap )
std::map<wxString, wxString> &elementStructureMap )
{
elementStructureMap = d->elementStructureMap;
}
@ -208,60 +193,59 @@ void XMLCALL XmlPromptGenerator::elementdeclhandler (
PromptGeneratorData *d;
d = ( PromptGeneratorData * ) data;
std::string myElement = name;
wxString myElement ( name, wxConvUTF8 );
std::set<std::string> children;
getContent ( *model, d->elementStructureMap[myElement], children );
if ( !children.empty() )
d->elementMap[myElement] = children;
getContent ( *model, d->elementStructureMap[myElement], d->elementMap[myElement] );
XML_FreeContentModel ( d->p, model );
}
void XmlPromptGenerator::getContent (
const XML_Content &content,
std::string &contentModel,
std::set<std::string> &list )
wxString &contentModel,
std::set<wxString> &list )
{
wxString name;
switch ( content.type )
{
case XML_CTYPE_EMPTY:
contentModel += "EMPTY";
contentModel += _T("EMPTY");
return;
case XML_CTYPE_ANY:
contentModel += "ANY";
contentModel += _T("ANY");
return;
case XML_CTYPE_NAME:
list.insert ( content.name );
contentModel += content.name;
name = wxString ( content.name, wxConvUTF8 );
list.insert ( name );
contentModel += name;
break;
case XML_CTYPE_CHOICE:
case XML_CTYPE_SEQ:
case XML_CTYPE_MIXED:
default:
std::string sep;
sep = ( content.type == XML_CTYPE_CHOICE ) ? "|" : ",";//_T("|") : _T(",");
contentModel += ( content.type == XML_CTYPE_MIXED ) ? "(#PCDATA|" : "(";
wxString sep;
sep = ( content.type == XML_CTYPE_CHOICE ) ? _T("|") : _T(",");
contentModel += ( content.type == XML_CTYPE_MIXED ) ? _T("(#PCDATA|") : _T("(");
for ( unsigned i = 0; i < content.numchildren; i++ )
{
if ( i > 0 )
contentModel += sep;
getContent ( content.children[i], contentModel, list);
}
contentModel += ")";//_T(")");
contentModel += _T(")");
break;
}
switch ( content.quant )
{
case XML_CQUANT_OPT:
contentModel += "?";//_T("?");
contentModel += _T("?");
break;
case XML_CQUANT_REP:
contentModel += "*";//_T("*");
contentModel += _T("*");
break;
case XML_CQUANT_PLUS:
contentModel += "+";//_T("+");
contentModel += _T("+");
break;
case XML_CQUANT_NONE:
default:
@ -280,7 +264,7 @@ void XMLCALL XmlPromptGenerator::attlistdeclhandler (
PromptGeneratorData *d;
d = ( PromptGeneratorData * ) data;
std::set<std::string> &attributeValues = d->attributeMap[elname][attname];
std::set<wxString> &attributeValues = d->attributeMap[elname][attname];
if ( *att_type == '(' ) // change to exclude _known_ identifiers?
{
const char *s, *word;
@ -292,7 +276,7 @@ void XMLCALL XmlPromptGenerator::attlistdeclhandler (
while ( *s != '|' && *s != ')' )
s++;
std::string currentValue ( word, s - word );
wxString currentValue ( word, wxConvUTF8, s - word );
attributeValues.insert ( currentValue );
while ( *s != '|' && *s != ')')
@ -302,7 +286,7 @@ void XMLCALL XmlPromptGenerator::attlistdeclhandler (
if ( isrequired )
{
d->requiredAttributeMap[elname].insert ( attname );
d->requiredAttributeMap[elname].insert ( wxString ( attname, wxConvUTF8 ) );
}
}
@ -407,7 +391,7 @@ void XMLCALL XmlPromptGenerator::entitydeclhandler (
!publicId &&
!notationName )
{
d->entitySet.insert ( entityName );
d->entitySet.insert ( wxString ( entityName, wxConvUTF8 ) );
}
}
@ -416,26 +400,28 @@ void XmlPromptGenerator::handleSchema (
const XML_Char *el,
const XML_Char **attr )
{
if ( !d->isRootElement )
return;
// first check for XML Schema association
XML_Char **schemaAttr = ( XML_Char ** ) attr; // now redundant; could use attr
const char **schemaAttr = ( const XML_Char ** ) attr; // now redundant; could use attr
std::string path;
for ( ; d->isRootElement && *schemaAttr; schemaAttr += 2 )
for ( ; *schemaAttr; schemaAttr += 2 )
{
// no namespace
if ( !strcmp ( ( const char * ) *schemaAttr, "xsi:noNamespaceSchemaLocation" ) )
if ( !strcmp ( *schemaAttr, "xsi:noNamespaceSchemaLocation" ) )
{
path = ( const char * ) * ( schemaAttr + 1 );
path = * ( schemaAttr + 1 );
break;
}
// with namespace -- check if this works
else if ( !strcmp ( ( const char * ) *schemaAttr, "xsi:schemaLocation" ) )
else if ( !strcmp ( *schemaAttr, "xsi:schemaLocation" ) )
{
char *searchIterator;
for ( searchIterator = ( char * ) * ( schemaAttr + 1 ); *searchIterator && *searchIterator != ' ' && *searchIterator != '\t' && *searchIterator != '\n'; searchIterator++ )
;
const char *searchIterator = * ( schemaAttr + 1 );
while ( *searchIterator && *searchIterator != ' ' && *searchIterator != '\t' && *searchIterator != '\n' )
searchIterator++;
if ( *searchIterator )
{
path = ( const char * ) ( searchIterator + 1 );
path = searchIterator + 1;
break;
}
}
@ -484,39 +470,29 @@ void XmlPromptGenerator::handleSchema (
return;
}
char *s;
while ( elemEnum.hasMoreElements() )
{
const SchemaElementDecl& curElem = elemEnum.nextElement();
std::string element;
std::set<std::string> children;
wxString element;
const QName *qnm = curElem.getElementName();
if ( qnm )
{
s = XMLString::transcode ( qnm->getRawName() ); // this includes any prefix:localname combinations
element = s;
XMLString::release( &s );
}
if ( qnm == NULL )
continue;
element = WrapXerces::toString ( qnm->getRawName() ); // this includes any prefix:localname combinations
if ( element.empty() )
continue;
const XMLCh* fmtCntModel = curElem.getFormattedContentModel();
if ( fmtCntModel != NULL ) // tbd: this does not yet pick up prefix:localname combinations
{
std::string structure;
s = XMLString::transcode ( fmtCntModel );
structure = s;
XMLString::release( &s );
d->elementStructureMap.insert ( make_pair ( element, structure ) );
wxString structure = WrapXerces::toString ( fmtCntModel );
d->elementStructureMap[element] = structure;
}
const ContentSpecNode *spec = curElem.getContentSpec();
if ( spec != NULL )
{
getContent ( spec, children );
if ( !children.empty() )
d->elementMap.insert ( make_pair ( element, children ) );
getContent ( spec, d->elementMap[element] );
}
// fetch attributes
@ -526,7 +502,7 @@ void XmlPromptGenerator::handleSchema (
XMLAttDefList& attIter = curElem.getAttDefList();
for ( unsigned int i = 0; i < attIter.getAttDefCount(); i++ )
{
std::string attribute, attributeValue;
wxString attribute, attributeValue;
XMLAttDef& attr = attIter.getAttDef ( i );
XMLAttDef::DefAttTypes ty = attr.getDefaultType();
@ -535,24 +511,16 @@ void XmlPromptGenerator::handleSchema (
SchemaAttDef *pAttr = ( SchemaAttDef * ) &attr;
const QName *qnm = pAttr->getAttName();
if ( qnm )
{
s = XMLString::transcode ( qnm->getRawName() );
attribute = s;
XMLString::release( &s );
}
if ( qnm == NULL )
continue;
attribute = WrapXerces::toString ( qnm->getRawName() );
if ( attribute.empty() )
continue;
// Value
if ( pAttr->getValue() )
{
s = XMLString::transcode ( pAttr->getValue() );
attributeValue = s;
XMLString::release( &s );
}
attributeValue = WrapXerces::toString ( pAttr->getValue() );
d->attributeMap[element][attribute].insert( attributeValue );
if ( ty == XMLAttDef::Required || ty == XMLAttDef::Required_And_Fixed)
d->requiredAttributeMap[element].insert ( attribute );
}
@ -563,17 +531,16 @@ void XmlPromptGenerator::handleSchema (
void XmlPromptGenerator::getContent (
const ContentSpecNode *spec,
std::set<std::string> &list )
std::set<wxString> &list )
{
//if ( spec == NULL) return;
const QName *qnm = spec->getElement();
if ( qnm )
{
char *element = XMLString::transcode ( qnm->getRawName() );
if ( element != NULL )
wxString element = WrapXerces::toString ( qnm->getRawName() );
if ( !element.IsEmpty() )
list.insert( element );
XMLString::release( &element );
}
if ( spec->getFirst() != NULL)

View File

@ -20,6 +20,7 @@
#ifndef XML_PROMPT_GENERATOR_H
#define XML_PROMPT_GENERATOR_H
#include <wx/wx.h>
//#include <expat.h>
#include <map>
#include <set>
@ -30,12 +31,12 @@
struct PromptGeneratorData : public ParserData
{
std::map<std::string, std::map<std::string, std::set<std::string> > >
std::map<wxString, std::map<wxString, std::set<wxString> > >
attributeMap;
std::map<std::string, std::set<std::string> > elementMap;
std::map<std::string, std::set<std::string> > requiredAttributeMap;
std::map<std::string, std::string> elementStructureMap;
std::set<std::string> entitySet;
std::map<wxString, std::set<wxString> > elementMap;
std::map<wxString, std::set<wxString> > requiredAttributeMap;
std::map<wxString, wxString> elementStructureMap;
std::set<wxString> entitySet;
std::string catalogPath, basePath, auxPath, rootElement;
bool isRootElement, grammarFound;
unsigned attributeValueCutoff;
@ -51,17 +52,17 @@ class XmlPromptGenerator : public WrapExpat
const std::string& auxPath = "" );
virtual ~XmlPromptGenerator();
void getAttributeMap (
std::map<std::string, std::map<std::string, std::set<std::string> > >
std::map<wxString, std::map<wxString, std::set<wxString> > >
&attributeMap );
void getRequiredAttributeMap (
std::map<std::string, std::set<std::string> > &requiredAttributeMap );
std::map<wxString, std::set<wxString> > &requiredAttributeMap );
void getElementMap (
std::map<std::string, std::set<std::string> > &elementMap );
std::map<wxString, std::set<wxString> > &elementMap );
void getEntitySet (
std::set<std::string> &entitySet );
std::set<wxString> &entitySet );
bool getGrammarFound();
void getElementStructureMap (
std::map<std::string, std::string> &elementStructureMap );
std::map<wxString, wxString> &elementStructureMap );
private:
std::auto_ptr<PromptGeneratorData> d;
static void XMLCALL starthandler (
@ -84,8 +85,8 @@ class XmlPromptGenerator : public WrapExpat
XML_Content *model );
static void getContent (
const XML_Content &content,
std::string &contentModel,
std::set<std::string> &list );
wxString &contentModel,
std::set<wxString> &list );
static void XMLCALL attlistdeclhandler (
void *userData,
const XML_Char *elname,
@ -115,7 +116,7 @@ class XmlPromptGenerator : public WrapExpat
const XML_Char **attr );
static void getContent (
const xercesc::ContentSpecNode *spec,
std::set<std::string> &list );
std::set<wxString> &list );
};
#endif

View File

@ -17,6 +17,7 @@
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <wx/wx.h>
#include <iostream>
#include <string>
#include <vector>
@ -27,11 +28,11 @@
#include "xmlshallowvalidator.h"
XmlShallowValidator::XmlShallowValidator (
std::map<std::string, std::set<std::string> > &elementMap,
std::map<std::string, std::map<std::string, std::set<std::string> > >
std::map<wxString, std::set<wxString> > &elementMap,
std::map<wxString, std::map<wxString, std::set<wxString> > >
&attributeMap,
std::map<std::string, std::set<std::string> > &requiredAttributeMap,
std::set<std::string> &entitySet,
std::map<wxString, std::set<wxString> > &requiredAttributeMap,
std::set<wxString> &entitySet,
int maxLine,
bool segmentOnly ) : vd ( new XmlShallowValidatorData() )
{
@ -71,30 +72,34 @@ void XMLCALL XmlShallowValidator::start ( void *data,
++ ( vd->depth );
//check element ok
std::string parent = vd->getParent();
wxString parent ( vd->getParent().c_str(), wxConvUTF8 );
if ( parent.empty() )
return;
if ( vd->elementMap.empty() )
return;
if ( !vd->elementMap[parent].count ( vd->getElement() ) )
wxString element ( vd->getElement().c_str(), wxConvUTF8 );
if ( !vd->elementMap[parent].count ( element ) )
{
vd->isValid = false;
vd->positionVector.push_back (
make_pair ( XML_GetCurrentLineNumber ( vd->p ), XML_GetCurrentColumnNumber ( vd->p ) ) );
}
std::map<std::string, std::set<std::string> > attributeMap;
size_t requiredAttributeCount = vd->requiredAttributeMap[el].size();
std::string currentAttribute;
element = wxString ( el, wxConvUTF8 );
std::map<wxString, std::set<wxString> > attributeMap;
size_t requiredAttributeCount = vd->requiredAttributeMap[element].size();
wxString currentAttribute;
while ( *attr )
{
attributeMap = vd->attributeMap[el];
attributeMap = vd->attributeMap[element];
// check for existence
if ( !attributeMap.count ( *attr ) )
currentAttribute = wxString ( *attr, wxConvUTF8 );
if ( !attributeMap.count ( currentAttribute ) )
{
vd->isValid = false;
vd->positionVector.push_back ( make_pair (
@ -102,9 +107,8 @@ void XMLCALL XmlShallowValidator::start ( void *data,
XML_GetCurrentColumnNumber ( vd->p ) ) );
}
// check for requirement
currentAttribute = ( const char * ) *attr;
if ( vd->requiredAttributeMap[el].find ( currentAttribute ) !=
vd->requiredAttributeMap[el].end() )
if ( vd->requiredAttributeMap[element].find ( currentAttribute ) !=
vd->requiredAttributeMap[element].end() )
--requiredAttributeCount;
attr += 2;

View File

@ -33,11 +33,11 @@ struct XmlShallowValidatorData : public ParserData
{
XmlShallowValidatorData()
{}
std::map<std::string, std::set<std::string> > elementMap;
std::map<std::string, std::map<std::string, std::set<std::string> > >
std::map<wxString, std::set<wxString> > elementMap;
std::map<wxString, std::map<wxString, std::set<wxString> > >
attributeMap;
std::map<std::string, std::set<std::string> > requiredAttributeMap;
std::set<std::string> entitySet;
std::map<wxString, std::set<wxString> > requiredAttributeMap;
std::set<wxString> entitySet;
std::vector<std::pair<int, int> > positionVector;
bool isValid, segmentOnly;
int depth, maxLine;
@ -49,11 +49,11 @@ class XmlShallowValidator : public WrapExpat
{
public:
XmlShallowValidator (
std::map<std::string, std::set<std::string> > &elementMap,
std::map<std::string, std::map<std::string, std::set<std::string> > >
std::map<wxString, std::set<wxString> > &elementMap,
std::map<wxString, std::map<wxString, std::set<wxString> > >
&attributeMap,
std::map<std::string, std::set<std::string> > &requiredAttributeMap,
std::set<std::string> &entitySet,
std::map<wxString, std::set<wxString> > &requiredAttributeMap,
std::set<wxString> &entitySet,
int maxLine = 0,
bool segmentOnly = false );
virtual ~XmlShallowValidator();