dom_iterators.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 5 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2016 The PHP Group |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 3.01 of the PHP license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available through the world-wide-web at the following url: |
  10. | http://www.php.net/license/3_01.txt |
  11. | If you did not receive a copy of the PHP license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | license@php.net so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. | Authors: Christian Stocker <chregu@php.net> |
  16. | Rob Richards <rrichards@php.net> |
  17. +----------------------------------------------------------------------+
  18. */
  19. /* $Id$ */
  20. #ifdef HAVE_CONFIG_H
  21. #include "config.h"
  22. #endif
  23. #include "php.h"
  24. #if HAVE_LIBXML && HAVE_DOM
  25. #include "php_dom.h"
  26. #include "dom_ce.h"
  27. typedef struct _nodeIterator nodeIterator;
  28. struct _nodeIterator {
  29. int cur;
  30. int index;
  31. xmlNode *node;
  32. };
  33. typedef struct _notationIterator notationIterator;
  34. struct _notationIterator {
  35. int cur;
  36. int index;
  37. xmlNotation *notation;
  38. };
  39. static void itemHashScanner (void *payload, void *data, xmlChar *name) /* {{{ */
  40. {
  41. nodeIterator *priv = (nodeIterator *)data;
  42. if(priv->cur < priv->index) {
  43. priv->cur++;
  44. } else {
  45. if(priv->node == NULL) {
  46. priv->node = (xmlNode *)payload;
  47. }
  48. }
  49. }
  50. /* }}} */
  51. xmlNodePtr create_notation(const xmlChar *name, const xmlChar *ExternalID, const xmlChar *SystemID) /* {{{ */
  52. {
  53. xmlEntityPtr ret;
  54. ret = (xmlEntityPtr) xmlMalloc(sizeof(xmlEntity));
  55. memset(ret, 0, sizeof(xmlEntity));
  56. ret->type = XML_NOTATION_NODE;
  57. ret->name = xmlStrdup(name);
  58. ret->ExternalID = xmlStrdup(ExternalID);
  59. ret->SystemID = xmlStrdup(SystemID);
  60. ret->length = 0;
  61. ret->content = NULL;
  62. ret->URI = NULL;
  63. ret->orig = NULL;
  64. ret->children = NULL;
  65. ret->parent = NULL;
  66. ret->doc = NULL;
  67. ret->_private = NULL;
  68. ret->last = NULL;
  69. ret->prev = NULL;
  70. return((xmlNodePtr) ret);
  71. }
  72. /* }}} */
  73. xmlNode *php_dom_libxml_hash_iter(xmlHashTable *ht, int index) /* {{{ */
  74. {
  75. xmlNode *nodep = NULL;
  76. nodeIterator *iter;
  77. int htsize;
  78. if ((htsize = xmlHashSize(ht)) > 0 && index < htsize) {
  79. iter = emalloc(sizeof(nodeIterator));
  80. iter->cur = 0;
  81. iter->index = index;
  82. iter->node = NULL;
  83. xmlHashScan(ht, itemHashScanner, iter);
  84. nodep = iter->node;
  85. efree(iter);
  86. return nodep;
  87. } else {
  88. return NULL;
  89. }
  90. }
  91. /* }}} */
  92. xmlNode *php_dom_libxml_notation_iter(xmlHashTable *ht, int index) /* {{{ */
  93. {
  94. notationIterator *iter;
  95. xmlNotation *notep = NULL;
  96. int htsize;
  97. if ((htsize = xmlHashSize(ht)) > 0 && index < htsize) {
  98. iter = emalloc(sizeof(notationIterator));
  99. iter->cur = 0;
  100. iter->index = index;
  101. iter->notation = NULL;
  102. xmlHashScan(ht, itemHashScanner, iter);
  103. notep = iter->notation;
  104. efree(iter);
  105. return create_notation(notep->name, notep->PublicID, notep->SystemID);
  106. } else {
  107. return NULL;
  108. }
  109. }
  110. /* }}} */
  111. static void php_dom_iterator_dtor(zend_object_iterator *iter TSRMLS_DC) /* {{{ */
  112. {
  113. php_dom_iterator *iterator = (php_dom_iterator *)iter;
  114. zval_ptr_dtor((zval**)&iterator->intern.data);
  115. if (iterator->curobj) {
  116. zval_ptr_dtor((zval**)&iterator->curobj);
  117. }
  118. efree(iterator);
  119. }
  120. /* }}} */
  121. static int php_dom_iterator_valid(zend_object_iterator *iter TSRMLS_DC) /* {{{ */
  122. {
  123. php_dom_iterator *iterator = (php_dom_iterator *)iter;
  124. if (iterator->curobj) {
  125. return SUCCESS;
  126. } else {
  127. return FAILURE;
  128. }
  129. }
  130. /* }}} */
  131. static void php_dom_iterator_current_data(zend_object_iterator *iter, zval ***data TSRMLS_DC) /* {{{ */
  132. {
  133. php_dom_iterator *iterator = (php_dom_iterator *)iter;
  134. *data = &iterator->curobj;
  135. }
  136. /* }}} */
  137. static void php_dom_iterator_current_key(zend_object_iterator *iter, zval *key TSRMLS_DC) /* {{{ */
  138. {
  139. php_dom_iterator *iterator = (php_dom_iterator *)iter;
  140. zval *object = (zval *)iterator->intern.data;
  141. if (instanceof_function(Z_OBJCE_P(object), dom_nodelist_class_entry TSRMLS_CC)) {
  142. ZVAL_LONG(key, iter->index);
  143. } else {
  144. dom_object *intern = (dom_object *)zend_object_store_get_object(iterator->curobj TSRMLS_CC);
  145. if (intern != NULL && intern->ptr != NULL) {
  146. xmlNodePtr curnode = (xmlNodePtr)((php_libxml_node_ptr *)intern->ptr)->node;
  147. ZVAL_STRINGL(key, (char *) curnode->name, xmlStrlen(curnode->name), 1);
  148. } else {
  149. ZVAL_NULL(key);
  150. }
  151. }
  152. }
  153. /* }}} */
  154. static void php_dom_iterator_move_forward(zend_object_iterator *iter TSRMLS_DC) /* {{{ */
  155. {
  156. zval *curobj, *curattr = NULL;
  157. zval *object;
  158. xmlNodePtr curnode = NULL, basenode;
  159. dom_object *intern;
  160. dom_object *nnmap;
  161. dom_nnodemap_object *objmap;
  162. int ret, previndex=0;
  163. HashTable *nodeht;
  164. zval **entry;
  165. php_dom_iterator *iterator = (php_dom_iterator *)iter;
  166. object = (zval *)iterator->intern.data;
  167. nnmap = (dom_object *)zend_object_store_get_object(object TSRMLS_CC);
  168. objmap = (dom_nnodemap_object *)nnmap->ptr;
  169. curobj = iterator->curobj;
  170. intern = (dom_object *)zend_object_store_get_object(curobj TSRMLS_CC);
  171. if (intern != NULL && intern->ptr != NULL) {
  172. if (objmap->nodetype != XML_ENTITY_NODE &&
  173. objmap->nodetype != XML_NOTATION_NODE) {
  174. if (objmap->nodetype == DOM_NODESET) {
  175. nodeht = HASH_OF(objmap->baseobjptr);
  176. zend_hash_move_forward(nodeht);
  177. if (zend_hash_get_current_data(nodeht, (void **) &entry)==SUCCESS) {
  178. curattr = *entry;
  179. Z_ADDREF_P(curattr);
  180. }
  181. } else {
  182. curnode = (xmlNodePtr)((php_libxml_node_ptr *)intern->ptr)->node;
  183. if (objmap->nodetype == XML_ATTRIBUTE_NODE ||
  184. objmap->nodetype == XML_ELEMENT_NODE) {
  185. curnode = curnode->next;
  186. } else {
  187. /* Nav the tree evey time as this is LIVE */
  188. basenode = dom_object_get_node(objmap->baseobj);
  189. if (basenode && (basenode->type == XML_DOCUMENT_NODE ||
  190. basenode->type == XML_HTML_DOCUMENT_NODE)) {
  191. basenode = xmlDocGetRootElement((xmlDoc *) basenode);
  192. } else if (basenode) {
  193. basenode = basenode->children;
  194. } else {
  195. goto err;
  196. }
  197. curnode = dom_get_elements_by_tag_name_ns_raw(basenode, objmap->ns, objmap->local, &previndex, iter->index);
  198. }
  199. }
  200. } else {
  201. if (objmap->nodetype == XML_ENTITY_NODE) {
  202. curnode = php_dom_libxml_hash_iter(objmap->ht, iter->index);
  203. } else {
  204. curnode = php_dom_libxml_notation_iter(objmap->ht, iter->index);
  205. }
  206. }
  207. }
  208. err:
  209. zval_ptr_dtor((zval**)&curobj);
  210. if (curnode) {
  211. MAKE_STD_ZVAL(curattr);
  212. curattr = php_dom_create_object(curnode, &ret, curattr, objmap->baseobj TSRMLS_CC);
  213. }
  214. iterator->curobj = curattr;
  215. }
  216. /* }}} */
  217. zend_object_iterator_funcs php_dom_iterator_funcs = {
  218. php_dom_iterator_dtor,
  219. php_dom_iterator_valid,
  220. php_dom_iterator_current_data,
  221. php_dom_iterator_current_key,
  222. php_dom_iterator_move_forward,
  223. NULL
  224. };
  225. zend_object_iterator *php_dom_get_iterator(zend_class_entry *ce, zval *object, int by_ref TSRMLS_DC) /* {{{ */
  226. {
  227. dom_object *intern;
  228. dom_nnodemap_object *objmap;
  229. xmlNodePtr nodep, curnode=NULL;
  230. zval *curattr = NULL;
  231. int ret, curindex = 0;
  232. HashTable *nodeht;
  233. zval **entry;
  234. php_dom_iterator *iterator;
  235. if (by_ref) {
  236. zend_error(E_ERROR, "An iterator cannot be used with foreach by reference");
  237. }
  238. iterator = emalloc(sizeof(php_dom_iterator));
  239. Z_ADDREF_P(object);
  240. iterator->intern.data = (void*)object;
  241. iterator->intern.funcs = &php_dom_iterator_funcs;
  242. intern = (dom_object *)zend_object_store_get_object(object TSRMLS_CC);
  243. objmap = (dom_nnodemap_object *)intern->ptr;
  244. if (objmap != NULL) {
  245. if (objmap->nodetype != XML_ENTITY_NODE &&
  246. objmap->nodetype != XML_NOTATION_NODE) {
  247. if (objmap->nodetype == DOM_NODESET) {
  248. nodeht = HASH_OF(objmap->baseobjptr);
  249. zend_hash_internal_pointer_reset(nodeht);
  250. if (zend_hash_get_current_data(nodeht, (void **) &entry)==SUCCESS) {
  251. curattr = *entry;
  252. Z_ADDREF_P(curattr);
  253. }
  254. } else {
  255. nodep = (xmlNode *)dom_object_get_node(objmap->baseobj);
  256. if (!nodep) {
  257. goto err;
  258. }
  259. if (objmap->nodetype == XML_ATTRIBUTE_NODE || objmap->nodetype == XML_ELEMENT_NODE) {
  260. if (objmap->nodetype == XML_ATTRIBUTE_NODE) {
  261. curnode = (xmlNodePtr) nodep->properties;
  262. } else {
  263. curnode = (xmlNodePtr) nodep->children;
  264. }
  265. } else {
  266. if (nodep->type == XML_DOCUMENT_NODE || nodep->type == XML_HTML_DOCUMENT_NODE) {
  267. nodep = xmlDocGetRootElement((xmlDoc *) nodep);
  268. } else {
  269. nodep = nodep->children;
  270. }
  271. curnode = dom_get_elements_by_tag_name_ns_raw(nodep, objmap->ns, objmap->local, &curindex, 0);
  272. }
  273. }
  274. } else {
  275. if (objmap->nodetype == XML_ENTITY_NODE) {
  276. curnode = php_dom_libxml_hash_iter(objmap->ht, 0);
  277. } else {
  278. curnode = php_dom_libxml_notation_iter(objmap->ht, 0);
  279. }
  280. }
  281. }
  282. err:
  283. if (curnode) {
  284. MAKE_STD_ZVAL(curattr);
  285. curattr = php_dom_create_object(curnode, &ret, curattr, objmap->baseobj TSRMLS_CC);
  286. }
  287. iterator->curobj = curattr;
  288. return (zend_object_iterator*)iterator;
  289. }
  290. /* }}} */
  291. #endif
  292. /*
  293. * Local variables:
  294. * tab-width: 4
  295. * c-basic-offset: 4
  296. * End:
  297. * vim600: noet sw=4 ts=4 fdm=marker
  298. * vim<600: noet sw=4 ts=4
  299. */