1 /* -----------------------------------------------------------------------------
2 * See the LICENSE file for information on copyright, usage and redistribution
3 * of SWIG, and the README file for authors - http://www.swig.org/release.html.
7 * An Xml parse tree generator.
8 * ----------------------------------------------------------------------------- */
10 char cvsroot_xml_cxx[] = "$Id: xml.cxx 10898 2008-11-03 12:51:45Z wsfulton $";
14 static const char *usage = "\
15 XML Options (available with -xml)\n\
16 -xmllang <lang> - Typedef language\n\
17 -xmllite - More lightweight version of XML\n\
19 deprecated (use -o): -xml <output.xml> - Use <output.xml> as output file (extension .xml mandatory)\n";
22 static int xmllite = 0;
25 class XML:public Language {
31 XML() :indent_level(0) , id(0) {
37 virtual void main(int argc, char *argv[]) {
38 SWIG_typemap_lang("xml");
39 for (int iX = 0; iX < argc; iX++) {
40 if (strcmp(argv[iX], "-xml") == 0) {
44 extension = argv[iX + 1] + strlen(argv[iX + 1]) - 4;
45 if (strcmp(extension, ".xml"))
49 String *outfile = NewString(argv[iX]);
50 out = NewFile(outfile, "w", SWIG_output_files());
52 FileErrorDisplay(outfile);
53 SWIG_exit(EXIT_FAILURE);
57 if (strcmp(argv[iX], "-xmllang") == 0) {
60 SWIG_typemap_lang(argv[iX]);
64 if (strcmp(argv[iX], "-help") == 0) {
67 if (strcmp(argv[iX], "-xmllite") == 0) {
73 // Add a symbol to the parser for conditional compilation
74 Preprocessor_define("SWIGXML 1", 0);
77 /* Top of the parse tree */
79 virtual int top(Node *n) {
81 String *outfile = Getattr(n, "outfile");
82 Replaceall(outfile, ".cxx", ".xml");
83 Replaceall(outfile, ".cpp", ".xml");
84 Replaceall(outfile, ".c", ".xml");
85 out = NewFile(outfile, "w", SWIG_output_files());
87 FileErrorDisplay(outfile);
88 SWIG_exit(EXIT_FAILURE);
91 Printf(out, "<?xml version=\"1.0\" ?> \n");
96 void print_indent(int l) {
98 for (i = 0; i < indent_level; i++) {
106 void Xml_print_tree(DOH *obj) {
109 obj = nextSibling(obj);
113 void Xml_print_attributes(Node *obj) {
117 Printf(out, "<attributelist id=\"%ld\" addr=\"%x\" >\n", ++id, obj);
123 if ((Cmp(k, "nodeType") == 0)
124 || (Cmp(k, "firstChild") == 0)
125 || (Cmp(k, "lastChild") == 0)
126 || (Cmp(k, "parentNode") == 0)
127 || (Cmp(k, "nextSibling") == 0)
128 || (Cmp(k, "previousSibling") == 0)
129 || (*(Char(k)) == '$')) {
131 } else if (Cmp(k, "module") == 0) {
132 Xml_print_module(Getattr(obj, k));
133 } else if (Cmp(k, "baselist") == 0) {
134 Xml_print_baselist(Getattr(obj, k));
135 } else if (!xmllite && Cmp(k, "typescope") == 0) {
136 Xml_print_typescope(Getattr(obj, k));
137 } else if (!xmllite && Cmp(k, "typetab") == 0) {
138 Xml_print_typetab(Getattr(obj, k));
139 } else if (Cmp(k, "kwargs") == 0) {
140 Xml_print_kwargs(Getattr(obj, k));
141 } else if (Cmp(k, "parms") == 0 || Cmp(k, "pattern") == 0) {
142 Xml_print_parmlist(Getattr(obj, k));
146 if (DohIsString(Getattr(obj, k))) {
147 String *ck = NewString(k);
148 o = Str(Getattr(obj, k));
149 Replaceall(ck, ":", "_");
150 Replaceall(ck, "<", "<");
151 /* Do first to avoid aliasing errors. */
152 Replaceall(o, "&", "&");
153 Replaceall(o, "<", "<");
154 Replaceall(o, "\"", """);
155 Replaceall(o, "\\", "\\\\");
156 Replaceall(o, "\n", " ");
157 Printf(out, "<attribute name=\"%s\" value=\"%s\" id=\"%ld\" addr=\"%x\" />\n", ck, o, ++id, o);
162 String *ck = NewString(k);
163 Replaceall(ck, ":", "_");
164 Printf(out, "<attribute name=\"%s\" value=\"%x\" id=\"%ld\" addr=\"%x\" />\n", ck, o, ++id, o);
172 Printf(out, "</attributelist >\n");
176 void Xml_print_node(Node *obj) {
180 Printf(out, "<%s id=\"%ld\" addr=\"%x\" >\n", nodeType(obj), ++id, obj);
181 Xml_print_attributes(obj);
182 cobj = firstChild(obj);
186 Xml_print_tree(cobj);
193 Printf(out, "</%s >\n", nodeType(obj));
197 void Xml_print_parmlist(ParmList *p) {
200 Printf(out, "<parmlist id=\"%ld\" addr=\"%x\" >\n", ++id, p);
204 Printf(out, "<parm id=\"%ld\">\n", ++id);
205 Xml_print_attributes(p);
207 Printf(out, "</parm >\n");
212 Printf(out, "</parmlist >\n");
215 void Xml_print_baselist(List *p) {
218 Printf(out, "<baselist id=\"%ld\" addr=\"%x\" >\n", ++id, p);
221 for (s = First(p); s.item; s = Next(s)) {
223 String *item_name = Xml_escape_string(s.item);
224 Printf(out, "<base name=\"%s\" id=\"%ld\" addr=\"%x\" />\n", item_name, ++id, s.item);
229 Printf(out, "</baselist >\n");
232 String *Xml_escape_string(String *str) {
233 String *escaped_str = 0;
235 escaped_str = NewString(str);
236 Replaceall(escaped_str, "&", "&");
237 Replaceall(escaped_str, "<", "<");
238 Replaceall(escaped_str, "\"", """);
239 Replaceall(escaped_str, "\\", "\\\\");
240 Replaceall(escaped_str, "\n", " ");
245 void Xml_print_module(Node *p) {
248 Printf(out, "<attribute name=\"module\" value=\"%s\" id=\"%ld\" addr=\"%x\" />\n", Getattr(p, "name"), ++id, p);
251 void Xml_print_kwargs(Hash *p) {
252 Xml_print_hash(p, "kwargs");
255 void Xml_print_typescope(Hash *p) {
257 Xml_print_hash(p, "typescope");
260 void Xml_print_typetab(Hash *p) {
262 Xml_print_hash(p, "typetab");
266 void Xml_print_hash(Hash *p, const char *markup) {
269 Printf(out, "<%s id=\"%ld\" addr=\"%x\" >\n", markup, ++id, p);
270 Xml_print_attributes(p);
272 Iterator n = First(p);
275 Printf(out, "<%ssitem id=\"%ld\" addr=\"%x\" >\n", markup, ++id, n.item);
276 Xml_print_attributes(n.item);
278 Printf(out, "</%ssitem >\n", markup);
283 Printf(out, "</%s >\n", markup);
288 /* -----------------------------------------------------------------------------
291 * Dump an XML version of the parse tree. This is different from using the -xml
292 * language module normally as it allows the real language module to process the
293 * tree first, possibly stuffing in new attributes, so the XML that is output ends
294 * up being a post-processing version of the tree.
295 * ----------------------------------------------------------------------------- */
297 void Swig_print_xml(DOH *obj, String *filename) {
304 out = NewFile(filename, "w", SWIG_output_files());
306 FileErrorDisplay(filename);
307 SWIG_exit(EXIT_FAILURE);
311 Printf(out, "<?xml version=\"1.0\" ?> \n");
312 xml.Xml_print_tree(obj);
315 static Language *new_swig_xml() {
318 extern "C" Language *swig_xml(void) {
319 return new_swig_xml();