1 /*
2  * Copyright 2005-2009 Gerald Schmidt.
3  *
4  * This file is part of Xml Copy Editor.
5  *
6  * Xml Copy Editor is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * Xml Copy Editor is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Xml Copy Editor; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  */
20 
21 #include <wx/wx.h>
22 #include <wx/textbuf.h>
23 #include <boost/scoped_ptr.hpp>
24 #include "xmlctrl.h"
25 #include "validationthread.h"
26 #include "wrapxerces.h"
27 #include <stdexcept>
28 #include "threadreaper.h"
29 
30 extern wxCriticalSection xmlcopyeditorCriticalSection;
31 
32 DEFINE_EVENT_TYPE(wxEVT_COMMAND_VALIDATION_COMPLETED);
33 
ValidationThread(wxEvtHandler * handler,const char * utf8Buffer,const wxString & system)34 ValidationThread::ValidationThread (
35 	wxEvtHandler *handler,
36 	const char *utf8Buffer,
37 	const wxString &system )
38 	: wxThread ( wxTHREAD_JOINABLE )
39 	, mStopping ( false )
40 {
41 	if ( utf8Buffer == NULL )
42 	{
43 		throw;
44 	}
45 
46 	myEventHandler = handler;
47 	myBuffer = utf8Buffer;
48 	mySystem = system;
49 	myIsSucceeded = false;
50 }
51 
Entry()52 void *ValidationThread::Entry()
53 {
54 	boost::scoped_ptr<WrapXerces> validator ( new WrapXerces() );
55 
56 	if ( TestDestroy()  )
57 	{
58 		myBuffer.clear();
59 		return NULL;
60 	}
61 
62 	myIsSucceeded = validator->validateMemory (
63 		myBuffer.c_str(),
64 		myBuffer.size(),
65 		mySystem,
66 		this,
67 		// Don't be too harsh to new created documents, which may haven't
68 		// associated any schema yet
69 		/*forceCheckGrammar*/false,
70 		wxTextBuffer::GetEOL() );
71 
72 	myBuffer.clear();
73 
74 	if ( TestDestroy() )
75 	{
76 		return NULL;
77 	}
78 
79 	wxCriticalSectionLocker locker ( xmlcopyeditorCriticalSection );
80 
81 	myPosition = validator->getErrorPosition();
82 	myMessage = validator->getLastError();
83 
84 	if ( !TestDestroy() )
85 	{
86 		wxCommandEvent event ( wxEVT_COMMAND_VALIDATION_COMPLETED );
87 		wxPostEvent ( myEventHandler, event );
88 	}
89 
90 	return NULL;
91 }
92 
PendingDelete()93 void ValidationThread::PendingDelete ()
94 {
95 	Cancel();
96 
97 	ThreadReaper::get().add ( this );
98 }
99