Committing the first phase of the phpDocBuilder.
[phpeclipse.git] / utilities / phpdocbuilder / DocBuilder.phpc
1 <?php
2 /**
3  * Created on Jul 9, 2007
4  *
5  * To change the template for this generated file go to
6  * Window - Preferences - PHPeclipse - PHP - Code Templates
7  */
8
9 /**
10  * Doc Builder
11  * 
12  * Class will parse and build a custom master xml document from the phpdoc docbook xml
13  * files.
14  * @author ed_mann
15  * @copyright Copyright &copy; 2007, emann
16  * @license http://pleaseaddme.com some license that the dev team likes.
17  */
18 final class Docbuilder {
19
20         /**
21           * out_dom
22           *
23           * @var object XML Document Dom Object
24           * @access private
25           */
26         private $out_dom;
27
28         /**
29          * out_file
30          * @access private
31          * @param string
32          */
33         private $out_file = NULL;
34
35         /**
36          * ref_path
37          * @access private
38          * @param string Path the the phpdoc reference directory 
39          * this shoud be in "phpdoc/en/reference/"
40          */
41         private $ref_path = NULL;
42
43         /**
44          * ref files
45          * @access private
46          * @param array our associtive array of ref directories and each file under it
47          */
48         private $ref_files = NULL;
49     
50     /**
51      * version_dom
52      * @access private
53      * @param object DomDocument object
54      */
55      private $version_dom = NULL;
56      
57      /**
58       * version_xpath
59       * @access private
60       * @param object DomXPath object
61       */
62       private $version_xpath = NULL;
63       
64      /**
65       * manual_dom
66       * @access private
67       * @param object DomDocument object
68       */
69       private $manual_dom = NULL;
70     
71         /**
72          * constructor
73          * Constructor will create our out_dom object, load the manual dom object and the function
74      * dom object.
75          * @access public
76          * @param string $out_file output file name
77      * @param string $ref_path Path to phpdoc directory
78          * @return boolean true if succeded in loading files false otherwise
79          */
80         public function __construct($out_file, $ref_path) {
81                 $this->out_dom = new DOMDocument("1.0");
82         
83                 $this->out_file = $out_file;
84                 $this->ref_path = $ref_path;
85                 if(!$this->loadManualDom()){
86                         return false;
87                 }
88                 if(!$this->loadVersionDom()){
89                         return false;
90                 }
91         return true;
92         }
93     
94     /**
95      * loadManualDom
96      * Function will instantiate the manual dom object
97      * @access private
98      * @param void
99      * @return boolean true if successful False if cannot load
100      */
101      private function loadManualDom(){
102         try {
103             $this->manual_dom = new DomDocument();
104             $this->manual_dom->validateOnParse = true;
105             $this->manual_dom->substituteEntities = true;
106             $this->manual_dom->resolveExternals = true;
107             $this->manual_dom->preserveWhiteSpace = false;
108             if(!$this->manual_dom->Load($this->ref_path."/.manual.xml")){
109                    throw new Exception('Could not load the .manual.xml file');
110             }
111        } catch (Exception $e){
112                 print($e->__toString());
113             return false;
114         }
115         $this->manual_dom_xpath = new DomXPath($this->manual_dom);
116         return true;
117      }
118      
119     /**
120      * loadVersionDom
121      * Function will instantiate the version dom object. Also we will create the xpath
122      * object that we will use to get our version info.
123      * @access private
124      * @param void
125      * @return boolean true if successful False if cannot load
126      */
127      private function loadVersionDom(){
128         try {
129         $this->version_dom = new DomDocument();
130         $this->version_dom->validateOnParse = true;
131         $this->version_dom->preserveWhiteSpace = false;
132         if(!$this->version_dom->Load($this->ref_path."/phpbook/phpbook-xsl/version.xml")){
133             throw new Exception('Could not load the version.xml file');
134         }
135         } catch (Exception $e){
136             print($e->__toString());
137             return false;
138         }
139         $this->version_xpath = new DomXPath($this->version_dom);
140         return true;
141      }
142     
143     /**
144      * buildDoc
145      * 
146      * Build our output document
147      * @access public
148      * @param void
149      * @return boolean true if successful false if not
150      */
151      public function buildDoc(){
152         $entries = $this->manual_dom->getElementsByTagName('refentry');
153         
154   foreach($entries as $entry){
155         /*
156          * note: you use the xpath to get the different roles. You need to loop the roles
157          * to get good output. Each role should be it's own function. It is left here
158          * because i am going home, and don't have time to finish this right now.
159          */
160         $query = "*[@role = 'changelog']";
161         $info = $this->manual_dom_xpath->query($query, $entry);
162         
163         print($info->item(0)->getElementsByTagName('entry')->item(3)->nodeValue); die;
164         // end of xpath stuff.
165     $ref_name = $entry->getElementsByTagName('refname')->item(0)->nodeValue;
166     $ref_purp = $entry->getElementsByTagName('refpurpose')->item(0)->nodeValue;
167     $type = $entry->getElementsByTagName('type')->item(0)->nodeValue;
168     $synopsis = $this->buildSynopsis($entry);
169     if($entry->getElementsByTagName('para')->item(0)){
170         print ("yes"); die;
171     }
172     die;
173     $desc = 
174     $version = $this->getVersionInfo($ref_name);
175     print($ref_name."  ".$ref_purp." ".$type ." ". $synopsis. "\n"); die;
176   }
177   die;
178      }
179      
180     /**
181      * build Synopsis
182      * Function will build the php function synopsis
183      * @access private
184      * @param string $entry xml entry to parse
185      * @return string $synopsis Synopsis output string
186      */
187      private function buildSynopsis($entry){
188         $meth = $entry->getElementsByTagName('methodsynopsis')->item(0);
189         $output = $entry->getElementsByTagName('type')->item(0)->nodeValue;
190         $output .= "<strong>".$entry->getElementsByTagName('methodname')->item(0)->nodeValue."</strong>";
191         $output .= ("(");
192         $count = 0;
193         $end = "";
194         foreach ($meth->getElementsByTagName('methodparam') as $meth){
195             if($count == 1){
196                 $output .= (" [, ");
197                 $end .= "]";
198             }
199                 $output .= ($meth->getElementsByTagName('type')->item(0)->nodeValue."  $".$meth->getElementsByTagName('parameter')->item(0)->nodeValue);
200             $count = 1;
201         }
202         $output .= ($end.")");
203         return $output;
204      }
205      
206     /**
207      * getVersionInfo
208      * Function will query the version xml file and find the version information for 
209      * function paramater. We are using xpath query to get our name of the function, then we
210      * can use the results to get the Node 'from' value. Calls explode to strip the string into
211      * an array.
212      * @access private
213      * @param string $function Function to search for
214      * @return array Function version info in array 
215      */
216      private function getVersionInfo($function){
217         $query = "/versions/function[@name = '$function']";
218         $info = $this->version_xpath->query($query);
219         if($info->length > 0){           
220                 return explode(',', $this->version_xpath->query($query)->item(0)->getAttributeNode('from')->nodeValue);
221         } else {
222                 return array('(No version information available, might be only in CVS)');
223         }
224      }
225 }
226 ?>