tagging as ATutor 1.5.4-release
[atutor.git] / include / classes / Module / ModuleParser.class.php
1 <?php
2 /************************************************************************/
3 /* ATutor                                                                                                                               */
4 /************************************************************************/
5 /* Copyright (c) 2002-2004 by Greg Gay, Joel Kronenberg & Heidi Hazelton*/
6 /* Adaptive Technology Resource Centre / University of Toronto                  */
7 /* http://atutor.ca                                                                                                             */
8 /*                                                                                                                                              */
9 /* This program is free software. You can redistribute it and/or                */
10 /* modify it under the terms of the GNU General Public License                  */
11 /* as published by the Free Software Foundation.                                                */
12 /************************************************************************/
13 // $Id$
14
15 /**
16 * ModuleParser
17 * Class for parsing XML module info and returning a Module Object
18 * @access       public
19 * @author       Joel Kronenberg
20 * @package      Module
21 */
22 class ModuleParser {
23
24         // all private
25         var $parser; // the XML handler
26         var $rows = array(); // the module data used for creating the Module Object
27         var $character_data; // tmp variable for storing the data
28         var $element_path; // array of element paths (basically a stack)
29         var $row_num;
30         var $maintainer_num;
31
32         var $maintainers = array();
33         var $attributes;
34
35         function ModuleParser() {
36         }
37
38         // public
39         function parse($xml_data) {
40                 $this->element_path = array();
41                 $this->rows         = array();
42                 $this->character_data = '';
43                 $this->row_num        = 0;
44                 $this->maintainer_num = 0;
45
46                 $this->parser = xml_parser_create(''); 
47
48                 xml_set_object($this->parser, $this);
49                 xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, false); /* conform to W3C specs */
50                 xml_set_element_handler($this->parser, 'startElement', 'endElement');
51                 xml_set_character_data_handler($this->parser, 'characterData');
52                 xml_parse($this->parser, $xml_data, TRUE);
53         }
54
55         // public
56         function getModule($row_num) {
57                 return new Module($this->rows[$row_num]);
58         }
59
60         // public
61         function getNewModule($row_num) {
62                 //return new LanguageEditor($this->language_rows[$row_num]);
63         }
64
65         // private
66         function startElement($parser, $name, $attributes) {
67                 array_push($this->element_path, $name);
68
69                 $this->attributes = $attributes;
70    }
71
72         // private
73         /* called when an element ends */
74         /* removed the current element from the $path */
75         function endElement($parser, $name) {
76                 if ($this->element_path == array('module', 'name')) {
77                         if (isset($this->attributes['lang'])) {
78                                 $this->rows[$this->row_num]['name'][$this->attributes['lang']] = trim($this->character_data);
79                         } else {
80                                 $this->rows[$this->row_num]['name'][] = trim($this->character_data);
81                         }
82
83                 } else if ($this->element_path === array('module', 'description')) {
84                         if (isset($this->attributes['lang'])) {
85                                 $this->rows[$this->row_num]['description'][$this->attributes['lang']] = trim($this->character_data);
86                         } else {
87                                 $this->rows[$this->row_num]['description'][] = trim($this->character_data);
88                         }
89
90                 } else if ($this->element_path === array('module', 'url')) {
91                         $this->rows[$this->row_num]['url'] = trim($this->character_data);
92
93                 } else if ($this->element_path === array('module', 'license')) {
94                         $this->rows[$this->row_num]['license'] = trim($this->character_data);
95
96                 } else if ($this->element_path === array('module', 'maintainers', 'maintainer', 'name')) {
97                         $this->rows[$this->row_num]['maintainers'][$this->maintainer_num]['name'] = trim($this->character_data);
98
99                 } else if ($this->element_path === array('module', 'maintainers', 'maintainer', 'email')) {
100                         $this->rows[$this->row_num]['maintainers'][$this->maintainer_num]['email'] = trim($this->character_data);
101
102                         $this->maintainer_num++;
103
104                 } else if ($this->element_path === array('module', 'release', 'version')) {
105                         $this->rows[$this->row_num]['version'] = trim($this->character_data);
106
107                 } else if ($this->element_path === array('module', 'release', 'date')) {
108                         $this->rows[$this->row_num]['date'] = trim($this->character_data);
109
110                 } else if ($this->element_path === array('module', 'release', 'state')) {
111                         $this->rows[$this->row_num]['state'] = trim($this->character_data);
112
113                 } else if ($this->element_path === array('module', 'release', 'notes')) {
114                         $this->rows[$this->row_num]['notes'] = trim($this->character_data);
115
116                 } else if ($this->element_path === array('module')) {
117                         $this->row_num++;
118                 }
119                 array_pop($this->element_path);
120                 $this->character_data = '';
121         }
122
123         // private
124         function characterData($parser, $data){
125                 $this->character_data .= $data;
126         }
127
128 }
129
130
131 ?>