PageRenderTime 35ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 1ms

/ext/soap/php_schema.c

http://github.com/php/php-src
C | 2585 lines | 2081 code | 247 blank | 257 comment | 837 complexity | b19a0a403c66655103fd9da84020597d MD5 | raw file
Possible License(s): BSD-2-Clause, BSD-3-Clause, MPL-2.0-no-copyleft-exception, LGPL-2.1
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Copyright (c) The PHP Group |
  4. +----------------------------------------------------------------------+
  5. | This source file is subject to version 3.01 of the PHP license, |
  6. | that is bundled with this package in the file LICENSE, and is |
  7. | available through the world-wide-web at the following url: |
  8. | http://www.php.net/license/3_01.txt |
  9. | If you did not receive a copy of the PHP license and are unable to |
  10. | obtain it through the world-wide-web, please send a note to |
  11. | license@php.net so we can mail you a copy immediately. |
  12. +----------------------------------------------------------------------+
  13. | Authors: Brad Lafountain <rodif_bl@yahoo.com> |
  14. | Shane Caraveo <shane@caraveo.com> |
  15. | Dmitry Stogov <dmitry@php.net> |
  16. +----------------------------------------------------------------------+
  17. */
  18. #include "php_soap.h"
  19. #include "libxml/uri.h"
  20. static int schema_simpleType(sdlPtr sdl, xmlAttrPtr tns, xmlNodePtr simpleType, sdlTypePtr cur_type);
  21. static int schema_complexType(sdlPtr sdl, xmlAttrPtr tns, xmlNodePtr compType, sdlTypePtr cur_type);
  22. static int schema_list(sdlPtr sdl, xmlAttrPtr tns, xmlNodePtr listType, sdlTypePtr cur_type);
  23. static int schema_union(sdlPtr sdl, xmlAttrPtr tns, xmlNodePtr unionType, sdlTypePtr cur_type);
  24. static int schema_simpleContent(sdlPtr sdl, xmlAttrPtr tns, xmlNodePtr simpCompType, sdlTypePtr cur_type);
  25. static int schema_restriction_simpleContent(sdlPtr sdl, xmlAttrPtr tns, xmlNodePtr restType, sdlTypePtr cur_type, int simpleType);
  26. static int schema_restriction_complexContent(sdlPtr sdl, xmlAttrPtr tns, xmlNodePtr restType, sdlTypePtr cur_type);
  27. static int schema_extension_simpleContent(sdlPtr sdl, xmlAttrPtr tns, xmlNodePtr extType, sdlTypePtr cur_type);
  28. static int schema_extension_complexContent(sdlPtr sdl, xmlAttrPtr tns, xmlNodePtr extType, sdlTypePtr cur_type);
  29. static int schema_sequence(sdlPtr sdl, xmlAttrPtr tns, xmlNodePtr seqType, sdlTypePtr cur_type, sdlContentModelPtr model);
  30. static int schema_all(sdlPtr sdl, xmlAttrPtr tns, xmlNodePtr extType, sdlTypePtr cur_type, sdlContentModelPtr model);
  31. static int schema_choice(sdlPtr sdl, xmlAttrPtr tns, xmlNodePtr choiceType, sdlTypePtr cur_type, sdlContentModelPtr model);
  32. static int schema_group(sdlPtr sdl, xmlAttrPtr tns, xmlNodePtr groupType, sdlTypePtr cur_type, sdlContentModelPtr model);
  33. static int schema_any(sdlPtr sdl, xmlAttrPtr tns, xmlNodePtr extType, sdlTypePtr cur_type, sdlContentModelPtr model);
  34. static int schema_element(sdlPtr sdl, xmlAttrPtr tns, xmlNodePtr element, sdlTypePtr cur_type, sdlContentModelPtr model);
  35. static int schema_attribute(sdlPtr sdl, xmlAttrPtr tns, xmlNodePtr attrType, sdlTypePtr cur_type, sdlCtx *ctx);
  36. static int schema_attributeGroup(sdlPtr sdl, xmlAttrPtr tns, xmlNodePtr attrType, sdlTypePtr cur_type, sdlCtx *ctx);
  37. static int schema_restriction_var_int(xmlNodePtr val, sdlRestrictionIntPtr *valptr);
  38. static int schema_restriction_var_char(xmlNodePtr val, sdlRestrictionCharPtr *valptr);
  39. static void schema_type_fixup(sdlCtx *ctx, sdlTypePtr type);
  40. static encodePtr create_encoder(sdlPtr sdl, sdlTypePtr cur_type, const xmlChar *ns, const xmlChar *type)
  41. {
  42. smart_str nscat = {0};
  43. encodePtr enc, enc_ptr;
  44. if (sdl->encoders == NULL) {
  45. sdl->encoders = emalloc(sizeof(HashTable));
  46. zend_hash_init(sdl->encoders, 0, NULL, delete_encoder, 0);
  47. }
  48. smart_str_appends(&nscat, (char*)ns);
  49. smart_str_appendc(&nscat, ':');
  50. smart_str_appends(&nscat, (char*)type);
  51. smart_str_0(&nscat);
  52. if ((enc_ptr = zend_hash_find_ptr(sdl->encoders, nscat.s)) != NULL) {
  53. enc = enc_ptr;
  54. if (enc->details.ns) {
  55. efree(enc->details.ns);
  56. }
  57. if (enc->details.type_str) {
  58. efree(enc->details.type_str);
  59. }
  60. } else {
  61. enc_ptr = NULL;
  62. enc = emalloc(sizeof(encode));
  63. }
  64. memset(enc, 0, sizeof(encode));
  65. enc->details.ns = estrdup((char*)ns);
  66. enc->details.type_str = estrdup((char*)type);
  67. enc->details.sdl_type = cur_type;
  68. enc->to_xml = sdl_guess_convert_xml;
  69. enc->to_zval = sdl_guess_convert_zval;
  70. if (enc_ptr == NULL) {
  71. zend_hash_update_ptr(sdl->encoders, nscat.s, enc);
  72. }
  73. smart_str_free(&nscat);
  74. return enc;
  75. }
  76. static encodePtr get_create_encoder(sdlPtr sdl, sdlTypePtr cur_type, const xmlChar *ns, const xmlChar *type)
  77. {
  78. encodePtr enc = get_encoder(sdl, (char*)ns, (char*)type);
  79. if (enc == NULL) {
  80. enc = create_encoder(sdl, cur_type, ns, type);
  81. }
  82. return enc;
  83. }
  84. static void schema_load_file(sdlCtx *ctx, xmlAttrPtr ns, xmlChar *location, xmlAttrPtr tns, int import) {
  85. if (location != NULL &&
  86. !zend_hash_str_exists(&ctx->docs, (char*)location, xmlStrlen(location))) {
  87. xmlDocPtr doc;
  88. xmlNodePtr schema;
  89. xmlAttrPtr new_tns;
  90. sdl_set_uri_credentials(ctx, (char*)location);
  91. doc = soap_xmlParseFile((char*)location);
  92. sdl_restore_uri_credentials(ctx);
  93. if (doc == NULL) {
  94. soap_error1(E_ERROR, "Parsing Schema: can't import schema from '%s'", location);
  95. }
  96. schema = get_node(doc->children, "schema");
  97. if (schema == NULL) {
  98. xmlFreeDoc(doc);
  99. soap_error1(E_ERROR, "Parsing Schema: can't import schema from '%s'", location);
  100. }
  101. new_tns = get_attribute(schema->properties, "targetNamespace");
  102. if (import) {
  103. if (ns != NULL && (new_tns == NULL || xmlStrcmp(ns->children->content, new_tns->children->content) != 0)) {
  104. xmlFreeDoc(doc);
  105. soap_error2(E_ERROR, "Parsing Schema: can't import schema from '%s', unexpected 'targetNamespace'='%s'", location, ns->children->content);
  106. }
  107. if (ns == NULL && new_tns != NULL) {
  108. xmlFreeDoc(doc);
  109. soap_error2(E_ERROR, "Parsing Schema: can't import schema from '%s', unexpected 'targetNamespace'='%s'", location, new_tns->children->content);
  110. }
  111. } else {
  112. new_tns = get_attribute(schema->properties, "targetNamespace");
  113. if (new_tns == NULL) {
  114. if (tns != NULL) {
  115. xmlSetProp(schema, BAD_CAST("targetNamespace"), tns->children->content);
  116. }
  117. } else if (tns != NULL && xmlStrcmp(tns->children->content, new_tns->children->content) != 0) {
  118. xmlFreeDoc(doc);
  119. soap_error1(E_ERROR, "Parsing Schema: can't include schema from '%s', different 'targetNamespace'", location);
  120. }
  121. }
  122. zend_hash_str_add_ptr(&ctx->docs, (char*)location, xmlStrlen(location), doc);
  123. load_schema(ctx, schema);
  124. }
  125. }
  126. /*
  127. 2.6.1 xsi:type
  128. 2.6.2 xsi:nil
  129. 2.6.3 xsi:schemaLocation, xsi:noNamespaceSchemaLocation
  130. */
  131. /*
  132. <schema
  133. attributeFormDefault = (qualified | unqualified) : unqualified
  134. blockDefault = (#all | List of (extension | restriction | substitution)) : ''
  135. elementFormDefault = (qualified | unqualified) : unqualified
  136. finalDefault = (#all | List of (extension | restriction)) : ''
  137. id = ID
  138. targetNamespace = anyURI
  139. version = token
  140. xml:lang = language
  141. {any attributes with non-schema namespace . . .}>
  142. Content: ((include | import | redefine | annotation)*, (((simpleType | complexType | group | attributeGroup) | element | attribute | notation), annotation*)*)
  143. </schema>
  144. */
  145. int load_schema(sdlCtx *ctx, xmlNodePtr schema)
  146. {
  147. xmlNodePtr trav;
  148. xmlAttrPtr tns;
  149. if (!ctx->sdl->types) {
  150. ctx->sdl->types = emalloc(sizeof(HashTable));
  151. zend_hash_init(ctx->sdl->types, 0, NULL, delete_type, 0);
  152. }
  153. if (!ctx->attributes) {
  154. ctx->attributes = emalloc(sizeof(HashTable));
  155. zend_hash_init(ctx->attributes, 0, NULL, delete_attribute, 0);
  156. }
  157. if (!ctx->attributeGroups) {
  158. ctx->attributeGroups = emalloc(sizeof(HashTable));
  159. zend_hash_init(ctx->attributeGroups, 0, NULL, delete_type, 0);
  160. }
  161. tns = get_attribute(schema->properties, "targetNamespace");
  162. if (tns == NULL) {
  163. tns = xmlSetProp(schema, BAD_CAST("targetNamespace"), BAD_CAST(""));
  164. xmlNewNs(schema, BAD_CAST(""), NULL);
  165. }
  166. trav = schema->children;
  167. while (trav != NULL) {
  168. if (node_is_equal(trav,"include")) {
  169. xmlAttrPtr location;
  170. location = get_attribute(trav->properties, "schemaLocation");
  171. if (location == NULL) {
  172. soap_error0(E_ERROR, "Parsing Schema: include has no 'schemaLocation' attribute");
  173. } else {
  174. xmlChar *uri;
  175. xmlChar *base = xmlNodeGetBase(trav->doc, trav);
  176. if (base == NULL) {
  177. uri = xmlBuildURI(location->children->content, trav->doc->URL);
  178. } else {
  179. uri = xmlBuildURI(location->children->content, base);
  180. xmlFree(base);
  181. }
  182. schema_load_file(ctx, NULL, uri, tns, 0);
  183. xmlFree(uri);
  184. }
  185. } else if (node_is_equal(trav,"redefine")) {
  186. xmlAttrPtr location;
  187. location = get_attribute(trav->properties, "schemaLocation");
  188. if (location == NULL) {
  189. soap_error0(E_ERROR, "Parsing Schema: redefine has no 'schemaLocation' attribute");
  190. } else {
  191. xmlChar *uri;
  192. xmlChar *base = xmlNodeGetBase(trav->doc, trav);
  193. if (base == NULL) {
  194. uri = xmlBuildURI(location->children->content, trav->doc->URL);
  195. } else {
  196. uri = xmlBuildURI(location->children->content, base);
  197. xmlFree(base);
  198. }
  199. schema_load_file(ctx, NULL, uri, tns, 0);
  200. xmlFree(uri);
  201. /* TODO: <redefine> support */
  202. }
  203. } else if (node_is_equal(trav,"import")) {
  204. xmlAttrPtr ns, location;
  205. xmlChar *uri = NULL;
  206. ns = get_attribute(trav->properties, "namespace");
  207. location = get_attribute(trav->properties, "schemaLocation");
  208. if (ns != NULL && tns != NULL && xmlStrcmp(ns->children->content, tns->children->content) == 0) {
  209. if (location) {
  210. soap_error1(E_ERROR, "Parsing Schema: can't import schema from '%s', namespace must not match the enclosing schema 'targetNamespace'", location->children->content);
  211. } else {
  212. soap_error0(E_ERROR, "Parsing Schema: can't import schema. Namespace must not match the enclosing schema 'targetNamespace'");
  213. }
  214. }
  215. if (location) {
  216. xmlChar *base = xmlNodeGetBase(trav->doc, trav);
  217. if (base == NULL) {
  218. uri = xmlBuildURI(location->children->content, trav->doc->URL);
  219. } else {
  220. uri = xmlBuildURI(location->children->content, base);
  221. xmlFree(base);
  222. }
  223. }
  224. schema_load_file(ctx, ns, uri, tns, 1);
  225. if (uri != NULL) {xmlFree(uri);}
  226. } else if (node_is_equal(trav,"annotation")) {
  227. /* TODO: <annotation> support */
  228. /* annotation cleanup
  229. xmlNodePtr tmp = trav;
  230. trav = trav->next;
  231. xmlUnlinkNode(tmp);
  232. xmlFreeNode(tmp);
  233. continue;
  234. */
  235. } else {
  236. break;
  237. }
  238. trav = trav->next;
  239. }
  240. while (trav != NULL) {
  241. if (node_is_equal(trav,"simpleType")) {
  242. schema_simpleType(ctx->sdl, tns, trav, NULL);
  243. } else if (node_is_equal(trav,"complexType")) {
  244. schema_complexType(ctx->sdl, tns, trav, NULL);
  245. } else if (node_is_equal(trav,"group")) {
  246. schema_group(ctx->sdl, tns, trav, NULL, NULL);
  247. } else if (node_is_equal(trav,"attributeGroup")) {
  248. schema_attributeGroup(ctx->sdl, tns, trav, NULL, ctx);
  249. } else if (node_is_equal(trav,"element")) {
  250. schema_element(ctx->sdl, tns, trav, NULL, NULL);
  251. } else if (node_is_equal(trav,"attribute")) {
  252. schema_attribute(ctx->sdl, tns, trav, NULL, ctx);
  253. } else if (node_is_equal(trav,"notation")) {
  254. /* TODO: <notation> support */
  255. } else if (node_is_equal(trav,"annotation")) {
  256. /* TODO: <annotation> support */
  257. } else {
  258. soap_error1(E_ERROR, "Parsing Schema: unexpected <%s> in schema", trav->name);
  259. }
  260. trav = trav->next;
  261. }
  262. return TRUE;
  263. }
  264. /*
  265. <simpleType
  266. final = (#all | (list | union | restriction))
  267. id = ID
  268. name = NCName
  269. {any attributes with non-schema namespace . . .}>
  270. Content: (annotation?, (restriction | list | union))
  271. </simpleType>
  272. */
  273. static int schema_simpleType(sdlPtr sdl, xmlAttrPtr tns, xmlNodePtr simpleType, sdlTypePtr cur_type)
  274. {
  275. xmlNodePtr trav;
  276. xmlAttrPtr name, ns;
  277. ns = get_attribute(simpleType->properties, "targetNamespace");
  278. if (ns == NULL) {
  279. ns = tns;
  280. }
  281. name = get_attribute(simpleType->properties, "name");
  282. if (cur_type != NULL) {
  283. /* Anonymous type inside <element> or <restriction> */
  284. sdlTypePtr newType, ptr;
  285. newType = emalloc(sizeof(sdlType));
  286. memset(newType, 0, sizeof(sdlType));
  287. newType->kind = XSD_TYPEKIND_SIMPLE;
  288. if (name != NULL) {
  289. newType->name = estrdup((char*)name->children->content);
  290. newType->namens = estrdup((char*)ns->children->content);
  291. } else {
  292. newType->name = estrdup(cur_type->name);
  293. newType->namens = estrdup(cur_type->namens);
  294. }
  295. ptr = zend_hash_next_index_insert_ptr(sdl->types, newType);
  296. if (sdl->encoders == NULL) {
  297. sdl->encoders = emalloc(sizeof(HashTable));
  298. zend_hash_init(sdl->encoders, 0, NULL, delete_encoder, 0);
  299. }
  300. cur_type->encode = emalloc(sizeof(encode));
  301. memset(cur_type->encode, 0, sizeof(encode));
  302. cur_type->encode->details.ns = estrdup(newType->namens);
  303. cur_type->encode->details.type_str = estrdup(newType->name);
  304. cur_type->encode->details.sdl_type = ptr;
  305. cur_type->encode->to_xml = sdl_guess_convert_xml;
  306. cur_type->encode->to_zval = sdl_guess_convert_zval;
  307. zend_hash_next_index_insert_ptr(sdl->encoders, cur_type->encode);
  308. cur_type =ptr;
  309. } else if (name != NULL) {
  310. sdlTypePtr newType, ptr;
  311. newType = emalloc(sizeof(sdlType));
  312. memset(newType, 0, sizeof(sdlType));
  313. newType->kind = XSD_TYPEKIND_SIMPLE;
  314. newType->name = estrdup((char*)name->children->content);
  315. newType->namens = estrdup((char*)ns->children->content);
  316. if (cur_type == NULL) {
  317. ptr = zend_hash_next_index_insert_ptr(sdl->types, newType);
  318. } else {
  319. if (cur_type->elements == NULL) {
  320. cur_type->elements = emalloc(sizeof(HashTable));
  321. zend_hash_init(cur_type->elements, 0, NULL, delete_type, 0);
  322. }
  323. ptr = zend_hash_str_update_ptr(cur_type->elements, newType->name, strlen(newType->name), newType);
  324. }
  325. cur_type = ptr;
  326. create_encoder(sdl, cur_type, ns->children->content, name->children->content);
  327. } else {
  328. soap_error0(E_ERROR, "Parsing Schema: simpleType has no 'name' attribute");
  329. }
  330. trav = simpleType->children;
  331. if (trav != NULL && node_is_equal(trav,"annotation")) {
  332. /* TODO: <annotation> support */
  333. trav = trav->next;
  334. }
  335. if (trav != NULL) {
  336. if (node_is_equal(trav,"restriction")) {
  337. schema_restriction_simpleContent(sdl, tns, trav, cur_type, 1);
  338. trav = trav->next;
  339. } else if (node_is_equal(trav,"list")) {
  340. cur_type->kind = XSD_TYPEKIND_LIST;
  341. schema_list(sdl, tns, trav, cur_type);
  342. trav = trav->next;
  343. } else if (node_is_equal(trav,"union")) {
  344. cur_type->kind = XSD_TYPEKIND_UNION;
  345. schema_union(sdl, tns, trav, cur_type);
  346. trav = trav->next;
  347. } else {
  348. soap_error1(E_ERROR, "Parsing Schema: unexpected <%s> in simpleType", trav->name);
  349. }
  350. } else {
  351. soap_error0(E_ERROR, "Parsing Schema: expected <restriction>, <list> or <union> in simpleType");
  352. }
  353. if (trav != NULL) {
  354. soap_error1(E_ERROR, "Parsing Schema: unexpected <%s> in simpleType", trav->name);
  355. }
  356. return TRUE;
  357. }
  358. /*
  359. <list
  360. id = ID
  361. itemType = QName
  362. {any attributes with non-schema namespace . . .}>
  363. Content: (annotation?, (simpleType?))
  364. </list>
  365. */
  366. static int schema_list(sdlPtr sdl, xmlAttrPtr tns, xmlNodePtr listType, sdlTypePtr cur_type)
  367. {
  368. xmlNodePtr trav;
  369. xmlAttrPtr itemType;
  370. itemType = get_attribute(listType->properties, "itemType");
  371. if (itemType != NULL) {
  372. char *type, *ns;
  373. xmlNsPtr nsptr;
  374. parse_namespace(itemType->children->content, &type, &ns);
  375. nsptr = xmlSearchNs(listType->doc, listType, BAD_CAST(ns));
  376. if (nsptr != NULL) {
  377. sdlTypePtr newType;
  378. newType = emalloc(sizeof(sdlType));
  379. memset(newType, 0, sizeof(sdlType));
  380. newType->name = estrdup(type);
  381. newType->namens = estrdup((char*)nsptr->href);
  382. newType->encode = get_create_encoder(sdl, newType, nsptr->href, BAD_CAST(type));
  383. if (cur_type->elements == NULL) {
  384. cur_type->elements = emalloc(sizeof(HashTable));
  385. zend_hash_init(cur_type->elements, 0, NULL, delete_type, 0);
  386. }
  387. zend_hash_next_index_insert_ptr(cur_type->elements, newType);
  388. }
  389. if (type) {efree(type);}
  390. if (ns) {efree(ns);}
  391. }
  392. trav = listType->children;
  393. if (trav != NULL && node_is_equal(trav,"annotation")) {
  394. /* TODO: <annotation> support */
  395. trav = trav->next;
  396. }
  397. if (trav != NULL && node_is_equal(trav,"simpleType")) {
  398. sdlTypePtr newType;
  399. if (itemType != NULL) {
  400. soap_error0(E_ERROR, "Parsing Schema: element has both 'itemType' attribute and subtype");
  401. }
  402. newType = emalloc(sizeof(sdlType));
  403. memset(newType, 0, sizeof(sdlType));
  404. {
  405. char buf[MAX_LENGTH_OF_LONG + 1];
  406. char *res = zend_print_long_to_buf(buf + sizeof(buf) - 1, zend_hash_num_elements(sdl->types));
  407. char *str = emalloc(sizeof("anonymous")-1 + (buf + sizeof(buf) - res));
  408. memcpy(str, "anonymous", sizeof("anonymous")-1);
  409. memcpy(str + sizeof("anonymous")-1, res, buf + sizeof(buf) - res);
  410. newType->name = str;
  411. }
  412. newType->namens = estrdup((char*)tns->children->content);
  413. if (cur_type->elements == NULL) {
  414. cur_type->elements = emalloc(sizeof(HashTable));
  415. zend_hash_init(cur_type->elements, 0, NULL, delete_type, 0);
  416. }
  417. zend_hash_next_index_insert_ptr(cur_type->elements, newType);
  418. schema_simpleType(sdl, tns, trav, newType);
  419. trav = trav->next;
  420. }
  421. if (trav != NULL) {
  422. soap_error1(E_ERROR, "Parsing Schema: unexpected <%s> in list", trav->name);
  423. }
  424. return TRUE;
  425. }
  426. /*
  427. <union
  428. id = ID
  429. memberTypes = List of QName
  430. {any attributes with non-schema namespace . . .}>
  431. Content: (annotation?, (simpleType*))
  432. </union>
  433. */
  434. static int schema_union(sdlPtr sdl, xmlAttrPtr tns, xmlNodePtr unionType, sdlTypePtr cur_type)
  435. {
  436. xmlNodePtr trav;
  437. xmlAttrPtr memberTypes;
  438. memberTypes = get_attribute(unionType->properties, "memberTypes");
  439. if (memberTypes != NULL) {
  440. char *str, *start, *end, *next;
  441. char *type, *ns;
  442. xmlNsPtr nsptr;
  443. str = estrdup((char*)memberTypes->children->content);
  444. whiteSpace_collapse(BAD_CAST(str));
  445. start = str;
  446. while (start != NULL && *start != '\0') {
  447. end = strchr(start,' ');
  448. if (end == NULL) {
  449. next = NULL;
  450. } else {
  451. *end = '\0';
  452. next = end+1;
  453. }
  454. parse_namespace(BAD_CAST(start), &type, &ns);
  455. nsptr = xmlSearchNs(unionType->doc, unionType, BAD_CAST(ns));
  456. if (nsptr != NULL) {
  457. sdlTypePtr newType;
  458. newType = emalloc(sizeof(sdlType));
  459. memset(newType, 0, sizeof(sdlType));
  460. newType->name = estrdup(type);
  461. newType->namens = estrdup((char*)nsptr->href);
  462. newType->encode = get_create_encoder(sdl, newType, nsptr->href, BAD_CAST(type));
  463. if (cur_type->elements == NULL) {
  464. cur_type->elements = emalloc(sizeof(HashTable));
  465. zend_hash_init(cur_type->elements, 0, NULL, delete_type, 0);
  466. }
  467. zend_hash_next_index_insert_ptr(cur_type->elements, newType);
  468. }
  469. if (type) {efree(type);}
  470. if (ns) {efree(ns);}
  471. start = next;
  472. }
  473. efree(str);
  474. }
  475. trav = unionType->children;
  476. if (trav != NULL && node_is_equal(trav,"annotation")) {
  477. /* TODO: <annotation> support */
  478. trav = trav->next;
  479. }
  480. while (trav != NULL) {
  481. if (node_is_equal(trav,"simpleType")) {
  482. sdlTypePtr newType;
  483. newType = emalloc(sizeof(sdlType));
  484. memset(newType, 0, sizeof(sdlType));
  485. {
  486. char buf[MAX_LENGTH_OF_LONG + 1];
  487. char *res = zend_print_long_to_buf(buf + sizeof(buf) - 1, zend_hash_num_elements(sdl->types));
  488. char *str = emalloc(sizeof("anonymous")-1 + (buf + sizeof(buf) - res));
  489. memcpy(str, "anonymous", sizeof("anonymous")-1);
  490. memcpy(str + sizeof("anonymous")-1, res, buf + sizeof(buf) - res);
  491. newType->name = str;
  492. }
  493. newType->namens = estrdup((char*)tns->children->content);
  494. if (cur_type->elements == NULL) {
  495. cur_type->elements = emalloc(sizeof(HashTable));
  496. zend_hash_init(cur_type->elements, 0, NULL, delete_type, 0);
  497. }
  498. zend_hash_next_index_insert_ptr(cur_type->elements, newType);
  499. schema_simpleType(sdl, tns, trav, newType);
  500. } else {
  501. soap_error1(E_ERROR, "Parsing Schema: unexpected <%s> in union", trav->name);
  502. }
  503. trav = trav->next;
  504. }
  505. if (trav != NULL) {
  506. soap_error1(E_ERROR, "Parsing Schema: unexpected <%s> in union", trav->name);
  507. }
  508. return TRUE;
  509. }
  510. /*
  511. <simpleContent
  512. id = ID
  513. {any attributes with non-schema namespace . . .}>
  514. Content: (annotation?, (restriction | extension))
  515. </simpleContent>
  516. */
  517. static int schema_simpleContent(sdlPtr sdl, xmlAttrPtr tns, xmlNodePtr simpCompType, sdlTypePtr cur_type)
  518. {
  519. xmlNodePtr trav;
  520. trav = simpCompType->children;
  521. if (trav != NULL && node_is_equal(trav,"annotation")) {
  522. /* TODO: <annotation> support */
  523. trav = trav->next;
  524. }
  525. if (trav != NULL) {
  526. if (node_is_equal(trav, "restriction")) {
  527. cur_type->kind = XSD_TYPEKIND_RESTRICTION;
  528. schema_restriction_simpleContent(sdl, tns, trav, cur_type, 0);
  529. trav = trav->next;
  530. } else if (node_is_equal(trav, "extension")) {
  531. cur_type->kind = XSD_TYPEKIND_EXTENSION;
  532. schema_extension_simpleContent(sdl, tns, trav, cur_type);
  533. trav = trav->next;
  534. } else {
  535. soap_error1(E_ERROR, "Parsing Schema: unexpected <%s> in simpleContent", trav->name);
  536. }
  537. } else {
  538. soap_error0(E_ERROR, "Parsing Schema: expected <restriction> or <extension> in simpleContent");
  539. }
  540. if (trav != NULL) {
  541. soap_error1(E_ERROR, "Parsing Schema: unexpected <%s> in simpleContent", trav->name);
  542. }
  543. return TRUE;
  544. }
  545. /*
  546. simpleType:<restriction
  547. base = QName
  548. id = ID
  549. {any attributes with non-schema namespace . . .}>
  550. Content: (annotation?, (simpleType?, (minExclusive | minInclusive | maxExclusive | maxInclusive | totalDigits | fractionDigits | length | minLength | maxLength | enumeration | whiteSpace | pattern)*)?)
  551. </restriction>
  552. simpleContent:<restriction
  553. base = QName
  554. id = ID
  555. {any attributes with non-schema namespace . . .}>
  556. Content: (annotation?, (simpleType?, (minExclusive | minInclusive | maxExclusive | maxInclusive | totalDigits | fractionDigits | length | minLength | maxLength | enumeration | whiteSpace | pattern)*)?, ((attribute | attributeGroup)*, anyAttribute?))
  557. </restriction>
  558. */
  559. static int schema_restriction_simpleContent(sdlPtr sdl, xmlAttrPtr tns, xmlNodePtr restType, sdlTypePtr cur_type, int simpleType)
  560. {
  561. xmlNodePtr trav;
  562. xmlAttrPtr base;
  563. base = get_attribute(restType->properties, "base");
  564. if (base != NULL) {
  565. char *type, *ns;
  566. xmlNsPtr nsptr;
  567. parse_namespace(base->children->content, &type, &ns);
  568. nsptr = xmlSearchNs(restType->doc, restType, BAD_CAST(ns));
  569. if (nsptr != NULL) {
  570. cur_type->encode = get_create_encoder(sdl, cur_type, nsptr->href, BAD_CAST(type));
  571. }
  572. if (type) {efree(type);}
  573. if (ns) {efree(ns);}
  574. } else if (!simpleType) {
  575. soap_error0(E_ERROR, "Parsing Schema: restriction has no 'base' attribute");
  576. }
  577. if (cur_type->restrictions == NULL) {
  578. cur_type->restrictions = emalloc(sizeof(sdlRestrictions));
  579. memset(cur_type->restrictions, 0, sizeof(sdlRestrictions));
  580. }
  581. trav = restType->children;
  582. if (trav != NULL && node_is_equal(trav, "annotation")) {
  583. /* TODO: <annotation> support */
  584. trav = trav->next;
  585. }
  586. if (trav != NULL && node_is_equal(trav, "simpleType")) {
  587. schema_simpleType(sdl, tns, trav, cur_type);
  588. trav = trav->next;
  589. }
  590. while (trav != NULL) {
  591. if (node_is_equal(trav, "minExclusive")) {
  592. schema_restriction_var_int(trav, &cur_type->restrictions->minExclusive);
  593. } else if (node_is_equal(trav, "minInclusive")) {
  594. schema_restriction_var_int(trav, &cur_type->restrictions->minInclusive);
  595. } else if (node_is_equal(trav, "maxExclusive")) {
  596. schema_restriction_var_int(trav, &cur_type->restrictions->maxExclusive);
  597. } else if (node_is_equal(trav, "maxInclusive")) {
  598. schema_restriction_var_int(trav, &cur_type->restrictions->maxInclusive);
  599. } else if (node_is_equal(trav, "totalDigits")) {
  600. schema_restriction_var_int(trav, &cur_type->restrictions->totalDigits);
  601. } else if (node_is_equal(trav, "fractionDigits")) {
  602. schema_restriction_var_int(trav, &cur_type->restrictions->fractionDigits);
  603. } else if (node_is_equal(trav, "length")) {
  604. schema_restriction_var_int(trav, &cur_type->restrictions->length);
  605. } else if (node_is_equal(trav, "minLength")) {
  606. schema_restriction_var_int(trav, &cur_type->restrictions->minLength);
  607. } else if (node_is_equal(trav, "maxLength")) {
  608. schema_restriction_var_int(trav, &cur_type->restrictions->maxLength);
  609. } else if (node_is_equal(trav, "whiteSpace")) {
  610. schema_restriction_var_char(trav, &cur_type->restrictions->whiteSpace);
  611. } else if (node_is_equal(trav, "pattern")) {
  612. schema_restriction_var_char(trav, &cur_type->restrictions->pattern);
  613. } else if (node_is_equal(trav, "enumeration")) {
  614. sdlRestrictionCharPtr enumval = NULL;
  615. schema_restriction_var_char(trav, &enumval);
  616. if (cur_type->restrictions->enumeration == NULL) {
  617. cur_type->restrictions->enumeration = emalloc(sizeof(HashTable));
  618. zend_hash_init(cur_type->restrictions->enumeration, 0, NULL, delete_restriction_var_char, 0);
  619. }
  620. if (zend_hash_str_add_ptr(cur_type->restrictions->enumeration, enumval->value, strlen(enumval->value), enumval) == NULL) {
  621. delete_restriction_var_char_int(enumval);
  622. }
  623. } else {
  624. break;
  625. }
  626. trav = trav->next;
  627. }
  628. if (!simpleType) {
  629. while (trav != NULL) {
  630. if (node_is_equal(trav,"attribute")) {
  631. schema_attribute(sdl, tns, trav, cur_type, NULL);
  632. } else if (node_is_equal(trav,"attributeGroup")) {
  633. schema_attributeGroup(sdl, tns, trav, cur_type, NULL);
  634. } else if (node_is_equal(trav,"anyAttribute")) {
  635. /* TODO: <anyAttribute> support */
  636. trav = trav->next;
  637. break;
  638. } else {
  639. soap_error1(E_ERROR, "Parsing Schema: unexpected <%s> in restriction", trav->name);
  640. }
  641. trav = trav->next;
  642. }
  643. }
  644. if (trav != NULL) {
  645. soap_error1(E_ERROR, "Parsing Schema: unexpected <%s> in restriction", trav->name);
  646. }
  647. return TRUE;
  648. }
  649. /*
  650. <restriction
  651. base = QName
  652. id = ID
  653. {any attributes with non-schema namespace . . .}>
  654. Content: (annotation?, (group | all | choice | sequence)?, ((attribute | attributeGroup)*, anyAttribute?))
  655. </restriction>
  656. */
  657. static int schema_restriction_complexContent(sdlPtr sdl, xmlAttrPtr tns, xmlNodePtr restType, sdlTypePtr cur_type)
  658. {
  659. xmlAttrPtr base;
  660. xmlNodePtr trav;
  661. base = get_attribute(restType->properties, "base");
  662. if (base != NULL) {
  663. char *type, *ns;
  664. xmlNsPtr nsptr;
  665. parse_namespace(base->children->content, &type, &ns);
  666. nsptr = xmlSearchNs(restType->doc, restType, BAD_CAST(ns));
  667. if (nsptr != NULL) {
  668. cur_type->encode = get_create_encoder(sdl, cur_type, nsptr->href, BAD_CAST(type));
  669. }
  670. if (type) {efree(type);}
  671. if (ns) {efree(ns);}
  672. } else {
  673. soap_error0(E_ERROR, "Parsing Schema: restriction has no 'base' attribute");
  674. }
  675. trav = restType->children;
  676. if (trav != NULL && node_is_equal(trav,"annotation")) {
  677. /* TODO: <annotation> support */
  678. trav = trav->next;
  679. }
  680. if (trav != NULL) {
  681. if (node_is_equal(trav,"group")) {
  682. schema_group(sdl, tns, trav, cur_type, NULL);
  683. trav = trav->next;
  684. } else if (node_is_equal(trav,"all")) {
  685. schema_all(sdl, tns, trav, cur_type, NULL);
  686. trav = trav->next;
  687. } else if (node_is_equal(trav,"choice")) {
  688. schema_choice(sdl, tns, trav, cur_type, NULL);
  689. trav = trav->next;
  690. } else if (node_is_equal(trav,"sequence")) {
  691. schema_sequence(sdl, tns, trav, cur_type, NULL);
  692. trav = trav->next;
  693. }
  694. }
  695. while (trav != NULL) {
  696. if (node_is_equal(trav,"attribute")) {
  697. schema_attribute(sdl, tns, trav, cur_type, NULL);
  698. } else if (node_is_equal(trav,"attributeGroup")) {
  699. schema_attributeGroup(sdl, tns, trav, cur_type, NULL);
  700. } else if (node_is_equal(trav,"anyAttribute")) {
  701. /* TODO: <anyAttribute> support */
  702. trav = trav->next;
  703. break;
  704. } else {
  705. soap_error1(E_ERROR, "Parsing Schema: unexpected <%s> in restriction", trav->name);
  706. }
  707. trav = trav->next;
  708. }
  709. if (trav != NULL) {
  710. soap_error1(E_ERROR, "Parsing Schema: unexpected <%s> in restriction", trav->name);
  711. }
  712. return TRUE;
  713. }
  714. static int schema_restriction_var_int(xmlNodePtr val, sdlRestrictionIntPtr *valptr)
  715. {
  716. xmlAttrPtr fixed, value;
  717. if ((*valptr) == NULL) {
  718. (*valptr) = emalloc(sizeof(sdlRestrictionInt));
  719. }
  720. memset((*valptr), 0, sizeof(sdlRestrictionInt));
  721. fixed = get_attribute(val->properties, "fixed");
  722. (*valptr)->fixed = FALSE;
  723. if (fixed != NULL) {
  724. if (!strncmp((char*)fixed->children->content, "true", sizeof("true")) ||
  725. !strncmp((char*)fixed->children->content, "1", sizeof("1")))
  726. (*valptr)->fixed = TRUE;
  727. }
  728. value = get_attribute(val->properties, "value");
  729. if (value == NULL) {
  730. soap_error0(E_ERROR, "Parsing Schema: missing restriction value");
  731. }
  732. (*valptr)->value = atoi((char*)value->children->content);
  733. return TRUE;
  734. }
  735. static int schema_restriction_var_char(xmlNodePtr val, sdlRestrictionCharPtr *valptr)
  736. {
  737. xmlAttrPtr fixed, value;
  738. if ((*valptr) == NULL) {
  739. (*valptr) = emalloc(sizeof(sdlRestrictionChar));
  740. }
  741. memset((*valptr), 0, sizeof(sdlRestrictionChar));
  742. fixed = get_attribute(val->properties, "fixed");
  743. (*valptr)->fixed = FALSE;
  744. if (fixed != NULL) {
  745. if (!strncmp((char*)fixed->children->content, "true", sizeof("true")) ||
  746. !strncmp((char*)fixed->children->content, "1", sizeof("1"))) {
  747. (*valptr)->fixed = TRUE;
  748. }
  749. }
  750. value = get_attribute(val->properties, "value");
  751. if (value == NULL) {
  752. soap_error0(E_ERROR, "Parsing Schema: missing restriction value");
  753. }
  754. (*valptr)->value = estrdup((char*)value->children->content);
  755. return TRUE;
  756. }
  757. /*
  758. From simpleContent (not supported):
  759. <extension
  760. base = QName
  761. id = ID
  762. {any attributes with non-schema namespace . . .}>
  763. Content: (annotation?, ((attribute | attributeGroup)*, anyAttribute?))
  764. </extension>
  765. */
  766. static int schema_extension_simpleContent(sdlPtr sdl, xmlAttrPtr tns, xmlNodePtr extType, sdlTypePtr cur_type)
  767. {
  768. xmlNodePtr trav;
  769. xmlAttrPtr base;
  770. base = get_attribute(extType->properties, "base");
  771. if (base != NULL) {
  772. char *type, *ns;
  773. xmlNsPtr nsptr;
  774. parse_namespace(base->children->content, &type, &ns);
  775. nsptr = xmlSearchNs(extType->doc, extType, BAD_CAST(ns));
  776. if (nsptr != NULL) {
  777. cur_type->encode = get_create_encoder(sdl, cur_type, nsptr->href, BAD_CAST(type));
  778. }
  779. if (type) {efree(type);}
  780. if (ns) {efree(ns);}
  781. } else {
  782. soap_error0(E_ERROR, "Parsing Schema: extension has no 'base' attribute");
  783. }
  784. trav = extType->children;
  785. if (trav != NULL && node_is_equal(trav,"annotation")) {
  786. /* TODO: <annotation> support */
  787. trav = trav->next;
  788. }
  789. while (trav != NULL) {
  790. if (node_is_equal(trav,"attribute")) {
  791. schema_attribute(sdl, tns, trav, cur_type, NULL);
  792. } else if (node_is_equal(trav,"attributeGroup")) {
  793. schema_attributeGroup(sdl, tns, trav, cur_type, NULL);
  794. } else if (node_is_equal(trav,"anyAttribute")) {
  795. /* TODO: <anyAttribute> support */
  796. trav = trav->next;
  797. break;
  798. } else {
  799. soap_error1(E_ERROR, "Parsing Schema: unexpected <%s> in extension", trav->name);
  800. }
  801. trav = trav->next;
  802. }
  803. if (trav != NULL) {
  804. soap_error1(E_ERROR, "Parsing Schema: unexpected <%s> in extension", trav->name);
  805. }
  806. return TRUE;
  807. }
  808. /*
  809. From complexContent:
  810. <extension
  811. base = QName
  812. id = ID
  813. {any attributes with non-schema namespace . . .}>
  814. Content: (annotation?, ((group | all | choice | sequence)?, ((attribute | attributeGroup)*, anyAttribute?)))
  815. </extension>
  816. */
  817. static int schema_extension_complexContent(sdlPtr sdl, xmlAttrPtr tns, xmlNodePtr extType, sdlTypePtr cur_type)
  818. {
  819. xmlNodePtr trav;
  820. xmlAttrPtr base;
  821. base = get_attribute(extType->properties, "base");
  822. if (base != NULL) {
  823. char *type, *ns;
  824. xmlNsPtr nsptr;
  825. parse_namespace(base->children->content, &type, &ns);
  826. nsptr = xmlSearchNs(extType->doc, extType, BAD_CAST(ns));
  827. if (nsptr != NULL) {
  828. cur_type->encode = get_create_encoder(sdl, cur_type, nsptr->href, BAD_CAST(type));
  829. }
  830. if (type) {efree(type);}
  831. if (ns) {efree(ns);}
  832. } else {
  833. soap_error0(E_ERROR, "Parsing Schema: extension has no 'base' attribute");
  834. }
  835. trav = extType->children;
  836. if (trav != NULL && node_is_equal(trav,"annotation")) {
  837. /* TODO: <annotation> support */
  838. trav = trav->next;
  839. }
  840. if (trav != NULL) {
  841. if (node_is_equal(trav,"group")) {
  842. schema_group(sdl, tns, trav, cur_type, NULL);
  843. trav = trav->next;
  844. } else if (node_is_equal(trav,"all")) {
  845. schema_all(sdl, tns, trav, cur_type, NULL);
  846. trav = trav->next;
  847. } else if (node_is_equal(trav,"choice")) {
  848. schema_choice(sdl, tns, trav, cur_type, NULL);
  849. trav = trav->next;
  850. } else if (node_is_equal(trav,"sequence")) {
  851. schema_sequence(sdl, tns, trav, cur_type, NULL);
  852. trav = trav->next;
  853. }
  854. }
  855. while (trav != NULL) {
  856. if (node_is_equal(trav,"attribute")) {
  857. schema_attribute(sdl, tns, trav, cur_type, NULL);
  858. } else if (node_is_equal(trav,"attributeGroup")) {
  859. schema_attributeGroup(sdl, tns, trav, cur_type, NULL);
  860. } else if (node_is_equal(trav,"anyAttribute")) {
  861. /* TODO: <anyAttribute> support */
  862. trav = trav->next;
  863. break;
  864. } else {
  865. soap_error1(E_ERROR, "Parsing Schema: unexpected <%s> in extension", trav->name);
  866. }
  867. trav = trav->next;
  868. }
  869. if (trav != NULL) {
  870. soap_error1(E_ERROR, "Parsing Schema: unexpected <%s> in extension", trav->name);
  871. }
  872. return TRUE;
  873. }
  874. void schema_min_max(xmlNodePtr node, sdlContentModelPtr model)
  875. {
  876. xmlAttrPtr attr = get_attribute(node->properties, "minOccurs");
  877. if (attr) {
  878. model->min_occurs = atoi((char*)attr->children->content);
  879. } else {
  880. model->min_occurs = 1;
  881. }
  882. attr = get_attribute(node->properties, "maxOccurs");
  883. if (attr) {
  884. if (!strncmp((char*)attr->children->content, "unbounded", sizeof("unbounded"))) {
  885. model->max_occurs = -1;
  886. } else {
  887. model->max_occurs = atoi((char*)attr->children->content);
  888. }
  889. } else {
  890. model->max_occurs = 1;
  891. }
  892. }
  893. /*
  894. <all
  895. id = ID
  896. maxOccurs = 1 : 1
  897. minOccurs = (0 | 1) : 1
  898. {any attributes with non-schema namespace . . .}>
  899. Content: (annotation?, element*)
  900. </all>
  901. */
  902. static int schema_all(sdlPtr sdl, xmlAttrPtr tns, xmlNodePtr all, sdlTypePtr cur_type, sdlContentModelPtr model)
  903. {
  904. xmlNodePtr trav;
  905. sdlContentModelPtr newModel;
  906. newModel = emalloc(sizeof(sdlContentModel));
  907. newModel->kind = XSD_CONTENT_ALL;
  908. newModel->u.content = emalloc(sizeof(HashTable));
  909. zend_hash_init(newModel->u.content, 0, NULL, delete_model, 0);
  910. if (model == NULL) {
  911. cur_type->model = newModel;
  912. } else {
  913. zend_hash_next_index_insert_ptr(model->u.content, newModel);
  914. }
  915. schema_min_max(all, newModel);
  916. trav = all->children;
  917. if (trav != NULL && node_is_equal(trav,"annotation")) {
  918. /* TODO: <annotation> support */
  919. trav = trav->next;
  920. }
  921. while (trav != NULL) {
  922. if (node_is_equal(trav,"element")) {
  923. schema_element(sdl, tns, trav, cur_type, newModel);
  924. } else {
  925. soap_error1(E_ERROR, "Parsing Schema: unexpected <%s> in all", trav->name);
  926. }
  927. trav = trav->next;
  928. }
  929. return TRUE;
  930. }
  931. /*
  932. <group
  933. name = NCName
  934. Content: (annotation?, (all | choice | sequence))
  935. </group>
  936. <group
  937. name = NCName
  938. ref = QName>
  939. Content: (annotation?)
  940. </group>
  941. */
  942. static int schema_group(sdlPtr sdl, xmlAttrPtr tns, xmlNodePtr groupType, sdlTypePtr cur_type, sdlContentModelPtr model)
  943. {
  944. xmlNodePtr trav;
  945. xmlAttrPtr ns, name, ref = NULL;
  946. sdlContentModelPtr newModel = NULL;
  947. ns = get_attribute(groupType->properties, "targetNamespace");
  948. if (ns == NULL) {
  949. ns = tns;
  950. }
  951. name = get_attribute(groupType->properties, "name");
  952. if (name == NULL) {
  953. name = ref = get_attribute(groupType->properties, "ref");
  954. }
  955. if (name) {
  956. smart_str key = {0};
  957. if (ref) {
  958. char *type, *ns;
  959. xmlNsPtr nsptr;
  960. parse_namespace(ref->children->content, &type, &ns);
  961. nsptr = xmlSearchNs(groupType->doc, groupType, BAD_CAST(ns));
  962. if (nsptr != NULL) {
  963. smart_str_appends(&key, (char*)nsptr->href);
  964. } else {
  965. xmlAttrPtr ns = get_attribute(groupType->properties, "targetNamespace");
  966. if (ns == NULL) {
  967. ns = tns;
  968. }
  969. if (ns) {
  970. smart_str_appends(&key, (char*)ns->children->content);
  971. }
  972. }
  973. smart_str_appendc(&key, ':');
  974. smart_str_appends(&key, type);
  975. smart_str_0(&key);
  976. newModel = emalloc(sizeof(sdlContentModel));
  977. newModel->kind = XSD_CONTENT_GROUP_REF;
  978. newModel->u.group_ref = estrndup(ZSTR_VAL(key.s), ZSTR_LEN(key.s));
  979. if (type) {efree(type);}
  980. if (ns) {efree(ns);}
  981. } else {
  982. newModel = emalloc(sizeof(sdlContentModel));
  983. newModel->kind = XSD_CONTENT_SEQUENCE; /* will be redefined */
  984. newModel->u.content = emalloc(sizeof(HashTable));
  985. zend_hash_init(newModel->u.content, 0, NULL, delete_model, 0);
  986. smart_str_appends(&key, (char*)ns->children->content);
  987. smart_str_appendc(&key, ':');
  988. smart_str_appends(&key, (char*)name->children->content);
  989. smart_str_0(&key);
  990. }
  991. if (cur_type == NULL) {
  992. sdlTypePtr newType;
  993. newType = emalloc(sizeof(sdlType));
  994. memset(newType, 0, sizeof(sdlType));
  995. if (sdl->groups == NULL) {
  996. sdl->groups = emalloc(sizeof(HashTable));
  997. zend_hash_init(sdl->groups, 0, NULL, delete_type, 0);
  998. }
  999. if (zend_hash_add_ptr(sdl->groups, key.s, newType) == NULL) {
  1000. soap_error1(E_ERROR, "Parsing Schema: group '%s' already defined", ZSTR_VAL(key.s));
  1001. }
  1002. cur_type = newType;
  1003. }
  1004. smart_str_free(&key);
  1005. if (model == NULL) {
  1006. cur_type->model = newModel;
  1007. } else {
  1008. zend_hash_next_index_insert_ptr(model->u.content, newModel);
  1009. }
  1010. } else {
  1011. soap_error0(E_ERROR, "Parsing Schema: group has no 'name' nor 'ref' attributes");
  1012. }
  1013. schema_min_max(groupType, newModel);
  1014. trav = groupType->children;
  1015. if (trav != NULL && node_is_equal(trav,"annotation")) {
  1016. /* TODO: <annotation> support */
  1017. trav = trav->next;
  1018. }
  1019. if (trav != NULL) {
  1020. if (node_is_equal(trav,"choice")) {
  1021. if (ref != NULL) {
  1022. soap_error0(E_ERROR, "Parsing Schema: group has both 'ref' attribute and subcontent");
  1023. }
  1024. newModel->kind = XSD_CONTENT_CHOICE;
  1025. schema_choice(sdl, tns, trav, cur_type, newModel);
  1026. trav = trav->next;
  1027. } else if (node_is_equal(trav,"sequence")) {
  1028. if (ref != NULL) {
  1029. soap_error0(E_ERROR, "Parsing Schema: group has both 'ref' attribute and subcontent");
  1030. }
  1031. newModel->kind = XSD_CONTENT_SEQUENCE;
  1032. schema_sequence(sdl, tns, trav, cur_type, newModel);
  1033. trav = trav->next;
  1034. } else if (node_is_equal(trav,"all")) {
  1035. if (ref != NULL) {
  1036. soap_error0(E_ERROR, "Parsing Schema: group has both 'ref' attribute and subcontent");
  1037. }
  1038. newModel->kind = XSD_CONTENT_ALL;
  1039. schema_all(sdl, tns, trav, cur_type, newModel);
  1040. trav = trav->next;
  1041. } else {
  1042. soap_error1(E_ERROR, "Parsing Schema: unexpected <%s> in group", trav->name);
  1043. }
  1044. }
  1045. if (trav != NULL) {
  1046. soap_error1(E_ERROR, "Parsing Schema: unexpected <%s> in group", trav->name);
  1047. }
  1048. return TRUE;
  1049. }
  1050. /*
  1051. <choice
  1052. id = ID
  1053. maxOccurs = (nonNegativeInteger | unbounded) : 1
  1054. minOccurs = nonNegativeInteger : 1
  1055. {any attributes with non-schema namespace . . .}>
  1056. Content: (annotation?, (element | group | choice | sequence | any)*)
  1057. </choice>
  1058. */
  1059. static int schema_choice(sdlPtr sdl, xmlAttrPtr tns, xmlNodePtr choiceType, sdlTypePtr cur_type, sdlContentModelPtr model)
  1060. {
  1061. xmlNodePtr trav;
  1062. sdlContentModelPtr newModel;
  1063. newModel = emalloc(sizeof(sdlContentModel));
  1064. newModel->kind = XSD_CONTENT_CHOICE;
  1065. newModel->u.content = emalloc(sizeof(HashTable));
  1066. zend_hash_init(newModel->u.content, 0, NULL, delete_model, 0);
  1067. if (model == NULL) {
  1068. cur_type->model = newModel;
  1069. } else {
  1070. zend_hash_next_index_insert_ptr(model->u.content, newModel);
  1071. }
  1072. schema_min_max(choiceType, newModel);
  1073. trav = choiceType->children;
  1074. if (trav != NULL && node_is_equal(trav,"annotation")) {
  1075. /* TODO: <annotation> support */
  1076. trav = trav->next;
  1077. }
  1078. while (trav != NULL) {
  1079. if (node_is_equal(trav,"element")) {
  1080. schema_element(sdl, tns, trav, cur_type, newModel);
  1081. } else if (node_is_equal(trav,"group")) {
  1082. schema_group(sdl, tns, trav, cur_type, newModel);
  1083. } else if (node_is_equal(trav,"choice")) {
  1084. schema_choice(sdl, tns, trav, cur_type, newModel);
  1085. } else if (node_is_equal(trav,"sequence")) {
  1086. schema_sequence(sdl, tns, trav, cur_type, newModel);
  1087. } else if (node_is_equal(trav,"any")) {
  1088. schema_any(sdl, tns, trav, cur_type, newModel);
  1089. } else {
  1090. soap_error1(E_ERROR, "Parsing Schema: unexpected <%s> in choice", trav->name);
  1091. }
  1092. trav = trav->next;
  1093. }
  1094. return TRUE;
  1095. }
  1096. /*
  1097. <sequence
  1098. id = ID
  1099. maxOccurs = (nonNegativeInteger | unbounded) : 1
  1100. minOccurs = nonNegativeInteger : 1
  1101. {any attributes with non-schema namespace . . .}>
  1102. Content: (annotation?, (element | group | choice | sequence | any)*)
  1103. </sequence>
  1104. */
  1105. static int schema_sequence(sdlPtr sdl, xmlAttrPtr tns, xmlNodePtr seqType, sdlTypePtr cur_type, sdlContentModelPtr model)
  1106. {
  1107. xmlNodePtr trav;
  1108. sdlContentModelPtr newModel;
  1109. newModel = emalloc(sizeof(sdlContentModel));
  1110. newModel->kind = XSD_CONTENT_SEQUENCE;
  1111. newModel->u.content = emalloc(sizeof(HashTable));
  1112. zend_hash_init(newModel->u.content, 0, NULL, delete_model, 0);
  1113. if (model == NULL) {
  1114. cur_type->model = newModel;
  1115. } else {
  1116. zend_hash_next_index_insert_ptr(model->u.content, newModel);
  1117. }
  1118. schema_min_max(seqType, newModel);
  1119. trav = seqType->children;
  1120. if (trav != NULL && node_is_equal(trav,"annotation")) {
  1121. /* TODO: <annotation> support */
  1122. trav = trav->next;
  1123. }
  1124. while (trav != NULL) {
  1125. if (node_is_equal(trav,"element")) {
  1126. schema_element(sdl, tns, trav, cur_type, newModel);
  1127. } else if (node_is_equal(trav,"group")) {
  1128. schema_group(sdl, tns, trav, cur_type, newModel);
  1129. } else if (node_is_equal(trav,"choice")) {
  1130. schema_choice(sdl, tns, trav, cur_type, newModel);
  1131. } else if (node_is_equal(trav,"sequence")) {
  1132. schema_sequence(sdl, tns, trav, cur_type, newModel);
  1133. } else if (node_is_equal(trav,"any")) {
  1134. schema_any(sdl, tns, trav, cur_type, newModel);
  1135. } else {
  1136. soap_error1(E_ERROR, "Parsing Schema: unexpected <%s> in sequence", trav->name);
  1137. }
  1138. trav = trav->next;
  1139. }
  1140. return TRUE;
  1141. }
  1142. /*
  1143. <any
  1144. id = ID
  1145. maxOccurs = (nonNegativeInteger | unbounded) : 1
  1146. minOccurs = nonNegativeInteger : 1
  1147. namespace = ((##any | ##other) | List of (anyURI | (##targetNamespace | ##local)) ) : ##any
  1148. processContents = (lax | skip | strict) : strict
  1149. {any attributes with non-schema namespace . . .}>
  1150. Content: (annotation?)
  1151. </any>
  1152. */
  1153. static int schema_any(sdlPtr sdl, xmlAttrPtr tns, xmlNodePtr anyType, sdlTypePtr cur_type, sdlContentModelPtr model)
  1154. {
  1155. if (model != NULL) {
  1156. sdlContentModelPtr newModel;
  1157. newModel = emalloc(sizeof(sdlContentModel));
  1158. newModel->kind = XSD_CONTENT_ANY;
  1159. schema_min_max(anyType, newModel);
  1160. zend_hash_next_index_insert_ptr(model->u.content, newModel);
  1161. }
  1162. return TRUE;
  1163. }
  1164. /*
  1165. <complexContent
  1166. id = ID
  1167. mixed = boolean
  1168. {any attributes with non-schema namespace . . .}>
  1169. Content: (annotation?, (restriction | extension))
  1170. </complexContent>
  1171. */
  1172. static int schema_complexContent(sdlPtr sdl, xmlAttrPtr tns, xmlNodePtr compCont, sdlTypePtr cur_type)
  1173. {
  1174. xmlNodePtr trav;
  1175. trav = compCont->children;
  1176. if (trav != NULL && node_is_equal(trav,"annotation")) {
  1177. /* TODO: <annotation> support */
  1178. trav = trav->next;
  1179. }
  1180. if (trav != NULL) {
  1181. if (node_is_equal(trav, "restriction")) {
  1182. cur_type->kind = XSD_TYPEKIND_RESTRICTION;
  1183. schema_restriction_complexContent(sdl, tns, trav, cur_type);
  1184. trav = trav->next;
  1185. } else if (node_is_equal(trav, "extension")) {
  1186. cur_type->kind = XSD_TYPEKIND_EXTENSION;
  1187. schema_extension_complexContent(sdl, tns, trav, cur_type);
  1188. trav = trav->next;
  1189. } else {
  1190. soap_error1(E_ERROR, "Parsing Schema: unexpected <%s> in complexContent", trav->name);
  1191. }
  1192. } else {
  1193. soap_error0(E_ERROR, "Parsing Schema: <restriction> or <extension> expected in complexContent");
  1194. }
  1195. if (trav != NULL) {
  1196. soap_error1(E_ERROR, "Parsing Schema: unexpected <%s> in complexContent", trav->name);
  1197. }
  1198. return TRUE;
  1199. }
  1200. /*
  1201. <complexType
  1202. abstract = boolean : false
  1203. block = (#all | List of (extension | restriction))
  1204. final = (#all | List of (extension | restriction))
  1205. id = ID
  1206. mixed = boolean : false
  1207. name = NCName
  1208. {any attributes with non-schema namespace . . .}>
  1209. Content: (annotation?, (simpleContent | complexContent | ((group | all | choice | sequence)?, ((attribute | attributeGroup)*, anyAttribute?))))
  1210. </complexType>
  1211. */
  1212. static int schema_complexType(sdlPtr sdl, xmlAttrPtr tns, xmlNodePtr compType, sdlTypePtr cur_type)
  1213. {
  1214. xmlNodePtr trav;
  1215. xmlAttrPtr attrs, name, ns;
  1216. attrs = compType->properties;
  1217. ns = get_attribute(attrs, "targetNamespace");
  1218. if (ns == NULL) {
  1219. ns = tns;
  1220. }
  1221. name = get_attribute(attrs, "name");
  1222. if (cur_type != NULL) {
  1223. /* Anonymous type inside <element> */
  1224. sdlTypePtr newType, ptr;
  1225. newType = emalloc(sizeof(sdlType));
  1226. memset(newType, 0, sizeof(sdlType));
  1227. newType->kind = XSD_TYPEKIND_COMPLEX;
  1228. if (name != NULL) {
  1229. newType->name = estrdup((char*)name->children->content);
  1230. newType->namens = estrdup((char*)ns->children->content);
  1231. } else {
  1232. newType->name = estrdup(cur_type->name);
  1233. newType->namens = estrdup(cur_type->namens);
  1234. }
  1235. ptr = zend_hash_next_index_insert_ptr(sdl->types, newType);
  1236. if (sdl->encoders == NULL) {
  1237. sdl->encoders = emalloc(sizeof(HashTable));
  1238. zend_hash_init(sdl->encoders, 0, NULL, delete_encoder, 0);
  1239. }
  1240. cur_type->encode = emalloc(sizeof(encode));
  1241. memset(cur_type->encode, 0, sizeof(encode));
  1242. cur_type->encode->details.ns = estrdup(newType->namens);
  1243. cur_type->encode->details.type_str = estrdup(newType->name);
  1244. cur_type->encode->details.sdl_type = ptr;
  1245. cur_type->encode->to_xml = sdl_guess_convert_xml;
  1246. cur_type->encode->to_zval = sdl_guess_convert_zval;
  1247. zend_hash_next_index_insert_ptr(sdl->encoders, cur_type->encode);
  1248. cur_type = ptr;
  1249. } else if (name) {
  1250. sdlTypePtr newType, ptr;
  1251. newType = emalloc(sizeof(sdlType));
  1252. memset(newType, 0, sizeof(sdlType));
  1253. newType->kind = XSD_TYPEKIND_COMPLEX;
  1254. newType->name = estrdup((char*)name->children->content);
  1255. newType->namens = estrdup((char*)ns->children->content);
  1256. ptr = zend_hash_next_index_insert_ptr(sdl->types, newType);
  1257. cur_type = ptr;
  1258. create_encoder(sdl, cur_type, ns->children->content, name->children->content);
  1259. } else {
  1260. soap_error0(E_ERROR, "Parsing Schema: complexType has no 'name' attribute");
  1261. return FALSE;
  1262. }
  1263. trav = compType->children;
  1264. if (trav != NULL && node_is_equal(trav, "annotation")) {
  1265. /* TODO: <annotation> support */
  1266. trav = trav->next;
  1267. }
  1268. if (trav != NULL) {
  1269. if (node_is_equal(trav,"simpleContent")) {
  1270. schema_simpleContent(sdl, tns, trav, cur_type);
  1271. trav = trav->next;
  1272. } else if (node_is_equal(trav,"complexContent")) {
  1273. schema_complexContent(sdl, tns, trav, cur_type);
  1274. trav = trav->next;
  1275. } else {
  1276. if (node_is_equal(trav,"group")) {
  1277. schema_group(sdl, tns, trav, cur_type, NULL);
  1278. trav = trav->next;
  1279. } else if (node_is_equal(trav,"all")) {
  1280. schema_all(sdl, tns, trav, cur_type, NULL);
  1281. trav = trav->next;
  1282. } else if (node_is_equal(trav,"choice")) {
  1283. schema_choice(sdl, tns, trav, cur_type, NULL);
  1284. trav = trav->next;
  1285. } else if (node_is_equal(trav,"sequence")) {
  1286. schema_sequence(sdl, tns, trav, cur_type, NULL);
  1287. trav = trav->next;
  1288. }
  1289. while (trav != NULL) {
  1290. if (node_is_equal(trav,"attribute")) {
  1291. schema_attribute(sdl, tns, trav, cur_type, NULL);
  1292. } else if (node_is_equal(trav,"attributeGroup")) {
  1293. schema_attributeGroup(sdl, tns, trav, cur_type, NULL);
  1294. } else if (node_is_equal(trav,"anyAttribute")) {
  1295. /* TODO: <anyAttribute> support */
  1296. trav = trav->next;
  1297. break;
  1298. } else {
  1299. soap_error1(E_ERROR, "Parsing Schema: unexpected <%s> in complexType", trav->name);
  1300. }
  1301. trav = trav->next;
  1302. }
  1303. }
  1304. }
  1305. if (trav != NULL) {
  1306. soap_error1(E_ERROR, "Parsing Schema: unexpected <%s> in complexType", trav->name);
  1307. }
  1308. return TRUE;
  1309. }
  1310. /*
  1311. <element
  1312. abstract = boolean : false
  1313. block = (#all | List of (extension | restriction | substitution))
  1314. default = string
  1315. final = (#all | List of (extension | restriction))
  1316. fixed = string
  1317. form = (qualified | unqualified)
  1318. id = ID
  1319. maxOccurs = (nonNegativeInteger | unbounded) : 1
  1320. minOccurs = nonNegativeInteger : 1
  1321. name = NCName
  1322. nillable = boolean : false
  1323. ref = QName
  1324. substitutionGroup = QName
  1325. type = QName
  1326. {any attributes with non-schema namespace . . .}>
  1327. Content: (annotation?, ((simpleType | complexType)?, (unique | key | keyref)*))
  1328. </element>
  1329. */
  1330. static int schema_element(sdlPtr sdl, xmlAttrPtr tns, xmlNodePtr element, sdlTypePtr cur_type, sdlContentModelPtr model)
  1331. {
  1332. xmlNodePtr trav;
  1333. xmlAttrPtr attrs, attr, ns, name, type, ref = NULL;
  1334. attrs = element->properties;
  1335. ns = get_attribute(attrs, "targetNamespace");
  1336. if (ns == NULL) {
  1337. ns = tns;
  1338. }
  1339. name = get_attribute(attrs, "name");
  1340. if (name == NULL) {
  1341. name = ref = get_attribute(attrs, "ref");
  1342. }
  1343. if (name) {
  1344. HashTable *addHash;
  1345. sdlTypePtr newType;
  1346. smart_str key = {0};
  1347. newType = emalloc(sizeof(sdlType));
  1348. memset(newType, 0, sizeof(sdlType));
  1349. if (ref) {
  1350. smart_str nscat = {0};
  1351. char *type, *ns;
  1352. xmlNsPtr nsptr;
  1353. parse_namespace(ref->children->content, &type, &ns);
  1354. nsptr = xmlSearchNs(element->doc, element, BAD_CAST(ns));
  1355. if (nsptr != NULL) {
  1356. smart_str_appends(&nscat, (char*)nsptr->href);
  1357. newType->namens = estrdup((char*)nsptr->href);
  1358. } else {
  1359. xmlAttrPtr ns = get_attribute(attrs, "targetNamespace");
  1360. if (ns == NULL) {
  1361. ns = tns;
  1362. }
  1363. if (ns) {
  1364. smart_str_appends(&nscat, (char*)ns->children->content);
  1365. }
  1366. }
  1367. smart_str_appendc(&nscat, ':');
  1368. smart_str_appends(&nscat, type);
  1369. newType->name = estrdup(type);
  1370. smart_str_0(&nscat);
  1371. if (type) {efree(type);}
  1372. if (ns) {efree(ns);}
  1373. newType->ref = estrndup(ZSTR_VAL(nscat.s), ZSTR_LEN(nscat.s));
  1374. smart_str_free(&nscat);
  1375. } else {
  1376. newType->name = estrdup((char*)name->children->content);
  1377. newType->namens = estrdup((char*)ns->children->content);
  1378. }
  1379. newType->nillable = FALSE;
  1380. if (cur_type == NULL) {
  1381. if (sdl->elements == NULL) {
  1382. sdl->elements = emalloc(sizeof(HashTable));
  1383. zend_hash_init(sdl->elements, 0, NULL, delete_type, 0);
  1384. }
  1385. addHash = sdl->elements;
  1386. smart_str_appends(&key, newType->namens);
  1387. smart_str_appendc(&key, ':');
  1388. smart_str_appends(&key, newType->name);
  1389. } else {
  1390. if (cur_type->elements == NULL) {
  1391. cur_type->elements = emalloc(sizeof(HashTable));
  1392. zend_hash_init(cur_type->elements, 0, NULL, delete_type, 0);
  1393. }
  1394. addHash = cur_type->elements;
  1395. smart_str_appends(&key, newType->name);
  1396. }
  1397. smart_str_0(&key);
  1398. if (zend_hash_add_ptr(addHash, key.s, newType) == NULL) {
  1399. if (cur_type == NULL) {
  1400. soap_error1(E_ERROR, "Parsing Schema: element '%s' already defined", ZSTR_VAL(key.s));
  1401. } else {
  1402. zend_hash_next_index_insert_ptr(addHash, newType);
  1403. }
  1404. }
  1405. smart_str_free(&key);
  1406. if (model != NULL) {
  1407. sdlContentModelPtr newModel = emalloc(sizeof(sdlContentModel));
  1408. newModel->kind = XSD_CONTENT_ELEMENT;
  1409. newModel->u.element = newType;
  1410. schema_min_max(element, newModel);
  1411. zend_hash_next_index_insert_ptr(model->u.content, newModel);
  1412. }
  1413. cur_type = newType;
  1414. } else {
  1415. soap_error0(E_ERROR, "Parsing Schema: element has no 'name' nor 'ref' attributes");
  1416. }
  1417. /* nillable = boolean : false */
  1418. attrs = element->properties;
  1419. attr = get_attribute(attrs, "nillable");
  1420. if (attr) {
  1421. if (ref != NULL) {
  1422. soap_error0(E_ERROR, "Parsing Schema: element has both 'ref' and 'nillable' attributes");
  1423. }
  1424. if (!stricmp((char*)attr->children->content, "true") ||
  1425. !stricmp((char*)attr->children->content, "1")) {
  1426. cur_type->nillable = TRUE;
  1427. } else {
  1428. cur_type->nillable = FALSE;
  1429. }
  1430. } else {
  1431. cur_type->nillable = FALSE;
  1432. }
  1433. attr = get_attribute(attrs, "fixed");
  1434. if (attr) {
  1435. if (ref != NULL) {
  1436. soap_error0(E_ERROR, "Parsing Schema: element has both 'ref' and 'fixed' attributes");
  1437. }
  1438. cur_type->fixed = estrdup((char*)attr->children->content);
  1439. }
  1440. attr = get_attribute(attrs, "default");
  1441. if (attr) {
  1442. if (ref != NULL) {
  1443. soap_error0(E_ERROR, "Parsing Schema: element has both 'ref' and 'fixed' attributes");
  1444. } else if (ref != NULL) {
  1445. soap_error0(E_ERROR, "Parsing Schema: element has both 'default' and 'fixed' attributes");
  1446. }
  1447. cur_type->def = estrdup((char*)attr->children->content);
  1448. }
  1449. /* form */
  1450. attr = get_attribute(attrs, "form");
  1451. if (attr) {
  1452. if (strncmp((char*)attr->children->content, "qualified", sizeof("qualified")) == 0) {
  1453. cur_type->form = XSD_FORM_QUALIFIED;
  1454. } else if (strncmp((char*)attr->children->content, "unqualified", sizeof("unqualified")) == 0) {
  1455. cur_type->form = XSD_FORM_UNQUALIFIED;
  1456. } else {
  1457. cur_type->form = XSD_FORM_DEFAULT;
  1458. }
  1459. } else {
  1460. cur_type->form = XSD_FORM_DEFAULT;
  1461. }
  1462. if (cur_type->form == XSD_FORM_DEFAULT) {
  1463. xmlNodePtr parent = element->parent;
  1464. while (parent) {
  1465. if (node_is_equal_ex(parent, "schema", SCHEMA_NAMESPACE)) {
  1466. xmlAttrPtr def;
  1467. def = get_attribute(parent->properties, "elementFormDefault");
  1468. if(def == NULL || strncmp((char*)def->children->content, "qualified", sizeof("qualified"))) {
  1469. cur_type->form = XSD_FORM_UNQUALIFIED;
  1470. } else {
  1471. cur_type->form = XSD_FORM_QUALIFIED;
  1472. }
  1473. break;
  1474. }
  1475. parent = parent->parent;
  1476. }
  1477. if (parent == NULL) {
  1478. cur_type->form = XSD_FORM_UNQUALIFIED;
  1479. }
  1480. }
  1481. /* type = QName */
  1482. type = get_attribute(attrs, "type");
  1483. if (type) {
  1484. char *cptype, *str_ns;
  1485. xmlNsPtr nsptr;
  1486. if (ref != NULL) {
  1487. soap_error0(E_ERROR, "Parsing Schema: element has both 'ref' and 'type' attributes");
  1488. }
  1489. parse_namespace(type->children->content, &cptype, &str_ns);
  1490. nsptr = xmlSearchNs(element->doc, element, BAD_CAST(str_ns));
  1491. if (nsptr != NULL) {
  1492. cur_type->encode = get_create_encoder(sdl, cur_type, nsptr->href, BAD_CAST(cptype));
  1493. }
  1494. if (str_ns) {efree(str_ns);}
  1495. if (cptype) {efree(cptype);}
  1496. }
  1497. trav = element->children;
  1498. if (trav != NULL && node_is_equal(trav, "annotation")) {
  1499. /* TODO: <annotation> support */
  1500. trav = trav->next;
  1501. }
  1502. if (trav != NULL) {
  1503. if (node_is_equal(trav,"simpleType")) {
  1504. if (ref != NULL) {
  1505. soap_error0(E_ERROR, "Parsing Schema: element has both 'ref' attribute and subtype");
  1506. } else if (type != NULL) {
  1507. soap_error0(E_ERROR, "Parsing Schema: element has both 'type' attribute and subtype");
  1508. }
  1509. schema_simpleType(sdl, tns, trav, cur_type);
  1510. trav = trav->next;
  1511. } else if (node_is_equal(trav,"complexType")) {
  1512. if (ref != NULL) {
  1513. soap_error0(E_ERROR, "Parsing Schema: element has both 'ref' attribute and subtype");
  1514. } else if (type != NULL) {
  1515. soap_error0(E_ERROR, "Parsing Schema: element has both 'type' attribute and subtype");
  1516. }
  1517. schema_complexType(sdl, tns, trav, cur_type);
  1518. trav = trav->next;
  1519. }
  1520. }
  1521. while (trav != NULL) {
  1522. if (node_is_equal(trav,"unique")) {
  1523. /* TODO: <unique> support */
  1524. } else if (node_is_equal(trav,"key")) {
  1525. /* TODO: <key> support */
  1526. } else if (node_is_equal(trav,"keyref")) {
  1527. /* TODO: <keyref> support */
  1528. } else {
  1529. soap_error1(E_ERROR, "Parsing Schema: unexpected <%s> in element", trav->name);
  1530. }
  1531. trav = trav->next;
  1532. }
  1533. return TRUE;
  1534. }
  1535. /*
  1536. <attribute
  1537. default = string
  1538. fixed = string
  1539. form = (qualified | unqualified)
  1540. id = ID
  1541. name = NCName
  1542. ref = QName
  1543. type = QName
  1544. use = (optional | prohibited | required) : optional
  1545. {any attributes with non-schema namespace . . .}>
  1546. Content: (annotation?, (simpleType?))
  1547. </attribute>
  1548. */
  1549. static int schema_attribute(sdlPtr sdl, xmlAttrPtr tns, xmlNodePtr attrType, sdlTypePtr cur_type, sdlCtx *ctx)
  1550. {
  1551. sdlAttributePtr newAttr;
  1552. xmlAttrPtr attr, name, ref = NULL, type = NULL;
  1553. xmlNodePtr trav;
  1554. name = get_attribute(attrType->properties, "name");
  1555. if (name == NULL) {
  1556. name = ref = get_attribute(attrType->properties, "ref");
  1557. }
  1558. if (name) {
  1559. HashTable *addHash;
  1560. smart_str key = {0};
  1561. newAttr = emalloc(sizeof(sdlAttribute));
  1562. memset(newAttr, 0, sizeof(sdlAttribute));
  1563. if (ref) {
  1564. char *attr_name, *ns;
  1565. xmlNsPtr nsptr;
  1566. parse_namespace(ref->children->content, &attr_name, &ns);
  1567. nsptr = xmlSearchNs(attrType->doc, attrType, BAD_CAST(ns));
  1568. if (nsptr != NULL) {
  1569. smart_str_appends(&key, (char*)nsptr->href);
  1570. newAttr->namens = estrdup((char*)nsptr->href);
  1571. } else {
  1572. xmlAttrPtr ns = get_attribute(attrType->properties, "targetNamespace");
  1573. if (ns == NULL) {
  1574. ns = tns;
  1575. }
  1576. if (ns) {
  1577. smart_str_appends(&key, (char*)ns->children->content);
  1578. }
  1579. }
  1580. smart_str_appendc(&key, ':');
  1581. smart_str_appends(&key, attr_name);
  1582. smart_str_0(&key);
  1583. newAttr->ref = estrndup(ZSTR_VAL(key.s), ZSTR_LEN(key.s));
  1584. if (attr_name) {efree(attr_name);}
  1585. if (ns) {efree(ns);}
  1586. } else {
  1587. xmlAttrPtr ns;
  1588. ns = get_attribute(attrType->properties, "targetNamespace");
  1589. if (ns == NULL) {
  1590. ns = tns;
  1591. }
  1592. if (ns != NULL) {
  1593. smart_str_appends(&key, (char*)ns->children->content);
  1594. smart_str_appendc(&key, ':');
  1595. newAttr->namens = estrdup((char*)ns->children->content);
  1596. }
  1597. smart_str_appends(&key, (char*)name->children->content);
  1598. smart_str_0(&key);
  1599. }
  1600. if (cur_type == NULL) {
  1601. addHash = ctx->attributes;
  1602. } else {
  1603. if (cur_type->attributes == NULL) {
  1604. cur_type->attributes = emalloc(sizeof(HashTable));
  1605. zend_hash_init(cur_type->attributes, 0, NULL, delete_attribute, 0);
  1606. }
  1607. addHash = cur_type->attributes;
  1608. }
  1609. if (zend_hash_add_ptr(addHash, key.s, newAttr) == NULL) {
  1610. soap_error1(E_ERROR, "Parsing Schema: attribute '%s' already defined", ZSTR_VAL(key.s));
  1611. }
  1612. smart_str_free(&key);
  1613. } else{
  1614. soap_error0(E_ERROR, "Parsing Schema: attribute has no 'name' nor 'ref' attributes");
  1615. return FALSE; /* the above call is noreturn, but not marked as such */
  1616. }
  1617. /* type = QName */
  1618. type = get_attribute(attrType->properties, "type");
  1619. if (type) {
  1620. char *cptype, *str_ns;
  1621. xmlNsPtr nsptr;
  1622. if (ref != NULL) {
  1623. soap_error0(E_ERROR, "Parsing Schema: attribute has both 'ref' and 'type' attributes");
  1624. }
  1625. parse_namespace(type->children->content, &cptype, &str_ns);
  1626. nsptr = xmlSearchNs(attrType->doc, attrType, BAD_CAST(str_ns));
  1627. if (nsptr != NULL) {
  1628. newAttr->encode = get_create_encoder(sdl, cur_type, nsptr->href, BAD_CAST(cptype));
  1629. }
  1630. if (str_ns) {efree(str_ns);}
  1631. if (cptype) {efree(cptype);}
  1632. }
  1633. attr = attrType->properties;
  1634. while (attr != NULL) {
  1635. if (attr_is_equal_ex(attr, "default", SCHEMA_NAMESPACE)) {
  1636. newAttr->def = estrdup((char*)attr->children->content);
  1637. } else if (attr_is_equal_ex(attr, "fixed", SCHEMA_NAMESPACE)) {
  1638. newAttr->fixed = estrdup((char*)attr->children->content);
  1639. } else if (attr_is_equal_ex(attr, "form", SCHEMA_NAMESPACE)) {
  1640. if (strncmp((char*)attr->children->content, "qualified", sizeof("qualified")) == 0) {
  1641. newAttr->form = XSD_FORM_QUALIFIED;
  1642. } else if (strncmp((char*)attr->children->content, "unqualified", sizeof("unqualified")) == 0) {
  1643. newAttr->form = XSD_FORM_UNQUALIFIED;
  1644. } else {
  1645. newAttr->form = XSD_FORM_DEFAULT;
  1646. }
  1647. } else if (attr_is_equal_ex(attr, "id", SCHEMA_NAMESPACE)) {
  1648. /* skip */
  1649. } else if (attr_is_equal_ex(attr, "name", SCHEMA_NAMESPACE)) {
  1650. newAttr->name = estrdup((char*)attr->children->content);
  1651. } else if (attr_is_equal_ex(attr, "ref", SCHEMA_NAMESPACE)) {
  1652. /* already processed */
  1653. } else if (attr_is_equal_ex(attr, "type", SCHEMA_NAMESPACE)) {
  1654. /* already processed */
  1655. } else if (attr_is_equal_ex(attr, "use", SCHEMA_NAMESPACE)) {
  1656. if (strncmp((char*)attr->children->content, "prohibited", sizeof("prohibited")) == 0) {
  1657. newAttr->use = XSD_USE_PROHIBITED;
  1658. } else if (strncmp((char*)attr->children->content, "required", sizeof("required")) == 0) {
  1659. newAttr->use = XSD_USE_REQUIRED;
  1660. } else if (strncmp((char*)attr->children->content, "optional", sizeof("optional")) == 0) {
  1661. newAttr->use = XSD_USE_OPTIONAL;
  1662. } else {
  1663. newAttr->use = XSD_USE_DEFAULT;
  1664. }
  1665. } else {
  1666. xmlNsPtr nsPtr = attr_find_ns(attr);
  1667. if (strncmp((char*)nsPtr->href, SCHEMA_NAMESPACE, sizeof(SCHEMA_NAMESPACE))) {
  1668. smart_str key2 = {0};
  1669. sdlExtraAttributePtr ext;
  1670. xmlNsPtr nsptr;
  1671. char *value, *ns;
  1672. ext = emalloc(sizeof(sdlExtraAttribute));
  1673. memset(ext, 0, sizeof(sdlExtraAttribute));
  1674. parse_namespace(attr->children->content, &value, &ns);
  1675. nsptr = xmlSearchNs(attr->doc, attr->parent, BAD_CAST(ns));
  1676. if (nsptr) {
  1677. ext->ns = estrdup((char*)nsptr->href);
  1678. ext->val = estrdup(value);
  1679. } else {
  1680. ext->val = estrdup((char*)attr->children->content);
  1681. }
  1682. if (ns) {efree(ns);}
  1683. efree(value);
  1684. if (!newAttr->extraAttributes) {
  1685. newAttr->extraAttributes = emalloc(sizeof(HashTable));
  1686. zend_hash_init(newAttr->extraAttributes, 0, NULL, delete_extra_attribute, 0);
  1687. }
  1688. smart_str_appends(&key2, (char*)nsPtr->href);
  1689. smart_str_appendc(&key2, ':');
  1690. smart_str_appends(&key2, (char*)attr->name);
  1691. smart_str_0(&key2);
  1692. zend_hash_add_ptr(newAttr->extraAttributes, key2.s, ext);
  1693. smart_str_free(&key2);
  1694. }
  1695. }
  1696. attr = attr->next;
  1697. }
  1698. if (newAttr->form == XSD_FORM_DEFAULT) {
  1699. xmlNodePtr parent = attrType->parent;
  1700. while (parent) {
  1701. if (node_is_equal_ex(parent, "schema", SCHEMA_NAMESPACE)) {
  1702. xmlAttrPtr def;
  1703. def = get_attribute(parent->properties, "attributeFormDefault");
  1704. if(def == NULL || strncmp((char*)def->children->content, "qualified", sizeof("qualified"))) {
  1705. newAttr->form = XSD_FORM_UNQUALIFIED;
  1706. } else {
  1707. newAttr->form = XSD_FORM_QUALIFIED;
  1708. }
  1709. break;
  1710. }
  1711. parent = parent->parent;
  1712. }
  1713. if (parent == NULL) {
  1714. newAttr->form = XSD_FORM_UNQUALIFIED;
  1715. }
  1716. }
  1717. trav = attrType->children;
  1718. if (trav != NULL && node_is_equal(trav, "annotation")) {
  1719. /* TODO: <annotation> support */
  1720. trav = trav->next;
  1721. }
  1722. if (trav != NULL) {
  1723. if (node_is_equal(trav,"simpleType")) {
  1724. sdlTypePtr dummy_type;
  1725. zval zv;
  1726. if (ref != NULL) {
  1727. soap_error0(E_ERROR, "Parsing Schema: attribute has both 'ref' attribute and subtype");
  1728. } else if (type != NULL) {
  1729. soap_error0(E_ERROR, "Parsing Schema: attribute has both 'type' attribute and subtype");
  1730. }
  1731. dummy_type = emalloc(sizeof(sdlType));
  1732. memset(dummy_type, 0, sizeof(sdlType));
  1733. {
  1734. char buf[MAX_LENGTH_OF_LONG + 1];
  1735. char *res = zend_print_long_to_buf(buf + sizeof(buf) - 1, zend_hash_num_elements(sdl->types));
  1736. char *str = emalloc(sizeof("anonymous")-1 + (buf + sizeof(buf) - res));
  1737. memcpy(str, "anonymous", sizeof("anonymous")-1);
  1738. memcpy(str + sizeof("anonymous")-1, res, buf + sizeof(buf) - res);
  1739. dummy_type->name = str;
  1740. }
  1741. dummy_type->namens = estrdup((char*)tns->children->content);
  1742. schema_simpleType(sdl, tns, trav, dummy_type);
  1743. newAttr->encode = dummy_type->encode;
  1744. ZVAL_PTR(&zv, dummy_type);
  1745. delete_type(&zv);
  1746. trav = trav->next;
  1747. }
  1748. }
  1749. if (trav != NULL) {
  1750. soap_error1(E_ERROR, "Parsing Schema: unexpected <%s> in attribute", trav->name);
  1751. }
  1752. return TRUE;
  1753. }
  1754. static int schema_attributeGroup(sdlPtr sdl, xmlAttrPtr tns, xmlNodePtr attrGroup, sdlTypePtr cur_type, sdlCtx *ctx)
  1755. {
  1756. xmlNodePtr trav;
  1757. xmlAttrPtr name, ref = NULL;
  1758. name = get_attribute(attrGroup->properties, "name");
  1759. if (name == NULL) {
  1760. name = ref = get_attribute(attrGroup->properties, "ref");
  1761. }
  1762. if (name) {
  1763. if (cur_type == NULL) {
  1764. xmlAttrPtr ns;
  1765. sdlTypePtr newType;
  1766. smart_str key = {0};
  1767. ns = get_attribute(attrGroup->properties, "targetNamespace");
  1768. if (ns == NULL) {
  1769. ns = tns;
  1770. }
  1771. newType = emalloc(sizeof(sdlType));
  1772. memset(newType, 0, sizeof(sdlType));
  1773. newType->name = estrdup((char*)name->children->content);
  1774. newType->namens = estrdup((char*)ns->children->content);
  1775. smart_str_appends(&key, newType->namens);
  1776. smart_str_appendc(&key, ':');
  1777. smart_str_appends(&key, newType->name);
  1778. smart_str_0(&key);
  1779. if (zend_hash_add_ptr(ctx->attributeGroups, key.s, newType) == NULL) {
  1780. soap_error1(E_ERROR, "Parsing Schema: attributeGroup '%s' already defined", ZSTR_VAL(key.s));
  1781. }
  1782. cur_type = newType;
  1783. smart_str_free(&key);
  1784. } else if (ref) {
  1785. sdlAttributePtr newAttr;
  1786. char *group_name, *ns;
  1787. smart_str key = {0};
  1788. xmlNsPtr nsptr;
  1789. if (cur_type->attributes == NULL) {
  1790. cur_type->attributes = emalloc(sizeof(HashTable));
  1791. zend_hash_init(cur_type->attributes, 0, NULL, delete_attribute, 0);
  1792. }
  1793. newAttr = emalloc(sizeof(sdlAttribute));
  1794. memset(newAttr, 0, sizeof(sdlAttribute));
  1795. parse_namespace(ref->children->content, &group_name, &ns);
  1796. nsptr = xmlSearchNs(attrGroup->doc, attrGroup, BAD_CAST(ns));
  1797. if (nsptr != NULL) {
  1798. smart_str_appends(&key, (char*)nsptr->href);
  1799. }
  1800. smart_str_appendc(&key, ':');
  1801. smart_str_appends(&key, group_name);
  1802. smart_str_0(&key);
  1803. newAttr->ref = estrndup(ZSTR_VAL(key.s), ZSTR_LEN(key.s));
  1804. if (group_name) {efree(group_name);}
  1805. if (ns) {efree(ns);}
  1806. smart_str_free(&key);
  1807. zend_hash_next_index_insert_ptr(cur_type->attributes, newAttr);
  1808. cur_type = NULL;
  1809. }
  1810. } else{
  1811. soap_error0(E_ERROR, "Parsing Schema: attributeGroup has no 'name' nor 'ref' attributes");
  1812. }
  1813. trav = attrGroup->children;
  1814. if (trav != NULL && node_is_equal(trav, "annotation")) {
  1815. /* TODO: <annotation> support */
  1816. trav = trav->next;
  1817. }
  1818. while (trav != NULL) {
  1819. if (node_is_equal(trav,"attribute")) {
  1820. if (ref != NULL) {
  1821. soap_error0(E_ERROR, "Parsing Schema: attributeGroup has both 'ref' attribute and subattribute");
  1822. }
  1823. schema_attribute(sdl, tns, trav, cur_type, NULL);
  1824. } else if (node_is_equal(trav,"attributeGroup")) {
  1825. if (ref != NULL) {
  1826. soap_error0(E_ERROR, "Parsing Schema: attributeGroup has both 'ref' attribute and subattribute");
  1827. }
  1828. schema_attributeGroup(sdl, tns, trav, cur_type, NULL);
  1829. } else if (node_is_equal(trav,"anyAttribute")) {
  1830. if (ref != NULL) {
  1831. soap_error0(E_ERROR, "Parsing Schema: attributeGroup has both 'ref' attribute and subattribute");
  1832. }
  1833. /* TODO: <anyAttribute> support */
  1834. trav = trav->next;
  1835. break;
  1836. } else {
  1837. soap_error1(E_ERROR, "Parsing Schema: unexpected <%s> in attributeGroup", trav->name);
  1838. }
  1839. trav = trav->next;
  1840. }
  1841. if (trav != NULL) {
  1842. soap_error1(E_ERROR, "Parsing Schema: unexpected <%s> in attributeGroup", trav->name);
  1843. }
  1844. return TRUE;
  1845. }
  1846. static void copy_extra_attribute(zval *zv)
  1847. {
  1848. sdlExtraAttributePtr new_attr;
  1849. new_attr = emalloc(sizeof(sdlExtraAttribute));
  1850. memcpy(new_attr, Z_PTR_P(zv), sizeof(sdlExtraAttribute));
  1851. Z_PTR_P(zv) = new_attr;
  1852. if (new_attr->ns) {
  1853. new_attr->ns = estrdup(new_attr->ns);
  1854. }
  1855. if (new_attr->val) {
  1856. new_attr->val = estrdup(new_attr->val);
  1857. }
  1858. }
  1859. static void* schema_find_by_ref(HashTable *ht, char *ref)
  1860. {
  1861. void *tmp;
  1862. if ((tmp = zend_hash_str_find_ptr(ht, ref, strlen(ref))) != NULL) {
  1863. return tmp;
  1864. } else {
  1865. ref = strrchr(ref, ':');
  1866. if (ref) {
  1867. if ((tmp = zend_hash_str_find_ptr(ht, ref, strlen(ref))) != NULL) {
  1868. return tmp;
  1869. }
  1870. }
  1871. }
  1872. return NULL;
  1873. }
  1874. static void schema_attribute_fixup(sdlCtx *ctx, sdlAttributePtr attr)
  1875. {
  1876. sdlAttributePtr tmp;
  1877. if (attr->ref != NULL) {
  1878. if (ctx->attributes != NULL) {
  1879. tmp = (sdlAttributePtr)schema_find_by_ref(ctx->attributes, attr->ref);
  1880. if (tmp) {
  1881. schema_attribute_fixup(ctx, tmp);
  1882. if (tmp->name != NULL && attr->name == NULL) {
  1883. attr->name = estrdup(tmp->name);
  1884. }
  1885. if (tmp->namens != NULL && attr->namens == NULL) {
  1886. attr->namens = estrdup(tmp->namens);
  1887. }
  1888. if (tmp->def != NULL && attr->def == NULL) {
  1889. attr->def = estrdup(tmp->def);
  1890. }
  1891. if (tmp->fixed != NULL && attr->fixed == NULL) {
  1892. attr->fixed = estrdup(tmp->fixed);
  1893. }
  1894. if (attr->form == XSD_FORM_DEFAULT) {
  1895. attr->form = tmp->form;
  1896. }
  1897. if (attr->use == XSD_USE_DEFAULT) {
  1898. attr->use = tmp->use;
  1899. }
  1900. if (tmp->extraAttributes != NULL) {
  1901. attr->extraAttributes = emalloc(sizeof(HashTable));
  1902. zend_hash_init(attr->extraAttributes, zend_hash_num_elements(tmp->extraAttributes), NULL, delete_extra_attribute, 0);
  1903. zend_hash_copy(attr->extraAttributes, tmp->extraAttributes, copy_extra_attribute);
  1904. }
  1905. attr->encode = tmp->encode;
  1906. }
  1907. }
  1908. if (attr->name == NULL && attr->ref != NULL) {
  1909. char *name = strrchr(attr->ref, ':');
  1910. if (name) {
  1911. attr->name = estrdup(name+1);
  1912. } else{
  1913. attr->name = estrdup(attr->ref);
  1914. }
  1915. }
  1916. efree(attr->ref);
  1917. attr->ref = NULL;
  1918. }
  1919. }
  1920. static void schema_attributegroup_fixup(sdlCtx *ctx, sdlAttributePtr attr, HashTable *ht)
  1921. {
  1922. sdlTypePtr tmp;
  1923. sdlAttributePtr tmp_attr;
  1924. if (attr->ref != NULL) {
  1925. if (ctx->attributeGroups != NULL) {
  1926. tmp = (sdlTypePtr)schema_find_by_ref(ctx->attributeGroups, attr->ref);
  1927. if (tmp) {
  1928. if (tmp->attributes) {
  1929. zend_hash_internal_pointer_reset(tmp->attributes);
  1930. while ((tmp_attr = zend_hash_get_current_data_ptr(tmp->attributes)) != NULL) {
  1931. if (zend_hash_get_current_key_type(tmp->attributes) == HASH_KEY_IS_STRING) {
  1932. zend_string* _key;
  1933. sdlAttributePtr newAttr;
  1934. schema_attribute_fixup(ctx, tmp_attr);
  1935. newAttr = emalloc(sizeof(sdlAttribute));
  1936. memcpy(newAttr, tmp_attr, sizeof(sdlAttribute));
  1937. if (newAttr->def) {newAttr->def = estrdup(newAttr->def);}
  1938. if (newAttr->fixed) {newAttr->fixed = estrdup(newAttr->fixed);}
  1939. if (newAttr->namens) {newAttr->namens = estrdup(newAttr->namens);}
  1940. if (newAttr->name) {newAttr->name = estrdup(newAttr->name);}
  1941. if (newAttr->extraAttributes) {
  1942. HashTable *ht = emalloc(sizeof(HashTable));
  1943. zend_hash_init(ht, zend_hash_num_elements(newAttr->extraAttributes), NULL, delete_extra_attribute, 0);
  1944. zend_hash_copy(ht, newAttr->extraAttributes, copy_extra_attribute);
  1945. newAttr->extraAttributes = ht;
  1946. }
  1947. zend_hash_get_current_key(tmp->attributes, &_key, NULL);
  1948. zend_hash_add_ptr(ht, _key, newAttr);
  1949. zend_hash_move_forward(tmp->attributes);
  1950. } else {
  1951. zend_ulong index;
  1952. schema_attributegroup_fixup(ctx, tmp_attr, ht);
  1953. zend_hash_get_current_key(tmp->attributes, NULL, &index);
  1954. zend_hash_index_del(tmp->attributes, index);
  1955. }
  1956. }
  1957. }
  1958. }
  1959. }
  1960. efree(attr->ref);
  1961. attr->ref = NULL;
  1962. }
  1963. }
  1964. static void schema_content_model_fixup(sdlCtx *ctx, sdlContentModelPtr model)
  1965. {
  1966. switch (model->kind) {
  1967. case XSD_CONTENT_GROUP_REF: {
  1968. sdlTypePtr tmp;
  1969. if (ctx->sdl->groups && (tmp = zend_hash_str_find_ptr(ctx->sdl->groups, model->u.group_ref, strlen(model->u.group_ref))) != NULL) {
  1970. schema_type_fixup(ctx, tmp);
  1971. efree(model->u.group_ref);
  1972. model->kind = XSD_CONTENT_GROUP;
  1973. model->u.group = tmp;
  1974. } else {
  1975. soap_error1(E_ERROR, "Parsing Schema: unresolved group 'ref' attribute '%s'", model->u.group_ref);
  1976. }
  1977. break;
  1978. }
  1979. case XSD_CONTENT_CHOICE: {
  1980. if (model->max_occurs != 1) {
  1981. sdlContentModelPtr tmp;
  1982. ZEND_HASH_FOREACH_PTR(model->u.content, tmp) {
  1983. tmp->min_occurs = 0;
  1984. tmp->max_occurs = model->max_occurs;
  1985. } ZEND_HASH_FOREACH_END();
  1986. model->kind = XSD_CONTENT_ALL;
  1987. model->min_occurs = 1;
  1988. model->max_occurs = 1;
  1989. }
  1990. }
  1991. case XSD_CONTENT_SEQUENCE:
  1992. case XSD_CONTENT_ALL: {
  1993. sdlContentModelPtr tmp;
  1994. ZEND_HASH_FOREACH_PTR(model->u.content, tmp) {
  1995. schema_content_model_fixup(ctx, tmp);
  1996. } ZEND_HASH_FOREACH_END();
  1997. break;
  1998. }
  1999. default:
  2000. break;
  2001. }
  2002. }
  2003. static void schema_type_fixup(sdlCtx *ctx, sdlTypePtr type)
  2004. {
  2005. sdlTypePtr tmp;
  2006. sdlAttributePtr attr;
  2007. if (type->ref != NULL) {
  2008. if (ctx->sdl->elements != NULL) {
  2009. tmp = (sdlTypePtr)schema_find_by_ref(ctx->sdl->elements, type->ref);
  2010. if (tmp) {
  2011. type->kind = tmp->kind;
  2012. type->encode = tmp->encode;
  2013. if (tmp->nillable) {
  2014. type->nillable = 1;
  2015. }
  2016. if (tmp->fixed) {
  2017. type->fixed = estrdup(tmp->fixed);
  2018. }
  2019. if (tmp->def) {
  2020. type->def = estrdup(tmp->def);
  2021. }
  2022. type->form = tmp->form;
  2023. } else if (strcmp(type->ref, SCHEMA_NAMESPACE ":schema") == 0) {
  2024. type->encode = get_conversion(XSD_ANYXML);
  2025. } else {
  2026. soap_error1(E_ERROR, "Parsing Schema: unresolved element 'ref' attribute '%s'", type->ref);
  2027. }
  2028. }
  2029. efree(type->ref);
  2030. type->ref = NULL;
  2031. }
  2032. if (type->elements) {
  2033. ZEND_HASH_FOREACH_PTR(type->elements, tmp) {
  2034. schema_type_fixup(ctx, tmp);
  2035. } ZEND_HASH_FOREACH_END();
  2036. }
  2037. if (type->model) {
  2038. schema_content_model_fixup(ctx, type->model);
  2039. }
  2040. if (type->attributes) {
  2041. zend_string *str_key;
  2042. zend_ulong index;
  2043. ZEND_HASH_FOREACH_KEY_PTR(type->attributes, index, str_key, attr) {
  2044. if (str_key) {
  2045. schema_attribute_fixup(ctx, attr);
  2046. } else {
  2047. schema_attributegroup_fixup(ctx, attr, type->attributes);
  2048. zend_hash_index_del(type->attributes, index);
  2049. }
  2050. } ZEND_HASH_FOREACH_END();
  2051. }
  2052. }
  2053. void schema_pass2(sdlCtx *ctx)
  2054. {
  2055. sdlPtr sdl = ctx->sdl;
  2056. sdlAttributePtr attr;
  2057. sdlTypePtr type;
  2058. if (ctx->attributes) {
  2059. ZEND_HASH_FOREACH_PTR(ctx->attributes, attr) {
  2060. schema_attribute_fixup(ctx, attr);
  2061. } ZEND_HASH_FOREACH_END();
  2062. }
  2063. if (ctx->attributeGroups) {
  2064. ZEND_HASH_FOREACH_PTR(ctx->attributeGroups, type) {
  2065. schema_type_fixup(ctx, type);
  2066. } ZEND_HASH_FOREACH_END();
  2067. }
  2068. if (sdl->elements) {
  2069. ZEND_HASH_FOREACH_PTR(sdl->elements, type) {
  2070. schema_type_fixup(ctx, type);
  2071. } ZEND_HASH_FOREACH_END();
  2072. }
  2073. if (sdl->groups) {
  2074. ZEND_HASH_FOREACH_PTR(sdl->groups, type) {
  2075. schema_type_fixup(ctx, type);
  2076. } ZEND_HASH_FOREACH_END();
  2077. }
  2078. if (sdl->types) {
  2079. ZEND_HASH_FOREACH_PTR(sdl->types, type) {
  2080. schema_type_fixup(ctx, type);
  2081. } ZEND_HASH_FOREACH_END();
  2082. }
  2083. if (ctx->attributes) {
  2084. zend_hash_destroy(ctx->attributes);
  2085. efree(ctx->attributes);
  2086. }
  2087. if (ctx->attributeGroups) {
  2088. zend_hash_destroy(ctx->attributeGroups);
  2089. efree(ctx->attributeGroups);
  2090. }
  2091. }
  2092. void delete_model(zval *zv)
  2093. {
  2094. sdlContentModelPtr tmp = Z_PTR_P(zv);
  2095. switch (tmp->kind) {
  2096. case XSD_CONTENT_ELEMENT:
  2097. case XSD_CONTENT_GROUP:
  2098. break;
  2099. case XSD_CONTENT_SEQUENCE:
  2100. case XSD_CONTENT_ALL:
  2101. case XSD_CONTENT_CHOICE:
  2102. zend_hash_destroy(tmp->u.content);
  2103. efree(tmp->u.content);
  2104. break;
  2105. case XSD_CONTENT_GROUP_REF:
  2106. efree(tmp->u.group_ref);
  2107. break;
  2108. default:
  2109. break;
  2110. }
  2111. efree(tmp);
  2112. }
  2113. static void delete_model_persistent_int(sdlContentModelPtr tmp)
  2114. {
  2115. switch (tmp->kind) {
  2116. case XSD_CONTENT_ELEMENT:
  2117. case XSD_CONTENT_GROUP:
  2118. break;
  2119. case XSD_CONTENT_SEQUENCE:
  2120. case XSD_CONTENT_ALL:
  2121. case XSD_CONTENT_CHOICE:
  2122. zend_hash_destroy(tmp->u.content);
  2123. free(tmp->u.content);
  2124. break;
  2125. case XSD_CONTENT_GROUP_REF:
  2126. free(tmp->u.group_ref);
  2127. break;
  2128. default:
  2129. break;
  2130. }
  2131. free(tmp);
  2132. }
  2133. void delete_model_persistent(zval *zv)
  2134. {
  2135. delete_model_persistent_int(Z_PTR_P(zv));
  2136. }
  2137. void delete_type(zval *zv)
  2138. {
  2139. sdlTypePtr type = Z_PTR_P(zv);
  2140. if (type->name) {
  2141. efree(type->name);
  2142. }
  2143. if (type->namens) {
  2144. efree(type->namens);
  2145. }
  2146. if (type->def) {
  2147. efree(type->def);
  2148. }
  2149. if (type->fixed) {
  2150. efree(type->fixed);
  2151. }
  2152. if (type->elements) {
  2153. zend_hash_destroy(type->elements);
  2154. efree(type->elements);
  2155. }
  2156. if (type->attributes) {
  2157. zend_hash_destroy(type->attributes);
  2158. efree(type->attributes);
  2159. }
  2160. if (type->model) {
  2161. zval zv;
  2162. ZVAL_PTR(&zv, type->model);
  2163. delete_model(&zv);
  2164. }
  2165. if (type->restrictions) {
  2166. delete_restriction_var_int(type->restrictions->minExclusive);
  2167. delete_restriction_var_int(type->restrictions->minInclusive);
  2168. delete_restriction_var_int(type->restrictions->maxExclusive);
  2169. delete_restriction_var_int(type->restrictions->maxInclusive);
  2170. delete_restriction_var_int(type->restrictions->totalDigits);
  2171. delete_restriction_var_int(type->restrictions->fractionDigits);
  2172. delete_restriction_var_int(type->restrictions->length);
  2173. delete_restriction_var_int(type->restrictions->minLength);
  2174. delete_restriction_var_int(type->restrictions->maxLength);
  2175. delete_restriction_var_char_int(type->restrictions->whiteSpace);
  2176. delete_restriction_var_char_int(type->restrictions->pattern);
  2177. if (type->restrictions->enumeration) {
  2178. zend_hash_destroy(type->restrictions->enumeration);
  2179. efree(type->restrictions->enumeration);
  2180. }
  2181. efree(type->restrictions);
  2182. }
  2183. efree(type);
  2184. }
  2185. void delete_type_persistent(zval *zv)
  2186. {
  2187. sdlTypePtr type = Z_PTR_P(zv);
  2188. if (type->name) {
  2189. free(type->name);
  2190. }
  2191. if (type->namens) {
  2192. free(type->namens);
  2193. }
  2194. if (type->def) {
  2195. free(type->def);
  2196. }
  2197. if (type->fixed) {
  2198. free(type->fixed);
  2199. }
  2200. if (type->elements) {
  2201. zend_hash_destroy(type->elements);
  2202. free(type->elements);
  2203. }
  2204. if (type->attributes) {
  2205. zend_hash_destroy(type->attributes);
  2206. free(type->attributes);
  2207. }
  2208. if (type->model) {
  2209. delete_model_persistent_int(type->model);
  2210. }
  2211. if (type->restrictions) {
  2212. delete_restriction_var_int_persistent(type->restrictions->minExclusive);
  2213. delete_restriction_var_int_persistent(type->restrictions->minInclusive);
  2214. delete_restriction_var_int_persistent(type->restrictions->maxExclusive);
  2215. delete_restriction_var_int_persistent(type->restrictions->maxInclusive);
  2216. delete_restriction_var_int_persistent(type->restrictions->totalDigits);
  2217. delete_restriction_var_int_persistent(type->restrictions->fractionDigits);
  2218. delete_restriction_var_int_persistent(type->restrictions->length);
  2219. delete_restriction_var_int_persistent(type->restrictions->minLength);
  2220. delete_restriction_var_int_persistent(type->restrictions->maxLength);
  2221. delete_restriction_var_char_persistent_int(type->restrictions->whiteSpace);
  2222. delete_restriction_var_char_persistent_int(type->restrictions->pattern);
  2223. if (type->restrictions->enumeration) {
  2224. zend_hash_destroy(type->restrictions->enumeration);
  2225. free(type->restrictions->enumeration);
  2226. }
  2227. free(type->restrictions);
  2228. }
  2229. free(type);
  2230. }
  2231. void delete_extra_attribute(zval *zv)
  2232. {
  2233. sdlExtraAttributePtr attr = Z_PTR_P(zv);
  2234. if (attr->ns) {
  2235. efree(attr->ns);
  2236. }
  2237. if (attr->val) {
  2238. efree(attr->val);
  2239. }
  2240. efree(attr);
  2241. }
  2242. void delete_extra_attribute_persistent(zval *zv)
  2243. {
  2244. sdlExtraAttributePtr attr = Z_PTR_P(zv);
  2245. if (attr->ns) {
  2246. free(attr->ns);
  2247. }
  2248. if (attr->val) {
  2249. free(attr->val);
  2250. }
  2251. free(attr);
  2252. }
  2253. void delete_attribute(zval *zv)
  2254. {
  2255. sdlAttributePtr attr = Z_PTR_P(zv);
  2256. if (attr->def) {
  2257. efree(attr->def);
  2258. }
  2259. if (attr->fixed) {
  2260. efree(attr->fixed);
  2261. }
  2262. if (attr->name) {
  2263. efree(attr->name);
  2264. }
  2265. if (attr->namens) {
  2266. efree(attr->namens);
  2267. }
  2268. if (attr->ref) {
  2269. efree(attr->ref);
  2270. }
  2271. if (attr->extraAttributes) {
  2272. zend_hash_destroy(attr->extraAttributes);
  2273. efree(attr->extraAttributes);
  2274. }
  2275. efree(attr);
  2276. }
  2277. void delete_attribute_persistent(zval *zv)
  2278. {
  2279. sdlAttributePtr attr = Z_PTR_P(zv);
  2280. if (attr->def) {
  2281. free(attr->def);
  2282. }
  2283. if (attr->fixed) {
  2284. free(attr->fixed);
  2285. }
  2286. if (attr->name) {
  2287. free(attr->name);
  2288. }
  2289. if (attr->namens) {
  2290. free(attr->namens);
  2291. }
  2292. if (attr->ref) {
  2293. free(attr->ref);
  2294. }
  2295. if (attr->extraAttributes) {
  2296. zend_hash_destroy(attr->extraAttributes);
  2297. free(attr->extraAttributes);
  2298. }
  2299. free(attr);
  2300. }
  2301. void delete_restriction_var_int(sdlRestrictionIntPtr ptr)
  2302. {
  2303. if (ptr) {
  2304. efree(ptr);
  2305. }
  2306. }
  2307. void delete_restriction_var_int_persistent(sdlRestrictionIntPtr ptr)
  2308. {
  2309. if (ptr) {
  2310. free(ptr);
  2311. }
  2312. }
  2313. void delete_restriction_var_char_int(sdlRestrictionCharPtr ptr)
  2314. {
  2315. if (ptr) {
  2316. if (ptr->value) {
  2317. efree(ptr->value);
  2318. }
  2319. efree(ptr);
  2320. }
  2321. }
  2322. void delete_restriction_var_char(zval *zv)
  2323. {
  2324. delete_restriction_var_char_int(Z_PTR_P(zv));
  2325. }
  2326. void delete_restriction_var_char_persistent_int(sdlRestrictionCharPtr ptr)
  2327. {
  2328. if (ptr) {
  2329. if (ptr->value) {
  2330. free(ptr->value);
  2331. }
  2332. free(ptr);
  2333. }
  2334. }
  2335. void delete_restriction_var_char_persistent(zval *zv)
  2336. {
  2337. delete_restriction_var_char_persistent_int(Z_PTR_P(zv));
  2338. }