Back to index

php5  5.3.10
documentfragment.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: documentfragment.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 /* {{{ arginfo */
00031 ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_documentfragement_construct, 0, 0, 0)
00032 ZEND_END_ARG_INFO();
00033 
00034 ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_documentfragement_appendXML, 0, 0, 1)
00035        ZEND_ARG_INFO(0, data)
00036 ZEND_END_ARG_INFO();
00037 /* }}} */
00038 
00039 /*
00040 * class DOMDocumentFragment extends DOMNode 
00041 *
00042 * URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#ID-B63ED1A3
00043 * Since: 
00044 */
00045 
00046 const zend_function_entry php_dom_documentfragment_class_functions[] = {
00047        PHP_ME(domdocumentfragment, __construct, arginfo_dom_documentfragement_construct, ZEND_ACC_PUBLIC)
00048        PHP_ME(domdocumentfragment, appendXML, arginfo_dom_documentfragement_appendXML, ZEND_ACC_PUBLIC)
00049        PHP_FE_END
00050 };
00051 
00052 /* {{{ proto void DOMDocumentFragment::__construct(); */
00053 PHP_METHOD(domdocumentfragment, __construct)
00054 {
00055 
00056        zval *id;
00057        xmlNodePtr nodep = NULL, oldnode = NULL;
00058        dom_object *intern;
00059        zend_error_handling error_handling;
00060 
00061        zend_replace_error_handling(EH_THROW, dom_domexception_class_entry, &error_handling TSRMLS_CC);
00062        if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &id, dom_documentfragment_class_entry) == FAILURE) {
00063               zend_restore_error_handling(&error_handling TSRMLS_CC);
00064               return;
00065        }
00066 
00067        zend_restore_error_handling(&error_handling TSRMLS_CC);
00068        nodep = xmlNewDocFragment(NULL);
00069 
00070        if (!nodep) {
00071               php_dom_throw_error(INVALID_STATE_ERR, 1 TSRMLS_CC);
00072               RETURN_FALSE;
00073        }
00074 
00075        intern = (dom_object *)zend_object_store_get_object(id TSRMLS_CC);
00076        if (intern != NULL) {
00077               oldnode = dom_object_get_node(intern);
00078               if (oldnode != NULL) {
00079                      php_libxml_node_free_resource(oldnode  TSRMLS_CC);
00080               }
00081               /* php_dom_set_object(intern, nodep TSRMLS_CC); */
00082               php_libxml_increment_node_ptr((php_libxml_node_object *)intern, nodep, (void *)intern TSRMLS_CC);
00083        }
00084 }
00085 /* }}} end DOMDocumentFragment::__construct */
00086 
00087 /* php_dom_xmlSetTreeDoc is a custom implementation of xmlSetTreeDoc
00088  needed for hack in appendXML due to libxml bug - no need to share this function */
00089 static void php_dom_xmlSetTreeDoc(xmlNodePtr tree, xmlDocPtr doc) /* {{{ */
00090 {
00091     xmlAttrPtr prop;
00092        xmlNodePtr cur;
00093 
00094     if (tree) {
00095               if(tree->type == XML_ELEMENT_NODE) {
00096                      prop = tree->properties;
00097                      while (prop != NULL) {
00098                             prop->doc = doc;
00099                             if (prop->children) {
00100                                    cur = prop->children;
00101                                    while (cur != NULL) {
00102                                           php_dom_xmlSetTreeDoc(cur, doc);
00103                                           cur = cur->next;
00104                                    }
00105                             }
00106                             prop = prop->next;
00107                      }
00108               }
00109               if (tree->children != NULL) {
00110                      cur = tree->children;
00111                      while (cur != NULL) {
00112                             php_dom_xmlSetTreeDoc(cur, doc);
00113                             cur = cur->next;
00114                      }
00115               }
00116               tree->doc = doc;
00117     }
00118 }
00119 /* }}} */
00120 
00121 /* {{{ proto void DOMDocumentFragment::appendXML(string data); */
00122 PHP_METHOD(domdocumentfragment, appendXML) {
00123        zval *id;
00124        xmlNode *nodep;
00125        dom_object *intern;
00126        char *data = NULL;
00127        int data_len = 0;
00128        int err;
00129        xmlNodePtr lst;
00130 
00131        if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Os", &id, dom_documentfragment_class_entry, &data, &data_len) == FAILURE) {
00132               return;
00133        }
00134 
00135        DOM_GET_OBJ(nodep, id, xmlNodePtr, intern);
00136 
00137        if (dom_node_is_read_only(nodep) == SUCCESS) {
00138               php_dom_throw_error(NO_MODIFICATION_ALLOWED_ERR, dom_get_strict_error(intern->document) TSRMLS_CC);
00139               RETURN_FALSE;
00140        }
00141 
00142        if (data) {
00143               err = xmlParseBalancedChunkMemory(nodep->doc, NULL, NULL, 0, data, &lst);
00144               if (err != 0) {
00145                      RETURN_FALSE;
00146               }
00147               /* Following needed due to bug in libxml2 <= 2.6.14 
00148               ifdef after next libxml release as bug is fixed in their cvs */
00149               php_dom_xmlSetTreeDoc(lst, nodep->doc);
00150               /* End stupid hack */
00151 
00152               xmlAddChildList(nodep,lst);
00153        }
00154 
00155        RETURN_TRUE;
00156 }
00157 /* }}} */
00158 
00159 #endif
00160 
00161 /*
00162  * Local variables:
00163  * tab-width: 4
00164  * c-basic-offset: 4
00165  * End:
00166  * vim600: noet sw=4 ts=4 fdm=marker
00167  * vim<600: noet sw=4 ts=4
00168  */