Code

Updated category creation
[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;
69                         if(!in_array($ele['tag'],array("MENU","CATEGORY","DIRECTORY"))) continue;
71                         if($ele['type'] == "open"){
72                                 $tag = $ele['tag'].$i;
73                 
74                                 if($ele['tag'] != "MENU"){
75                                         $i++;
76                                         $this->currentPos ++ ;
77                                         $this->depth[$this->currentPos] = $tag;
78                                 }
79                                 $current = &$this->data;
80                                 foreach($this->depth as $name){
81                                         $current = &$current[$name];    
82                                 }
83                         }elseif($ele['type']=="close"){
84                                 unset($this->depth[($this->currentPos)]);
85                                 $this->currentPos --;
86                                 if($ele['tag'] == "MENU")
87                                 $dir ="";
88                         }
89                         if(($ele['tag'] == "CATEGORY")&&(!empty($ele['value']))&&(isset($ele['value']))){
90                                 $current[$dir]['name'] = $ele['value'];
91                         }
92                         if(isset($current['type'])){
93                                 unset($current['type']);
94                         }
95                         if($ele['tag'] == "DIRECTORY"){
96                                 $dir .= preg_replace("/\.directory/","",$ele['value'])."/";
97                         }
98                         if(($ele['tag'] == "CATEGORY")&&(!empty($ele['value']))&&(isset($ele['value']))){
99                                 $current[$dir] = $ele['value'];
100                         }
101                         $curback = $current;    
102                 }
103                 print_a($this->data);
104         }
107         function parse($filename)
108         {
109                 if(!file_exists($filename)){
110                         return(false);
111                 }else{ 
112                         $fh= fopen($filename, "r"); 
113                         $xmldata= fread($fh, 100000);
114                         fclose($fh); 
115                         if(!xml_parse($this->parser, chop($xmldata))){
116                                 print(sprintf(_("XML error in %s : %s at line %d"),$filename,
117                                                         xml_error_string(xml_get_error_code($this->parser)),
118                                                         xml_get_current_line_number($this->parser)));
119                         }
120                 }
121                 return(true);
122         }
124         function tag_open($parser, $tag, $attrs)
125         {
126                 $this->currentPos ++ ;
127                 $this->depth[$this->currentPos] = $tag;
129                 $current = &$this->data;
130                 foreach($this->depth as $name){
131                         $current = &$current[$name];    
132                 }
133                 $current = $attrs;
134         }
136         function tag_close($parser, $tag)
137         {
138                 unset($this->depth[($this->currentPos)]);
139                 $this->currentPos --; 
140         }
142         function GetData()
143         {
144                 return ($this->data);
145         }
148 ?>