Back to index

php5  5.3.10
nodelist.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: nodelist.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_nodelist_item, 0, 0, 1)
00033        ZEND_ARG_INFO(0, index)
00034 ZEND_END_ARG_INFO();
00035 /* }}} */
00036 
00037 /*
00038 * class DOMNodeList 
00039 *
00040 * URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#ID-536297177
00041 * Since: 
00042 */
00043 
00044 const zend_function_entry php_dom_nodelist_class_functions[] = {
00045        PHP_FALIAS(item, dom_nodelist_item, arginfo_dom_nodelist_item)
00046        PHP_FE_END
00047 };
00048 
00049 /* {{{ length int    
00050 readonly=yes 
00051 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#ID-203510337
00052 Since: 
00053 */
00054 int dom_nodelist_length_read(dom_object *obj, zval **retval TSRMLS_DC)
00055 {
00056        dom_nnodemap_object *objmap;
00057        xmlNodePtr nodep, curnode;
00058        int count = 0;
00059        HashTable *nodeht;
00060 
00061        objmap = (dom_nnodemap_object *)obj->ptr;
00062        if (objmap != NULL) {
00063               if (objmap->ht) {
00064                      count = xmlHashSize(objmap->ht);
00065               } else {
00066                      if (objmap->nodetype == DOM_NODESET) {
00067                             nodeht = HASH_OF(objmap->baseobjptr);
00068                             count = zend_hash_num_elements(nodeht);
00069                      } else {
00070                             nodep = dom_object_get_node(objmap->baseobj);
00071                             if (nodep) {
00072                                    if (objmap->nodetype == XML_ATTRIBUTE_NODE || objmap->nodetype == XML_ELEMENT_NODE) {
00073                                           curnode = nodep->children;
00074                                           if (curnode) {
00075                                                  count++;
00076                                                  while (curnode->next != NULL) {
00077                                                         count++;
00078                                                         curnode = curnode->next;
00079                                                  }
00080                                           }
00081                                    } else {
00082                                           if (nodep->type == XML_DOCUMENT_NODE || nodep->type == XML_HTML_DOCUMENT_NODE) {
00083                                                  nodep = xmlDocGetRootElement((xmlDoc *) nodep);
00084                                           } else {
00085                                                  nodep = nodep->children;
00086                                           }
00087                                           curnode = dom_get_elements_by_tag_name_ns_raw(nodep, objmap->ns, objmap->local, &count, -1);
00088                                    }
00089                             }
00090                      }
00091               }
00092        }
00093 
00094        MAKE_STD_ZVAL(*retval);
00095        ZVAL_LONG(*retval, count);
00096        return SUCCESS;
00097 }
00098 
00099 /* }}} */
00100 
00101 /* {{{ proto DOMNode dom_nodelist_item(int index);
00102 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#ID-844377136
00103 Since: 
00104 */
00105 PHP_FUNCTION(dom_nodelist_item)
00106 {
00107        zval *id, *rv = NULL;
00108        long index;
00109        int ret;
00110        dom_object *intern;
00111        xmlNodePtr itemnode = NULL;
00112 
00113        dom_nnodemap_object *objmap;
00114        xmlNodePtr nodep, curnode;
00115        int count = 0;
00116        HashTable *nodeht;
00117        zval **entry;
00118 
00119        if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Ol", &id, dom_nodelist_class_entry, &index) == FAILURE) {
00120               return;
00121        }
00122 
00123        if (index >= 0) {
00124               intern = (dom_object *)zend_object_store_get_object(id TSRMLS_CC);
00125 
00126               objmap = (dom_nnodemap_object *)intern->ptr;
00127               if (objmap != NULL) {
00128                      if (objmap->ht) {
00129                             if (objmap->nodetype == XML_ENTITY_NODE) {
00130                                    itemnode = php_dom_libxml_hash_iter(objmap->ht, index);
00131                             } else {
00132                                    itemnode = php_dom_libxml_notation_iter(objmap->ht, index);
00133                             }
00134                      } else {
00135                             if (objmap->nodetype == DOM_NODESET) {
00136                                    nodeht = HASH_OF(objmap->baseobjptr);
00137                                    if (zend_hash_index_find(nodeht, index, (void **) &entry)==SUCCESS) {
00138                                           *return_value = **entry;
00139                                           zval_copy_ctor(return_value);
00140                                           return;
00141                                    }
00142                             } else if (objmap->baseobj) {
00143                                    nodep = dom_object_get_node(objmap->baseobj);
00144                                    if (nodep) {
00145                                           if (objmap->nodetype == XML_ATTRIBUTE_NODE || objmap->nodetype == XML_ELEMENT_NODE) {
00146                                                  curnode = nodep->children;
00147                                                  while (count < index && curnode != NULL) {
00148                                                         count++;
00149                                                         curnode = curnode->next;
00150                                                  }
00151                                                  itemnode = curnode;
00152                                           } else {
00153                                                  if (nodep->type == XML_DOCUMENT_NODE || nodep->type == XML_HTML_DOCUMENT_NODE) {
00154                                                         nodep = xmlDocGetRootElement((xmlDoc *) nodep);
00155                                                  } else {
00156                                                         nodep = nodep->children;
00157                                                  }
00158                                                  itemnode = dom_get_elements_by_tag_name_ns_raw(nodep, objmap->ns, objmap->local, &count, index);
00159                                           }
00160                                    }
00161                             }
00162                      }
00163               }
00164 
00165               if (itemnode) {
00166                      DOM_RET_OBJ(rv, itemnode, &ret, objmap->baseobj);
00167                      return;
00168               }
00169        }
00170 
00171        RETVAL_NULL();
00172 }
00173 /* }}} end dom_nodelist_item */
00174 
00175 #endif
00176 
00177 /*
00178  * Local variables:
00179  * tab-width: 4
00180  * c-basic-offset: 4
00181  * End:
00182  * vim600: noet sw=4 ts=4 fdm=marker
00183  * vim<600: noet sw=4 ts=4
00184  */