PageRenderTime 43ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/opencaps/conversion_service/include/classes/core_XmlTagTrace.php

https://github.com/atutor/atutor_opencaps
PHP | 242 lines | 97 code | 63 blank | 82 comment | 17 complexity | 9c468b4b4d6b8297d43506398b26eeda MD5 | raw file
  1. <?php
  2. //
  3. /**
  4. * This class provides the functionality to trace an xml tag
  5. */
  6. class XmlTagTrace
  7. {
  8. private $tagToTrace; // the xml tag to be collected
  9. private $tagToTraceLevel; // The level of the xml tag bo be collected/traced
  10. private $xmlString; // Xml data as string
  11. private $xmlTag; // temporary object to store XML object while reading xml array
  12. private $xmlTagState; // true or false if tracing tag is done
  13. private $myXmlCollection; // collection of the traced xml tag
  14. /**
  15. * Class Constructor: starts tracing a XML tag. It recieves a xml TagName and xml TagLevel
  16. * @param String $theXmlString XML data as String
  17. * @param String $theTagToTrace XML Tag to trace
  18. * @param String $theTagToTraceLevel Level of the XML tag to trace
  19. */
  20. function __construct($theXmlString,$theTagToTrace,$theTagToTraceLevel)
  21. {
  22. // set the XML data
  23. //$this->xmlString = $theXmlString;
  24. // set the tag to trace and the starting level
  25. $this->tagToTrace = $theTagToTrace;
  26. $this->tagToTraceLevel = $theTagToTraceLevel;
  27. // create XML tag Collection
  28. $this->myXmlCollection = new XmlTagCollection();
  29. // start tracing
  30. $this->traceXmlTag($theXmlString);
  31. } // __construct() end
  32. /**
  33. * Returns the XML tag Collection
  34. *
  35. */
  36. public function getCollection()
  37. {
  38. //get from here of from XmlTagCollection collection object???
  39. return $this->myXmlCollection->getXmlTagCollection();
  40. //return $this->myXmlCollection;
  41. //echo '<br>From '
  42. }// end getCollection()
  43. /**
  44. * Creates an XML parcer and Iterates through array
  45. *
  46. * @param unknown_type $theXmlString
  47. */
  48. private function traceXmlTag($theXmlString)
  49. {
  50. // create XML parser
  51. $p = xml_parser_create();
  52. // parse XML data into array
  53. xml_parse_into_struct($p, $theXmlString, $xmlVals, $xmlIndex);
  54. // free XML parser
  55. xml_parser_free($p);
  56. // initialize XML object ????
  57. //$myXmlTag = new XmlTag()
  58. // start looping xml array
  59. for($i = 0; $i < count($xmlVals); $i++)
  60. {
  61. // initialize XML data
  62. $theTagName='';
  63. $theTagType='';
  64. $theTagLevel='';
  65. $theTagValue='';
  66. $theTagAtrib = Array();
  67. // verify data before adding
  68. if (isset($xmlVals[$i]['tag']))
  69. {
  70. $theTagName = $xmlVals[$i]['tag'];
  71. }
  72. if (isset($xmlVals[$i]['type']))
  73. {
  74. $theTagType = $xmlVals[$i]['type'];
  75. }
  76. if (isset($xmlVals[$i]['level']))
  77. {
  78. $theTagLevel = $xmlVals[$i]['level'];
  79. }
  80. if (isset($xmlVals[$i]['value']))
  81. {
  82. $theTagValue = $xmlVals[$i]['value'];
  83. }
  84. if (isset($xmlVals[$i]['attributes']))
  85. {
  86. $theTagAtrib = $xmlVals[$i]['attributes'];
  87. }
  88. // set xml data
  89. $this->setXmlData($theTagName,$theTagType,$theTagLevel,$theTagValue,$theTagAtrib);
  90. } // end for loop xml array
  91. } // end traceXmlTag()
  92. /**
  93. * Recieves Xml data from each tag and determines if values should be collected
  94. * this to trace <P> and <div> tags
  95. *
  96. * @param String $theTagName
  97. * @param String $theTagType
  98. * @param String $theTagLevel
  99. * @param String $theTagValue
  100. * @param String $theTagAtrib
  101. */
  102. private function setXmlData($theTagName,$theTagType,$theTagLevel,$theTagValue,$theTagAtrib)
  103. {
  104. //echo '<br/>'.$this->tagToTrace;
  105. // verify also if the level is lower !!!!
  106. //echo '<br/>'.$theTagName;
  107. // verify if this data is from the xml tag being traced/collected
  108. if ($theTagName == $this->tagToTrace)
  109. {
  110. //echo '<br/>'.$theTagName.' TAG IS THE SAME';
  111. //echo '<br/>Value: '.$theTagValue.'';
  112. // if tag is complete. The tag has NO children
  113. if ($theTagType=='complete')
  114. {
  115. // create a XmlTag object
  116. $this->xmlTag = new XmlTag($theTagName,$theTagType,$theTagLevel,$theTagValue,$theTagAtrib);
  117. // tracing this tag is done.
  118. $this->xmlTagState = true;
  119. $this->xmlTag->setTagState(true);
  120. //echo '<br/>'.$theTagName.' TAG is complete !!!';
  121. //$this->xmlTag->toString();
  122. // add to collection
  123. $this->myXmlCollection->addXmlTagObject($this->xmlTag);
  124. // reset temp object
  125. $this->xmlTag = null;
  126. } // end if complete
  127. // if tag is open. tag do have children
  128. else if ($theTagType=='open')
  129. {
  130. // create a new XmlTag object
  131. $this->xmlTag = new XmlTag($theTagName,$theTagType,$theTagLevel,$theTagValue,$theTagAtrib);
  132. // tracing this tag is NOT done.
  133. $this->xmlTagState = false;
  134. //echo '<br/>'.$theTagName.' TAG is Open !!!';
  135. } // end if
  136. // if tag cdata. Getting data of the traced Tag, after any children data
  137. else if ($theTagType=='cdata')
  138. {
  139. // add value to traced tag
  140. // OJO !! This skips any child value
  141. $this->xmlTag->addToTagValue($theTagValue);
  142. // tracing this tag is NOT done.
  143. $this->xmlTagState = false;
  144. //echo '<br/>'.$theTagName.' TAG is cdata!!!';
  145. } // end if
  146. // if tag close. Getting data of the traced Tag is done
  147. else if ($theTagType=='close')
  148. {
  149. // add value of traced tag
  150. $this->xmlTag->addToTagValue($theTagValue);
  151. // tracing this tag is NOT done.
  152. $this->xmlTagState = true;
  153. $this->xmlTag->setTagState(true);
  154. //echo '<br/>'.$theTagName.' TAG is close!!!';
  155. //add to collection
  156. $this->myXmlCollection->addXmlTagObject($this->xmlTag);
  157. // reset temp object
  158. $this->xmlTag = null;
  159. } // end if
  160. }// end if tracing xml tag
  161. /*
  162. * if the tag is a children. the tagToTraceLevel is lower than the current TagLevel
  163. * this for all tags like <br/>, <b>, <i>, <u>, or even for <span> inside our traced tag
  164. */
  165. if ($this->tagToTraceLevel<$theTagLevel && (isset($this->xmlTag)))
  166. {
  167. // adding children data to tag as value
  168. $this->xmlTag->addChildTagAsValue($theTagName,$theTagType,$theTagValue);
  169. } // end if
  170. } // end setXmlData()
  171. /**
  172. * Print all values of the XML tag as a String
  173. */
  174. public function toString()
  175. {
  176. $this->myXmlCollection->toString();
  177. }// toString() end
  178. } // end class XmlTagTrace
  179. ?>