BxDolXmlParser.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. $sTag = strtoupper($sXmlTag);
  46. if(!isset($aIndexes[$sTag]))
  47. return false;
  48. $iFieldIndex = $aIndexes[$sTag][0];
  49. $sAttribute = strtoupper($sXmlAttribute);
  50. if(!isset($aValues[$iFieldIndex]['attributes'][$sAttribute]))
  51. return false;
  52. return $aValues[$iFieldIndex]['attributes'][$sAttribute];
  53. }
  54. /**
  55. * Get an array of attributes for specified tag or an array of tags with the same name.
  56. */
  57. function getAttributes($sXmlContent, $sXmlTagName, $sXmlTagIndex = -1)
  58. {
  59. $aValues = $aIndexes = array();
  60. $rParser = xml_parser_create("UTF-8");
  61. xml_parse_into_struct($rParser, $sXmlContent, $aValues, $aIndexes);
  62. xml_parser_free($rParser);
  63. /**
  64. * gets two-dimensional array of attributes.
  65. * tags-attributes
  66. */
  67. if($sXmlTagIndex == -1) {
  68. $aResult = array();
  69. $aTagIndexes = $aIndexes[strtoupper($sXmlTagName)];
  70. if(count($aTagIndexes) <= 0) return NULL;
  71. foreach($aTagIndexes as $iTagIndex)
  72. $aResult[] = $aValues[$iTagIndex]['attributes'];
  73. return $aResult;
  74. } else {
  75. $iTagIndex = $aIndexes[strtoupper($sXmlTagName)][$sXmlTagIndex];
  76. return $aValues[$iTagIndex]['attributes'];
  77. }
  78. }
  79. /**
  80. * Get an array of tags or one tag if its index is specified.
  81. */
  82. function getTags($sXmlContent, $sXmlTagName, $iXmlTagIndex = -1)
  83. {
  84. $aValues = $aIndexes = array();
  85. $rParser = xml_parser_create("UTF-8");
  86. xml_parse_into_struct($rParser, $sXmlContent, $aValues, $aIndexes);
  87. xml_parser_free($rParser);
  88. //--- Get an array of tags ---//
  89. if($iXmlTagIndex == -1) {
  90. $aResult = array();
  91. $aTagIndexes = $aIndexes[strtoupper($sXmlTagName)];
  92. if(count($aTagIndexes) <= 0) return NULL;
  93. foreach($aTagIndexes as $iTagIndex)
  94. $aResult[] = $aValues[$iTagIndex];
  95. return $aResult;
  96. } else {
  97. $iTagIndex = $aIndexes[strtoupper($sXmlTagName)][$iXmlTagIndex];
  98. return $aValues[$iTagIndex];
  99. }
  100. }
  101. /**
  102. * Gets the values of the given tag.
  103. */
  104. function getValues($sXmlContent, $sXmlTagName)
  105. {
  106. $aValues = $aIndexes = array();
  107. $rParser = xml_parser_create("UTF-8");
  108. xml_parse_into_struct($rParser, $sXmlContent, $aValues, $aIndexes);
  109. xml_parser_free($rParser);
  110. $sTag = strtoupper($sXmlTagName);
  111. $aTagIndexes = isset($aIndexes[$sTag]) ? $aIndexes[$sTag] : array();
  112. $aReturnValues = array();
  113. foreach($aTagIndexes as $iTagIndex) {
  114. $aReturnValues[$aValues[$iTagIndex]['attributes']['NAME']] =
  115. isset($aValues[$iTagIndex]['value']) ? $aValues[$iTagIndex]['value'] : NULL;
  116. }
  117. return $aReturnValues;
  118. }
  119. /**
  120. * Sets the values of tag where attribute "key" equals to specified.
  121. */
  122. function setValues($sXmlContent, $sXmlTagName, $aKeyValues)
  123. {
  124. $aValues = $aIndexes = array();
  125. $rParser = xml_parser_create("UTF-8");
  126. xml_parse_into_struct($rParser, $sXmlContent, $aValues, $aIndexes);
  127. xml_parser_free($rParser);
  128. $aTagIndexes = $aIndexes[strtoupper($sXmlTagName)];
  129. if(count($aTagIndexes) == 0) return $this->getContent();
  130. foreach($aTagIndexes as $iTagIndex)
  131. foreach($aKeyValues as $sKey => $sValue)
  132. if($aValues[$iTagIndex]['attributes']['KEY'] == $sKey) {
  133. $aValues[$iTagIndex]['value'] = $sValue;
  134. break;
  135. }
  136. return $this->getContent($aValues);
  137. }
  138. /**
  139. * Adds given values to XML content.
  140. */
  141. function addValues($sXmlContent, $sXmlTagName, $aKeyValues)
  142. {
  143. $aValues = $aIndexes = array();
  144. $rParser = xml_parser_create("UTF-8");
  145. xml_parse_into_struct($rParser, $sXmlContent, $aValues, $aIndexes);
  146. xml_parser_free($rParser);
  147. $aTagIndexes = $aIndexes[strtoupper($sXmlTagName)];
  148. $iLastTagIndex = $aTagIndexes[count($aTagIndexes) - 1];
  149. $iAddsCount = count($aKeyValues);
  150. $iLevel = $aValues[$iLastTagIndex]["level"];
  151. for($i=count($aValues)-1; $i>$iLastTagIndex; $i--)
  152. $aValues[$i+$iAddsCount] = $aValues[$i];
  153. $i = $iLastTagIndex;
  154. foreach($aKeyValues as $sKey => $sValue) {
  155. $i++;
  156. $aValues[$i] = Array("tag" => $sXmlTagName, "type" => "complete", "level" => $iLevel, "attributes" => Array("KEY" => $sKey), "value" => $sValue);
  157. }
  158. return $this->getContent($aValues);
  159. }
  160. /**
  161. * get content in XML format from given values array
  162. */
  163. function getContent($aValues = array())
  164. {
  165. $sContent = "";
  166. foreach($aValues as $aValue) {
  167. $sTagName = strtolower($aValue['tag']);
  168. switch($aValue['type']) {
  169. case "open":
  170. $sContent .= "<" . $sTagName . ">";
  171. break;
  172. case "complete":
  173. $sContent .= "<" . $sTagName;
  174. if(isset($aValue['attributes']))
  175. foreach($aValue['attributes'] as $sAttrKey => $sAttrValue)
  176. $sContent .= " " . strtolower($sAttrKey) . "=\"" . $sAttrValue . "\"";
  177. $sContent .= isset($aValue['value']) ? "><![CDATA[" . $aValue['value'] . "]]></" . $sTagName . ">" : " />";
  178. break;
  179. case "close":
  180. $sContent .= "</" . $sTagName . ">";
  181. break;
  182. }
  183. }
  184. return $sContent;
  185. }
  186. }
  187. /** @} */