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