<commit_before><?php
/**
  * ICalendar
  * The ICal* classes in this file together partially implement RFC 2445.
  * @package ExternalData
  * @subpackage Calendar
 */

/**
  * @package ExternalData
  * @subpackage Calendar
  */
class ICSDataParser extends DataParser
{
    protected $eventClass='ICalEvent';
    protected $calendarClass='ICalendar';
    protected function unfold($text) {
        return str_replace("\n ", "", $text);
    }

    public function getEventCategories()
    {
        return call_user_func(array($this->eventClass, 'getEventCategories'));
    }
    
    public function init($args)
    {
        parent::init($args);

        if (isset($args['EVENT_CLASS'])) {
            $this->setEventClass($args['EVENT_CLASS']);
        }

        if (isset($args['CALENDAR_CLASS'])) {
            $this->setCalendarClass($args['CALENDAR_CLASS']);
        }
        
    }
    
    protected function contentline($line) {
        $contentline = array(
            'name'=>'',
            'value'=>'',
            'params'=>array()
        );

        if (preg_match('/([^":]*(?:"[^"]*"[^":]*)*):(.*)/', $line, $parts)) {
            $params = explode(';', $parts[1]);
            $contentline['name'] = array_shift($params);
            $contentline['value'] = trim(ICalendar::ical_unescape_text($parts[2]));
            $contentline['params'] = array();
            foreach ($params as $param) {
                preg_match("/(.*?)=(.*)/", $param, $param_bits);
                $contentline['params'][$param_bits[1]] = str_replace("\"", "", $param_bits[2]);
            }
        } else {
            Kurogo::log(LOG_WARNING, "Found an invalid ICS line: $line", 'data');
        }
        
        return $contentline;
    }
    
    public function setEventClass($eventClass)
    {
    	if ($eventClass) {
    		if (!class_exists($eventClass)) {
                throw new KurogoConfigurationException("Event class $eventClass not defined");
    		} 
			$this->eventClass = $eventClass;
		}
    }

    public function setCalendarClass($calendarClass)
    {
    	if ($calendarClass) {
    		if (!class_exists($calendarClass)) {
                throw new KurogoConfigurationException("Calendar class $calendarClass not defined");
    		} 
			$this->calendarClass = $calendarClass;
		}
    }
    
    protected function initEvent() {
        $event = new $this->eventClass();
        $event->init($this->initArgs);
        return $event;
    }

    protected function initCalendar() {
        $calendar = new $this->calendarClass();
        $calendar->init($this->initArgs);
        return $calendar;
    }
    
    public function parseData($contents)
    {
        $calendar = $this->initCalendar();
        if (!$calendar instanceOf CalendarInterface) {
            throw new KurogoConfigurationException("Calendar $class $this->calendarClass not a CalendarInterface");
        }
        $nesting = array();
        $nestingType = array();
        $contents = str_replace("\r\n", "\n", $contents);
        $lines = explode("\n", $this->unfold($contents));
        foreach ($lines as $line) {<commit_msg>KGO-656: added a function to trim all leading and trailing whitespace.<commit_after><?php
/**
  * ICalendar
  * The ICal* classes in this file together partially implement RFC 2445.
  * @package ExternalData
  * @subpackage Calendar
 */

/**
  * @package ExternalData
  * @subpackage Calendar
  */
class ICSDataParser extends DataParser
{
    protected $eventClass='ICalEvent';
    protected $calendarClass='ICalendar';
    protected function unfold($text) {
        return str_replace("\n ", "", $text);
    }
    
    protected function cleanWhitespace($contents) {
    	return trim($contents);
    }

    public function getEventCategories()
    {
        return call_user_func(array($this->eventClass, 'getEventCategories'));
    }
    
    public function init($args)
    {
        parent::init($args);

        if (isset($args['EVENT_CLASS'])) {
            $this->setEventClass($args['EVENT_CLASS']);
        }

        if (isset($args['CALENDAR_CLASS'])) {
            $this->setCalendarClass($args['CALENDAR_CLASS']);
        }
        
    }
    
    protected function contentline($line) {
        $contentline = array(
            'name'=>'',
            'value'=>'',
            'params'=>array()
        );

        if (preg_match('/([^":]*(?:"[^"]*"[^":]*)*):(.*)/', $line, $parts)) {
            $params = explode(';', $parts[1]);
            $contentline['name'] = array_shift($params);
            $contentline['value'] = trim(ICalendar::ical_unescape_text($parts[2]));
            $contentline['params'] = array();
            foreach ($params as $param) {
                preg_match("/(.*?)=(.*)/", $param, $param_bits);
                $contentline['params'][$param_bits[1]] = str_replace("\"", "", $param_bits[2]);
            }
        } else {
            Kurogo::log(LOG_WARNING, "Found an invalid ICS line: $line", 'data');
        }
        
        return $contentline;
    }
    
    public function setEventClass($eventClass)
    {
    	if ($eventClass) {
    		if (!class_exists($eventClass)) {
                throw new KurogoConfigurationException("Event class $eventClass not defined");
    		} 
			$this->eventClass = $eventClass;
		}
    }

    public function setCalendarClass($calendarClass)
    {
    	if ($calendarClass) {
    		if (!class_exists($calendarClass)) {
                throw new KurogoConfigurationException("Calendar class $calendarClass not defined");
    		} 
			$this->calendarClass = $calendarClass;
		}
    }
    
    protected function initEvent() {
        $event = new $this->eventClass();
        $event->init($this->initArgs);
        return $event;
    }

    protected function initCalendar() {
        $calendar = new $this->calendarClass();
        $calendar->init($this->initArgs);
        return $calendar;
    }
    
    public function parseData($contents)
    {
        $calendar = $this->initCalendar();
        if (!$calendar instanceOf CalendarInterface) {
            throw new KurogoConfigurationException("Calendar $class $this->calendarClass not a CalendarInterface");
        }
        $nesting = array();
        $nestingType = array();
        //all leading and trailing whitespace will be ignored
        $contents = str_replace("\r\n", "\n", $this->cleanWhitespace($contents));
        $lines = explode("\n", $this->unfold($contents));
        foreach ($lines as $line) {
{'fraction_non_alphanumeric': 0.1551644279759148, 'fraction_numerical': 0.0029334568473058514, 'mean_word_length': 2.4257006874669487, 'pattern_counts': {'":': 4, '<': 5, '<?xml version=': 0, '>': 38, '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': '902008', 'n_tokens_mistral': 1890, 'n_tokens_neox': 1825, 'n_words': 373}