PageRenderTime 49ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/trunk/Source/Modules/xml.cxx

#
C++ | 324 lines | 260 code | 40 blank | 24 comment | 67 complexity | 6287297009fbb161cf94153bac1ea85b MD5 | raw file
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
  1. /* -----------------------------------------------------------------------------
  2. * This file is part of SWIG, which is licensed as a whole under version 3
  3. * (or any later version) of the GNU General Public License. Some additional
  4. * terms also apply to certain portions of SWIG. The full details of the SWIG
  5. * license and copyrights can be found in the LICENSE and COPYRIGHT files
  6. * included with the SWIG source code as distributed by the SWIG developers
  7. * and at http://www.swig.org/legal.html.
  8. *
  9. * xml.cxx
  10. *
  11. * An Xml parse tree generator.
  12. * ----------------------------------------------------------------------------- */
  13. char cvsroot_xml_cxx[] = "$Id: xml.cxx 11876 2010-02-27 23:53:33Z wsfulton $";
  14. #include "swigmod.h"
  15. static const char *usage = "\
  16. XML Options (available with -xml)\n\
  17. -xmllang <lang> - Typedef language\n\
  18. -xmllite - More lightweight version of XML\n\
  19. ------\n\
  20. deprecated (use -o): -xml <output.xml> - Use <output.xml> as output file (extension .xml mandatory)\n";
  21. static File *out = 0;
  22. static int xmllite = 0;
  23. class XML:public Language {
  24. public:
  25. int indent_level;
  26. long id;
  27. XML() :indent_level(0) , id(0) {
  28. }
  29. virtual ~ XML() {
  30. }
  31. virtual void main(int argc, char *argv[]) {
  32. SWIG_typemap_lang("xml");
  33. for (int iX = 0; iX < argc; iX++) {
  34. if (strcmp(argv[iX], "-xml") == 0) {
  35. char *extension = 0;
  36. if (iX + 1 >= argc)
  37. continue;
  38. extension = argv[iX + 1] + strlen(argv[iX + 1]) - 4;
  39. if (strcmp(extension, ".xml"))
  40. continue;
  41. iX++;
  42. Swig_mark_arg(iX);
  43. String *outfile = NewString(argv[iX]);
  44. out = NewFile(outfile, "w", SWIG_output_files());
  45. if (!out) {
  46. FileErrorDisplay(outfile);
  47. SWIG_exit(EXIT_FAILURE);
  48. }
  49. continue;
  50. }
  51. if (strcmp(argv[iX], "-xmllang") == 0) {
  52. Swig_mark_arg(iX);
  53. iX++;
  54. SWIG_typemap_lang(argv[iX]);
  55. Swig_mark_arg(iX);
  56. continue;
  57. }
  58. if (strcmp(argv[iX], "-help") == 0) {
  59. fputs(usage, stdout);
  60. }
  61. if (strcmp(argv[iX], "-xmllite") == 0) {
  62. Swig_mark_arg(iX);
  63. xmllite = 1;
  64. }
  65. }
  66. // Add a symbol to the parser for conditional compilation
  67. Preprocessor_define("SWIGXML 1", 0);
  68. }
  69. /* Top of the parse tree */
  70. virtual int top(Node *n) {
  71. if (out == 0) {
  72. String *outfile = Getattr(n, "outfile");
  73. Replaceall(outfile, ".cxx", ".xml");
  74. Replaceall(outfile, ".cpp", ".xml");
  75. Replaceall(outfile, ".c", ".xml");
  76. out = NewFile(outfile, "w", SWIG_output_files());
  77. if (!out) {
  78. FileErrorDisplay(outfile);
  79. SWIG_exit(EXIT_FAILURE);
  80. }
  81. }
  82. Printf(out, "<?xml version=\"1.0\" ?> \n");
  83. Xml_print_tree(n);
  84. return SWIG_OK;
  85. }
  86. void print_indent(int l) {
  87. int i;
  88. for (i = 0; i < indent_level; i++) {
  89. Printf(out, " ");
  90. }
  91. if (l) {
  92. Printf(out, " ");
  93. }
  94. }
  95. void Xml_print_tree(DOH *obj) {
  96. while (obj) {
  97. Xml_print_node(obj);
  98. obj = nextSibling(obj);
  99. }
  100. }
  101. void Xml_print_attributes(Node *obj) {
  102. String *k;
  103. indent_level += 4;
  104. print_indent(0);
  105. Printf(out, "<attributelist id=\"%ld\" addr=\"%x\" >\n", ++id, obj);
  106. indent_level += 4;
  107. Iterator ki;
  108. ki = First(obj);
  109. while (ki.key) {
  110. k = ki.key;
  111. if ((Cmp(k, "nodeType") == 0)
  112. || (Cmp(k, "firstChild") == 0)
  113. || (Cmp(k, "lastChild") == 0)
  114. || (Cmp(k, "parentNode") == 0)
  115. || (Cmp(k, "nextSibling") == 0)
  116. || (Cmp(k, "previousSibling") == 0)
  117. || (*(Char(k)) == '$')) {
  118. /* Do nothing */
  119. } else if (Cmp(k, "module") == 0) {
  120. Xml_print_module(Getattr(obj, k));
  121. } else if (Cmp(k, "baselist") == 0) {
  122. Xml_print_baselist(Getattr(obj, k));
  123. } else if (!xmllite && Cmp(k, "typescope") == 0) {
  124. Xml_print_typescope(Getattr(obj, k));
  125. } else if (!xmllite && Cmp(k, "typetab") == 0) {
  126. Xml_print_typetab(Getattr(obj, k));
  127. } else if (Cmp(k, "kwargs") == 0) {
  128. Xml_print_kwargs(Getattr(obj, k));
  129. } else if (Cmp(k, "parms") == 0 || Cmp(k, "pattern") == 0) {
  130. Xml_print_parmlist(Getattr(obj, k));
  131. } else {
  132. DOH *o;
  133. print_indent(0);
  134. if (DohIsString(Getattr(obj, k))) {
  135. String *ck = NewString(k);
  136. o = Str(Getattr(obj, k));
  137. Replaceall(ck, ":", "_");
  138. Replaceall(ck, "<", "&lt;");
  139. /* Do first to avoid aliasing errors. */
  140. Replaceall(o, "&", "&amp;");
  141. Replaceall(o, "<", "&lt;");
  142. Replaceall(o, "\"", "&quot;");
  143. Replaceall(o, "\\", "\\\\");
  144. Replaceall(o, "\n", "&#10;");
  145. Printf(out, "<attribute name=\"%s\" value=\"%s\" id=\"%ld\" addr=\"%x\" />\n", ck, o, ++id, o);
  146. Delete(o);
  147. Delete(ck);
  148. } else {
  149. o = Getattr(obj, k);
  150. String *ck = NewString(k);
  151. Replaceall(ck, ":", "_");
  152. Printf(out, "<attribute name=\"%s\" value=\"%x\" id=\"%ld\" addr=\"%x\" />\n", ck, o, ++id, o);
  153. Delete(ck);
  154. }
  155. }
  156. ki = Next(ki);
  157. }
  158. indent_level -= 4;
  159. print_indent(0);
  160. Printf(out, "</attributelist >\n");
  161. indent_level -= 4;
  162. }
  163. void Xml_print_node(Node *obj) {
  164. Node *cobj;
  165. print_indent(0);
  166. Printf(out, "<%s id=\"%ld\" addr=\"%x\" >\n", nodeType(obj), ++id, obj);
  167. Xml_print_attributes(obj);
  168. cobj = firstChild(obj);
  169. if (cobj) {
  170. indent_level += 4;
  171. Printf(out, "\n");
  172. Xml_print_tree(cobj);
  173. indent_level -= 4;
  174. } else {
  175. print_indent(1);
  176. Printf(out, "\n");
  177. }
  178. print_indent(0);
  179. Printf(out, "</%s >\n", nodeType(obj));
  180. }
  181. void Xml_print_parmlist(ParmList *p) {
  182. print_indent(0);
  183. Printf(out, "<parmlist id=\"%ld\" addr=\"%x\" >\n", ++id, p);
  184. indent_level += 4;
  185. while (p) {
  186. print_indent(0);
  187. Printf(out, "<parm id=\"%ld\">\n", ++id);
  188. Xml_print_attributes(p);
  189. print_indent(0);
  190. Printf(out, "</parm >\n");
  191. p = nextSibling(p);
  192. }
  193. indent_level -= 4;
  194. print_indent(0);
  195. Printf(out, "</parmlist >\n");
  196. }
  197. void Xml_print_baselist(List *p) {
  198. print_indent(0);
  199. Printf(out, "<baselist id=\"%ld\" addr=\"%x\" >\n", ++id, p);
  200. indent_level += 4;
  201. Iterator s;
  202. for (s = First(p); s.item; s = Next(s)) {
  203. print_indent(0);
  204. String *item_name = Xml_escape_string(s.item);
  205. Printf(out, "<base name=\"%s\" id=\"%ld\" addr=\"%x\" />\n", item_name, ++id, s.item);
  206. Delete(item_name);
  207. }
  208. indent_level -= 4;
  209. print_indent(0);
  210. Printf(out, "</baselist >\n");
  211. }
  212. String *Xml_escape_string(String *str) {
  213. String *escaped_str = 0;
  214. if (str) {
  215. escaped_str = NewString(str);
  216. Replaceall(escaped_str, "&", "&amp;");
  217. Replaceall(escaped_str, "<", "&lt;");
  218. Replaceall(escaped_str, "\"", "&quot;");
  219. Replaceall(escaped_str, "\\", "\\\\");
  220. Replaceall(escaped_str, "\n", "&#10;");
  221. }
  222. return escaped_str;
  223. }
  224. void Xml_print_module(Node *p) {
  225. print_indent(0);
  226. Printf(out, "<attribute name=\"module\" value=\"%s\" id=\"%ld\" addr=\"%x\" />\n", Getattr(p, "name"), ++id, p);
  227. }
  228. void Xml_print_kwargs(Hash *p) {
  229. Xml_print_hash(p, "kwargs");
  230. }
  231. void Xml_print_typescope(Hash *p) {
  232. Xml_print_hash(p, "typescope");
  233. }
  234. void Xml_print_typetab(Hash *p) {
  235. Xml_print_hash(p, "typetab");
  236. }
  237. void Xml_print_hash(Hash *p, const char *markup) {
  238. print_indent(0);
  239. Printf(out, "<%s id=\"%ld\" addr=\"%x\" >\n", markup, ++id, p);
  240. Xml_print_attributes(p);
  241. indent_level += 4;
  242. Iterator n = First(p);
  243. while (n.key) {
  244. print_indent(0);
  245. Printf(out, "<%ssitem id=\"%ld\" addr=\"%x\" >\n", markup, ++id, n.item);
  246. Xml_print_attributes(n.item);
  247. print_indent(0);
  248. Printf(out, "</%ssitem >\n", markup);
  249. n = Next(n);
  250. }
  251. indent_level -= 4;
  252. print_indent(0);
  253. Printf(out, "</%s >\n", markup);
  254. }
  255. };
  256. /* -----------------------------------------------------------------------------
  257. * Swig_print_xml
  258. *
  259. * Dump an XML version of the parse tree. This is different from using the -xml
  260. * language module normally as it allows the real language module to process the
  261. * tree first, possibly stuffing in new attributes, so the XML that is output ends
  262. * up being a post-processing version of the tree.
  263. * ----------------------------------------------------------------------------- */
  264. void Swig_print_xml(DOH *obj, String *filename) {
  265. XML xml;
  266. xmllite = 1;
  267. if (!filename) {
  268. out = stdout;
  269. } else {
  270. out = NewFile(filename, "w", SWIG_output_files());
  271. if (!out) {
  272. FileErrorDisplay(filename);
  273. SWIG_exit(EXIT_FAILURE);
  274. }
  275. }
  276. Printf(out, "<?xml version=\"1.0\" ?> \n");
  277. xml.Xml_print_tree(obj);
  278. }
  279. static Language *new_swig_xml() {
  280. return new XML();
  281. }
  282. extern "C" Language *swig_xml(void) {
  283. return new_swig_xml();
  284. }