/mods/_core/modules/classes/ModuleListParser.class.php

https://github.com/harriswong/ATutor · PHP · 140 lines · 100 code · 12 blank · 28 comment · 28 complexity · d757904b1c5ada5fcbae012eaf7277a2 MD5 · raw file

  1. <?php
  2. /************************************************************************/
  3. /* ATutor */
  4. /************************************************************************/
  5. /* Copyright (c) 2002-2010 */
  6. /* Inclusive Design Institute */
  7. /* http://atutor.ca */
  8. /* */
  9. /* This program is free software. You can redistribute it and/or */
  10. /* modify it under the terms of the GNU General Public License */
  11. /* as published by the Free Software Foundation. */
  12. /************************************************************************/
  13. // $Id$
  14. /**
  15. * ModuleListParser
  16. * Class for parsing XML module list info
  17. * @access public
  18. * @author Cindy Qi Li
  19. * @package Admin Module
  20. */
  21. class ModuleListParser {
  22. // all private
  23. var $parser; // the XML handler
  24. var $module_rows = array(); // the module data
  25. var $character_data; // tmp variable for storing the data
  26. var $element_path; // array of element paths (basically a stack)
  27. var $row_num;
  28. var $history_num;
  29. function ModuleListParser() {
  30. $this->parser = xml_parser_create('');
  31. xml_set_object($this->parser, $this);
  32. xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, false); /* conform to W3C specs */
  33. xml_set_element_handler($this->parser, 'startElement', 'endElement');
  34. xml_set_character_data_handler($this->parser, 'characterData');
  35. }
  36. // public
  37. function parse($xml_data) {
  38. $this->element_path = array();
  39. $this->module_rows = array();
  40. $this->character_data = '';
  41. $this->row_num = 0;
  42. $this->history_num = 0;
  43. xml_parse($this->parser, $xml_data, TRUE);
  44. }
  45. // private
  46. function startElement($parser, $name, $attributes)
  47. {
  48. array_push($this->element_path, $name);
  49. }
  50. // private
  51. /* called when an element ends */
  52. /* removed the current element from the $path */
  53. function endElement($parser, $name) {
  54. if ($this->element_path == array('module_list', 'module', 'name'))
  55. {
  56. $this->module_rows[$this->row_num]['name'] = trim($this->character_data);
  57. }
  58. else if ($this->element_path === array('module_list', 'module', 'atutor_version'))
  59. {
  60. $this->module_rows[$this->row_num]['atutor_version'] = trim($this->character_data);
  61. }
  62. else if ($this->element_path === array('module_list', 'module', 'description'))
  63. {
  64. $this->module_rows[$this->row_num]['description'] = trim($this->character_data);
  65. }
  66. else if ($this->element_path === array('module_list', 'module', 'history'))
  67. {
  68. $this->history_num = 0;
  69. }
  70. else if ($this->element_path === array('module_list', 'module', 'history', 'release'))
  71. {
  72. $this->history_num++;
  73. }
  74. else if ($this->element_path === array('module_list', 'module', 'history', 'release', 'version'))
  75. {
  76. $this->module_rows[$this->row_num]['history'][$this->history_num]['version'] = trim($this->character_data);
  77. }
  78. else if ($this->element_path === array('module_list', 'module', 'history', 'release', 'filename'))
  79. {
  80. $this->module_rows[$this->row_num]['history'][$this->history_num]['filename'] = trim($this->character_data);
  81. }
  82. else if ($this->element_path === array('module_list', 'module', 'history', 'release', 'location'))
  83. {
  84. $this->module_rows[$this->row_num]['history'][$this->history_num]['location'] = trim($this->character_data);
  85. }
  86. else if ($this->element_path === array('module_list', 'module', 'history', 'release', 'install_folder'))
  87. {
  88. $this->module_rows[$this->row_num]['history'][$this->history_num]['install_folder'] = trim($this->character_data);
  89. }
  90. else if ($this->element_path === array('module_list', 'module', 'history', 'release', 'date'))
  91. {
  92. $this->module_rows[$this->row_num]['history'][$this->history_num]['date'] = trim($this->character_data);
  93. }
  94. else if ($this->element_path === array('module_list', 'module', 'history', 'release', 'state'))
  95. {
  96. $this->module_rows[$this->row_num]['history'][$this->history_num]['state'] = trim($this->character_data);
  97. }
  98. else if ($this->element_path === array('module_list', 'module', 'history', 'release', 'maintainer'))
  99. {
  100. $this->module_rows[$this->row_num]['history'][$this->history_num]['maintainer'] = trim($this->character_data);
  101. }
  102. else if ($this->element_path === array('module_list', 'module', 'history', 'release', 'notes'))
  103. {
  104. $this->module_rows[$this->row_num]['history'][$this->history_num]['notes'] = trim($this->character_data);
  105. }
  106. else if ($this->element_path === array('module_list', 'module'))
  107. {
  108. $this->row_num++;
  109. }
  110. array_pop($this->element_path);
  111. $this->character_data = '';
  112. }
  113. // private
  114. function characterData($parser, $data){
  115. $this->character_data .= $data;
  116. }
  117. // public
  118. function getNumOfModules()
  119. {
  120. return count($this->module_rows);
  121. }
  122. // public
  123. function getParsedArray()
  124. {
  125. return $this->module_rows;
  126. }
  127. }
  128. ?>