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: */
3
// +----------------------------------------------------------------------+
4
// | PHP Version 4                                                        |
5
// +----------------------------------------------------------------------+
6
// | Copyright (c) 1997-2002 The PHP Group                                |
7
// +----------------------------------------------------------------------+
8
// | This source file is subject to version 2.0 of the PHP license,       |
9
// | that is bundled with this package in the file LICENSE, and is        |
10
// | available at through the world-wide-web at                           |
11
// | http://www.php.net/license/2_02.txt.                                 |
12
// | If you did not receive a copy of the PHP license and are unable to   |
13
// | obtain it through the world-wide-web, please send a note to          |
14
// | license@php.net so we can mail you a copy immediately.               |
15
// +----------------------------------------------------------------------+
16
// | Authors: Stephan Schmidt <schst@php.net>                             |
17
// +----------------------------------------------------------------------+
18
 
19
/**
20
 * XML/Beautifier/Renderer/Plain.php
21
 *
22
 * @package  XML_Beautifier
23
 * @author   Stephan Schmidt <schst@php.net>
24
 */
25
 
26
/**
27
 * XML_Util is needed to create the tags
28
 */
29
require_once 'XML/Util.php';
30
 
31
/**
32
 * Renderer base class
33
 */
34
require_once 'XML/Beautifier/Renderer.php';
35
 
36
/**
37
 * Basic XML Renderer for XML Beautifier
38
 *
39
 * @package  XML_Beautifier
40
 * @author   Stephan Schmidt <schst@php.net>
41
 * @todo     option to specify inline tags
42
 * @todo     option to specify treatment of whitespac in data sections
43
 * @todo     automatically create <![CDATA[ ]]> sections
44
 */
