Further improvements to background validation: critical section locks now in place.

This commit is contained in:
Gerald Schmidt 2008-07-10 17:06:32 +00:00
parent 361eed32b0
commit 8be8af2aa0
4 changed files with 54 additions and 85 deletions

View File

@ -32,7 +32,10 @@ void *ValidationThread::Entry()
std::auto_ptr<WrapXerces> validator ( new WrapXerces() ); std::auto_ptr<WrapXerces> validator ( new WrapXerces() );
if ( *myReleasePtr || TestDestroy() ) if ( *myReleasePtr || TestDestroy() )
{
Exit(); Exit();
return NULL;
}
bool res = validator->validateMemory ( bool res = validator->validateMemory (
myBuffer.c_str(), myBuffer.c_str(),
@ -40,7 +43,10 @@ void *ValidationThread::Entry()
myBuffer.size() ); myBuffer.size() );
if ( *myReleasePtr || TestDestroy() ) if ( *myReleasePtr || TestDestroy() )
{
Exit(); Exit();
return NULL;
}
if ( !res ) if ( !res )
{ {
@ -54,7 +60,6 @@ void *ValidationThread::Entry()
*myPositionPtr = std::make_pair ( 0, 0 ); *myPositionPtr = std::make_pair ( 0, 0 );
*myMessagePtr = ""; *myMessagePtr = "";
} }
return NULL; return NULL;
} }

View File

