Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
3
 
4
/**
5
 * XML_Beautifier
6
 *
7
 * XML Beautifier package
8
 *
9
 * PHP versions 4 and 5
10
 *
11
 * LICENSE:
12
 *
13
 * Copyright (c) 2003-2008 Stephan Schmidt <schst@php.net>
14
 * All rights reserved.
15
 *
16
 * Redistribution and use in source and binary forms, with or without
17
 * modification, are permitted provided that the following conditions
18
 * are met:
19
 *
20
 *    * Redistributions of source code must retain the above copyright
21
 *      notice, this list of conditions and the following disclaimer.
22
 *    * Redistributions in binary form must reproduce the above copyright
23
 *      notice, this list of conditions and the following disclaimer in the
24
 *      documentation and/or other materials provided with the distribution.
25
 *    * The name of the author may not be used to endorse or promote products
26
 *      derived from this software without specific prior written permission.
27
 *
28
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
29
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
30
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
31
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
32
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
33
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
34
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
35
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
36
 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
37
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
38
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39
 *
40
 * @category  XML
41
 * @package   XML_Beautifier
42
 * @author    Stephan Schmidt <schst@php.net>
43
 * @copyright 2003-2008 Stephan Schmidt <schst@php.net>
44
 * @license   http://opensource.org/licenses/bsd-license New BSD License
45
 * @version   CVS: $Id: Beautifier.php 265438 2008-08-24 19:44:14Z ashnazg $
46
 * @link      http://pear.php.net/package/XML_Beautifier
47
 */
48
 
49
 
50
/**
51
 * define include path constant
52
 */
53
if (!defined('XML_BEAUTIFIER_INCLUDE_PATH')) {
54
    define('XML_BEAUTIFIER_INCLUDE_PATH', 'XML/Beautifier');
55
}
56
 
57
/**
58
 * element is empty
59
 */
60
define('XML_BEAUTIFIER_EMPTY', 0);
61
 
62
/**
63
 * CData
64
 */
65
define('XML_BEAUTIFIER_CDATA', 1);
66
 
67
/**
68
 * XML element
69
 */
70
define('XML_BEAUTIFIER_ELEMENT', 2);
71
 
72
/**
73
 * processing instruction
74
 */
75
define('XML_BEAUTIFIER_PI', 4);
76
 
77
/**
78
 * entity
79
 */
80
define('XML_BEAUTIFIER_ENTITY', 8);
81
 
82
/**
83
 * comment
84
 */
85
define('XML_BEAUTIFIER_COMMENT', 16);
86
 
87
/**
88
 * XML declaration
89
 */
90
define('XML_BEAUTIFIER_XML_DECLARATION', 32);
91
 
92
/**
93
 * doctype declaration
94
 */
95
define('XML_BEAUTIFIER_DT_DECLARATION', 64);
96
 
97
/**
98
 * cdata section
99
 */
100
define('XML_BEAUTIFIER_CDATA_SECTION', 128);
101
 
102
/**
103
 * default
104
 */
105
define('XML_BEAUTIFIER_DEFAULT', 256);
106
 
107
/**
108
 * overwrite the original file
109
 */
110
define('XML_BEAUTIFIER_OVERWRITE', -1);
111
 
112
/**
113
 * could not write to output file
114
 */
115
define('XML_BEAUTIFIER_ERROR_NO_OUTPUT_FILE', 151);
116
 
117
/**
118
 * could not load renderer
119
 */
120
define('XML_BEAUTIFIER_ERROR_UNKNOWN_RENDERER', 152);
121
 