45
class PHPDoc_XML_Beautifier_Renderer_Plain extends XML_Beautifier_Renderer {
46
 
47
   /**
48
    * Serialize the XML tokens
49
    *
50
    * @access   public
51
    * @param    array   XML tokens
52
    * @return   string  XML document
53
    */
54
    function serialize($tokens)
55
    {
56
        $tokens = $this->normalize($tokens);
57
 
58
        $xml    = '';
59
        $cnt    = count($tokens);
60
        for($i = 0; $i < $cnt; $i++ )
61
        {
62
            $xml .= $this->_serializeToken($tokens[$i]);
63
        }
64
        return $xml;
65
    }
66
 
67
    /**
68
     * serialize a token
69
     *
70
     * This method does the actual beautifying.
71
     *
72
     * @access  private
73
     * @param   array   $token structure that should be serialized
74
     * @todo    split this method into smaller methods
75
     */
76
    function _serializeToken($token)
77
    {
78
        switch ($token["type"]) {
79
 
80
            /*
81
            * serialize XML Element
82
            */
83
            case    XML_BEAUTIFIER_ELEMENT:
84
                $indent = $this->_getIndentString($token["depth"]);
85
 
86
                // adjust tag case
87
                if ($this->_options["caseFolding"] === true) {
88
                    switch ($this->_options["caseFoldingTo"]) {
89
                        case "uppercase":
90
                            $token["tagname"] = strtoupper($token["tagname"]);
91
                            $token["attribs"] = array_change_key_case($token["attribs"], CASE_UPPER);
92
                            break;
93
                        case "lowercase":
94
                            $token["tagname"] = strtolower($token["tagname"]);
95
                            $token["attribs"] = array_change_key_case($token["attribs"], CASE_LOWER);
96
                            break;
97
                    }
98
                }
99
 
100
                if ($this->_options["multilineTags"] == true) {
101
                    $attIndent = $indent . str_repeat(" ", (2+strlen($token["tagname"])));
102
                } else {
103
                    $attIndent = null;
104
                }
105
                // check for children
106
                switch ($token["contains"]) {
107
 
108
                    // contains only CData or is empty
109
                    case    XML_BEAUTIFIER_CDATA:
110
                    case    XML_BEAUTIFIER_EMPTY:
111
                        if (sizeof($token["children"]) >= 1) {
112
                        $data = $token["children"][0]["data"];
113
                        } else {
114
                            $data = '';
115
                        }
116
 
117
                        if( strstr( $data, "\n" ) && $token['contains'] != PHPDOC_BEAUTIFIER_CDATA)
118
                        {
119
                            $data   =   "\n" . $this->_indentTextBlock( $data, $token['depth']+1, true );
120
                        }
121
 
122
                        $xml  = $indent . XML_Util::createTag($token["tagname"], $token["attribs"], $data, null, false, $this->_options["multilineTags"], $attIndent)
123
                              . $this->_options["linebreak"];
124
                        break;
125
                    // contains mixed content
126
                    default:
127
                        $xml = $indent . XML_Util::createStartElement($token["tagname"], $token["attribs"], null, $this->_options["multilineTags"], $attIndent)
128
                             . $this->_options["linebreak"];
129
 
130
                        $cnt = count($token["children"]);
131
                        for ($i = 0; $i < $cnt; $i++) {
132
                            $xml .= $this->_serializeToken($token["children"][$i]);
133
                        }
134
                        $xml .= $indent . XML_Util::createEndElement($token["tagname"])
135
                             . $this->_options["linebreak"];
136
                        break;
137
                    break;
138
                }
139
                break;
140
 
141
            /*
142
            * serialize <![CDATA
143
            */
144
            case PHPDOC_BEAUTIFIER_CDATA:
145
                $xml = $token['data'] . $this->_options['linebreak'];
146
                break;
147
 
148
            /*
149
            * serialize CData
150
            */
151
            case    XML_BEAUTIFIER_CDATA:
152
                if ($token["depth"] > 0) {
153
                    $xml = str_repeat($this->_options["indent"], $token["depth"]);
154
                } else {
155
                    $xml = "";
156
                }
157
 
158
                $xml .= $token["data"] . $this->_options["linebreak"];
159
                break;
160
 
161
            /*
162
            * serialize Processing instruction
163
            */
164
            case    XML_BEAUTIFIER_PI:
165
                $indent = $this->_getIndentString($token["depth"]);
166
 
167
                $xml  = $indent."<?".$token["target"].$this->_options["linebreak"]
168
                      . $this->_indentTextBlock(rtrim($token["data"]), $token["depth"])
169
                      . $indent."?>".$this->_options["linebreak"];
170
                break;
171
 
172
            /*
173
            * comments
174
            */
175
            case    XML_BEAUTIFIER_COMMENT:
176
                $lines   = count(explode("\n",$token["data"]));
177
 
178
                /*
179
                * normalize comment, i.e. combine it to one
180
                * line and remove whitespace
181
                */
182
                if ($this->_options["normalizeComments"] && $lines > 1){
183
                    $comment = preg_replace("/\s\s+/s", " ", str_replace( "\n" , " ", $token["data"]));
184
                    $lines   = 1;
185
                } else {
186
                    $comment = $token["data"];
187
                }
188
 
189
                /*
190
                * check for the maximum length of one line
191
                */
192
                if ($this->_options["maxCommentLine"] > 0) {
193
                    if ($lines > 1) {
194
                        $commentLines = explode("\n", $comment);
195
                    } else {
196
                        $commentLines = array($comment);
197
                    }
198
 
199
                    $comment = "";
200
                    for ($i = 0; $i < $lines; $i++) {
201
                        if (strlen($commentLines[$i]) <= $this->_options["maxCommentLine"]) {
202
                            $comment .= $commentLines[$i];
203
                            continue;
204
                        }
205
                        $comment .= wordwrap($commentLines[$i], $this->_options["maxCommentLine"] );
206
                        if ($i != ($lines-1)) {
207
                            $comment .= "\n";
208
                        }
209
                    }
210
                    $lines   = count(explode("\n",$comment));
211
                }
212
 
213
                $indent = $this->_getIndentString($token["depth"]);
214
 
215
                if ($lines > 1) {
216
                    $xml  = $indent . "<!--" . $this->_options["linebreak"]
217
                          . $this->_indentTextBlock($comment, $token["depth"]+1, true)
218
                          . $indent . "-->" . $this->_options["linebreak"];
219
                } else {
220
                    $xml = $indent . sprintf( "<!-- %s -->", trim($comment) ) . $this->_options["linebreak"];
221
                }
222
                break;
223
 
224
            /*
225
            * xml declaration
226
            */
227
            case    XML_BEAUTIFIER_XML_DECLARATION:
228
                $indent = $this->_getIndentString($token["depth"]);
229
                $xml    = $indent . XML_Util::getXMLDeclaration($token["version"], $token["encoding"], $token["standalone"]);
230
                break;
231
 
232
            /*
233
            * xml declaration
234
            */
235
            case    XML_BEAUTIFIER_DT_DECLARATION:
236
                $xml    = $token["data"];
237
                break;
238
 
239
            /*
240
            * all other elements
241
            */
242
            case    XML_BEAUTIFIER_DEFAULT:
243
            default:
244
                $xml    = XML_Util::replaceEntities( $token["data"] );
245
                break;
246
        }
247
        return $xml;
248
    }
249
}
250
?>