2a1c8035df644acf9e97a17790d768fac17d4342
[atutor.git] / mods / patcher / classes / PatchListParser.class.php
1 <?php
2 /************************************************************************/
3 /* ATutor                                                                                                                               */
4 /************************************************************************/
5 /* Copyright (c) 2002-2008 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: PatchListParser.class.php 7208 2008-02-08 16:07:24Z cindy $
14
15 /**
16 * PatchListParser
17 * Class for parsing XML patch list info
18 * @access       public
19 * @author       Cindy Qi Li
20 * @package      Patch
21 */
22 class PatchListParser {
23
24         // all private
25         var $parser; // the XML handler
26         var $patch_rows = array(); // the patch data
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 $dependent_patches_num;
31
32         function PatchListParser() {
33                 $this->parser = xml_parser_create(''); 
34
35                 xml_set_object($this->parser, $this);
36                 xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, false); /* conform to W3C specs */
37                 xml_set_element_handler($this->parser, 'startElement', 'endElement');
38                 xml_set_character_data_handler($this->parser, 'characterData');
39         }
40
41         // public
42         function parse($xml_data) {
43                 $this->element_path   = array();
44                 $this->patch_rows  = array();
45                 $this->character_data = '';
46                 $this->row_num        = 0;
47                 xml_parse($this->parser, $xml_data, TRUE);
48         }
49
50         // private
51         function startElement($parser, $name, $attributes) 
52         {
53                 array_push($this->element_path, $name);
54    }
55
56         // private
57         /* called when an element ends */
58         /* removed the current element from the $path */
59         function endElement($parser, $name) {
60                 if ($this->element_path == array('patch_list', 'patch', 'atutor_patch_id')) 
61                 {
62                         $this->patch_rows[$this->row_num]['atutor_patch_id'] = trim($this->character_data);
63                 } 
64                 else if ($this->element_path === array('patch_list', 'patch', 'applied_version')) 
65                 {
66                         $this->patch_rows[$this->row_num]['applied_version'] = trim($this->character_data);
67                 } 
68                 else if ($this->element_path === array('patch_list', 'patch', 'patch_folder')) 
69                 {
70                         $this->patch_rows[$this->row_num]['patch_folder'] = trim($this->character_data);
71                 } 
72                 else if ($this->element_path === array('patch_list', 'patch', 'description')) 
73                 {
74                         $this->patch_rows[$this->row_num]['description'] = trim($this->character_data);
75                 } 
76                 else if ($this->element_path === array('patch_list', 'patch', 'available_to')) 
77                 {
78                         $this->patch_rows[$this->row_num]['available_to'] = trim($this->character_data);
79                 } 
80                 else if ($this->element_path === array('patch_list', 'patch', 'dependent_patches', 'dependent_patch')) 
81                 {
82                         $this->patch_rows[$this->row_num]['dependent_patches'][$this->dependent_patches_num++] = trim($this->character_data);
83                 }
84                 else if ($this->element_path === array('patch_list', 'patch')) 
85                 {
86                         $this->row_num++;
87                         $this->dependent_patches_num = 0;
88                 }
89
90                 array_pop($this->element_path);
91                 $this->character_data = '';
92         }
93
94         // private
95         function characterData($parser, $data){
96                 $this->character_data .= $data;
97         }
98
99         // public
100         function getNumPathes() 
101         {
102                 return count($this->patch_rows);
103         }
104
105         // public
106         function getParsedArray() 
107         {
108                 return $this->patch_rows;
109         }
110
111         // public
112         // return parsed array only for given name & version
113         function getMyParsedArrayForVersion($version, $who='public') 
114         {
115                 $my_array = array();
116
117                 // filter out the patch for given version
118                 foreach ($this->patch_rows as $key => $row) 
119                 {
120             if ($row['applied_version'] == $version && $row['available_to']==$who)
121                 array_push($my_array, $row);
122                 }
123                 
124                 return $my_array;
125         }
126 }
127
128 ?>