]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/XmlParser.php
split is deprecated, replace with explode
[SourceForge/phpwiki.git] / lib / XmlParser.php
1 <?php // -*-php-*-
2 // rcs_id('$Id$');
3 /**
4  * Base XmlParser Class.
5  * Requires the expat.so/.dll, usually enabled by default.
6  * Used by HtmlParser and RssParser.
7  *
8  * @author: Reini Urban
9  *
10  * TODO: Convert more perl Html::Element style to our XmlElement style
11  * Needed additions to XmlElement: 
12  *   Html::Element::parent() <=> XmlElement::parent
13  *   Html::Element::attr()   <=> XmlElement::getAttr()
14  *   Html::Element::tag      <=> XmlElement::_tag
15  *   Html::Element::content_list() <=> ->getContent() ??? or ->_children[]
16  *   all_external_attr_names() <=> 
17  *
18  * Problems:
19  * The HtmlParser object set by xml_parse() doesn't keep its parameters, 
20  * esp. $this->root is lost. So we have to this into a global.
21  */
22
23 /*
24  This file is part of PhpWiki.
25
26  PhpWiki is free software; you can redistribute it and/or modify
27  it under the terms of the GNU General Public License as published by
28  the Free Software Foundation; either version 2 of the License, or
29  (at your option) any later version.
30
31  PhpWiki is distributed in the hope that it will be useful,
32  but WITHOUT ANY WARRANTY; without even the implied warranty of
33  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
34  GNU General Public License for more details.
35
36  You should have received a copy of the GNU General Public License
37  along with PhpWiki; if not, write to the Free Software
38  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
39  */
40
41 /**
42  * class XmlParser - Parse into a tree of XmlElement nodes.
43  * 
44  * PHP Problems:
45  *   inside the handlers no globals are transported, only class vars.
46  *   when leaving the handler class all class vars are destroyed, so we 
47  *   have to copy the root to a global.
48  *
49  */
50 class XmlParser {
51
52     var $_parser, $root, $current, $previous, $parent;
53
54     function XmlParser($encoding = '') { //  "ISO-8859-1"
55         if ($encoding)
56             $this->_parser = xml_parser_create($encoding);
57         else
58             $this->_parser = xml_parser_create(); 
59
60         xml_parser_set_option($this->_parser, XML_OPTION_TARGET_ENCODING, $GLOBALS['charset']);
61
62         //This unfortunately does not work
63         //xml_set_object($this->_parser, &$this);
64
65         xml_set_element_handler($this->_parser,
66                                 array(&$this, 'tag_open'),
67                                 array(&$this, 'tag_close' ));
68         xml_set_character_data_handler($this->_parser,
69                                        array(&$this, 'cdata'));
70         //xml_set_element_handler($this->_parser, "tag_open", "tag_close");
71         //xml_set_character_data_handler($this->_parser, "cdata");
72
73         // Hack: workaround php OO bug
74         unset($GLOBALS['xml_parser_root']);
75     }
76
77     function __destruct() {
78         global $xml_parser_root, $xml_parser_current;
79
80         if (!empty($this->_parser)) xml_parser_free($this->_parser);
81         unset($this->_parser);
82         
83         if (isset($xml_parser_root)) {
84             $xml_parser_root->_destruct();
85             unset($xml_parser_root); // nested parsing forbidden!
86         }
87         unset($xml_parser_current);
88     }
89
90     function tag_open($parser, $name, $attrs='') {
91         $this->_tag = strtolower($name);
92         $node = new XmlElement($this->_tag);
93         if (is_string($attrs) and !empty($attrs)) {
94             // lowercase attr names
95             foreach(explode(' ', $attrs) as $pair) {
96                 if (strstr($pair,"=")) {
97                     list($key,$val) = explode('=', $pair);
98                     $key = strtolower(trim($key));
99                     $val = str_replace(array('"',"'"),'',trim($val));
100                     $node->_attr[$key] = $val;
101                 } else {
102                     $key = str_replace(array('"',"'"),'',strtolower(trim($pair)));
103                     $node->_attr[$key] = $key;
104                 }
105             }
106         } elseif (!empty($attrs) and is_array($attrs)) {
107             foreach ($attrs as $key => $val) {  
108                 $key = strtolower(trim($key));
109                 $val = str_replace(array('"',"'"),'',trim($val));
110                 $node->_attr[$key] = $val;
111             }
112         }
113         if (!is_null($this->current)) {
114             $this->current->_content[] =& $node;    // copy or ref?
115             $node->previous =& $this->current;      // ref to parallel prev
116         }
117         $this->current =& $node;                        // ref 
118         if (empty($this->root)) {
119             $this->root =& $node;                       // ref for === test below
120             $GLOBALS['xml_parser_root'] =& $this->root;  // copy
121         }
122     }
123
124     function tag_close($parser, $name, $attrs='') {
125         $this->current->parent = $this->current;    // copy!
126         $this->current =& $this->current->parent;   // ref!
127         //unset($this->current);
128     }
129
130     function cdata($parser, $data) {
131         if (isset($this->current)) {
132             $this->current->_content[] = $data;
133         } else {
134             trigger_error(sprintf("unparsed content outside tags: %s",$data), E_USER_WARNING);
135         }
136         if ($this->current === $this->root) {   // workaround php OO bug: ref => copy
137             $GLOBALS['xml_parser_root'] =& $this->root; // copy!
138             //$this->root = $this->current;       // copy?
139         }
140     }
141
142     function parse($content, $is_final = true) {
143         xml_parse($this->_parser, $content, $is_final) or 
144             trigger_error(sprintf("XML error: %s at line %d", 
145                                   xml_error_string(xml_get_error_code($this->_parser)), 
146                                   xml_get_current_line_number($this->_parser)),
147                           E_USER_WARNING);
148     }
149
150     function parse_url($file, $debug=false)   {
151         if (get_cfg_var('allow_url_fopen')) {
152             if (!($fp = fopen("$file","r"))) {
153                 trigger_error("Error parse url $file");
154                 return;
155             }
156             $content = "";
157             while ($data = fread($fp, 4096))  {
158                 $content .= $data;
159             }
160             fclose($fp);
161             $this->parse($content);
162         } else {
163             // other url_fopen workarounds: curl, socket (http 80 only)
164             $data = url_get_contents($file);
165             if (empty($data)) {
166                 trigger_error("Error parse url $file");
167                 return;
168             }
169             $this->parse($data);
170         }
171     }
172 }
173
174 // For emacs users
175 // Local Variables:
176 // mode: php
177 // tab-width: 8
178 // c-basic-offset: 4
179 // c-hanging-comment-ender-p: nil
180 // indent-tabs-mode: nil
181 // End:
182 ?>