Subversion Repositories Applications.gtt

Rev

Rev 94 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 94 Rev 187
1
<?php
1
<?php
2
/**
2
/**
3
 * PEAR_FTP
3
 * PEAR_XMLParser
4
 *
4
 *
5
 * PHP versions 4 and 5
5
 * PHP versions 4 and 5
6
 *
6
 *
7
 * LICENSE: This source file is subject to version 3.0 of the PHP license
-
 
8
 * that is available through the world-wide-web at the following URI:
-
 
9
 * http://www.php.net/license/3_0.txt.  If you did not receive a copy of
-
 
10
 * the PHP License and are unable to obtain it through the web, please
-
 
11
 * send a note to license@php.net so we can mail you a copy immediately.
-
 
12
 *
-
 
13
 * @category   pear
7
 * @category   pear
14
 * @package    PEAR
8
 * @package    PEAR
15
 * @author     Greg Beaver <cellog@php.net>
9
 * @author     Greg Beaver <cellog@php.net>
16
 * @author     Stephan Schmidt (original XML_Unserializer code)
10
 * @author     Stephan Schmidt (original XML_Unserializer code)
17
 * @copyright  1997-2006 The PHP Group
11
 * @copyright  1997-2009 The Authors
18
 * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
12
 * @license   http://opensource.org/licenses/bsd-license New BSD License
19
 * @version    CVS: $Id: XMLParser.php,v 1.12 2006/03/27 04:39:03 cellog Exp $
-
 
20
 * @link       http://pear.php.net/package/PEAR
13
 * @link       http://pear.php.net/package/PEAR
21
 * @since      File available since Release 1.4.0a1
14
 * @since      File available since Release 1.4.0a1
22
 */
15
 */
23
 
16
 
24
/**
17
/**
25
 * Parser for any xml file
18
 * Parser for any xml file
26
 * @category   pear
19
 * @category  pear
27
 * @package    PEAR
20
 * @package   PEAR
28
 * @author     Greg Beaver <cellog@php.net>
21
 * @author    Greg Beaver <cellog@php.net>
29
 * @author     Stephan Schmidt (original XML_Unserializer code)
22
 * @author    Stephan Schmidt (original XML_Unserializer code)
30
 * @copyright  1997-2006 The PHP Group
23
 * @copyright 1997-2009 The Authors
31
 * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
24
 * @license   http://opensource.org/licenses/bsd-license New BSD License
32
 * @version    Release: 1.5.1
25
 * @version   Release: 1.10.1
33
 * @link       http://pear.php.net/package/PEAR
26
 * @link      http://pear.php.net/package/PEAR
34
 * @since      Class available since Release 1.4.0a1
27
 * @since     Class available since Release 1.4.0a1
35
 */
28
 */
