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