PageRenderTime 41ms CodeModel.GetById 6ms 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

Large files files are truncated, but you can click here to view the full file

  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 Schem…

Large files files are truncated, but you can click here to view the full file