root/trunk/patXMLPretty/Engine/Classic.php

Revision 36, 5.0 kB (checked in by argh, 3 years ago)

Added the name property; Disabled parsing as this is not finished.

Line 
1 <?php
2
3 class patXMLPretty_Engine_Classic extends patXMLPretty_Engine
4 {
5    /**
6     * Stores the name of the engine
7     *
8     * @access    private
9     * @var        string
10     */
11     var $name = 'Classic';
12
13    /**
14     * Stores the generated output line by line
15     *
16     * @access    private
17     * @var        array
18     */
19     var    $output    =    array();
20
21    /**
22     * Stores a list of all external files present in the XML
23     *
24     * @access     private
25     * @var        array
26     */
27     var    $externalFiles = array();
28
29    /**
30     * Stores the current tag depth in the
31     * parsing stage
32     *
33     * @access     private
34     * @var        int
35     */
36     var $depth = 0;
37
38    /**
39     * Stack of tags found in the current parsing stage
40     *
41     * @access    private
42     * @var        array
43     */
44     var $tagStack = array();
45
46     function parse( $xmlSource )
47     {
48         return array();
49
50         // init XML Parser
51         $parser = xml_parser_create();
52         xml_set_object( $parser, $this );
53         xml_set_element_handler( $parser, '_handle_startElement', '_handle_endElement' );
54         xml_set_character_data_handler( $parser, '_handle_characterData' );
55         xml_set_external_entity_ref_handler( $parser, '_handle_externalEntity' );
56         xml_parser_set_option( $parser, XML_OPTION_CASE_FOLDING, false );
57
58         $success = @xml_parse( $parser, $xmlSource );
59         if( !$success ) {
60             $error = patErrorManager::raiseError(
61                 PATXMLPRETTY_ERROR_XML_PARSING_FAILED,
62                 'XML parsing failed',
63                 'XML Error ['.xml_error_string( xml_get_error_code( $parser ) ).'] '.
64                 'at line ['.xml_get_current_line_number( $parser ).']'
65             );
66
67             xml_parser_free( $parser );
68             return $error;
69         }
70
71         echo '<pre>';
72         print_r( $this->elementsCollection );
73         echo '</pre>';
74
75         xml_parser_free( $parser );
76         $result = true;
77         return $result;
78     }
79
80     var $elementsCollection = array();
81
82     var $infoStack = array();
83
84    /**
85     * Handler for XML start elements
86     *
87     * @access     private
88     * @param    int
89     * @param    string
90     * @param     array
91     * @see         endElement()
92     */
93     function _handle_startElement( $parser, $name, $attributes )
94     {
95         $parent = $this->lastOpenedTag;
96         array_push( $this->tagStack, $name );
97
98         $path = implode( '.', array_slice( $this->tagStack, 0, $this->depth ) );
99         $tagInfo = array(
100             'name' => $name,
101             'attributes' => $attributes,
102             'parent' => $parent,
103             'path' => $path
104         );
105
106         array_push( $this->infoStack, $tagInfo );
107
108         $this->renderer->renderOpeningTag( $name, $attributes, $this->depth );
109
110         $this->mode = 'startElement';
111         $this->lastOpenedTag = $name;
112         $this->depth++;
113     }
114
115 /**
116 *    handle end element
117 *    if the end element contains a namespace calls the eppropriate handler
118 *
119 *    @param    int        $parser        resource id of the current parser
120 *    @param    string    $name        name of the element
121 */
122     function _handle_endElement( $parser, $name )
123     {
124         $this->depth--;
125
126         //    remove the last tag
127         array_pop( $this->tagStack );
128         $tagInfo = array_pop( $this->infoStack );
129
130         if( !is_array( $this->elementsCollection[$tagInfo['path']] ) ) {
131             $this->elementsCollection[$tagInfo['path']] = array();
132         }
133
134         $this->elementsCollection[$tagInfo['path']][] = $tagInfo;
135
136         $this->renderer->renderClosingTag( $name, $this->depth );
137         return true;
138     }
139
140 /**
141 *    handle character data
142 *    if the character data was found between tags using namespaces, the appropriate namesapce handler will be called
143 *
144 *    @param    int        $parser        resource id of the current parser
145 *    @param    string    $data        character data, that was found
146 */
147     function _handle_characterData( $parser, $data )
148     {
149         if( $this->configReader->hideCDATA( $data ) ) {
150             $data = str_repeat( "*", strlen( $data ) );
151         }
152
153         $tag = array_pop( $this->tagStack );
154         array_push( $this->tagStack, $tag );
155         $this->renderer->renderCDATA( $tag, $data, $this->depth );
156     }
157
158 /*
159 *    parse an external entity
160 *
161 *    @param    int        $parser                resource id of the current parser
162 *    @param    string    $openEntityNames    space-separated list of the names of the entities that are open for the parse of this entity (including the name of the referenced entity)
163 *    @param    string    $base                currently empty string
164 *    @param    string    $systemId            system identifier as specified in the entity declaration
165 *    @param    string    $publicId            publicId, is the public identifier as specified in the entity declaration, or an empty string if none was specified; the whitespace in the public identifier will have been normalized as required by the XML spec
166 */
167
168     function _handle_externalEntity( $parser, $openEntityNames, $base, $systemId, $publicId )
169     {
170         if( $systemId )
171         {
172             array_push( $this->externalFiles, $systemId );
173
174             if( $this->configReader->parseEntities() ) {
175                 $this->_parseFile( $systemId );
176             } else {
177                 $url                =    $GLOBALS["PHP_SELF"]."?file=".$systemId;
178                 $this->output[]        =    array(    "indent"    =>    $this->configReader->getIndent( $this->depth ),
179                                                 "tag"        =>    "<b><a href=\"".$url."\" onMouseOver=\"popUp('Click to show source of the external entity.')\" onMouseOut=\"hidePopUp();\"><font color=\"".$this->configReader->getColor( 'externalEntity' )."\">&".$openEntityNames.";</font></a></b> <font color=\"".$this->configReader->getColor( 'externalEntity' )."\">(".$systemId.")</font>" );
180
181                 $this->mode                =    "endElement";
182             }
183         }
184
185         return    true;
186     }
187 }
188 ?>
Note: See TracBrowser for help on using the browser.