root/trunk/patXMLPretty/ConfigReader.php

Revision 48, 6.6 kB (checked in by argh, 3 years ago)

Added default highlight color.

Line 
1 <?php
2 /**
3  * File containing the patXMLPretty_ConfigReader class.
4  *
5  * @access        public
6  * @package        patXMLPretty
7  * @subpackage    ConfigReader
8  * @see            patXMLPretty_ConfigReader
9  */
10
11 /**
12  * The default color that will be used when some color definitions
13  * are missing. This is ugly on purpose to quickly spot missing
14  * colors.
15  *
16  */
17  define( 'PATXMLPRETTY_CONFIGREADER_DEFAULT_COLOR', 'ff00ff' );
18
19 /**
20  * Configuration reader base class that is extended by the
21  * actual configuration reader classes.
22  *
23  * @access        public
24  * @package        patXMLPretty
25  * @subpackage    ConfigReader
26  * @author        Sebastian 'The Argh' Mordziol <argh@php-tools.net>
27  * @link         http://www.php-tools.net
28  * @link        http://sebastian.mordziol.de
29  * @see            patXMLPretty_Module
30  */
31 class patXMLPretty_ConfigReader extends patXMLPretty_Module
32 {
33    /**
34     * Stores color definitions for the available elements
35     *
36     * @access    private
37     * @var        array
38     * @see         getColor()
39     */
40     var $colors = array(
41         'cdata' => null,
42         'tag' => null,
43         'attribName' => null,
44         'attribValue' => null,
45         'externalEntity' => null,
46         'namespace' => null,
47         'brackets' => null,
48         'commentBrackets' => null,
49         'commentValue' => null,
50         'xmlDeclaration' => null,
51         'xmlDeclarationAttribName' => null,
52         'xmlDeclarationAttribValue' => null,
53         'xmlDeclarationTag' => null,
54         'cdataTag' => null,
55         'cdataContent' => null,
56         'entityReference' => null,
57     );
58
59    /**
60     * List of tag names and the colours to render them in.
61     *
62     * @access    private
63     * @var        array
64     * @see        getTagColor()
65     */
66     var $tagColors = array();
67
68    /**
69     * List of namespaces and the colours to render them in.
70     *
71     * @access    private
72     * @var        array
73     * @see        getNamespaceColor()
74     */
75     var $namespaceColors = array();
76
77    /**
78     * List of tag names to keep the line in (not to make
79     * linebreaks for CDATA in those tags)
80     *
81     * @access    private
82     * @var        array
83     * @see        keepLine()
84     */
85     var $keepLine = array();
86
87    /**
88     * The maximum string length up to which to automatically
89     * keep a line. If a tag's CDATA content does not exceed
90     * this length, the line will automatically be kept. Set to
91     * 0 to disable this feature.
92     *
93     * @access    private
94     * @var        int
95     * @see        cdataKeepLine()
96     */
97     var $cdataKeepLine = 0;
98
99    /**
100     * CDATA wrap length: if the content of a CDATA section
101     * exceeds the specified length, it will automatically
102     * be wrapped to this length.
103     *
104     * @access    private
105     * @var        int
106     * @see        getContentWrapLength()
107     */
108     var $contentWrapLength = null;
109
110    /**
111     * List of tag names in which to hide the CDATA content
112     *
113     * @access    private
114     * @var        array
115     * @see        hideCDATA()
116     */
117     var $hideCDATA = array();
118
119    /**
120     * Stores the definition for the inline documentation.
121     *
122     * @access    private
123     * @var        array
124     * @see        hasInlineDoc()
125     * @see         getInlineDoc()
126     */
127     var $inlineDoc = array();
128
129    /**
130     * The indent amount, i.e. the amount of times
131     * the indent character will be repeated for each
132     * level of indentation in the generated source.
133     *
134     * Example: setting this to 4 will by default generate
135     * 4 spaces for each indentation level.
136     *
137     * Note: the way the indentation is rendered depends
138     * on the selected renderer.
139     *
140     * @access    private
141     * @var        int
142     * @see        getIndent()
143     */
144     var $indent = 0;
145
146    /**
147     * Stores the base indentation amount to use for the
148     * whole document - this will be applied to all lines
149     * in the document on top of the indentation amount
150     * for each level.
151     *
152     * @access    private
153     * @var        int
154     * @see        getIndent()
155     */
156     var $baseIndent = 0;
157
158    /**
159     * Checks whether to keep contents on the same line
160     * for the specified tag.
161     *
162     * @access    public
163     * @param    string
164     * @return    bool
165     * @see         $keepLine
166     */
167     function keepLine( $tag )
168     {
169         $tag = strtolower( $tag );
170         $result = in_array( $tag, $this->keepLine );
171         return $result;
172     }
173
174     function wrapCDATA()
175     {
176         $result = !is_null( $this->contentWrapLength );
177         return $result;
178     }
179
180     function getContentWrapLength()
181     {
182         return $this->contentWrapLength;
183     }
184
185     function cdataKeepLine( $data )
186     {
187         $len = strlen( $data );
188         if( $len <= $this->cdataKeepLine ) {
189             return true;
190         }
191
192         return false;
193     }
194
195    /**
196     * Checks whether to hide cdata in the given tag
197     *
198     * @access    public
199     * @param    string
200     * @return    bool
201     * @see         $hideCDATA
202     */
203     function hideCDATA( $tag )
204     {
205         $tag = strtolower( $tag );
206         $result = in_array( $tag, $this->hideCDATA );
207         return $result;
208     }
209
210    /**
211     * Retrieves the indent value for the given tag depth.
212     *
213     * @access    public
214     * @param    int
215     * @return    int
216     * @see         $indent
217     * @see         $baseIndent
218     */
219     function getIndent( $depth )
220     {
221         $indent = ( $this->indent * $depth ) + $this->baseIndent;
222         return $indent;
223     }
224
225    /**
226     * Retrieves the color for the specified element.
227     *
228     * Available elements:
229     * <ul>
230     *     <li>cdata</li>
231     *     <li>tag</li>
232     *     <li>attribName</li>
233     *     <li>attribValue</li>
234     *     <li>externalEntity</li>
235     *     <li>namespace</li>
236     *     <li>brackets</li>
237     *     <li>commentBrackets</li>
238     *     <li>commentValue</li>
239     *     <li>xmlDeclaration</li>
240     *     <li>xmlDeclarationAttribName</li>
241     *     <li>xmlDeclarationAttribValue</li>
242     *     <li>xmlDeclarationTag</li>
243     *     <li>cdataTag</li>
244     *     <li>cdataContent</li>
245     *     <li>entityReference</li>
246     * </ul>
247     *
248     * @access    public
249     * @param    string
250     * @return    string
251     * @see         getTagColor()
252     * @see         getNamespaceColor()
253     * @see         $colors
254     */
255     function getColor( $element )
256     {
257         if( !isset( $this->colors[$element] ) ) {
258             return PATXMLPRETTY_CONFIGREADER_DEFAULT_COLOR;
259         }
260
261         return $this->colors[$element];
262     }
263
264    /**
265     * Retrieves the color for a specific tag. If no custom color
266     * is defined for that tag, the global tag color is used.
267     *
268     * @access    public
269     * @param    string
270     * @return    string
271     * @see         getColor()
272     * @see         $tagColors
273     */
274     function getTagColor( $tag )
275     {
276         $tag = strtolower( $tag );
277         if( isset( $this->tagColors[$tag] ) ) {
278             return $this->tagColors[$tag];
279         }
280
281         return $this->getColor( 'tag' );
282     }
283
284    /**
285     * Retrieves the color of a specific namespace. If there is
286     * no custom color for that namespace, the global namespace
287     * color is used.
288     *
289     * @access    public
290     * @param    string
291     * @return    string
292     * @see         getColor()
293     * @see         $namespaceColors
294     */
295     function getNamespaceColor( $namespace )
296     {
297         $namespace = strtolower( $namespace );
298         if( isset( $this->namespaceColors[$namespace] ) ) {
299             return $this->namespaceColors[$namespace];
300         }
301
302         return $this->getColor( 'namespace' );
303     }
304
305     function read()
306     {
307         $result = true;
308         return $result;
309     }
310
311
312     function hasInlineDoc( $tag, $attribute = null )
313     {
314         $tag = strtolower( $tag );
315         if( !isset( $this->inlineDoc[$tag] ) ) {
316             return false;
317         }
318
319         if( !is_null( $attribute ) ) {
320             $attribute = strtolower( $attribute );
321             if( !isset( $this->inlineDoc[$tag]['attribs'][$attribute] ) ) {
322                 return false;
323             }
324         }
325
326         return true;
327     }
328
329 }
330
331 ?>
Note: See TracBrowser for help on using the browser.