/home/dko/projects/mobilec/trunk/src/xml_parser.c

Go to the documentation of this file.
00001 /* SVN FILE INFO
00002  * $Revision: 174 $ : Last Committed Revision
00003  * $Date: 2008年06月24日 10:50:29 -0700 (2008年6月24日) $ : Last Committed Date */
00004 /*[
00005  * Copyright (c) 2007 Integration Engineering Laboratory
00006  University of California, Davis
00007  *
00008  * Permission to use, copy, and distribute this software and its
00009  * documentation for any purpose with or without fee is hereby granted,
00010  * provided that the above copyright notice appear in all copies and
00011  * that both that copyright notice and this permission notice appear
00012  * in supporting documentation.
00013  *
00014  * Permission to modify the software is granted, but not the right to
00015  * distribute the complete modified source code. Modifications are to
00016  * be distributed as patches to the released version. Permission to
00017  * distribute binaries produced by compiling modified sources is granted,
00018  * provided you
00019  * 1. distribute the corresponding source modifications from the
00020  * released version in the form of a patch file along with the binaries,
00021  * 2. add special version identification to distinguish your version
00022  * in addition to the base release version number,
00023  * 3. provide your name and address as the primary contact for the
00024  * support of your modified version, and
00025  * 4. retain our contact information in regard to use of the base
00026  * software.
00027  * Permission to distribute the released version of the source code along
00028  * with corresponding source modifications in the form of a patch file is
00029  * granted with same provisions 2 through 4 for binary distributions.
00030  *
00031  * This software is provided "as is" without express or implied warranty
00032  * to the extent permitted by applicable law.
00033 ]*/
00034 
00035 #include <mxml.h>
00036 #include <string.h>
00037 #include <stdlib.h>
00038 #define _XOPEN_SOURCE 600
00039 #include <stdlib.h>
00040 #ifndef _WIN32
00041 #include "config.h"
00042 #else
00043 #include "winconfig.h"
00044 #endif
00045 #include "include/interpreter_variable_data.h"
00046 #include "include/message.h"
00047 #include "include/xml_parser.h"
00048 #include "include/xml_helper.h"
00049 
00050 /* *** */
00051 /* agent_xml_parse */
00052 error_code_t agent_xml_parse(agent_p agent)
00053 {
00054 xml_parser_t xml_parser;
00055 xml_parser.root = agent->datastate->xml_agent_root;
00056 xml_parser.node = xml_parser.root;
00057 agent_xml_parse__mobile_agent(agent, &xml_parser);
00058 return MC_SUCCESS;
00059 }
00060 
00061 /* *** */
00062 /* agent_xml_parse__gaf_message */
00063 error_code_t 
00064 agent_xml_parse__mobile_agent
00065 (
00066 agent_p agent, 
00067 xml_parser_p xml_parser
00068 )
00069 {
00070 /* make sure this is the 'MOBILE_AGENT' tag */
00071 if ( 
00072 strcmp(
00073 (const char*)xml_get_element_name(xml_parser->node),
00074 "MOBILE_AGENT"
00075 )
00076 )
00077 {
00078 return MC_ERR_PARSE;
00079 }
00080 /* There is only one child node: AGENT_DATA*/
00081 xml_parser->node = (const mxml_node_t*)xml_get_child(
00082 xml_parser->node,
00083 "AGENT_DATA",
00084 1);
00085 
00086 return agent_xml_parse__agent_data(agent, xml_parser);
00087 }
00088 
00089 /* *** */
00090 /* agent_xml_parse__message */
00091 error_code_t
00092 agent_xml_parse__agent_data
00093 (
00094 agent_p agent,
00095 xml_parser_p xml_parser
00096 )
00097 {
00098 const mxml_node_t* agent_data_node;
00099 error_code_t err_code;
00100 
00101 if (xml_parser->node == NULL) {
00102 return MC_ERR_PARSE;
00103 }
00104 
00105 agent_data_node = xml_parser->node;
00106 
00107 xml_parser->node = xml_get_child(
00108 agent_data_node,
00109 "NAME", 
00110 1
00111 );
00112 if ( (err_code = agent_xml_parse__name(agent, xml_parser)) ) {
00113 return err_code;
00114 }
00115 
00116 xml_parser->node = xml_get_child(
00117 agent_data_node,
00118 "OWNER",
00119 1
00120 );
00121 if ( (err_code = agent_xml_parse__owner(agent, xml_parser)) ) {
00122 return err_code;
00123 }
00124 
00125 xml_parser->node = xml_get_child(
00126 agent_data_node,
00127 "HOME",
00128 1
00129 );
00130 if ( (err_code = agent_xml_parse__home(agent, xml_parser)) ) {
00131 return err_code;
00132 }
00133 
00134 xml_parser->node = xml_get_child(
00135 agent_data_node,
00136 "TASKS",
00137 1
00138 );
00139 if ( (err_code = agent_xml_parse__tasks(agent, xml_parser)) ) {
00140 return err_code;
00141 }
00142 return MC_SUCCESS;
00143 }
00144 
00145 /* *** */
00146 /* agent_xml_parse__name */
00147 error_code_t
00148 agent_xml_parse__name(agent_p agent, xml_parser_p xml_parser)
00149 {
00150 char* text;
00151 const mxml_node_t* name_node;
00152 if (xml_parser->node == NULL) {
00153 return MC_ERR_PARSE;
00154 }
00155 name_node = xml_parser->node;
00156 
00157 text = xml_get_text( name_node );
00158 CHECK_NULL(text, return MC_ERR_PARSE;);
00159 
00160 agent->name = (char*)malloc(
00161 sizeof(char)*(strlen(text)+1)
00162 );
00163 strcpy(
00164 agent->name,
00165 text
00166 );
00167 free(text);
00168 return MC_SUCCESS;
00169 }
00170 
00171 /* *** */
00172 /* agent_xml_parse__owner */
00173 error_code_t
00174 agent_xml_parse__owner(agent_p agent, xml_parser_p xml_parser)
00175 {
00176 char *text;
00177 const mxml_node_t* owner_node;
00178 if (xml_parser->node == NULL) {
00179 /* It's ok if there is no owner node: It is not a required field. */
00180 agent->owner = NULL;
00181 return MC_SUCCESS;
00182 }
00183 owner_node = xml_parser->node;
00184 
00185 text = xml_get_text( owner_node );
00186 CHECK_NULL(text, agent->owner=NULL;return MC_SUCCESS;);
00187 agent->owner = (char*)malloc(
00188 sizeof(char)*(strlen(text)+1)
00189 );
00190 strcpy(
00191 agent->owner,
00192 text
00193 );
00194 free(text);
00195 return MC_SUCCESS;
00196 }
00197 
00198 /* *** */
00199 /* agent_xml_parse__home */
00200 error_code_t
00201 agent_xml_parse__home(agent_p agent, xml_parser_p xml_parser)
00202 {
00203 char *text;
00204 const mxml_node_t* home_node;
00205 if (xml_parser->node == NULL) {
00206 /* It's ok if there is no home node: It is not a required field. */
00207 agent->home= NULL;
00208 return MC_SUCCESS;
00209 }
00210 home_node = xml_parser->node;
00211 text = xml_get_text( home_node );
00212 CHECK_NULL(text, agent->home=NULL;return MC_SUCCESS;);
00213 agent->home = (char*)malloc(
00214 sizeof(char)*(strlen(text)+1)
00215 );
00216 strcpy(
00217 agent->home,
00218 text
00219 );
00220 free(text);
00221 return MC_SUCCESS;
00222 }
00223 
00224 /* *** */
00225 /* agent_xml_parse__tasks */
00226 error_code_t
00227 agent_xml_parse__tasks(agent_p agent, xml_parser_p xml_parser)
00228 {
00229 int i;
00230 int code_num=0;
00231 int err_code;
00232 const char* attribute;
00233 const mxml_node_t* tasks_node;
00234 char buf[20];
00235 
00236 tasks_node = xml_parser->node;
00237 
00238 /* parse the 'task' attribute */
00239 attribute = mxmlElementGetAttr(
00240 (mxml_node_t*)tasks_node,
00241 "task"
00242 );
00243 if (attribute == NULL) {
00244 agent->datastate->number_of_tasks = 1;
00245 } else {
00246 agent->datastate->number_of_tasks = atoi((char*)attribute);
00247 }
00248 agent->datastate->tasks = (agent_task_p*)malloc(
00249 sizeof(agent_task_p) * agent->datastate->number_of_tasks
00250 );
00251 
00252 /* parse the 'num' attribute */
00253 attribute = mxmlElementGetAttr(
00254 (mxml_node_t*)tasks_node,
00255 "num"
00256 );
00257 if (attribute == NULL) {
00258 agent->datastate->task_progress = 0;
00259 } else {
00260 agent->datastate->task_progress = atoi((char*)attribute);
00261 }
00262 
00263 /* Allocate each task */
00264 for(i = 0; i<agent->datastate->number_of_tasks; i++) {
00265 agent->datastate->tasks[i] = agent_task_New();
00266 }
00267 
00268 /* Parse each task */
00269 for(i = 0; i < agent->datastate->number_of_tasks; i++) {
00270 sprintf(buf, "%d", i);
00271 xml_parser->node = mxmlFindElement(
00272 tasks_node,
00273 tasks_node,
00274 "TASK",
00275 "num",
00276 buf,
00277 MXML_DESCEND_FIRST );
00278 if(xml_parser->node == NULL) {
00279 fprintf(stderr,
00280 "ERROR: Could not find task num %d! %s:%d\n",
00281 i, __FILE__, __LINE__);
00282 return MC_ERR_PARSE;
00283 }
00284 agent_xml_parse__task(agent, xml_parser, i);
00285 }
00286 
00287 /* Need to get all of the agent codes. Even though we may execute only
00288  * one right now, in the future, the agent may decide at runtime which block
00289  * to execute, so we need them all. */
00290 xml_parser->node = mxmlFindElement
00291 (
00292 tasks_node,
00293 tasks_node,
00294 "AGENT_CODE",
00295 NULL,
00296 NULL,
00297 MXML_DESCEND
00298 );
00299 
00300 
00301 /* First we count the number of code blocks */
00302 while(xml_parser->node != NULL) {
00303 code_num++;
00304 xml_parser->node = mxmlFindElement
00305 (
00306 xml_parser->node,
00307 tasks_node,
00308 "AGENT_CODE",
00309 NULL,
00310 NULL,
00311 MXML_NO_DESCEND
00312 );
00313 }
00314 
00315 /* Allocate correct amount of memory for code blocks. */
00316 agent->datastate->agent_code_ids = (char**)malloc
00317 (
00318 (code_num+1) * sizeof(char*)
00319 );
00320 agent->datastate->agent_codes = (char**)malloc
00321 (
00322 (code_num+1) * sizeof(char*)
00323 );
00324 /* Set sigil */
00325 agent->datastate->agent_code_ids[code_num] = NULL;
00326 agent->datastate->agent_codes[code_num] = NULL;
00327 
00328 /* Go through all code again and parse */
00329 xml_parser->node = mxmlFindElement
00330 (
00331 tasks_node,
00332 tasks_node,
00333 "AGENT_CODE",
00334 NULL,
00335 NULL,
00336 MXML_DESCEND
00337 );
00338 i = 0;
00339 while(xml_parser->node != NULL) {
00340 err_code = agent_xml_parse__agent_code(agent, i, xml_parser);
00341 i++;
00342 xml_parser->node = mxmlFindElement
00343 (
00344 xml_parser->node,
00345 tasks_node,
00346 "AGENT_CODE",
00347 NULL,
00348 NULL,
00349 MXML_NO_DESCEND
00350 );
00351 }
00352 
00353 if (agent->datastate->agent_code == NULL) {
00354 /* Something is wrong. */
00355 fprintf(stderr, "Parse error: Agent code not found. %s:%d\n", __FILE__, __LINE__);
00356 return MC_ERR_PARSE;
00357 }
00358 
00359 return 0;
00360 }
00361 
00362 /* *** */
00363 /* agent_xml_parse__task */
00364 error_code_t
00365 agent_xml_parse__task(agent_p agent, xml_parser_p xml_parser, int index)
00366 {
00367 const char* attribute;
00368 const mxml_node_t* task_node;
00369 error_code_t err_code = MC_SUCCESS;
00370 CHECK_NULL(xml_parser->node, return MC_ERR_PARSE;);
00371 task_node = xml_parser->node;
00372 
00373 /* Parse the multiple DATA nodes */
00374 xml_parser->node = mxmlFindElement(
00375 task_node,
00376 task_node,
00377 "DATA",
00378 NULL,
00379 NULL,
00380 MXML_DESCEND_FIRST);
00381 while(xml_parser->node != NULL) {
00382 /* There may be no DATA nodes */
00383 if ((err_code = agent_xml_parse__data(agent, xml_parser, index)))
00384 {
00385 return err_code;
00386 }
00387 xml_parser->node = mxmlFindElement(
00388 xml_parser->node,
00389 task_node,
00390 "DATA",
00391 NULL,
00392 NULL,
00393 MXML_NO_DESCEND );
00394 }
00395 
00396 /* 'code_id' */
00397 attribute = mxmlElementGetAttr(
00398 (mxml_node_t*)task_node,
00399 "code_id");
00400 if (attribute != NULL) {
00401 agent->datastate->tasks[index]->code_id = malloc
00402 (
00403 sizeof(char) * 
00404 (strlen(attribute) + 1)
00405 );
00406 strcpy(agent->datastate->tasks[index]->code_id, attribute);
00407 } else {
00408 agent->datastate->tasks[index]->code_id = NULL;
00409 }
00410 
00411 /* 'num' - The number of this task (indexed from 0) */
00412 attribute = mxmlElementGetAttr(
00413 (mxml_node_t*)task_node,
00414 "num"
00415 );
00416 CHECK_NULL(attribute, return MC_ERR_PARSE;);
00417 
00418 /* 'complete' - Is this task complete?*/
00419 attribute = mxmlElementGetAttr(
00420 (mxml_node_t*)task_node,
00421 "complete"
00422 );
00423 CHECK_NULL(attribute, return MC_ERR_PARSE;);
00424 agent->datastate->tasks[index]->task_completed = 
00425 atoi((char*)attribute);
00426 
00427 /* 'server' - The server this task should be performed on */
00428 attribute = mxmlElementGetAttr(
00429 (mxml_node_t*)task_node,
00430 "server"
00431 );
00432 CHECK_NULL(attribute, return MC_ERR_PARSE;);
00433 agent->datastate->tasks[index]->server_name = 
00434 (char*)malloc(sizeof(char) * (strlen(attribute)+1) );
00435 strcpy(
00436 agent->datastate->tasks[index]->server_name,
00437 attribute
00438 );
00439 
00440 /* 'return' - The name of the return variable, if there is one */
00441 attribute = mxmlElementGetAttr(
00442 (mxml_node_t*)task_node,
00443 "return" );
00444 if (attribute == NULL) {
00445 agent->datastate->tasks[index]->var_name = strdup("no-return");
00446 } else {
00447 agent->datastate->tasks[index]->var_name = strdup(attribute);
00448 }
00449 CHECK_NULL(agent->datastate->tasks[index]->var_name, exit(1););
00450 
00451 return err_code;
00452 }
00453 
00454 /* *** */
00455 /* agent_xml_parse__data */
00456 error_code_t
00457 agent_xml_parse__data(agent_p agent, xml_parser_p xml_parser, int index)
00458 {
00459 const char* attribute;
00460 const char* attribute2;
00461 const mxml_node_t *data_node;
00462 int data_type_size;
00463 interpreter_variable_data_t* interp_variable;
00464 if (xml_parser->node == NULL) {
00465 return MC_ERR_PARSE;
00466 }
00467 if (strcmp(
00468 "DATA",
00469 xml_get_element_name(xml_parser->node) )
00470 )
00471 {
00472 return MC_ERR_PARSE;
00473 }
00474 data_node = xml_parser->node;
00475 
00476 /* Check to see if this is the return variable */
00477 attribute = mxmlElementGetAttr(
00478 data_node->parent,
00479 "return" );
00480 attribute2 = mxmlElementGetAttr(
00481 data_node,
00482 "name" );
00483 if (attribute != NULL && !strcmp(attribute, attribute2)) {
00484 /* This variable is the return variable. */
00485 
00486 /* Allocate Return data structure */
00487 /* FIXME: This may not be the right place to do this,
00488  * but it is safe and leak free. */
00489 agent->datastate->tasks[index]->agent_return_data = 
00490 interpreter_variable_data_New(); 
00491 interp_variable = agent->datastate->tasks[index]->agent_return_data;
00492 } else {
00493 interp_variable = interpreter_variable_data_New();
00494 agent_variable_list_Add(
00495 agent->datastate->tasks[index]->agent_variable_list,
00496 interp_variable );
00497 }
00498 
00499 
00500 /* Attributes we need to parse:
00501  * O dim - dimension of the return data
00502  * O name - name of the return variable
00503  * O persistent - is the agent persistent for this task?
00504  * O type - return variable type
00505  * O return_value - return value, if not an array
00506  * O code_id - ID of the code block to execute. If this
00507  * attribute is missing, execute the first 
00508  * code block available.
00509  *
00510  * 'O' denotes optional attribute. */
00511 
00512 
00513 /* 'dim' */
00514 attribute = mxmlElementGetAttr(
00515 (mxml_node_t*)xml_parser->node,
00516 "dim"
00517 );
00518 if (attribute != NULL) {
00519 interp_variable->array_dim = 
00520 atoi((char*)attribute);
00521 } else {
00522 interp_variable->array_dim = 
00523 0;
00524 }
00525 
00526 /* 'name' */
00527 attribute = mxmlElementGetAttr(
00528 (mxml_node_t*)xml_parser->node,
00529 "name"
00530 );
00531 if (attribute != NULL) {
00532 interp_variable->name = 
00533 (char*)malloc(sizeof(char)*(strlen(attribute)+1));
00534 strcpy(
00535 interp_variable->name,
00536 attribute
00537 );
00538 }
00539 
00540 /* 'persistent' */
00541 attribute = mxmlElementGetAttr(
00542 (mxml_node_t*)data_node,
00543 "persistent"
00544 );
00545 if (attribute != NULL) {
00546 agent->datastate->tasks[index]->persistent = 
00547 atoi((char*)attribute);
00548 } else {
00549 agent->datastate->tasks[index]->persistent = 0;
00550 }
00551 
00552 /* 'type' */
00553 attribute = mxmlElementGetAttr(
00554 (mxml_node_t*)data_node,
00555 "type"
00556 );
00557 if (attribute != NULL) {
00558 CH_STRING_DATATYPE(
00559 attribute,
00560 interp_variable->data_type
00561 );
00562 CH_DATATYPE_SIZE(
00563 interp_variable->data_type,
00564 data_type_size
00565 );
00566 } else {
00567 interp_variable->data_type = 
00568 CH_UNDEFINETYPE;
00569 data_type_size = 0;
00570 }
00571 
00572 if (interp_variable->array_dim == 0) {
00573 /* 'return_value' */
00574 attribute = mxmlElementGetAttr(
00575 (mxml_node_t*)data_node,
00576 "value" );
00577 if (attribute != NULL && data_type_size != 0) {
00578 interp_variable->data =
00579 malloc(data_type_size);
00580 CH_DATATYPE_STR_TO_VAL(
00581 interp_variable->data_type,
00582 attribute,
00583 interp_variable->data
00584 );
00585 }
00586 } else {
00587 /* The only possible child node to parse are row nodes. */
00588 xml_parser->node = xml_get_child(
00589 xml_parser->node,
00590 "ROW",
00591 1
00592 );
00593 agent_xml_parse__row(interp_variable, xml_parser, index);
00594 }
00595 xml_parser->node = data_node;
00596 return MC_SUCCESS;
00597 }
00598 
00599 /* *** */
00600 /* agent_xml_parse__row */
00601 error_code_t
00602 agent_xml_parse__row(interpreter_variable_data_t* interp_variable, xml_parser_p xml_parser, int index)
00603 {
00604 int j;
00605 int data_type_size;
00606 int tmp;
00607 int num_elements;
00608 const mxml_node_t* row_node;
00609 
00610 if (xml_parser->node == NULL) {
00611 return MC_SUCCESS;
00612 }
00613 
00614 if (strcmp(
00615 xml_get_element_name(xml_parser->node),
00616 "ROW" )
00617 )
00618 {
00619 return MC_SUCCESS;
00620 }
00621 row_node = xml_parser->node;
00622 
00623 /* malloc mem for the task data elements */
00624 /* First, find the extents of the dimensions */
00625 if (interp_variable->array_dim != 0) {
00626 interp_variable->array_extent = (int*)
00627 malloc
00628 (
00629 sizeof(int) * 
00630 interp_variable->array_dim
00631 );
00632 tmp = 0;
00633 agent_xml_parse__fill_row_data(NULL,
00634 interp_variable->data_type,
00635 interp_variable->array_extent,
00636 row_node,
00637 &tmp);
00638 num_elements = 1;
00639 for (j = 0; j<interp_variable->array_dim; j++) {
00640 num_elements *= interp_variable->array_extent[j];
00641 }
00642 
00643 /* Allocate space for the return data */
00644 CH_DATATYPE_SIZE
00645 (
00646 interp_variable->data_type,
00647 data_type_size
00648 );
00649 interp_variable->data =
00650 malloc(num_elements * data_type_size);
00651 
00652 /* Get the data */
00653 tmp = 0;
00654 agent_xml_parse__fill_row_data(
00655 interp_variable->data,
00656 interp_variable->data_type,
00657 interp_variable->array_extent,
00658 row_node,
00659 &tmp );
00660 } else { 
00661 return MC_SUCCESS;
00662 }
00663 return MC_SUCCESS;
00664 }
00665 
00666 void agent_xml_parse__fill_row_data(
00667 void *data,
00668 ChType_t type,
00669 int *extent,
00670 const mxml_node_t* node,
00671 int *index) 
00672 {
00673 mxml_node_t* tmp_node;
00674 int i=0;
00675 char *buf;
00676 char *tmp;
00677 #ifndef _WIN32
00678 char *saveptr;
00679 #endif
00680 int datasize;
00681 /* Check to see if the child is an element or text. All children must be
00682  * either an element or text. If it is text, that means we are at the very bottom 
00683  * and we need to retrive data. */
00684 (*extent) = 0; 
00685 if (node->child->type == MXML_TEXT) {
00686 node = node->child;
00687 /* Now we parse the data */
00688 CH_DATATYPE_SIZE(type, datasize);
00689 buf = (char*)malloc(
00690 sizeof(char) +
00691 (strlen(node->value.text.string) + 1));
00692 strcpy(buf, node->value.text.string);
00693 /* Tokenize by commas */
00694 #ifndef _WIN32
00695 tmp = strtok_r(buf, ",", &saveptr);
00696 #else
00697 tmp = strtok(buf, ",");
00698 #endif
00699 while ( tmp != NULL) {
00700 switch(type) {
00701 case CH_CHARTYPE:
00702 if (data != NULL)
00703 ((char*)data)[*index] = *(char*)tmp;
00704 (*index)++;
00705 break;
00706 case CH_INTTYPE:
00707 if (data != NULL)
00708 ((int*)data)[*index] = strtol(tmp, NULL, 0);
00709 (*index)++;
00710 break;
00711 case CH_UINTTYPE:
00712 if (data != NULL)
00713 ((unsigned int*)data)[*index] = strtoul(tmp, NULL, 0);
00714 (*index)++;
00715 break;
00716 case CH_SHORTTYPE:
00717 if (data != NULL)
00718 ((short*)data)[*index] = (short)strtol(tmp, NULL, 0);
00719 (*index)++;
00720 break;
00721 case CH_USHORTTYPE:
00722 if (data != NULL)
00723 ((unsigned short*)data)[*index] = (unsigned short)strtol(tmp, NULL, 0);
00724 (*index)++;
00725 break;
00726 case CH_FLOATTYPE:
00727 if (data != NULL)
00728 #ifndef _WIN32
00729 ((float*)data)[*index] = strtof(tmp, NULL);
00730 #else 
00731 ((float*)data)[*index] = (float)strtod(tmp, NULL);
00732 #endif
00733 (*index)++;
00734 break;
00735 case CH_DOUBLETYPE:
00736 if (data != NULL)
00737 ((double*)data)[*index] = strtod(tmp, NULL);
00738 (*index)++;
00739 break;
00740 default:
00741 fprintf(stderr, 
00742 "Unsupported data type: %d %s:%d\n",
00743 type, __FILE__, __LINE__);
00744 }
00745 #ifndef _WIN32
00746 tmp = strtok_r(NULL, ",", &saveptr);
00747 #else
00748 tmp = strtok(NULL, ",");
00749 #endif
00750 (*extent)++;
00751 }
00752 free(buf);
00753 } else if (node->type == MXML_ELEMENT) {
00754 buf = (char*)malloc(sizeof(char)*10);
00755 buf[0] = '0円';
00756 sprintf(buf, "%d", i);
00757 tmp_node = mxmlFindElement(
00758 (mxml_node_t*)node,
00759 (mxml_node_t*)node,
00760 "ROW",
00761 "index",
00762 buf,
00763 MXML_DESCEND_FIRST);
00764 while (tmp_node != NULL) {
00765 (*extent)++;
00766 agent_xml_parse__fill_row_data(data, type,(extent+1), tmp_node, index);
00767 i++;
00768 buf[0] = '0円';
00769 sprintf(buf, "%d", i);
00770 tmp_node = mxmlFindElement(
00771 (mxml_node_t*)node,
00772 (mxml_node_t*)node,
00773 "ROW",
00774 "index",
00775 buf,
00776 MXML_DESCEND_FIRST);
00777 }
00778 free(buf);
00779 }
00780 }
00781 
00782 /* *** */
00783 /* agent_xml_parse__agent_code */
00784 error_code_t
00785 agent_xml_parse__agent_code(agent_p agent, int index, xml_parser_p xml_parser)
00786 {
00787 char *attribute;
00788 int cur_task = agent->datastate->task_progress;
00789 if( cur_task == agent->datastate->number_of_tasks )
00790 cur_task--;
00791 agent->datastate->agent_codes[index] = 
00792 xml_get_text
00793 (
00794 xml_parser->node
00795 );
00796 
00797 /* Get the code id */
00798 attribute = mxmlElementGetAttr
00799 (
00800 (mxml_node_t*)xml_parser->node,
00801 "id"
00802 );
00803 if (attribute) {
00804 agent->datastate->agent_code_ids[index] = malloc
00805 (
00806 sizeof(char) * 
00807 (strlen(attribute) + 1)
00808 );
00809 strcpy(agent->datastate->agent_code_ids[index], attribute);
00810 } else {
00811 agent->datastate->agent_code_ids[index] = malloc(sizeof(char));
00812 *(agent->datastate->agent_code_ids[index]) = '0円';
00813 }
00814 if (agent->datastate->tasks[cur_task]->code_id && attribute != NULL) {
00815 if (!strcmp(attribute, agent->datastate->tasks[cur_task]->code_id)) {
00816 agent->datastate->agent_code = agent->datastate->agent_codes[index];
00817 }
00818 } else {
00819 agent->datastate->agent_code = agent->datastate->agent_codes[0];
00820 }
00821 return MC_SUCCESS;
00822 }
00823 
00824 /* agent return parsing ******************************************************/
00825 error_code_t
00826 agent_return_xml_parse(agent_p agent)
00827 {
00828 xml_parser_t xml_parser;
00829 xml_parser.root = agent->datastate->xml_root;
00830 xml_parser.node = (const mxml_node_t*)xml_get_child(
00831 xml_parser.root,
00832 "NAME",
00833 1);
00834 
00835 agent_xml_parse__name(agent, &xml_parser);
00836 
00837 xml_parser.node = (const mxml_node_t*)xml_get_child(
00838 xml_parser.root,
00839 "OWNER",
00840 1);
00841 
00842 agent_xml_parse__owner(agent, &xml_parser);
00843 
00844 xml_parser.node = (const mxml_node_t*)xml_get_child(
00845 xml_parser.root,
00846 "HOME",
00847 1);
00848 
00849 agent_xml_parse__home(agent, &xml_parser);
00850 
00851 xml_parser.node = (const mxml_node_t*)xml_get_child(
00852 xml_parser.root,
00853 "TASK",
00854 1);
00855 
00856 agent_xml_parse__tasks(agent, &xml_parser);
00857 return MC_SUCCESS;
00858 }
00859 /* message parsing **********************************************************/
00860 error_code_t
00861 message_xml_parse(message_p message)
00862 {
00863 int err_code;
00864 xml_parser_p xml_parser;
00865 xml_parser = (xml_parser_p)malloc(sizeof(xml_parser_t));
00866 xml_parser->root = message->xml_root;
00867 xml_parser->node = mxmlFindElement
00868 (
00869 (mxml_node_t*)xml_parser->root,
00870 (mxml_node_t*)xml_parser->root,
00871 "MOBILEC_MESSAGE",
00872 NULL,
00873 NULL,
00874 MXML_NO_DESCEND
00875 );
00876 if (xml_parser->node == NULL) {
00877 xml_parser->node = mxmlFindElement
00878 (
00879 (mxml_node_t*)xml_parser->root,
00880 (mxml_node_t*)xml_parser->root,
00881 "MOBILEC_MESSAGE",
00882 NULL,
00883 NULL,
00884 MXML_DESCEND
00885 );
00886 }
00887 if (xml_parser->node == NULL) {
00888 err_code = MC_ERR_PARSE;
00889 goto cleanup;
00890 }
00891 xml_parser->root = xml_parser->node;
00892 if(
00893 strcmp(
00894 (const char*)xml_get_element_name(xml_parser->node),
00895 "MOBILEC_MESSAGE"
00896 )
00897 )
00898 {
00899 fprintf(stderr, "Parse error. %s:%d\n", __FILE__, __LINE__);
00900 err_code = MC_ERR_PARSE;
00901 goto cleanup;
00902 }
00903 xml_parser->node = (const mxml_node_t*)xml_get_child
00904 (
00905 xml_parser->node,
00906 "MESSAGE",
00907 1
00908 );
00909 err_code = message_xml_parse__message(message, xml_parser);
00910 cleanup:
00911 free(xml_parser);
00912 return err_code;
00913 }
00914 
00915 error_code_t
00916 message_xml_parse__message(message_p message, xml_parser_p xml_parser)
00917 {
00918 const char* attribute;
00919 char* buf;
00920 char* hostname;
00921 char* port_str;
00922 #ifndef _WIN32
00923 char* save_ptr; /* Save ptr for re-entrant strtok */
00924 #endif
00925 int port;
00926 if (xml_parser->node == NULL) {
00927 return MC_ERR_PARSE;
00928 }
00929 attribute = mxmlElementGetAttr
00930 (
00931 (mxml_node_t*)xml_parser->node,
00932 "message"
00933 );
00934 if (!strcmp(attribute, "MOBILE_AGENT")) {
00935 message->message_type = MOBILE_AGENT;
00936 message->xml_payload = xml_get_child
00937 (
00938 xml_parser->node,
00939 "MOBILE_AGENT",
00940 1
00941 );
00942 } else if (!strcmp(attribute, "RETURN_MSG")) {
00943 message->message_type = RETURN_MSG;
00944 message->xml_payload = xml_get_child
00945 (
00946 xml_parser->node,
00947 "MOBILE_AGENT",
00948 1
00949 );
00950 } else if (!strcmp(attribute, "ACL")) {
00951 message->message_type = FIPA_ACL;
00952 } else if (!strcmp(attribute, "ENCRYPTION_INITIALIZE")) {
00953 message->message_type = ENCRYPTION_INITIALIZE;
00954 message->xml_payload = xml_get_child
00955 (
00956 xml_parser->node,
00957 "ENCRYPTION_DATA",
00958 1
00959 );
00960 } else if (!strcmp(attribute, "ENCRYPTED_DATA")) {
00961 message->message_type = ENCRYPTED_DATA;
00962 message->xml_payload = xml_get_child
00963 (
00964 xml_parser->node,
00965 "ENCRYPTED_DATA",
00966 1
00967 );
00968 } else if (!strcmp(attribute, "REQUEST_ENCRYPTION_INITIALIZE")) {
00969 message->message_type = REQUEST_ENCRYPTION_INITIALIZE;
00970 } else {
00971 fprintf(stderr, "Parse error. %s:%d\n", __FILE__, __LINE__);
00972 return MC_ERR_PARSE;
00973 }
00974 attribute = mxmlElementGetAttr
00975 (
00976 (mxml_node_t*)xml_parser->node,
00977 "from"
00978 );
00979 if(attribute != NULL) {
00980 /* Free 'from_address' first, if we need. */
00981 if(message->from_address) free(message->from_address);
00982 message->from_address = (char*)malloc
00983 (
00984 sizeof(char) * 
00985 (strlen(attribute)+1)
00986 );
00987 CHECK_NULL(message->from_address, exit(0););
00988 strcpy(message->from_address, attribute);
00989 buf = (char*)malloc
00990 (
00991 sizeof(char) * 
00992 (strlen(message->from_address)+1)
00993 );
00994 CHECK_NULL(buf, exit(0););
00995 strcpy(buf, message->from_address);
00996 hostname = strtok_r(buf, ":", &save_ptr);
00997 port_str = strtok_r(NULL, ":", &save_ptr);
00998 port = atoi(port_str);
00999 message->addr->sin_port = htons(port);
01000 free(buf);
01001 }
01002 return MC_SUCCESS;
01003 }
01004 
01005 

Generated on Tue Oct 28 17:03:23 2008 for Mobile-C by doxygen 1.5.5

AltStyle によって変換されたページ (->オリジナル) /