move code up one directory
[atutor.git] / mods / _standard / patcher / classes / PatchListParser.class.php
1 <?php
2 /************************************************************************/
3 /* ATutor                                                               */
4 /************************************************************************/
5 /* Copyright (c) 2002-2010                                              */
6 /* Inclusive Design Institute                                           */
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 * 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', 'author')) 
81                 {
82                         $this->patch_rows[$this->row_num]['author'] = trim($this->character_data);
83                 } 
84                 else if ($this->element_path === array('patch_list', 'patch', 'dependent_patches', 'dependent_patch')) 
85                 {
86                         $this->patch_rows[$this->row_num]['dependent_patches'][$this->dependent_patches_num++] = trim($this->character_data);
87                 }
88                 else if ($this->element_path === array('patch_list', 'patch')) 
89                 {
90                         $this->row_num++;
91                         $this->dependent_patches_num = 0;
92                 }
93
94                 array_pop($this->element_path);
95                 $this->character_data = '';
96         }
97
98         // private
99         function characterData($parser, $data){
100                 $this->character_data .= $data;
101         }
102
103         // public
104         function getNumPathes() 
105         {
106                 return count($this->patch_rows);
107         }
108
109         // public
110         function getParsedArray() 
111         {
112                 return $this->patch_rows;
113         }
114
115         // public
116         // return parsed array only for given name & version
117         function getMyParsedArrayForVersion($version, $who='public') 
118         {
119                 $my_array = array();
120
121                 // filter out the patch for given version
122                 foreach ($this->patch_rows as $key => $row) 
123                 {
124             if ($row['applied_version'] == $version && $row['available_to']==$who)
125                 array_push($my_array, $row);
126                 }
127                 
128                 return $my_array;
129         }
130 }
131
132 ?>