1 : /*
2 : +----------------------------------------------------------------------+
3 : | PHP Version 5 |
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: attr.c 277100 2009-03-13 13:41:42Z rrichards $ */
21 :
22 : #ifdef HAVE_CONFIG_H
23 : #include "config.h"
24 : #endif
25 :
26 : #include "php.h"
27 :
28 : #if HAVE_LIBXML && HAVE_DOM
29 :
30 : #include "php_dom.h"
31 :
32 : /* {{{ arginfo */
33 : static
34 : ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_attr_is_id, 0, 0, 0)
35 : ZEND_END_ARG_INFO();
36 :
37 : static
38 : ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_attr_construct, 0, 0, 1)
39 : ZEND_ARG_INFO(0, name)
40 : ZEND_ARG_INFO(0, value)
41 : ZEND_END_ARG_INFO();
42 : /* }}} */
43 :
44 : /*
45 : * class DOMAttr extends DOMNode
46 : *
47 : * URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#ID-637646024
48 : * Since:
49 : */
50 :
51 : zend_function_entry php_dom_attr_class_functions[] = {
52 : PHP_FALIAS(isId, dom_attr_is_id, arginfo_dom_attr_is_id)
53 : PHP_ME(domattr, __construct, arginfo_dom_attr_construct, ZEND_ACC_PUBLIC)
54 : {NULL, NULL, NULL}
55 : };
56 :
57 : /* {{{ proto void DOMAttr::__construct(string name, [string value]); */
58 : PHP_METHOD(domattr, __construct)
59 5 : {
60 :
61 : zval *id;
62 5 : xmlAttrPtr nodep = NULL;
63 5 : xmlNodePtr oldnode = NULL;
64 : dom_object *intern;
65 5 : char *name, *value = NULL;
66 : int name_len, value_len, name_valid;
67 :
68 5 : php_set_error_handling(EH_THROW, dom_domexception_class_entry TSRMLS_CC);
69 5 : 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) {
70 1 : php_std_error_handling();
71 1 : return;
72 : }
73 :
74 4 : php_std_error_handling();
75 4 : intern = (dom_object *)zend_object_store_get_object(id TSRMLS_CC);
76 :
77 4 : name_valid = xmlValidateName((xmlChar *) name, 0);
78 4 : if (name_valid != 0) {
79 0 : php_dom_throw_error(INVALID_CHARACTER_ERR, 1 TSRMLS_CC);
80 0 : RETURN_FALSE;
81 : }
82 :
83 4 : nodep = xmlNewProp(NULL, (xmlChar *) name, value);
84 :
85 4 : if (!nodep) {
86 0 : php_dom_throw_error(INVALID_STATE_ERR, 1 TSRMLS_CC);
87 0 : RETURN_FALSE;
88 : }
89 :
90 4 : if (intern != NULL) {
91 4 : oldnode = dom_object_get_node(intern);
92 4 : if (oldnode != NULL) {
93 0 : php_libxml_node_free_resource(oldnode TSRMLS_CC);
94 : }
95 4 : php_libxml_increment_node_ptr((php_libxml_node_object *)intern, (xmlNodePtr)nodep, (void *)intern TSRMLS_CC);
96 : }
97 : }
98 :
99 : /* }}} end DOMAttr::__construct */
100 :
101 : /* {{{ name string
102 : readonly=yes
103 : URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#ID-1112119403
104 : Since:
105 : */
106 : int dom_attr_name_read(dom_object *obj, zval **retval TSRMLS_DC)
107 1 : {
108 : xmlAttrPtr attrp;
109 :
110 1 : attrp = (xmlAttrPtr) dom_object_get_node(obj);
111 :
112 1 : if (attrp == NULL) {
113 0 : php_dom_throw_error(INVALID_STATE_ERR, 0 TSRMLS_CC);
114 0 : return FAILURE;
115 : }
116 :
117 1 : ALLOC_ZVAL(*retval);
118 1 : ZVAL_STRING(*retval, (char *) (attrp->name), 1);
119 :
120 1 : return SUCCESS;
121 : }
122 :
123 : /* }}} */
124 :
125 : /* {{{ specified boolean
126 : readonly=yes
127 : URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#ID-862529273
128 : Since:
129 : */
130 : int dom_attr_specified_read(dom_object *obj, zval **retval TSRMLS_DC)
131 0 : {
132 : /* TODO */
133 0 : ALLOC_ZVAL(*retval);
134 0 : ZVAL_TRUE(*retval);
135 0 : return SUCCESS;
136 : }
137 :
138 : /* }}} */
139 :
140 : /* {{{ value string
141 : readonly=no
142 : URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#ID-221662474
143 : Since:
144 : */
145 : int dom_attr_value_read(dom_object *obj, zval **retval TSRMLS_DC)
146 6 : {
147 : xmlAttrPtr attrp;
148 : xmlChar *content;
149 :
150 6 : attrp = (xmlAttrPtr) dom_object_get_node(obj);
151 :
152 6 : if (attrp == NULL) {
153 0 : php_dom_throw_error(INVALID_STATE_ERR, 0 TSRMLS_CC);
154 0 : return FAILURE;
155 : }
156 :
157 6 : ALLOC_ZVAL(*retval);
158 :
159 :
160 6 : if ((content = xmlNodeGetContent((xmlNodePtr) attrp)) != NULL) {
161 5 : ZVAL_STRING(*retval, content, 1);
162 5 : xmlFree(content);
163 : } else {
164 1 : ZVAL_EMPTY_STRING(*retval);
165 : }
166 :
167 6 : return SUCCESS;
168 :
169 : }
170 :
171 : int dom_attr_value_write(dom_object *obj, zval *newval TSRMLS_DC)
172 5 : {
173 : zval value_copy;
174 : xmlAttrPtr attrp;
175 :
176 5 : attrp = (xmlAttrPtr) dom_object_get_node(obj);
177 :
178 5 : if (attrp == NULL) {
179 0 : php_dom_throw_error(INVALID_STATE_ERR, 0 TSRMLS_CC);
180 0 : return FAILURE;
181 : }
182 :
183 5 : if (attrp->children) {
184 4 : node_list_unlink(attrp->children TSRMLS_CC);
185 : }
186 :
187 5 : if (newval->type != IS_STRING) {
188 1 : if(newval->refcount > 1) {
189 0 : value_copy = *newval;
190 0 : zval_copy_ctor(&value_copy);
191 0 : newval = &value_copy;
192 : }
193 1 : convert_to_string(newval);
194 : }
195 :
196 5 : xmlNodeSetContentLen((xmlNodePtr) attrp, Z_STRVAL_P(newval), Z_STRLEN_P(newval) + 1);
197 :
198 5 : if (newval == &value_copy) {
199 0 : zval_dtor(newval);
200 : }
201 :
202 5 : return SUCCESS;
203 : }
204 :
205 : /* }}} */
206 :
207 : /* {{{ ownerElement DOMElement
208 : readonly=yes
209 : URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#Attr-ownerElement
210 : Since: DOM Level 2
211 : */
212 : int dom_attr_owner_element_read(dom_object *obj, zval **retval TSRMLS_DC)
213 1 : {
214 : xmlNodePtr nodep, nodeparent;
215 : int ret;
216 :
217 1 : nodep = dom_object_get_node(obj);
218 :
219 1 : if (nodep == NULL) {
220 0 : php_dom_throw_error(INVALID_STATE_ERR, 0 TSRMLS_CC);
221 0 : return FAILURE;
222 : }
223 :
224 1 : ALLOC_ZVAL(*retval);
225 :
226 1 : nodeparent = nodep->parent;
227 1 : if (!nodeparent) {
228 0 : ZVAL_NULL(*retval);
229 0 : return SUCCESS;
230 : }
231 :
232 1 : if (NULL == (*retval = php_dom_create_object(nodeparent, &ret, NULL, *retval, obj TSRMLS_CC))) {
233 0 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot create required DOM object");
234 0 : return FAILURE;
235 : }
236 1 : return SUCCESS;
237 :
238 : }
239 :
240 : /* }}} */
241 :
242 : /* {{{ schemaTypeInfo DOMTypeInfo
243 : readonly=yes
244 : URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#Attr-schemaTypeInfo
245 : Since: DOM Level 3
246 : */
247 : int dom_attr_schema_type_info_read(dom_object *obj, zval **retval TSRMLS_DC)
248 0 : {
249 0 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "Not yet implemented");
250 0 : ALLOC_ZVAL(*retval);
251 0 : ZVAL_NULL(*retval);
252 0 : return SUCCESS;
253 : }
254 :
255 : /* }}} */
256 :
257 : /* {{{ proto boolean dom_attr_is_id();
258 : URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#Attr-isId
259 : Since: DOM Level 3
260 : */
261 : PHP_FUNCTION(dom_attr_is_id)
262 1 : {
263 : zval *id;
264 : dom_object *intern;
265 : xmlAttrPtr attrp;
266 :
267 1 : if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &id, dom_attr_class_entry) == FAILURE) {
268 0 : return;
269 : }
270 :
271 1 : DOM_GET_OBJ(attrp, id, xmlAttrPtr, intern);
272 :
273 1 : if (attrp->atype == XML_ATTRIBUTE_ID) {
274 0 : RETURN_TRUE;
275 : } else {
276 1 : RETURN_FALSE;
277 : }
278 : }
279 : /* }}} end dom_attr_is_id */
280 :
281 : #endif
|