c936c9debf95d3c589fd1852d7df93be43d88617
[acontent.git] / docs / updater / classes / PatchParser.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 * PatchParser
15 * Class for parsing XML patch info (patch.xml)
16 * @access       public
17 * @author       Cindy Qi Li
18 * @package      Patch
19 */
20 class PatchParser {
21
22         // all private
23         var $parser; // the XML handler
24         var $patch_row = 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 $file_num;
28         var $action_detail_num;
29         var $dependent_patches_num;
30
31         function PatchParser() {
32                 $this->parser = xml_parser_create(''); 
33
34                 xml_set_object($this->parser, $this);
35                 xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, false); /* conform to W3C specs */
36                 xml_set_element_handler($this->parser, 'startElement', 'endElement');
37                 xml_set_character_data_handler($this->parser, 'characterData');
38         }
39
40         // public
41         function parse($xml_data) {
42                 $this->element_path   = array();
43                 $this->patch_row  = array();
44                 $this->character_data = '';
45                 $this->file_num = 0;
46                 $this->action_detail_num = 0;
47                 $this->dependent_patches_num = 0;
48                 
49                 xml_parse($this->parser, $xml_data, TRUE);
50         }
51
52         // private
53         function startElement($parser, $name, $attributes) 
54         {
55                 array_push($this->element_path, $name);
56   }
57
58         // private
59         /* called when an element ends */
60         /* removed the current element from the $path */
61         function endElement($parser, $name)
62         {
63                 if ($this->element_path == array('patch', 'system_patch_id')) 
64                 {
65                         $this->patch_row['system_patch_id'] = trim($this->character_data);
66                 }
67                 if ($this->element_path == array('patch', 'applied_version')) 
68                 {
69                         $this->patch_row['applied_version'] = trim($this->character_data);
70                 }
71                 if ($this->element_path == array('patch', 'sequence')) 
72                 {
73                         $this->patch_row['sequence'] = trim($this->character_data);
74                 }
75                 if ($this->element_path == array('patch', 'description')) 
76                 {
77                         $this->patch_row['description'] = trim($this->character_data);
78                 }
79                 if ($this->element_path === array('patch', 'dependent_patches', 'dependent_patch')) 
80                 {
81                         $this->patch_row['dependent_patches'][$this->dependent_patches_num++] = trim($this->character_data);
82                 }
83                 if ($this->element_path == array('patch', 'sql')) 
84                 {
85                         $this->patch_row['sql'] = trim($this->character_data);
86                 }
87                 else if ($this->element_path === array('patch', 'files', 'file', 'action')) 
88                 {
89                         $this->patch_row['files'][$this->file_num]['action'] = trim($this->character_data);
90                 } 
91                 else if ($this->element_path === array('patch', 'files', 'file', 'name')) 
92                 {
93                         $this->patch_row['files'][$this->file_num]['name'] = trim($this->character_data);
94                 } 
95                 else if ($this->element_path === array('patch', 'files', 'file', 'location')) 
96                 {
97                         $this->patch_row['files'][$this->file_num]['location'] = trim($this->character_data);
98                 } 
99                 else if ($this->element_path === array('patch', 'files', 'file', 'action_detail', 'type')) 
100                 {
101                         $this->patch_row['files'][$this->file_num]['action_detail'][$this->action_detail_num]['type'] = trim($this->character_data);
102                 } 
103                 else if ($this->element_path === array('patch', 'files', 'file', 'action_detail', 'code_from')) 
104                 {
105                         $this->patch_row['files'][$this->file_num]['action_detail'][$this->action_detail_num]['code_from'] = trim($this->character_data);
106                 } 
107                 else if ($this->element_path === array('patch', 'files', 'file', 'action_detail', 'code_to')) 
108                 {
109                         $this->patch_row['files'][$this->file_num]['action_detail'][$this->action_detail_num]['code_to'] = trim($this->character_data);
110                 } 
111                 else if ($this->element_path === array('patch', 'files', 'file')) 
112                 {
113                         $this->file_num++;
114                 }
115                 else if ($this->element_path === array('patch', 'files', 'file', 'action_detail')) 
116                 {
117                         $this->action_detail_num++;
118                 }
119
120                 array_pop($this->element_path);
121                 $this->character_data = '';
122         }
123
124         // private
125   function characterData($parser, $data)
126   {
127                 $this->character_data .= $data;
128         }
129
130         // public
131         function getParsedArray() 
132         {
133                 return $this->patch_row;
134         }
135 }
136
137 ?>