You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1030 lines
22 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /******************************************************************
  2. * $Id: xsd2c.c,v 1.7 2004/10/15 13:35:39 snowdrop Exp $
  3. *
  4. * CSOAP Project: A SOAP client/server library in C
  5. * Copyright (C) 2003 Ferhat Ayaz
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Library General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Library General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Library General Public
  18. * License along with this library; if not, write to the
  19. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  20. * Boston, MA 02111-1307, USA.
  21. *
  22. * Email: ayaz@jprogrammer.net
  23. ******************************************************************/
  24. #include <libxml/xmlmemory.h>
  25. #include <libxml/parser.h>
  26. #include <stdio.h>
  27. #include <string.h>
  28. #include "obj.h"
  29. #include "tr.h"
  30. #include "formatter.h"
  31. #include "xsd2c.h"
  32. #include <sys/stat.h>
  33. #define NODE_NAME_EQUALS(xmlnode, text) \
  34. (!xmlStrcmp(xmlnode->name, (const xmlChar *)text))
  35. static xmlNodePtr _xmlGetChild (xmlNodePtr node);
  36. static xmlNodePtr _xmlGetNext (xmlNodePtr node);
  37. static HCOMPLEXTYPE xsdProcComplexType (xmlNodePtr node, const char *type);
  38. static xsdKeyword
  39. xsdGetKeyword (xmlNodePtr node)
  40. {
  41. if (node == NULL)
  42. return XSD_UNKNOWN;
  43. if (NODE_NAME_EQUALS (node, XSD_ALL_STR))
  44. return XSD_ALL;
  45. if (NODE_NAME_EQUALS (node, XSD_ANNOTATION_STR))
  46. return XSD_ANNOTATION;
  47. if (NODE_NAME_EQUALS (node, XSD_ANY_STR))
  48. return XSD_ANY;
  49. if (NODE_NAME_EQUALS (node, XSD_ANY_ATTRIBUTE_STR))
  50. return XSD_ANY_ATTRIBUTE;
  51. if (NODE_NAME_EQUALS (node, XSD_APPINFO_STR))
  52. return XSD_APPINFO;
  53. if (NODE_NAME_EQUALS (node, XSD_ATTRIBUTE_STR))
  54. return XSD_ATTRIBUTE;
  55. if (NODE_NAME_EQUALS (node, XSD_ATTRIBUTE_GROUP_STR))
  56. return XSD_ATTRIBUTE_GROUP;
  57. if (NODE_NAME_EQUALS (node, XSD_CHOICE_STR))
  58. return XSD_CHOICE;
  59. if (NODE_NAME_EQUALS (node, XSD_COMPLEX_TYPE_STR))
  60. return XSD_COMPLEX_TYPE;
  61. if (NODE_NAME_EQUALS (node, XSD_COMPLEX_CONTENT_STR))
  62. return XSD_COMPLEX_CONTENT;
  63. if (NODE_NAME_EQUALS (node, XSD_DOCUMENTATION_STR))
  64. return XSD_DOCUMENTATION;
  65. if (NODE_NAME_EQUALS (node, XSD_ELEMENT_STR))
  66. return XSD_ELEMENT;
  67. if (NODE_NAME_EQUALS (node, XSD_ENUMERATION_STR))
  68. return XSD_ENUMERATION;
  69. if (NODE_NAME_EQUALS (node, XSD_EXTENSION_STR))
  70. return XSD_EXTENSION;
  71. if (NODE_NAME_EQUALS (node, XSD_FIELD_STR))
  72. return XSD_FIELD;
  73. if (NODE_NAME_EQUALS (node, XSD_GROUP_STR))
  74. return XSD_GROUP;
  75. if (NODE_NAME_EQUALS (node, XSD_IMPORT_STR))
  76. return XSD_IMPORT;
  77. if (NODE_NAME_EQUALS (node, XSD_INCLUDE_STR))
  78. return XSD_INCLUDE;
  79. if (NODE_NAME_EQUALS (node, XSD_KEY_STR))
  80. return XSD_KEY;
  81. if (NODE_NAME_EQUALS (node, XSD_KEYREF_STR))
  82. return XSD_KEYREF;
  83. if (NODE_NAME_EQUALS (node, XSD_LIST_STR))
  84. return XSD_LIST;
  85. if (NODE_NAME_EQUALS (node, XSD_NOTATION_STR))
  86. return XSD_NOTATION;
  87. if (NODE_NAME_EQUALS (node, XSD_REDEFINE_STR))
  88. return XSD_REDEFINE;
  89. if (NODE_NAME_EQUALS (node, XSD_RESTRICTION_STR))
  90. return XSD_RESTRICTION;
  91. if (NODE_NAME_EQUALS (node, XSD_SCHEMA_STR))
  92. return XSD_SCHEMA;
  93. if (NODE_NAME_EQUALS (node, XSD_SELECTOR_STR))
  94. return XSD_SELECTOR;
  95. if (NODE_NAME_EQUALS (node, XSD_SEQUENCE_STR))
  96. return XSD_SEQUENCE;
  97. if (NODE_NAME_EQUALS (node, XSD_SIMPLE_CONTENT_STR))
  98. return XSD_SIMPLE_CONTENT;
  99. if (NODE_NAME_EQUALS (node, XSD_SIMPLE_TYPE_STR))
  100. return XSD_SIMPLE_TYPE;
  101. if (NODE_NAME_EQUALS (node, XSD_UNION_STR))
  102. return XSD_UNION;
  103. if (NODE_NAME_EQUALS (node, XSD_UNIQUE_STR))
  104. return XSD_UNIQUE;
  105. if (NODE_NAME_EQUALS (node, XSD_MIN_INCLUSIVE_STR))
  106. return XSD_MIN_INCLUSIVE;
  107. if (NODE_NAME_EQUALS (node, XSD_MAX_INCLUSIVE_STR))
  108. return XSD_MAX_INCLUSIVE;
  109. if (NODE_NAME_EQUALS (node, XSD_MIN_EXCLUSIVE_STR))
  110. return XSD_MIN_EXCLUSIVE;
  111. if (NODE_NAME_EQUALS (node, XSD_MAX_EXCLUSIVE_STR))
  112. return XSD_MAX_EXCLUSIVE;
  113. return XSD_UNKNOWN;
  114. }
  115. static char outDir[1054];
  116. static xmlNodePtr
  117. xmlFindSubElement (xmlNodePtr root, const char *element_name)
  118. {
  119. xmlNodePtr cur;
  120. cur = root->xmlChildrenNode;
  121. while (cur != NULL)
  122. {
  123. if (cur->type != XML_ELEMENT_NODE)
  124. {
  125. cur = cur->next;
  126. continue;
  127. }
  128. if (!xmlStrcmp (cur->name, (const xmlChar *) element_name))
  129. {
  130. return cur;
  131. }
  132. cur = cur->next;
  133. }
  134. return NULL;
  135. }
  136. xmlNodePtr
  137. xsdLoadFile (const char *filename)
  138. {
  139. xmlDocPtr doc;
  140. xmlNodePtr cur;
  141. doc = xmlParseFile (filename);
  142. if (doc == NULL)
  143. return NULL;
  144. cur = xmlDocGetRootElement (doc);
  145. return cur;
  146. }
  147. xmlNodePtr
  148. wsdlLoadFile (const char *filename)
  149. {
  150. xmlDocPtr doc;
  151. xmlNodePtr cur;
  152. xmlNodePtr sub;
  153. xsdKeyword keyword;
  154. doc = xmlParseFile (filename);
  155. if (doc == NULL)
  156. return NULL;
  157. cur = xmlDocGetRootElement (doc);
  158. if (cur == NULL)
  159. {
  160. return NULL;
  161. }
  162. cur = xmlFindSubElement (cur, "types");
  163. if (cur == NULL)
  164. return NULL;
  165. /*
  166. sub = xmlFindSubElement(cur, "schema");
  167. if (sub != NULL)
  168. return sub;
  169. */
  170. /* some wsdl's defines xsd without root <schema> element */
  171. sub = _xmlGetChild (cur);
  172. keyword = xsdGetKeyword (sub);
  173. switch (keyword)
  174. {
  175. case XSD_ELEMENT:
  176. case XSD_COMPLEX_TYPE:
  177. case XSD_SIMPLE_TYPE:
  178. case XSD_SCHEMA:
  179. return sub;
  180. default:
  181. fprintf (stderr, "Unexpected node: '%s'\n", cur->name);
  182. }
  183. return NULL;
  184. }
  185. static xmlNodePtr
  186. _xmlGetChild (xmlNodePtr node)
  187. {
  188. xmlNodePtr cur = NULL;
  189. cur = node->xmlChildrenNode;
  190. while (cur != NULL)
  191. {
  192. if (cur->type != XML_ELEMENT_NODE)
  193. {
  194. cur = cur->next;
  195. continue;
  196. }
  197. return cur;
  198. }
  199. return cur;
  200. }
  201. static xmlNodePtr
  202. _xmlGetNext (xmlNodePtr node)
  203. {
  204. xmlNodePtr cur = NULL;
  205. cur = node->next;
  206. while (cur != NULL)
  207. {
  208. if (cur->type != XML_ELEMENT_NODE)
  209. {
  210. cur = cur->next;
  211. continue;
  212. }
  213. return cur;
  214. }
  215. return cur;
  216. }
  217. static void
  218. xsdProcAttribute (HCOMPLEXTYPE parent, xmlNodePtr node)
  219. {
  220. char *name, *type;
  221. /* xmlNodePtr cur;
  222. char buffer[1054];
  223. */
  224. name = xmlGetProp (node, ATTR_NAME_STR);
  225. type = xmlGetProp (node, ATTR_TYPE_STR);
  226. /* printf(" %s: %s\n", type?type:"(null)",
  227. name?name:"(null)");
  228. */
  229. if (name == NULL)
  230. {
  231. fprintf (stderr, "WARNING: Attribute without name!\n");
  232. return;
  233. }
  234. if (type == NULL)
  235. {
  236. fprintf (stderr, "WARNING: Attribute '%s' has no type\n", name);
  237. }
  238. /* sprintf(buffer, "attr_%s", name); */
  239. objAddAttribute (parent, name, type, 0);
  240. }
  241. static void
  242. xsdProcElement (HCOMPLEXTYPE parent, xmlNodePtr node)
  243. {
  244. char *name, *type, *minostr, *maxostr;
  245. xmlNodePtr cur;
  246. xsdKeyword keyword;
  247. char buffer[1054];
  248. HCOMPLEXTYPE ct;
  249. int mino, maxo;
  250. name = xmlGetProp (node, ATTR_NAME_STR);
  251. type = xmlGetProp (node, ATTR_TYPE_STR);
  252. minostr = xmlGetProp (node, ATTR_MIN_OCCURS_STR);
  253. maxostr = xmlGetProp (node, ATTR_MAX_OCCURS_STR);
  254. /* printf(" %s: %s\n", type?type:"(null)",
  255. name?name:"(null)");
  256. */
  257. if (minostr == NULL)
  258. mino = 1;
  259. else
  260. mino = atoi (minostr);
  261. if (maxostr == NULL)
  262. maxo = 1;
  263. else
  264. {
  265. if (!strcmp (maxostr, ATTR_VALUE_UNBOUNDED))
  266. maxo = -1;
  267. else
  268. maxo = atoi (maxostr);
  269. }
  270. if (type == NULL)
  271. {
  272. /* check for complexType */
  273. cur = _xmlGetChild (node);
  274. if (cur == NULL)
  275. {
  276. fprintf (stderr, "WARNING: Element '%s' has no childs\n", name);
  277. return;
  278. }
  279. do
  280. {
  281. keyword = xsdGetKeyword (cur);
  282. switch (keyword)
  283. {
  284. case XSD_COMPLEX_TYPE:
  285. /*
  286. type = xmlGetProp(cur, ATTR_NAME_STR);
  287. if (type == NULL)
  288. {
  289. fprintf(stderr, "WARNING: Type name not found\n");
  290. break;
  291. }
  292. */
  293. sprintf (buffer, "%s_%s", parent->type, (const char *) name);
  294. ct = xsdProcComplexType (cur, (const char *) buffer);
  295. if (ct != NULL)
  296. {
  297. objAddElement (parent, name, buffer, 0, mino, maxo);
  298. }
  299. break;
  300. default:
  301. fprintf (stderr, "Unexpected node: '%s'\n", cur->name);
  302. }
  303. } while ((cur = _xmlGetNext (cur)) != NULL);
  304. }
  305. else
  306. {
  307. objAddElement (parent, name, type, 0, mino, maxo);
  308. }
  309. /* if (name) xmlFree(name);
  310. if (type) xmlFree(type);*/
  311. }
  312. static void
  313. xsdProcSequence (HCOMPLEXTYPE ct, xmlNodePtr node)
  314. {
  315. xmlNodePtr cur = NULL;
  316. xsdKeyword keyword;
  317. cur = _xmlGetChild (node);
  318. if (cur == NULL)
  319. {
  320. fprintf (stderr, "WARNING: Empty sequence\n");
  321. return;
  322. }
  323. do
  324. {
  325. keyword = xsdGetKeyword (cur);
  326. switch (keyword)
  327. {
  328. case XSD_ANNOTATION:
  329. /* nothing to do */
  330. break;
  331. case XSD_GROUP:
  332. fprintf (stderr, "WARNING: %s not supported\n", XSD_GROUP_STR);
  333. break;
  334. case XSD_CHOICE:
  335. fprintf (stderr, "WARNING: %s not supported\n", XSD_CHOICE_STR);
  336. break;
  337. case XSD_SEQUENCE:
  338. fprintf (stderr, "WARNING: %s not supported\n", XSD_SEQUENCE_STR);
  339. break;
  340. case XSD_ANY:
  341. fprintf (stderr, "WARNING: %s not supported\n", XSD_ANY_STR);
  342. break;
  343. case XSD_ELEMENT:
  344. xsdProcElement (ct, cur);
  345. break;
  346. default:
  347. fprintf (stderr, "WARNING: Unknown child ('%s')!\n",
  348. (char *) cur->name);
  349. };
  350. } while ((cur = _xmlGetNext (cur)) != NULL);
  351. }
  352. static void
  353. xsdProcExtension (HCOMPLEXTYPE ct, xmlNodePtr node, const char *type)
  354. {
  355. xmlNodePtr cur = NULL;
  356. xsdKeyword keyword;
  357. char *base;
  358. base = xmlGetProp (node, ATTR_BASE_STR);
  359. if (base == NULL)
  360. {
  361. fprintf (stderr, "WARNING: No base defined\n");
  362. return;
  363. }
  364. printf (" =[Base] -> %s\n", base);
  365. objSetBaseType (ct, base);
  366. /* xmlFree(base);*/
  367. cur = _xmlGetChild (node);
  368. if (cur == NULL)
  369. {
  370. fprintf (stderr, "WARNING: Empty node\n");
  371. return;
  372. }
  373. do
  374. {
  375. keyword = xsdGetKeyword (cur);
  376. switch (keyword)
  377. {
  378. case XSD_ANNOTATION:
  379. /* nothing to do */
  380. break;
  381. case XSD_ALL:
  382. fprintf (stderr, " WARNING: %s not supported\n", XSD_ALL_STR);
  383. break;
  384. case XSD_GROUP:
  385. fprintf (stderr, "WARNING: %s not supported\n", XSD_GROUP_STR);
  386. break;
  387. case XSD_CHOICE:
  388. fprintf (stderr, "WARNING: %s not supported\n", XSD_CHOICE_STR);
  389. break;
  390. case XSD_ATTRIBUTE:
  391. xsdProcAttribute (ct, cur);
  392. break;
  393. case XSD_ATTRIBUTE_GROUP:
  394. fprintf (stderr, "WARNING: %s not supported\n",
  395. XSD_ATTRIBUTE_GROUP_STR);
  396. break;
  397. case XSD_ANY_ATTRIBUTE:
  398. fprintf (stderr, "WARNING: %s not supported\n", XSD_ANY_ATTRIBUTE_STR);
  399. break;
  400. case XSD_SEQUENCE:
  401. xsdProcSequence (ct, cur);
  402. break;
  403. default:
  404. fprintf (stderr, "WARNING: Unknown child ('%s')!\n",
  405. (char *) cur->name);
  406. };
  407. } while ((cur = _xmlGetNext (cur)) != NULL);
  408. }
  409. static void
  410. xsdProcComplexContent (HCOMPLEXTYPE ct, xmlNodePtr node, const char *type)
  411. {
  412. xmlNodePtr cur = NULL;
  413. xsdKeyword keyword;
  414. cur = _xmlGetChild (node);
  415. if (cur == NULL)
  416. {
  417. fprintf (stderr, "WARNING: Empty sequence\n");
  418. return;
  419. }
  420. do
  421. {
  422. keyword = xsdGetKeyword (cur);
  423. switch (keyword)
  424. {
  425. case XSD_ANNOTATION:
  426. /* nothing to do */
  427. break;
  428. case XSD_EXTENSION:
  429. xsdProcExtension (ct, cur, type);
  430. break;
  431. case XSD_RESTRICTION:
  432. fprintf (stderr, "WARNING: %s not supported\n", XSD_RESTRICTION_STR);
  433. break;
  434. default:
  435. fprintf (stderr, "WARNING: Unknown child ('%s')!\n",
  436. (char *) cur->name);
  437. };
  438. } while ((cur = _xmlGetNext (cur)) != NULL);
  439. }
  440. void
  441. xsdProcRestriction(HCOMPLEXTYPE ct, xmlNodePtr node, const char *type)
  442. {
  443. xmlNodePtr cur = NULL;
  444. xsdKeyword keyword;
  445. char *value;
  446. HRESTRICTION res;
  447. char *base;
  448. base = xmlGetProp (node, ATTR_BASE_STR);
  449. if (base == NULL)
  450. {
  451. fprintf (stderr, "WARNING: No base defined\n");
  452. return;
  453. }
  454. printf (" =[Base] -> %s\n", base);
  455. res = resCreate(base);
  456. ct->restriction = res;
  457. /* xmlFree(base);*/
  458. cur = _xmlGetChild (node);
  459. if (cur == NULL)
  460. {
  461. fprintf (stderr, "WARNING: Empty sequence\n");
  462. return;
  463. }
  464. do
  465. {
  466. value = xmlGetProp (cur, ATTR_VALUE_STR);
  467. if (!value) {
  468. fprintf(stderr, "WARNING: Found SimpleContent->%s without attribute value.\n",
  469. cur->name);
  470. continue;
  471. }
  472. keyword = xsdGetKeyword (cur);
  473. switch (keyword)
  474. {
  475. case XSD_MIN_INCLUSIVE:
  476. res->minInclusive = atoi(value);
  477. res->minInclusiveSet = 1;
  478. res->mode = RES_MODE_MINMAX;
  479. break;
  480. case XSD_MAX_INCLUSIVE:
  481. res->maxInclusive = atoi(value);
  482. res->maxInclusiveSet = 1;
  483. res->mode = RES_MODE_MINMAX;
  484. break;
  485. case XSD_MIN_EXCLUSIVE:
  486. res->minExclusive = atoi(value);
  487. res->minExclusiveSet = 1;
  488. res->mode = RES_MODE_MINMAX;
  489. break;
  490. case XSD_MAX_EXCLUSIVE:
  491. res->maxExclusive = atoi(value);
  492. res->maxExclusiveSet = 1;
  493. res->mode = RES_MODE_MINMAX;
  494. break;
  495. case XSD_ENUMERATION:
  496. Enumeration_Add_value(res->enumeration, value);
  497. res->mode = RES_MODE_ENUMERATION;
  498. default:
  499. fprintf (stderr, "WARNING: Unknown child (SimpleContent->'%s')!\n",
  500. (char *) cur->name);
  501. };
  502. } while ((cur = _xmlGetNext (cur)) != NULL);
  503. }
  504. void
  505. xsdProcSimpleContent (HCOMPLEXTYPE ct, xmlNodePtr node, const char *type)
  506. {
  507. xmlNodePtr cur = NULL;
  508. xsdKeyword keyword;
  509. ct->isSimpleContent = 1;
  510. cur = _xmlGetChild (node);
  511. if (cur == NULL)
  512. {
  513. fprintf (stderr, "WARNING: Empty node\n");
  514. return;
  515. }
  516. do
  517. {
  518. keyword = xsdGetKeyword (cur);
  519. switch (keyword)
  520. {
  521. case XSD_ANNOTATION:
  522. /* nothing to do */
  523. break;
  524. case XSD_RESTRICTION:
  525. xsdProcRestriction(ct, cur, type);
  526. break;
  527. case XSD_ATTRIBUTE:
  528. xsdProcAttribute (ct, cur);
  529. break;
  530. default:
  531. fprintf (stderr, "WARNING: Unknown child (SimpleContent->'%s')!\n",
  532. (char *) cur->name);
  533. };
  534. } while ((cur = _xmlGetNext (cur)) != NULL);
  535. }
  536. static HCOMPLEXTYPE
  537. xsdProcComplexType (xmlNodePtr node, const char *type)
  538. {
  539. char *name;
  540. xmlNodePtr cur = NULL;
  541. xsdKeyword keyword;
  542. HCOMPLEXTYPE ct;
  543. if (!type)
  544. name = xmlGetProp (node, ATTR_NAME_STR);
  545. else
  546. {
  547. name = (char *) malloc (strlen (type) + 1);
  548. strcpy (name, type);
  549. }
  550. if (!name)
  551. {
  552. fprintf (stderr, "\nWARNING: complexType has no typename!\n");
  553. return NULL;
  554. }
  555. ct = objCreateComplexType (name);
  556. printf ("\ncomplexType->%s\n", name);
  557. cur = _xmlGetChild (node);
  558. if (cur == NULL)
  559. {
  560. fprintf (stderr, "WARNING: Empty complexType\n");
  561. return ct;
  562. }
  563. do
  564. {
  565. keyword = xsdGetKeyword (cur);
  566. switch (keyword)
  567. {
  568. case XSD_ANNOTATION:
  569. /* nothing to do */
  570. break;
  571. case XSD_SIMPLE_CONTENT:
  572. xsdProcSimpleContent (ct, cur, name);
  573. break;
  574. case XSD_COMPLEX_CONTENT:
  575. xsdProcComplexContent (ct, cur, name);
  576. /* fprintf(stderr, "WARNING: %s not supported\n", XSD_COMPLEX_CONTENT_STR); */
  577. break;
  578. case XSD_ALL:
  579. fprintf (stderr, "WARNING: %s not supported\n", XSD_ALL_STR);
  580. break;
  581. case XSD_GROUP:
  582. fprintf (stderr, "WARNING: %s not supported\n", XSD_GROUP_STR);
  583. break;
  584. case XSD_CHOICE:
  585. fprintf (stderr, "WARNING: %s not supported\n", XSD_CHOICE_STR);
  586. break;
  587. case XSD_ATTRIBUTE:
  588. xsdProcAttribute (ct, cur);
  589. break;
  590. case XSD_ATTRIBUTE_GROUP:
  591. fprintf (stderr, "WARNING: %s not supported\n",
  592. XSD_ATTRIBUTE_GROUP_STR);
  593. break;
  594. case XSD_ANY_ATTRIBUTE:
  595. fprintf (stderr, "WARNING: %s not supported\n", XSD_ANY_ATTRIBUTE_STR);
  596. break;
  597. case XSD_SEQUENCE:
  598. xsdProcSequence (ct, cur);
  599. break;
  600. default:
  601. fprintf (stderr, "WARNING: Unknown child ('%s')!\n",
  602. (char *) cur->name);
  603. };
  604. } while ((cur = _xmlGetNext (cur)) != NULL);
  605. /* xmlFree(name);*/
  606. return ct;
  607. }
  608. static void
  609. runGenerator (xmlNodePtr xsdRoot)
  610. {
  611. xmlNodePtr cur;
  612. xmlNodePtr node;
  613. xmlChar *type;
  614. cur = xsdRoot->xmlChildrenNode;
  615. while (cur != NULL)
  616. {
  617. if (cur->type != XML_ELEMENT_NODE)
  618. {
  619. cur = cur->next;
  620. continue;
  621. }
  622. if (xsdGetKeyword (cur) == XSD_COMPLEX_TYPE)
  623. {
  624. xsdProcComplexType (cur, NULL);
  625. }
  626. else if (xsdGetKeyword (cur) == XSD_SIMPLE_TYPE)
  627. {
  628. fprintf (stderr, "WARNING: SimpleType not supported!\n");
  629. }
  630. else if (xsdGetKeyword (cur) == XSD_ELEMENT)
  631. {
  632. type = xmlGetProp (cur, "name");
  633. if (type == NULL)
  634. {
  635. fprintf (stderr, "WARNING: Element found without name ('%s')\n",
  636. cur->name);
  637. }
  638. else
  639. {
  640. node = xmlFindSubElement (cur, XSD_COMPLEX_TYPE_STR);
  641. if (node != NULL)
  642. {
  643. xsdProcComplexType (node, type);
  644. }
  645. else
  646. {
  647. /* fprintf (stderr, "WARNING: Element on root node will be ignored\n"); */
  648. /* TODO: I don't know if this is a good idea!?
  649. make typedef instead.*/
  650. xsdProcComplexType(cur, type);
  651. }
  652. }
  653. /*xsdProcElement(..., cur); */
  654. }
  655. else
  656. {
  657. fprintf(stderr, "WARNING: '%s' not supported!\n", cur->name);
  658. }
  659. cur = cur->next;
  660. }
  661. }
  662. int
  663. declareStructs (HCOMPLEXTYPE ct)
  664. {
  665. char fname[255];
  666. FILE *f;
  667. sprintf (fname, "%s/%s.h", outDir, ct->type); /* _xsd */
  668. printf ("Generating file '%s' ...\n", fname);
  669. f = fopen (fname, "w");
  670. if (f == NULL)
  671. {
  672. fprintf (stderr, "Can not open '%s'\n", fname);
  673. return 0;
  674. }
  675. writeComplexTypeHeaderFile (f, ct);
  676. fclose (f);
  677. return 1;
  678. }
  679. int
  680. writeSource (HCOMPLEXTYPE ct)
  681. {
  682. char fname[255];
  683. FILE *f;
  684. sprintf (fname, "%s/%s.c", outDir, ct->type); /* _xsd */
  685. printf ("Generating file '%s' ...\n", fname);
  686. f = fopen (fname, "w");
  687. if (f == NULL)
  688. {
  689. fprintf (stderr, "Can not open '%s'\n", fname);
  690. return 0;
  691. }
  692. writeComplexTypeSourceFile (f, ct);
  693. fclose (f);
  694. return 1;
  695. }
  696. int
  697. xsdInitTrModule (xmlNodePtr xsdNode)
  698. {
  699. xmlNsPtr ns = NULL;
  700. char xsd_ns[50];
  701. ns =
  702. xmlSearchNsByHref (xsdNode->doc, xsdNode,
  703. "http://www.w3.org/2001/XMLSchema");
  704. if (ns == NULL)
  705. ns =
  706. xmlSearchNsByHref (xsdNode->doc, xsdNode,
  707. "http://www.w3.org/2001/XMLSchema/");
  708. if (ns == NULL)
  709. ns =
  710. xmlSearchNsByHref (xsdNode->doc, xsdNode,
  711. "http://www.w3.org/1999/XMLSchema");
  712. if (ns == NULL)
  713. ns =
  714. xmlSearchNsByHref (xsdNode->doc, xsdNode,
  715. "http://www.w3.org/1999/XMLSchema/");
  716. if (ns == NULL)
  717. ns =
  718. xmlSearchNsByHref (xsdNode->doc, xmlDocGetRootElement (xsdNode->doc),
  719. "http://www.w3.org/2001/XMLSchema");
  720. if (ns == NULL)
  721. ns =
  722. xmlSearchNsByHref (xsdNode->doc, xmlDocGetRootElement (xsdNode->doc),
  723. "http://www.w3.org/2001/XMLSchema/");
  724. if (ns == NULL)
  725. ns =
  726. xmlSearchNsByHref (xsdNode->doc, xmlDocGetRootElement (xsdNode->doc),
  727. "http://www.w3.org/1999/XMLSchema");
  728. if (ns == NULL)
  729. ns =
  730. xmlSearchNsByHref (xsdNode->doc, xmlDocGetRootElement (xsdNode->doc),
  731. "http://www.w3.org/1999/XMLSchema/");
  732. /*
  733. if (ns != NULL && ns->prefix != NULL) {
  734. fprintf(stdout, "XMLSchema namespace prefix: '%s'\n", ns->prefix);
  735. trInitModule(ns->prefix);
  736. } else { */
  737. /*
  738. Search for:
  739. <definitions xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  740. <type>
  741. <schema xmlns="http://www.w3.org/2001/XMLSchema">
  742. ...
  743. */
  744. /*if (ns != NULL && ns->prefix != NULL) {
  745. fprintf(stdout, "XMLSchema namespace prefix: '%s'\n", ns->prefix);
  746. trInitModule(ns->prefix);
  747. } else {
  748. printf("Initializing XML Schema type register with default 'xs'\n");
  749. trInitModule("xs");
  750. }
  751. }
  752. } else {
  753. printf("Initializing XML Schema type register with default 'xs'\n");
  754. trInitModule("xs");
  755. }
  756. */
  757. if (ns != NULL && ns->prefix != NULL)
  758. {
  759. strcpy (xsd_ns, ns->prefix);
  760. }
  761. else
  762. {
  763. fprintf (stderr, "WARNING: using XML Schema prefix 'xsd' as default!\n");
  764. strcpy (xsd_ns, "xsd");
  765. }
  766. trInitModule ();
  767. fprintf(stdout, "XML Schema prefix: '%s'\n", xsd_ns);
  768. trRegisterTypeNS (xsd_ns, "ID", "char*", 1);
  769. trRegisterTypeNS (xsd_ns, "IDREF", "char*", 1);
  770. trRegisterTypeNS (xsd_ns, "IDREFS", "char*", 1);
  771. trRegisterTypeNS (xsd_ns, "string", "char*", 1);
  772. trRegisterTypeNS (xsd_ns, "integer", "int", 1);
  773. trRegisterTypeNS (xsd_ns, "int", "int", 1);
  774. trRegisterTypeNS (xsd_ns, "double", "double", 1);
  775. trRegisterTypeNS (xsd_ns, "float", "float", 1);
  776. trRegisterTypeNS (xsd_ns, "boolean", "int", 1);
  777. ns = xmlSearchNsByHref (xsdNode->doc, xsdNode, "http://www.w3.org/2003/05/soap-encoding");
  778. if (ns == NULL)
  779. ns = xmlSearchNsByHref (xsdNode->doc, xsdNode, "http://www.w3.org/2003/05/soap-encoding/");
  780. if (ns != NULL && ns->prefix != NULL) {
  781. strcpy(xsd_ns, ns->prefix);
  782. trRegisterTypeNS (xsd_ns, "ID", "char*", 1);
  783. trRegisterTypeNS (xsd_ns, "IDREF", "char*", 1);
  784. trRegisterTypeNS (xsd_ns, "IDREFS", "char*", 1);
  785. trRegisterTypeNS (xsd_ns, "string", "char*", 1);
  786. trRegisterTypeNS (xsd_ns, "integer", "int", 1);
  787. trRegisterTypeNS (xsd_ns, "int", "int", 1);
  788. trRegisterTypeNS (xsd_ns, "double", "double", 1);
  789. trRegisterTypeNS (xsd_ns, "float", "float", 1);
  790. trRegisterTypeNS (xsd_ns, "boolean", "int", 1);
  791. }
  792. return 1;
  793. }
  794. int
  795. xsdInitObjModule (xmlNodePtr xsdNode)
  796. {
  797. xmlChar *tns = NULL;
  798. xmlNsPtr ns;
  799. if (xsdNode != NULL)
  800. tns = xmlGetProp (xsdNode, (const xmlChar *) "targetNamespace");
  801. if (tns == NULL)
  802. {
  803. objInitModule (NULL);
  804. }
  805. else
  806. {
  807. ns = xmlSearchNsByHref (xsdNode->doc, xsdNode, tns);
  808. if (ns == NULL)
  809. {
  810. fprintf (stderr, "WARNING: Target namespace not found!\n");
  811. return 0;
  812. }
  813. if (ns->prefix == NULL)
  814. {
  815. fprintf (stderr, "WARNING: Target namespace not found!\n");
  816. return 0;
  817. }
  818. fprintf (stdout, "Target namespace ('%s') prefix: '%s'\n", tns,
  819. ns->prefix);
  820. objInitModule (ns->prefix);
  821. }
  822. return 1;
  823. }
  824. void
  825. xsdSetDestDir (const char *destDir)
  826. {
  827. strcpy (outDir, destDir);
  828. #ifdef __MINGW32__
  829. mkdir (destDir);
  830. #else
  831. mkdir (destDir, S_IRUSR | S_IWUSR | S_IXUSR |
  832. S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | S_IXOTH);
  833. #endif
  834. }
  835. int
  836. xsdEngineRun (xmlNodePtr xsdNode, const char *destDir)
  837. {
  838. xsdSetDestDir (destDir);
  839. if (xsdNode != NULL)
  840. {
  841. runGenerator (xsdNode);
  842. objRegistryEnumComplexType (declareStructs);
  843. objRegistryEnumComplexType (writeSource);
  844. }
  845. return 0;
  846. }