@@ -271,16 +271,11 @@ SimulationOutputHandler::SimulationOutputHandler(SimulationOutputWidget *pSimula
271271 } else {
272272 mpSimulationMessageModel = 0 ;
273273 }
274- mXmlSimpleReader .setContentHandler (this );
275- mXmlSimpleReader .setErrorHandler (this );
276- mpXmlInputSource = new QXmlInputSource;
277- mpXmlInputSource->setData (simulationOutput.prepend (" <root>" ));
278- mXmlSimpleReader .parse (mpXmlInputSource, true );
274+ parseSimulationOutput (simulationOutput.prepend (" <root>" ));
279275}
280276
281277SimulationOutputHandler::~SimulationOutputHandler ()
282278{
283- delete mpXmlInputSource;
284279 simulationProcessFinished ();
285280}
286281
@@ -289,10 +284,124 @@ SimulationOutputHandler::~SimulationOutputHandler()
289284 * Sets the new simulation output data and continues the parsing.
290285 * \param output
291286 */
292- void SimulationOutputHandler::parseSimulationOutput (QString output)
287+ void SimulationOutputHandler::parseSimulationOutput (const QString & output)
293288{
294- mpXmlInputSource->setData (output);
295- mXmlSimpleReader .parseContinue ();
289+ /* if display limit is reached then close and display the so far text
290+ * and display a message showing that the limit is reached.
291+ */
292+ if (isMaximumDisplayLimitReached ()) {
293+ // Only generate the reached display limit message once.
294+ if (!mShownDisplayLimitReachedMessage ) {
295+ mShownDisplayLimitReachedMessage = true ;
296+
297+ while (mLevel > 0 ) {
298+ endElement ();
299+ }
300+
301+ if (mpSimulationOutputWidget->isOutputStructured ()) {
302+ mpSimulationMessage = new SimulationMessage (mpSimulationMessageModel->getRootSimulationMessage ());
303+ } else {
304+ mpSimulationMessage = new SimulationMessage;
305+ }
306+ mpSimulationMessage->mStream = " stdout" ;
307+ mpSimulationMessage->mType = StringHandler::OMEditInfo;
308+ mpSimulationMessage->mText = QString (" Reached display limit" );
309+ mpSimulationMessage->mLevel = mLevel ;
310+ mSimulationMessagesLevelMap .insert (mLevel , mpSimulationMessage);
311+ mLevel ++;
312+ endElement ();
313+ }
314+ return ;
315+ }
316+
317+ mXmlStreamReader .addData (output);
318+ // parse
319+ while (!mXmlStreamReader .atEnd ()) {
320+ QXmlStreamReader::TokenType token = mXmlStreamReader .readNext ();
321+ if (token == QXmlStreamReader::StartElement) {
322+ QXmlStreamAttributes attributes = mXmlStreamReader .attributes ();
323+ if (mXmlStreamReader .name () == " message" ) {
324+ QString text = attributes.value (" text" ).toString () + " \n " ;
325+ mNumberOfBytes += text.toUtf8 ().size ();
326+ // write simulation log file
327+ writeSimulationLog (text);
328+ if (mpSimulationOutputWidget->isOutputStructured ()) {
329+ mpSimulationMessage = new SimulationMessage (mpSimulationMessageModel->getRootSimulationMessage ());
330+ } else {
331+ mpSimulationMessage = new SimulationMessage;
332+ }
333+ mpSimulationMessage->mStream = attributes.value (" stream" ).toString ();
334+ mpSimulationMessage->mType = StringHandler::getSimulationMessageType (attributes.value (" type" ).toString ());
335+ // check if we get the message about embedded opc-ua server initialized.
336+ if (attributes.value (" text" ) == " The embedded server is initialized." ) {
337+ mpSimulationOutputWidget->embeddedServerInitialized ();
338+ }
339+ if (mpSimulationOutputWidget->isOutputStructured ()) {
340+ mpSimulationMessage->mText = Qt::convertFromPlainText (attributes.value (" text" ).toString ());
341+ } else {
342+ mpSimulationMessage->mText = attributes.value (" text" ).toString ();
343+ }
344+ mpSimulationMessage->mLevel = mLevel ;
345+ mSimulationMessagesLevelMap .insert (mLevel , mpSimulationMessage);
346+ if (mLevel > 0 ) {
347+ SimulationMessage *pSimulationMessage = mSimulationMessagesLevelMap .value (mLevel - 1 , 0 );
348+ if (pSimulationMessage) {
349+ mpSimulationMessage->setParent (pSimulationMessage);
350+ if (mpSimulationOutputWidget->isOutputStructured ()) {
351+ mpSimulationMessage->mDeweyId = pSimulationMessage->mDeweyId + " ." + QString::number (pSimulationMessage->children ().size () + 1 );
352+ mpSimulationMessage->mDeweyId = QString (" %1.%2" ).arg (pSimulationMessage->mDeweyId )
353+ .arg (QString::number (pSimulationMessage->children ().size () + 1 ));
354+ }
355+ pSimulationMessage->mChildren .append (mpSimulationMessage);
356+ }
357+ } else {
358+ if (mpSimulationOutputWidget->isOutputStructured ()) {
359+ mpSimulationMessage->mDeweyId = QString (" %1.%2" ).arg (mpSimulationMessageModel->getRootSimulationMessage ()->mDeweyId )
360+ .arg (QString::number (mpSimulationMessageModel->getRootSimulationMessage ()->children ().size () + 1 ));
361+ }
362+ }
363+ mLevel ++;
364+ } else if (mXmlStreamReader .name () == " used" ) {
365+ if (mpSimulationMessage) {
366+ mpSimulationMessage->mIndex = attributes.value (" index" ).toString ();
367+ }
368+ } else if (mXmlStreamReader .name () == " status" ) {
369+ int progress = attributes.value (" progress" ).toInt ();
370+ mpSimulationOutputWidget->getProgressBar ()->setValue (progress/100 );
371+ }
372+ } else if (token == QXmlStreamReader::EndElement) {
373+ if (mXmlStreamReader .name () == " message" ) {
374+ endElement ();
375+ }
376+ }
377+ }
378+
379+ // error handling
380+ if (mXmlStreamReader .hasError ()) {
381+ // read the error message
382+ if (mXmlStreamReader .error () != QXmlStreamReader::PrematureEndOfDocumentError) {
383+ QString errorText = QString (" Fatal error on line %1, column %2: %3" )
384+ .arg (mXmlStreamReader .lineNumber ())
385+ .arg (mXmlStreamReader .columnNumber ())
386+ .arg (mXmlStreamReader .errorString ());
387+ // construct the SimulationMessage object with error
388+ SimulationMessage *pSimulationMessage;
389+ if (mpSimulationOutputWidget->isOutputStructured ()) {
390+ pSimulationMessage = new SimulationMessage (mpSimulationMessageModel->getRootSimulationMessage ());
391+ } else {
392+ pSimulationMessage = new SimulationMessage;
393+ }
394+ pSimulationMessage->mStream = " stderr" ;
395+ pSimulationMessage->mType = StringHandler::getSimulationMessageType (" error" );
396+ pSimulationMessage->mText = errorText;
397+ pSimulationMessage->mLevel = 0 ;
398+ if (mpSimulationOutputWidget->isOutputStructured ()) {
399+ mpSimulationMessageModel->insertSimulationMessage (pSimulationMessage);
400+ } else {
401+ mpSimulationOutputWidget->writeSimulationMessage (pSimulationMessage);
402+ }
403+ }
404+ }
296405}
297406
298407/* !
@@ -342,155 +451,14 @@ bool SimulationOutputHandler::isMaximumDisplayLimitReached() const
342451 return mNumberOfBytes > OptionsDialog::instance ()->getSimulationPage ()->getDisplayLimitSpinBox ()->value () * 1000 ;
343452}
344453
345- /* !
346- * \brief SimulationOutputHandler::startElement
347- * The reader calls this function when it has parsed a start element tag.
348- * \param namespaceURI
349- * \param localName
350- * \param qName
351- * \param atts
352- * \return
353- */
354- bool SimulationOutputHandler::startElement (const QString &namespaceURI, const QString &localName, const QString &qName, const QXmlAttributes &atts)
355- {
356- Q_UNUSED (namespaceURI);
357- Q_UNUSED (localName);
358-
359- // write simulation log file
360- if (qName == " message" ) {
361- QString text = atts.value (" text" ) + " \n " ;
362- mNumberOfBytes += text.toUtf8 ().size ();
363- writeSimulationLog (text);
364- }
365- /* if display limit is reached then close and display the so far text
366- * and display a message showing that the limit is reached.
367- */
368- if (isMaximumDisplayLimitReached ()) {
369- // Only generate the reached display limit message once.
370- if (!mShownDisplayLimitReachedMessage ) {
371- mShownDisplayLimitReachedMessage = true ;
372-
373- while (mLevel > 0 ) {
374- endElement (" " , " " , " message" );
375- }
376-
377- if (mpSimulationOutputWidget->isOutputStructured ()) {
378- mpSimulationMessage = new SimulationMessage (mpSimulationMessageModel->getRootSimulationMessage ());
379- } else {
380- mpSimulationMessage = new SimulationMessage;
381- }
382- mpSimulationMessage->mStream = " stdout" ;
383- mpSimulationMessage->mType = StringHandler::OMEditInfo;
384- mpSimulationMessage->mText = QString (" Reached display limit" );
385- mpSimulationMessage->mLevel = mLevel ;
386- mSimulationMessagesLevelMap .insert (mLevel , mpSimulationMessage);
387- mLevel ++;
388- endElement (" " , " " , " message" );
389- }
390- return true ;
391- }
392-
393- if (qName == " message" ) {
394- if (mpSimulationOutputWidget->isOutputStructured ()) {
395- mpSimulationMessage = new SimulationMessage (mpSimulationMessageModel->getRootSimulationMessage ());
396- } else {
397- mpSimulationMessage = new SimulationMessage;
398- }
399- mpSimulationMessage->mStream = atts.value (" stream" );
400- mpSimulationMessage->mType = StringHandler::getSimulationMessageType (atts.value (" type" ));
401- // check if we get the message about embedded opc-ua server initialized.
402- if (atts.value (" text" ).compare (" The embedded server is initialized." ) == 0 ) {
403- mpSimulationOutputWidget->embeddedServerInitialized ();
404- }
405- if (mpSimulationOutputWidget->isOutputStructured ()) {
406- mpSimulationMessage->mText = Qt::convertFromPlainText (atts.value (" text" ));
407- } else {
408- mpSimulationMessage->mText = atts.value (" text" );
409- }
410- mpSimulationMessage->mLevel = mLevel ;
411- mSimulationMessagesLevelMap .insert (mLevel , mpSimulationMessage);
412- if (mLevel > 0 ) {
413- SimulationMessage *pSimulationMessage = mSimulationMessagesLevelMap .value (mLevel - 1 , 0 );
414- if (pSimulationMessage) {
415- mpSimulationMessage->setParent (pSimulationMessage);
416- if (mpSimulationOutputWidget->isOutputStructured ()) {
417- mpSimulationMessage->mDeweyId = pSimulationMessage->mDeweyId + " ." + QString::number (pSimulationMessage->children ().size () + 1 );
418- mpSimulationMessage->mDeweyId = QString (" %1.%2" ).arg (pSimulationMessage->mDeweyId )
419- .arg (QString::number (pSimulationMessage->children ().size () + 1 ));
420- }
421- pSimulationMessage->mChildren .append (mpSimulationMessage);
422- }
423- } else {
424- if (mpSimulationOutputWidget->isOutputStructured ()) {
425- mpSimulationMessage->mDeweyId = QString (" %1.%2" ).arg (mpSimulationMessageModel->getRootSimulationMessage ()->mDeweyId )
426- .arg (QString::number (mpSimulationMessageModel->getRootSimulationMessage ()->children ().size () + 1 ));
427- }
428- }
429- mLevel ++;
430- } else if (qName == " used" ) {
431- if (mpSimulationMessage) {
432- mpSimulationMessage->mIndex = atts.value (" index" );
433- }
434- } else if (qName == " status" ) {
435- int progress = atts.value (" progress" ).toInt ();
436- mpSimulationOutputWidget->getProgressBar ()->setValue (progress/100 );
437- }
438- return true ;
439- }
440-
441454/* !
442455 * \brief SimulationOutputHandler::endElement
443- * The reader calls this function when it has parsed an end element tag.
444- * \param namespaceURI
445- * \param localName
446- * \param qName
447- * \return
448- */
449- bool SimulationOutputHandler::endElement (const QString &namespaceURI, const QString &localName, const QString &qName)
450- {
451- Q_UNUSED (namespaceURI);
452- Q_UNUSED (localName);
453- if (qName == " message" ) {
454- mLevel --;
455- // if mLevel is 0 then we have finished the one complete top level message tag. Add it to SimulationMessageModel now.
456- if (mLevel == 0 ) {
457- addSimulationMessage (mSimulationMessagesLevelMap .value (0 , 0 ));
458- }
459- }
460- return true ;
461- }
462-
463- /* !
464- * \brief SimulationOutputHandler::fatalError
465- * Reports a non-recoverable error. Details of the error are stored in exception.
466- * \param exception
467- * \return
468456 */
469- bool SimulationOutputHandler::fatalError ( const QXmlParseException &exception )
457+ void SimulationOutputHandler::endElement ( )
470458{
471- if (isMaximumDisplayLimitReached ()) {
472- return false ;
473- }
474- // read the error message
475- QString error = QString (" Fatal error on line %1, column %2: %3" )
476- .arg (exception.lineNumber ())
477- .arg (exception.columnNumber ())
478- .arg (exception.message ());
479- // construct the SimulationMessage object with error
480- SimulationMessage *pSimulationMessage;
481- if (mpSimulationOutputWidget->isOutputStructured ()) {
482- pSimulationMessage = new SimulationMessage (mpSimulationMessageModel->getRootSimulationMessage ());
483- } else {
484- pSimulationMessage = new SimulationMessage;
485- }
486- pSimulationMessage->mStream = " stderr" ;
487- pSimulationMessage->mType = StringHandler::getSimulationMessageType (" error" );
488- pSimulationMessage->mText = error;
489- pSimulationMessage->mLevel = 0 ;
490- if (mpSimulationOutputWidget->isOutputStructured ()) {
491- mpSimulationMessageModel->insertSimulationMessage (pSimulationMessage);
492- } else {
493- mpSimulationOutputWidget->writeSimulationMessage (pSimulationMessage);
459+ mLevel --;
460+ // if mLevel is 0 then we have finished the one complete top level message tag. Add it to SimulationMessageModel now.
461+ if (mLevel == 0 ) {
462+ addSimulationMessage (mSimulationMessagesLevelMap .value (0 , 0 ));
494463 }
495- return false ;
496464}
0 commit comments