changed git call from https to git readonly
[atutor.git] / mods / phpdoc / PHPDoc / xmlwriter / PhpdocXMLWriter.php
1 <?php\r
2 /**\r
3 * Creates XML documents.\r
4\r
5 * PHPDoc uses this helper class to generate xml documents. It's \r
6 * not much what this class can do but it provides some simple\r
7 * functions to handle attributes and hides file handling tasks.\r
8\r
9 * @author               Ulf Wendel <ulf.wendel@phpdoc.de>\r
10 * @version      $Id: PhpdocXMLWriter.php,v 1.3 2000/12/03 22:37:39 uw Exp $\r
11 */\r
12 class PhpdocXMLWriter extends PhpdocObject {\r
13         \r
14         /**\r
15         * Generated XML document.\r
16         *\r
17         * @var  string  $xml\r
18         */\r
19         var $xml = "";\r
20         \r
21         /**\r
22         * PHPDoc Warning object\r
23         *\r
24         * @var  object  PhpdocWarning\r
25         */\r
26         var $warn;\r
27         \r
28         /**\r
29         * Filehandler used for IO operations\r
30         *\r
31         * @var  object  PhpdocFilehandler\r
32         * @see  PhpdocXMLWriter()\r
33         */\r
34         var $fileHandler;\r
35         \r
36         /**\r
37         * Creates a new PhpdocFileHandler\r
38         *\r
39         * @see  $filehandler\r
40         */\r
41         function PhpdocXMLWriter() {\r
42                 $this->fileHandler = new PhpdocFileHandler;\r
43         } // end constructor\r
44         \r
45         /**\r
46         * Clears the internal xml data buffer so that a new document can be passed to the object.\r
47         * @access       public\r
48         */\r
49         function free() {\r
50                 $this->xml = "";\r
51         } // end func free\r
52         \r
53         /**\r
54         * Adds xml to the generated xml.\r
55         *\r
56         * @param        string  xml to append\r
57         * @access       public\r
58         */\r
59         function addXML($xml) {\r
60         \r
61                 $this->xml.= $xml;\r
62                         \r
63         } // end func addXML\r
64 \r
65         /**\r
66         * Saves the xml to the specified file.\r
67         *\r
68         * @param        string  Name of the target file\r
69         * @access       public\r
70         */      \r
71         function export($filename) {\r
72                 return $this->fileHandler->createFile($filename, $this->xml);\r
73         } // end func export\r
74         \r
75         /**\r
76         * Adds an open (or single) xml tag to the generated xml.\r
77         *\r
78         * Use this function to add new elements/tags to the xml document. \r
79         * The tagname and all attributenames will be converted to lowercase.\r
80         *\r
81         * @param        string  elementname (tagname)\r
82         *       @param  string  value of the container: <name>value\r
83         * @param        array           Array of attributes: $attribs[n][type] = boolean|cdata, $attribs[n][value] = value\r
84         * @param        boolean Flag indication that you want an empty tag like <name/>.\r
85         * @access       public\r
86         * @see          endElement()\r
87         */\r
88         function startElement($name, $value="", $attribs="", $close = false) {\r
89         \r
90                 $xml = "<".strtolower($name);\r
91                 \r
92                 if (is_array($attribs)) {\r
93                         \r
94                         reset($attribs);\r
95                         while (list($attrib, $data)=each($attribs)) {\r
96                         \r
97                                 $attrib = strtolower($attrib);\r
98                                 $type = strtolower($data["type"]);\r
99                                 \r
100                                 switch($type) {\r
101                                         case "boolean":\r
102                                                 $xml.= sprintf(' %s="%s"', $attrib, ($data["value"]) ? "true" : "false");\r
103                                                 break;\r
104                                                 \r
105                                         case "cdata":\r
106                                                 $xml.= sprintf(' %s="%s"', $attrib, $this->xmlencode($data["value"]) );\r
107                                                 break;\r
108                                 }\r
109                         }\r
110                         \r
111                 } \r
112                 \r
113                 if ($close) {\r
114                 \r
115                         $xml.= "/>";\r
116                         \r
117                 } else {\r
118                 \r
119                         $xml.= ">";\r
120                         if (""!=$value)\r
121                                 $xml.= $this->xmlencode($value);\r
122                                 \r
123                 }\r
124                 \r
125                 $this->xml.= $xml;\r
126                 \r
127         } // end func startElement\r
128         \r
129         /**\r
130         * Adds a closing xml tag to the generated xml document.\r
131         *\r
132         * @param        string  Elementname (tagname)\r
133         * @access       public\r
134         * @see  startElement()\r
135         */\r
136         function endElement($name) {\r
137                 $this->xml.= sprintf("</%s>",   strtolower($name)       );\r
138         } // end func endElement\r
139         \r
140         /**\r
141         * Adds a complete xml container to the generated xml document.\r
142         *\r
143         * @param        string  Elementname (tagname)\r
144         * @param        string  Value\r
145         * @param        array           Attributes\r
146         * @access       public\r
147         * @see  startElement(), endElement()\r
148         */\r
149         function addElement($name, $value="", $attribs="") {\r
150                 \r
151                 if (""==$value) {\r
152                 \r
153                         $this->startElement($name, $value, $attribs, true);\r
154                         \r
155                 } else {\r
156                 \r
157                         $this->startElement($name, $value, $attribs, false);\r
158                         $this->endElement($name);\r
159                         \r
160                 }\r
161                         \r
162         } // end func addElement\r
163         \r
164         /**\r
165         * Encodes XML values.\r
166         * @param        string  $value\r
167         * @return       string  $value\r
168         */\r
169         function xmlencode($value) {\r
170 #               return preg_replace( array("@<@", "@>@", "@'@", '@"@', "@&@", "@" . PHPDOC_LINEBREAK ."@", "@\n@", "@\r@"), array("&lt;", "&gt;", "&apos;", "&quot;", "&amp;", '&#x0a;', '&#x0a;', '&#x0a;'), $value);\r
171                 return utf8_encode(preg_replace( array("@<@", "@>@", "@'@", '@"@', "@&@", "@" . PHPDOC_LINEBREAK . "@", "@\n@", "@\r@"), array("&lt;", "&gt;", "&apos;", "&quot;", "&amp;", '&#x0a;', '&#x0a;', '&#x0a;'), $value));\r
172         } // end func xmlencode\r
173         \r
174 } // end class PhpdocXMLWriter\r
175 ?>