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

@ -31,30 +31,35 @@ void *ValidationThread::Entry()
{
std::auto_ptr<WrapXerces> validator ( new WrapXerces() );
if ( *myReleasePtr || TestDestroy() )
Exit();
if ( *myReleasePtr || TestDestroy() )
{
Exit();
return NULL;
}
bool res = validator->validateMemory (
myBuffer.c_str(),
mySystem.c_str(),
myBuffer.size() );
if ( *myReleasePtr || TestDestroy() )
Exit();
if ( !res )
{
*mySuccessPtr = res;
*myPositionPtr = validator->getErrorPosition();
*myMessagePtr = validator->getLastError();
}
else
{
*mySuccessPtr = true;
*myPositionPtr = std::make_pair ( 0, 0 );
*myMessagePtr = "";
}
if ( *myReleasePtr || TestDestroy() )
{
Exit();
return NULL;
}
if ( !res )
{
*mySuccessPtr = res;
*myPositionPtr = validator->getErrorPosition();
*myMessagePtr = validator->getLastError();
}
else
{
*mySuccessPtr = true;
*myPositionPtr = std::make_pair ( 0, 0 );
*myMessagePtr = "";
}
return NULL;
}

View File

@ -179,7 +179,7 @@ BEGIN_EVENT_TABLE ( MyFrame, wxFrame )
#endif
END_EVENT_TABLE()
IMPLEMENT_APP ( MyApp )
IMPLEMENT_APP ( MyApp)
MyApp::MyApp() : checker ( NULL ), server ( NULL ), connection ( NULL ),
#ifdef __WXMSW__
@ -1633,7 +1633,7 @@ void MyFrame::OnIdle ( wxIdleEvent& event )
if ( !parent.empty() && properties.validateAsYouType && doc->getValidationRequired() )
{
// 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 );
insertEntityPanel->update ( doc );
if ( properties.validateAsYouType )
doc->shallowValidate();
doc->backgroundValidate();
}
void MyFrame::OnOpen ( wxCommandEvent& event )
@ -3114,7 +3114,7 @@ bool MyFrame::openFile ( wxString& fileName, bool largeFile )
if ( !largeFile && ( properties.validateAsYouType && doc->getGrammarFound() ) )
{
statusProgress ( _T ( "Validating document..." ) );
doc->shallowValidate ( finalBuffer, doc->getFullFileName().mb_str(wxConvUTF8), finalBufferLen );
doc->backgroundValidate ( finalBuffer, doc->getFullFileName().mb_str(wxConvUTF8), finalBufferLen );
statusProgress ( wxEmptyString );
}
@ -3629,7 +3629,7 @@ void MyFrame::OnValidateDTD ( wxCommandEvent& event )
doc->SetSelection ( cursorPos, cursorPos );
// 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 );
return;
@ -4748,7 +4748,7 @@ bool MyFrame::saveFile ( XmlDoc *doc, wxString& fileName, bool checkLastModified
if ( properties.validateAsYouType && isXml )
{
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 )

View File

@ -40,6 +40,9 @@ BEGIN_EVENT_TABLE ( XmlCtrl, wxStyledTextCtrl )
EVT_MIDDLE_DOWN ( XmlCtrl::OnMiddleDown )
END_EVENT_TABLE()
// global protection for validation threads
wxCriticalSection xmlcopyeditorCriticalSection;
XmlCtrl::XmlCtrl (
wxWindow *parent,
XmlCtrlProperties propertiesParameter,
@ -143,20 +146,24 @@ void XmlCtrl::OnIdle ( wxIdleEvent& event )
adjustNoColumnWidth(); // exits if unchanged
// poll validation thread output if any
if (validationStarted && validationFinished)
{
validationStarted = false;
MyFrame *frame = (MyFrame *)GetGrandParent();
if ( validationSuccess )
wxCriticalSectionLocker locker ( xmlcopyeditorCriticalSection );
if (validationStarted && validationFinished)
{
clearErrorIndicators ( GetLineCount() );
frame->statusProgress ( wxEmptyString );
}
else
{
clearErrorIndicators ( validationPosition.first -1 );
setErrorIndicator ( validationPosition.first - 1, 0 );
frame->statusProgress ( wxString ( validationMessage.c_str(), wxConvUTF8, validationMessage.size() ) );
validationStarted = false;
MyFrame *frame = (MyFrame *)GetGrandParent();
if ( validationSuccess )
{
clearErrorIndicators ( GetLineCount() );
frame->statusProgress ( wxEmptyString );
}
else
{
clearErrorIndicators ( GetLineCount() );
setErrorIndicator ( validationPosition.first - 1, 0 );
frame->statusProgress ( wxString ( validationMessage.c_str(), wxConvUTF8, validationMessage.size() ) );
}
}
}
}
@ -1996,21 +2003,20 @@ std::string XmlCtrl::getElementStructure ( const wxString& element )
return ret;
}
bool XmlCtrl::shallowValidate ( int maxLine, bool segmentOnly )
bool XmlCtrl::backgroundValidate()
{
if ( !properties.validateAsYouType || type != FILE_TYPE_XML )
return true;
clearErrorIndicators ( GetLineCount() );
std::string bufferUtf8 = myGetTextRaw();
return shallowValidate (
return backgroundValidate (
bufferUtf8.c_str(),
basePath.c_str(),
bufferUtf8.size() );
}
bool XmlCtrl::shallowValidate (
bool XmlCtrl::backgroundValidate (
const char *buffer,
const char *system,
size_t bufferLen
@ -2019,7 +2025,7 @@ bool XmlCtrl::shallowValidate (
if ( !validationRequired )
return true;
wxCriticalSectionLocker locker ( xmlcopyeditorCriticalSection );
if ( validationStarted && !validationFinished )
{
validationRelease = true;
@ -2047,43 +2053,8 @@ bool XmlCtrl::shallowValidate (
validationStarted = true;
validationFinished = false;
validationThread->Run();
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()
@ -2106,12 +2077,6 @@ void XmlCtrl::setErrorIndicator ( int line, int column )
int length = endPos - startPos;
/*
from CellBuffer.cxx:
PLATFORM_ASSERT(lengthStyle == 0 ||
(lengthStyle > 0 && lengthStyle + position < length));
*/
if ( length == 0 || ( length > 0 && length + startPos < GetLength() ) )
{
SetStyling ( length, wxSTC_INDIC2_MASK );

View File

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