@ -1633,7 +1633,7 @@ void MyFrame::OnIdle ( wxIdleEvent& event )
if ( !parent.empty() && properties.validateAsYouType && doc->getValidationRequired() ) if ( !parent.empty() && properties.validateAsYouType && doc->getValidationRequired() )
{ {
// tbd: limit to parent element // tbd: limit to parent element
doc->shallowValidate ( doc->LineFromPosition ( current ), true ); doc->backgroundValidate();
} }
@ -2742,7 +2742,7 @@ void MyFrame::newDocument ( const std::string& s, const std::string& path, bool
insertSiblingPanel->update ( doc, wxEmptyString ); insertSiblingPanel->update ( doc, wxEmptyString );
insertEntityPanel->update ( doc ); insertEntityPanel->update ( doc );
if ( properties.validateAsYouType ) if ( properties.validateAsYouType )
doc->shallowValidate(); doc->backgroundValidate();
} }
void MyFrame::OnOpen ( wxCommandEvent& event ) void MyFrame::OnOpen ( wxCommandEvent& event )
@ -3114,7 +3114,7 @@ bool MyFrame::openFile ( wxString& fileName, bool largeFile )
if ( !largeFile && ( properties.validateAsYouType && doc->getGrammarFound() ) ) if ( !largeFile && ( properties.validateAsYouType && doc->getGrammarFound() ) )
{ {
statusProgress ( _T ( "Validating document..." ) ); statusProgress ( _T ( "Validating document..." ) );
doc->shallowValidate ( finalBuffer, doc->getFullFileName().mb_str(wxConvUTF8), finalBufferLen ); doc->backgroundValidate ( finalBuffer, doc->getFullFileName().mb_str(wxConvUTF8), finalBufferLen );
statusProgress ( wxEmptyString ); statusProgress ( wxEmptyString );
} }
@ -3629,7 +3629,7 @@ void MyFrame::OnValidateDTD ( wxCommandEvent& event )
doc->SetSelection ( cursorPos, cursorPos ); doc->SetSelection ( cursorPos, cursorPos );
// shallow validate all // shallow validate all
doc->shallowValidate(); // has to come first as it deletes all indicators doc->backgroundValidate(); // has to come first as it deletes all indicators
doc->setErrorIndicator ( posPair.first, posPair.second ); doc->setErrorIndicator ( posPair.first, posPair.second );
return; return;
@ -4748,7 +4748,7 @@ bool MyFrame::saveFile ( XmlDoc *doc, wxString& fileName, bool checkLastModified
if ( properties.validateAsYouType && isXml ) if ( properties.validateAsYouType && isXml )
{ {
doc->clearErrorIndicators(); doc->clearErrorIndicators();
doc->shallowValidate ( utf8Buffer.c_str(), doc->getFullFileName().mb_str(wxConvUTF8), utf8Buffer.size() ); doc->backgroundValidate ( utf8Buffer.c_str(), doc->getFullFileName().mb_str(wxConvUTF8), utf8Buffer.size() );
} }
if ( !unlimitedUndo ) if ( !unlimitedUndo )

View File

@ -40,6 +40,9 @@ BEGIN_EVENT_TABLE ( XmlCtrl, wxStyledTextCtrl )
EVT_MIDDLE_DOWN ( XmlCtrl::OnMiddleDown ) EVT_MIDDLE_DOWN ( XmlCtrl::OnMiddleDown )
END_EVENT_TABLE() END_EVENT_TABLE()
// global protection for validation threads
wxCriticalSection xmlcopyeditorCriticalSection;
XmlCtrl::XmlCtrl ( XmlCtrl::XmlCtrl (
wxWindow *parent, wxWindow *parent,
XmlCtrlProperties propertiesParameter, XmlCtrlProperties propertiesParameter,
@ -143,6 +146,9 @@ void XmlCtrl::OnIdle ( wxIdleEvent& event )
adjustNoColumnWidth(); // exits if unchanged adjustNoColumnWidth(); // exits if unchanged
// poll validation thread output if any // poll validation thread output if any
{
wxCriticalSectionLocker locker ( xmlcopyeditorCriticalSection );
if (validationStarted && validationFinished) if (validationStarted && validationFinished)
{ {
validationStarted = false; validationStarted = false;
@ -154,12 +160,13 @@ void XmlCtrl::OnIdle ( wxIdleEvent& event )
} }
else else
{ {
clearErrorIndicators ( validationPosition.first -1 ); clearErrorIndicators ( GetLineCount() );
setErrorIndicator ( validationPosition.first - 1, 0 ); setErrorIndicator ( validationPosition.first - 1, 0 );
frame->statusProgress ( wxString ( validationMessage.c_str(), wxConvUTF8, validationMessage.size() ) ); frame->statusProgress ( wxString ( validationMessage.c_str(), wxConvUTF8, validationMessage.size() ) );
} }
} }
} }
}
void XmlCtrl::OnChar ( wxKeyEvent& event ) void XmlCtrl::OnChar ( wxKeyEvent& event )
{ {
@ -1996,21 +2003,20 @@ std::string XmlCtrl::getElementStructure ( const wxString& element )
return ret; return ret;
} }
bool XmlCtrl::shallowValidate ( int maxLine, bool segmentOnly ) bool XmlCtrl::backgroundValidate()
{ {
if ( !properties.validateAsYouType || type != FILE_TYPE_XML ) if ( !properties.validateAsYouType || type != FILE_TYPE_XML )
return true; return true;
clearErrorIndicators ( GetLineCount() );
std::string bufferUtf8 = myGetTextRaw(); std::string bufferUtf8 = myGetTextRaw();
return shallowValidate ( return backgroundValidate (
bufferUtf8.c_str(), bufferUtf8.c_str(),
basePath.c_str(), basePath.c_str(),
bufferUtf8.size() ); bufferUtf8.size() );
} }
bool XmlCtrl::shallowValidate ( bool XmlCtrl::backgroundValidate (
const char *buffer, const char *buffer,
const char *system, const char *system,
size_t bufferLen size_t bufferLen
@ -2019,7 +2025,7 @@ bool XmlCtrl::shallowValidate (
if ( !validationRequired ) if ( !validationRequired )
return true; return true;
wxCriticalSectionLocker locker ( xmlcopyeditorCriticalSection );
if ( validationStarted && !validationFinished ) if ( validationStarted && !validationFinished )
{ {
validationRelease = true; validationRelease = true;
@ -2047,43 +2053,8 @@ bool XmlCtrl::shallowValidate (
validationStarted = true; validationStarted = true;
validationFinished = false; validationFinished = false;
validationThread->Run(); validationThread->Run();
return true; return true;
/*
std::auto_ptr<XmlShallowValidator> validator ( new XmlShallowValidator (
elementMap,
attributeMap,
requiredAttributeMap,
entitySet,
maxLine,
segmentOnly ) );
bool res = validator->parse ( buffer, bufferLen, ( segmentOnly ) ? false : true );
if (
!validator->getOverrideFailure() &&
( !res || !validator->isValid() ) )
{
std::vector<std::pair<int, int> > positionVector = validator->getPositionVector();
if ( res == XML_STATUS_ERROR )
{
positionVector.push_back ( validator->getErrorPosition() );
}
std::vector<std::pair<int, int> >::iterator it;
for ( it = positionVector.begin(); it != positionVector.end(); it++ )
{
int line, column;
line = ( it->first - 1 ) + startLine;
column = it->second + columnOffset;
setErrorIndicator ( line, column );
}
return false;
}
return true;
*/
} }
std::string XmlCtrl::myGetTextRaw() std::string XmlCtrl::myGetTextRaw()
@ -2106,12 +2077,6 @@ void XmlCtrl::setErrorIndicator ( int line, int column )
int length = endPos - startPos; int length = endPos - startPos;
/*
from CellBuffer.cxx:
PLATFORM_ASSERT(lengthStyle == 0 ||
(lengthStyle > 0 && lengthStyle + position < length));
*/
if ( length == 0 || ( length > 0 && length + startPos < GetLength() ) ) if ( length == 0 || ( length > 0 && length + startPos < GetLength() ) )
{ {
SetStyling ( length, wxSTC_INDIC2_MASK ); SetStyling ( length, wxSTC_INDIC2_MASK );

View File

@ -1,4 +1,3 @@
/* /*
* Copyright 2005-2007 Gerald Schmidt. * Copyright 2005-2007 Gerald Schmidt.
* *
@ -145,8 +144,8 @@ class XmlCtrl: public wxStyledTextCtrl
bool canInsertAt ( int pos ); bool canInsertAt ( int pos );
int getTagStartPos ( int pos ); int getTagStartPos ( int pos );
void toggleLineBackground(); void toggleLineBackground();
bool shallowValidate ( int maxLine = 0, bool segmentOnly = false ); bool backgroundValidate ( );
bool shallowValidate ( bool backgroundValidate (
const char *buffer, const char *buffer,
const char *system, const char *system,
size_t bufferLen ); size_t bufferLen );