php_schema.c 77 KB

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