1 : /*
2 : +----------------------------------------------------------------------+
3 : | PHP Version 6 |
4 : +----------------------------------------------------------------------+
5 : | Copyright (c) 1997-2009 The PHP Group |
6 : +----------------------------------------------------------------------+
7 : | This source file is subject to version 3.01 of the PHP license, |
8 : | that is bundled with this package in the file LICENSE, and is |
9 : | available through the world-wide-web at the following url: |
10 : | http://www.php.net/license/3_01.txt |
11 : | If you did not receive a copy of the PHP license and are unable to |
12 : | obtain it through the world-wide-web, please send a note to |
13 : | license@php.net so we can mail you a copy immediately. |
14 : +----------------------------------------------------------------------+
15 : | Authors: Christian Stocker <chregu@php.net> |
16 : | Rob Richards <rrichards@php.net> |
17 : +----------------------------------------------------------------------+
18 : */
19 :
20 : /* $Id: documentfragment.c 276986 2009-03-10 23:40:06Z helly $ */
21 :
22 : #ifdef HAVE_CONFIG_H
23 : #include "config.h"
24 : #endif
25 :
26 : #include "php.h"
27 : #if HAVE_LIBXML && HAVE_DOM
28 : #include "php_dom.h"
29 :
30 : /* {{{ arginfo */
31 : ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_documentfragement_construct, 0, 0, 0)
32 : ZEND_END_ARG_INFO();
33 :
34 : ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_documentfragement_appendXML, 0, 0, 1)
35 : ZEND_ARG_INFO(0, data)
36 : ZEND_END_ARG_INFO();
37 : /* }}} */
38 :
39 : /*
40 : * class DOMDocumentFragment extends DOMNode
41 : *
42 : * URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#ID-B63ED1A3
43 : * Since:
44 : */
45 :
46 : const zend_function_entry php_dom_documentfragment_class_functions[] = {
47 : PHP_ME(domdocumentfragment, __construct, arginfo_dom_documentfragement_construct, ZEND_ACC_PUBLIC)
48 : PHP_ME(domdocumentfragment, appendXML, arginfo_dom_documentfragement_appendXML, ZEND_ACC_PUBLIC)
49 : {NULL, NULL, NULL}
50 : };
51 :
52 : /* {{{ proto void DOMDocumentFragment::__construct() U */
53 : PHP_METHOD(domdocumentfragment, __construct)
54 7 : {
55 :
56 : zval *id;
57 7 : xmlNodePtr nodep = NULL, oldnode = NULL;
58 : dom_object *intern;
59 : zend_error_handling error_handling;
60 :
61 7 : zend_replace_error_handling(EH_THROW, dom_domexception_class_entry, &error_handling TSRMLS_CC);
62 7 : if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &id, dom_documentfragment_class_entry) == FAILURE) {
63 1 : zend_restore_error_handling(&error_handling TSRMLS_CC);
64 1 : return;
65 : }
66 :
67 6 : zend_restore_error_handling(&error_handling TSRMLS_CC);
68 6 : nodep = xmlNewDocFragment(NULL);
69 :
70 6 : if (!nodep) {
71 0 : php_dom_throw_error(INVALID_STATE_ERR, 1 TSRMLS_CC);
72 0 : RETURN_FALSE;
73 : }
74 :
75 6 : intern = (dom_object *)zend_object_store_get_object(id TSRMLS_CC);
76 6 : if (intern != NULL) {
77 6 : oldnode = dom_object_get_node(intern);
78 6 : if (oldnode != NULL) {
79 1 : php_libxml_node_free_resource(oldnode TSRMLS_CC);
80 : }
81 : /* php_dom_set_object(intern, nodep TSRMLS_CC); */
82 6 : php_libxml_increment_node_ptr((php_libxml_node_object *)intern, nodep, (void *)intern TSRMLS_CC);
83 : }
84 : }
85 : /* }}} end DOMDocumentFragment::__construct */
86 :
87 : /* php_dom_xmlSetTreeDoc is a custom implementation of xmlSetTreeDoc
88 : needed for hack in appendXML due to libxml bug - no need to share this function */
89 : static void php_dom_xmlSetTreeDoc(xmlNodePtr tree, xmlDocPtr doc) /* {{{ */
90 7 : {
91 : xmlAttrPtr prop;
92 : xmlNodePtr cur;
93 :
94 7 : if (tree) {
95 7 : if(tree->type == XML_ELEMENT_NODE) {
96 3 : prop = tree->properties;
97 7 : while (prop != NULL) {
98 1 : prop->doc = doc;
99 1 : if (prop->children) {
100 1 : cur = prop->children;
101 3 : while (cur != NULL) {
102 1 : php_dom_xmlSetTreeDoc(cur, doc);
103 1 : cur = cur->next;
104 : }
105 : }
106 1 : prop = prop->next;
107 : }
108 : }
109 7 : if (tree->children != NULL) {
110 3 : cur = tree->children;
111 9 : while (cur != NULL) {
112 3 : php_dom_xmlSetTreeDoc(cur, doc);
113 3 : cur = cur->next;
114 : }
115 : }
116 7 : tree->doc = doc;
117 : }
118 7 : }
119 : /* }}} */
120 :
121 : /* {{{ proto void DOMDocumentFragment::appendXML(string data) U */
122 7 : PHP_METHOD(domdocumentfragment, appendXML) {
123 : zval *id;
124 : xmlNode *nodep;
125 : dom_object *intern;
126 7 : char *data = NULL;
127 7 : int data_len = 0;
128 : int err;
129 : xmlNodePtr lst;
130 :
131 7 : if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Os&", &id, dom_documentfragment_class_entry, &data, &data_len, UG(utf8_conv)) == FAILURE) {
132 1 : return;
133 : }
134 :
135 6 : DOM_GET_OBJ(nodep, id, xmlNodePtr, intern);
136 :
137 6 : if (dom_node_is_read_only(nodep) == SUCCESS) {
138 2 : php_dom_throw_error(NO_MODIFICATION_ALLOWED_ERR, dom_get_strict_error(intern->document) TSRMLS_CC);
139 2 : RETURN_FALSE;
140 : }
141 :
142 4 : if (data) {
143 4 : err = xmlParseBalancedChunkMemory(nodep->doc, NULL, NULL, 0, data, &lst);
144 4 : if (err != 0) {
145 1 : RETURN_FALSE;
146 : }
147 : /* Following needed due to bug in libxml2 <= 2.6.14
148 : ifdef after next libxml release as bug is fixed in their cvs */
149 3 : php_dom_xmlSetTreeDoc(lst, nodep->doc);
150 : /* End stupid hack */
151 :
152 3 : xmlAddChildList(nodep,lst);
153 : }
154 :
155 3 : RETURN_TRUE;
156 : }
157 : /* }}} */
158 :
159 : #endif
160 :
161 : /*
162 : * Local variables:
163 : * tab-width: 4
164 : * c-basic-offset: 4
165 : * End:
166 : * vim600: noet sw=4 ts=4 fdm=marker
167 : * vim<600: noet sw=4 ts=4
168 : */
|