36
class PEAR_XMLParser
29
class PEAR_XMLParser
37
{
30
{
38
    /**
31
    /**
39
     * unserilialized data
32
     * unserilialized data
40
     * @var string $_serializedData
33
     * @var string $_serializedData
41
     */
34
     */
42
    var $_unserializedData = null;
35
    var $_unserializedData = null;
43
 
36
 
44
    /**
37
    /**
45
     * name of the root tag
38
     * name of the root tag
46
     * @var string $_root
39
     * @var string $_root
47
     */
40
     */
48
    var $_root = null;
41
    var $_root = null;
49
 
42
 
50
    /**
43
    /**
51
     * stack for all data that is found
44
     * stack for all data that is found
52
     * @var array    $_dataStack
45
     * @var array    $_dataStack
53
     */
46
     */
54
    var $_dataStack  =   array();
47
    var $_dataStack = array();
55
 
48
 
56
    /**
49
    /**
57
     * stack for all values that are generated
50
     * stack for all values that are generated
58
     * @var array    $_valStack
51
     * @var array    $_valStack
59
     */
52
     */
60
    var $_valStack  =   array();
53
    var $_valStack = array();
61
 
54
 
62
    /**
55
    /**
63
     * current tag depth
56
     * current tag depth
64
     * @var int    $_depth
57
     * @var int    $_depth
65
     */
58
     */
66
    var $_depth = 0;
59
    var $_depth = 0;
67
 
60
 
68
    /**
61
    /**
-
 
62
     * The XML encoding to use
-
 
63
     * @var string $encoding
-
 
64
     */
-
 
65
    var $encoding = 'ISO-8859-1';
-
 
66
 
-
 
67
    /**
69
     * @return array
68
     * @return array
70
     */
69
     */
71
    function getData()
70
    function getData()
72
    {
71
    {
73
        return $this->_unserializedData;
72
        return $this->_unserializedData;
74
    }
73
    }
75
 
74
 
76
    /**
75
    /**
77
     * @param string xml content
76
     * @param string xml content
78
     * @return true|PEAR_Error
77
     * @return true|PEAR_Error
79
     */
78
     */
80
    function parse($data)
79
    function parse($data)
81
    {
80
    {
82
        if (!extension_loaded('xml')) {
81
        if (!extension_loaded('xml')) {
83
            include_once 'PEAR.php';
82
            include_once 'PEAR.php';
84
            return PEAR::raiseError("XML Extension not found", 1);
83
            return PEAR::raiseError("XML Extension not found", 1);
85
        }
84
        }
86
        $this->_valStack = array();
-
 
87
        $this->_dataStack = array();
85
        $this->_dataStack =  $this->_valStack = array();
88
        $this->_depth = 0;
86
        $this->_depth = 0;
89
 
87
 
90
        if (version_compare(phpversion(), '5.0.0', 'lt')) {
88
        if (
91
            if (strpos($data, 'encoding="UTF-8"')) {
89
            strpos($data, 'encoding="UTF-8"')
92
                $data = utf8_decode($data);
-
 
93
            }
-
 
94
            $xp = xml_parser_create('ISO-8859-1');
-
 
95
        } else {
90
            || strpos($data, 'encoding="utf-8"')
96
            if (strpos($data, 'encoding="UTF-8"')) {
91
            || strpos($data, "encoding='UTF-8'")
97
                $xp = xml_parser_create('UTF-8');
92
            || strpos($data, "encoding='utf-8'")
98
            } else {
93
        ) {
99
                $xp = xml_parser_create('ISO-8859-1');
-
 
100
            }
94
            $this->encoding = 'UTF-8';
-
 
95
        }
-
 
96
 
101
        }
97
        $xp = xml_parser_create($this->encoding);
102
        xml_parser_set_option($xp, XML_OPTION_CASE_FOLDING, 0);
98
        xml_parser_set_option($xp, XML_OPTION_CASE_FOLDING, 0);
103
        xml_set_object($xp, $this);
99
        xml_set_object($xp, $this);
104
        xml_set_element_handler($xp, 'startHandler', 'endHandler');
100
        xml_set_element_handler($xp, 'startHandler', 'endHandler');
105
        xml_set_character_data_handler($xp, 'cdataHandler');
101
        xml_set_character_data_handler($xp, 'cdataHandler');
106
        if (!xml_parse($xp, $data)) {
102
        if (!xml_parse($xp, $data)) {
107
            $msg = xml_error_string(xml_get_error_code($xp));
103
            $msg = xml_error_string(xml_get_error_code($xp));
108
            $line = xml_get_current_line_number($xp);
104
            $line = xml_get_current_line_number($xp);
109
            xml_parser_free($xp);
105
            xml_parser_free($xp);
110
            include_once 'PEAR.php';
106
            include_once 'PEAR.php';
111
            return PEAR::raiseError("XML Error: '$msg' on line '$line'", 2);
107
            return PEAR::raiseError("XML Error: '$msg' on line '$line'", 2);
112
        }
108
        }
113
        xml_parser_free($xp);
109
        xml_parser_free($xp);
114
        return true;
110
        return true;
115
    }
111
    }
116
 
112
 
117
    /**
113
    /**
118
     * Start element handler for XML parser
114
     * Start element handler for XML parser
119
     *
115
     *
120
     * @access private
116
     * @access private
121
     * @param  object $parser  XML parser object
117
     * @param  object $parser  XML parser object
122
     * @param  string $element XML element
118
     * @param  string $element XML element
123
     * @param  array  $attribs attributes of XML tag
119
     * @param  array  $attribs attributes of XML tag
124
     * @return void
120
     * @return void
125
     */
121
     */
126
    function startHandler($parser, $element, $attribs)
122
    function startHandler($parser, $element, $attribs)
127
    {
123
    {
128
        $type = 'string';
-
 
129
 
-
 
130
        $this->_depth++;
124
        $this->_depth++;
131
        $this->_dataStack[$this->_depth] = null;
125
        $this->_dataStack[$this->_depth] = null;
132
 
126
 
133
        $val = array(
127
        $val = array(
134
                     'name'         => $element,
128
            'name'         => $element,
135
                     'value'        => null,
129
            'value'        => null,
136
                     'type'         => $type,
130
            'type'         => 'string',
137
                     'childrenKeys' => array(),
131
            'childrenKeys' => array(),
138
                     'aggregKeys'   => array()
132
            'aggregKeys'   => array()
139
                    );
133
       );
140
 
134
 
141
        if (count($attribs) > 0) {
135
        if (count($attribs) > 0) {
142
            $val['children'] = array();
136
            $val['children'] = array();
143
            $val['type'] = 'array';
137
            $val['type'] = 'array';
144
 
-
 
145
            $val['children']['attribs'] = $attribs;
138
            $val['children']['attribs'] = $attribs;
146
 
-
 
147
        }
139
        }
148
 
140
 
149
        array_push($this->_valStack, $val);
141
        array_push($this->_valStack, $val);
150
    }
142
    }
151
 
143
 
152
    /**
144
    /**
153
     * post-process data
145
     * post-process data
154
     *
146
     *
155
     * @param string $data
147
     * @param string $data
156
     * @param string $element element name
148
     * @param string $element element name
157
     */
149
     */
158
    function postProcess($data, $element)
150
    function postProcess($data, $element)
159
    {
151
    {
160
        return trim($data);
152
        return trim($data);
161
    }
153
    }
162
 
154
 
163
    /**
155
    /**
164
     * End element handler for XML parser
156
     * End element handler for XML parser
165
     *
157
     *
166
     * @access private
158
     * @access private
167
     * @param  object XML parser object
159
     * @param  object XML parser object
168
     * @param  string
160
     * @param  string
169
     * @return void
161
     * @return void
170
     */
162
     */
171
    function endHandler($parser, $element)
163
    function endHandler($parser, $element)
172
    {
164
    {
173
        $value = array_pop($this->_valStack);
165
        $value = array_pop($this->_valStack);
174
        $data  = $this->postProcess($this->_dataStack[$this->_depth], $element);
166
        $data  = $this->postProcess($this->_dataStack[$this->_depth], $element);
175
 
167
 
176
        // adjust type of the value
168
        // adjust type of the value
177
        switch(strtolower($value['type'])) {
169
        switch (strtolower($value['type'])) {
178
 
-
 
179
            /*
-
 
180
             * unserialize an array
170
            // unserialize an array
181
             */
-
 
182
            case 'array':
171
            case 'array':
183
                if ($data !== '') {
172
                if ($data !== '') {
184
                    $value['children']['_content'] = $data;
173
                    $value['children']['_content'] = $data;
185
                }
174
                }
186
                if (isset($value['children'])) {
-
 
-
 
175
 
187
                    $value['value'] = $value['children'];
176
                $value['value'] = isset($value['children']) ? $value['children'] : array();
188
                } else {
-
 
189
                    $value['value'] = array();
-
 
190
                }
-
 
191
                break;
177
                break;
192
 
178
 
193
            /*
179
            /*
194
             * unserialize a null value
180
             * unserialize a null value
195
             */
181
             */
196
            case 'null':
182
            case 'null':
197
                $data = null;
183
                $data = null;
198
                break;
184
                break;
199
 
185
 
200
            /*
186
            /*
201
             * unserialize any scalar value
187
             * unserialize any scalar value
202
             */
188
             */
203
            default:
189
            default:
204
                settype($data, $value['type']);
190
                settype($data, $value['type']);
205
                $value['value'] = $data;
191
                $value['value'] = $data;
206
                break;
192
                break;
207
        }
193
        }
-
 
194
 
208
        $parent = array_pop($this->_valStack);
195
        $parent = array_pop($this->_valStack);
209
        if ($parent === null) {
196
        if ($parent === null) {
210
            $this->_unserializedData = &$value['value'];
197
            $this->_unserializedData = &$value['value'];
211
            $this->_root = &$value['name'];
198
            $this->_root = &$value['name'];
212
            return true;
199
            return true;
213
        } else {
200
        }
-
 
201
 
214
            // parent has to be an array
202
        // parent has to be an array
215
            if (!isset($parent['children']) || !is_array($parent['children'])) {
203
        if (!isset($parent['children']) || !is_array($parent['children'])) {
216
                $parent['children'] = array();
204
            $parent['children'] = array();
217
                if ($parent['type'] != 'array') {
205
            if ($parent['type'] != 'array') {
218
                    $parent['type'] = 'array';
206
                $parent['type'] = 'array';
219
                }
-
 
220
            }
207
            }
-
 
208
        }
221
 
209
 
222
            if (!empty($value['name'])) {
210
        if (!empty($value['name'])) {
223
                // there already has been a tag with this name
211
            // there already has been a tag with this name
224
                if (in_array($value['name'], $parent['childrenKeys'])) {
212
            if (in_array($value['name'], $parent['childrenKeys'])) {
225
                    // no aggregate has been created for this tag
213
                // no aggregate has been created for this tag
226
                    if (!in_array($value['name'], $parent['aggregKeys'])) {
214
                if (!in_array($value['name'], $parent['aggregKeys'])) {
227
                        if (isset($parent['children'][$value['name']])) {
215
                    if (isset($parent['children'][$value['name']])) {
228
                            $parent['children'][$value['name']] = array($parent['children'][$value['name']]);
216
                        $parent['children'][$value['name']] = array($parent['children'][$value['name']]);
229
                        } else {
217
                    } else {
230
                            $parent['children'][$value['name']] = array();
-
 
231
                        }
-
 
232
                        array_push($parent['aggregKeys'], $value['name']);
218
                        $parent['children'][$value['name']] = array();
233
                    }
-
 
234
                    array_push($parent['children'][$value['name']], $value['value']);
-
 
235
                } else {
-
 
236
                    $parent['children'][$value['name']] = &$value['value'];
219
                    }
237
                    array_push($parent['childrenKeys'], $value['name']);
220
                    array_push($parent['aggregKeys'], $value['name']);
-
 
221
                }
238
                }
222
                array_push($parent['children'][$value['name']], $value['value']);
-
 
223
            } else {
239
            } else {
224
                $parent['children'][$value['name']] = &$value['value'];
240
                array_push($parent['children'],$value['value']);
225
                array_push($parent['childrenKeys'], $value['name']);
-
 
226
            }
241
            }
227
        } else {
242
            array_push($this->_valStack, $parent);
228
            array_push($parent['children'],$value['value']);
-
 
229
        }
243
        }
230
        array_push($this->_valStack, $parent);
244
 
231
 
245
        $this->_depth--;
232
        $this->_depth--;
246
    }
233
    }
247
 
234
 
248
    /**
235
    /**
249
     * Handler for character data
236
     * Handler for character data
250
     *
237
     *
251
     * @access private
238
     * @access private
252
     * @param  object XML parser object
239
     * @param  object XML parser object
253
     * @param  string CDATA
240
     * @param  string CDATA
254
     * @return void
241
     * @return void
255
     */
242
     */
256
    function cdataHandler($parser, $cdata)
243
    function cdataHandler($parser, $cdata)
257
    {
244
    {
258
        $this->_dataStack[$this->_depth] .= $cdata;
245
        $this->_dataStack[$this->_depth] .= $cdata;
259
    }
246
    }
260
}
-
 
261
?>
-
 
262
247
}
-
 
248
263
249