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/Renderer
6
 *
7
 * XML Beautifier's Rendere
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: Renderer.php 265438 2008-08-24 19:44:14Z ashnazg $
46
 * @link      http://pear.php.net/package/XML_Beautifier
47
 */
48
 
49
/**
50
 * Renderer base class for XML_Beautifier
51
 *
52
 * @category  XML
53
 * @package   XML_Beautifier
54
 * @author    Stephan Schmidt <schst@php.net>
55
 * @copyright 2003-2008 Stephan Schmidt <schst@php.net>
56
 * @license   http://opensource.org/licenses/bsd-license New BSD License
57
 * @version   Release: @package_version@
58
 * @link      http://pear.php.net/package/XML_Beautifier
59
 */
60
class XML_Beautifier_Renderer
61
{
62
    /**
63
     * options
64
     * @var array
65
     */
66
    var $_options = array();
67
 
68
    /**
69
     * create a new renderer
70
     *
71
     * @param array $options for the serialization
72
     *
73
     * @access   public
74
     */
75
    function XML_Beautifier_Renderer($options = array())
76
    {
77
        $this->_options = $options;
78
    }
79
 
80
    /**
81
     * Serialize the XML tokens
82
     *
83
     * @param array $tokens XML tokens
84
     *
85
     * @return string XML document
86
     * @access public
87
     * @abstract
88
     */
89
    function serialize($tokens)
90
    {
91
        return  '';
92
    }
93
 
94
    /**
95
     * normalize the XML tree
96
     *
97
     * When normalizing an XML tree, adjacent data sections
98
     * are combined to one data section.
99
     *
100
     * @param array $tokens XML tree as returned by the tokenizer
101
     *
102
     * @return array XML tree
103
     * @access public
104
     */
105
    function normalize($tokens)
106
    {
107
        $tmp = array();
108
        foreach ($tokens as $token) {
109
            array_push($tmp, $this->_normalizeToken($token));
110
        }
111
        return $tmp;
112
    }
113
 
114
    /**
115
     * normalize one element in the XML tree
116
     *
117
     * This method will combine all data sections of an element.
118
     *
119
     * @param array $token token array
120
     *
121
     * @return array $struct
122
     * @access private
123
     */
124
    function _normalizeToken($token)
125
    {
126
        if ((isset($token["children"]))
127
            && !is_array($token["children"])
128
            || empty($token["children"])
129
        ) {
130
            return $token;
131
        }
132
 
133
        $children          = $token["children"];
134
        $token["children"] = array();
135
        $cnt               = count($children);
136
        $currentMode       = 0;
137
        for ($i = 0; $i < $cnt; $i++ ) {
138
            // no data section
139
            if ($children[$i]["type"] != XML_BEAUTIFIER_CDATA
140
                && $children[$i]["type"] != XML_BEAUTIFIER_CDATA_SECTION
141
            ) {
142
                $children[$i] = $this->_normalizeToken($children[$i]);
143
 
144
                $currentMode = 0;
145
                array_push($token["children"], $children[$i]);
146
                continue;
147
            }
148
 
149
            /*
150
             * remove whitespace
151
             */
152
            if ($this->_options['removeLineBreaks'] == true) {
153
                $children[$i]['data'] = trim($children[$i]['data']);
154
                if ($children[$i]['data'] == '') {
155
                    continue;
156
                }
157
            }
158
 
159
            if ($currentMode == $children[$i]["type"]) {
160
                $tmp = array_pop($token["children"]);
161
 
162
                if ($children[$i]['data'] != '') {
163
                    if ($tmp['data'] != ''
164
                        && $this->_options['removeLineBreaks'] == true
165
                    ) {
166
                        $tmp['data'] .= ' ';
167
                    }
168
                    $tmp["data"] .= $children[$i]["data"];
169
                }
170
                array_push($token["children"], $tmp);
171
            } else {
172
                array_push($token["children"], $children[$i]);
173
            }
174
 
175
            $currentMode = $children[$i]["type"];
176
        }
177
        return $token;
178
    }
179
 
180
    /**
181
     * indent a text block consisting of several lines
182
     *
183
     * @param string  $text  textblock
184
     * @param integer $depth depth to indent
185
     * @param boolean $trim  trim the lines
186
     *
187
     * @return string indented text block
188
     * @access private
189
     */
190
    function _indentTextBlock($text, $depth, $trim = false)
191
    {
192
        $indent = $this->_getIndentString($depth);
193
        $tmp    = explode("\n", $text);
194
        $cnt    = count($tmp);
195
        $xml    = '';
196
        for ($i = 0; $i < $cnt; $i++ ) {
197
            if ($trim) {
198
                $tmp[$i] = trim($tmp[$i]);
199
            }
200
            if ($tmp[$i] == '') {
201
                continue;
202
            }
203
            $xml .= $indent.$tmp[$i].$this->_options["linebreak"];
204
        }
205
        return $xml;
206
    }
207
 
208
    /**
209
     * get the string that is used for indentation in a specific depth
210
     *
211
     * This depends on the option 'indent'.
212
     *
213
     * @param integer $depth nesting level
214
     *
215
     * @return string indent string
216
     * @access private
217
     */
218
    function _getIndentString($depth)
219
    {
220
        if ($depth > 0) {
221
            return str_repeat($this->_options["indent"], $depth);
222
        }
223
        return "";
224
    }
225
}
226
?>