Start working on detail XML.
[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 __attribute__((unused)) xmlNode *
163 find_page_setup (xmlNode *node)
164 {
165   for (; node; node = node->next)
166     {
167       if (node->name && !strcmp ((char *) node->name, "pageSetup"))
168         return node;
169
170       xmlNode *ps = find_page_setup (node->children);
171       if (ps)
172         return ps;
173     }
174   return NULL;
175 }
176
177 static void
178 print_text (xmlNode *node)
179 {
180   for (; node; node = node->next)
181     {
182       if (node->type == XML_ELEMENT_NODE)
183         {
184           printf ("%s", node->name);
185           for (xmlNode *child = node->children; child; child = child->next)
186             if (child->type == XML_TEXT_NODE)
187               {
188                 putchar (' ');
189                 print_string (child->content);
190               }
191           putchar ('\n');
192         }
193
194       print_text (node->children);
195     }
196 }
197
198 static void
199 usage (void)
200 {
201   fprintf (stderr, "usage: parse-xml FILE.xml containment|attributes\n");
202   exit (1);
203 }
204
205 int
206 main (int argc, char **argv)
207 {
208   if (argc != 3)
209     usage ();
210
211   LIBXML_TEST_VERSION;
212
213   xmlDoc *doc = xmlReadFile(argv[1], NULL, XML_PARSE_NOBLANKS);
214   if (doc == NULL)
215     {
216       fprintf (stderr, "error: could not parse file %s\n", argv[1]);
217       exit (1);
218     }
219
220   xmlNode *root = xmlDocGetRootElement(doc);
221
222 #if 0
223   /* Limit what we look at to pageSetup node and below. */
224   root = find_page_setup(root);
225   if (!root)
226     return 0;
227   root->next = NULL;
228 #endif
229
230   if (!strcmp(argv[2], "parents"))
231     print_parents (root);
232   else if (!strcmp(argv[2], "containment"))
233     print_containment (root);
234   else if (!strcmp(argv[2], "attributes"))
235     print_attributes (root);
236   else if (!strcmp(argv[2], "cdata"))
237     print_cdata (root);
238   else if (!strcmp(argv[2], "text"))
239     print_text (root);
240   else if (!strncmp(argv[2], "attr:", 5))
241     print_attribute (root, argv[2] + 5);
242   else if (!strcmp(argv[2], "labels"))
243     print_labels (root);
244   else
245     usage ();
246
247   xmlFreeDoc(doc);
248   xmlCleanupParser();
249
250   return 0;
251 }