<commit_before>/*=============================================================================

  Library: CTK

  Copyright (c) German Cancer Research Center,
    Division of Medical and Biological Informatics

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.

=============================================================================*/

#include "ctkCmdLineModuleXmlProgressWatcher.h"

#include <QIODevice>
#include <QProcess>
#include <QXmlStreamReader>

#include <QDebug>

namespace {

static QString FILTER_START = "filter-start";
static QString FILTER_NAME = "filter-name";
static QString FILTER_COMMENT = "filter-comment";
static QString FILTER_PROGRESS = "filter-progress";
static QString FILTER_END = "filter-end";

}

//----------------------------------------------------------------------------
class ctkCmdLineModuleXmlProgressWatcherPrivate
{
public:

  ctkCmdLineModuleXmlProgressWatcherPrivate(QIODevice* input, ctkCmdLineModuleXmlProgressWatcher* qq)
    : input(input), process(NULL), readPos(0), q(qq), error(false), currentProgress(0)
  {
    // wrap the content in an artifical root element
    reader.addData("<module-root>");
  }

  ctkCmdLineModuleXmlProgressWatcherPrivate(QProcess* input, ctkCmdLineModuleXmlProgressWatcher* qq)
    : input(input), process(input), readPos(0), q(qq), error(false), currentProgress(0)
  {
    // wrap the content in an artifical root element
    reader.addData("<module-root>");
  }

  void _q_readyRead()
  {
    input->seek(readPos);
    reader.addData(input->readAll());
    readPos = input->pos();
    parseProgressXml();
  }

  void _q_readyReadError()
  {
    emit q->errorDataAvailable(process->readAllStandardError());
  }

  void parseProgressXml()
  {
    QXmlStreamReader::TokenType type = reader.readNext();
    QByteArray outputData;
    while(type != QXmlStreamReader::Invalid)
    {
      switch(type)
      {
      case QXmlStreamReader::NoToken: break;
      case QXmlStreamReader::Characters:
      {
        if (stack.empty())
        {
          QByteArray output(reader.text().toAscii());
          // get rid of a possible newline after the last xml end tag
          if (output.startsWith('\n')) output = output.remove(0,1);
          outputData.append(output);<commit_msg>Do not use Qt 4.8 QStringRef::toAscii() method.<commit_after>/*=============================================================================

  Library: CTK

  Copyright (c) German Cancer Research Center,
    Division of Medical and Biological Informatics

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.

=============================================================================*/

#include "ctkCmdLineModuleXmlProgressWatcher.h"

#include <QIODevice>
#include <QProcess>
#include <QXmlStreamReader>

#include <QDebug>

namespace {

static QString FILTER_START = "filter-start";
static QString FILTER_NAME = "filter-name";
static QString FILTER_COMMENT = "filter-comment";
static QString FILTER_PROGRESS = "filter-progress";
static QString FILTER_END = "filter-end";

}

//----------------------------------------------------------------------------
class ctkCmdLineModuleXmlProgressWatcherPrivate
{
public:

  ctkCmdLineModuleXmlProgressWatcherPrivate(QIODevice* input, ctkCmdLineModuleXmlProgressWatcher* qq)
    : input(input), process(NULL), readPos(0), q(qq), error(false), currentProgress(0)
  {
    // wrap the content in an artifical root element
    reader.addData("<module-root>");
  }

  ctkCmdLineModuleXmlProgressWatcherPrivate(QProcess* input, ctkCmdLineModuleXmlProgressWatcher* qq)
    : input(input), process(input), readPos(0), q(qq), error(false), currentProgress(0)
  {
    // wrap the content in an artifical root element
    reader.addData("<module-root>");
  }

  void _q_readyRead()
  {
    input->seek(readPos);
    reader.addData(input->readAll());
    readPos = input->pos();
    parseProgressXml();
  }

  void _q_readyReadError()
  {
    emit q->errorDataAvailable(process->readAllStandardError());
  }

  void parseProgressXml()
  {
    QXmlStreamReader::TokenType type = reader.readNext();
    QByteArray outputData;
    while(type != QXmlStreamReader::Invalid)
    {
      switch(type)
      {
      case QXmlStreamReader::NoToken: break;
      case QXmlStreamReader::Characters:
      {
        if (stack.empty())
        {
          QByteArray output(reader.text().toString().toAscii());
          // get rid of a possible newline after the last xml end tag
          if (output.startsWith('\n')) output = output.remove(0,1);
          outputData.append(output);
{'fraction_non_alphanumeric': 0.1774048005775131, 'fraction_numerical': 0.003970402454430608, 'mean_word_length': 3.6966101694915254, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7392026', 'n_tokens_mistral': 1553, 'n_tokens_neox': 1506, 'n_words': 429}