BxDolXmlParser.php 6.9 KB

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