Document pageSetup.
[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_containment (xmlNode *node)
8 {
9   for (; node; node = node->next)
10     {
11       if (node->type == XML_ELEMENT_NODE)
12         {
13           printf ("%s", node->name);
14           for (xmlNode *child = node->children; child; child = child->next)
15             {
16               putchar (' ');
17               if (child->type == XML_ELEMENT_NODE)
18                 {
19                   printf ("%s", child->name);
20
21                   int n = 0;
22                   while (child->next
23                          && child->next->type == XML_ELEMENT_NODE
24                          && !strcmp((char *) child->name, (char *) child->next->name))
25                     {
26                       child = child->next;
27                       n++;
28                     }
29                   if (n > 0)
30                     putchar ('+');
31                 }
32               else if (child->type == XML_TEXT_NODE)
33                 printf ("<text>");
34               else if (child->type == XML_CDATA_SECTION_NODE)
35                 printf ("<cdata>");
36               else
37                 printf ("<%d>", child->type);
38             }
39           putchar ('\n');
40         }
41
42       print_containment (node->children);
43     }
44 }
45
46 static void
47 print_labels (xmlNode *node)
48 {
49   for (; node; node = node->next)
50     {
51       if (node->type == XML_ELEMENT_NODE
52           && !strcmp((char *) node->name, "label")
53           && node->parent->type == XML_ELEMENT_NODE
54           && !strcmp((char *) node->parent->name, "container"))
55         {
56           for (xmlNode *child = node->children; child; child = child->next)
57             if (child->type == XML_TEXT_NODE)
58               puts ((char *) child->content);
59         }
60
61       print_labels (node->children);
62     }
63 }
64
65 static void
66 print_attributes (xmlNode * a_node)
67 {
68   for (xmlNode *node = a_node; node; node = node->next)
69     {
70       if (node->type == XML_ELEMENT_NODE)
71         {
72           printf ("%s", node->name);
73           for (xmlAttr *attr = node->properties; attr; attr = attr->next)
74             printf (" %s", attr->name);
75           putchar ('\n');
76         }
77
78       print_attributes (node->children);
79     }
80 }
81
82 static void
83 print_string(xmlChar *s)
84 {
85   for (char *p = (char *) s; *p; p++)
86     if (*p == '\n')
87       printf ("\\n");
88     else
89       putchar (*p);
90 }
91
92 static void
93 print_cdata (xmlNode * a_node)
94 {
95   for (xmlNode *node = a_node; node; node = node->next)
96     {
97       if (node->type == XML_CDATA_SECTION_NODE)
98         {
99           print_string (node->content);
100           putchar ('\n');
101         }
102
103       print_cdata (node->children);
104     }
105 }
106
107 static void
108 print_attribute (xmlNode *node, const char *attr)
109 {
110   for (; node; node = node->next)
111     {
112       const char *s = (char *) xmlGetProp (node, (xmlChar *) attr);
113       if (s)
114         printf ("%s %s=%s\n", node->name, attr, s);
115
116       print_attribute (node->children, attr);
117     }
118 }
119
120 static xmlNode *
121 find_page_setup (xmlNode *node)
122 {
123   for (; node; node = node->next)
124     {
125       if (node->name && !strcmp ((char *) node->name, "pageSetup"))
126         return node;
127
128       xmlNode *ps = find_page_setup (node->children);
129       if (ps)
130         return ps;
131     }
132   return NULL;
133 }
134
135 static void
136 print_text (xmlNode *node)
137 {
138   for (; node; node = node->next)
139     {
140       if (node->type == XML_ELEMENT_NODE)
141         {
142           printf ("%s", node->name);
143           for (xmlNode *child = node->children; child; child = child->next)
144             if (child->type == XML_TEXT_NODE)
145               {
146                 putchar (' ');
147                 print_string (child->content);
148               }
149           putchar ('\n');
150         }
151
152       print_text (node->children);
153     }
154 }
155
156 static void
157 usage (void)
158 {
159   fprintf (stderr, "usage: parse-xml FILE.xml containment|attributes\n");
160   exit (1);
161 }
162
163 int
164 main (int argc, char **argv)
165 {
166   if (argc != 3)
167     usage ();
168
169   LIBXML_TEST_VERSION;
170
171   xmlDoc *doc = xmlReadFile(argv[1], NULL, 0);
172   if (doc == NULL)
173     {
174       fprintf (stderr, "error: could not parse file %s\n", argv[1]);
175       exit (1);
176     }
177
178   xmlNode *root = xmlDocGetRootElement(doc);
179   root = find_page_setup(root);
180   if (!root)
181     return 0;
182   root->next = NULL;
183
184   if (!strcmp(argv[2], "containment"))
185     print_containment (root);
186   else if (!strcmp(argv[2], "attributes"))
187     print_attributes (root);
188   else if (!strcmp(argv[2], "cdata"))
189     print_cdata (root);
190   else if (!strcmp(argv[2], "text"))
191     print_text (root);
192   else if (!strncmp(argv[2], "attr:", 5))
193     print_attribute (root, argv[2] + 5);
194   else if (!strcmp(argv[2], "labels"))
195     print_labels (root);
196   else
197     usage ();
198
199   xmlFreeDoc(doc);
200   xmlCleanupParser();
201
202   return 0;
203 }