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 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 45 | static personPtr |
| 46 | parsePerson(xmlDocPtr doc, xmlNsPtr ns, xmlNodePtr cur) { |
Daniel Veillard | bb2da58 | 1999-06-13 14:37:07 +0000 | [diff] [blame] | 47 | personPtr ret = NULL; |
| 48 | |
| 49 | DEBUG("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 Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 61 | /* COMPAT xmlChildrenNode is a macro unifying libxml1 and libxml2 names */ |
| 62 | cur = cur->xmlChildrenNode; |
Daniel Veillard | bb2da58 | 1999-06-13 14:37:07 +0000 | [diff] [blame] | 63 | while (cur != NULL) { |
Daniel Veillard | f17e09b | 2001-01-25 13:55:35 +0000 | [diff] [blame] | 64 | if ((!xmlStrcmp(cur->name, (const xmlChar *)"Person")) && |
| 65 | (cur->ns == ns)) |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 66 | ret->name = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); |
Daniel Veillard | f17e09b | 2001-01-25 13:55:35 +0000 | [diff] [blame] | 67 | if ((!xmlStrcmp(cur->name, (const xmlChar *)"Email")) && |
| 68 | (cur->ns == ns)) |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 69 | ret->email = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); |
Daniel Veillard | bb2da58 | 1999-06-13 14:37:07 +0000 | [diff] [blame] | 70 | cur = cur->next; |
| 71 | } |
| 72 | |
| 73 | return(ret); |
| 74 | } |
| 75 | |
| 76 | /* |
| 77 | * and to print it |
| 78 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 79 | static void |
| 80 | printPerson(personPtr cur) { |
Daniel Veillard | bb2da58 | 1999-06-13 14:37:07 +0000 | [diff] [blame] | 81 | 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 | */ |
| 96 | typedef struct job { |
Daniel Veillard | f17e09b | 2001-01-25 13:55:35 +0000 | [diff] [blame] | 97 | xmlChar *projectID; |
| 98 | xmlChar *application; |
| 99 | xmlChar *category; |
Daniel Veillard | bb2da58 | 1999-06-13 14:37:07 +0000 | [diff] [blame] | 100 | 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 Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 108 | static jobPtr |
| 109 | parseJob(xmlDocPtr doc, xmlNsPtr ns, xmlNodePtr cur) { |
Daniel Veillard | bb2da58 | 1999-06-13 14:37:07 +0000 | [diff] [blame] | 110 | jobPtr ret = NULL; |
| 111 | |
| 112 | DEBUG("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 Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 124 | cur = cur->xmlChildrenNode; |
Daniel Veillard | bb2da58 | 1999-06-13 14:37:07 +0000 | [diff] [blame] | 125 | while (cur != NULL) { |
| 126 | |
Daniel Veillard | f17e09b | 2001-01-25 13:55:35 +0000 | [diff] [blame] | 127 | if ((!xmlStrcmp(cur->name, (const xmlChar *) "Project")) && |
| 128 | (cur->ns == ns)) { |
| 129 | ret->projectID = xmlGetProp(cur, (const xmlChar *) "ID"); |
Daniel Veillard | bb2da58 | 1999-06-13 14:37:07 +0000 | [diff] [blame] | 130 | if (ret->projectID == NULL) { |
| 131 | fprintf(stderr, "Project has no ID\n"); |
| 132 | } |
| 133 | } |
Daniel Veillard | f17e09b | 2001-01-25 13:55:35 +0000 | [diff] [blame] | 134 | 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 Veillard | bb2da58 | 1999-06-13 14:37:07 +0000 | [diff] [blame] | 144 | 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 Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 154 | static void |
| 155 | printJob(jobPtr cur) { |
Daniel Veillard | bb2da58 | 1999-06-13 14:37:07 +0000 | [diff] [blame] | 156 | 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 | */ |
| 173 | typedef struct gjob { |
| 174 | int nbJobs; |
| 175 | jobPtr jobs[500]; /* using dynamic alloc is left as an exercise */ |
| 176 | } gJob, *gJobPtr; |
| 177 | |
| 178 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 179 | static gJobPtr |
| 180 | parseGjobFile(char *filename) { |
Daniel Veillard | bb2da58 | 1999-06-13 14:37:07 +0000 | [diff] [blame] | 181 | xmlDocPtr doc; |
| 182 | gJobPtr ret; |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 183 | jobPtr curjob; |
Daniel Veillard | bb2da58 | 1999-06-13 14:37:07 +0000 | [diff] [blame] | 184 | xmlNsPtr ns; |
| 185 | xmlNodePtr cur; |
| 186 | |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 187 | #ifdef LIBXML_SAX1_ENABLED |
Daniel Veillard | bb2da58 | 1999-06-13 14:37:07 +0000 | [diff] [blame] | 188 | /* |
| 189 | * build an XML tree from a the file; |
| 190 | */ |
| 191 | doc = xmlParseFile(filename); |
| 192 | if (doc == NULL) return(NULL); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 193 | #else |
| 194 | /* |
| 195 | * the library has been compiled without some of the old interfaces |
| 196 | */ |
| 197 | return(NULL); |
| 198 | #endif /* LIBXML_SAX1_ENABLED */ |
Daniel Veillard | bb2da58 | 1999-06-13 14:37:07 +0000 | [diff] [blame] | 199 | |
| 200 | /* |
| 201 | * Check the document is of the right kind |
| 202 | */ |
Daniel Veillard | f302982 | 2000-05-06 08:11:19 +0000 | [diff] [blame] | 203 | |
Daniel Veillard | f302982 | 2000-05-06 08:11:19 +0000 | [diff] [blame] | 204 | cur = xmlDocGetRootElement(doc); |
Daniel Veillard | bb2da58 | 1999-06-13 14:37:07 +0000 | [diff] [blame] | 205 | if (cur == NULL) { |
| 206 | fprintf(stderr,"empty document\n"); |
| 207 | xmlFreeDoc(doc); |
| 208 | return(NULL); |
| 209 | } |
Daniel Veillard | f17e09b | 2001-01-25 13:55:35 +0000 | [diff] [blame] | 210 | ns = xmlSearchNsByHref(doc, cur, |
| 211 | (const xmlChar *) "http://www.gnome.org/some-location"); |
Daniel Veillard | bb2da58 | 1999-06-13 14:37:07 +0000 | [diff] [blame] | 212 | if (ns == NULL) { |
| 213 | fprintf(stderr, |
| 214 | "document of the wrong type, GJob Namespace not found\n"); |
| 215 | xmlFreeDoc(doc); |
| 216 | return(NULL); |
| 217 | } |
Daniel Veillard | f17e09b | 2001-01-25 13:55:35 +0000 | [diff] [blame] | 218 | if (xmlStrcmp(cur->name, (const xmlChar *) "Helping")) { |
Daniel Veillard | bb2da58 | 1999-06-13 14:37:07 +0000 | [diff] [blame] | 219 | fprintf(stderr,"document of the wrong type, root node != Helping"); |
| 220 | xmlFreeDoc(doc); |
| 221 | return(NULL); |
| 222 | } |
| 223 | |
| 224 | /* |
| 225 | * Allocate the structure to be returned. |
| 226 | */ |
| 227 | ret = (gJobPtr) malloc(sizeof(gJob)); |
| 228 | if (ret == NULL) { |
| 229 | fprintf(stderr,"out of memory\n"); |
| 230 | xmlFreeDoc(doc); |
| 231 | return(NULL); |
| 232 | } |
| 233 | memset(ret, 0, sizeof(gJob)); |
| 234 | |
| 235 | /* |
| 236 | * Now, walk the tree. |
| 237 | */ |
| 238 | /* First level we expect just Jobs */ |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 239 | cur = cur->xmlChildrenNode; |
Daniel Veillard | f302982 | 2000-05-06 08:11:19 +0000 | [diff] [blame] | 240 | while ( cur && xmlIsBlankNode ( cur ) ) |
| 241 | { |
| 242 | cur = cur -> next; |
| 243 | } |
| 244 | if ( cur == 0 ) |
| 245 | return ( NULL ); |
Daniel Veillard | f17e09b | 2001-01-25 13:55:35 +0000 | [diff] [blame] | 246 | if ((xmlStrcmp(cur->name, (const xmlChar *) "Jobs")) || (cur->ns != ns)) { |
Daniel Veillard | f302982 | 2000-05-06 08:11:19 +0000 | [diff] [blame] | 247 | fprintf(stderr,"document of the wrong type, was '%s', Jobs expected", |
| 248 | cur->name); |
| 249 | fprintf(stderr,"xmlDocDump follows\n"); |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 250 | #ifdef LIBXML_OUTPUT_ENABLED |
Daniel Veillard | f302982 | 2000-05-06 08:11:19 +0000 | [diff] [blame] | 251 | xmlDocDump ( stderr, doc ); |
| 252 | fprintf(stderr,"xmlDocDump finished\n"); |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 253 | #endif /* LIBXML_OUTPUT_ENABLED */ |
Daniel Veillard | bb2da58 | 1999-06-13 14:37:07 +0000 | [diff] [blame] | 254 | xmlFreeDoc(doc); |
| 255 | free(ret); |
| 256 | return(NULL); |
| 257 | } |
| 258 | |
| 259 | /* Second level is a list of Job, but be laxist */ |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 260 | cur = cur->xmlChildrenNode; |
Daniel Veillard | bb2da58 | 1999-06-13 14:37:07 +0000 | [diff] [blame] | 261 | while (cur != NULL) { |
Daniel Veillard | f17e09b | 2001-01-25 13:55:35 +0000 | [diff] [blame] | 262 | if ((!xmlStrcmp(cur->name, (const xmlChar *) "Job")) && |
| 263 | (cur->ns == ns)) { |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 264 | curjob = parseJob(doc, ns, cur); |
| 265 | if (curjob != NULL) |
| 266 | ret->jobs[ret->nbJobs++] = curjob; |
Daniel Veillard | bb2da58 | 1999-06-13 14:37:07 +0000 | [diff] [blame] | 267 | if (ret->nbJobs >= 500) break; |
| 268 | } |
| 269 | cur = cur->next; |
| 270 | } |
| 271 | |
| 272 | return(ret); |
| 273 | } |
| 274 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 275 | static void |
| 276 | handleGjob(gJobPtr cur) { |
Daniel Veillard | bb2da58 | 1999-06-13 14:37:07 +0000 | [diff] [blame] | 277 | int i; |
| 278 | |
| 279 | /* |
| 280 | * Do whatever you want and free the structure. |
| 281 | */ |
| 282 | printf("%d Jobs registered\n", cur->nbJobs); |
| 283 | for (i = 0; i < cur->nbJobs; i++) printJob(cur->jobs[i]); |
| 284 | } |
| 285 | |
| 286 | int main(int argc, char **argv) { |
| 287 | int i; |
| 288 | gJobPtr cur; |
| 289 | |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 290 | /* COMPAT: Do not genrate nodes for formatting spaces */ |
Daniel Veillard | c230410 | 2000-06-29 00:43:27 +0000 | [diff] [blame] | 291 | LIBXML_TEST_VERSION |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 292 | xmlKeepBlanksDefault(0); |
| 293 | |
Daniel Veillard | bb2da58 | 1999-06-13 14:37:07 +0000 | [diff] [blame] | 294 | for (i = 1; i < argc ; i++) { |
| 295 | cur = parseGjobFile(argv[i]); |
Daniel Veillard | f302982 | 2000-05-06 08:11:19 +0000 | [diff] [blame] | 296 | if ( cur ) |
| 297 | handleGjob(cur); |
| 298 | else |
| 299 | fprintf( stderr, "Error parsing file '%s'\n", argv[i]); |
| 300 | |
Daniel Veillard | bb2da58 | 1999-06-13 14:37:07 +0000 | [diff] [blame] | 301 | } |
Daniel Veillard | 5aac4e4 | 2001-08-15 20:46:57 +0000 | [diff] [blame] | 302 | |
| 303 | /* Clean up everything else before quitting. */ |
| 304 | xmlCleanupParser(); |
| 305 | |
Daniel Veillard | bb2da58 | 1999-06-13 14:37:07 +0000 | [diff] [blame] | 306 | return(0); |
| 307 | } |