Back to index

php5  5.3.10
characterdata.c
Go to the documentation of this file.
00001 /*
00002    +----------------------------------------------------------------------+
00003    | PHP Version 5                                                        |
00004    +----------------------------------------------------------------------+
00005    | Copyright (c) 1997-2012 The PHP Group                                |
00006    +----------------------------------------------------------------------+
00007    | This source file is subject to version 3.01 of the PHP license,      |
00008    | that is bundled with this package in the file LICENSE, and is        |
00009    | available through the world-wide-web at the following url:           |
00010    | http://www.php.net/license/3_01.txt                                  |
00011    | If you did not receive a copy of the PHP license and are unable to   |
00012    | obtain it through the world-wide-web, please send a note to          |
00013    | license@php.net so we can mail you a copy immediately.               |
00014    +----------------------------------------------------------------------+
00015    | Authors: Christian Stocker <chregu@php.net>                          |
00016    |          Rob Richards <rrichards@php.net>                            |
00017    +----------------------------------------------------------------------+
00018 */
00019 
00020 /* $Id: characterdata.c 321634 2012-01-01 13:15:04Z felipe $ */
00021 
00022 #ifdef HAVE_CONFIG_H
00023 #include "config.h"
00024 #endif
00025 
00026 #include "php.h"
00027 #if HAVE_LIBXML && HAVE_DOM
00028 #include "php_dom.h"
00029 
00030 
00031 /* {{{ arginfo */
00032 ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_characterdata_substring_data, 0, 0, 2)
00033        ZEND_ARG_INFO(0, offset)
00034        ZEND_ARG_INFO(0, count)
00035 ZEND_END_ARG_INFO();
00036 
00037 ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_characterdata_append_data, 0, 0, 1)
00038        ZEND_ARG_INFO(0, arg)
00039 ZEND_END_ARG_INFO();
00040 
00041 ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_characterdata_insert_data, 0, 0, 2)
00042        ZEND_ARG_INFO(0, offset)
00043        ZEND_ARG_INFO(0, arg)
00044 ZEND_END_ARG_INFO();
00045 
00046 ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_characterdata_delete_data, 0, 0, 2)
00047        ZEND_ARG_INFO(0, offset)
00048        ZEND_ARG_INFO(0, count)
00049 ZEND_END_ARG_INFO();
00050 
00051 ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_characterdata_replace_data, 0, 0, 3)
00052        ZEND_ARG_INFO(0, offset)
00053        ZEND_ARG_INFO(0, count)
00054        ZEND_ARG_INFO(0, arg)
00055 ZEND_END_ARG_INFO();
00056 /* }}} */
00057 
00058 /*
00059 * class DOMCharacterData extends DOMNode 
00060 *
00061 * URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-FF21A306
00062 * Since: 
00063 */
00064 
00065 const zend_function_entry php_dom_characterdata_class_functions[] = {
00066        PHP_FALIAS(substringData, dom_characterdata_substring_data, arginfo_dom_characterdata_substring_data)
00067        PHP_FALIAS(appendData, dom_characterdata_append_data, arginfo_dom_characterdata_append_data)
00068        PHP_FALIAS(insertData, dom_characterdata_insert_data, arginfo_dom_characterdata_insert_data)
00069        PHP_FALIAS(deleteData, dom_characterdata_delete_data, arginfo_dom_characterdata_delete_data)
00070        PHP_FALIAS(replaceData, dom_characterdata_replace_data, arginfo_dom_characterdata_replace_data)
00071        PHP_FE_END
00072 };
00073 
00074 /* {{{ data   string 
00075 readonly=no 
00076 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-72AB8359
00077 Since: 
00078 */
00079 int dom_characterdata_data_read(dom_object *obj, zval **retval TSRMLS_DC)
00080 {
00081        xmlNodePtr nodep;
00082        xmlChar *content;
00083 
00084        nodep = dom_object_get_node(obj);
00085 
00086        if (nodep == NULL) {
00087               php_dom_throw_error(INVALID_STATE_ERR, 0 TSRMLS_CC);
00088               return FAILURE;
00089        }
00090 
00091        ALLOC_ZVAL(*retval);
00092        
00093        if ((content = xmlNodeGetContent(nodep)) != NULL) {
00094               ZVAL_STRING(*retval, content, 1);
00095               xmlFree(content);
00096        } else {
00097               ZVAL_EMPTY_STRING(*retval);
00098        }
00099 
00100        return SUCCESS;
00101 }
00102 
00103 int dom_characterdata_data_write(dom_object *obj, zval *newval TSRMLS_DC)
00104 {
00105        zval value_copy;
00106        xmlNode *nodep;
00107 
00108        nodep = dom_object_get_node(obj);
00109 
00110        if (nodep == NULL) {
00111               php_dom_throw_error(INVALID_STATE_ERR, 0 TSRMLS_CC);
00112               return FAILURE;
00113        }
00114 
00115        if (newval->type != IS_STRING) {
00116               if(Z_REFCOUNT_P(newval) > 1) {
00117                      value_copy = *newval;
00118                      zval_copy_ctor(&value_copy);
00119                      newval = &value_copy;
00120               }
00121               convert_to_string(newval);
00122        }
00123 
00124        xmlNodeSetContentLen(nodep, Z_STRVAL_P(newval), Z_STRLEN_P(newval) + 1);
00125 
00126        if (newval == &value_copy) {
00127               zval_dtor(newval);
00128        }
00129 
00130        return SUCCESS;
00131 }
00132 
00133 /* }}} */
00134 
00135 /* {{{ length long   
00136 readonly=yes 
00137 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-7D61178C
00138 Since: 
00139 */
00140 int dom_characterdata_length_read(dom_object *obj, zval **retval TSRMLS_DC)
00141 {
00142        xmlNodePtr nodep;
00143        xmlChar *content;
00144        long length = 0;
00145 
00146        nodep = dom_object_get_node(obj);
00147 
00148        if (nodep == NULL) {
00149               php_dom_throw_error(INVALID_STATE_ERR, 0 TSRMLS_CC);
00150               return FAILURE;
00151        }
00152 
00153        ALLOC_ZVAL(*retval);
00154        
00155        content = xmlNodeGetContent(nodep);
00156 
00157        if (content) {
00158               length = xmlUTF8Strlen(content);
00159               xmlFree(content);
00160        }
00161 
00162        ZVAL_LONG(*retval, length);
00163 
00164        return SUCCESS;
00165 }
00166 
00167 /* }}} */
00168 
00169 /* {{{ proto string dom_characterdata_substring_data(int offset, int count);
00170 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-6531BCCF
00171 Since: 
00172 */
00173 PHP_FUNCTION(dom_characterdata_substring_data)
00174 {
00175        zval       *id;
00176        xmlChar    *cur;
00177        xmlChar    *substring;
00178        xmlNodePtr  node;
00179        long        offset, count;
00180        int         length;
00181        dom_object    *intern;
00182 
00183        if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Oll", &id, dom_characterdata_class_entry, &offset, &count) == FAILURE) {
00184               return;
00185        }
00186 
00187        DOM_GET_OBJ(node, id, xmlNodePtr, intern);
00188 
00189        cur = xmlNodeGetContent(node);
00190        if (cur == NULL) {
00191               RETURN_FALSE;
00192        }
00193 
00194        length = xmlUTF8Strlen(cur);
00195 
00196        if (offset < 0 || count < 0 || offset > length) {
00197               xmlFree(cur);
00198               php_dom_throw_error(INDEX_SIZE_ERR, dom_get_strict_error(intern->document) TSRMLS_CC);
00199               RETURN_FALSE;
00200        }
00201 
00202        if ((offset + count) > length) {
00203               count = length - offset;
00204        }
00205 
00206        substring = xmlUTF8Strsub(cur, offset, count);
00207        xmlFree(cur);
00208 
00209        if (substring) {
00210               RETVAL_STRING(substring, 1);
00211               xmlFree(substring);
00212        } else {
00213               RETVAL_EMPTY_STRING();
00214        }
00215 }
00216 /* }}} end dom_characterdata_substring_data */
00217 
00218 /* {{{ proto void dom_characterdata_append_data(string arg);
00219 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-32791A2F
00220 Since: 
00221 */
00222 PHP_FUNCTION(dom_characterdata_append_data)
00223 {
00224        zval *id;
00225        xmlNode *nodep;
00226        dom_object *intern;
00227        char *arg;
00228        int arg_len;
00229 
00230        if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Os", &id, dom_characterdata_class_entry, &arg, &arg_len) == FAILURE) {
00231               return;
00232        }
00233 
00234        DOM_GET_OBJ(nodep, id, xmlNodePtr, intern);
00235 #if LIBXML_VERSION < 20627
00236 /* Implement logic from libxml xmlTextConcat to add suport for comments and PI */
00237     if ((nodep->content == (xmlChar *) &(nodep->properties)) ||
00238         ((nodep->doc != NULL) && (nodep->doc->dict != NULL) &&
00239               xmlDictOwns(nodep->doc->dict, nodep->content))) {
00240        nodep->content = xmlStrncatNew(nodep->content, arg, arg_len);
00241     } else {
00242         nodep->content = xmlStrncat(nodep->content, arg, arg_len);
00243     }
00244     nodep->properties = NULL;
00245 #else
00246        xmlTextConcat(nodep, arg, arg_len);
00247 #endif
00248        RETURN_TRUE;
00249 }
00250 /* }}} end dom_characterdata_append_data */
00251 
00252 /* {{{ proto void dom_characterdata_insert_data(int offset, string arg);
00253 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-3EDB695F
00254 Since: 
00255 */
00256 PHP_FUNCTION(dom_characterdata_insert_data)
00257 {
00258        zval *id;
00259        xmlChar              *cur, *first, *second;
00260        xmlNodePtr  node;
00261        char          *arg;
00262        long        offset;
00263        int         length, arg_len;
00264        dom_object    *intern;
00265 
00266        if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Ols", &id, dom_characterdata_class_entry, &offset, &arg, &arg_len) == FAILURE) {
00267               return;
00268        }
00269 
00270        DOM_GET_OBJ(node, id, xmlNodePtr, intern);
00271 
00272        cur = xmlNodeGetContent(node);
00273        if (cur == NULL) {
00274               RETURN_FALSE;
00275        }
00276 
00277        length = xmlUTF8Strlen(cur);
00278 
00279        if (offset < 0 || offset > length) {
00280               xmlFree(cur);
00281               php_dom_throw_error(INDEX_SIZE_ERR, dom_get_strict_error(intern->document) TSRMLS_CC);
00282               RETURN_FALSE;
00283        }
00284 
00285        first = xmlUTF8Strndup(cur, offset);
00286        second = xmlUTF8Strsub(cur, offset, length - offset);
00287        xmlFree(cur);
00288 
00289        xmlNodeSetContent(node, first);
00290        xmlNodeAddContent(node, arg);
00291        xmlNodeAddContent(node, second);
00292        
00293        xmlFree(first);
00294        xmlFree(second);
00295 
00296        RETURN_TRUE;
00297 }
00298 /* }}} end dom_characterdata_insert_data */
00299 
00300 /* {{{ proto void dom_characterdata_delete_data(int offset, int count);
00301 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-7C603781
00302 Since: 
00303 */
00304 PHP_FUNCTION(dom_characterdata_delete_data)
00305 {
00306        zval *id;
00307        xmlChar    *cur, *substring, *second;
00308        xmlNodePtr  node;
00309        long        offset, count;
00310        int         length;
00311        dom_object    *intern;
00312 
00313        if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Oll", &id, dom_characterdata_class_entry, &offset, &count) == FAILURE) {
00314               return;
00315        }
00316 
00317        DOM_GET_OBJ(node, id, xmlNodePtr, intern);
00318 
00319        cur = xmlNodeGetContent(node);
00320        if (cur == NULL) {
00321               RETURN_FALSE;
00322        }
00323 
00324        length = xmlUTF8Strlen(cur);
00325 
00326        if (offset < 0 || count < 0 || offset > length) {
00327               xmlFree(cur);
00328               php_dom_throw_error(INDEX_SIZE_ERR, dom_get_strict_error(intern->document) TSRMLS_CC);
00329               RETURN_FALSE;
00330        }
00331 
00332        if (offset > 0) {
00333               substring = xmlUTF8Strsub(cur, 0, offset);
00334        } else {
00335               substring = NULL;
00336        }
00337 
00338        if ((offset + count) > length) {
00339               count = length - offset;
00340        }
00341 
00342        second = xmlUTF8Strsub(cur, offset + count, length - offset);
00343        substring = xmlStrcat(substring, second);
00344 
00345        xmlNodeSetContent(node, substring);
00346 
00347        xmlFree(cur);
00348        xmlFree(second);
00349        xmlFree(substring);
00350 
00351        RETURN_TRUE;
00352 }
00353 /* }}} end dom_characterdata_delete_data */
00354 
00355 /* {{{ proto void dom_characterdata_replace_data(int offset, int count, string arg);
00356 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-E5CBA7FB
00357 Since: 
00358 */
00359 PHP_FUNCTION(dom_characterdata_replace_data)
00360 {
00361        zval *id;
00362        xmlChar              *cur, *substring, *second = NULL;
00363        xmlNodePtr  node;
00364        char          *arg;
00365        long        offset, count;
00366        int         length, arg_len;
00367        dom_object    *intern;
00368 
00369        if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Olls", &id, dom_characterdata_class_entry, &offset, &count, &arg, &arg_len) == FAILURE) {
00370               return;
00371        }
00372 
00373        DOM_GET_OBJ(node, id, xmlNodePtr, intern);
00374 
00375        cur = xmlNodeGetContent(node);
00376        if (cur == NULL) {
00377               RETURN_FALSE;
00378        }
00379 
00380        length = xmlUTF8Strlen(cur);
00381 
00382        if (offset < 0 || count < 0 || offset > length) {
00383               xmlFree(cur);
00384               php_dom_throw_error(INDEX_SIZE_ERR, dom_get_strict_error(intern->document) TSRMLS_CC);
00385               RETURN_FALSE;
00386        }
00387 
00388        if (offset > 0) {
00389               substring = xmlUTF8Strsub(cur, 0, offset);
00390        } else {
00391               substring = NULL;
00392        }
00393 
00394        if ((offset + count) > length) {
00395               count = length - offset;
00396        }
00397 
00398        if (offset < length) {
00399               second = xmlUTF8Strsub(cur, offset + count, length - offset);
00400        }
00401 
00402        substring = xmlStrcat(substring, arg);
00403        substring = xmlStrcat(substring, second);
00404 
00405        xmlNodeSetContent(node, substring);
00406 
00407        xmlFree(cur);
00408        if (second) {
00409               xmlFree(second);
00410        }
00411        xmlFree(substring);
00412 
00413        RETURN_TRUE;
00414 }
00415 /* }}} end dom_characterdata_replace_data */
00416 
00417 #endif
00418 
00419 /*
00420  * Local variables:
00421  * tab-width: 4
00422  * c-basic-offset: 4
00423  * End:
00424  * vim600: noet sw=4 ts=4 fdm=marker
00425  * vim<600: noet sw=4 ts=4
00426  */