/* {{{ proto DOMDocument dom_domimplementation_create_document(string namespaceURI, string qualifiedName, DOMDocumentType doctype); URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#Level-2-Core-DOM-createDocument Since: DOM Level 2 */ PHP_METHOD(domimplementation, createDocument) { zval *node = NULL, *rv = NULL; xmlDoc *docp; xmlNode *nodep; xmlDtdPtr doctype = NULL; xmlNsPtr nsptr = NULL; int ret, uri_len = 0, name_len = 0, errorcode = 0; char *uri, *name; char *prefix = NULL, *localname = NULL; dom_object *doctobj; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|ssO", &uri, &uri_len, &name, &name_len, &node, dom_documenttype_class_entry) == FAILURE) { return; } if (node != NULL) { DOM_GET_OBJ(doctype, node, xmlDtdPtr, doctobj); if (doctype->type == XML_DOCUMENT_TYPE_NODE) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid DocumentType object"); RETURN_FALSE; } if (doctype->doc != NULL) { php_dom_throw_error(WRONG_DOCUMENT_ERR, 1 TSRMLS_CC); RETURN_FALSE; } } else { doctobj = NULL; } if (name_len > 0) { errorcode = dom_check_qname(name, &localname, &prefix, 1, name_len); if (errorcode == 0 && uri_len > 0 && ((nsptr = xmlNewNs(NULL, uri, prefix)) == NULL)) { errorcode = NAMESPACE_ERR; } } if (prefix != NULL) { xmlFree(prefix); } if (errorcode != 0) { if (localname != NULL) { xmlFree(localname); } php_dom_throw_error(errorcode, 1 TSRMLS_CC); RETURN_FALSE; } /* currently letting libxml2 set the version string */ docp = xmlNewDoc(NULL); if (!docp) { if (localname != NULL) { xmlFree(localname); } RETURN_FALSE; } if (doctype != NULL) { docp->intSubset = doctype; doctype->parent = docp; doctype->doc = docp; docp->children = (xmlNodePtr) doctype; docp->last = (xmlNodePtr) doctype; } if (localname != NULL) { nodep = xmlNewDocNode (docp, nsptr, localname, NULL); if (!nodep) { if (doctype != NULL) { docp->intSubset = NULL; doctype->parent = NULL; doctype->doc = NULL; docp->children = NULL; docp->last = NULL; } xmlFreeDoc(docp); xmlFree(localname); /* Need some type of error here */ php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unexpected Error"); RETURN_FALSE; } nodep->nsDef = nsptr; xmlDocSetRootElement(docp, nodep); xmlFree(localname); } DOM_RET_OBJ(rv, (xmlNodePtr) docp, &ret, NULL); if (doctobj != NULL) { doctobj->document = ((dom_object *)((php_libxml_node_ptr *)docp->_private)->_private)->document; php_libxml_increment_doc_ref((php_libxml_node_object *)doctobj, docp TSRMLS_CC); } }
/* {{{ proto void DOMElement::__construct(string name, [string value], [string uri]); */ PHP_METHOD(domelement, __construct) { zval *id; xmlNodePtr nodep = NULL, oldnode = NULL; dom_object *intern; char *name, *value = NULL, *uri = NULL; char *localname = NULL, *prefix = NULL; int errorcode = 0, uri_len = 0; int name_len, value_len = 0, name_valid; xmlNsPtr nsptr = NULL; zend_error_handling error_handling; zend_replace_error_handling(EH_THROW, dom_domexception_class_entry, &error_handling TSRMLS_CC); if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Os|s!s", &id, dom_element_class_entry, &name, &name_len, &value, &value_len, &uri, &uri_len) == FAILURE) { zend_restore_error_handling(&error_handling TSRMLS_CC); return; } zend_restore_error_handling(&error_handling TSRMLS_CC); name_valid = xmlValidateName((xmlChar *) name, 0); if (name_valid != 0) { php_dom_throw_error(INVALID_CHARACTER_ERR, 1 TSRMLS_CC); RETURN_FALSE; } /* Namespace logic is separate and only when uri passed in to insure no BC breakage */ if (uri_len > 0) { errorcode = dom_check_qname(name, &localname, &prefix, uri_len, name_len); if (errorcode == 0) { nodep = xmlNewNode (NULL, (xmlChar *)localname); if (nodep != NULL && uri != NULL) { nsptr = dom_get_ns(nodep, uri, &errorcode, prefix); xmlSetNs(nodep, nsptr); } } xmlFree(localname); if (prefix != NULL) { xmlFree(prefix); } if (errorcode != 0) { if (nodep != NULL) { xmlFreeNode(nodep); } php_dom_throw_error(errorcode, 1 TSRMLS_CC); RETURN_FALSE; } } else { /* If you don't pass a namespace uri, then you can't set a prefix */ localname = xmlSplitQName2((xmlChar *)name, (xmlChar **) &prefix); if (prefix != NULL) { xmlFree(localname); xmlFree(prefix); php_dom_throw_error(NAMESPACE_ERR, 1 TSRMLS_CC); RETURN_FALSE; } nodep = xmlNewNode(NULL, (xmlChar *) name); } if (!nodep) { php_dom_throw_error(INVALID_STATE_ERR, 1 TSRMLS_CC); RETURN_FALSE; } if (value_len > 0) { xmlNodeSetContentLen(nodep, (xmlChar *) value, value_len); } intern = (dom_object *)zend_object_store_get_object(id TSRMLS_CC); if (intern != NULL) { oldnode = dom_object_get_node(intern); if (oldnode != NULL) { php_libxml_node_free_resource(oldnode TSRMLS_CC); } php_libxml_increment_node_ptr((php_libxml_node_object *)intern, nodep, (void *)intern TSRMLS_CC); } }
/* {{{ proto void DOMElement::__construct(string name, [string value], [string uri]) */ PHP_METHOD(domelement, __construct) { zval *id = getThis(); xmlNodePtr nodep = NULL, oldnode = NULL; dom_object *intern; char *name, *value = NULL, *uri = NULL; char *localname = NULL, *prefix = NULL; int errorcode = 0; size_t name_len, value_len = 0, uri_len = 0; int name_valid; xmlNsPtr nsptr = NULL; if (zend_parse_parameters_throw(ZEND_NUM_ARGS(), "s|s!s", &name, &name_len, &value, &value_len, &uri, &uri_len) == FAILURE) { return; } name_valid = xmlValidateName((xmlChar *) name, 0); if (name_valid != 0) { php_dom_throw_error(INVALID_CHARACTER_ERR, 1); RETURN_FALSE; } /* Namespace logic is separate and only when uri passed in to insure no BC breakage */ if (uri_len > 0) { errorcode = dom_check_qname(name, &localname, &prefix, uri_len, name_len); if (errorcode == 0) { nodep = xmlNewNode (NULL, (xmlChar *)localname); if (nodep != NULL && uri != NULL) { nsptr = dom_get_ns(nodep, uri, &errorcode, prefix); xmlSetNs(nodep, nsptr); } } xmlFree(localname); if (prefix != NULL) { xmlFree(prefix); } if (errorcode != 0) { if (nodep != NULL) { xmlFreeNode(nodep); } php_dom_throw_error(errorcode, 1); RETURN_FALSE; } } else { /* If you don't pass a namespace uri, then you can't set a prefix */ localname = (char *) xmlSplitQName2((xmlChar *) name, (xmlChar **) &prefix); if (prefix != NULL) { xmlFree(localname); xmlFree(prefix); php_dom_throw_error(NAMESPACE_ERR, 1); RETURN_FALSE; } nodep = xmlNewNode(NULL, (xmlChar *) name); } if (!nodep) { php_dom_throw_error(INVALID_STATE_ERR, 1); RETURN_FALSE; } if (value_len > 0) { xmlNodeSetContentLen(nodep, (xmlChar *) value, value_len); } intern = Z_DOMOBJ_P(id); oldnode = dom_object_get_node(intern); if (oldnode != NULL) { php_libxml_node_free_resource(oldnode ); } php_libxml_increment_node_ptr((php_libxml_node_object *)intern, nodep, (void *)intern); }