php_schema.c 77 KB

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