]> granicus.if.org Git - php/commitdiff
fix __clone()
authorSterling Hughes <sterling@php.net>
Tue, 27 May 2003 22:15:17 +0000 (22:15 +0000)
committerSterling Hughes <sterling@php.net>
Tue, 27 May 2003 22:15:17 +0000 (22:15 +0000)
add schema support

ext/simplexml/php_simplexml.h
ext/simplexml/simplexml.c

index 2861ab9753c11226474494ab7c4daa35ba16d19e..e3a754baa39ffd2043980cfb716049041abc6441 100644 (file)
@@ -43,6 +43,7 @@ extern zend_module_entry simplexml_module_entry;
 #include <libxml/xpath.h>
 #include <libxml/xpathInternals.h>
 #include <libxml/xpointer.h>
+#include <libxml/xmlschemas.h>
 
 PHP_MINIT_FUNCTION(simplexml);
 PHP_MSHUTDOWN_FUNCTION(simplexml);
index 466c38b7fb893930e2eb156a552f4f7f15046d45..cbe2b5fd121853251f57d342b47f105866e4d1cf 100644 (file)
@@ -478,6 +478,9 @@ simplexml_ce_xpath_search(INTERNAL_FUNCTION_PARAMETERS)
        if (!sxe->xpath) {
                sxe->xpath = xmlXPathNewContext(sxe->document);
        }
+       if (!sxe->node) {
+               sxe->node = xmlDocGetRootElement(sxe->document);
+       }
        sxe->xpath->node = sxe->node;
 
        result = xmlXPathEval(query, sxe->xpath)->nodesetval;
@@ -505,6 +508,56 @@ simplexml_ce_xpath_search(INTERNAL_FUNCTION_PARAMETERS)
 }
 /* }}} */
 
+#define SCHEMA_FILE 0
+#define SCHEMA_BLOB 1
+#define SCHEMA_OBJECT 2
+
+/* {{{ simplexml_ce_schema_validate_file()
+ */
+static void
+simplexml_ce_schema_validate(INTERNAL_FUNCTION_PARAMETERS, int type)
+{
+       php_sxe_object         *sxe;
+       zval                   *source;
+       xmlSchemaParserCtxtPtr  parser;
+       xmlSchemaPtr            sptr;
+       xmlSchemaValidCtxtPtr   vptr;
+       int                     is_valid;
+       
+       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &source) == FAILURE) {
+               return;
+       }
+
+       sxe = php_sxe_fetch_object(getThis() TSRMLS_CC);
+
+       switch (type) {
+               case SCHEMA_FILE:
+                       convert_to_string_ex(&source);
+                       parser = xmlSchemaNewParserCtxt(Z_STRVAL_P(source));
+                       sptr = xmlSchemaParse(parser);
+                       xmlSchemaFreeParserCtxt(parser);
+                       break;
+               case SCHEMA_BLOB:
+                       convert_to_string_ex(&source);
+                       parser = xmlSchemaNewMemParserCtxt(Z_STRVAL_P(source), Z_STRLEN_P(source));
+                       sptr = xmlSchemaParse(parser);
+                       xmlSchemaFreeParserCtxt(parser);
+                       break;
+       }
+
+       vptr = xmlSchemaNewValidCtxt(sptr);
+       is_valid = xmlSchemaValidateDoc(vptr, sxe->document);
+       xmlSchemaFree(sptr);
+       xmlSchemaFreeValidCtxt(vptr);
+
+       if (is_valid) {
+               RETURN_TRUE;
+       } else {
+               RETURN_FALSE;
+       }
+}
+/* }}} */
+
 
 /* {{{ sxe_call_method()
  */
@@ -513,6 +566,10 @@ sxe_call_method(char *method, INTERNAL_FUNCTION_PARAMETERS)
 {
        if (!strcmp(method, "xsearch")) {
                simplexml_ce_xpath_search(INTERNAL_FUNCTION_PARAM_PASSTHRU);
+       } else if (!strcmp(method, "validate_schema_file")) {
+               simplexml_ce_schema_validate(INTERNAL_FUNCTION_PARAM_PASSTHRU, SCHEMA_FILE);    
+       } else if (!strcmp(method, "validate_schema_buffer")) {
+               simplexml_ce_schema_validate(INTERNAL_FUNCTION_PARAM_PASSTHRU, SCHEMA_BLOB);
        } else {
                RETVAL_NULL();
        }
@@ -650,15 +707,7 @@ sxe_object_clone(void *object, void **clone_ptr TSRMLS_DC)
 
        clone = php_sxe_object_new(TSRMLS_C);
 
-       /**
-        * XXX: Change parts of the code not to rely on sxe->document
-        * being set.
-        */
-       if (xmlDocGetRootElement(sxe->document) == sxe->node) {
-               clone->document = xmlCopyDoc(sxe->document, 1);
-       } else {
-               clone->node = xmlCopyNode(sxe->node, 0);
-       }
+       clone->document = xmlCopyDoc(sxe->document, 1);
 
        *clone_ptr = (void *) clone;
 }