| |
| |
| |
| |
| |
| |
| |
|
|
| #include <stdio.h> |
| #include <string.h> |
| #include <stdlib.h> |
|
|
| |
| |
| |
| |
| |
|
|
| |
| |
| |
| |
| #include <libxml/xmlmemory.h> |
| #include <libxml/parser.h> |
|
|
| #define DEBUG(x) printf(x) |
|
|
| |
| |
| |
| |
| typedef struct person { |
| xmlChar *name; |
| xmlChar *email; |
| xmlChar *company; |
| xmlChar *organisation; |
| xmlChar *smail; |
| xmlChar *webPage; |
| xmlChar *phone; |
| } person, *personPtr; |
|
|
| |
| |
| |
| static personPtr |
| parsePerson(xmlDocPtr doc, xmlNsPtr ns, xmlNodePtr cur) { |
| personPtr ret = NULL; |
|
|
| DEBUG("parsePerson\n"); |
| |
| |
| |
| ret = (personPtr) malloc(sizeof(person)); |
| if (ret == NULL) { |
| fprintf(stderr,"out of memory\n"); |
| return(NULL); |
| } |
| memset(ret, 0, sizeof(person)); |
|
|
| |
| |
| cur = cur->xmlChildrenNode; |
| while (cur != NULL) { |
| if ((!xmlStrcmp(cur->name, (const xmlChar *)"Person")) && |
| (cur->ns == ns)) |
| ret->name = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); |
| if ((!xmlStrcmp(cur->name, (const xmlChar *)"Email")) && |
| (cur->ns == ns)) |
| ret->email = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); |
| cur = cur->next; |
| } |
|
|
| return(ret); |
| } |
|
|
| |
| |
| |
| static void |
| printPerson(personPtr cur) { |
| if (cur == NULL) return; |
| printf("------ Person\n"); |
| if (cur->name) printf(" name: %s\n", cur->name); |
| if (cur->email) printf(" email: %s\n", cur->email); |
| if (cur->company) printf(" company: %s\n", cur->company); |
| if (cur->organisation) printf(" organisation: %s\n", cur->organisation); |
| if (cur->smail) printf(" smail: %s\n", cur->smail); |
| if (cur->webPage) printf(" Web: %s\n", cur->webPage); |
| if (cur->phone) printf(" phone: %s\n", cur->phone); |
| printf("------\n"); |
| } |
|
|
| |
| |
| |
| typedef struct job { |
| xmlChar *projectID; |
| xmlChar *application; |
| xmlChar *category; |
| personPtr contact; |
| int nbDevelopers; |
| personPtr developers[100]; |
| } job, *jobPtr; |
|
|
| |
| |
| |
| static jobPtr |
| parseJob(xmlDocPtr doc, xmlNsPtr ns, xmlNodePtr cur) { |
| jobPtr ret = NULL; |
|
|
| DEBUG("parseJob\n"); |
| |
| |
| |
| ret = (jobPtr) malloc(sizeof(job)); |
| if (ret == NULL) { |
| fprintf(stderr,"out of memory\n"); |
| return(NULL); |
| } |
| memset(ret, 0, sizeof(job)); |
|
|
| |
| cur = cur->xmlChildrenNode; |
| while (cur != NULL) { |
| |
| if ((!xmlStrcmp(cur->name, (const xmlChar *) "Project")) && |
| (cur->ns == ns)) { |
| ret->projectID = xmlGetProp(cur, (const xmlChar *) "ID"); |
| if (ret->projectID == NULL) { |
| fprintf(stderr, "Project has no ID\n"); |
| } |
| } |
| if ((!xmlStrcmp(cur->name, (const xmlChar *) "Application")) && |
| (cur->ns == ns)) |
| ret->application = |
| xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); |
| if ((!xmlStrcmp(cur->name, (const xmlChar *) "Category")) && |
| (cur->ns == ns)) |
| ret->category = |
| xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); |
| if ((!xmlStrcmp(cur->name, (const xmlChar *) "Contact")) && |
| (cur->ns == ns)) |
| ret->contact = parsePerson(doc, ns, cur); |
| cur = cur->next; |
| } |
|
|
| return(ret); |
| } |
|
|
| |
| |
| |
| static void |
| printJob(jobPtr cur) { |
| int i; |
|
|
| if (cur == NULL) return; |
| printf("======= Job\n"); |
| if (cur->projectID != NULL) printf("projectID: %s\n", cur->projectID); |
| if (cur->application != NULL) printf("application: %s\n", cur->application); |
| if (cur->category != NULL) printf("category: %s\n", cur->category); |
| if (cur->contact != NULL) printPerson(cur->contact); |
| printf("%d developers\n", cur->nbDevelopers); |
|
|
| for (i = 0;i < cur->nbDevelopers;i++) printPerson(cur->developers[i]); |
| printf("======= \n"); |
| } |
|
|
| |
| |
| |
| typedef struct gjob { |
| int nbJobs; |
| jobPtr jobs[500]; |
| } gJob, *gJobPtr; |
|
|
|
|
| static gJobPtr |
| parseGjobFile(char *filename) { |
| xmlDocPtr doc; |
| gJobPtr ret; |
| jobPtr curjob; |
| xmlNsPtr ns; |
| xmlNodePtr cur; |
|
|
| |
| |
| |
| doc = xmlReadFile(filename, NULL, XML_PARSE_NOBLANKS); |
| if (doc == NULL) return(NULL); |
|
|
| |
| |
| |
| |
| cur = xmlDocGetRootElement(doc); |
| if (cur == NULL) { |
| fprintf(stderr,"empty document\n"); |
| xmlFreeDoc(doc); |
| return(NULL); |
| } |
| ns = xmlSearchNsByHref(doc, cur, |
| (const xmlChar *) "http://www.gnome.org/some-location"); |
| if (ns == NULL) { |
| fprintf(stderr, |
| "document of the wrong type, GJob Namespace not found\n"); |
| xmlFreeDoc(doc); |
| return(NULL); |
| } |
| if (xmlStrcmp(cur->name, (const xmlChar *) "Helping")) { |
| fprintf(stderr,"document of the wrong type, root node != Helping"); |
| xmlFreeDoc(doc); |
| return(NULL); |
| } |
|
|
| |
| |
| |
| ret = (gJobPtr) malloc(sizeof(gJob)); |
| if (ret == NULL) { |
| fprintf(stderr,"out of memory\n"); |
| xmlFreeDoc(doc); |
| return(NULL); |
| } |
| memset(ret, 0, sizeof(gJob)); |
|
|
| |
| |
| |
| |
| cur = cur->xmlChildrenNode; |
| while ( cur && xmlIsBlankNode ( cur ) ) { |
| cur = cur -> next; |
| } |
| if ( cur == 0 ) { |
| xmlFreeDoc(doc); |
| free(ret); |
| return ( NULL ); |
| } |
| if ((xmlStrcmp(cur->name, (const xmlChar *) "Jobs")) || (cur->ns != ns)) { |
| fprintf(stderr,"document of the wrong type, was '%s', Jobs expected", |
| cur->name); |
| fprintf(stderr,"xmlDocDump follows\n"); |
| #ifdef LIBXML_OUTPUT_ENABLED |
| xmlDocDump ( stderr, doc ); |
| fprintf(stderr,"xmlDocDump finished\n"); |
| #endif |
| xmlFreeDoc(doc); |
| free(ret); |
| return(NULL); |
| } |
|
|
| |
| cur = cur->xmlChildrenNode; |
| while (cur != NULL) { |
| if ((!xmlStrcmp(cur->name, (const xmlChar *) "Job")) && |
| (cur->ns == ns)) { |
| curjob = parseJob(doc, ns, cur); |
| if (curjob != NULL) |
| ret->jobs[ret->nbJobs++] = curjob; |
| if (ret->nbJobs >= 500) break; |
| } |
| cur = cur->next; |
| } |
|
|
| return(ret); |
| } |
|
|
| static void |
| handleGjob(gJobPtr cur) { |
| int i; |
|
|
| |
| |
| |
| printf("%d Jobs registered\n", cur->nbJobs); |
| for (i = 0; i < cur->nbJobs; i++) printJob(cur->jobs[i]); |
| } |
|
|
| int main(int argc, char **argv) { |
| int i; |
| gJobPtr cur; |
|
|
| |
| LIBXML_TEST_VERSION |
|
|
| for (i = 1; i < argc ; i++) { |
| cur = parseGjobFile(argv[i]); |
| if ( cur ) |
| handleGjob(cur); |
| else |
| fprintf( stderr, "Error parsing file '%s'\n", argv[i]); |
|
|
| } |
|
|
| |
| xmlCleanupParser(); |
|
|
| return(0); |
| } |
|
|