blob: 599ece8b85949e0764a01158b5507344ad4ae47b [file] [log] [blame]
Daniel Veillardbb2da581999-06-13 14:37:07 +00001/*
2 * gjobread.c : a small test program for gnome jobs XML format
3 *
4 * See Copyright for the status of this software.
5 *
6 * Daniel.Veillard@w3.org
7 */
8
9#include <stdio.h>
10#include <string.h>
11#include <stdlib.h>
12
Daniel Veillardbe803962000-06-28 23:40:59 +000013/*
14 * This example should compile and run indifferently with libxml-1.8.8 +
15 * and libxml2-2.1.0 +
16 * Check the COMPAT comments below
17 */
18
19/*
20 * COMPAT using xml-config --cflags to get the include path this will
21 * work with both
22 */
23#include <libxml/xmlmemory.h>
Daniel Veillardf3029822000-05-06 08:11:19 +000024#include <libxml/parser.h>
Daniel Veillardbb2da581999-06-13 14:37:07 +000025
26#define DEBUG(x) printf(x)
27
28/*
29 * A person record
Daniel Veillardf17e09b2001-01-25 13:55:35 +000030 * an xmlChar * is really an UTF8 encoded char string (0 terminated)
Daniel Veillardbb2da581999-06-13 14:37:07 +000031 */
32typedef struct person {
Daniel Veillardf17e09b2001-01-25 13:55:35 +000033 xmlChar *name;
34 xmlChar *email;
35 xmlChar *company;
36 xmlChar *organisation;
37 xmlChar *smail;
38 xmlChar *webPage;
39 xmlChar *phone;
Daniel Veillardbb2da581999-06-13 14:37:07 +000040} person, *personPtr;
41
42/*
43 * And the code needed to parse it
44 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +000045static personPtr
46parsePerson(xmlDocPtr doc, xmlNsPtr ns, xmlNodePtr cur) {
Daniel Veillardbb2da581999-06-13 14:37:07 +000047 personPtr ret = NULL;
48
49DEBUG("parsePerson\n");
50 /*
51 * allocate the struct
52 */
53 ret = (personPtr) malloc(sizeof(person));
54 if (ret == NULL) {
55 fprintf(stderr,"out of memory\n");
56 return(NULL);
57 }
58 memset(ret, 0, sizeof(person));
59
60 /* We don't care what the top level element name is */
Daniel Veillardbe803962000-06-28 23:40:59 +000061 /* COMPAT xmlChildrenNode is a macro unifying libxml1 and libxml2 names */
62 cur = cur->xmlChildrenNode;
Daniel Veillardbb2da581999-06-13 14:37:07 +000063 while (cur != NULL) {
Daniel Veillardf17e09b2001-01-25 13:55:35 +000064 if ((!xmlStrcmp(cur->name, (const xmlChar *)"Person")) &&
65 (cur->ns == ns))
Daniel Veillardbe803962000-06-28 23:40:59 +000066 ret->name = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
Daniel Veillardf17e09b2001-01-25 13:55:35 +000067 if ((!xmlStrcmp(cur->name, (const xmlChar *)"Email")) &&
68 (cur->ns == ns))
Daniel Veillardbe803962000-06-28 23:40:59 +000069 ret->email = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
Daniel Veillardbb2da581999-06-13 14:37:07 +000070 cur = cur->next;
71 }
72
73 return(ret);
74}
75
76/*
77 * and to print it
78 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +000079static void
80printPerson(personPtr cur) {
Daniel Veillardbb2da581999-06-13 14:37:07 +000081 if (cur == NULL) return;
82 printf("------ Person\n");
83 if (cur->name) printf(" name: %s\n", cur->name);
84 if (cur->email) printf(" email: %s\n", cur->email);
85 if (cur->company) printf(" company: %s\n", cur->company);
86 if (cur->organisation) printf(" organisation: %s\n", cur->organisation);
87 if (cur->smail) printf(" smail: %s\n", cur->smail);
88 if (cur->webPage) printf(" Web: %s\n", cur->webPage);
89 if (cur->phone) printf(" phone: %s\n", cur->phone);
90 printf("------\n");
91}
92
93/*
94 * a Description for a Job
95 */
96typedef struct job {
Daniel Veillardf17e09b2001-01-25 13:55:35 +000097 xmlChar *projectID;
98 xmlChar *application;
99 xmlChar *category;
Daniel Veillardbb2da581999-06-13 14:37:07 +0000100 personPtr contact;
101 int nbDevelopers;
102 personPtr developers[100]; /* using dynamic alloc is left as an exercise */
103} job, *jobPtr;
104
105/*
106 * And the code needed to parse it
107 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000108static jobPtr
109parseJob(xmlDocPtr doc, xmlNsPtr ns, xmlNodePtr cur) {
Daniel Veillardbb2da581999-06-13 14:37:07 +0000110 jobPtr ret = NULL;
111
112DEBUG("parseJob\n");
113 /*
114 * allocate the struct
115 */
116 ret = (jobPtr) malloc(sizeof(job));
117 if (ret == NULL) {
118 fprintf(stderr,"out of memory\n");
119 return(NULL);
120 }
121 memset(ret, 0, sizeof(job));
122
123 /* We don't care what the top level element name is */
Daniel Veillardbe803962000-06-28 23:40:59 +0000124 cur = cur->xmlChildrenNode;
Daniel Veillardbb2da581999-06-13 14:37:07 +0000125 while (cur != NULL) {
126
Daniel Veillardf17e09b2001-01-25 13:55:35 +0000127 if ((!xmlStrcmp(cur->name, (const xmlChar *) "Project")) &&
128 (cur->ns == ns)) {
129 ret->projectID = xmlGetProp(cur, (const xmlChar *) "ID");
Daniel Veillardbb2da581999-06-13 14:37:07 +0000130 if (ret->projectID == NULL) {
131 fprintf(stderr, "Project has no ID\n");
132 }
133 }
Daniel Veillardf17e09b2001-01-25 13:55:35 +0000134 if ((!xmlStrcmp(cur->name, (const xmlChar *) "Application")) &&
135 (cur->ns == ns))
136 ret->application =
137 xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
138 if ((!xmlStrcmp(cur->name, (const xmlChar *) "Category")) &&
139 (cur->ns == ns))
140 ret->category =
141 xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
142 if ((!xmlStrcmp(cur->name, (const xmlChar *) "Contact")) &&
143 (cur->ns == ns))
Daniel Veillardbb2da581999-06-13 14:37:07 +0000144 ret->contact = parsePerson(doc, ns, cur);
145 cur = cur->next;
146 }
147
148 return(ret);
149}
150
151/*
152 * and to print it
153 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000154static void
155printJob(jobPtr cur) {
Daniel Veillardbb2da581999-06-13 14:37:07 +0000156 int i;
157
158 if (cur == NULL) return;
159 printf("======= Job\n");
160 if (cur->projectID != NULL) printf("projectID: %s\n", cur->projectID);
161 if (cur->application != NULL) printf("application: %s\n", cur->application);
162 if (cur->category != NULL) printf("category: %s\n", cur->category);
163 if (cur->contact != NULL) printPerson(cur->contact);
164 printf("%d developers\n", cur->nbDevelopers);
165
166 for (i = 0;i < cur->nbDevelopers;i++) printPerson(cur->developers[i]);
167 printf("======= \n");
168}
169
170/*
171 * A pool of Gnome Jobs
172 */
173typedef struct gjob {
174 int nbJobs;
175 jobPtr jobs[500]; /* using dynamic alloc is left as an exercise */
176} gJob, *gJobPtr;
177
178
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000179static gJobPtr
180parseGjobFile(char *filename) {
Daniel Veillardbb2da581999-06-13 14:37:07 +0000181 xmlDocPtr doc;
182 gJobPtr ret;
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000183 jobPtr curjob;
Daniel Veillardbb2da581999-06-13 14:37:07 +0000184 xmlNsPtr ns;
185 xmlNodePtr cur;
186
187 /*
188 * build an XML tree from a the file;
189 */
190 doc = xmlParseFile(filename);
191 if (doc == NULL) return(NULL);
192
193 /*
194 * Check the document is of the right kind
195 */
Daniel Veillardf3029822000-05-06 08:11:19 +0000196
Daniel Veillardf3029822000-05-06 08:11:19 +0000197 cur = xmlDocGetRootElement(doc);
Daniel Veillardbb2da581999-06-13 14:37:07 +0000198 if (cur == NULL) {
199 fprintf(stderr,"empty document\n");
200 xmlFreeDoc(doc);
201 return(NULL);
202 }
Daniel Veillardf17e09b2001-01-25 13:55:35 +0000203 ns = xmlSearchNsByHref(doc, cur,
204 (const xmlChar *) "http://www.gnome.org/some-location");
Daniel Veillardbb2da581999-06-13 14:37:07 +0000205 if (ns == NULL) {
206 fprintf(stderr,
207 "document of the wrong type, GJob Namespace not found\n");
208 xmlFreeDoc(doc);
209 return(NULL);
210 }
Daniel Veillardf17e09b2001-01-25 13:55:35 +0000211 if (xmlStrcmp(cur->name, (const xmlChar *) "Helping")) {
Daniel Veillardbb2da581999-06-13 14:37:07 +0000212 fprintf(stderr,"document of the wrong type, root node != Helping");
213 xmlFreeDoc(doc);
214 return(NULL);
215 }
216
217 /*
218 * Allocate the structure to be returned.
219 */
220 ret = (gJobPtr) malloc(sizeof(gJob));
221 if (ret == NULL) {
222 fprintf(stderr,"out of memory\n");
223 xmlFreeDoc(doc);
224 return(NULL);
225 }
226 memset(ret, 0, sizeof(gJob));
227
228 /*
229 * Now, walk the tree.
230 */
231 /* First level we expect just Jobs */
Daniel Veillardbe803962000-06-28 23:40:59 +0000232 cur = cur->xmlChildrenNode;
Daniel Veillardf3029822000-05-06 08:11:19 +0000233 while ( cur && xmlIsBlankNode ( cur ) )
234 {
235 cur = cur -> next;
236 }
237 if ( cur == 0 )
238 return ( NULL );
Daniel Veillardf17e09b2001-01-25 13:55:35 +0000239 if ((xmlStrcmp(cur->name, (const xmlChar *) "Jobs")) || (cur->ns != ns)) {
Daniel Veillardf3029822000-05-06 08:11:19 +0000240 fprintf(stderr,"document of the wrong type, was '%s', Jobs expected",
241 cur->name);
242 fprintf(stderr,"xmlDocDump follows\n");
243 xmlDocDump ( stderr, doc );
244 fprintf(stderr,"xmlDocDump finished\n");
Daniel Veillardbb2da581999-06-13 14:37:07 +0000245 xmlFreeDoc(doc);
246 free(ret);
247 return(NULL);
248 }
249
250 /* Second level is a list of Job, but be laxist */
Daniel Veillardbe803962000-06-28 23:40:59 +0000251 cur = cur->xmlChildrenNode;
Daniel Veillardbb2da581999-06-13 14:37:07 +0000252 while (cur != NULL) {
Daniel Veillardf17e09b2001-01-25 13:55:35 +0000253 if ((!xmlStrcmp(cur->name, (const xmlChar *) "Job")) &&
254 (cur->ns == ns)) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000255 curjob = parseJob(doc, ns, cur);
256 if (curjob != NULL)
257 ret->jobs[ret->nbJobs++] = curjob;
Daniel Veillardbb2da581999-06-13 14:37:07 +0000258 if (ret->nbJobs >= 500) break;
259 }
260 cur = cur->next;
261 }
262
263 return(ret);
264}
265
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000266static void
267handleGjob(gJobPtr cur) {
Daniel Veillardbb2da581999-06-13 14:37:07 +0000268 int i;
269
270 /*
271 * Do whatever you want and free the structure.
272 */
273 printf("%d Jobs registered\n", cur->nbJobs);
274 for (i = 0; i < cur->nbJobs; i++) printJob(cur->jobs[i]);
275}
276
277int main(int argc, char **argv) {
278 int i;
279 gJobPtr cur;
280
Daniel Veillardbe803962000-06-28 23:40:59 +0000281 /* COMPAT: Do not genrate nodes for formatting spaces */
Daniel Veillardc2304102000-06-29 00:43:27 +0000282 LIBXML_TEST_VERSION
Daniel Veillardbe803962000-06-28 23:40:59 +0000283 xmlKeepBlanksDefault(0);
284
Daniel Veillardbb2da581999-06-13 14:37:07 +0000285 for (i = 1; i < argc ; i++) {
286 cur = parseGjobFile(argv[i]);
Daniel Veillardf3029822000-05-06 08:11:19 +0000287 if ( cur )
288 handleGjob(cur);
289 else
290 fprintf( stderr, "Error parsing file '%s'\n", argv[i]);
291
Daniel Veillardbb2da581999-06-13 14:37:07 +0000292 }
Daniel Veillard5aac4e42001-08-15 20:46:57 +0000293
294 /* Clean up everything else before quitting. */
295 xmlCleanupParser();
296
Daniel Veillardbb2da581999-06-13 14:37:07 +0000297 return(0);
298}