cvs: php4 /ext/domxml php_domxml.c php_domxml.h
| From: | Christian Stocker | Date: | Mon, 11 Mar 2002 22:37:33 +0000 |
| Subject: | cvs: php4 /ext/domxml php_domxml.c php_domxml.h | ||
| Groups: | php.cvs | ||
| Request: | Send a blank email to php-cvs+get-9915@lists.php.net to get a copy of this message | ||
chregu Mon Mar 11 17:37:33 2002 EDT
Modified files:
/php4/ext/domxml php_domxml.c php_domxml.h
Log:
@ implemented domxml_elem_get_elements_by_tagname
@ new function domxml_doc_get_elements_by_tagname
@ new function domxml_doc_get_element_by_id (chregu)
Index: php4/ext/domxml/php_domxml.c diff -u php4/ext/domxml/php_domxml.c:1.123 php4/ext/domxml/php_domxml.c:1.124 --- php4/ext/domxml/php_domxml.c:1.123 Fri Mar 8 17:16:07 2002 +++ php4/ext/domxml/php_domxml.c Mon Mar 11 17:37:32 2002 @@ -16,7 +16,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: php_domxml.c,v 1.123 2002/03/08 22:16:07 chregu Exp $ */ +/* $Id: php_domxml.c,v 1.124 2002/03/11 22:37:32 chregu Exp $ */ /* TODO * - Support Notation Nodes @@ -213,6 +213,8 @@ PHP_FE(xpath_eval, NULL) PHP_FE(xpath_eval_expression, NULL) PHP_FE(xpath_register_ns, NULL) + PHP_FE(domxml_doc_get_elements_by_tagname, NULL) + PHP_FE(domxml_doc_get_element_by_id, NULL) #endif #if defined(LIBXML_XPTR_ENABLED) PHP_FE(xptr_new_context, NULL) @@ -272,6 +274,8 @@ PHP_FALIAS(xpath_init, xpath_init, NULL) PHP_FALIAS(xpath_new_context, xpath_new_context, NULL) PHP_FALIAS(xptr_new_context, xptr_new_context, NULL) + PHP_FALIAS(get_elements_by_tagname, domxml_doc_get_elements_by_tagname, NULL) + PHP_FALIAS(get_element_by_id, domxml_doc_get_element_by_id, NULL) #endif {NULL, NULL, NULL} }; @@ -336,7 +340,7 @@ PHP_FALIAS(remove_attribute, domxml_elem_remove_attribute, NULL) PHP_FALIAS(get_attribute_node, domxml_elem_get_attribute_node, NULL) PHP_FALIAS(set_attribute_node, domxml_elem_set_attribute_node, NULL) - PHP_FALIAS(get_element_by_tagname, domxml_elem_get_element_by_tagname, NULL) + PHP_FALIAS(get_elements_by_tagname, domxml_elem_get_elements_by_tagname, NULL) {NULL, NULL, NULL} }; @@ -500,6 +504,27 @@ } } +static xmlNodeSetPtr php_get_elements_by_tagname(xmlNodePtr n, xmlChar* name) +{ + xmlNodeSetPtr rv = NULL; + xmlNodePtr cld = NULL; + + if ( n != NULL && name != NULL ) { + cld = n->children; + while ( cld != NULL ) { + if ( xmlStrcmp( name, cld->name ) == 0 ){ + if ( rv == NULL ) { + rv = xmlXPathNodeSetCreate( cld ) ; + } + else { + xmlXPathNodeSetAdd( rv, cld ); + } + } + cld = cld->next; + } + } + return rv; +} static void php_free_xml_doc(zend_rsrc_list_entry *rsrc TSRMLS_DC) { @@ -2259,25 +2284,205 @@ } /* }}} */ -/* {{{ proto string domxml_elem_get_element_by_tagname(string tagname) - Returns element for given attribute */ -PHP_FUNCTION(domxml_elem_get_element_by_tagname) +#if defined(LIBXML_XPATH_ENABLED) +/* {{{ proto string domxml_doc_get_elements_by_tagname(string tagname [,object xpathctx_handle] ) + Returns array with nodes with given tagname in document or empty array, if not found*/ +PHP_FUNCTION(domxml_doc_get_elements_by_tagname) +{ + zval *id, *rv, *contextnode = NULL,*ctxpin = NULL; + xmlXPathContextPtr ctxp; + xmlDocPtr docp; + + xmlXPathObjectPtr xpathobjp; + xmlNode *contextnodep; + int name_len; + char *str,*name; + + contextnode = NULL; + contextnodep = NULL; + + DOMXML_PARAM_FOUR(docp, id, le_domxmldocp, "s|oo", &name, &name_len,&ctxpin,&contextnodep); + + /* if no xpath_context was submitted, create a new one */ + if (ctxpin == NULL) + { + ctxp = xmlXPathNewContext(docp); + } + else + { + DOMXML_GET_OBJ(ctxp, ctxpin, le_xpathctxp); + } + + if (contextnode) { + DOMXML_GET_OBJ(contextnodep, contextnode, le_domxmlnodep); + } + ctxp->node = contextnodep; + str = (char*) emalloc((name_len+3) * sizeof(char)) ; + if (str == NULL) + { + php_error(E_WARNING, "%s(): cannot allocate memory for string", get_active_function_name(TSRMLS_C)); + } + sprintf(str ,"//%s",name); + + xpathobjp = xmlXPathEval(str, ctxp); + efree(str); + ctxp->node = NULL; + if (!xpathobjp) { + RETURN_FALSE; + } + MAKE_STD_ZVAL(rv); + + if(array_init(rv) != SUCCESS) + { + php_error(E_WARNING, "%s(): cannot create required array", get_active_function_name(TSRMLS_C)); + RETURN_FALSE; + } + + switch (Z_TYPE_P(xpathobjp)) { + + case XPATH_NODESET: + { + int i; + xmlNodeSetPtr nodesetp; + + if (NULL == (nodesetp = xpathobjp->nodesetval)) { + zval_dtor(rv); + RETURN_FALSE; + } + + for (i = 0; i < nodesetp->nodeNr; i++) { + xmlNodePtr node = nodesetp->nodeTab[i]; + zval *child; + int retnode; + + /* construct a node object */ + child = php_domobject_new(node, &retnode TSRMLS_CC); + zend_hash_next_index_insert(Z_ARRVAL_P(rv), &child, sizeof(zval *), NULL); + } + + break; + } + default: + break; + } + + *return_value = *rv; + FREE_ZVAL(rv); +} +/* }}} */ + +/* {{{ proto string domxml_doc_get_element_by_id(string id [,object xpathctx_handle] ) + Returns element for given id or false if not found */ +PHP_FUNCTION(domxml_doc_get_element_by_id) { - zval *id, *arg1; + zval *id, *rv, *contextnode = NULL, *ctxpin = NULL; + xmlXPathContextPtr ctxp ; + xmlDocPtr docp; + + xmlXPathObjectPtr xpathobjp; + xmlNode *contextnodep; + int name_len; + char *str,*name; + + contextnode = NULL; + contextnodep = NULL; + + DOMXML_PARAM_FOUR(docp, id, le_domxmldocp, "s|oo", &name, &name_len,&ctxpin,&contextnodep); + + /* if no xpath_context was submitted, create a new one */ + if (ctxpin == NULL) + { + ctxp = xmlXPathNewContext(docp); + } + else + { + DOMXML_GET_OBJ(ctxp, ctxpin, le_xpathctxp); + } + + if (contextnode) { + DOMXML_GET_OBJ(contextnodep, contextnode, le_domxmlnodep); + } + ctxp->node = contextnodep; + str = (char*) emalloc((name_len+14) * sizeof(char)) ; + if (str == NULL) + { + php_error(E_WARNING, "%s(): cannot allocate memory for string", get_active_function_name(TSRMLS_C)); + } + sprintf(str ,"//*[@ID = '%s']",name); + xpathobjp = xmlXPathEval(str, ctxp); + efree(str); + ctxp->node = NULL; + if (!xpathobjp) { + RETURN_FALSE; + } + + switch (Z_TYPE_P(xpathobjp)) { + + case XPATH_NODESET: + { + xmlNodeSetPtr nodesetp; + + if (NULL == (nodesetp = xpathobjp->nodesetval)) { + zval_dtor(rv); + RETURN_FALSE; + } + + if (nodesetp->nodeNr > 0) { + xmlNodePtr node = nodesetp->nodeTab[0]; + int retnode; + rv = php_domobject_new(node, &retnode TSRMLS_CC); + } + else { + /*return false array, if no nodes were found */ + RETURN_FALSE; + } + + break; + } + default: + break; + } + + *return_value = *rv; + FREE_ZVAL(rv); +} +/* }}} */ +#endif +/* {{{ proto string domxml_elem_get_elements_by_tagname(string tagname) + Returns array with nodes with given tagname in element or empty array, if not found */ +PHP_FUNCTION(domxml_elem_get_elements_by_tagname) +{ + zval *id,*rv; xmlNode *nodep; + int name_len,i; + char *name; + xmlNodeSet *nodesetp; - DOMXML_NOT_IMPLEMENTED(); + DOMXML_PARAM_TWO(nodep, id, le_domxmlelementp, "s", &name, &name_len); - if ((ZEND_NUM_ARGS() == 1) && getParameters(ht, 1, &arg1) == SUCCESS) { - id = getThis(); - nodep = php_dom_get_object(id, le_domxmlelementp, 0 TSRMLS_CC); - } else { - WRONG_PARAM_COUNT; + MAKE_STD_ZVAL(rv); + + if(array_init(rv) != SUCCESS) + { + php_error(E_WARNING, "%s(): cannot create required array", get_active_function_name(TSRMLS_C)); + RETURN_FALSE; } - convert_to_string(arg1); + nodesetp = php_get_elements_by_tagname(nodep,name); - /* FIXME: not implemented */ + if(nodesetp) { + + for (i = 0; i < nodesetp->nodeNr; i++) { + xmlNodePtr node = nodesetp->nodeTab[i]; + zval *child; + int retnode; + + child = php_domobject_new(node, &retnode TSRMLS_CC); + zend_hash_next_index_insert(Z_ARRVAL_P(rv), &child, sizeof(zval *), NULL); + } + } + *return_value = *rv; + FREE_ZVAL(rv); } /* }}} */ Index: php4/ext/domxml/php_domxml.h diff -u php4/ext/domxml/php_domxml.h:1.42 php4/ext/domxml/php_domxml.h:1.43 --- php4/ext/domxml/php_domxml.h:1.42 Thu Feb 28 03:26:06 2002 +++ php4/ext/domxml/php_domxml.h Mon Mar 11 17:37:32 2002 @@ -16,7 +16,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: php_domxml.h,v 1.42 2002/02/28 08:26:06 sebastian Exp $ */ +/* $Id: php_domxml.h,v 1.43 2002/03/11 22:37:32 chregu Exp $ */ #ifndef PHP_DOMXML_H #define PHP_DOMXML_H @@ -141,8 +141,7 @@ PHP_FUNCTION(domxml_elem_remove_attribute); PHP_FUNCTION(domxml_elem_get_attribute_node); PHP_FUNCTION(domxml_elem_set_attribute_node); -PHP_FUNCTION(domxml_elem_get_element_by_tagname); - +PHP_FUNCTION(domxml_elem_get_elements_by_tagname); /* Class CData methods */ PHP_FUNCTION(domxml_cdata_length); @@ -166,6 +165,8 @@ PHP_FUNCTION(xpath_eval); PHP_FUNCTION(xpath_eval_expression); PHP_FUNCTION(xpath_register_ns); +PHP_FUNCTION(domxml_doc_get_elements_by_tagname); +PHP_FUNCTION(domxml_doc_get_element_by_id); #endif #if defined(LIBXML_XPTR_ENABLED) PHP_FUNCTION(xptr_new_context);
Index: php4/ext/domxml/php_domxml.c diff -u php4/ext/domxml/php_domxml.c:1.123 php4/ext/domxml/php_domxml.c:1.124 --- php4/ext/domxml/php_domxml.c:1.123 Fri Mar 8 17:16:07 2002 +++ php4/ext/domxml/php_domxml.c Mon Mar 11 17:37:32 2002 @@ -16,7 +16,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: php_domxml.c,v 1.123 2002/03/08 22:16:07 chregu Exp $ */ +/* $Id: php_domxml.c,v 1.124 2002/03/11 22:37:32 chregu Exp $ */ /* TODO * - Support Notation Nodes @@ -213,6 +213,8 @@ PHP_FE(xpath_eval, NULL) PHP_FE(xpath_eval_expression, NULL) PHP_FE(xpath_register_ns, NULL) + PHP_FE(domxml_doc_get_elements_by_tagname, NULL) + PHP_FE(domxml_doc_get_element_by_id, NULL) #endif #if defined(LIBXML_XPTR_ENABLED) PHP_FE(xptr_new_context, NULL) @@ -272,6 +274,8 @@ PHP_FALIAS(xpath_init, xpath_init, NULL) PHP_FALIAS(xpath_new_context, xpath_new_context, NULL) PHP_FALIAS(xptr_new_context, xptr_new_context, NULL) + PHP_FALIAS(get_elements_by_tagname, domxml_doc_get_elements_by_tagname, NULL) + PHP_FALIAS(get_element_by_id, domxml_doc_get_element_by_id, NULL) #endif {NULL, NULL, NULL} }; @@ -336,7 +340,7 @@ PHP_FALIAS(remove_attribute, domxml_elem_remove_attribute, NULL) PHP_FALIAS(get_attribute_node, domxml_elem_get_attribute_node, NULL) PHP_FALIAS(set_attribute_node, domxml_elem_set_attribute_node, NULL) - PHP_FALIAS(get_element_by_tagname, domxml_elem_get_element_by_tagname, NULL) + PHP_FALIAS(get_elements_by_tagname, domxml_elem_get_elements_by_tagname, NULL) {NULL, NULL, NULL} }; @@ -500,6 +504,27 @@ } } +static xmlNodeSetPtr php_get_elements_by_tagname(xmlNodePtr n, xmlChar* name) +{ + xmlNodeSetPtr rv = NULL; + xmlNodePtr cld = NULL; + + if ( n != NULL && name != NULL ) { + cld = n->children; + while ( cld != NULL ) { + if ( xmlStrcmp( name, cld->name ) == 0 ){ + if ( rv == NULL ) { + rv = xmlXPathNodeSetCreate( cld ) ; + } + else { + xmlXPathNodeSetAdd( rv, cld ); + } + } + cld = cld->next; + } + } + return rv; +} static void php_free_xml_doc(zend_rsrc_list_entry *rsrc TSRMLS_DC) { @@ -2259,25 +2284,205 @@ } /* }}} */ -/* {{{ proto string domxml_elem_get_element_by_tagname(string tagname) - Returns element for given attribute */ -PHP_FUNCTION(domxml_elem_get_element_by_tagname) +#if defined(LIBXML_XPATH_ENABLED) +/* {{{ proto string domxml_doc_get_elements_by_tagname(string tagname [,object xpathctx_handle] ) + Returns array with nodes with given tagname in document or empty array, if not found*/ +PHP_FUNCTION(domxml_doc_get_elements_by_tagname) +{ + zval *id, *rv, *contextnode = NULL,*ctxpin = NULL; + xmlXPathContextPtr ctxp; + xmlDocPtr docp; + + xmlXPathObjectPtr xpathobjp; + xmlNode *contextnodep; + int name_len; + char *str,*name; + + contextnode = NULL; + contextnodep = NULL; + + DOMXML_PARAM_FOUR(docp, id, le_domxmldocp, "s|oo", &name, &name_len,&ctxpin,&contextnodep); + + /* if no xpath_context was submitted, create a new one */ + if (ctxpin == NULL) + { + ctxp = xmlXPathNewContext(docp); + } + else + { + DOMXML_GET_OBJ(ctxp, ctxpin, le_xpathctxp); + } + + if (contextnode) { + DOMXML_GET_OBJ(contextnodep, contextnode, le_domxmlnodep); + } + ctxp->node = contextnodep; + str = (char*) emalloc((name_len+3) * sizeof(char)) ; + if (str == NULL) + { + php_error(E_WARNING, "%s(): cannot allocate memory for string", get_active_function_name(TSRMLS_C)); + } + sprintf(str ,"//%s",name); + + xpathobjp = xmlXPathEval(str, ctxp); + efree(str); + ctxp->node = NULL; + if (!xpathobjp) { + RETURN_FALSE; + } + MAKE_STD_ZVAL(rv); + + if(array_init(rv) != SUCCESS) + { + php_error(E_WARNING, "%s(): cannot create required array", get_active_function_name(TSRMLS_C)); + RETURN_FALSE; + } + + switch (Z_TYPE_P(xpathobjp)) { + + case XPATH_NODESET: + { + int i; + xmlNodeSetPtr nodesetp; + + if (NULL == (nodesetp = xpathobjp->nodesetval)) { + zval_dtor(rv); + RETURN_FALSE; + } + + for (i = 0; i < nodesetp->nodeNr; i++) { + xmlNodePtr node = nodesetp->nodeTab[i]; + zval *child; + int retnode; + + /* construct a node object */ + child = php_domobject_new(node, &retnode TSRMLS_CC); + zend_hash_next_index_insert(Z_ARRVAL_P(rv), &child, sizeof(zval *), NULL); + } + + break; + } + default: + break; + } + + *return_value = *rv; + FREE_ZVAL(rv); +} +/* }}} */ + +/* {{{ proto string domxml_doc_get_element_by_id(string id [,object xpathctx_handle] ) + Returns element for given id or false if not found */ +PHP_FUNCTION(domxml_doc_get_element_by_id) { - zval *id, *arg1; + zval *id, *rv, *contextnode = NULL, *ctxpin = NULL; + xmlXPathContextPtr ctxp ; + xmlDocPtr docp; + + xmlXPathObjectPtr xpathobjp; + xmlNode *contextnodep; + int name_len; + char *str,*name; + + contextnode = NULL; + contextnodep = NULL; + + DOMXML_PARAM_FOUR(docp, id, le_domxmldocp, "s|oo", &name, &name_len,&ctxpin,&contextnodep); + + /* if no xpath_context was submitted, create a new one */ + if (ctxpin == NULL) + { + ctxp = xmlXPathNewContext(docp); + } + else + { + DOMXML_GET_OBJ(ctxp, ctxpin, le_xpathctxp); + } + + if (contextnode) { + DOMXML_GET_OBJ(contextnodep, contextnode, le_domxmlnodep); + } + ctxp->node = contextnodep; + str = (char*) emalloc((name_len+14) * sizeof(char)) ; + if (str == NULL) + { + php_error(E_WARNING, "%s(): cannot allocate memory for string", get_active_function_name(TSRMLS_C)); + } + sprintf(str ,"//*[@ID = '%s']",name); + xpathobjp = xmlXPathEval(str, ctxp); + efree(str); + ctxp->node = NULL; + if (!xpathobjp) { + RETURN_FALSE; + } + + switch (Z_TYPE_P(xpathobjp)) { + + case XPATH_NODESET: + { + xmlNodeSetPtr nodesetp; + + if (NULL == (nodesetp = xpathobjp->nodesetval)) { + zval_dtor(rv); + RETURN_FALSE; + } + + if (nodesetp->nodeNr > 0) { + xmlNodePtr node = nodesetp->nodeTab[0]; + int retnode; + rv = php_domobject_new(node, &retnode TSRMLS_CC); + } + else { + /*return false array, if no nodes were found */ + RETURN_FALSE; + } + + break; + } + default: + break; + } + + *return_value = *rv; + FREE_ZVAL(rv); +} +/* }}} */ +#endif +/* {{{ proto string domxml_elem_get_elements_by_tagname(string tagname) + Returns array with nodes with given tagname in element or empty array, if not found */ +PHP_FUNCTION(domxml_elem_get_elements_by_tagname) +{ + zval *id,*rv; xmlNode *nodep; + int name_len,i; + char *name; + xmlNodeSet *nodesetp; - DOMXML_NOT_IMPLEMENTED(); + DOMXML_PARAM_TWO(nodep, id, le_domxmlelementp, "s", &name, &name_len); - if ((ZEND_NUM_ARGS() == 1) && getParameters(ht, 1, &arg1) == SUCCESS) { - id = getThis(); - nodep = php_dom_get_object(id, le_domxmlelementp, 0 TSRMLS_CC); - } else { - WRONG_PARAM_COUNT; + MAKE_STD_ZVAL(rv); + + if(array_init(rv) != SUCCESS) + { + php_error(E_WARNING, "%s(): cannot create required array", get_active_function_name(TSRMLS_C)); + RETURN_FALSE; } - convert_to_string(arg1); + nodesetp = php_get_elements_by_tagname(nodep,name); - /* FIXME: not implemented */ + if(nodesetp) { + + for (i = 0; i < nodesetp->nodeNr; i++) { + xmlNodePtr node = nodesetp->nodeTab[i]; + zval *child; + int retnode; + + child = php_domobject_new(node, &retnode TSRMLS_CC); + zend_hash_next_index_insert(Z_ARRVAL_P(rv), &child, sizeof(zval *), NULL); + } + } + *return_value = *rv; + FREE_ZVAL(rv); } /* }}} */ Index: php4/ext/domxml/php_domxml.h diff -u php4/ext/domxml/php_domxml.h:1.42 php4/ext/domxml/php_domxml.h:1.43 --- php4/ext/domxml/php_domxml.h:1.42 Thu Feb 28 03:26:06 2002 +++ php4/ext/domxml/php_domxml.h Mon Mar 11 17:37:32 2002 @@ -16,7 +16,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: php_domxml.h,v 1.42 2002/02/28 08:26:06 sebastian Exp $ */ +/* $Id: php_domxml.h,v 1.43 2002/03/11 22:37:32 chregu Exp $ */ #ifndef PHP_DOMXML_H #define PHP_DOMXML_H @@ -141,8 +141,7 @@ PHP_FUNCTION(domxml_elem_remove_attribute); PHP_FUNCTION(domxml_elem_get_attribute_node); PHP_FUNCTION(domxml_elem_set_attribute_node); -PHP_FUNCTION(domxml_elem_get_element_by_tagname); - +PHP_FUNCTION(domxml_elem_get_elements_by_tagname); /* Class CData methods */ PHP_FUNCTION(domxml_cdata_length); @@ -166,6 +165,8 @@ PHP_FUNCTION(xpath_eval); PHP_FUNCTION(xpath_eval_expression); PHP_FUNCTION(xpath_register_ns); +PHP_FUNCTION(domxml_doc_get_elements_by_tagname); +PHP_FUNCTION(domxml_doc_get_element_by_id); #endif #if defined(LIBXML_XPTR_ENABLED) PHP_FUNCTION(xptr_new_context);