blob: ce3f31bbcd3b787adad0825f164448ccdd5852d5 [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
13#include "parser.h"
14
15#define DEBUG(x) printf(x)
16
17/*
18 * A person record
19 */
20typedef struct person {
21 char *name;
22 char *email;
23 char *company;
24 char *organisation;
25 char *smail;
26 char *webPage;
27 char *phone;
28} person, *personPtr;
29
30/*
31 * And the code needed to parse it
32 */
33personPtr parsePerson(xmlDocPtr doc, xmlNsPtr ns, xmlNodePtr cur) {
34 personPtr ret = NULL;
35
36DEBUG("parsePerson\n");
37 /*
38 * allocate the struct
39 */
40 ret = (personPtr) malloc(sizeof(person));
41 if (ret == NULL) {
42 fprintf(stderr,"out of memory\n");
43 return(NULL);
44 }
45 memset(ret, 0, sizeof(person));
46
47 /* We don't care what the top level element name is */
48 cur = cur->childs;
49 while (cur != NULL) {
50 if ((!strcmp(cur->name, "Person")) && (cur->ns == ns))
51 ret->name = xmlNodeListGetString(doc, cur->childs, 1);
52 if ((!strcmp(cur->name, "Email")) && (cur->ns == ns))
53 ret->email = xmlNodeListGetString(doc, cur->childs, 1);
54 cur = cur->next;
55 }
56
57 return(ret);
58}
59
60/*
61 * and to print it
62 */
63void printPerson(personPtr cur) {
64 if (cur == NULL) return;
65 printf("------ Person\n");
66 if (cur->name) printf(" name: %s\n", cur->name);
67 if (cur->email) printf(" email: %s\n", cur->email);
68 if (cur->company) printf(" company: %s\n", cur->company);
69 if (cur->organisation) printf(" organisation: %s\n", cur->organisation);
70 if (cur->smail) printf(" smail: %s\n", cur->smail);
71 if (cur->webPage) printf(" Web: %s\n", cur->webPage);
72 if (cur->phone) printf(" phone: %s\n", cur->phone);
73 printf("------\n");
74}
75
76/*
77 * a Description for a Job
78 */
79typedef struct job {
80 char *projectID;
81 char *application;
82 char *category;
83 personPtr contact;
84 int nbDevelopers;
85 personPtr developers[100]; /* using dynamic alloc is left as an exercise */
86} job, *jobPtr;
87
88/*
89 * And the code needed to parse it
90 */
91jobPtr parseJob(xmlDocPtr doc, xmlNsPtr ns, xmlNodePtr cur) {
92 jobPtr ret = NULL;
93
94DEBUG("parseJob\n");
95 /*
96 * allocate the struct
97 */
98 ret = (jobPtr) malloc(sizeof(job));
99 if (ret == NULL) {
100 fprintf(stderr,"out of memory\n");
101 return(NULL);
102 }
103 memset(ret, 0, sizeof(job));
104
105 /* We don't care what the top level element name is */
106 cur = cur->childs;
107 while (cur != NULL) {
108
109 if ((!strcmp(cur->name, "Project")) && (cur->ns == ns)) {
110 ret->projectID = xmlGetProp(cur, "ID");
111 if (ret->projectID == NULL) {
112 fprintf(stderr, "Project has no ID\n");
113 }
114 }
115 if ((!strcmp(cur->name, "Application")) && (cur->ns == ns))
116 ret->application = xmlNodeListGetString(doc, cur->childs, 1);
117 if ((!strcmp(cur->name, "Category")) && (cur->ns == ns))
118 ret->category = xmlNodeListGetString(doc, cur->childs, 1);
119 if ((!strcmp(cur->name, "Contact")) && (cur->ns == ns))
120 ret->contact = parsePerson(doc, ns, cur);
121 cur = cur->next;
122 }
123
124 return(ret);
125}
126
127/*
128 * and to print it
129 */
130void printJob(jobPtr cur) {
131 int i;
132
133 if (cur == NULL) return;
134 printf("======= Job\n");
135 if (cur->projectID != NULL) printf("projectID: %s\n", cur->projectID);
136 if (cur->application != NULL) printf("application: %s\n", cur->application);
137 if (cur->category != NULL) printf("category: %s\n", cur->category);
138 if (cur->contact != NULL) printPerson(cur->contact);
139 printf("%d developers\n", cur->nbDevelopers);
140
141 for (i = 0;i < cur->nbDevelopers;i++) printPerson(cur->developers[i]);
142 printf("======= \n");
143}
144
145/*
146 * A pool of Gnome Jobs
147 */
148typedef struct gjob {
149 int nbJobs;
150 jobPtr jobs[500]; /* using dynamic alloc is left as an exercise */
151} gJob, *gJobPtr;
152
153
154gJobPtr parseGjobFile(char *filename) {
155 xmlDocPtr doc;
156 gJobPtr ret;
157 jobPtr job;
158 xmlNsPtr ns;
159 xmlNodePtr cur;
160
161 /*
162 * build an XML tree from a the file;
163 */
164 doc = xmlParseFile(filename);
165 if (doc == NULL) return(NULL);
166
167 /*
168 * Check the document is of the right kind
169 */
170 cur = doc->root;
171 if (cur == NULL) {
172 fprintf(stderr,"empty document\n");
173 xmlFreeDoc(doc);
174 return(NULL);
175 }
176 ns = xmlSearchNsByHref(doc, cur, "http://www.gnome.org/some-location");
177 if (ns == NULL) {
178 fprintf(stderr,
179 "document of the wrong type, GJob Namespace not found\n");
180 xmlFreeDoc(doc);
181 return(NULL);
182 }
183 if (strcmp(cur->name, "Helping")) {
184 fprintf(stderr,"document of the wrong type, root node != Helping");
185 xmlFreeDoc(doc);
186 return(NULL);
187 }
188
189 /*
190 * Allocate the structure to be returned.
191 */
192 ret = (gJobPtr) malloc(sizeof(gJob));
193 if (ret == NULL) {
194 fprintf(stderr,"out of memory\n");
195 xmlFreeDoc(doc);
196 return(NULL);
197 }
198 memset(ret, 0, sizeof(gJob));
199
200 /*
201 * Now, walk the tree.
202 */
203 /* First level we expect just Jobs */
204 cur = cur->childs;
205 if ((strcmp(cur->name, "Jobs")) || (cur->ns != ns)) {
206 fprintf(stderr,"document of the wrong type, Jobs expected");
207 xmlFreeDoc(doc);
208 free(ret);
209 return(NULL);
210 }
211
212 /* Second level is a list of Job, but be laxist */
213 cur = cur->childs;
214 while (cur != NULL) {
215 if ((!strcmp(cur->name, "Job")) && (cur->ns == ns)) {
216 job = parseJob(doc, ns, cur);
217 if (job != NULL)
218 ret->jobs[ret->nbJobs++] = job;
219 if (ret->nbJobs >= 500) break;
220 }
221 cur = cur->next;
222 }
223
224 return(ret);
225}
226
227void handleGjob(gJobPtr cur) {
228 int i;
229
230 /*
231 * Do whatever you want and free the structure.
232 */
233 printf("%d Jobs registered\n", cur->nbJobs);
234 for (i = 0; i < cur->nbJobs; i++) printJob(cur->jobs[i]);
235}
236
237int main(int argc, char **argv) {
238 int i;
239 gJobPtr cur;
240
241 for (i = 1; i < argc ; i++) {
242 cur = parseGjobFile(argv[i]);
243 handleGjob(cur);
244 }
245 return(0);
246}