/* ************************************************** */
int do_configuration(void) {
    xmlSchemaValidCtxtPtr sv_ctxt = NULL;
    xmlSchemaParserCtxtPtr sp_ctxt = NULL;
    xmlSchemaPtr schema = NULL;
    xmlParserCtxtPtr p_ctxt = NULL;
    xmlDocPtr doc = NULL;
    xmlXPathContextPtr xp_ctx = NULL; 
    xmlXPathObjectPtr simul_xobj = NULL;
    xmlXPathObjectPtr entity_xobj = NULL; 
    xmlXPathObjectPtr environment_xobj = NULL; 
    xmlXPathObjectPtr bundle_xobj = NULL; 
    xmlXPathObjectPtr node_xobj = NULL; 
    xmlNodeSetPtr nodeset;
    xmlpathobj_t xpathobj[] = {{&simul_xobj, (xmlChar *) XML_X_SIMULATION}, 
                               {&entity_xobj, (xmlChar *) XML_X_ENTITY},
                               {&environment_xobj, (xmlChar *) XML_X_ENVIRONMENT},
                               {&bundle_xobj, (xmlChar *) XML_X_BUNDLE},
                               {&node_xobj, (xmlChar *) XML_X_NODE}};
    int ok = 0, i;

    /* Check XML version */
    LIBXML_TEST_VERSION;
        
    /* Initialise and parse schema */
    sp_ctxt = xmlSchemaNewParserCtxt(schemafile);
    if (sp_ctxt == NULL) {
        fprintf(stderr, "config: XML schema parser initialisation failure (do_configuration())\n");
        ok = -1;
        goto cleanup;
    }
    xmlSchemaSetParserErrors(sp_ctxt,
                             (xmlSchemaValidityErrorFunc)   xml_error,
                             (xmlSchemaValidityWarningFunc) xml_warning,
                             NULL);
    
    schema = xmlSchemaParse(sp_ctxt);
    if (schema == NULL) {
        fprintf(stderr, "config: error in schema %s (do_configuration())\n", schemafile);
        ok = -1;
        goto cleanup;
    }
    xmlSchemaSetValidErrors(sv_ctxt,
                            (xmlSchemaValidityErrorFunc)   xml_error,
                            (xmlSchemaValidityWarningFunc) xml_warning,
                            NULL);
    
    sv_ctxt = xmlSchemaNewValidCtxt(schema);
    if (sv_ctxt == NULL) {
        fprintf(stderr, "config: XML schema validator initialisation failure (do_configuration())\n");
        ok = -1;
        goto cleanup;
    }
    
    /* Initialise and parse document */
    p_ctxt = xmlNewParserCtxt();
    if (p_ctxt == NULL) {
        fprintf(stderr, "config: XML parser initialisation failure (do_configuration())\n");
        ok = -1;
        goto cleanup;
    }
    
    doc = xmlCtxtReadFile(p_ctxt, configfile, NULL, XML_PARSE_NONET | XML_PARSE_NOBLANKS | XML_PARSE_NSCLEAN);
    if (doc == NULL) {
        fprintf(stderr, "config: failed to parse %s (do_configuration())\n", configfile);
        ok = -1;
        goto cleanup;
    }

    /* Validate document */
    if (xmlSchemaValidateDoc(sv_ctxt, doc)) {
        fprintf(stderr, "config: error in configuration file %s (do_configuration())\n", configfile);
        ok = -1;
        goto cleanup;
    }
    
    /* Create xpath context */
    xp_ctx = xmlXPathNewContext(doc);
    if (xp_ctx == NULL) {
        fprintf(stderr, "config: XPath initialisation failure (do_configuration())\n");
        ok = -1;
        goto cleanup;
    }
    xmlXPathRegisterNs(xp_ctx, (xmlChar *) XML_NS_ID, (xmlChar *) XML_NS_URL); 
    
    
    /* Get xpath obj */
    for (i = 0 ; i < (int) (sizeof(xpathobj) / sizeof(xpathobj[0])); i++) {
        *xpathobj[i].ptr = xmlXPathEvalExpression(xpathobj[i].expr, xp_ctx);
        if (*xpathobj[i].ptr == NULL) {
            fprintf(stderr, "config: unable to evaluate xpath \"%s\" (do_configuration())\n", xpathobj[i].expr);
            ok = -1;
            goto cleanup;
            
        }
    }

    /***************/
    /* Counting... */
    /***************/
    nodeset = entity_xobj->nodesetval;
    if ((entities.size = (nodeset) ? nodeset->nodeNr : 0) == 0) {
        fprintf(stderr, "config: no entity defined (do_configuration())\n");
        ok = -1; 
        goto cleanup;
    }
    fprintf(stderr, "\nFound %d entities...\n", entities.size);
    nodeset = environment_xobj->nodesetval;
    if (((nodeset) ? nodeset->nodeNr : 0) == 0) {
        fprintf(stderr, "config: no environment defined (do_configuration())\n");
        ok = -1; 
        goto cleanup;
    }
    fprintf(stderr, "Found 1 environment...\n");
    nodeset = bundle_xobj->nodesetval;
    if ((bundles.size = (nodeset) ? nodeset->nodeNr : 0) == 0) {
        fprintf(stderr, "config: no bundle defined (do_configuration())\n");
        ok = -1; 
        goto cleanup;
    }
    fprintf(stderr, "Found %d bundles...\n", bundles.size);
    

    if ((dflt_params = das_create()) == NULL) {
        ok = -1; 
        goto cleanup;
    }

    /**************/
    /* Simulation */
    /**************/
    if (parse_simulation(simul_xobj->nodesetval)) {
        ok = -1;
        goto cleanup;
    }
                                                          
    /**********/
    /* Entity */
    /**********/
    /* initialize library paths */
    config_set_usr_modulesdir();
    user_path_list = g_strsplit(user_modulesdir, ":", 0); /* TOCLEAN */
    sys_path_list = g_strsplit(sys_modulesdir, ":", 0); /* TOCLEAN */

    /* parse */
    if (parse_entities(entity_xobj->nodesetval)) {
        ok = -1;
        goto cleanup;
    }

    /**************/
    /* Measure    */
    /**************/
    if (parse_measure()) {
        ok = -1;
        goto cleanup;
    }

    /***************/
    /* Environment */
    /***************/
    if (parse_environment(environment_xobj->nodesetval)) {
        ok = -1;
        goto cleanup;
    }
    
    /***************/
    /* Bundle      */
    /***************/
    if (parse_bundles(bundle_xobj->nodesetval)) {
        ok = -1;
        goto cleanup;
    }

    /***************/
    /* Nodes      */
    /***************/
    if (parse_nodes(node_xobj->nodesetval)) {
        ok = -1;
        goto cleanup;
    }
 
    /* edit by Quentin Lampin <*****@*****.**> */
    gchar **path = NULL;
    for (path = user_path_list ; *path ; path++) {
        g_free(*path);
    }
    path = NULL;
    for (path = sys_path_list  ; *path ; path++) {
        g_free(*path);
    }
    /* end of edition */

 cleanup:
    clean_params();
    
    for (i = 0 ; i < (int) (sizeof(xpathobj) / sizeof(xpathobj[0])); i++) {
        xmlXPathFreeObject(*xpathobj[i].ptr);
    }

    if (xp_ctx) {
        xmlXPathFreeContext(xp_ctx);
    }

    if (sp_ctxt) {
        xmlSchemaFreeParserCtxt(sp_ctxt);		
    }

    if (schema) {
        xmlSchemaFree(schema);
    }

    if (sv_ctxt) {
        xmlSchemaFreeValidCtxt(sv_ctxt);
    }

    if (doc) {
        xmlFreeDoc(doc);
    }

    if (p_ctxt) {
        xmlFreeParserCtxt(p_ctxt);
    }

    xmlCleanupParser();
    return ok;
}
Example #2
0
int parse_status(xmlDocPtr *doc, xmlNode *node, status **stptr){
    if(!node || !doc || !stptr)
        return -1;
    xmlNode *attr = node->children;

    status *st = *stptr;
    while(attr){ // parse status attributes
        if(!xmlStrcmp(attr->name, (const xmlChar *)"id")){ //status id
            char *id = xmlNodeListGetString(*doc, attr->xmlChildrenNode, 1);
            if(id){
                // look up status by id
                /*
                pthread_mutex_lock(&status_map_mutex);
                st = g_hash_table_lookup(status_map,id);
                pthread_mutex_unlock(&status_map_mutex);
                */
                if(!st){
                    // create new status and insert into the map
                    st = newstatus();
                    st->id = strdup(id);
                    /*
                    pthread_mutex_lock(&status_map_mutex);
                    g_hash_table_insert(status_map,st->id,st);
                    pthread_mutex_unlock(&status_map_mutex);
                    */
                }
                else
                    break;
            }
            else
                break;
        }
        else if(!xmlStrcmp(attr->name, (const xmlChar *)"text")){ //status text
            char *text = xmlNodeListGetString(*doc, attr->xmlChildrenNode, 1);
            if(text){
                st->wtext = malloc((TWEET_MAX_LEN+1)*sizeof(wchar_t));
                memset(st->wtext,'\0',(TWEET_MAX_LEN+1)*sizeof(wchar_t));
                st->length = mbstowcs(st->wtext,text,TWEET_MAX_LEN);
            }
        }
        else if(!xmlStrcmp(attr->name, (const xmlChar *)"favorited")){ 
            char *favorited = xmlNodeListGetString(*doc, attr->xmlChildrenNode, 1);
            if(favorited && strcmp(favorited,"true") == 0)
                SET_FAVORITED(st->extra_info);
        }
        else if(!xmlStrcmp(attr->name, (const xmlChar *)"user")){ //user
            parse_user(doc,attr,&(st->composer));
        }
        /*
        else if(!xmlStrcmp(attr->name, (const xmlChar *)"in_reply_to_status_id")){ 
            char *in_reply_to_status_id = xmlNodeListGetString(*doc, attr->xmlChildrenNode, 1);
            if(in_reply_to_status_id && strlen(in_reply_to_status_id) > 0){
                st->in_reply_to_status_id = strdup(in_reply_to_status_id);
            }
        }
        */
        else if(!xmlStrcmp(attr->name,(const xmlChar *)"retweeted_status")){
            parse_status(doc,attr,&(st->retweeted_status));
            /*
            for(entity *et=st->retweeted_status->entities;et;et=et->next)
                printf("%ls\n",et->text);
                */
        }
        else if(!xmlStrcmp(attr->name,(const xmlChar *)"entities")){
            parse_entities(doc,attr,st);
        }
        attr = attr->next;
    }
    split_status_entities(st);
    *stptr = st;
    return 0;
}