BxDolXmlParser.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) BoonEx Pty Limited - http://www.boonex.com/
  4. * CC-BY License - http://creativecommons.org/licenses/by/3.0/
  5. *
  6. * @defgroup DolphinCore Dolphin Core
  7. * @{
  8. */
  9. defined('BX_DOL') or die('hack attempt');
  10. bx_import('BxDol');
  11. class BxDolXmlParser extends BxDol implements iBxDolSingleton {
  12. protected $rParser;
  13. function BxDolXmlParser() {
  14. if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
  15. trigger_error ('Multiple instances are not allowed for the class: ' . get_class($this), E_USER_ERROR);
  16. parent::BxDol();
  17. }
  18. /**
  19. * Prevent cloning the instance
  20. */
  21. public function __clone() {
  22. if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
  23. trigger_error('Clone is not allowed for the class: ' . get_class($this), E_USER_ERROR);
  24. }
  25. /**
  26. * Get singleton instance of the class
  27. */
  28. public static function getInstance() {
  29. if(!isset($GLOBALS['bxDolClasses'][__CLASS__]))
  30. $GLOBALS['bxDolClasses'][__CLASS__] = new BxDolXmlParser();
  31. return $GLOBALS['bxDolClasses'][__CLASS__];
  32. }
  33. /**
  34. * Get the value of specified attribute for specified tag.
  35. */
  36. function getAttribute($sXmlContent, $sXmlTag, $sXmlAttribute) {
  37. $aValues = $aIndexes = array();
  38. $rParser = xml_parser_create("UTF-8");
  39. xml_parse_into_struct($rParser, $sXmlContent, $aValues, $aIndexes);
  40. xml_parser_free($rParser);
  41. $aFieldIndex = $aIndexes[strtoupper($sXmlTag)][0];
  42. return $aValues[$aFieldIndex]['attributes'][strtoupper($sXmlAttribute)];
  43. }
  44. /**
  45. * Get an array of attributes for specified tag or an array of tags with the same name.
  46. */
  47. function getAttributes($sXmlContent, $sXmlTagName, $sXmlTagIndex = -1) {
  48. $aValues = $aIndexes = array();
  49. $rParser = xml_parser_create("UTF-8");
  50. xml_parse_into_struct($rParser, $sXmlContent, $aValues, $aIndexes);
  51. xml_parser_free($rParser);
  52. /**
  53. * gets two-dimensional array of attributes.
  54. * tags-attributes
  55. */
  56. if($sXmlTagIndex == -1) {
  57. $aResult = array();
  58. $aTagIndexes = $aIndexes[strtoupper($sXmlTagName)];
  59. if(count($aTagIndexes) <= 0) return NULL;
  60. foreach($aTagIndexes as $iTagIndex)
  61. $aResult[] = $aValues[$iTagIndex]['attributes'];
  62. return $aResult;
  63. }
  64. else {
  65. $iTagIndex = $aIndexes[strtoupper($sXmlTagName)][$sXmlTagIndex];
  66. return $aValues[$iTagIndex]['attributes'];
  67. }
  68. }
  69. /**
  70. * Get an array of tags or one tag if its index is specified.
  71. */
  72. function getTags($sXmlContent, $sXmlTagName, $iXmlTagIndex = -1) {
  73. $aValues = $aIndexes = array();
  74. $rParser = xml_parser_create("UTF-8");
  75. xml_parse_into_struct($rParser, $sXmlContent, $aValues, $aIndexes);
  76. xml_parser_free($rParser);
  77. //--- Get an array of tags ---//
  78. if($iXmlTagIndex == -1) {
  79. $aResult = array();
  80. $aTagIndexes = $aIndexes[strtoupper($sXmlTagName)];
  81. if(count($aTagIndexes) <= 0) return NULL;
  82. foreach($aTagIndexes as $iTagIndex)
  83. $aResult[] = $aValues[$iTagIndex];
  84. return $aResult;
  85. }
  86. else {
  87. $iTagIndex = $aIndexes[strtoupper($sXmlTagName)][$iXmlTagIndex];
  88. return $aValues[$iTagIndex];
  89. }
  90. }
  91. /**
  92. * Gets the values of the given tag.
  93. */
  94. function getValues($sXmlContent, $sXmlTagName) {
  95. $aValues = $aIndexes = array();
  96. $rParser = xml_parser_create("UTF-8");
  97. xml_parse_into_struct($rParser, $sXmlContent, $aValues, $aIndexes);
  98. xml_parser_free($rParser);
  99. $aTagIndexes = $aIndexes[strtoupper($sXmlTagName)];
  100. $aTagIndexes = isset($aTagIndexes) ? $aTagIndexes : array();
  101. $aReturnValues = array();
  102. foreach($aTagIndexes as $iTagIndex) {
  103. $aReturnValues[$aValues[$iTagIndex]['attributes']['NAME']] =
  104. isset($aValues[$iTagIndex]['value']) ? $aValues[$iTagIndex]['value'] : NULL;
  105. }
  106. return $aReturnValues;
  107. }
  108. /**
  109. * Sets the values of tag where attribute "key" equals to specified.
  110. */
  111. function setValues($sXmlContent, $sXmlTagName, $aKeyValues) {
  112. $aValues = $aIndexes = array();
  113. $rParser = xml_parser_create("UTF-8");
  114. xml_parse_into_struct($rParser, $sXmlContent, $aValues, $aIndexes);
  115. xml_parser_free($rParser);
  116. $aTagIndexes = $aIndexes[strtoupper($sXmlTagName)];
  117. if(count($aTagIndexes) == 0) return $this->getContent();
  118. foreach($aTagIndexes as $iTagIndex)
  119. foreach($aKeyValues as $sKey => $sValue)
  120. if($aValues[$iTagIndex]['attributes']['KEY'] == $sKey) {
  121. $aValues[$iTagIndex]['value'] = $sValue;
  122. break;
  123. }
  124. return $this->getContent($aValues);
  125. }
  126. /**
  127. * Adds given values to XML content.
  128. */
  129. function addValues($sXmlContent, $sXmlTagName, $aKeyValues) {
  130. $aValues = $aIndexes = array();
  131. $rParser = xml_parser_create("UTF-8");
  132. xml_parse_into_struct($rParser, $sXmlContent, $aValues, $aIndexes);
  133. xml_parser_free($rParser);
  134. $aTagIndexes = $aIndexes[strtoupper($sXmlTagName)];
  135. $iLastTagIndex = $aTagIndexes[count($aTagIndexes) - 1];
  136. $iAddsCount = count($aKeyValues);
  137. $iLevel = $aValues[$iLastTagIndex]["level"];
  138. for($i=count($aValues)-1; $i>$iLastTagIndex; $i--)
  139. $aValues[$i+$iAddsCount] = $aValues[$i];
  140. $i = $iLastTagIndex;
  141. foreach($aKeyValues as $sKey => $sValue) {
  142. $i++;
  143. $aValues[$i] = Array("tag" => $sXmlTagName, "type" => "complete", "level" => $iLevel, "attributes" => Array("KEY" => $sKey), "value" => $sValue);
  144. }
  145. return $this->getContent($aValues);
  146. }
  147. /**
  148. * get content in XML format from given values array
  149. */
  150. function getContent($aValues = array()) {
  151. $sContent = "";
  152. foreach($aValues as $aValue) {
  153. $sTagName = strtolower($aValue['tag']);
  154. switch($aValue['type']) {
  155. case "open":
  156. $sContent .= "<" . $sTagName . ">";
  157. break;
  158. case "complete":
  159. $sContent .= "<" . $sTagName;
  160. if(isset($aValue['attributes']))
  161. foreach($aValue['attributes'] as $sAttrKey => $sAttrValue)
  162. $sContent .= " " . strtolower($sAttrKey) . "=\"" . $sAttrValue . "\"";
  163. $sContent .= isset($aValue['value']) ? "><![CDATA[" . $aValue['value'] . "]]></" . $sTagName . ">" : " />";
  164. break;
  165. case "close":
  166. $sContent .= "</" . $sTagName . ">";
  167. break;
  168. }
  169. }
  170. return $sContent;
  171. }
  172. }
  173. /** @} */