root/trunk/patXMLPretty/Engine/Unserializer.php

Revision 47, 4.0 kB (checked in by argh, 3 years ago)

Added an error check.

Line 
1 <?php
2 /**
3  * File containing the patXMLPretty_Engine_Unserializer class.
4  *
5  * @package        patXMLPretty
6  * @subpackage     Engine
7  * @author        Sebastian 'The Argh' Mordziol <argh@php-tools.net>
8  * @see         patXMLPretty_Engine_Unserializer
9  */
10
11 /**
12  * Warning: cannot find XML unserializer class.
13  */
14  define( 'PATXMLPRETTY_ENGINE_UNSERIALIZER_WARNING_MISSING_UNSERIALIZER', 'patXMLPretty_Engine_Unserializer:01' );
15
16 /**
17  * Unserializer XML parsing engine that uses the PEAR::XML_Unserializer
18  * to transform the XML structure into the internal elements tree used
19  * by patXMLPretty renderers.
20  *
21  * @package        patXMLPretty
22  * @subpackage     Engine
23  * @author        Sebastian 'The Argh' Mordziol <argh@php-tools.net>
24  * @version        0.1.0
25  * @license        LGPL
26  * @link        http://www.php-tools.net
27  * @see         patXMLPretty_Engine
28  */
29 class patXMLPretty_Engine_Unserializer extends patXMLPretty_Engine
30 {
31    /**
32     * Stores the name of the engine
33     *
34     * @access    private
35     * @var        string
36     */
37     var $name = 'Unserializer';
38
39    /**
40     * Stores the engine's options:
41     *
42     * <ul>
43     *     <li>
44     *         <b>parseDoctype</b> [yes|no] default: no<br/>
45     *         Sets whether to parse a doctype declaration if present. If the renderer supports it, the doctype will be included in the generated highlighted source.
46     *    </li>
47     * </ul>
48     *
49     * @access    private
50     * @var         array
51     */
52     var $options = array(
53         'parseDoctype' => 'no',
54     );
55
56     var $depth = 0;
57
58     function parse( $xmlSource )
59     {
60         @include_once 'XML/Unserializer.php';
61         if( !class_exists( 'XML_Unserializer' ) ) {
62             $error = patErrorManager::raiseWarning(
63                 PATXMLPRETTY_ENGINE_UNSERIALIZER_WARNING_MISSING_UNSERIALIZER,
64                 $this->userMessages['warning'],
65                 'Could not include the PEAR::XML_Unserializer class.'
66             );
67
68             return $error;
69         }
70
71         $ser =& new XML_Unserializer;
72         $ser->setOption( XML_UNSERIALIZER_OPTION_ATTRIBUTES_PARSE, true );
73         $ser->setOption( XML_UNSERIALIZER_OPTION_ATTRIBUTES_ARRAYKEY, '_attribs' );
74         $ser->unserialize( $xmlSource );
75
76         $data = $ser->getUnserializedData();
77         $doctype = null;
78         if( $this->getOption( 'parseDoctype' ) == 'yes' ) {
79             $doctype = $this->_detectDoctype( $xmlSource );
80         }
81
82         // create the output array with document
83         // information and the elements tree.
84         $output = array(
85             'document' => $this->_create_document(),
86             'elements' => $this->_create_tree( $ser->getRootName(), $data, array() ),
87             'doctype' => $doctype
88         );
89
90         return $output;
91     }
92
93     function _create_document()
94     {
95         $document = array(
96             'encoding' => 'UTF-8',
97             'version' => '1.0',
98             'standalone' => 'yes',
99         );
100
101         return $document;
102     }
103
104     function _create_tree( $parentTag, $tagData )
105     {
106         // the node data collection
107         $nodeData = array();
108         $nodeData['name'] = $parentTag;
109         $nodeData['type'] = 'default';
110         $nodeData['text'] = null;
111         $nodeData['attributes'] = null;
112         $nodeData['children'] = null;
113
114         // attributeless tag
115         if( !is_array( $tagData ) ) {
116             $nodeData['name'] = $parentTag;
117             $nodeData['text'] = $tagData;
118             return $nodeData;
119         }
120
121         // collection of subtags, we go through each
122         if( isset( $tagData[0] ) ) {
123             $nodeData['children'] = array();
124             foreach( $tagData as $row => $tagDef ) {
125                 $nodeData['children'][] = $this->_create_tree( $parentTag, $tagDef );
126             }
127
128             return $nodeData;
129         }
130
131         // regular tag
132         if( isset( $tagData['_attribs'] ) ) {
133             $nodeData['attributes'] = $tagData['_attribs'];
134         }
135
136         if( isset( $tagData['_content'] ) ) {
137             $nodeData['text'] = $tagData['_content'];
138         }
139
140         // determine which subtags there are
141         $subtags = $this->_getSubtags( $tagData );
142
143         // empty tag?
144         if( empty( $subtags ) && is_null( $nodeData['text'] ) ) {
145             return $nodeData;
146         }
147
148         if( empty( $subtags ) ) {
149             return $nodeData;
150         }
151
152         $nodeData['children'] = array();
153         foreach( $subtags as $subtag ) {
154             $nodeData['children'][] = $this->_create_tree( $subtag, $tagData[$subtag] );
155         }
156
157         return $nodeData;
158     }
159
160     function _getSubtags( $tagData )
161     {
162         $subtags = array();
163         foreach( $tagData as $tagName => $tagDef ) {
164             if( $tagName == '_attribs' || $tagName == '_content' || is_numeric( $tagName ) ) {
165                 continue;
166             }
167
168             $subtags[] = $tagName;
169         }
170
171         return $subtags;
172     }
173 }
174
175 ?>
Note: See TracBrowser for help on using the browser.