processinginstruction.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. /* {{{ arginfo */
  27. ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_processinginstruction_construct, 0, 0, 1)
  28. ZEND_ARG_INFO(0, name)
  29. ZEND_ARG_INFO(0, value)
  30. ZEND_END_ARG_INFO();
  31. /* }}} */
  32. /*
  33. * class DOMProcessingInstruction extends DOMNode
  34. *
  35. * URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#ID-1004215813
  36. * Since:
  37. */
  38. const zend_function_entry php_dom_processinginstruction_class_functions[] = {
  39. PHP_ME(domprocessinginstruction, __construct, arginfo_dom_processinginstruction_construct, ZEND_ACC_PUBLIC)
  40. PHP_FE_END
  41. };
  42. /* {{{ proto void DOMProcessingInstruction::__construct(string name, [string value]); */
  43. PHP_METHOD(domprocessinginstruction, __construct)
  44. {
  45. zval *id;
  46. xmlNodePtr nodep = NULL, oldnode = NULL;
  47. dom_object *intern;
  48. char *name, *value = NULL;
  49. int name_len, value_len, name_valid;
  50. zend_error_handling error_handling;
  51. zend_replace_error_handling(EH_THROW, dom_domexception_class_entry, &error_handling TSRMLS_CC);
  52. if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Os|s", &id, dom_processinginstruction_class_entry, &name, &name_len, &value, &value_len) == FAILURE) {
  53. zend_restore_error_handling(&error_handling TSRMLS_CC);
  54. return;
  55. }
  56. zend_restore_error_handling(&error_handling TSRMLS_CC);
  57. name_valid = xmlValidateName((xmlChar *) name, 0);
  58. if (name_valid != 0) {
  59. php_dom_throw_error(INVALID_CHARACTER_ERR, 1 TSRMLS_CC);
  60. RETURN_FALSE;
  61. }
  62. nodep = xmlNewPI((xmlChar *) name, (xmlChar *) value);
  63. if (!nodep) {
  64. php_dom_throw_error(INVALID_STATE_ERR, 1 TSRMLS_CC);
  65. RETURN_FALSE;
  66. }
  67. intern = (dom_object *)zend_object_store_get_object(id TSRMLS_CC);
  68. if (intern != NULL) {
  69. oldnode = dom_object_get_node(intern);
  70. if (oldnode != NULL) {
  71. php_libxml_node_free_resource(oldnode TSRMLS_CC);
  72. }
  73. php_libxml_increment_node_ptr((php_libxml_node_object *)intern, nodep, (void *)intern TSRMLS_CC);
  74. }
  75. }
  76. /* }}} end DOMProcessingInstruction::__construct */
  77. /* {{{ target string
  78. readonly=yes
  79. URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#ID-1478689192
  80. Since:
  81. */
  82. int dom_processinginstruction_target_read(dom_object *obj, zval **retval TSRMLS_DC)
  83. {
  84. xmlNodePtr nodep;
  85. nodep = dom_object_get_node(obj);
  86. if (nodep == NULL) {
  87. php_dom_throw_error(INVALID_STATE_ERR, 0 TSRMLS_CC);
  88. return FAILURE;
  89. }
  90. ALLOC_ZVAL(*retval);
  91. ZVAL_STRING(*retval, (char *) (nodep->name), 1);
  92. return SUCCESS;
  93. }
  94. /* }}} */
  95. /* {{{ data string
  96. readonly=no
  97. URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#ID-837822393
  98. Since:
  99. */
  100. int dom_processinginstruction_data_read(dom_object *obj, zval **retval TSRMLS_DC)
  101. {
  102. xmlNodePtr nodep;
  103. xmlChar *content;
  104. nodep = dom_object_get_node(obj);
  105. if (nodep == NULL) {
  106. php_dom_throw_error(INVALID_STATE_ERR, 0 TSRMLS_CC);
  107. return FAILURE;
  108. }
  109. ALLOC_ZVAL(*retval);
  110. if ((content = xmlNodeGetContent(nodep)) != NULL) {
  111. ZVAL_STRING(*retval, content, 1);
  112. xmlFree(content);
  113. } else {
  114. ZVAL_EMPTY_STRING(*retval);
  115. }
  116. return SUCCESS;
  117. }
  118. int dom_processinginstruction_data_write(dom_object *obj, zval *newval TSRMLS_DC)
  119. {
  120. zval value_copy;
  121. xmlNode *nodep;
  122. nodep = dom_object_get_node(obj);
  123. if (nodep == NULL) {
  124. php_dom_throw_error(INVALID_STATE_ERR, 0 TSRMLS_CC);
  125. return FAILURE;
  126. }
  127. if (newval->type != IS_STRING) {
  128. if(Z_REFCOUNT_P(newval) > 1) {
  129. value_copy = *newval;
  130. zval_copy_ctor(&value_copy);
  131. newval = &value_copy;
  132. }
  133. convert_to_string(newval);
  134. }
  135. xmlNodeSetContentLen(nodep, Z_STRVAL_P(newval), Z_STRLEN_P(newval) + 1);
  136. if (newval == &value_copy) {
  137. zval_dtor(newval);
  138. }
  139. return SUCCESS;
  140. }
  141. /* }}} */
  142. #endif
  143. /*
  144. * Local variables:
  145. * tab-width: 4
  146. * c-basic-offset: 4
  147. * End:
  148. * vim600: noet sw=4 ts=4 fdm=marker
  149. * vim<600: noet sw=4 ts=4
  150. */