Back to index

php5  5.3.10
attr.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: attr.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 
00028 #if HAVE_LIBXML && HAVE_DOM
00029 
00030 #include "php_dom.h"
00031 
00032 /* {{{ arginfo */
00033 ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_attr_is_id, 0, 0, 0)
00034 ZEND_END_ARG_INFO();
00035 
00036 ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_attr_construct, 0, 0, 1)
00037        ZEND_ARG_INFO(0, name)
00038        ZEND_ARG_INFO(0, value)
00039 ZEND_END_ARG_INFO();
00040 /* }}} */
00041 
00042 /*
00043 * class DOMAttr extends DOMNode 
00044 *
00045 * URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#ID-637646024
00046 * Since: 
00047 */
00048 
00049 const zend_function_entry php_dom_attr_class_functions[] = {
00050        PHP_FALIAS(isId, dom_attr_is_id, arginfo_dom_attr_is_id)
00051        PHP_ME(domattr, __construct, arginfo_dom_attr_construct, ZEND_ACC_PUBLIC)
00052        PHP_FE_END
00053 };
00054 
00055 /* {{{ proto void DOMAttr::__construct(string name, [string value]); */
00056 PHP_METHOD(domattr, __construct)
00057 {
00058 
00059        zval *id;
00060        xmlAttrPtr nodep = NULL;
00061        xmlNodePtr oldnode = NULL;
00062        dom_object *intern;
00063        char *name, *value = NULL;
00064        int name_len, value_len, name_valid;
00065        zend_error_handling error_handling;
00066 
00067        zend_replace_error_handling(EH_THROW, dom_domexception_class_entry, &error_handling TSRMLS_CC);
00068        if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Os|s", &id, dom_attr_class_entry, &name, &name_len, &value, &value_len) == FAILURE) {
00069               zend_restore_error_handling(&error_handling TSRMLS_CC);
00070               return;
00071        }
00072 
00073        zend_restore_error_handling(&error_handling TSRMLS_CC);
00074        intern = (dom_object *)zend_object_store_get_object(id TSRMLS_CC);
00075 
00076        name_valid = xmlValidateName((xmlChar *) name, 0);
00077        if (name_valid != 0) {
00078               php_dom_throw_error(INVALID_CHARACTER_ERR, 1 TSRMLS_CC);
00079               RETURN_FALSE;
00080        }
00081 
00082        nodep = xmlNewProp(NULL, (xmlChar *) name, value);
00083 
00084        if (!nodep) {
00085               php_dom_throw_error(INVALID_STATE_ERR, 1 TSRMLS_CC);
00086               RETURN_FALSE;
00087        }
00088 
00089        if (intern != NULL) {
00090               oldnode = dom_object_get_node(intern);
00091               if (oldnode != NULL) {
00092                      php_libxml_node_free_resource(oldnode  TSRMLS_CC);
00093               }
00094               php_libxml_increment_node_ptr((php_libxml_node_object *)intern, (xmlNodePtr)nodep, (void *)intern TSRMLS_CC);
00095        }
00096 }
00097 
00098 /* }}} end DOMAttr::__construct */
00099 
00100 /* {{{ name   string 
00101 readonly=yes 
00102 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#ID-1112119403
00103 Since: 
00104 */
00105 int dom_attr_name_read(dom_object *obj, zval **retval TSRMLS_DC)
00106 {
00107        xmlAttrPtr attrp;
00108 
00109        attrp = (xmlAttrPtr) dom_object_get_node(obj);
00110 
00111        if (attrp == NULL) {
00112               php_dom_throw_error(INVALID_STATE_ERR, 0 TSRMLS_CC);
00113               return FAILURE;
00114        }
00115 
00116        ALLOC_ZVAL(*retval);
00117        ZVAL_STRING(*retval, (char *) (attrp->name), 1);
00118 
00119        return SUCCESS;
00120 }
00121 
00122 /* }}} */
00123 
00124 /* {{{ specified     boolean       
00125 readonly=yes 
00126 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#ID-862529273
00127 Since: 
00128 */
00129 int dom_attr_specified_read(dom_object *obj, zval **retval TSRMLS_DC)
00130 {
00131        /* TODO */
00132        ALLOC_ZVAL(*retval);
00133        ZVAL_TRUE(*retval);
00134        return SUCCESS;
00135 }
00136 
00137 /* }}} */
00138 
00139 /* {{{ value  string 
00140 readonly=no 
00141 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#ID-221662474
00142 Since: 
00143 */
00144 int dom_attr_value_read(dom_object *obj, zval **retval TSRMLS_DC)
00145 {
00146        xmlAttrPtr attrp;
00147        xmlChar *content;
00148 
00149        attrp = (xmlAttrPtr) dom_object_get_node(obj);
00150 
00151        if (attrp == NULL) {
00152               php_dom_throw_error(INVALID_STATE_ERR, 0 TSRMLS_CC);
00153               return FAILURE;
00154        }
00155 
00156        ALLOC_ZVAL(*retval);
00157 
00158        
00159        if ((content = xmlNodeGetContent((xmlNodePtr) attrp)) != NULL) {
00160               ZVAL_STRING(*retval, content, 1);
00161               xmlFree(content);
00162        } else {
00163               ZVAL_EMPTY_STRING(*retval);
00164        }
00165 
00166        return SUCCESS;
00167 
00168 }
00169 
00170 int dom_attr_value_write(dom_object *obj, zval *newval TSRMLS_DC)
00171 {
00172        zval value_copy;
00173        xmlAttrPtr attrp;
00174 
00175        attrp = (xmlAttrPtr) dom_object_get_node(obj);
00176 
00177        if (attrp == NULL) {
00178               php_dom_throw_error(INVALID_STATE_ERR, 0 TSRMLS_CC);
00179               return FAILURE;
00180        }
00181 
00182        if (attrp->children) {
00183               node_list_unlink(attrp->children TSRMLS_CC);
00184        }
00185 
00186        if (newval->type != IS_STRING) {
00187               if(Z_REFCOUNT_P(newval) > 1) {
00188                      value_copy = *newval;
00189                      zval_copy_ctor(&value_copy);
00190                      newval = &value_copy;
00191               }
00192               convert_to_string(newval);
00193        }
00194 
00195        xmlNodeSetContentLen((xmlNodePtr) attrp, Z_STRVAL_P(newval), Z_STRLEN_P(newval) + 1);
00196 
00197        if (newval == &value_copy) {
00198               zval_dtor(newval);
00199        }
00200 
00201        return SUCCESS;
00202 }
00203 
00204 /* }}} */
00205 
00206 /* {{{ ownerElement  DOMElement    
00207 readonly=yes 
00208 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#Attr-ownerElement
00209 Since: DOM Level 2
00210 */
00211 int dom_attr_owner_element_read(dom_object *obj, zval **retval TSRMLS_DC)
00212 {
00213        xmlNodePtr nodep, nodeparent;
00214        int ret;
00215 
00216        nodep = dom_object_get_node(obj);
00217 
00218        if (nodep == NULL) {
00219               php_dom_throw_error(INVALID_STATE_ERR, 0 TSRMLS_CC);
00220               return FAILURE;
00221        }
00222 
00223        ALLOC_ZVAL(*retval);
00224 
00225        nodeparent = nodep->parent;
00226        if (!nodeparent) {
00227               ZVAL_NULL(*retval);
00228               return SUCCESS;
00229        }
00230 
00231        if (NULL == (*retval = php_dom_create_object(nodeparent, &ret, NULL, *retval, obj TSRMLS_CC))) {
00232               php_error_docref(NULL TSRMLS_CC, E_WARNING,  "Cannot create required DOM object");
00233               return FAILURE;
00234        }
00235        return SUCCESS;
00236 
00237 }
00238 
00239 /* }}} */
00240 
00241 /* {{{ schemaTypeInfo       DOMTypeInfo   
00242 readonly=yes 
00243 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#Attr-schemaTypeInfo
00244 Since: DOM Level 3
00245 */
00246 int dom_attr_schema_type_info_read(dom_object *obj, zval **retval TSRMLS_DC)
00247 {
00248        php_error_docref(NULL TSRMLS_CC, E_WARNING, "Not yet implemented");
00249        ALLOC_ZVAL(*retval);
00250        ZVAL_NULL(*retval);
00251        return SUCCESS;
00252 }
00253 
00254 /* }}} */
00255 
00256 /* {{{ proto boolean dom_attr_is_id();
00257 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#Attr-isId
00258 Since: DOM Level 3
00259 */
00260 PHP_FUNCTION(dom_attr_is_id)
00261 {
00262        zval *id;
00263        dom_object *intern;
00264        xmlAttrPtr attrp;
00265 
00266        if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &id, dom_attr_class_entry) == FAILURE) {
00267               return;
00268        }
00269 
00270        DOM_GET_OBJ(attrp, id, xmlAttrPtr, intern);
00271 
00272        if (attrp->atype == XML_ATTRIBUTE_ID) {
00273               RETURN_TRUE;
00274        } else {
00275               RETURN_FALSE;
00276        }
00277 }
00278 /* }}} end dom_attr_is_id */
00279 
00280 #endif
00281 
00282 /*
00283  * Local variables:
00284  * tab-width: 4
00285  * c-basic-offset: 4
00286  * End:
00287  * vim600: noet sw=4 ts=4 fdm=marker
00288  * vim<600: noet sw=4 ts=4
00289  */