9cf6694f91a008f64fef9f1a1dcb37192da289fb
[pspp] / parse-xml.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <libxml/parser.h>
4 #include <libxml/tree.h>
5
6 static void
7 print_parents(xmlNode *node)
8 {
9   for (; node; node = node->next)
10     {
11       if (node->type == XML_ELEMENT_NODE)
12         {
13           for (xmlNode *child = node->children; child; child = child->next)
14             {
15               if (child->type == XML_ELEMENT_NODE)
16                   printf ("%s %s\n", node->name, child->name);
17               else if (child->type == XML_TEXT_NODE)
18                 printf ("%s <text>\n", node->name);
19               else if (child->type == XML_CDATA_SECTION_NODE)
20                 printf ("%s <cdata>\n", node->name);
21               else if (child->type == XML_COMMENT_NODE)
22                 printf ("%s <comment>\n", node->name);
23               else
24                 printf ("%s <%d>\n", node->name, child->type);
25             }
26         }
27
28       print_parents (node->children);
29     }
30 }
31
32 static void
33 print_containment (xmlNode *node)
34 {
35   for (; node; node = node->next)
36     {
37       if (node->type == XML_ELEMENT_NODE)
38         {
39           const char *child_names[512];
40           int child_name_cnt[512];
41           int n_names = 0;
42           for (xmlNode *child = node->children; child; child = child->next)
43             {
44               const char *name;
45
46               if (child->type == XML_ELEMENT_NODE)
47                 name = (char *) child->name;
48               else if (child->type == XML_TEXT_NODE)
49                 name = "<text>";
50               else if (child->type == XML_CDATA_SECTION_NODE)
51                 name = "<cdata>";
52               else if (child->type == XML_COMMENT_NODE)
53                 {
54                   name = "<comment>";
55                   //printf ("comment %s\n", (char *) child->content);
56                   continue;
57                 }
58               else
59                 name = "<other>";
60
61               for (int i = 0; i < n_names; i++)
62                 if (!strcmp(name, child_names[i]))
63                   {
64                     child_name_cnt[i]++;
65                     goto next;
66                   }
67               child_names[n_names] = name;
68               child_name_cnt[n_names] = 1;
69               n_names++;
70
71             next:;
72             }
73
74           printf ("%s", node->name);
75           for (int i = 0; i < n_names; i++)
76             {
77               printf (" %s", child_names[i]);
78               if (child_name_cnt[i] > 1)
79                 printf ("+");
80             }
81           printf ("\n");
82         }
83
84       print_containment (node->children);
85     }
86 }
87
88 static void
89 print_labels (xmlNode *node)
90 {
91   for (; node; node = node->next)
92     {
93       if (node->type == XML_ELEMENT_NODE
94           && !strcmp((char *) node->name, "label")
95           && node->parent->type == XML_ELEMENT_NODE
96           && !strcmp((char *) node->parent->name, "container"))
97         {
98           for (xmlNode *child = node->children; child; child = child->next)
99             if (child->type == XML_TEXT_NODE)
100               puts ((char *) child->content);
101         }
102
103       print_labels (node->children);
104     }
105 }
106
107 static void
108 print_attributes (xmlNode * a_node)
109 {
110   for (xmlNode *node = a_node; node; node = node->next)
111     {
112       if (node->type == XML_ELEMENT_NODE)
113         {
114           printf ("%s", node->name);
115           for (xmlAttr *attr = node->properties; attr; attr = attr->next)
116             printf (" %s", attr->name);
117           putchar ('\n');
118         }
119
120       print_attributes (node->children);
121     }
122 }
123
124 static void
125 print_string(xmlChar *s)
126 {
127   for (char *p = (char *) s; *p; p++)
128     if (*p == '\n')
129       printf ("\\n");
130     else
131       putchar (*p);
132 }
133
134 static void
135 print_cdata (xmlNode * a_node)
136 {
137   for (xmlNode *node = a_node; node; node = node->next)
138     {
139       if (node->type == XML_CDATA_SECTION_NODE)
140         {
141           print_string (node->content);
142           putchar ('\n');
143         }
144
145       print_cdata (node->children);
146     }
147 }
148
149 static void
150 print_attribute (xmlNode *node, const char *attr)
151 {
152   for (; node; node = node->next)
153     {
154       const char *s = (char *) xmlGetProp (node, (xmlChar *) attr);
155       if (s)
156         printf ("%s %s=%s\n", node->name, attr, s);
157
158       print_attribute (node->children, attr);
159     }
160 }
161
162 static void
163 print_element (xmlDoc *doc, xmlNode *node, const char *element)
164 {
165   for (; node; node = node->next)
166     {
167       if (node->name && !strcmp(element, (char *) node->name))
168         {
169           xmlBuffer *buf = xmlBufferCreate();
170           xmlNodeDump (buf, doc, node, 0, 1);
171           xmlBufferDump (stdout, buf);
172           xmlBufferFree (buf);
173           putchar ('\n');
174         }
175
176       print_element (doc, node->children, element);
177     }
178 }
179
180 static __attribute__((unused)) xmlNode *
181 find_page_setup (xmlNode *node)
182 {
183   for (; node; node = node->next)
184     {
185       if (node->name && !strcmp ((char *) node->name, "pageSetup"))
186         return node;
187
188       xmlNode *ps = find_page_setup (node->children);
189       if (ps)
190         return ps;
191     }
192   return NULL;
193 }
194
195 static void
196 print_text (xmlNode *node)
197 {
198   for (; node; node = node->next)
199     {
200       if (node->type == XML_ELEMENT_NODE)
201         {
202           printf ("%s", node->name);
203           for (xmlNode *child = node->children; child; child = child->next)
204             if (child->type == XML_TEXT_NODE)
205               {
206                 putchar (' ');
207                 print_string (child->content);
208               }
209           putchar ('\n');
210         }
211
212       print_text (node->children);
213     }
214 }
215
216 static void
217 usage (void)
218 {
219   fprintf (stderr, "usage: parse-xml FILE.xml containment|attributes\n");
220   exit (1);
221 }
222
223 int
224 main (int argc, char **argv)
225 {
226   if (argc != 3)
227     usage ();
228
229   LIBXML_TEST_VERSION;
230
231   xmlDoc *doc = xmlReadFile(argv[1], NULL, XML_PARSE_NOBLANKS);
232   if (doc == NULL)
233     {
234       fprintf (stderr, "error: could not parse file %s\n", argv[1]);
235       exit (1);
236     }
237
238   xmlNode *root = xmlDocGetRootElement(doc);
239
240 #if 0
241   /* Limit what we look at to pageSetup node and below. */
242   root = find_page_setup(root);
243   if (!root)
244     return 0;
245   root->next = NULL;
246 #endif
247
248   if (!strcmp(argv[2], "parents"))
249     print_parents (root);
250   else if (!strcmp(argv[2], "containment"))
251     print_containment (root);
252   else if (!strcmp(argv[2], "attributes"))
253     print_attributes (root);
254   else if (!strcmp(argv[2], "cdata"))
255     print_cdata (root);
256   else if (!strcmp(argv[2], "text"))
257     print_text (root);
258   else if (!strncmp(argv[2], "element:", 8))
259     print_element (doc, root, argv[2] + 8);
260   else if (!strncmp(argv[2], "attr:", 5))
261     print_attribute (root, argv[2] + 5);
262   else if (!strcmp(argv[2], "labels"))
263     print_labels (root);
264   else
265     usage ();
266
267   xmlFreeDoc(doc);
268   xmlCleanupParser();
269
270   return 0;
271 }