jasperserver/unmarshaller.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2007 JasperSoft http://www.jaspersoft.com
00003  * 
00004  * This program is free software; you can redistribute it and/or modify
00005  * it under the terms of the GNU General Public License as published by
00006  * the Free Software Foundation; either version 2 of the License, or
00007  * (at your option) any later version.
00008  * 
00009  * This program is distributed WITHOUT ANY WARRANTY; and without the 
00010  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00011  * See the GNU General Public License for more details.
00012  * 
00013  * You should have received a copy of the GNU General Public License
00014  * along with this program; if not, see http://www.gnu.org/licenses/gpl.txt 
00015  * or write to:
00016  * 
00017  * Free Software Foundation, Inc.,
00018  * 59 Temple Place - Suite 330,
00019  * Boston, MA  USA  02111-1307
00020  */
00021 
00022 #include <stdio.h>
00023 #include <libxml/xmlreader.h>
00024 
00025 
00026 #include "unmarshaller.h"
00027 
00028 
00029 int jasperserver_readNodeText(xmlNode *node, jasperserver_string_t *dest);
00030 jasperserver_resource_descriptor_t *jasperserver_read_resource_descriptor(xmlNode *node);
00031 jasperserver_resource_property_t *jasperserver_read_resource_property(xmlNode *node);
00032 jasperserver_parameter_t *jasperserver_read_parameter(xmlNode *node);
00033 
00034 jasperserver_operation_result_t *jasperserver_response_unmarshal(jasperserver_string_t *xml)
00035 {
00036      xmlDoc *doc = NULL;
00037      xmlNode *operationResultNode = NULL;
00038      xmlNode *child_node = NULL;
00039 
00040      jasperserver_operation_result_t *operationResult = NULL;
00041      
00042      operationResult = jasperserver_operation_result_new();
00043      operationResult->returnCode = -1;
00044      
00045      /*
00046      * The document being in memory, it have no base per RFC 2396,
00047      * and the "noname.xml" argument will serve as its base.
00048      */
00049      doc = xmlReadMemory(JS_UTFSTR(xml), xml->buffer->use,"noname.xml", "UTF-8", 0);
00050      if (doc == NULL) {
00051          jasperserver_string_cset(operationResult->returnMessage, "Unable to process the operation result");
00052          return operationResult;    
00053      }
00054      
00055      /*Get the root element node */
00056      operationResultNode = xmlDocGetRootElement(doc);
00057      
00058      // jasperserver_string_set is NULL safe...
00059      jasperserver_string_set(operationResult->version, xmlGetProp(operationResultNode, BAD_CAST "version" ) );
00060 
00061      for (child_node = operationResultNode->children; child_node; child_node = child_node->next) {
00062         if (child_node->type == XML_ELEMENT_NODE) {
00063         
00064             //printf("node type: Element, name: %s\n", child_node->name);
00065             if (!xmlStrcmp(child_node->name, BAD_CAST "returnCode"))
00066             {
00067                   jasperserver_string_t *fieldValue = NULL;
00068                   fieldValue = jasperserver_string_new();
00069                   if (jasperserver_readNodeText(child_node, fieldValue))
00070                   {
00071                      operationResult->returnCode = atoi( JS_CSTR(fieldValue) );
00072                   }
00073                   jasperserver_string_free(fieldValue);                         
00074             }
00075             else if (!xmlStrcmp(child_node->name, BAD_CAST "message"))
00076             {
00077                   jasperserver_readNodeText(child_node, operationResult->returnMessage);
00078             }
00079             else if (!xmlStrcmp(child_node->name, BAD_CAST "resourceDescriptor"))
00080             {
00081                   jasperserver_resource_descriptor_t *res = NULL;
00082                   res = jasperserver_read_resource_descriptor(child_node);
00083                   if (res)
00084                   {
00085                       jasperserver_list_append((jasperserver_list_t **)(&operationResult->resources), (jasperserver_list_t *)res);    
00086                   }
00087             }
00088         }
00089      }
00090      
00091      xmlFreeDoc(doc);
00092      xmlCleanupParser();
00093      return operationResult;
00094 }
00095 
00104 int jasperserver_readNodeText(xmlNode *node, jasperserver_string_t *dest)
00105 {
00106      xmlNode *child_node;
00107      child_node = node->children;
00108      
00109      if ( child_node && child_node->content)
00110      {
00111           jasperserver_string_set(dest, child_node->content);
00112           return 1;
00113      }
00114      return 0;     
00115 }
00116 
00121 jasperserver_resource_descriptor_t *jasperserver_read_resource_descriptor(xmlNode *resNode)
00122 {
00123     jasperserver_resource_descriptor_t *res = NULL;
00124     res = jasperserver_resource_descriptor_new();
00125     xmlNode *child_node = NULL;
00126     
00127     // jasperserver_string_set is NULL safe...
00128     if (xmlHasProp(resNode, BAD_CAST "name" )) { jasperserver_string_set(res->name, xmlGetProp(resNode, BAD_CAST "name" ) ); }
00129     if (xmlHasProp(resNode, BAD_CAST "wsType" )) { jasperserver_string_set(res->wsType, xmlGetProp(resNode, BAD_CAST "wsType" ) ); }
00130     if (xmlHasProp(resNode, BAD_CAST "uriString" )) { jasperserver_string_set(res->uriString, xmlGetProp(resNode, BAD_CAST "uriString" ) ); }
00131     if (xmlHasProp(resNode, BAD_CAST "isNew" )) {
00132        res->isNew = (!xmlStrcmp(xmlGetProp(resNode, BAD_CAST "isNew" ), "true")) ? 1 : 0; 
00133     }
00134 
00135 
00136     for (child_node = resNode->children; child_node; child_node = child_node->next) {
00137         if (child_node->type == XML_ELEMENT_NODE) {
00138         
00139             //printf("node type: Element, name: %s\n", child_node->name);
00140             if (!xmlStrcmp(child_node->name, BAD_CAST "label"))
00141             {
00142                   jasperserver_readNodeText(child_node, res->label);
00143             }
00144             else if (!xmlStrcmp(child_node->name, BAD_CAST "description"))
00145             {
00146                   jasperserver_readNodeText(child_node, res->description);
00147             }
00148             else if (!xmlStrcmp(child_node->name, BAD_CAST "resourceProperty"))
00149             {
00150                   jasperserver_resource_property_t *resProperty = NULL;
00151                   resProperty = jasperserver_read_resource_property(child_node);
00152                   if (res)
00153                   {
00154                       jasperserver_list_append((jasperserver_list_t **)(&res->properties), (jasperserver_list_t *)resProperty);    
00155                   }
00156             }
00157             else if (!xmlStrcmp(child_node->name, BAD_CAST "resourceDescriptor"))
00158             {
00159                   jasperserver_resource_descriptor_t *resDescriptor = NULL;
00160                   resDescriptor = jasperserver_read_resource_descriptor(child_node);
00161                   if (resDescriptor)
00162                   {
00163                       jasperserver_list_append((jasperserver_list_t **)(&res->children), (jasperserver_list_t *)resDescriptor);    
00164                   }
00165             }
00166             else if (!xmlStrcmp(child_node->name, BAD_CAST "parameter"))
00167             {
00168                   jasperserver_parameter_t *parameter = NULL;
00169                   parameter = jasperserver_read_parameter(child_node);
00170                   if (res)
00171                   {
00172                       jasperserver_list_append((jasperserver_list_t **)(&res->parameters), (jasperserver_list_t *)parameter);    
00173                   }
00174             }
00175         }
00176      }
00177      
00178     return res;
00179 }
00180 
00185 jasperserver_resource_property_t *jasperserver_read_resource_property(xmlNode *resNode)
00186 {
00187     jasperserver_resource_property_t *property = NULL;
00188     property = jasperserver_resource_property_new();
00189     xmlNode *child_node = NULL;
00190     
00191     // jasperserver_string_set is NULL safe...
00192     if (xmlHasProp(resNode, BAD_CAST "name" )) { jasperserver_string_set(property->name, xmlGetProp(resNode, BAD_CAST "name" ) ); }
00193     
00194 
00195     for (child_node = resNode->children; child_node; child_node = child_node->next) {
00196         if (child_node->type == XML_ELEMENT_NODE) {
00197         
00198             if (!xmlStrcmp(child_node->name, BAD_CAST "value"))
00199             {
00200                   // Read the text or PCDATA inside this node...
00201                   jasperserver_readNodeText(child_node, property->value);
00202             }
00203             else if (!xmlStrcmp(child_node->name, BAD_CAST "resourceProperty"))
00204             {
00205                   jasperserver_resource_property_t *resProperty = NULL;
00206                   resProperty = jasperserver_read_resource_property(child_node);
00207                   if (resProperty)
00208                   {
00209                       jasperserver_list_append((jasperserver_list_t **)(&property->properties), (jasperserver_list_t *)resProperty);    
00210                   }
00211             }
00212         }
00213      }
00214      
00215     return property;
00216 }
00217 
00222 jasperserver_parameter_t *jasperserver_read_parameter(xmlNode *resNode)
00223 {
00224     jasperserver_parameter_t *parameter = NULL;
00225     parameter = jasperserver_parameter_new();
00226     
00227     // jasperserver_string_set is NULL safe...
00228     if (xmlHasProp(resNode, BAD_CAST "name" )) { jasperserver_string_set(parameter->name, xmlGetProp(resNode, BAD_CAST "name" ) ); }
00229     if (xmlHasProp(resNode, BAD_CAST "isListItem" )) {
00230        parameter->isListItem = (!xmlStrcmp(xmlGetProp(resNode, BAD_CAST "isListItem" ), "true")) ? 1 : 0; 
00231     }
00232 
00233     // Read the text or PCDATA inside the parameter...
00234     jasperserver_readNodeText(resNode, parameter->value);
00235      
00236     return parameter;
00237 }

Generated on Wed Apr 18 16:55:51 2007 for JasperServer C webservices by  doxygen 1.5.2