summaryrefslogtreecommitdiff
path: root/buildscripts/PhpDocumentor/phpDocumentor/Converters/XML/DocBook/peardoc2/Plain.php
blob: b99ced18cb09781b95f19319abb1e8457bfa2e87 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
<?PHP
/* vim: set expandtab tabstop=4 shiftwidth=4: */
// +----------------------------------------------------------------------+
// | PHP Version 4                                                        |
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2002 The PHP Group                                |
// +----------------------------------------------------------------------+
// | This source file is subject to version 2.0 of the PHP license,       |
// | that is bundled with this package in the file LICENSE, and is        |
// | available at through the world-wide-web at                           |
// | http://www.php.net/license/2_02.txt.                                 |
// | If you did not receive a copy of the PHP license and are unable to   |
// | obtain it through the world-wide-web, please send a note to          |
// | license@php.net so we can mail you a copy immediately.               |
// +----------------------------------------------------------------------+
// | Authors: Stephan Schmidt <schst@php.net>                             |
// +----------------------------------------------------------------------+

/**
 * XML/Beautifier/Renderer/Plain.php
 *
 * @package  XML_Beautifier
 * @author   Stephan Schmidt <schst@php.net>
 */

/**
 * XML_Util is needed to create the tags
 */
require_once 'XML/Util.php';

/**
 * Renderer base class
 */
require_once 'XML/Beautifier/Renderer.php';

/**
 * Basic XML Renderer for XML Beautifier
 *
 * @package  XML_Beautifier
 * @author   Stephan Schmidt <schst@php.net>
 * @todo     option to specify inline tags
 * @todo     option to specify treatment of whitespac in data sections
 * @todo     automatically create <![CDATA[ ]]> sections
 */
class PHPDoc_XML_Beautifier_Renderer_Plain extends XML_Beautifier_Renderer {

   /**
    * Serialize the XML tokens
    *
    * @access   public
    * @param    array   XML tokens
    * @return   string  XML document
    */
    function serialize($tokens)
    {
        $tokens = $this->normalize($tokens);
        
        $xml    = '';
        $cnt    = count($tokens);
        for($i = 0; $i < $cnt; $i++ )
        {
            $xml .= $this->_serializeToken($tokens[$i]);
        }
        return $xml;
    }

