#include "php_soap.h" #include "libxml/uri.h" static int schema_simpleType(sdlPtr sdl, xmlAttrPtr tsn, xmlNodePtr simpleType, sdlTypePtr cur_type); static int schema_complexType(sdlPtr sdl, xmlAttrPtr tsn, xmlNodePtr compType, sdlTypePtr cur_type); static int schema_list(sdlPtr sdl, xmlAttrPtr tsn, xmlNodePtr listType, sdlTypePtr cur_type); static int schema_union(sdlPtr sdl, xmlAttrPtr tsn, xmlNodePtr unionType, sdlTypePtr cur_type); static int schema_simpleContent(sdlPtr sdl, xmlAttrPtr tsn, xmlNodePtr simpCompType, sdlTypePtr cur_type); static int schema_restriction_simpleContent(sdlPtr sdl, xmlAttrPtr tsn, xmlNodePtr restType, sdlTypePtr cur_type, int simpleType); static int schema_restriction_complexContent(sdlPtr sdl, xmlAttrPtr tsn, xmlNodePtr restType, sdlTypePtr cur_type); static int schema_extension_simpleContent(sdlPtr sdl, xmlAttrPtr tsn, xmlNodePtr extType, sdlTypePtr cur_type); static int schema_extension_complexContent(sdlPtr sdl, xmlAttrPtr tsn, xmlNodePtr extType, sdlTypePtr cur_type); static int schema_sequence(sdlPtr sdl, xmlAttrPtr tsn, xmlNodePtr seqType, sdlTypePtr cur_type, sdlContentModelPtr model); static int schema_all(sdlPtr sdl, xmlAttrPtr tsn, xmlNodePtr extType, sdlTypePtr cur_type, sdlContentModelPtr model); static int schema_choice(sdlPtr sdl, xmlAttrPtr tsn, xmlNodePtr choiceType, sdlTypePtr cur_type, sdlContentModelPtr model); static int schema_group(sdlPtr sdl, xmlAttrPtr tsn, xmlNodePtr groupType, sdlTypePtr cur_type, sdlContentModelPtr model); static int schema_any(sdlPtr sdl, xmlAttrPtr tsn, xmlNodePtr extType, sdlTypePtr cur_type, sdlContentModelPtr model); static int schema_element(sdlPtr sdl, xmlAttrPtr tsn, xmlNodePtr element, sdlTypePtr cur_type, sdlContentModelPtr model); static int schema_attribute(sdlPtr sdl, xmlAttrPtr tsn, xmlNodePtr attrType, sdlTypePtr cur_type); static int schema_attributeGroup(sdlPtr sdl, xmlAttrPtr tsn, xmlNodePtr attrType, sdlTypePtr cur_type); static int schema_restriction_var_int(xmlNodePtr val, sdlRestrictionIntPtr *valptr); static int schema_restriction_var_char(xmlNodePtr val, sdlRestrictionCharPtr *valptr); static void schema_type_fixup(sdlPtr sdl, sdlTypePtr type); static void delete_model(void *handle); static void delete_type(void *data); static void delete_attribute(void *attribute); static void delete_restriction_var_int(void *rvi); static void delete_schema_restriction_var_char(void *srvc); static encodePtr create_encoder(sdlPtr sdl, sdlTypePtr cur_type, const char *ns, const char *type) { smart_str nscat = {0}; encodePtr enc, *enc_ptr; if (sdl->encoders == NULL) { sdl->encoders = malloc(sizeof(HashTable)); zend_hash_init(sdl->encoders, 0, NULL, delete_encoder, 1); } smart_str_appends(&nscat, ns); smart_str_appendc(&nscat, ':'); smart_str_appends(&nscat, type); smart_str_0(&nscat); if (zend_hash_find(sdl->encoders, nscat.c, nscat.len + 1, (void**)&enc_ptr) == SUCCESS) { enc = *enc_ptr; if (enc->details.ns) { free(enc->details.ns); } if (enc->details.type_str) { free(enc->details.type_str); } } else { enc_ptr = NULL; enc = malloc(sizeof(encode)); } memset(enc, 0, sizeof(encode)); enc->details.ns = strdup(ns); enc->details.type_str = strdup(type); enc->details.sdl_type = cur_type; enc->to_xml = sdl_guess_convert_xml; enc->to_zval = sdl_guess_convert_zval; if (enc_ptr == NULL) { zend_hash_update(sdl->encoders, nscat.c, nscat.len + 1, &enc, sizeof(encodePtr), NULL); } smart_str_free(&nscat); return enc; } static encodePtr get_create_encoder(sdlPtr sdl, sdlTypePtr cur_type, const char *ns, const char *type) { encodePtr enc = NULL; smart_str nscat = {0}; TSRMLS_FETCH(); smart_str_appends(&nscat, ns); smart_str_appendc(&nscat, ':'); smart_str_appends(&nscat, type); smart_str_0(&nscat); enc = get_conversion_from_href_type(nscat.c); if (enc == NULL) { enc = get_conversion_from_href_type_ex(sdl->encoders, nscat.c, nscat.len); } if (enc == NULL) { enc = create_encoder(sdl, cur_type, ns, type); } smart_str_free(&nscat); return enc; } static int is_blank(const char* str) { while (*str != '\0') { if (*str != ' ' && *str != 0x9 && *str != 0xa && *str != 0xd) { return 0; } str++; } return 1; } /* removes all empty text, comments and other insignoficant nodes */ static void schema_cleanup(xmlNodePtr schema) { xmlNodePtr trav; xmlNodePtr del = NULL; trav = schema->children; while (trav != NULL) { if (del != NULL) { xmlUnlinkNode(del); xmlFreeNode(del); del = NULL; } if (trav->type == XML_TEXT_NODE) { if (is_blank(trav->content)) { del = trav; } } else if ((trav->type != XML_ELEMENT_NODE) && (trav->type != XML_CDATA_SECTION_NODE)) { del = trav; } else if (trav->children != NULL) { schema_cleanup(trav); } trav = trav->next; } if (del != NULL) { xmlUnlinkNode(del); xmlFreeNode(del); } } static void schema_load_file(sdlPtr sdl, xmlAttrPtr ns, xmlChar *location, xmlAttrPtr tns, int import) { if (location != NULL && !zend_hash_exists(&sdl->docs, location, strlen(location)+1)) { xmlDocPtr doc; xmlNodePtr schema; xmlAttrPtr new_tns; doc = xmlParseFile(location); xmlCleanupParser(); if (doc == NULL) { php_error(E_ERROR, "Error parsing schema (can't import schema from '%s')",location); } schema = get_node(doc->children, "schema"); if (schema == NULL) { xmlFreeDoc(doc); php_error(E_ERROR, "Error parsing schema (can't import schema from '%s')",location); } new_tns = get_attribute(schema->properties, "targetNamespace"); if (import) { if (ns != NULL && (new_tns == NULL || strcmp(ns->children->content,new_tns->children->content) != 0)) { xmlFreeDoc(doc); php_error(E_ERROR, "Error parsing schema (can't import schema from '%s', unexpected 'targetNamespace'='%s')",location,new_tns->children->content); } if (ns == NULL && new_tns != NULL) { xmlFreeDoc(doc); php_error(E_ERROR, "Error parsing schema (can't import schema from '%s', unexpected 'targetNamespace'='%s')",location,new_tns->children->content); } } else { new_tns = get_attribute(schema->properties, "targetNamespace"); if (new_tns == NULL) { if (tns != NULL) { xmlFreeDoc(doc); xmlSetProp(schema, "targetNamespace", tns->children->content); } } else if (tns != NULL && strcmp(tns->children->content,new_tns->children->content) != 0) { xmlFreeDoc(doc); php_error(E_ERROR, "Error parsing schema (can't include schema from '%s', different 'targetNamespace')",location); } } zend_hash_add(&sdl->docs, location, strlen(location)+1, (void**)&doc, sizeof(xmlDocPtr), NULL); load_schema(sdl, schema); } } /* 2.6.1 xsi:type 2.6.2 xsi:nil 2.6.3 xsi:schemaLocation, xsi:noNamespaceSchemaLocation */ /* Content: ((include | import | redefine | annotation)*, (((simpleType | complexType | group | attributeGroup) | element | attribute | notation), annotation*)*) */ int load_schema(sdlPtr sdl,xmlNodePtr schema) { xmlNodePtr trav; xmlAttrPtr tns; schema_cleanup(schema); if (!sdl->types) { sdl->types = malloc(sizeof(HashTable)); zend_hash_init(sdl->types, 0, NULL, delete_type, 1); } if (!sdl->elements) { sdl->elements = malloc(sizeof(HashTable)); zend_hash_init(sdl->elements, 0, NULL, delete_type, 1); } tns = get_attribute(schema->properties, "targetNamespace"); trav = schema->children; while (trav != NULL) { if (node_is_equal(trav,"include")) { xmlAttrPtr location; location = get_attribute(trav->properties, "schemaLocation"); if (location == NULL) { php_error(E_ERROR, "Error parsing schema (include has no 'schemaLocation' attribute)"); } else { xmlChar *uri; xmlChar *base = xmlNodeGetBase(trav->doc, trav); if (base == NULL) { uri = xmlBuildURI(location->children->content, trav->doc->URL); } else { uri = xmlBuildURI(location->children->content, base); xmlFree(base); } schema_load_file(sdl,NULL,uri,tns,0); xmlFree(uri); } } else if (node_is_equal(trav,"redefine")) { xmlAttrPtr location; location = get_attribute(trav->properties, "schemaLocation"); if (location == NULL) { php_error(E_ERROR, "Error parsing schema (redefine has no 'schemaLocation' attribute)"); } else { xmlChar *uri; xmlChar *base = xmlNodeGetBase(trav->doc, trav); if (base == NULL) { uri = xmlBuildURI(location->children->content, trav->doc->URL); } else { uri = xmlBuildURI(location->children->content, base); xmlFree(base); } schema_load_file(sdl,NULL,uri,tns,0); xmlFree(uri); /* TODO: support */ } } else if (node_is_equal(trav,"import")) { xmlAttrPtr ns, location; xmlChar *uri = NULL; ns = get_attribute(trav->properties, "namespace"); location = get_attribute(trav->properties, "schemaLocation"); if (ns != NULL && tns != NULL && strcmp(ns->children->content,tns->children->content) == 0) { php_error(E_ERROR, "Error parsing schema (can't import schema from '%s', namespace must not match the enclosing schema 'targetNamespace')",location->children->content); } if (location) { xmlChar *base = xmlNodeGetBase(trav->doc, trav); if (base == NULL) { uri = xmlBuildURI(location->children->content, trav->doc->URL); } else { uri = xmlBuildURI(location->children->content, base); xmlFree(base); } } schema_load_file(sdl,ns,uri,tns,1); if (uri != NULL) {xmlFree(uri);} } else if (node_is_equal(trav,"annotation")) { /* TODO: support */ /* annotation cleanup xmlNodePtr tmp = trav; trav = trav->next; xmlUnlinkNode(tmp); xmlFreeNode(tmp); continue; */ } else { break; } trav = trav->next; } while (trav != NULL) { if (node_is_equal(trav,"simpleType")) { schema_simpleType(sdl, tns, trav, NULL); } else if (node_is_equal(trav,"complexType")) { schema_complexType(sdl, tns, trav, NULL); } else if (node_is_equal(trav,"group")) { schema_group(sdl, tns, trav, NULL, NULL); } else if (node_is_equal(trav,"attributeGroup")) { schema_attributeGroup(sdl, tns, trav, NULL); } else if (node_is_equal(trav,"element")) { schema_element(sdl, tns, trav, NULL, NULL); } else if (node_is_equal(trav,"attribute")) { schema_attribute(sdl, tns, trav, NULL); } else if (node_is_equal(trav,"notation")) { /* TODO: support */ } else if (node_is_equal(trav,"annotation")) { /* TODO: support */ } else { php_error(E_ERROR, "Error parsing schema (unexpected <%s> in schema)",trav->name); } trav = trav->next; } return TRUE; } /* Content: (annotation?, (restriction | list | union)) */ static int schema_simpleType(sdlPtr sdl, xmlAttrPtr tsn, xmlNodePtr simpleType, sdlTypePtr cur_type) { xmlNodePtr trav; xmlAttrPtr name, ns; ns = get_attribute(simpleType->properties, "targetNamespace"); if (ns == NULL) { ns = tsn; } name = get_attribute(simpleType->properties, "name"); if (cur_type != NULL) { /* Anonymous type inside or */ sdlTypePtr newType, *ptr; newType = malloc(sizeof(sdlType)); memset(newType, 0, sizeof(sdlType)); newType->kind = XSD_TYPEKIND_SIMPLE; if (name != NULL) { newType->name = strdup(name->children->content); newType->namens = strdup(ns->children->content); } else { newType->name = strdup(cur_type->name); newType->namens = strdup(cur_type->namens); } zend_hash_next_index_insert(sdl->types, &newType, sizeof(sdlTypePtr), (void **)&ptr); if (sdl->encoders == NULL) { sdl->encoders = malloc(sizeof(HashTable)); zend_hash_init(sdl->encoders, 0, NULL, delete_encoder, 1); } cur_type->encode = malloc(sizeof(encode)); memset(cur_type->encode, 0, sizeof(encode)); cur_type->encode->details.ns = strdup(newType->namens); cur_type->encode->details.type_str = strdup(newType->name); cur_type->encode->details.sdl_type = *ptr; cur_type->encode->to_xml = sdl_guess_convert_xml; cur_type->encode->to_zval = sdl_guess_convert_zval; zend_hash_next_index_insert(sdl->encoders, &cur_type->encode, sizeof(encodePtr), NULL); cur_type =*ptr; } else if (name != NULL) { sdlTypePtr newType, *ptr; newType = malloc(sizeof(sdlType)); memset(newType, 0, sizeof(sdlType)); newType->kind = XSD_TYPEKIND_SIMPLE; newType->name = strdup(name->children->content); newType->namens = strdup(ns->children->content); if (cur_type == NULL) { zend_hash_next_index_insert(sdl->types, &newType, sizeof(sdlTypePtr), (void **)&ptr); } else { if (cur_type->elements == NULL) { cur_type->elements = malloc(sizeof(HashTable)); zend_hash_init(cur_type->elements, 0, NULL, delete_type, 1); } zend_hash_update(cur_type->elements, newType->name, strlen(newType->name)+1, &newType, sizeof(sdlTypePtr), (void **)&ptr); } cur_type = (*ptr); create_encoder(sdl, cur_type, ns->children->content, name->children->content); } else { php_error(E_ERROR, "Error parsing schema (simpleType has no 'name' attribute)"); } trav = simpleType->children; if (trav != NULL && node_is_equal(trav,"annotation")) { /* TODO: support */ trav = trav->next; } if (trav != NULL) { if (node_is_equal(trav,"restriction")) { schema_restriction_simpleContent(sdl, tsn, trav, cur_type, 1); trav = trav->next; } else if (node_is_equal(trav,"list")) { cur_type->kind = XSD_TYPEKIND_LIST; schema_list(sdl, tsn, trav, cur_type); trav = trav->next; } else if (node_is_equal(trav,"union")) { cur_type->kind = XSD_TYPEKIND_UNION; schema_union(sdl, tsn, trav, cur_type); trav = trav->next; } else { php_error(E_ERROR, "Error parsing schema (unexpected <%s> in simpleType)",trav->name); } } else { php_error(E_ERROR, "Error parsing schema (expected , or in simpleType)"); } if (trav != NULL) { php_error(E_ERROR, "Error parsing schema (unexpected <%s> in simpleType)",trav->name); } return TRUE; } /* Content: (annotation?, (simpleType?)) */ static int schema_list(sdlPtr sdl, xmlAttrPtr tsn, xmlNodePtr listType, sdlTypePtr cur_type) { xmlNodePtr trav; xmlAttrPtr itemType; itemType = get_attribute(listType->properties, "itemType"); if (itemType != NULL) { char *type, *ns; xmlNsPtr nsptr; parse_namespace(itemType->children->content, &type, &ns); nsptr = xmlSearchNs(listType->doc, listType, ns); if (nsptr != NULL) { sdlTypePtr newType, *tmp; newType = malloc(sizeof(sdlType)); memset(newType, 0, sizeof(sdlType)); newType->name = strdup(type); newType->namens = strdup(nsptr->href); newType->encode = get_create_encoder(sdl, newType, (char *)nsptr->href, type); if (cur_type->elements == NULL) { cur_type->elements = malloc(sizeof(HashTable)); zend_hash_init(cur_type->elements, 0, NULL, delete_type, 1); } zend_hash_next_index_insert(cur_type->elements, &newType, sizeof(sdlTypePtr), (void **)&tmp); } if (type) {efree(type);} if (ns) {efree(ns);} } trav = listType->children; if (trav != NULL && node_is_equal(trav,"annotation")) { /* TODO: support */ trav = trav->next; } if (trav != NULL && node_is_equal(trav,"simpleType")) { sdlTypePtr newType, *tmp; if (itemType != NULL) { php_error(E_ERROR, "Error parsing schema (element have both 'itemType' attribute and subtype)"); } newType = malloc(sizeof(sdlType)); memset(newType, 0, sizeof(sdlType)); newType->name = strdup("anonymous"); newType->namens = strdup(tsn->children->content); if (cur_type->elements == NULL) { cur_type->elements = malloc(sizeof(HashTable)); zend_hash_init(cur_type->elements, 0, NULL, delete_type, 1); } zend_hash_next_index_insert(cur_type->elements, &newType, sizeof(sdlTypePtr), (void **)&tmp); schema_simpleType(sdl, tsn, trav, newType); trav = trav->next; } if (trav != NULL) { php_error(E_ERROR, "Error parsing schema (unexpected <%s> in list)",trav->name); } return TRUE; } /* Content: (annotation?, (simpleType*)) */ static int schema_union(sdlPtr sdl, xmlAttrPtr tsn, xmlNodePtr unionType, sdlTypePtr cur_type) { xmlNodePtr trav; xmlAttrPtr memberTypes; memberTypes = get_attribute(unionType->properties, "memberTypes"); if (memberTypes != NULL) { char *str, *start, *end, *next; char *type, *ns; xmlNsPtr nsptr; str = estrdup(memberTypes->children->content); whiteSpace_collapse(str); start = str; while (start != NULL && *start != '\0') { end = strchr(start,' '); if (end == NULL) { next = NULL; } else { *end = '\0'; next = end+1; } parse_namespace(start, &type, &ns); nsptr = xmlSearchNs(unionType->doc, unionType, ns); if (nsptr != NULL) { sdlTypePtr newType, *tmp; newType = malloc(sizeof(sdlType)); memset(newType, 0, sizeof(sdlType)); newType->name = strdup(type); newType->namens = strdup(nsptr->href); newType->encode = get_create_encoder(sdl, newType, (char *)nsptr->href, type); if (cur_type->elements == NULL) { cur_type->elements = malloc(sizeof(HashTable)); zend_hash_init(cur_type->elements, 0, NULL, delete_type, 1); } zend_hash_next_index_insert(cur_type->elements, &newType, sizeof(sdlTypePtr), (void **)&tmp); } if (type) {efree(type);} if (ns) {efree(ns);} start = next; } efree(str); } trav = unionType->children; if (trav != NULL && node_is_equal(trav,"annotation")) { /* TODO: support */ trav = trav->next; } while (trav != NULL) { if (node_is_equal(trav,"simpleType")) { sdlTypePtr newType, *tmp; if (memberTypes != NULL) { php_error(E_ERROR, "Error parsing schema (union have both 'memberTypes' attribute and subtypes)"); } newType = malloc(sizeof(sdlType)); memset(newType, 0, sizeof(sdlType)); newType->name = strdup("anonymous"); newType->namens = strdup(tsn->children->content); if (cur_type->elements == NULL) { cur_type->elements = malloc(sizeof(HashTable)); zend_hash_init(cur_type->elements, 0, NULL, delete_type, 1); } zend_hash_next_index_insert(cur_type->elements, &newType, sizeof(sdlTypePtr), (void **)&tmp); schema_simpleType(sdl, tsn, trav, newType); } else { php_error(E_ERROR, "Error parsing schema (unexpected <%s> in union)",trav->name); } trav = trav->next; } if (trav != NULL) { php_error(E_ERROR, "Error parsing schema (unexpected <%s> in union)",trav->name); } return TRUE; } /* Content: (annotation?, (restriction | extension)) */ static int schema_simpleContent(sdlPtr sdl, xmlAttrPtr tsn, xmlNodePtr simpCompType, sdlTypePtr cur_type) { xmlNodePtr trav; trav = simpCompType->children; if (trav != NULL && node_is_equal(trav,"annotation")) { /* TODO: support */ trav = trav->next; } if (trav != NULL) { if (node_is_equal(trav, "restriction")) { cur_type->kind = XSD_TYPEKIND_RESTRICTION; schema_restriction_simpleContent(sdl, tsn, trav, cur_type, 0); trav = trav->next; } else if (node_is_equal(trav, "extension")) { cur_type->kind = XSD_TYPEKIND_EXTENSION; schema_extension_simpleContent(sdl, tsn, trav, cur_type); trav = trav->next; } else { php_error(E_ERROR, "Error parsing schema (unexpected <%s> in simpleContent)",trav->name); } } else { php_error(E_ERROR, "Error parsing schema (expected or in simpleContent)"); } if (trav != NULL) { php_error(E_ERROR, "Error parsing schema (unexpected <%s> in simpleContent)",trav->name); } return TRUE; } /* simpleType: Content: (annotation?, (simpleType?, (minExclusive | minInclusive | maxExclusive | maxInclusive | totalDigits | fractionDigits | length | minLength | maxLength | enumeration | whiteSpace | pattern)*)?) simpleContent: Content: (annotation?, (simpleType?, (minExclusive | minInclusive | maxExclusive | maxInclusive | totalDigits | fractionDigits | length | minLength | maxLength | enumeration | whiteSpace | pattern)*)?, ((attribute | attributeGroup)*, anyAttribute?)) */ static int schema_restriction_simpleContent(sdlPtr sdl, xmlAttrPtr tsn, xmlNodePtr restType, sdlTypePtr cur_type, int simpleType) { xmlNodePtr trav; xmlAttrPtr base; base = get_attribute(restType->properties, "base"); if (base != NULL) { char *type, *ns; xmlNsPtr nsptr; parse_namespace(base->children->content, &type, &ns); nsptr = xmlSearchNs(restType->doc, restType, ns); if (nsptr != NULL) { cur_type->encode = get_create_encoder(sdl, cur_type, (char *)nsptr->href, type); } if (type) {efree(type);} if (ns) {efree(ns);} } else if (!simpleType) { php_error(E_ERROR, "Error parsing schema (restriction has no 'base' attribute)"); } if (cur_type->restrictions == NULL) { cur_type->restrictions = malloc(sizeof(sdlRestrictions)); memset(cur_type->restrictions, 0, sizeof(sdlRestrictions)); } trav = restType->children; if (trav != NULL && node_is_equal(trav, "annotaion")) { /* TODO: support */ trav = trav->next; } if (trav != NULL && node_is_equal(trav, "simpleType")) { schema_simpleType(sdl, tsn, trav, cur_type); trav = trav->next; } while (trav != NULL) { if (!strcmp(trav->name, "minExclusive")) { schema_restriction_var_int(trav, &cur_type->restrictions->minExclusive); } else if (!strcmp(trav->name, "minInclusive")) { schema_restriction_var_int(trav, &cur_type->restrictions->minInclusive); } else if (!strcmp(trav->name, "maxExclusive")) { schema_restriction_var_int(trav, &cur_type->restrictions->maxExclusive); } else if (!strcmp(trav->name, "maxInclusive")) { schema_restriction_var_int(trav, &cur_type->restrictions->maxInclusive); } else if (!strcmp(trav->name, "totalDigits")) { schema_restriction_var_int(trav, &cur_type->restrictions->totalDigits); } else if (!strcmp(trav->name, "fractionDigits")) { schema_restriction_var_int(trav, &cur_type->restrictions->fractionDigits); } else if (!strcmp(trav->name, "length")) { schema_restriction_var_int(trav, &cur_type->restrictions->length); } else if (!strcmp(trav->name, "minLength")) { schema_restriction_var_int(trav, &cur_type->restrictions->minLength); } else if (!strcmp(trav->name, "maxLength")) { schema_restriction_var_int(trav, &cur_type->restrictions->maxLength); } else if (!strcmp(trav->name, "whiteSpace")) { schema_restriction_var_char(trav, &cur_type->restrictions->whiteSpace); } else if (!strcmp(trav->name, "pattern")) { schema_restriction_var_char(trav, &cur_type->restrictions->pattern); } else if (!strcmp(trav->name, "enumeration")) { sdlRestrictionCharPtr enumval = NULL; schema_restriction_var_char(trav, &enumval); if (cur_type->restrictions->enumeration == NULL) { cur_type->restrictions->enumeration = malloc(sizeof(HashTable)); zend_hash_init(cur_type->restrictions->enumeration, 0, NULL, delete_schema_restriction_var_char, 1); } zend_hash_add(cur_type->restrictions->enumeration, enumval->value, strlen(enumval->value)+1, &enumval, sizeof(sdlRestrictionCharPtr), NULL); } else { break; } trav = trav->next; } if (!simpleType) { while (trav != NULL) { if (node_is_equal(trav,"attribute")) { schema_attribute(sdl, tsn, trav, cur_type); } else if (node_is_equal(trav,"attributeGroup")) { schema_attributeGroup(sdl, tsn, trav, cur_type); } else if (node_is_equal(trav,"anyAttribute")) { /* TODO: support */ trav = trav->next; break; } else { php_error(E_ERROR, "Error parsing schema (unexpected <%s> in restriction)",trav->name); } trav = trav->next; } } if (trav != NULL) { php_error(E_ERROR, "Error parsing schema (unexpected <%s> in restriction)",trav->name); } return TRUE; } /* Content: (annotation?, (group | all | choice | sequence)?, ((attribute | attributeGroup)*, anyAttribute?)) */ static int schema_restriction_complexContent(sdlPtr sdl, xmlAttrPtr tsn, xmlNodePtr restType, sdlTypePtr cur_type) { xmlAttrPtr base; xmlNodePtr trav; base = get_attribute(restType->properties, "base"); if (base != NULL) { char *type, *ns; xmlNsPtr nsptr; parse_namespace(base->children->content, &type, &ns); nsptr = xmlSearchNs(restType->doc, restType, ns); if (nsptr != NULL) { cur_type->encode = get_create_encoder(sdl, cur_type, (char *)nsptr->href, type); } if (type) {efree(type);} if (ns) {efree(ns);} } else { php_error(E_ERROR, "Error parsing schema (restriction has no 'base' attribute)"); } trav = restType->children; if (trav != NULL && node_is_equal(trav,"annotation")) { /* TODO: support */ trav = trav->next; } if (trav != NULL) { if (node_is_equal(trav,"group")) { schema_group(sdl, tsn, trav, cur_type, NULL); trav = trav->next; } else if (node_is_equal(trav,"all")) { schema_all(sdl, tsn, trav, cur_type, NULL); trav = trav->next; } else if (node_is_equal(trav,"choice")) { schema_choice(sdl, tsn, trav, cur_type, NULL); trav = trav->next; } else if (node_is_equal(trav,"sequence")) { schema_sequence(sdl, tsn, trav, cur_type, NULL); trav = trav->next; } } while (trav != NULL) { if (node_is_equal(trav,"attribute")) { schema_attribute(sdl, tsn, trav, cur_type); } else if (node_is_equal(trav,"attributeGroup")) { schema_attributeGroup(sdl, tsn, trav, cur_type); } else if (node_is_equal(trav,"anyAttribute")) { /* TODO: support */ trav = trav->next; break; } else { php_error(E_ERROR, "Error parsing schema (unexpected <%s> in restriction)",trav->name); } trav = trav->next; } if (trav != NULL) { php_error(E_ERROR, "Error parsing schema (unexpected <%s> in restriction)",trav->name); } return TRUE; } static int schema_restriction_var_int(xmlNodePtr val, sdlRestrictionIntPtr *valptr) { xmlAttrPtr fixed, value; if ((*valptr) == NULL) { (*valptr) = malloc(sizeof(sdlRestrictionInt)); } memset((*valptr), 0, sizeof(sdlRestrictionInt)); fixed = get_attribute(val->properties, "fixed"); (*valptr)->fixed = FALSE; if (fixed != NULL) { if (!strcmp(fixed->children->content, "true") || !strcmp(fixed->children->content, "1")) (*valptr)->fixed = TRUE; } value = get_attribute(val->properties, "value"); if (value == NULL) { php_error(E_ERROR, "Error parsing wsdl (missing restriction value)"); } (*valptr)->value = atoi(value->children->content); return TRUE; } static int schema_restriction_var_char(xmlNodePtr val, sdlRestrictionCharPtr *valptr) { xmlAttrPtr fixed, value; if ((*valptr) == NULL) { (*valptr) = malloc(sizeof(sdlRestrictionChar)); } memset((*valptr), 0, sizeof(sdlRestrictionChar)); fixed = get_attribute(val->properties, "fixed"); (*valptr)->fixed = FALSE; if (fixed != NULL) { if (!strcmp(fixed->children->content, "true") || !strcmp(fixed->children->content, "1")) { (*valptr)->fixed = TRUE; } } value = get_attribute(val->properties, "value"); if (value == NULL) { php_error(E_ERROR, "Error parsing wsdl (missing restriction value)"); } (*valptr)->value = strdup(value->children->content); return TRUE; } /* From simpleContent (not supported): Content: (annotation?, ((attribute | attributeGroup)*, anyAttribute?)) */ static int schema_extension_simpleContent(sdlPtr sdl, xmlAttrPtr tsn, xmlNodePtr extType, sdlTypePtr cur_type) { xmlNodePtr trav; xmlAttrPtr base; base = get_attribute(extType->properties, "base"); if (base != NULL) { char *type, *ns; xmlNsPtr nsptr; parse_namespace(base->children->content, &type, &ns); nsptr = xmlSearchNs(extType->doc, extType, ns); if (nsptr != NULL) { cur_type->encode = get_create_encoder(sdl, cur_type, (char *)nsptr->href, type); } if (type) {efree(type);} if (ns) {efree(ns);} } else { php_error(E_ERROR, "Error parsing schema (extension has no 'base' attribute)"); } trav = extType->children; if (trav != NULL && node_is_equal(trav,"annotation")) { /* TODO: support */ trav = trav->next; } while (trav != NULL) { if (node_is_equal(trav,"attribute")) { schema_attribute(sdl, tsn, trav, cur_type); } else if (node_is_equal(trav,"attributeGroup")) { schema_attributeGroup(sdl, tsn, trav, cur_type); } else if (node_is_equal(trav,"anyAttribute")) { /* TODO: support */ trav = trav->next; break; } else { php_error(E_ERROR, "Error parsing schema (unexpected <%s> in extension)",trav->name); } trav = trav->next; } if (trav != NULL) { php_error(E_ERROR, "Error parsing schema (unexpected <%s> in extension)",trav->name); } return TRUE; } /* From complexContent: Content: (annotation?, ((group | all | choice | sequence)?, ((attribute | attributeGroup)*, anyAttribute?))) */ static int schema_extension_complexContent(sdlPtr sdl, xmlAttrPtr tsn, xmlNodePtr extType, sdlTypePtr cur_type) { xmlNodePtr trav; xmlAttrPtr base; base = get_attribute(extType->properties, "base"); if (base != NULL) { char *type, *ns; xmlNsPtr nsptr; parse_namespace(base->children->content, &type, &ns); nsptr = xmlSearchNs(extType->doc, extType, ns); if (nsptr != NULL) { cur_type->encode = get_create_encoder(sdl, cur_type, (char *)nsptr->href, type); } if (type) {efree(type);} if (ns) {efree(ns);} } else { php_error(E_ERROR, "Error parsing schema (extension has no 'base' attribute)"); } trav = extType->children; if (trav != NULL && node_is_equal(trav,"annotation")) { /* TODO: support */ trav = trav->next; } if (trav != NULL) { if (node_is_equal(trav,"group")) { schema_group(sdl, tsn, trav, cur_type, NULL); trav = trav->next; } else if (node_is_equal(trav,"all")) { schema_all(sdl, tsn, trav, cur_type, NULL); trav = trav->next; } else if (node_is_equal(trav,"choice")) { schema_choice(sdl, tsn, trav, cur_type, NULL); trav = trav->next; } else if (node_is_equal(trav,"sequence")) { schema_sequence(sdl, tsn, trav, cur_type, NULL); trav = trav->next; } } while (trav != NULL) { if (node_is_equal(trav,"attribute")) { schema_attribute(sdl, tsn, trav, cur_type); } else if (node_is_equal(trav,"attributeGroup")) { schema_attributeGroup(sdl, tsn, trav, cur_type); } else if (node_is_equal(trav,"anyAttribute")) { /* TODO: support */ trav = trav->next; break; } else { php_error(E_ERROR, "Error parsing schema (unexpected <%s> in extension)",trav->name); } trav = trav->next; } if (trav != NULL) { php_error(E_ERROR, "Error parsing schema (unexpected <%s> in extension)",trav->name); } return TRUE; } /* Content: (annotation?, element*) */ static int schema_all(sdlPtr sdl, xmlAttrPtr tsn, xmlNodePtr all, sdlTypePtr cur_type, sdlContentModelPtr model) { xmlNodePtr trav; xmlAttrPtr attr; sdlContentModelPtr newModel; newModel = malloc(sizeof(sdlContentModel)); newModel->kind = XSD_CONTENT_ALL; newModel->u.content = malloc(sizeof(HashTable)); zend_hash_init(newModel->u.content, 0, NULL, delete_model, 1); if (model == NULL) { cur_type->model = newModel; } else { zend_hash_next_index_insert(model->u.content,&newModel,sizeof(sdlContentModelPtr), NULL); } newModel->min_occurs = 1; newModel->max_occurs = 1; attr = get_attribute(all->properties, "minOccurs"); if (attr) { newModel->min_occurs = atoi(attr->children->content); } attr = get_attribute(all->properties, "maxOccurs"); if (attr) { if (!strcmp(attr->children->content, "unbounded")) { newModel->max_occurs = -1; } else { newModel->max_occurs = atoi(attr->children->content); } } trav = all->children; if (trav != NULL && node_is_equal(trav,"annotation")) { /* TODO: support */ trav = trav->next; } while (trav != NULL) { if (node_is_equal(trav,"element")) { schema_element(sdl, tsn, trav, cur_type, newModel); } else { php_error(E_ERROR, "Error parsing schema (unexpected <%s> in all)",trav->name); } trav = trav->next; } return TRUE; } /* Content: (annotation?) */ static int schema_group(sdlPtr sdl, xmlAttrPtr tsn, xmlNodePtr groupType, sdlTypePtr cur_type, sdlContentModelPtr model) { xmlNodePtr trav; xmlAttrPtr attr; xmlAttrPtr ns, name, ref = NULL; sdlContentModelPtr newModel; ns = get_attribute(groupType->properties, "targetNamespace"); if (ns == NULL) { ns = tsn; } name = get_attribute(groupType->properties, "name"); if (name == NULL) { name = ref = get_attribute(groupType->properties, "ref"); } if (name) { smart_str key = {0}; if (ref) { char *type, *ns; xmlNsPtr nsptr; parse_namespace(ref->children->content, &type, &ns); nsptr = xmlSearchNs(groupType->doc, groupType, ns); if (nsptr != NULL) { smart_str_appends(&key, nsptr->href); smart_str_appendc(&key, ':'); } smart_str_appends(&key, type); smart_str_0(&key); newModel = malloc(sizeof(sdlContentModel)); newModel->kind = XSD_CONTENT_GROUP_REF; newModel->u.group_ref = estrdup(key.c); if (type) {efree(type);} if (ns) {efree(ns);} } else { newModel = malloc(sizeof(sdlContentModel)); newModel->kind = XSD_CONTENT_SEQUENCE; /* will be redefined */ newModel->u.content = malloc(sizeof(HashTable)); zend_hash_init(newModel->u.content, 0, NULL, delete_model, 1); smart_str_appends(&key, ns->children->content); smart_str_appendc(&key, ':'); smart_str_appends(&key, name->children->content); smart_str_0(&key); } if (cur_type == NULL) { sdlTypePtr newType; newType = malloc(sizeof(sdlType)); memset(newType, 0, sizeof(sdlType)); if (sdl->groups == NULL) { sdl->groups = malloc(sizeof(HashTable)); zend_hash_init(sdl->groups, 0, NULL, delete_type, 1); } if (zend_hash_add(sdl->groups, key.c, key.len+1, (void**)&newType, sizeof(sdlTypePtr), NULL) != SUCCESS) { php_error(E_ERROR, "Error parsing schema (group '%s' already defined)",key.c); } cur_type = newType; } smart_str_free(&key); if (model == NULL) { cur_type->model = newModel; } else { zend_hash_next_index_insert(model->u.content, &newModel, sizeof(sdlContentModelPtr), NULL); } } else { php_error(E_ERROR, "Error parsing schema (group has no 'name' nor 'ref' attributes)"); } newModel->min_occurs = 1; newModel->max_occurs = 1; attr = get_attribute(groupType->properties, "minOccurs"); if (attr) { newModel->min_occurs = atoi(attr->children->content); } attr = get_attribute(groupType->properties, "maxOccurs"); if (attr) { if (!strcmp(attr->children->content, "unbounded")) { newModel->max_occurs = -1; } else { newModel->max_occurs = atoi(attr->children->content); } } trav = groupType->children; if (trav != NULL && node_is_equal(trav,"annotation")) { /* TODO: support */ trav = trav->next; } if (trav != NULL) { if (node_is_equal(trav,"choice")) { if (ref != NULL) { php_error(E_ERROR, "Error parsing schema (group have both 'ref' attribute and subcontent)"); } newModel->kind = XSD_CONTENT_CHOICE; schema_choice(sdl, tsn, trav, cur_type, newModel); trav = trav->next; } else if (node_is_equal(trav,"sequence")) { if (ref != NULL) { php_error(E_ERROR, "Error parsing schema (group have both 'ref' attribute and subcontent)"); } newModel->kind = XSD_CONTENT_SEQUENCE; schema_sequence(sdl, tsn, trav, cur_type, newModel); trav = trav->next; } else if (node_is_equal(trav,"all")) { if (ref != NULL) { php_error(E_ERROR, "Error parsing schema (group have both 'ref' attribute and subcontent)"); } newModel->kind = XSD_CONTENT_ALL; schema_all(sdl, tsn, trav, cur_type, newModel); trav = trav->next; } else { php_error(E_ERROR, "Error parsing schema (unexpected <%s> in group)",trav->name); } } if (trav != NULL) { php_error(E_ERROR, "Error parsing schema (unexpected <%s> in group)",trav->name); } return TRUE; } /* Content: (annotation?, (element | group | choice | sequence | any)*) */ static int schema_choice(sdlPtr sdl, xmlAttrPtr tsn, xmlNodePtr choiceType, sdlTypePtr cur_type, sdlContentModelPtr model) { xmlNodePtr trav; xmlAttrPtr attr; sdlContentModelPtr newModel; newModel = malloc(sizeof(sdlContentModel)); newModel->kind = XSD_CONTENT_CHOICE; newModel->u.content = malloc(sizeof(HashTable)); zend_hash_init(newModel->u.content, 0, NULL, delete_model, 1); if (model == NULL) { cur_type->model = newModel; } else { zend_hash_next_index_insert(model->u.content,&newModel,sizeof(sdlContentModelPtr), NULL); } newModel->min_occurs = 1; newModel->max_occurs = 1; attr = get_attribute(choiceType->properties, "minOccurs"); if (attr) { newModel->min_occurs = atoi(attr->children->content); } attr = get_attribute(choiceType->properties, "maxOccurs"); if (attr) { if (!strcmp(attr->children->content, "unbounded")) { newModel->max_occurs = -1; } else { newModel->max_occurs = atoi(attr->children->content); } } trav = choiceType->children; if (trav != NULL && node_is_equal(trav,"annotation")) { /* TODO: support */ trav = trav->next; } while (trav != NULL) { if (node_is_equal(trav,"element")) { schema_element(sdl, tsn, trav, cur_type, newModel); } else if (node_is_equal(trav,"group")) { schema_group(sdl, tsn, trav, cur_type, newModel); } else if (node_is_equal(trav,"choice")) { schema_choice(sdl, tsn, trav, cur_type, newModel); } else if (node_is_equal(trav,"sequence")) { schema_sequence(sdl, tsn, trav, cur_type, newModel); } else if (node_is_equal(trav,"any")) { schema_any(sdl, tsn, trav, cur_type, newModel); } else { php_error(E_ERROR, "Error parsing schema (unexpected <%s> in choice)",trav->name); } trav = trav->next; } return TRUE; } /* Content: (annotation?, (element | group | choice | sequence | any)*) */ static int schema_sequence(sdlPtr sdl, xmlAttrPtr tsn, xmlNodePtr seqType, sdlTypePtr cur_type, sdlContentModelPtr model) { xmlNodePtr trav; xmlAttrPtr attr; sdlContentModelPtr newModel; newModel = malloc(sizeof(sdlContentModel)); newModel->kind = XSD_CONTENT_SEQUENCE; newModel->u.content = malloc(sizeof(HashTable)); zend_hash_init(newModel->u.content, 0, NULL, delete_model, 1); if (model == NULL) { cur_type->model = newModel; } else { zend_hash_next_index_insert(model->u.content,&newModel,sizeof(sdlContentModelPtr), NULL); } newModel->min_occurs = 1; newModel->max_occurs = 1; attr = get_attribute(seqType->properties, "minOccurs"); if (attr) { newModel->min_occurs = atoi(attr->children->content); } attr = get_attribute(seqType->properties, "maxOccurs"); if (attr) { if (!strcmp(attr->children->content, "unbounded")) { newModel->max_occurs = -1; } else { newModel->max_occurs = atoi(attr->children->content); } } trav = seqType->children; if (trav != NULL && node_is_equal(trav,"annotation")) { /* TODO: support */ trav = trav->next; } while (trav != NULL) { if (node_is_equal(trav,"element")) { schema_element(sdl, tsn, trav, cur_type, newModel); } else if (node_is_equal(trav,"group")) { schema_group(sdl, tsn, trav, cur_type, newModel); } else if (node_is_equal(trav,"choice")) { schema_choice(sdl, tsn, trav, cur_type, newModel); } else if (node_is_equal(trav,"sequence")) { schema_sequence(sdl, tsn, trav, cur_type, newModel); } else if (node_is_equal(trav,"any")) { schema_any(sdl, tsn, trav, cur_type, newModel); } else { php_error(E_ERROR, "Error parsing schema (unexpected <%s> in sequence)",trav->name); } trav = trav->next; } return TRUE; } static int schema_any(sdlPtr sdl, xmlAttrPtr tsn, xmlNodePtr extType, sdlTypePtr cur_type, sdlContentModelPtr model) { /* TODO: support */ return TRUE; } /* Content: (annotation?, (restriction | extension)) */ static int schema_complexContent(sdlPtr sdl, xmlAttrPtr tsn, xmlNodePtr compCont, sdlTypePtr cur_type) { xmlNodePtr trav; trav = compCont->children; if (trav != NULL && node_is_equal(trav,"annotation")) { /* TODO: support */ trav = trav->next; } if (trav != NULL) { if (node_is_equal(trav, "restriction")) { cur_type->kind = XSD_TYPEKIND_RESTRICTION; schema_restriction_complexContent(sdl, tsn, trav, cur_type); trav = trav->next; } else if (node_is_equal(trav, "extension")) { cur_type->kind = XSD_TYPEKIND_EXTENSION; schema_extension_complexContent(sdl, tsn, trav, cur_type); trav = trav->next; } else { php_error(E_ERROR, "Error parsing schema (unexpected <%s> in complexContent)",trav->name); } } else { php_error(E_ERROR, "Error parsing schema ( or expected in complexContent)"); } if (trav != NULL) { php_error(E_ERROR, "Error parsing schema (unexpected <%s> in complexContent)", trav->name); } return TRUE; } /* Content: (annotation?, (simpleContent | complexContent | ((group | all | choice | sequence)?, ((attribute | attributeGroup)*, anyAttribute?)))) */ static int schema_complexType(sdlPtr sdl, xmlAttrPtr tsn, xmlNodePtr compType, sdlTypePtr cur_type) { xmlNodePtr trav; xmlAttrPtr attrs, name, ns; TSRMLS_FETCH(); attrs = compType->properties; ns = get_attribute(attrs, "targetNamespace"); if (ns == NULL) { ns = tsn; } name = get_attribute(attrs, "name"); if (cur_type != NULL) { /* Anonymous type inside */ sdlTypePtr newType, *ptr; newType = malloc(sizeof(sdlType)); memset(newType, 0, sizeof(sdlType)); newType->kind = XSD_TYPEKIND_COMPLEX; if (name != NULL) { newType->name = strdup(name->children->content); newType->namens = strdup(ns->children->content); } else { newType->name = strdup(cur_type->name); newType->namens = strdup(cur_type->namens); } zend_hash_next_index_insert(sdl->types, &newType, sizeof(sdlTypePtr), (void **)&ptr); if (sdl->encoders == NULL) { sdl->encoders = malloc(sizeof(HashTable)); zend_hash_init(sdl->encoders, 0, NULL, delete_encoder, 1); } cur_type->encode = malloc(sizeof(encode)); memset(cur_type->encode, 0, sizeof(encode)); cur_type->encode->details.ns = strdup(newType->namens); cur_type->encode->details.type_str = strdup(newType->name); cur_type->encode->details.sdl_type = *ptr; cur_type->encode->to_xml = sdl_guess_convert_xml; cur_type->encode->to_zval = sdl_guess_convert_zval; zend_hash_next_index_insert(sdl->encoders, &cur_type->encode, sizeof(encodePtr), NULL); cur_type =*ptr; } else if (name) { sdlTypePtr newType, *ptr; newType = malloc(sizeof(sdlType)); memset(newType, 0, sizeof(sdlType)); newType->kind = XSD_TYPEKIND_COMPLEX; newType->name = strdup(name->children->content); newType->namens = strdup(ns->children->content); zend_hash_next_index_insert(sdl->types, &newType, sizeof(sdlTypePtr), (void **)&ptr); cur_type = (*ptr); create_encoder(sdl, cur_type, ns->children->content, name->children->content); } else { php_error(E_ERROR, "Error parsing schema (complexType has no 'name' attribute)"); return FALSE; } trav = compType->children; if (trav != NULL && node_is_equal(trav, "annotation")) { /* TODO: support */ trav = trav->next; } if (trav != NULL) { if (node_is_equal(trav,"simpleContent")) { schema_simpleContent(sdl, tsn, trav, cur_type); trav = trav->next; } else if (node_is_equal(trav,"complexContent")) { schema_complexContent(sdl, tsn, trav, cur_type); trav = trav->next; } else { if (node_is_equal(trav,"group")) { schema_group(sdl, tsn, trav, cur_type, NULL); trav = trav->next; } else if (node_is_equal(trav,"all")) { schema_all(sdl, tsn, trav, cur_type, NULL); trav = trav->next; } else if (node_is_equal(trav,"choice")) { schema_choice(sdl, tsn, trav, cur_type, NULL); trav = trav->next; } else if (node_is_equal(trav,"sequence")) { schema_sequence(sdl, tsn, trav, cur_type, NULL); trav = trav->next; } while (trav != NULL) { if (node_is_equal(trav,"attribute")) { schema_attribute(sdl, tsn, trav, cur_type); } else if (node_is_equal(trav,"attributeGroup")) { schema_attributeGroup(sdl, tsn, trav, cur_type); } else if (node_is_equal(trav,"anyAttribute")) { /* TODO: support */ trav = trav->next; break; } else { php_error(E_ERROR, "Error parsing schema ---(unexpected <%s> in complexType)",trav->name); } trav = trav->next; } } } if (trav != NULL) { php_error(E_ERROR, "Error parsing schema +++(unexpected <%s> in complexType)",trav->name); } return TRUE; } /* Content: (annotation?, ((simpleType | complexType)?, (unique | key | keyref)*)) */ static int schema_element(sdlPtr sdl, xmlAttrPtr tsn, xmlNodePtr element, sdlTypePtr cur_type, sdlContentModelPtr model) { xmlNodePtr trav; xmlAttrPtr attrs, attr, ns, name, type, ref = NULL; attrs = element->properties; ns = get_attribute(attrs, "targetNamespace"); if (ns == NULL) { ns = tsn; } name = get_attribute(attrs, "name"); if (name == NULL) { name = ref = get_attribute(attrs, "ref"); } if (name) { HashTable *addHash; sdlTypePtr newType; smart_str key = {0}; newType = malloc(sizeof(sdlType)); memset(newType, 0, sizeof(sdlType)); if (ref) { smart_str nscat = {0}; char *type, *ns; xmlNsPtr nsptr; parse_namespace(ref->children->content, &type, &ns); nsptr = xmlSearchNs(element->doc, element, ns); if (nsptr != NULL) { smart_str_appends(&nscat, nsptr->href); smart_str_appendc(&nscat, ':'); newType->namens = strdup(nsptr->href); } smart_str_appends(&nscat, type); newType->name = strdup(type); smart_str_0(&nscat); if (type) {efree(type);} if (ns) {efree(ns);} newType->ref = estrdup(nscat.c); smart_str_free(&nscat); } else { newType->name = strdup(name->children->content); newType->namens = strdup(ns->children->content); } newType->nillable = FALSE; if (cur_type == NULL) { addHash = sdl->elements; smart_str_appends(&key, newType->namens); smart_str_appendc(&key, ':'); smart_str_appends(&key, newType->name); } else { if (cur_type->elements == NULL) { cur_type->elements = malloc(sizeof(HashTable)); zend_hash_init(cur_type->elements, 0, NULL, delete_type, 1); } addHash = cur_type->elements; smart_str_appends(&key, newType->name); } smart_str_0(&key); if (zend_hash_add(addHash, key.c, key.len + 1, &newType, sizeof(sdlTypePtr), NULL) != SUCCESS) { if (cur_type == NULL) { php_error(E_ERROR, "Error parsing schema (element '%s' already defined)",key.c); } else { zend_hash_next_index_insert(addHash, &newType, sizeof(sdlTypePtr), NULL); } } smart_str_free(&key); if (model != NULL) { sdlContentModelPtr newModel = malloc(sizeof(sdlContentModel)); newModel->kind = XSD_CONTENT_ELEMENT; newModel->u.element = newType; newModel->min_occurs = 1; newModel->max_occurs = 1; attr = get_attribute(attrs, "maxOccurs"); if (attr) { if (!strcmp(attr->children->content, "unbounded")) { newModel->max_occurs = -1; } else { newModel->max_occurs = atoi(attr->children->content); } } attr = get_attribute(attrs, "minOccurs"); if (attr) { newModel->min_occurs = atoi(attr->children->content); } zend_hash_next_index_insert(model->u.content, &newModel, sizeof(sdlContentModelPtr), NULL); } cur_type = newType; } else { php_error(E_ERROR, "Error parsing schema (element has no 'name' nor 'ref' attributes)"); } /* nillable = boolean : false */ attrs = element->properties; attr = get_attribute(attrs, "nillable"); if (attr) { if (ref != NULL) { php_error(E_ERROR, "Error parsing schema (element have both 'ref' and 'nillable' attributes)"); } if (!stricmp(attr->children->content, "true") || !stricmp(attr->children->content, "1")) { cur_type->nillable = TRUE; } else { cur_type->nillable = FALSE; } } else { cur_type->nillable = FALSE; } attr = get_attribute(attrs, "fixed"); if (attr) { if (ref != NULL) { php_error(E_ERROR, "Error parsing schema (element have both 'ref' and 'fixed' attributes)"); } cur_type->fixed = strdup(attr->children->content); } attr = get_attribute(attrs, "default"); if (attr) { if (ref != NULL) { php_error(E_ERROR, "Error parsing schema (element have both 'ref' and 'fixed' attributes)"); } else if (ref != NULL) { php_error(E_ERROR, "Error parsing schema (element have both 'default' and 'fixed' attributes)"); } cur_type->def = strdup(attr->children->content); } /* type = QName */ type = get_attribute(attrs, "type"); if (type) { char *cptype, *str_ns; xmlNsPtr nsptr; if (ref != NULL) { php_error(E_ERROR, "Error parsing schema (element have both 'ref' and 'type' attributes)"); } parse_namespace(type->children->content, &cptype, &str_ns); nsptr = xmlSearchNs(element->doc, element, str_ns); if (nsptr != NULL) { cur_type->encode = get_create_encoder(sdl, cur_type, (char *)nsptr->href, (char *)cptype); } if (str_ns) {efree(str_ns);} if (cptype) {efree(cptype);} } trav = element->children; if (trav != NULL && node_is_equal(trav, "annotation")) { /* TODO: support */ trav = trav->next; } if (trav != NULL) { if (node_is_equal(trav,"simpleType")) { if (ref != NULL) { php_error(E_ERROR, "Error parsing schema (element have both 'ref' attribute and subtype)"); } else if (type != NULL) { php_error(E_ERROR, "Error parsing schema (element have both 'type' attribute and subtype)"); } schema_simpleType(sdl, tsn, trav, cur_type); trav = trav->next; } else if (node_is_equal(trav,"complexType")) { if (ref != NULL) { php_error(E_ERROR, "Error parsing schema (element have both 'ref' attribute and subtype)"); } else if (type != NULL) { php_error(E_ERROR, "Error parsing schema (element have both 'type' attribute and subtype)"); } schema_complexType(sdl, tsn, trav, cur_type); trav = trav->next; } } while (trav != NULL) { if (node_is_equal(trav,"unique")) { /* TODO: support */ } else if (node_is_equal(trav,"key")) { /* TODO: support */ } else if (node_is_equal(trav,"key")) { /* TODO: support */ } else { php_error(E_ERROR, "Error parsing schema (unexpected <%s> in element)",trav->name); } trav = trav->next; } return TRUE; } /* Content: (annotation?, (simpleType?)) */ static int schema_attribute(sdlPtr sdl, xmlAttrPtr tsn, xmlNodePtr attrType, sdlTypePtr cur_type) { sdlAttributePtr newAttr; xmlAttrPtr attr, name, ref = NULL, type = NULL; xmlNodePtr trav; name = get_attribute(attrType->properties, "name"); if (name == NULL) { name = ref = get_attribute(attrType->properties, "ref"); } if (name) { HashTable *addHash; smart_str key = {0}; newAttr = malloc(sizeof(sdlAttribute)); memset(newAttr, 0, sizeof(sdlAttribute)); if (ref) { char *attr_name, *ns; xmlNsPtr nsptr; parse_namespace(ref->children->content, &attr_name, &ns); nsptr = xmlSearchNs(attrType->doc, attrType, ns); if (nsptr != NULL) { smart_str_appends(&key, nsptr->href); smart_str_appendc(&key, ':'); } smart_str_appends(&key, attr_name); smart_str_0(&key); newAttr->ref = estrdup(key.c); if (attr_name) {efree(attr_name);} if (ns) {efree(ns);} } else { xmlAttrPtr ns; ns = get_attribute(attrType->properties, "targetNamespace"); if (ns == NULL) { ns = tsn; } if (ns != NULL) { smart_str_appends(&key, ns->children->content); smart_str_appendc(&key, ':'); } smart_str_appends(&key, name->children->content); smart_str_0(&key); } if (cur_type == NULL) { if (sdl->attributes == NULL) { sdl->attributes = malloc(sizeof(HashTable)); zend_hash_init(sdl->attributes, 0, NULL, delete_attribute, 1); } addHash = sdl->attributes; } else { if (cur_type->attributes == NULL) { cur_type->attributes = malloc(sizeof(HashTable)); zend_hash_init(cur_type->attributes, 0, NULL, delete_attribute, 1); } addHash = cur_type->attributes; } if (zend_hash_add(addHash, key.c, key.len + 1, &newAttr, sizeof(sdlAttributePtr), NULL) != SUCCESS) { php_error(E_ERROR, "Error parsing schema (attribute '%s' already defined)", key.c); } smart_str_free(&key); } else{ php_error(E_ERROR, "Error parsing schema (attribute has no 'name' nor 'ref' attributes)"); } /* type = QName */ type = get_attribute(attrType->properties, "type"); if (type) { char *cptype, *str_ns; xmlNsPtr nsptr; if (ref != NULL) { php_error(E_ERROR, "Error parsing schema (attribute have both 'ref' and 'type' attributes)"); } parse_namespace(type->children->content, &cptype, &str_ns); nsptr = xmlSearchNs(attrType->doc, attrType, str_ns); if (nsptr != NULL) { newAttr->encode = get_create_encoder(sdl, cur_type, (char *)nsptr->href, (char *)cptype); } if (str_ns) {efree(str_ns);} if (cptype) {efree(cptype);} } attr = attrType->properties; while (attr != NULL) { if (attr_is_equal_ex(attr, "default", SCHEMA_NAMESPACE)) { newAttr->def = strdup(attr->children->content); } else if (attr_is_equal_ex(attr, "fixed", SCHEMA_NAMESPACE)) { newAttr->fixed = strdup(attr->children->content); } else if (attr_is_equal_ex(attr, "form", SCHEMA_NAMESPACE)) { if (strcmp(attr->children->content,"qualified") == 0) { newAttr->form = XSD_FORM_QUALIFIED; } else if (strcmp(attr->children->content,"unqualified") == 0) { newAttr->form = XSD_FORM_UNQUALIFIED; } else { newAttr->form = XSD_FORM_DEFAULT; } } else if (attr_is_equal_ex(attr, "id", SCHEMA_NAMESPACE)) { /* skip */ } else if (attr_is_equal_ex(attr, "name", SCHEMA_NAMESPACE)) { newAttr->name = strdup(attr->children->content); } else if (attr_is_equal_ex(attr, "ref", SCHEMA_NAMESPACE)) { /* already processed */ } else if (attr_is_equal_ex(attr, "type", SCHEMA_NAMESPACE)) { /* already processed */ } else if (attr_is_equal_ex(attr, "use", SCHEMA_NAMESPACE)) { if (strcmp(attr->children->content,"prohibited") == 0) { newAttr->use = XSD_USE_PROHIBITED; } else if (strcmp(attr->children->content,"required") == 0) { newAttr->use = XSD_USE_REQUIRED; } else if (strcmp(attr->children->content,"optional") == 0) { newAttr->use = XSD_USE_OPTIONAL; } else { newAttr->use = XSD_USE_DEFAULT; } } else { xmlNsPtr nsPtr = attr_find_ns(attr); if (strcmp(nsPtr->href, SCHEMA_NAMESPACE)) { smart_str key2 = {0}; if (!newAttr->extraAttributes) { newAttr->extraAttributes = malloc(sizeof(HashTable)); zend_hash_init(newAttr->extraAttributes, 0, NULL, NULL, 1); } smart_str_appends(&key2, nsPtr->href); smart_str_appendc(&key2, ':'); smart_str_appends(&key2, attr->name); smart_str_0(&key2); zend_hash_add(newAttr->extraAttributes, key2.c, key2.len + 1, &attr, sizeof(xmlAttrPtr), NULL); smart_str_free(&key2); } } attr = attr->next; } trav = attrType->children; if (trav != NULL && node_is_equal(trav, "annotation")) { /* TODO: support */ trav = trav->next; } if (trav != NULL) { if (node_is_equal(trav,"simpleType")) { sdlTypePtr dummy_type; if (ref != NULL) { php_error(E_ERROR, "Error parsing schema (attribute have both 'ref' attribute and subtype)"); } else if (type != NULL) { php_error(E_ERROR, "Error parsing schema (attribute have both 'type' attribute and subtype)"); } dummy_type = malloc(sizeof(sdlType)); memset(dummy_type, 0, sizeof(sdlType)); dummy_type->name = strdup("anonymous"); dummy_type->namens = strdup(tsn->children->content); schema_simpleType(sdl, tsn, trav, dummy_type); newAttr->encode = dummy_type->encode; delete_type(&dummy_type); trav = trav->next; } } if (trav != NULL) { php_error(E_ERROR, "Error parsing schema (unexpected <%s> in attribute)",trav->name); } return TRUE; } static int schema_attributeGroup(sdlPtr sdl, xmlAttrPtr tsn, xmlNodePtr attrGroup, sdlTypePtr cur_type) { xmlNodePtr trav; xmlAttrPtr name, ref = NULL; name = get_attribute(attrGroup->properties, "name"); if (name == NULL) { name = ref = get_attribute(attrGroup->properties, "ref"); } if (name) { if (cur_type == NULL) { xmlAttrPtr ns; sdlTypePtr newType; smart_str key = {0}; if (sdl->attributeGroups == NULL) { sdl->attributeGroups = malloc(sizeof(HashTable)); zend_hash_init(sdl->attributeGroups, 0, NULL, delete_type, 1); } ns = get_attribute(attrGroup->properties, "targetNamespace"); if (ns == NULL) { ns = tsn; } newType = malloc(sizeof(sdlType)); memset(newType, 0, sizeof(sdlType)); newType->name = strdup(name->children->content); newType->namens = strdup(ns->children->content); smart_str_appends(&key, newType->namens); smart_str_appendc(&key, ':'); smart_str_appends(&key, newType->name); smart_str_0(&key); if (zend_hash_add(sdl->attributeGroups, key.c, key.len + 1, &newType, sizeof(sdlTypePtr), NULL) != SUCCESS) { php_error(E_ERROR, "Error parsing schema (attributeGroup '%s' already defined)", key.c); } cur_type = newType; smart_str_free(&key); } else if (ref) { sdlAttributePtr newAttr; char *group_name, *ns; smart_str key = {0}; xmlNsPtr nsptr; if (cur_type->attributes == NULL) { cur_type->attributes = malloc(sizeof(HashTable)); zend_hash_init(cur_type->attributes, 0, NULL, delete_attribute, 1); } newAttr = malloc(sizeof(sdlAttribute)); memset(newAttr, 0, sizeof(sdlAttribute)); parse_namespace(ref->children->content, &group_name, &ns); nsptr = xmlSearchNs(attrGroup->doc, attrGroup, ns); if (nsptr != NULL) { smart_str_appends(&key, nsptr->href); smart_str_appendc(&key, ':'); } smart_str_appends(&key, group_name); smart_str_0(&key); newAttr->ref = estrdup(key.c); if (group_name) {efree(group_name);} if (ns) {efree(ns);} smart_str_free(&key); zend_hash_next_index_insert(cur_type->attributes, &newAttr, sizeof(sdlAttributePtr), NULL); cur_type = NULL; } } else{ php_error(E_ERROR, "Error parsing schema (attributeGroup has no 'name' nor 'ref' attributes)"); } trav = attrGroup->children; while (trav != NULL) { if (node_is_equal(trav,"attribute")) { if (ref != NULL) { php_error(E_ERROR, "Error parsing schema (attributeGroup have both 'ref' attribute and subattribute)"); } schema_attribute(sdl, tsn, trav, cur_type); } else if (node_is_equal(trav,"attributeGroup")) { if (ref != NULL) { php_error(E_ERROR, "Error parsing schema (attributeGroup have both 'ref' attribute and subattribute)"); } schema_attributeGroup(sdl, tsn, trav, cur_type); } else if (node_is_equal(trav,"anyAttribute")) { if (ref != NULL) { php_error(E_ERROR, "Error parsing schema (attributeGroup have both 'ref' attribute and subattribute)"); } /* TODO: support */ trav = trav->next; break; } else { php_error(E_ERROR, "Error parsing schema (unexpected <%s> in attributeGroup)",trav->name); } trav = trav->next; } if (trav != NULL) { php_error(E_ERROR, "Error parsing schema (unexpected <%s> in attributeGroup)",trav->name); } return TRUE; } static void schema_attribute_fixup(sdlPtr sdl, sdlAttributePtr attr) { sdlAttributePtr *tmp; if (attr->ref != NULL) { if (sdl->attributes != NULL) { if (zend_hash_find(sdl->attributes, attr->ref, strlen(attr->ref)+1, (void**)&tmp) == SUCCESS) { schema_attribute_fixup(sdl, *tmp); if ((*tmp)->name != NULL && attr->name == NULL) { attr->name = strdup((*tmp)->name); } if ((*tmp)->def != NULL && attr->def == NULL) { attr->def = strdup((*tmp)->def); } if ((*tmp)->fixed != NULL && attr->fixed == NULL) { attr->fixed = strdup((*tmp)->fixed); } if (attr->form == XSD_FORM_DEFAULT) { attr->form = (*tmp)->form; } if (attr->use == XSD_USE_DEFAULT) { attr->use = (*tmp)->use; } if ((*tmp)->extraAttributes != NULL) { xmlNodePtr node; attr->extraAttributes = malloc(sizeof(HashTable)); zend_hash_init(attr->extraAttributes, 0, NULL, NULL, 1); zend_hash_copy(attr->extraAttributes, (*tmp)->extraAttributes, NULL, &node, sizeof(xmlNodePtr)); } attr->encode = (*tmp)->encode; } } efree(attr->ref); attr->ref = NULL; } } static void schema_attributegroup_fixup(sdlPtr sdl, sdlAttributePtr attr, HashTable *ht) { sdlTypePtr *tmp; sdlAttributePtr *tmp_attr; if (attr->ref != NULL) { if (sdl->attributeGroups != NULL) { if (zend_hash_find(sdl->attributeGroups, attr->ref, strlen(attr->ref)+1, (void**)&tmp) == SUCCESS) { if ((*tmp)->attributes) { zend_hash_internal_pointer_reset((*tmp)->attributes); while (zend_hash_get_current_data((*tmp)->attributes,(void**)&tmp_attr) == SUCCESS) { if (zend_hash_get_current_key_type((*tmp)->attributes) == HASH_KEY_IS_STRING) { char* key; uint key_len; sdlAttributePtr newAttr; schema_attribute_fixup(sdl,*tmp_attr); newAttr = malloc(sizeof(sdlAttribute)); memcpy(newAttr, *tmp_attr, sizeof(sdlAttribute)); if (newAttr->def) {newAttr->def = strdup(newAttr->def);} if (newAttr->fixed) {newAttr->fixed = strdup(newAttr->fixed);} if (newAttr->name) {newAttr->name = strdup(newAttr->name);} if (newAttr->extraAttributes) { xmlNodePtr node; HashTable *ht = malloc(sizeof(HashTable)); zend_hash_init(ht, 0, NULL, NULL, 1); zend_hash_copy(ht, newAttr->extraAttributes, NULL, &node, sizeof(xmlNodePtr)); newAttr->extraAttributes = ht; } zend_hash_get_current_key_ex((*tmp)->attributes, &key, &key_len, NULL, 0, NULL); zend_hash_add(ht, key, key_len, &newAttr, sizeof(sdlAttributePtr), NULL); zend_hash_move_forward((*tmp)->attributes); } else { ulong index; schema_attributegroup_fixup(sdl,*tmp_attr, ht); zend_hash_get_current_key((*tmp)->attributes, NULL, &index, 0); zend_hash_index_del((*tmp)->attributes, index); } } } } } efree(attr->ref); attr->ref = NULL; } } static void schema_content_model_fixup(sdlPtr sdl, sdlContentModelPtr model) { switch (model->kind) { case XSD_CONTENT_GROUP_REF: { sdlTypePtr *tmp; if (sdl->groups && zend_hash_find(sdl->groups, model->u.group_ref, strlen(model->u.group_ref)+1, (void**)&tmp) == SUCCESS) { schema_type_fixup(sdl,*tmp); efree(model->u.group_ref); model->kind = XSD_CONTENT_GROUP; model->u.group = (*tmp)->model; } else { php_error(E_ERROR, "Error parsing schema (unresolved group 'ref' attribute)"); } break; } case XSD_CONTENT_SEQUENCE: case XSD_CONTENT_ALL: case XSD_CONTENT_CHOICE: { sdlContentModelPtr *tmp; zend_hash_internal_pointer_reset(model->u.content); while (zend_hash_get_current_data(model->u.content, (void**)&tmp) == SUCCESS) { schema_content_model_fixup(sdl, *tmp); zend_hash_move_forward(model->u.content); } break; } default: break; } } static void schema_type_fixup(sdlPtr sdl, sdlTypePtr type) { sdlTypePtr *tmp; sdlAttributePtr *attr; if (type->ref != NULL) { if (sdl->elements != NULL) { if (zend_hash_find(sdl->elements, type->ref, strlen(type->ref)+1, (void**)&tmp) == SUCCESS) { type->encode = (*tmp)->encode; /* TODO: nillable */ } else { php_error(E_ERROR, "Error parsing schema (unresolved element 'ref' attribute)"); } } efree(type->ref); type->ref = NULL; } if (type->elements) { zend_hash_internal_pointer_reset(type->elements); while (zend_hash_get_current_data(type->elements,(void**)&tmp) == SUCCESS) { schema_type_fixup(sdl,*tmp); zend_hash_move_forward(type->elements); } } if (type->model) { schema_content_model_fixup(sdl, type->model); } if (type->attributes) { zend_hash_internal_pointer_reset(type->attributes); while (zend_hash_get_current_data(type->attributes,(void**)&attr) == SUCCESS) { if (zend_hash_get_current_key_type(type->attributes) == HASH_KEY_IS_STRING) { schema_attribute_fixup(sdl,*attr); zend_hash_move_forward(type->attributes); } else { ulong index; schema_attributegroup_fixup(sdl,*attr,type->attributes); zend_hash_get_current_key(type->attributes, NULL, &index, 0); zend_hash_index_del(type->attributes, index); } } } } int schema_pass2(sdlPtr sdl) { sdlAttributePtr *attr; sdlTypePtr *type; if (sdl->attributes) { zend_hash_internal_pointer_reset(sdl->attributes); while (zend_hash_get_current_data(sdl->attributes,(void**)&attr) == SUCCESS) { schema_attribute_fixup(sdl,*attr); zend_hash_move_forward(sdl->attributes); } } if (sdl->attributeGroups) { zend_hash_internal_pointer_reset(sdl->attributeGroups); while (zend_hash_get_current_data(sdl->attributeGroups,(void**)&type) == SUCCESS) { schema_type_fixup(sdl,*type); zend_hash_move_forward(sdl->attributeGroups); } } if (sdl->elements) { zend_hash_internal_pointer_reset(sdl->elements); while (zend_hash_get_current_data(sdl->elements,(void**)&type) == SUCCESS) { schema_type_fixup(sdl,*type); zend_hash_move_forward(sdl->elements); } } if (sdl->groups) { zend_hash_internal_pointer_reset(sdl->groups); while (zend_hash_get_current_data(sdl->groups,(void**)&type) == SUCCESS) { schema_type_fixup(sdl,*type); zend_hash_move_forward(sdl->groups); } } if (sdl->types) { zend_hash_internal_pointer_reset(sdl->types); while (zend_hash_get_current_data(sdl->types,(void**)&type) == SUCCESS) { schema_type_fixup(sdl,*type); zend_hash_move_forward(sdl->types); } } return TRUE; } int schema_pass3(sdlPtr sdl) { /* if (sdl->elements) { zend_hash_destroy(sdl->elements); free(sdl->elements); sdl->elements = NULL; } */ if (sdl->attributes) { zend_hash_destroy(sdl->attributes); free(sdl->attributes); sdl->attributes = NULL; } if (sdl->attributeGroups) { zend_hash_destroy(sdl->attributeGroups); free(sdl->attributeGroups); sdl->attributeGroups = NULL; } return TRUE; } static void delete_model(void *handle) { sdlContentModelPtr tmp = *((sdlContentModelPtr*)handle); switch (tmp->kind) { case XSD_CONTENT_ELEMENT: case XSD_CONTENT_GROUP: break; case XSD_CONTENT_SEQUENCE: case XSD_CONTENT_ALL: case XSD_CONTENT_CHOICE: zend_hash_destroy(tmp->u.content); free(tmp->u.content); break; case XSD_CONTENT_GROUP_REF: efree(tmp->u.group_ref); break; } free(tmp); } static void delete_type(void *data) { sdlTypePtr type = *((sdlTypePtr*)data); if (type->name) { free(type->name); } if (type->namens) { free(type->namens); } if (type->def) { free(type->def); } if (type->fixed) { free(type->fixed); } if (type->elements) { zend_hash_destroy(type->elements); free(type->elements); } if (type->attributes) { zend_hash_destroy(type->attributes); free(type->attributes); } if (type->restrictions) { delete_restriction_var_int(&type->restrictions->minExclusive); delete_restriction_var_int(&type->restrictions->minInclusive); delete_restriction_var_int(&type->restrictions->maxExclusive); delete_restriction_var_int(&type->restrictions->maxInclusive); delete_restriction_var_int(&type->restrictions->totalDigits); delete_restriction_var_int(&type->restrictions->fractionDigits); delete_restriction_var_int(&type->restrictions->length); delete_restriction_var_int(&type->restrictions->minLength); delete_restriction_var_int(&type->restrictions->maxLength); delete_schema_restriction_var_char(&type->restrictions->whiteSpace); delete_schema_restriction_var_char(&type->restrictions->pattern); if (type->restrictions->enumeration) { zend_hash_destroy(type->restrictions->enumeration); free(type->restrictions->enumeration); } free(type->restrictions); } free(type); } static void delete_attribute(void *attribute) { sdlAttributePtr attr = *((sdlAttributePtr*)attribute); if (attr->def) { free(attr->def); } if (attr->fixed) { free(attr->fixed); } if (attr->name) { free(attr->name); } if (attr->ref) { free(attr->ref); } if (attr->extraAttributes) { zend_hash_destroy(attr->extraAttributes); free(attr->extraAttributes); } } static void delete_restriction_var_int(void *rvi) { sdlRestrictionIntPtr ptr = *((sdlRestrictionIntPtr*)rvi); if (ptr) { free(ptr); } } static void delete_schema_restriction_var_char(void *srvc) { sdlRestrictionCharPtr ptr = *((sdlRestrictionCharPtr*)srvc); if (ptr) { if (ptr->value) { free(ptr->value); } free(ptr); } }