Back to index

php5  5.3.10
domimplementation.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: domimplementation.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_implementation_get_feature, 0, 0, 2)
00032        ZEND_ARG_INFO(0, feature)
00033        ZEND_ARG_INFO(0, version)
00034 ZEND_END_ARG_INFO();
00035 
00036 ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_implementation_has_feature, 0, 0, 0)
00037 ZEND_END_ARG_INFO();
00038 
00039 ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_implementation_create_documenttype, 0, 0, 3)
00040        ZEND_ARG_INFO(0, qualifiedName)
00041        ZEND_ARG_INFO(0, publicId)
00042        ZEND_ARG_INFO(0, systemId)
00043 ZEND_END_ARG_INFO();
00044 
00045 ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_implementation_create_document, 0, 0, 3)
00046        ZEND_ARG_INFO(0, namespaceURI)
00047        ZEND_ARG_INFO(0, qualifiedName)
00048        ZEND_ARG_OBJ_INFO(0, docType, DOMDocumentType, 0)
00049 ZEND_END_ARG_INFO();
00050 /* }}} */
00051 
00052 /*
00053 * class DOMImplementation 
00054 *
00055 * URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#ID-102161490
00056 * Since: 
00057 */
00058 
00059 const zend_function_entry php_dom_domimplementation_class_functions[] = {
00060        PHP_ME(domimplementation, getFeature, arginfo_dom_implementation_get_feature, ZEND_ACC_PUBLIC|ZEND_ACC_ALLOW_STATIC)
00061        PHP_ME(domimplementation, hasFeature, arginfo_dom_implementation_has_feature, ZEND_ACC_PUBLIC|ZEND_ACC_ALLOW_STATIC)
00062        PHP_ME(domimplementation, createDocumentType, arginfo_dom_implementation_create_documenttype, ZEND_ACC_PUBLIC|ZEND_ACC_ALLOW_STATIC)
00063        PHP_ME(domimplementation, createDocument, arginfo_dom_implementation_create_document, ZEND_ACC_PUBLIC|ZEND_ACC_ALLOW_STATIC)
00064        PHP_FE_END
00065 };
00066 
00067 /* {{{ proto boolean dom_domimplementation_has_feature(string feature, string version);
00068 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#ID-5CED94D7
00069 Since: 
00070 */
00071 PHP_METHOD(domimplementation, hasFeature)
00072 {
00073        int feature_len, version_len;
00074        char *feature, *version;
00075 
00076        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", &feature, &feature_len, &version, &version_len) == FAILURE) {
00077               return;
00078        }
00079 
00080        if (dom_has_feature(feature, version)) {
00081               RETURN_TRUE;
00082        } else {
00083               RETURN_FALSE;
00084        }
00085 }
00086 /* }}} end dom_domimplementation_has_feature */
00087 
00088 /* {{{ proto DOMDocumentType dom_domimplementation_create_document_type(string qualifiedName, string publicId, string systemId);
00089 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#Level-2-Core-DOM-createDocType
00090 Since: DOM Level 2
00091 */
00092 PHP_METHOD(domimplementation, createDocumentType)
00093 {
00094        zval *rv = NULL;
00095        xmlDtd *doctype;
00096        int ret, name_len = 0, publicid_len = 0, systemid_len = 0;
00097        char *name = NULL, *publicid = NULL, *systemid = NULL;
00098        xmlChar *pch1 = NULL, *pch2 = NULL, *localname = NULL;
00099        xmlURIPtr uri;
00100 
00101        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|sss", &name, &name_len, &publicid, &publicid_len, &systemid, &systemid_len) == FAILURE) {
00102               return;
00103        }
00104 
00105        if (name_len == 0) {
00106               php_error_docref(NULL TSRMLS_CC, E_WARNING, "qualifiedName is required");
00107               RETURN_FALSE;
00108        }
00109 
00110        if (publicid_len > 0)
00111               pch1 = publicid;
00112        if (systemid_len > 0)
00113               pch2 = systemid;
00114 
00115        uri = xmlParseURI(name);
00116        if (uri != NULL && uri->opaque != NULL) {
00117               localname = xmlStrdup(uri->opaque);
00118               if (xmlStrchr(localname, (xmlChar) ':') != NULL) {
00119                      php_dom_throw_error(NAMESPACE_ERR, 1 TSRMLS_CC);
00120                      xmlFreeURI(uri);
00121                      xmlFree(localname);
00122                      RETURN_FALSE;
00123               }
00124        } else {
00125               localname = xmlStrdup(name);
00126        }
00127 
00128        /* TODO: Test that localname has no invalid chars 
00129        php_dom_throw_error(INVALID_CHARACTER_ERR, TSRMLS_CC);
00130        */
00131 
00132        if (uri) {
00133               xmlFreeURI(uri);
00134        }
00135 
00136        doctype = xmlCreateIntSubset(NULL, localname, pch1, pch2);
00137        xmlFree(localname);
00138 
00139        if (doctype == NULL) {
00140               php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to create DocumentType");
00141               RETURN_FALSE;
00142        }
00143 
00144        DOM_RET_OBJ(rv, (xmlNodePtr) doctype, &ret, NULL);
00145 }
00146 /* }}} end dom_domimplementation_create_document_type */
00147 
00148 /* {{{ proto DOMDocument dom_domimplementation_create_document(string namespaceURI, string qualifiedName, DOMDocumentType doctype);
00149 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#Level-2-Core-DOM-createDocument
00150 Since: DOM Level 2
00151 */
00152 PHP_METHOD(domimplementation, createDocument)
00153 {
00154        zval *node = NULL, *rv = NULL;
00155        xmlDoc *docp;
00156        xmlNode *nodep;
00157        xmlDtdPtr doctype = NULL;
00158        xmlNsPtr nsptr = NULL;
00159        int ret, uri_len = 0, name_len = 0, errorcode = 0;
00160        char *uri = NULL, *name = NULL;
00161        char *prefix = NULL, *localname = NULL;
00162        dom_object *doctobj;
00163 
00164        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|ssO", &uri, &uri_len, &name, &name_len, &node, dom_documenttype_class_entry) == FAILURE) {
00165               return;
00166        }
00167 
00168        if (node != NULL) {
00169               DOM_GET_OBJ(doctype, node, xmlDtdPtr, doctobj);
00170               if (doctype->type == XML_DOCUMENT_TYPE_NODE) {
00171                      php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid DocumentType object");
00172                      RETURN_FALSE;
00173               }
00174               if (doctype->doc != NULL) {
00175                      php_dom_throw_error(WRONG_DOCUMENT_ERR, 1 TSRMLS_CC);
00176                      RETURN_FALSE;
00177               }
00178        } else {
00179               doctobj = NULL;
00180        }
00181 
00182        if (name_len > 0) {
00183               errorcode = dom_check_qname(name, &localname, &prefix, 1, name_len);
00184               if (errorcode == 0 && uri_len > 0 && ((nsptr = xmlNewNs(NULL, uri, prefix)) == NULL)) {
00185                      errorcode = NAMESPACE_ERR;
00186               }
00187        }
00188 
00189        if (prefix != NULL) {
00190               xmlFree(prefix);
00191        }
00192 
00193        if (errorcode != 0) {
00194               if (localname != NULL) {
00195                      xmlFree(localname);
00196               }
00197               php_dom_throw_error(errorcode, 1 TSRMLS_CC);
00198               RETURN_FALSE;
00199        }
00200 
00201        /* currently letting libxml2 set the version string */
00202        docp = xmlNewDoc(NULL);
00203        if (!docp) {
00204               if (localname != NULL) {
00205                      xmlFree(localname);
00206               }
00207               RETURN_FALSE;
00208        }
00209 
00210        if (doctype != NULL) {
00211               docp->intSubset = doctype;
00212               doctype->parent = docp;
00213               doctype->doc = docp;
00214               docp->children = (xmlNodePtr) doctype;
00215               docp->last = (xmlNodePtr) doctype;
00216        }
00217 
00218        if (localname != NULL) {
00219               nodep = xmlNewDocNode (docp, nsptr, localname, NULL);
00220               if (!nodep) {
00221                      if (doctype != NULL) {
00222                             docp->intSubset = NULL;
00223                             doctype->parent = NULL;
00224                             doctype->doc = NULL;
00225                             docp->children = NULL;
00226                             docp->last = NULL;
00227                      }
00228                      xmlFreeDoc(docp);
00229                      xmlFree(localname);
00230                      /* Need some type of error here */
00231                      php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unexpected Error");
00232                      RETURN_FALSE;
00233               }
00234 
00235               nodep->nsDef = nsptr;
00236 
00237               xmlDocSetRootElement(docp, nodep);
00238               xmlFree(localname);
00239        }
00240 
00241        DOM_RET_OBJ(rv, (xmlNodePtr) docp, &ret, NULL);
00242 
00243        if (doctobj != NULL) {
00244               doctobj->document = ((dom_object *)((php_libxml_node_ptr *)docp->_private)->_private)->document;
00245               php_libxml_increment_doc_ref((php_libxml_node_object *)doctobj, docp TSRMLS_CC);
00246        }
00247 }
00248 /* }}} end dom_domimplementation_create_document */
00249 
00250 /* {{{ proto DOMNode dom_domimplementation_get_feature(string feature, string version);
00251 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#DOMImplementation3-getFeature
00252 Since: DOM Level 3
00253 */
00254 PHP_METHOD(domimplementation, getFeature)
00255 {
00256  DOM_NOT_IMPLEMENTED();
00257 }
00258 /* }}} end dom_domimplementation_get_feature */
00259 
00260 #endif
00261 
00262 /*
00263  * Local variables:
00264  * tab-width: 4
00265  * c-basic-offset: 4
00266  * End:
00267  * vim600: noet sw=4 ts=4 fdm=marker
00268  * vim<600: noet sw=4 ts=4
00269  */