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