122
/**
123
 * XML_Beautifier is a class that adds linebreaks and
124
 * indentation to your XML files. It can be used on XML
125
 * that looks ugly (e.g. any generated XML) to transform it
126
 * to a nicely looking XML that can be read by humans.
127
 *
128
 * It removes unnecessary whitespace and adds indentation
129
 * depending on the nesting level.
130
 *
131
 * It is able to treat tags, data, processing instructions
132
 * comments, external entities and the XML prologue.
133
 *
134
 * XML_Beautifier is using XML_Beautifier_Tokenizer to parse an XML
135
 * document with a SAX based parser and builds tokens of tags, comments,
136
 * entities, data, etc.
137
 * These tokens will be serialized and indented by a renderer
138
 * with your indent string.
139
 *
140
 * Example 1: Formatting a file
141
 * <code>
142
 * require_once 'XML/Beautifier.php';
143
 * $fmt = new XML_Beautifier();
144
 * $result = $fmt->formatFile('oldFile.xml', 'newFile.xml');
145
 * </code>
146
 *
147
 * Example 2: Formatting a string
148
 * <code>
149
 * require_once 'XML/Beautifier.php';
150
 * $xml = '<root><foo   bar = "pear"/></root>';
151
 * $fmt = new XML_Beautifier();
152
 * $result = $fmt->formatString($xml);
153
 * </code>
154
 *
155
 * @category  XML
156
 * @package   XML_Beautifier
157
 * @author    Stephan Schmidt <schst@php.net>
158
 * @copyright 2003-2008 Stephan Schmidt <schst@php.net>
159
 * @license   http://opensource.org/licenses/bsd-license New BSD License
160
 * @version   Release: @package_version@
161
 * @link      http://pear.php.net/package/XML_Beautifier
162
 */
