BxDolXmlParser.php 6.9 KB

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