Pelzini

This is the code documentation for the Pelzini project

source of /processor/xml_outputter.php

This file contains the MetadataOutputter class
  1. <?php
  2. /*
  3. Copyright 2008 Josh Heidenreich
  4.  
  5. This file is part of Pelzini.
  6.  
  7. Pelzini is free software: you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation, either version 3 of the License, or
  10. (at your option) any later version.
  11.  
  12. Pelzini is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with Pelzini. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20.  
  21.  
  22. /**
  23.  * This file contains the {@link MetadataOutputter} class
  24.  *
  25.  * @package Outputters
  26.  * @author Josh
  27.  * @since 0.3
  28.  **/
  29.  
  30. /**
  31.  * Outputs the tree to an xml file
  32.  *
  33.  * @author Josh, 2009-08-03
  34.  **/
  35. class XmlOutputter extends MetadataOutputter {
  36. private $dom;
  37. private $root;
  38.  
  39.  
  40. /**
  41.   * Returns the file extension of the outputted file (e.g. 'xml')
  42.   **/
  43. public function get_ext()
  44. {
  45. return 'xml';
  46. }
  47.  
  48.  
  49. /**
  50.   * Returns the mimetype of the outputted file (e.g. 'text/xml')
  51.   **/
  52. public function get_mimetype()
  53. {
  54. return 'text/xml';
  55. }
  56.  
  57.  
  58. /**
  59.   * Does the actual outputting of the file objects (and their sub-objects)
  60.   *
  61.   * @param array $parser_items The ParserItem(s) to save
  62.   * @return boolean True on success, false on failure
  63.   **/
  64. public function output($parser_items, Config $config)
  65. {
  66. $this->dom = new DOMDocument('1.0', 'UTF-8');
  67. $this->dom->formatOutput = true;
  68.  
  69. $this->root = $this->dom->createElement('documentation');
  70. $this->dom->appendChild($this->root);
  71.  
  72. foreach ($parser_items as $item) {
  73. if ($item instanceof ParserFile) {
  74. $this->process_file($item);
  75.  
  76. } else if ($item instanceof ParserDocument) {
  77. $this->process_document($item);
  78.  
  79. }
  80. }
  81.  
  82. $this->dom->save($this->filename);
  83.  
  84. return true;
  85. }
  86.  
  87.  
  88. /**
  89.   * Processes a file
  90.   **/
  91. private function process_file($item)
  92. {
  93. $node = $this->dom->createElement('file');
  94. $this->root->appendChild($node);
  95.  
  96. $node->setAttribute('name', $item->name);
  97.  
  98. $this->create_description_node($node, $item->description);
  99.  
  100. // sub-items
  101. foreach ($item->functions as $child) {
  102. $this->process_function ($node, $child);
  103. }
  104.  
  105. foreach ($item->classes as $child) {
  106. $this->process_class ($node, $child);
  107. }
  108. }
  109.  
  110.  
  111. /**
  112.   * Processes a function
  113.   **/
  114. private function process_function($parent_node, $item)
  115. {
  116. $node = $this->dom->createElement('function');
  117. $parent_node->appendChild($node);
  118.  
  119. $node->setAttribute('name', $item->name);
  120. $node->setAttribute('visibility', $item->visibility);
  121. if ($item->abstract) $node->setAttribute('abstract', 'abstract');
  122. if ($item->static) $node->setAttribute('static', 'static');
  123. if ($item->final) $node->setAttribute('final', 'final');
  124.  
  125. $this->create_description_node($node, $item->description);
  126.  
  127. foreach ($item->args as $child) {
  128. $this->process_argument($node, $child);
  129. }
  130.  
  131. foreach ($item->returns as $child) {
  132. $this->process_return($node, $child);
  133. }
  134. }
  135.  
  136.  
  137. /**
  138.   * Processes a function argument
  139.   **/
  140. private function process_argument($parent_node, $item)
  141. {
  142. $node = $this->dom->createElement('argument');
  143. $parent_node->appendChild($node);
  144.  
  145. $node->setAttribute('name', $item->name);
  146. $node->setAttribute('type', $item->type);
  147. $node->appendChild($this->dom->createTextNode(
  148. trim(strip_tags($item->description))
  149. ));
  150. }
  151.  
  152.  
  153. /**
  154.   * Processes a function argument
  155.   **/
  156. private function process_return($parent_node, $item)
  157. {
  158. $node = $this->dom->createElement('return');
  159. $parent_node->appendChild($node);
  160.  
  161. $node->setAttribute('type', $item->type);
  162. $node->appendChild($this->dom->createTextNode(
  163. trim(strip_tags($item->description))
  164. ));
  165. }
  166.  
  167.  
  168. /**
  169.   * Processes a class
  170.   **/
  171. private function process_class($parent_node, $item)
  172. {
  173. $node = $this->dom->createElement('class');
  174. $parent_node->appendChild($node);
  175.  
  176. $node->setAttribute('name', $item->name);
  177. if (isset($item->abstract) and $item->abstract) $node->setAttribute('abstract', 'abstract');
  178. if (isset($item->final) and $item->final) $node->setAttribute('final', 'final');
  179.  
  180. $this->create_description_node($node, $item->description);
  181.  
  182. // sub-items
  183. foreach ($item->functions as $child) {
  184. $this->process_function ($node, $child);
  185. }
  186. }
  187.  
  188.  
  189. /**
  190.   * Processes a document
  191.   **/
  192. private function process_document($item)
  193. {
  194.  
  195. }
  196.  
  197.  
  198. /**
  199.   * Creates a description node, and appends it to the specified node
  200.   **/
  201. private function create_description_node($node, $description)
  202. {
  203. $description = trim(strip_tags($description));
  204.  
  205. $desc = $this->dom->createElement('description');
  206. $desc->appendChild ($this->dom->createTextNode ($description));
  207.  
  208. $node->appendChild ($desc);
  209. }
  210.  
  211. }
  212.