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's Plain Renderer
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: Plain.php 265438 2008-08-24 19:44:14Z ashnazg $
46
 * @link      http://pear.php.net/package/XML_Beautifier
47
 */
48
 
49
/**
50
 * XML_Util is needed to create the tags
51
 */
52
require_once 'XML/Util.php';
53
 
54
/**
55
 * Renderer base class
56
 */
57
require_once XML_BEAUTIFIER_INCLUDE_PATH . '/Renderer.php';
58
 
59
/**
60
 * Basic XML Renderer for XML Beautifier
61
 *
62
 * @category  XML
63
 * @package   XML_Beautifier
64
 * @author    Stephan Schmidt <schst@php.net>
65
 * @copyright 2003-2008 Stephan Schmidt <schst@php.net>
66
 * @license   http://opensource.org/licenses/bsd-license New BSD License
67
 * @version   Release: @package_version@
68
 * @link      http://pear.php.net/package/XML_Beautifier
69
 * @todo      option to specify inline tags
70
 * @todo      option to specify treatment of whitespace in data sections
71
 */
72
class XML_Beautifier_Renderer_Plain extends XML_Beautifier_Renderer
73
{
74
    /**
75
     * Serialize the XML tokens
76
     *
77
     * @param array $tokens XML tokens
78
     *
79
     * @return string XML document
80
     * @access public
81
     */
82
    function serialize($tokens)
83
    {
84
        $tokens = $this->normalize($tokens);
85
 
86
        $xml = '';
87
        $cnt = count($tokens);
88
        for ($i = 0; $i < $cnt; $i++) {
89
            $xml .= $this->_serializeToken($tokens[$i]);
90
        }
91
        return $xml;
92
    }
93
 
94
    /**
95
     * serialize a token
96
     *
97
     * This method does the actual beautifying.
98
     *
99
     * @param array $token structure that should be serialized
100
     *
101
     * @return mixed
102
     * @access private
103
     * @todo split this method into smaller methods
104
     */
105
    function _serializeToken($token)
106
    {
107
        switch ($token["type"]) {
108
 
109
        /*
110
         * serialize XML Element
111
         */
112
        case XML_BEAUTIFIER_ELEMENT:
113
            $indent = $this->_getIndentString($token["depth"]);
114
 
115
            // adjust tag case
116
            if ($this->_options["caseFolding"] === true) {
117
                switch ($this->_options["caseFoldingTo"]) {
118
                case "uppercase":
119
                    $token["tagname"] = strtoupper($token["tagname"]);
120
                    $token["attribs"] =
121
                        array_change_key_case($token["attribs"], CASE_UPPER);
122
                    break;
123
                case "lowercase":
124
                    $token["tagname"] = strtolower($token["tagname"]);
125
                    $token["attribs"] =
126
                        array_change_key_case($token["attribs"], CASE_LOWER);
127
                    break;
128
                }
129
            }
130
 
131
            if ($this->_options["multilineTags"] == true) {
132
                $attIndent = $indent . str_repeat(" ",
133
                    (2+strlen($token["tagname"])));
134
            } else {
135
                $attIndent = null;
136
            }
137
 
138
            // check for children
139
            switch ($token["contains"]) {
140
 
141
            // contains only CData or is empty
142
            case XML_BEAUTIFIER_CDATA:
143
            case XML_BEAUTIFIER_EMPTY:
144
                if (sizeof($token["children"]) >= 1) {
145
                    $data = $token["children"][0]["data"];
146
                } else {
147
                    $data = '';
148
                }
149
 
150
                if (strstr($data, "\n")) {
151
                    $data = "\n"
152
                        . $this->_indentTextBlock($data, $token['depth']+1, true);
153
                }
154
 
155
                $xml = $indent
156
                    . XML_Util::createTag($token["tagname"],
157
                    $token["attribs"], $data, null, XML_UTIL_REPLACE_ENTITIES,
158
                    $this->_options["multilineTags"], $attIndent)
159
                    . $this->_options["linebreak"];
160
                break;
161
                // contains mixed content
162
            default:
163
                $xml = $indent . XML_Util::createStartElement($token["tagname"],
164
                    $token["attribs"], null, $this->_options["multilineTags"],
165
                    $attIndent) . $this->_options["linebreak"];
166
 
167
                $cnt = count($token["children"]);
168
                for ($i = 0; $i < $cnt; $i++) {
169
                    $xml .= $this->_serializeToken($token["children"][$i]);
170
                }
171
                $xml .= $indent . XML_Util::createEndElement($token["tagname"])
172
                    . $this->_options["linebreak"];
173
                break;
174
            break;
175
            }
176
            break;
177
 
178
        /*
179
         * serialize CData
180
         */
181
        case XML_BEAUTIFIER_CDATA:
182
            if ($token["depth"] > 0) {
183
                $xml = str_repeat($this->_options["indent"], $token["depth"]);
184
            } else {
185
                $xml = "";
186
            }
187
 
188
            $xml .= XML_Util::replaceEntities($token["data"])
189
                . $this->_options["linebreak"];
190
            break;
191
 
192
        /*
193
         * serialize CData section
194
         */
195
        case XML_BEAUTIFIER_CDATA_SECTION:
196
            if ($token["depth"] > 0) {
197
                $xml = str_repeat($this->_options["indent"], $token["depth"]);
198
            } else {
199
                $xml = "";
200
            }
201
 
202
            $xml .= '<![CDATA['.$token["data"].']]>' . $this->_options["linebreak"];
203
            break;
204
 
205
        /*
206
         * serialize entity
207
         */
208
        case XML_BEAUTIFIER_ENTITY:
209
            if ($token["depth"] > 0) {
210
                $xml = str_repeat($this->_options["indent"], $token["depth"]);
211
            } else {
212
                $xml = "";
213
            }
214
            $xml .= "&".$token["name"].";".$this->_options["linebreak"];
215
            break;
216
 
217
 
218
        /*
219
         * serialize Processing instruction
220
         */
221
        case XML_BEAUTIFIER_PI:
222
            $indent = $this->_getIndentString($token["depth"]);
223
 
224
            $xml = $indent."<?".$token["target"].$this->_options["linebreak"]
225
                . $this->_indentTextBlock(rtrim($token["data"]), $token["depth"])
226
                . $indent."?>".$this->_options["linebreak"];
227
            break;
228
 
229
        /*
230
         * comments
231
         */
232
        case XML_BEAUTIFIER_COMMENT:
233
            $lines = count(explode("\n", $token["data"]));
234
 
235
            /*
236
             * normalize comment, i.e. combine it to one
237
             * line and remove whitespace
238
             */
239
            if ($this->_options["normalizeComments"] && $lines > 1) {
240
                $comment = preg_replace("/\s\s+/s", " ",
241
                    str_replace("\n", " ", $token["data"]));
242
                $lines   = 1;
243
            } else {
244
                $comment = $token["data"];
245
            }
246
 
247
            /*
248
             * check for the maximum length of one line
249
             */
250
            if ($this->_options["maxCommentLine"] > 0) {
251
                if ($lines > 1) {
252
                    $commentLines = explode("\n", $comment);
253
                } else {
254
                    $commentLines = array($comment);
255
                }
256
 
257
                $comment = "";
258
                for ($i = 0; $i < $lines; $i++) {
259
                    if (strlen($commentLines[$i])
260
                        <= $this->_options["maxCommentLine"]
261
                    ) {
262
                        $comment .= $commentLines[$i];
263
                        continue;
264
                    }
265
                    $comment .= wordwrap($commentLines[$i],
266
                        $this->_options["maxCommentLine"]);
267
                    if ($i != ($lines-1)) {
268
                        $comment .= "\n";
269
                    }
270
                }
271
                    $lines = count(explode("\n", $comment));
272
            }
273
 
274
            $indent = $this->_getIndentString($token["depth"]);
275
 
276
            if ($lines > 1) {
277
                $xml = $indent . "<!--" . $this->_options["linebreak"]
278
                    . $this->_indentTextBlock($comment, $token["depth"]+1, true)
279
                    . $indent . "-->" . $this->_options["linebreak"];
280
            } else {
281
                $xml = $indent . sprintf("<!-- %s -->", trim($comment))
282
                    . $this->_options["linebreak"];
283
            }
284
            break;
285
 
286
        /*
287
         * xml declaration
288
         */
289
        case XML_BEAUTIFIER_XML_DECLARATION:
290
            $indent = $this->_getIndentString($token["depth"]);
291
            $xml    = $indent . XML_Util::getXMLDeclaration($token["version"],
292
                $token["encoding"], $token["standalone"]);
293
            break;
294
 
295
        /*
296
         * xml declaration
297
         */
298
        case XML_BEAUTIFIER_DT_DECLARATION:
299
            $xml = $token["data"];
300
            break;
301
 
302
        /*
303
         * all other elements
304
         */
305
        case XML_BEAUTIFIER_DEFAULT:
306
        default:
307
            $xml = XML_Util::replaceEntities($token["data"]);
308
            break;
309
        }
310
        return $xml;
311
    }
312
}
313
?>