root/trunk/patXMLPretty/Engine.php

Revision 35, 3.8 kB (checked in by argh, 3 years ago)

Added some docs.

Line 
1 <?php
2 /**
3  * File containing the patXMLPretty_Engine class.
4  *
5  * @package        patXMLPretty
6  * @subpackage     Engine
7  * @author        Sebastian 'The Argh' Mordziol <argh@php-tools.net>
8  * @see         patXMLPretty_Engine
9  */
10
11 /**
12  * Error: tried to call an abstract method
13  */
14  define( 'PATXMLPRETTY_ENGINE_ERROR_ABSTRACT_METHOD', 'patXMLPretty_Engine:01' );
15
16 /**
17  * Engine renderer class that engine classes extend for common functionality.
18  * Engines are used by patXMLPretty to parse the XML source and create the
19  * elements description tree used by the renderers to construct the 'prettified'
20  * XML source.
21  *
22  * @package        patXMLPretty
23  * @subpackage     Engine
24  * @author        Sebastian 'The Argh' Mordziol <argh@php-tools.net>
25  * @version        0.1.0
26  * @license        LGPL
27  * @link        http://www.php-tools.net
28  * @see         patXMLPretty_Module
29  */
30 class patXMLPretty_Engine extends patXMLPretty_Module
31 {
32    /**
33     * Stores the configuration reader object
34     *
35     * @access    private
36     * @var        object patXMLReader_ConfigReader
37     * @see         setConfigReader()
38     */
39     var $configReader = null;
40
41    /**
42     * Stores the renderer object
43     *
44     * @access    private
45     * @var        object patXMLPretty_Renderer
46     * @see         setRenderer()
47     */
48     var $renderer = null;
49
50    /**
51     * Stores user-safe error messages. These are used in error
52     * objects as the message displayed to the user and are meant
53     * not to disclose any critical application information.
54     *
55     * The detailed error message is stored separately for developers
56     * to use for debugging purposes.
57     *
58     * @access    private
59     * @var        array
60     * @see        patError
61     */
62     var $userMessages = array(
63         'error' => 'XML parsing engine could not start.',
64         'warning' => 'XML parsing engine could not be initialized properly',
65         'notice' => 'XML parsing engine misconfiguration'
66     );
67
68    /**
69     * Sets the configuration reader
70     *
71     * @access    public
72     * @param    object patXMLPretty_ConfigReader
73     * @see         $configReader
74     */
75     function setConfigReader( &$configReader )
76     {
77         $this->configReader =& $configReader;
78     }
79
80    /**
81     * Sets the renderer
82     *
83     * @access    public
84     * @param    object patXMLPretty_Renderer
85     * @see         $renderer
86     */
87     function setRenderer( &$renderer )
88     {
89         $this->renderer =& $renderer;
90     }
91
92    /**
93     * Main engine method that is called by patXMLPretty to parse the
94     * provided XML string and build the renderer-compatible element
95     * definitions tree from.
96     *
97     * @access    public
98     * @param    string
99     * @return    array|object patError
100     */
101     function parse( $xmlSource )
102     {
103         $error = patErrorManager::raiseError(
104             PATXMLPRETTY_ENGINE_ERROR_ABSTRACT_METHOD,
105             $this->userMessages['error'],
106             'The [parse] engine method is an abstract method that cannot be called directly. '.
107             'Chances are you either tried to call this method on the base class or the engine '.
108             'class you are using does not implement it. For reference, my class name is ['.get_class( $this ).'].'
109         );
110
111         return $error;
112     }
113
114    /**
115     * Tries to detect a doctype in the XML source using
116     * regular expressions. If a doctype is matched, this
117     * will return an array with two keys:
118     *
119     * array(
120     *     'name' => 'xxxx'      // name of the doctype
121     *     'elements' => array() // indexed array with raw doctype declaration elements (element|entity|attlist)
122     * );
123     *
124     * @access    private
125     * @param    string
126     * @return    array|null
127     */
128     function _detectDoctype( $xmlSource )
129     {
130         $preg = '|<!DOCTYPE[\s]+([\w]+)[\s]+\[[\s]+(.+?)\]>|is';
131         preg_match( $preg, $xmlSource, $found );
132         if( empty( $found ) ) {
133             $result = null;
134             return $result;
135         }
136
137         $doctype = array();
138         $doctype['name'] = $found[1];
139         $doctype['elements'] = array();
140
141         $itemsPreg = '/<!(?:ELEMENT|ENTITY|ATTLIST).+?>/is';
142         preg_match_all( $itemsPreg, $found[2], $items );
143         if( empty( $items ) ) {
144             return $doctype;
145         }
146
147         foreach( $items[0] as $item ) {
148             $item = trim( $item );
149             $item = str_replace( "\n", '', $item );
150             $item = str_replace( "\t", '', $item );
151             $doctype['elements'][] = $item;
152         }
153
154         return $doctype;
155     }
156 }
157
158 ?>
Note: See TracBrowser for help on using the browser.