namednodemap.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Copyright (c) The PHP Group |
  4. +----------------------------------------------------------------------+
  5. | This source file is subject to version 3.01 of the PHP license, |
  6. | that is bundled with this package in the file LICENSE, and is |
  7. | available through the world-wide-web at the following url: |
  8. | https://www.php.net/license/3_01.txt |
  9. | If you did not receive a copy of the PHP license and are unable to |
  10. | obtain it through the world-wide-web, please send a note to |
  11. | license@php.net so we can mail you a copy immediately. |
  12. +----------------------------------------------------------------------+
  13. | Authors: Christian Stocker <chregu@php.net> |
  14. | Rob Richards <rrichards@php.net> |
  15. +----------------------------------------------------------------------+
  16. */
  17. #ifdef HAVE_CONFIG_H
  18. #include "config.h"
  19. #endif
  20. #include "php.h"
  21. #if defined(HAVE_LIBXML) && defined(HAVE_DOM)
  22. #include "php_dom.h"
  23. #include "zend_interfaces.h"
  24. /*
  25. * class DOMNamedNodeMap
  26. *
  27. * URL: https://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-1780488922
  28. * Since:
  29. */
  30. static int get_namednodemap_length(dom_object *obj)
  31. {
  32. dom_nnodemap_object *objmap = (dom_nnodemap_object *) obj->ptr;
  33. if (!objmap) {
  34. return 0;
  35. }
  36. if (objmap->nodetype == XML_NOTATION_NODE || objmap->nodetype == XML_ENTITY_NODE) {
  37. return objmap->ht ? xmlHashSize(objmap->ht) : 0;
  38. }
  39. int count = 0;
  40. xmlNodePtr nodep = dom_object_get_node(objmap->baseobj);
  41. if (nodep) {
  42. xmlAttrPtr curnode = nodep->properties;
  43. if (curnode) {
  44. count++;
  45. while (curnode->next != NULL) {
  46. count++;
  47. curnode = curnode->next;
  48. }
  49. }
  50. }
  51. return count;
  52. }
  53. /* {{{ length int
  54. readonly=yes
  55. URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-6D0FB19E
  56. Since:
  57. */
  58. int dom_namednodemap_length_read(dom_object *obj, zval *retval)
  59. {
  60. ZVAL_LONG(retval, get_namednodemap_length(obj));
  61. return SUCCESS;
  62. }
  63. /* }}} */
  64. /* {{{ URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-1074577549
  65. Since:
  66. */
  67. PHP_METHOD(DOMNamedNodeMap, getNamedItem)
  68. {
  69. zval *id;
  70. int ret;
  71. size_t namedlen=0;
  72. dom_object *intern;
  73. xmlNodePtr itemnode = NULL;
  74. char *named;
  75. dom_nnodemap_object *objmap;
  76. xmlNodePtr nodep;
  77. xmlNotation *notep = NULL;
  78. id = ZEND_THIS;
  79. if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &named, &namedlen) == FAILURE) {
  80. RETURN_THROWS();
  81. }
  82. intern = Z_DOMOBJ_P(id);
  83. objmap = (dom_nnodemap_object *)intern->ptr;
  84. if (objmap != NULL) {
  85. if ((objmap->nodetype == XML_NOTATION_NODE) ||
  86. objmap->nodetype == XML_ENTITY_NODE) {
  87. if (objmap->ht) {
  88. if (objmap->nodetype == XML_ENTITY_NODE) {
  89. itemnode = (xmlNodePtr)xmlHashLookup(objmap->ht, (xmlChar *) named);
  90. } else {
  91. notep = (xmlNotation *)xmlHashLookup(objmap->ht, (xmlChar *) named);
  92. if (notep) {
  93. itemnode = create_notation(notep->name, notep->PublicID, notep->SystemID);
  94. }
  95. }
  96. }
  97. } else {
  98. nodep = dom_object_get_node(objmap->baseobj);
  99. if (nodep) {
  100. itemnode = (xmlNodePtr)xmlHasProp(nodep, (xmlChar *) named);
  101. }
  102. }
  103. }
  104. if (itemnode) {
  105. DOM_RET_OBJ(itemnode, &ret, objmap->baseobj);
  106. return;
  107. } else {
  108. RETVAL_NULL();
  109. }
  110. }
  111. /* }}} end dom_namednodemap_get_named_item */
  112. /* {{{ URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-349467F9
  113. Since:
  114. */
  115. PHP_METHOD(DOMNamedNodeMap, item)
  116. {
  117. zval *id;
  118. zend_long index;
  119. int ret;
  120. dom_object *intern;
  121. xmlNodePtr itemnode = NULL;
  122. dom_nnodemap_object *objmap;
  123. xmlNodePtr nodep, curnode;
  124. int count;
  125. id = ZEND_THIS;
  126. if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &index) == FAILURE) {
  127. RETURN_THROWS();
  128. }
  129. if (index < 0 || ZEND_LONG_INT_OVFL(index)) {
  130. zend_argument_value_error(1, "must be between 0 and %d", INT_MAX);
  131. RETURN_THROWS();
  132. }
  133. intern = Z_DOMOBJ_P(id);
  134. objmap = (dom_nnodemap_object *)intern->ptr;
  135. if (objmap != NULL) {
  136. if ((objmap->nodetype == XML_NOTATION_NODE) ||
  137. objmap->nodetype == XML_ENTITY_NODE) {
  138. if (objmap->ht) {
  139. if (objmap->nodetype == XML_ENTITY_NODE) {
  140. itemnode = php_dom_libxml_hash_iter(objmap->ht, index);
  141. } else {
  142. itemnode = php_dom_libxml_notation_iter(objmap->ht, index);
  143. }
  144. }
  145. } else {
  146. nodep = dom_object_get_node(objmap->baseobj);
  147. if (nodep) {
  148. curnode = (xmlNodePtr)nodep->properties;
  149. count = 0;
  150. while (count < index && curnode != NULL) {
  151. count++;
  152. curnode = (xmlNodePtr)curnode->next;
  153. }
  154. itemnode = curnode;
  155. }
  156. }
  157. }
  158. if (itemnode) {
  159. DOM_RET_OBJ(itemnode, &ret, objmap->baseobj);
  160. return;
  161. }
  162. RETVAL_NULL();
  163. }
  164. /* }}} end dom_namednodemap_item */
  165. /* {{{ URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-getNamedItemNS
  166. Since: DOM Level 2
  167. */
  168. PHP_METHOD(DOMNamedNodeMap, getNamedItemNS)
  169. {
  170. zval *id;
  171. int ret;
  172. size_t namedlen=0, urilen=0;
  173. dom_object *intern;
  174. xmlNodePtr itemnode = NULL;
  175. char *uri, *named;
  176. dom_nnodemap_object *objmap;
  177. xmlNodePtr nodep;
  178. xmlNotation *notep = NULL;
  179. id = ZEND_THIS;
  180. if (zend_parse_parameters(ZEND_NUM_ARGS(), "s!s", &uri, &urilen, &named, &namedlen) == FAILURE) {
  181. RETURN_THROWS();
  182. }
  183. intern = Z_DOMOBJ_P(id);
  184. objmap = (dom_nnodemap_object *)intern->ptr;
  185. if (objmap != NULL) {
  186. if ((objmap->nodetype == XML_NOTATION_NODE) ||
  187. objmap->nodetype == XML_ENTITY_NODE) {
  188. if (objmap->ht) {
  189. if (objmap->nodetype == XML_ENTITY_NODE) {
  190. itemnode = (xmlNodePtr)xmlHashLookup(objmap->ht, (xmlChar *) named);
  191. } else {
  192. notep = (xmlNotation *)xmlHashLookup(objmap->ht, (xmlChar *) named);
  193. if (notep) {
  194. itemnode = create_notation(notep->name, notep->PublicID, notep->SystemID);
  195. }
  196. }
  197. }
  198. } else {
  199. nodep = dom_object_get_node(objmap->baseobj);
  200. if (nodep) {
  201. itemnode = (xmlNodePtr)xmlHasNsProp(nodep, (xmlChar *) named, (xmlChar *) uri);
  202. }
  203. }
  204. }
  205. if (itemnode) {
  206. DOM_RET_OBJ(itemnode, &ret, objmap->baseobj);
  207. return;
  208. } else {
  209. RETVAL_NULL();
  210. }
  211. }
  212. /* }}} end dom_namednodemap_get_named_item_ns */
  213. /* {{{ */
  214. PHP_METHOD(DOMNamedNodeMap, count)
  215. {
  216. zval *id;
  217. dom_object *intern;
  218. id = ZEND_THIS;
  219. if (zend_parse_parameters_none() == FAILURE) {
  220. RETURN_THROWS();
  221. }
  222. intern = Z_DOMOBJ_P(id);
  223. RETURN_LONG(get_namednodemap_length(intern));
  224. }
  225. /* }}} end dom_namednodemap_count */
  226. PHP_METHOD(DOMNamedNodeMap, getIterator)
  227. {
  228. if (zend_parse_parameters_none() == FAILURE) {
  229. return;
  230. }
  231. zend_create_internal_iterator_zval(return_value, ZEND_THIS);
  232. }
  233. #endif