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