moved code up one level to eliminate the docs subdirectory
[acontent.git] / updater / classes / PatchListParser.class.php
1 <?php
2 /************************************************************************/
3 /* AContent                                                             */
4 /************************************************************************/
5 /* Copyright (c) 2010                                                   */
6 /* Inclusive Design Institute                                           */
7 /*                                                                      */
8 /* This program is free software. You can redistribute it and/or        */
9 /* modify it under the terms of the GNU General Public License          */
10 /* as published by the Free Software Foundation.                        */
11 /************************************************************************/
12
13 /**
14 * PatchListParser
15 * Class for parsing XML patch list info
16 * @access       public
17 * @author       Cindy Qi Li
18 * @package      Patch
19 */
20 class PatchListParser {
21
22         // all private
23         var $parser; // the XML handler
24         var $patch_rows = array(); // the patch data
25         var $character_data; // tmp variable for storing the data
26         var $element_path; // array of element paths (basically a stack)
27         var $row_num;
28         var $dependent_patches_num;
29
30         function PatchListParser() {
31                 $this->parser = xml_parser_create(''); 
32
33                 xml_set_object($this->parser, $this);
34                 xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, false); /* conform to W3C specs */
35                 xml_set_element_handler($this->parser, 'startElement', 'endElement');
36                 xml_set_character_data_handler($this->parser, 'characterData');
37         }
38
39         // public
40         function parse($xml_data) {
41                 $this->element_path   = array();
42                 $this->patch_rows  = array();
43                 $this->character_data = '';
44                 $this->row_num        = 0;
45                 xml_parse($this->parser, $xml_data, TRUE);
46         }
47
48         // private
49         function startElement($parser, $name, $attributes) 
50         {
51                 array_push($this->element_path, $name);
52    }
53
54         // private
55         /* called when an element ends */
56         /* removed the current element from the $path */
57         function endElement($parser, $name) {
58                 if ($this->element_path == array('patch_list', 'patch', 'system_patch_id')) 
59                 {
60                         $this->patch_rows[$this->row_num]['system_patch_id'] = trim($this->character_data);
61                 } 
62                 else if ($this->element_path === array('patch_list', 'patch', 'applied_version')) 
63                 {
64                         $this->patch_rows[$this->row_num]['applied_version'] = trim($this->character_data);
65                 } 
66                 else if ($this->element_path === array('patch_list', 'patch', 'patch_folder')) 
67                 {
68                         $this->patch_rows[$this->row_num]['patch_folder'] = trim($this->character_data);
69                 } 
70                 else if ($this->element_path === array('patch_list', 'patch', 'description')) 
71                 {
72                         $this->patch_rows[$this->row_num]['description'] = trim($this->character_data);
73                 } 
74                 else if ($this->element_path === array('patch_list', 'patch', 'available_to')) 
75                 {
76                         $this->patch_rows[$this->row_num]['available_to'] = trim($this->character_data);
77                 } 
78                 else if ($this->element_path === array('patch_list', 'patch', 'author')) 
79                 {
80                         $this->patch_rows[$this->row_num]['author'] = trim($this->character_data);
81                 } 
82                 else if ($this->element_path === array('patch_list', 'patch', 'dependent_patches', 'dependent_patch')) 
83                 {
84                         $this->patch_rows[$this->row_num]['dependent_patches'][$this->dependent_patches_num++] = trim($this->character_data);
85                 }
86                 else if ($this->element_path === array('patch_list', 'patch')) 
87                 {
88                         $this->row_num++;
89                         $this->dependent_patches_num = 0;
90                 }
91
92                 array_pop($this->element_path);
93                 $this->character_data = '';
94         }
95
96         // private
97         function characterData($parser, $data){
98                 $this->character_data .= $data;
99         }
100
101         // public
102         function getNumPathes() 
103         {
104                 return count($this->patch_rows);
105         }
106
107         // public
108         function getParsedArray() 
109         {
110                 return $this->patch_rows;
111         }
112
113         // public
114         // return parsed array only for given name & version
115         function getMyParsedArrayForVersion($version, $who='public') 
116         {
117                 $my_array = array();
118
119                 // filter out the patch for given version
120                 foreach ($this->patch_rows as $key => $row) 
121                 {
122             if ($row['applied_version'] == $version && $row['available_to']==$who)
123                 array_push($my_array, $row);
124                 }
125                 
126                 return $my_array;
127         }
128 }
129
130 ?>