163
class XML_Beautifier
164
{
165
    /**
166
     * default options for the output format
167
     * @var    array
168
     * @access private
169
     */
170
     var $_defaultOptions = array(
171
         "removeLineBreaks"   => true,
172
         "removeLeadingSpace" => true,       // not implemented, yet
173
         "indent"             => "    ",
174
         "linebreak"          => "\n",
175
         "caseFolding"        => false,
176
         "caseFoldingTo"      => "uppercase",
177
         "normalizeComments"  => false,
178
         "maxCommentLine"     => -1,
179
         "multilineTags"      => false
180
      );
181
 
182
    /**
183
     * options for the output format
184
     * @var    array
185
     * @access private
186
     */
187
     var $_options = array();
188
 
189
    /**
190
     * Constructor
191
     *
192
     * This is only used to specify the options of the
193
     * beautifying process.
194
     *
195
     * @param array $options options that override default options
196
     *
197
     * @access public
198
     */
199
    function XML_Beautifier($options = array())
200
    {
201
        $this->_options = array_merge($this->_defaultOptions, $options);
202
        $this->folding  = false;
203
    }
204
 
205
    /**
206
     * reset all options to default options
207
     *
208
     * @return void
209
     * @access public
210
     * @see setOption(), XML_Beautifier(), setOptions()
211
     */
212
    function resetOptions()
213
    {
214
        $this->_options = $this->_defaultOptions;
215
    }
216
 
217
    /**
218
     * set an option
219
     *
220
     * You can use this method if you do not want
221
     * to set all options in the constructor
222
     *
223
     * @param string $name  option name
224
     * @param mixed  $value option value
225
     *
226
     * @return void
227
     * @access public
228
     * @see resetOptions(), XML_Beautifier(), setOptions()
229
     */
230
    function setOption($name, $value)
231
    {
232
        $this->_options[$name] = $value;
233
    }
234
 
235
    /**
236
     * set several options at once
237
     *
238
     * You can use this method if you do not want
239
     * to set all options in the constructor
240
     *
241
     * @param array $options an options array
242
     *
243
     * @return void
244
     * @access   public
245
     * @see      resetOptions(), XML_Beautifier()
246
     */
247
    function setOptions($options)
248
    {
249
        $this->_options = array_merge($this->_options, $options);
250
    }
251
 
252
    /**
253
     * format a file or URL
254
     *
255
     * @param string $file     filename
256
     * @param mixed  $newFile  filename for beautified XML file
257
     *                         (if none is given, the XML string
258
     *                         will be returned).
259
     *                         if you want overwrite the original file,
260
     *                         use XML_BEAUTIFIER_OVERWRITE
261
     * @param string $renderer Renderer to use,
262
     *                         default is the plain xml renderer
263
     *
264
     * @return mixed XML string of no file should be written,
265
     *               true if file could be written
266
     * @access public
267
     * @throws PEAR_Error
268
     * @uses _loadRenderer() to load the desired renderer
269
     * @todo PEAR CS - should require_once be include_once?
270
     */
271
    function formatFile($file, $newFile = null, $renderer = "Plain")
272
    {
273
        if ($newFile == XML_BEAUTIFIER_OVERWRITE) {
274
            $newFile = $file;
275
        }
276
 
277
        /**
278
         * Split the document into tokens
279
         * using the XML_Tokenizer
280
         */
281
        require_once XML_BEAUTIFIER_INCLUDE_PATH . '/Tokenizer.php';
282
        $tokenizer = new XML_Beautifier_Tokenizer();
283
 
284
        $tokens = $tokenizer->tokenize($file, true);
285
 
286
        if (PEAR::isError($tokens)) {
287
            return $tokens;
288
        }
289
 
290
        $renderer = $this->_loadRenderer($renderer, $this->_options);
291
 
292
        if (PEAR::isError($renderer)) {
293
            return $renderer;
294
        }
295
 
296
        $xml = $renderer->serialize($tokens);
297
 
298
        if ($newFile == null) {
299
            return $xml;
300
        }
301
 
302
        $fp = @fopen($newFile, "w");
303
        if (!$fp) {
304
            return PEAR::raiseError("Could not write to output file",
305
                XML_BEAUTIFIER_ERROR_NO_OUTPUT_FILE);
306
        }
307
 
308
        flock($fp, LOCK_EX);
309
        fwrite($fp, $xml);
310
        flock($fp, LOCK_UN);
311
        fclose($fp);
312
        return true;
313
    }
314
 
315
    /**
316
     * format an XML string
317
     *
318
     * @param string $string   XML
319
     * @param string $renderer the renderer type
320
     *
321
     * @return string formatted XML string
322
     * @access public
323
     * @throws PEAR_Error
324
     * @todo PEAR CS - should require_once be include_once?
325
     */
326
    function formatString($string, $renderer = "Plain")
327
    {
328
        /**
329
         * Split the document into tokens
330
         * using the XML_Tokenizer
331
         */
332
        require_once XML_BEAUTIFIER_INCLUDE_PATH . '/Tokenizer.php';
333
        $tokenizer = new XML_Beautifier_Tokenizer();
334
 
335
        $tokens = $tokenizer->tokenize($string, false);
336
 
337
        if (PEAR::isError($tokens)) {
338
            return $tokens;
339
        }
340
 
341
        $renderer = $this->_loadRenderer($renderer, $this->_options);
342
 
343
        if (PEAR::isError($renderer)) {
344
            return $renderer;
345
        }
346
 
347
        $xml = $renderer->serialize($tokens);
348
 
349
        return $xml;
350
    }
351
 
352
    /**
353
     * load a renderer
354
     *
355
     * Renderers are used to serialize the XML tokens back
356
     * to an XML string.
357
     *
358
     * Renderers are located in the XML/Beautifier/Renderer directory.
359
     *
360
     * NOTE:  the "@" error suppression is used in this method
361
     *
362
     * @param string $name    name of the renderer
363
     * @param array  $options options for the renderer
364
     *
365
     * @return object renderer
366
     * @access private
367
     * @throws PEAR_Error
368
     */
369
    function &_loadRenderer($name, $options = array())
370
    {
371
        $file  = XML_BEAUTIFIER_INCLUDE_PATH . "/Renderer/$name.php";
372
        $class = "XML_Beautifier_Renderer_$name";
373
 
374
        @include_once $file;
375
        if (!class_exists($class)) {
376
            return PEAR::raiseError("Could not load renderer.",
377
                XML_BEAUTIFIER_ERROR_UNKNOWN_RENDERER);
378
        }
379
 
380
        $renderer = &new $class($options);
381
 
382
        return $renderer;
383
    }
384
 
385
    /**
386
     * return API version
387
     *
388
     * @access   public
389
     * @static
390
     * @return   string  $version API version
391
     */
392
    function apiVersion()
393
    {
394
        return "1.0";
395
    }
396
}
397
?>