Code

Added xmlParse class
[gosa.git] / include / class_xmlParse.inc
1 <?php
2 /*
3  * This code is part of GOsa (https://gosa.gonicus.de)
4  * Copyright (C) 2003  Cajus Pollmeier
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
21 class xmlParse  {
23         /* XML parser */
24         var $parser;
26         /* Selected connection */
27         var $current    = array();
28         var $currentPos = 0;
29         var $depth              = array();
30         var $data               = array();
32         function xmlParse()
33         {
34                 $this->parser = xml_parser_create();
35                 xml_set_object($this->parser, $this);
36                 xml_set_element_handler($this->parser, "tag_open", "tag_close");
37         }
39         function parseMenu($file)
40         {
41                 if (!($fp = @fopen($file, "r"))) {
42                         print_red(sprintf(_("could not open XML input '%s'."),$file));
43                         $this->data = array();
44                         return(false);
45                 }
47                 $data = fread($fp, filesize($file));
49                 $t = split("\n",$data);
50                 $s = preg_replace("/<!DOCTYPE.*>/","<Input>",$data."</Input>");
51                 $data = $t[0]."\n".$s;  
52                 
53                 fclose($fp);
54                 xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, 1);
55                 xml_parser_set_option($this->parser, XML_OPTION_SKIP_WHITE, 0);
56                 xml_parse_into_struct($this->parser, $data, $vals, $index);
58                 $params = array();
59                 $level = array();
60                 
61                 $this->data = array();  
62                 $i = 0 ; 
63                 $current ="";
64                 $dir = "";
65                 foreach($vals as $ele){
66                         if($ele['tag'] =="INPUT") continue;
67                         if($ele['tag'] =="INCLUDE") continue;
68                         if($ele['type'] == "open"){
69                                 $tag = $ele['tag'].$i;
70                 
71                                 if($ele['tag'] != "MENU"){
72                                         $i++;
73                                         $this->currentPos ++ ;
74                                         $this->depth[$this->currentPos] = $tag;
75                                 }
76                                 $current = &$this->data;
77                                 foreach($this->depth as $name){
78                                         $current = &$current[$name];    
79                                 }
80                         }elseif($ele['type']=="close"){
81                                 unset($this->depth[($this->currentPos)]);
82                                 $this->currentPos --;
83                                 if($ele['tag'] == "MENU")
84                                 $dir ="";
85                         }
86                         if($ele['tag'] == "CATEGORY"){
87                                 $current[$dir]['name'] = $ele['value'];
88                         }
89                         if(isset($current['type'])){
90                                 unset($current['type']);
91                         }
92                         if($ele['tag'] == "DIRECTORY"){
93                                 $dir .= preg_replace("/\.directory/","",$ele['value'])."/";
94 //                              $path= $ele['value'];
95 //                              $current[$dir]['path'] = $path;
96                         }
97                         if($ele['tag'] == "CATEGORY"){
98                                 $current[$dir] = $ele['value'];
99                         }
100                         $curback = $current;    
101                 }
102         }
105         function parse($filename)
106         {
107                 if(!file_exists($filename)){
108                         return(false);
109                 }else{ 
110                         $fh= fopen($filename, "r"); 
111                         $xmldata= fread($fh, 100000);
112                         fclose($fh); 
113                         if(!xml_parse($this->parser, chop($xmldata))){
114                                 print(sprintf(_("XML error in %s : %s at line %d"),$filename,
115                                                         xml_error_string(xml_get_error_code($this->parser)),
116                                                         xml_get_current_line_number($this->parser)));
117                         }
118                 }
119                 return(true);
120         }
122         function tag_open($parser, $tag, $attrs)
123         {
124                 $this->currentPos ++ ;
125                 $this->depth[$this->currentPos] = $tag;
127                 $current = &$this->data;
128                 foreach($this->depth as $name){
129                         $current = &$current[$name];    
130                 }
131                 $current = $attrs;
132         }
134         function tag_close($parser, $tag)
135         {
136                 unset($this->depth[($this->currentPos)]);
137                 $this->currentPos --; 
138         }
140         function GetData()
141         {
142                 return ($this->data);
143         }
146 ?>