Subversion Repositories Applications.gtt

Rev

Rev 94 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 94 Rev 187
Line 1... Line 1...
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
 */
Line 23... Line 16...
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
    /**
Line 49... Line 42...
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
     */
Line 54... Line 47...
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
Line 59... Line 52...
59
     */
52
     */
60
    var $_valStack  =   array();
53
    var $_valStack = array();
61
 
54
 
62
    /**
55
    /**
63
     * current tag depth
56
     * current tag depth
Line 64... Line 57...
64
     * @var int    $_depth
57
     * @var int    $_depth
-
 
58
     */
-
 
59
    var $_depth = 0;
-
 
60
 
-
 
61
    /**
-
 
62
     * The XML encoding to use
-
 
63
     * @var string $encoding
65
     */
64
     */
66
    var $_depth = 0;
65
    var $encoding = 'ISO-8859-1';
67
 
66
 
68
    /**
67
    /**
69
     * @return array
68
     * @return array
Line 81... Line 80...
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;
Line 89... Line 87...
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');
Line 123... Line 119...
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;
Line 132... Line 126...
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()
Line 139... Line 133...
139
                    );
133
       );
140
 
134
 
141
        if (count($attribs) > 0) {
135
        if (count($attribs) > 0) {
142
            $val['children'] = array();
-
 
143
            $val['type'] = 'array';
136
            $val['children'] = array();
144
 
-
 
145
            $val['children']['attribs'] = $attribs;
137
            $val['type'] = 'array';
Line 146... Line 138...
146
 
138
            $val['children']['attribs'] = $attribs;
147
        }
139
        }
Line 172... Line 164...
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);
Line 175... Line 167...
175
 
167
 
176
        // adjust type of the value
168
        // adjust type of the value
177
        switch(strtolower($value['type'])) {
-
 
178
 
-
 
179
            /*
169
        switch (strtolower($value['type'])) {
180
             * unserialize an array
-
 
181
             */
170
            // unserialize an array
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'];
-
 
188
                } else {
-
 
189
                    $value['value'] = array();
-
 
190
                }
176
                $value['value'] = isset($value['children']) ? $value['children'] : array();
Line 191... Line 177...
191
                break;
177
                break;
192
 
178
 
193
            /*
179
            /*
Line 203... Line 189...
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
        }
Line 221... Line 209...
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
        }
Line 243... Line 230...
243
        }
230
        array_push($this->_valStack, $parent);
244
 
231
 
Line 245... Line 232...
245
        $this->_depth--;
232
        $this->_depth--;
Line 255... Line 242...
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