    /**
     * serialize a token
     *
     * This method does the actual beautifying.
     *
     * @access  private 
     * @param   array   $token structure that should be serialized
     * @todo    split this method into smaller methods
     */
    function _serializeToken($token)
    {
        switch ($token["type"]) {

            /*
            * serialize XML Element
            */
            case    XML_BEAUTIFIER_ELEMENT:
                $indent = $this->_getIndentString($token["depth"]);

                // adjust tag case
                if ($this->_options["caseFolding"] === true) {
                    switch ($this->_options["caseFoldingTo"]) {
                        case "uppercase":
                            $token["tagname"] = strtoupper($token["tagname"]);
                            $token["attribs"] = array_change_key_case($token["attribs"], CASE_UPPER);
                            break;
                        case "lowercase":
                            $token["tagname"] = strtolower($token["tagname"]);
                            $token["attribs"] = array_change_key_case($token["attribs"], CASE_LOWER);
                            break;
                    }
                }
                
                if ($this->_options["multilineTags"] == true) {
                    $attIndent = $indent . str_repeat(" ", (2+strlen($token["tagname"])));
                } else {
                    $attIndent = null;
                }
                // check for children
                switch ($token["contains"]) {
                    
                    // contains only CData or is empty
                    case    XML_BEAUTIFIER_CDATA:
                    case    XML_BEAUTIFIER_EMPTY:
                        if (sizeof($token["children"]) >= 1) {
                        $data = $token["children"][0]["data"];
                        } else {
                            $data = '';
                        }

                        if( strstr( $data, "\n" ) && $token['contains'] != PHPDOC_BEAUTIFIER_CDATA)
                        {
                            $data   =   "\n" . $this->_indentTextBlock( $data, $token['depth']+1, true );
                        } 
                        
                        $xml  = $indent . XML_Util::createTag($token["tagname"], $token["attribs"], $data, null, false, $this->_options["multilineTags"], $attIndent)
                              . $this->_options["linebreak"];
                        break;
                    // contains mixed content
                    default:
                        $xml = $indent . XML_Util::createStartElement($token["tagname"], $token["attribs"], null, $this->_options["multilineTags"], $attIndent)
                             . $this->_options["linebreak"];
                        
                        $cnt = count($token["children"]);
                        for ($i = 0; $i < $cnt; $i++) {
                            $xml .= $this->_serializeToken($token["children"][$i]);
                        }
                        $xml .= $indent . XML_Util::createEndElement($token["tagname"])
                             . $this->_options["linebreak"];
                        break;
                    break;
                }
                break;

            /*
            * serialize <![CDATA
            */
            case PHPDOC_BEAUTIFIER_CDATA:
                $xml = $token['data'] . $this->_options['linebreak'];
                break;

            /*
            * serialize CData
            */
            case    XML_BEAUTIFIER_CDATA:
                if ($token["depth"] > 0) {
                    $xml = str_repeat($this->_options["indent"], $token["depth"]);
                } else {
                    $xml = "";
                }
				
                $xml .= $token["data"] . $this->_options["linebreak"];
                break;      

            /*
            * serialize Processing instruction
            */
            case    XML_BEAUTIFIER_PI:
                $indent = $this->_getIndentString($token["depth"]);

                $xml  = $indent."<?".$token["target"].$this->_options["linebreak"]
                      . $this->_indentTextBlock(rtrim($token["data"]), $token["depth"])
                      . $indent."?>".$this->_options["linebreak"];
                break;      

            /*
            * comments
            */
            case    XML_BEAUTIFIER_COMMENT:
                $lines   = count(explode("\n",$token["data"]));
                
                /*
                * normalize comment, i.e. combine it to one
                * line and remove whitespace
                */
                if ($this->_options["normalizeComments"] && $lines > 1){
                    $comment = preg_replace("/\s\s+/s", " ", str_replace( "\n" , " ", $token["data"]));
                    $lines   = 1;
                } else {
                    $comment = $token["data"];
                }
    
                /*
                * check for the maximum length of one line
                */
                if ($this->_options["maxCommentLine"] > 0) {
                    if ($lines > 1) {
                        $commentLines = explode("\n", $comment);
                    } else {
                        $commentLines = array($comment);
                    }
    
                    $comment = "";
                    for ($i = 0; $i < $lines; $i++) {
                        if (strlen($commentLines[$i]) <= $this->_options["maxCommentLine"]) {
                            $comment .= $commentLines[$i];
                            continue;
                        }
                        $comment .= wordwrap($commentLines[$i], $this->_options["maxCommentLine"] );
                        if ($i != ($lines-1)) {
                            $comment .= "\n";
                        }
                    }
                    $lines   = count(explode("\n",$comment));
                }

                $indent = $this->_getIndentString($token["depth"]);

                if ($lines > 1) {
                    $xml  = $indent . "<!--" . $this->_options["linebreak"]
                          . $this->_indentTextBlock($comment, $token["depth"]+1, true)
                          . $indent . "-->" . $this->_options["linebreak"];
                } else {
                    $xml = $indent . sprintf( "<!-- %s -->", trim($comment) ) . $this->_options["linebreak"];
                }
                break;      

            /*
            * xml declaration
            */
            case    XML_BEAUTIFIER_XML_DECLARATION:
                $indent = $this->_getIndentString($token["depth"]);
                $xml    = $indent . XML_Util::getXMLDeclaration($token["version"], $token["encoding"], $token["standalone"]);
                break;      

            /*
            * xml declaration
            */
            case    XML_BEAUTIFIER_DT_DECLARATION:
                $xml    = $token["data"];
                break;      

            /*
            * all other elements
            */
            case    XML_BEAUTIFIER_DEFAULT:
            default:
                $xml    = XML_Util::replaceEntities( $token["data"] );
                break;      
        }
        return $xml;
    }
}
?>