<commit_before><?php

/**
 * ATUI
 * @author Peter (<EMAIL>)
 * @date July 15, 2017
 * @version 1.0.0
 */

namespace ZKit\ATUI {
    require_once __DIR__ . '/utility/Log.php';

    define('REPORT_PARSER_START', 0);
    define('REPORT_PARSER_TESTCASE_FOUND', 1);
    define('REPORT_PARSER_DONE', 2);

    /**
     * It is one log parser for JUnit format report
     */
    class JUnitParser
    {

        public function parse($resultFile)
        {
            $log = \ZKit\console\utility\LogConsole::getInstance();
            $log->debug('Found a file: ' . $resultFile);

            $data = array();
            $reader = new \XMLReader();
            $reader->open($resultFile, 'UTF-8');
            $step = REPORT_PARSER_START;
            while ($reader->read()) {
                if ($reader->nodeType !== \XMLReader::ELEMENT) {
                    continue;
                }

                $this->findIssue($reader, $data, $step);

                if (REPORT_PARSER_DONE === $step) {
                    $log->info('Found an issue in ' . $resultFile);
                    break;
                }
            }
            $reader->close();
            return $data;
        }

        private function findDescription($reader, &$data)
        {
            if ($reader->read()) {
                if ($reader->nodeType === \XMLReader::CDATA || $reader->nodeType === \XMLReader::TEXT) {
                    $data['steps'] = $reader->value;
                }
            }
        }

        /**
         * find issue taged as failure and error under parent tag testcase
         * @param XMLReader $reader
         * @param array $data the data of the issue
         * @param int $step the step to lookup issue.
         */
        private function findIssue($reader, &$data, &$step)
        {
            switch ($reader->name) {
                case 'testcase':
                    $step = REPORT_PARSER_TESTCASE_FOUND;
                    $data['class'] = $reader->getAttribute('classname');
                    $data['function'] = $reader->getAttribute('name');
                    break;
                case 'error':
                case 'failure':
                    if (REPORT_PARSER_TESTCASE_FOUND === $step) {
                        $data['type'] = $reader->name;
                        $msg = $reader->getAttribute('message');
                        $pos = strpos($msg, '(Session');
                        $data['msg'] = (false === $pos ? $msg : preg_replace("/([\s]{2,})/", '', substr($msg, 0, $pos)));
                        $this->findDescription($reader, $data);
                        $step = REPORT_PARSER_DONE;
                    }
                    break;
            }
        }

    }
}<commit_msg>Fixed the bug to handle appium error<commit_after><?php

/**
 * ATUI
 * @author Peter (<EMAIL>)
 * @date July 15, 2017
 * @version 1.0.0
 */

namespace ZKit\ATUI {
    require_once __DIR__ . '/utility/Log.php';

    define('REPORT_PARSER_START', 0);
    define('REPORT_PARSER_TESTCASE_FOUND', 1);
    define('REPORT_PARSER_DONE', 2);

    /**
     * It is one log parser for JUnit format report
     */
    class JUnitParser
    {

        /**
         * parse the result file for issues.
         * @param string $resultFile the file with path
         * @return array the result parsed<p>
         * The format is:<br/>
         * [
         *  'type'=>$type,
         *  'class'=>$class,
         *  'function'=>$function,
         *  'msg'=>$msg,
         *  'steps'=>$steps
         * ]<br/>
         * <b>$type</b> is 'failure' or 'error'<br/>
         * <b>$msg</b> is brief of issue<br/>
         * <b>$steps</b> is detail of issue description<br/>
         * </p>
         */
        public function parse($resultFile)
        {
            $log = \ZKit\console\utility\LogConsole::getInstance();
            $log->debug('Found a file: ' . $resultFile);

            $data = array();
            $reader = new \XMLReader();
            $reader->open($resultFile, 'UTF-8');
            $step = REPORT_PARSER_START;
            while ($reader->read()) {
                if ($reader->nodeType !== \XMLReader::ELEMENT) {
                    continue;
                }

                $this->findIssue($reader, $data, $step);

                if (REPORT_PARSER_DONE === $step) {
                    $log->info('Found an issue in ' . $resultFile);
                    break;
                }
            }
            $reader->close();
            return $data;
        }

        private function findDescription($reader, &$data)
        {
            if ($reader->read()) {
                if ($reader->nodeType === \XMLReader::CDATA || $reader->nodeType === \XMLReader::TEXT) {
                    $data['steps'] = $reader->value;
                }
            }
        }

        /**
         * find issue taged as failure and error under parent tag testcase
         * @param XMLReader $reader
         * @param array $data the data of the issue
         * @param int $step the step to lookup issue.
         */
        private function findIssue($reader, &$data, &$step)
        {
            switch ($reader->name) {
                case 'testcase':
                    $step = REPORT_PARSER_TESTCASE_FOUND;
                    $data['class'] = $reader->getAttribute('classname');
                    $data['function'] = $reader->getAttribute('name');
                    break;
                case 'error':
                case 'failure':
                    if (REPORT_PARSER_TESTCASE_FOUND === $step) {
                        $data['type'] = $reader->name;
                        $data['msg'] = $this->handleMsg($reader->getAttribute('message'), strlen($data['type']) + strlen($data['class']) + strlen($data['function']));
                        $this->findDescription($reader, $data);
                        $step = REPORT_PARSER_DONE;
                    }
                    break;
            }
        }

        private function handleMsg($message, $lenCost)
        {
            $log = \ZKit\console\utility\LogConsole::getInstance();
            $log->debug('original message: ' . $message);
            $pos = strpos($message, "\n");
            if (false === $pos) {
                $pos = 250;
                $log->warning('NOT find &#10;');
            }
            $log->debug('position---->' . $pos);
            $log->debug('total message: ' . $message);
            return substr($message, 0, $pos > 250 - $lenCost ? (250 - $lenCost) : $pos);
        }

    }
}
{'fraction_non_alphanumeric': 0.14746332518337407, 'fraction_numerical': 0.006112469437652812, 'mean_word_length': 1.4358020096762187, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 64, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '6512583', 'n_tokens_mistral': 1876, 'n_tokens_neox': 1777, 'n_words': 392}