47ad5fb3a97dbfb3342b61ddfe2121e41385bb92
[atutor.git] / mods / phpdoc / PHPDoc / xmlexporter / PhpdocXMLExporter.php
1 <?php\r
2 /**\r
3 * Exporter used to export phpdoc internals data structures as xml documents.\r
4 *\r
5 * @version      $Id: PhpdocXMLExporter.php,v 1.2 2000/12/03 22:37:38 uw Exp $\r
6 */\r
7 class PhpdocXMLExporter extends PhpdocObject {\r
8         \r
9         /**\r
10         * Filename prefix for the generated xml document.\r
11         * \r
12         * This class variable must be overriden by all derived classes.\r
13         * PHPDoc uses the filename prefix to detect the content of \r
14         * the file.\r
15         * \r
16         * @var  string  $fileprefix\r
17         */\r
18         var $fileprefix = "";\r
19 \r
20         /**\r
21         * Target directory where the xml documents get saved.\r
22         * @var  string  $path\r
23         * @see  setPath()\r
24         */      \r
25         var $path = "";\r
26         \r
27         /**\r
28         * Data to save as a xml document.\r
29         * @var  array   $result\r
30         * @see  setResult(), export()\r
31         */\r
32         var $result = array();\r
33         \r
34         /**\r
35         * Instance of PhpdocXMLWriter used to generate the xml document.\r
36         * @var  object PhpdocXMLWriter\r
37         * @see  PhpdocXMLExporter()\r
38         */\r
39         var $xmlwriter;\r
40         \r
41         /**\r
42         * Creates a PhpdocXMLWriter object.\r
43         *\r
44         * Make sure that all derived classes call this constructor.\r
45         * \r
46         * @see  $xmlwriter\r
47         */                                                                                                                      \r
48         function PhpdocXMLExporter() {\r
49         \r
50                 $this->xmlwriter = new PhpdocXMLWriter;\r
51                 \r
52         } // end constructor                                                                                    \r
53 \r
54         /**\r
55         * Sets the target path for the generated xml documents.\r
56         *  \r
57         * @param        string\r
58         * @see          $path\r
59         * @access       public\r
60         */      \r
61         function setPath($path) {\r
62                 $this->path = $path;\r
63         } // end func setPath\r
64         \r
65         /**\r
66         * Exports the given result array as xml document.\r
67         *\r
68         * @param        array   \r
69         * @param        string  name of the target xml file\r
70         * @access       public\r
71         * @see          create(), $result\r
72         */\r
73         function export($result, $xmlfile="") {\r
74                 \r
75                 if (0 == count($result))\r
76                         return;\r
77 \r
78                 $this->result = $result;\r
79                 \r
80                 $this->xmlwriter->addXML('<?xml version="1.0"?>');\r
81                 $this->xmlwriter->startElement("phpdoc", "", "", false, true);\r
82 \r
83                 $this->create();\r
84 \r
85                 $this->xmlwriter->endElement("phpdoc", true);\r
86                 \r
87                 if ("" == $xmlfile)\r
88                         $xmlfile = $this->result["name"];\r
89                 \r
90                 /*\r
91                 if (file_exists($this->path.$xmlfile)) {\r
92                         $i = 1;\r
93                         while (file_exists($this->path.$name."_".$i.".xml"))\r
94                                 $i++;\r
95                                 \r
96                         $xmlfile =      $name."_".$i.".xml";\r
97                 }\r
98                 */\r
99                 \r
100                 $xmlfile = $this->nameToURL($xmlfile);\r
101                 $xmlfile = $this->path.$this->fileprefix.$xmlfile.".xml";\r
102                 \r
103                 $this->xmlwriter->export($xmlfile);\r
104                 $this->xmlwriter->free();\r
105                 \r
106         } // end func export\r
107         \r
108         /**\r
109         * @param        array\r
110         */\r
111         function setResult($result) {\r
112                 $this->result = $result;\r
113                 $this->create();\r
114         } // end func setResult\r
115         \r
116         /**\r
117         * Kind of array_intersect for xml attributes.\r
118         * \r
119         * This functions takes a data array and a list of allowed fields in the data\r
120         * array. All of the allowed fields that exists in the data array will be \r
121         * copied to returned array which looks like:\r
122         * $attribs[name] = array ( type => allowed[name], value => data[name] ). \r
123         * This structure is used by PhpdocXMLWriter->addElement().\r
124         *\r
125         * @param        array   data array\r
126         * @param        array   array of allowed fields and their attribute type \r
127         * @return       array   $attribs\r
128         */\r
129         function getAttributes($data, $allowed) {\r
130                 \r
131                 $attribs = array();\r
132                 \r
133                 reset($allowed);\r
134                 while (list($tag, $type)=each($allowed)) \r
135                         if (isset($data[$tag])) \r
136                                 $attribs[$tag] = array( "type"  => $type, "value" => $data[$tag] );\r
137                                 \r
138                 return $attribs;\r
139         } // end func getAttributes\r
140 \r
141 } // end PhpdocXMLExporter\r
142 ?>