From a6cc4ca8b8432acdc2ce7bf7ebb92583eeeeff9f Mon Sep 17 00:00:00 2001 From: snowdrop Date: Fri, 15 Oct 2004 13:34:47 +0000 Subject: development --- wsdl2c/CallFunc.c | 2 +- wsdl2c/CallVar.c | 4 +- wsdl2c/codewriter.c | 125 +++++++++++++++++++++++++++++++ wsdl2c/codewriter.h | 12 +++ wsdl2c/wsdl2c.c | 211 ++++++++++++++++++++++++++++++++++++++++++---------- 5 files changed, 313 insertions(+), 41 deletions(-) create mode 100755 wsdl2c/codewriter.c create mode 100755 wsdl2c/codewriter.h (limited to 'wsdl2c') diff --git a/wsdl2c/CallFunc.c b/wsdl2c/CallFunc.c index db13579..3314203 100644 --- a/wsdl2c/CallFunc.c +++ b/wsdl2c/CallFunc.c @@ -99,7 +99,7 @@ struct CallFunc* CallFunc_Deserialize(xmlNodePtr xmlRoot) if ((!xmlStrcmp(cur->name, (const xmlChar *)"name"))){ key = xmlNodeListGetString(cur->doc, cur->xmlChildrenNode, 1); CallFunc_Set_name(obj, (const char*)key); - xmlFree(key); + /*xmlFree(key);*/ } if ((!xmlStrcmp(cur->name, (const xmlChar *)"in"))){ CallFunc_Add_in( obj, CallVar_Deserialize(cur) ); diff --git a/wsdl2c/CallVar.c b/wsdl2c/CallVar.c index 035f56c..cc42dae 100644 --- a/wsdl2c/CallVar.c +++ b/wsdl2c/CallVar.c @@ -86,12 +86,12 @@ struct CallVar* CallVar_Deserialize(xmlNodePtr xmlRoot) if ((!xmlStrcmp(cur->name, (const xmlChar *)"name"))){ key = xmlNodeListGetString(cur->doc, cur->xmlChildrenNode, 1); CallVar_Set_name(obj, (const char*)key); - xmlFree(key); + /*xmlFree(key);*/ } if ((!xmlStrcmp(cur->name, (const xmlChar *)"type"))){ key = xmlNodeListGetString(cur->doc, cur->xmlChildrenNode, 1); CallVar_Set_type(obj, (const char*)key); - xmlFree(key); + /*xmlFree(key);*/ } // TODO: cur = cur->next; diff --git a/wsdl2c/codewriter.c b/wsdl2c/codewriter.c new file mode 100755 index 0000000..7f46b7f --- /dev/null +++ b/wsdl2c/codewriter.c @@ -0,0 +1,125 @@ +#include "codewriter.h" +#include +#include + +#include + +static FILE *header; +static FILE *source; +static char* ns = "impl"; + +static int openSourceFile(const char* filename); +static void closeSourceFile(); + +static int openHeaderFile(const char* filename); +static void closeHeaderFile(); + +static void writeOperation(struct CallFunc *call) +{ + struct CallVar_List* var; + + fprintf(header, "%s %s_%s(", trXSD2C(call->out->type), ns, call->name); + + var = CallFunc_Get_in(call); + + while (var) { + fprintf(header, "const %s %s", trXSD2C(var->value->type), var->value->name); + var = var->next; + if (var) fprintf(header, ","); + } + + fprintf(header, ")"); +} + +void codeWriteStubHeader(struct CallList *cl, const char* filename) +{ + struct CallFunc_List *call; + struct CallVar_List* var; + HCOMPLEXTYPE ct; + + openHeaderFile(filename); + + fprintf(header, "#ifndef __TEST_SOAP_H__\n#define __TEST_SOAP_H__\n\n"); + + /* Find header files */ + call = CallList_Get_operation(cl); + while (call) { + var = CallFunc_Get_in(call->value); + while (var) { + ct = objRegistryGetComplexType(var->value->type); + if (ct /* TODO: && not in written before */) + fprintf(header, "#include \"%s.h\"\n", trXSDParseNs(var->value->type)); /* _xsd*/ + var = var->next; + } + call = call->next; + } + + fprintf(header, "\n"); + call = CallList_Get_operation(cl); + while (call) { + + writeOperation(call->value); + fprintf(header, ";\n"); + call = call->next; + } + + fprintf(header, "\n\n#endif\n"); + + closeHeaderFile(); +} + + +void codeWriteStubSource(struct CallList *cl, const char* filename) +{ + struct CallFunc_List *call; + + openSourceFile(filename); + + fprintf(source, "#include \"test_stub.h\"\n\n"); + + call = CallList_Get_operation(cl); + while (call) { + + writeOperation(call->value); +/* fwrite(source, "\n{\n\t*/ + call = call->next; + } + + + fprintf(source, "\n"); + + closeSourceFile(); +} + + + +static +int openSourceFile(const char* filename) +{ + source = fopen(filename, "w"); + if (!source) return 0; + return 1; +} + +static +void closeSourceFile() +{ + fclose(source); +} + +static +int openHeaderFile(const char* filename) +{ + header= fopen(filename, "w"); + if (!header) return 0; + return 1; +} + + +static +void closeHeaderFile() +{ + fclose(header); +} + + diff --git a/wsdl2c/codewriter.h b/wsdl2c/codewriter.h new file mode 100755 index 0000000..ab2a5f4 --- /dev/null +++ b/wsdl2c/codewriter.h @@ -0,0 +1,12 @@ +#ifndef __CODE_WRITER_H__ +#define __CODE_WRITER_H__ + + +#include "CallList.h" + +void codeWriteStubHeader(struct CallList *cl, const char* filename); +void codeWriteStubSource(struct CallList *cl, const char* filename); + + +#endif + diff --git a/wsdl2c/wsdl2c.c b/wsdl2c/wsdl2c.c index f726dff..55a2e71 100755 --- a/wsdl2c/wsdl2c.c +++ b/wsdl2c/wsdl2c.c @@ -1,5 +1,5 @@ /****************************************************************** - * $Id: wsdl2c.c,v 1.7 2004/06/08 12:55:04 snowdrop Exp $ + * $Id: wsdl2c.c,v 1.8 2004/10/15 13:34:47 snowdrop Exp $ * * CSOAP Project: A SOAP client/server library in C * Copyright (C) 2003 Ferhat Ayaz @@ -70,6 +70,9 @@ void Writer_EndElement(const char* element_name, void* userData) struct CallList* callList = NULL; char soap_prefix[50]; +char wsdl_prefix[50]; +char target_ns[150]; +char target_prefix[50]; xmlXPathObjectPtr xpath_eval(xmlDocPtr doc, const char *xpath) { @@ -101,11 +104,12 @@ xmlXPathObjectPtr xpath_eval(xmlDocPtr doc, const char *xpath) default_ns = xmlSearchNs(doc, node, NULL); if (default_ns != NULL && default_ns->href != NULL) { fprintf(stdout, "Register default namespace '%s'\n", (const char*)default_ns->href); - xmlXPathRegisterNs(context, "wsdl", default_ns->href ); /* default_ns : "http://schemas.xmlsoap.org/wsdl/" */ + /* default_ns : "http://schemas.xmlsoap.org/wsdl/" */ + xmlXPathRegisterNs(context, "wsdl", default_ns->href ); } - + printf("XPath: '%s'\n", xpath); result = xmlXPathEvalExpression((xmlChar*)xpath, context); if (result == NULL) { fprintf(stderr, "ERROR: XPath error!\n"); @@ -114,6 +118,7 @@ xmlXPathObjectPtr xpath_eval(xmlDocPtr doc, const char *xpath) if(xmlXPathNodeSetIsEmpty(result->nodesetval)){ /* no result */ + printf("XPath result is empty\n"); return NULL; } @@ -154,11 +159,11 @@ xmlNodePtr findPortType(xmlDocPtr doc, const char *name) } if (!xmlStrcmp(attr_name, (const xmlChar*)name)) { - xmlFree(attr_name); + /*xmlFree(attr_name);*/ return cur; } - xmlFree(attr_name); + /*xmlFree(attr_name);*/ cur = cur->next; } @@ -199,11 +204,11 @@ xmlNodePtr findMessage(xmlDocPtr doc, const char *name) } if (!xmlStrcmp(attr_name, (const xmlChar*)name)) { - xmlFree(attr_name); + /*xmlFree(attr_name);*/ return cur; } - xmlFree(attr_name); + /*xmlFree(attr_name);*/ cur = cur->next; } @@ -281,7 +286,7 @@ void handleInputParameters(xmlDocPtr doc, const char *name, struct CallFunc *fun if (var_type == NULL) { fprintf(stderr, "ERROR: Found without attrbute 'type' or 'element'! (message:'%s')\n", message_name); - xmlFree(var_name); + /*xmlFree(var_name);*/ cur = cur->next; continue; } @@ -293,10 +298,78 @@ void handleInputParameters(xmlDocPtr doc, const char *name, struct CallFunc *fun fprintf(stdout, "\t\t(%s,%s)\n", trXSD2C((const char*)var_type), (const char*)var_name); - xmlFree(var_name); - xmlFree(var_type); + /*xmlFree(var_name); + xmlFree(var_type);*/ + + cur = cur->next; + } + +} + +void handleOutputParameters(xmlDocPtr doc, const char *name, struct CallFunc *func) +{ + char ns[10]; + char message_name[255]; + xmlNodePtr node; + xmlNodePtr cur; + struct CallVar *var; + xmlChar *var_name, *var_type; + + parseNS(name, ns, message_name); /* check why to pase ns? */ + + node = findMessage(doc, message_name); + if (node == NULL) { + fprintf(stderr, "ERROR: Can not find message '%s'\n", message_name); + return; + } + + cur = node->xmlChildrenNode; + while (cur != NULL) { + + if (cur->type != XML_ELEMENT_NODE) { + cur = cur->next; + continue; + } + + if (xmlStrcmp(cur->name, (const xmlChar*)"part")) { + cur = cur->next; + continue; + } + + var_name = xmlGetProp(cur, "name"); + if (var_name == NULL) { + fprintf(stderr, "ERROR: Found without attrbute 'name'! (message:'%s')\n", + message_name); + cur = cur->next; + continue; + } + + var_type = xmlGetProp(cur, "type"); + if (var_type == NULL) { + var_type = xmlGetProp(cur, "element"); + } + + if (var_type == NULL) { + fprintf(stderr, "ERROR: Found without attrbute 'type' or 'element'! (message:'%s')\n", + message_name); + /*xmlFree(var_name);*/ + cur = cur->next; + continue; + } + var = CallVar_Create(); + CallVar_Set_name(var, (const char*)var_name); + CallVar_Set_type(var, (const char*)var_type); + CallFunc_Set_out(func, var); + + fprintf(stdout, "\t\t(%s,%s)\n", trXSD2C((const char*)var_type), (const char*)var_name); + + /*xmlFree(var_name); + xmlFree(var_type);*/ + cur = cur->next; + + break; /* only one return parameter */ } } @@ -338,7 +411,7 @@ void handlePortType(xmlDocPtr doc, const char *name) } strcpy(opname, (const char*)attr_name); - xmlFree(attr_name); + /*xmlFree(attr_name);*/ func = CallFunc_Create(); CallList_Add_operation(callList, func); @@ -356,7 +429,7 @@ void handlePortType(xmlDocPtr doc, const char *name) message = xmlGetProp(input, "message"); if (message == NULL) { - fprintf(stderr, "ERROR: No message attribute for input operation '%'\n", opname); + fprintf(stderr, "ERROR: No message attribute for input operation '%s'\n", opname); cur = cur->next; continue; } @@ -365,7 +438,7 @@ void handlePortType(xmlDocPtr doc, const char *name) handleInputParameters(doc, (const char*)message, func); - xmlFree(message); + /*xmlFree(message);*/ /* handle output */ output = findSubNode(cur, "output"); @@ -376,14 +449,15 @@ void handlePortType(xmlDocPtr doc, const char *name) message = xmlGetProp(output, "message"); if (message == NULL) { - fprintf(stderr, "ERROR: No message attribute for output operation '%'\n", opname); + fprintf(stderr, "ERROR: No message attribute for output operation '%s'\n", opname); cur = cur->next; continue; } fprintf(stdout, "\toutput = '%s'\n", (const char*)message); - xmlFree(message); + /*xmlFree(message);*/ + handleOutputParameters(doc, (const char*)message, func); cur = cur->next; } @@ -404,7 +478,10 @@ void handleBinding(xmlDocPtr doc, const char *type) parseNS(type, binding_ns, binding_type); /* Set xpath query */ - sprintf(query, "//wsdl:definitions/wsdl:binding[@name=\"%s\"]", binding_type); + if (wsdl_prefix[0]) + sprintf(query, "//%s:definitions/%s:binding[@name=\"%s\"]", wsdl_prefix, wsdl_prefix, binding_type); + else + sprintf(query, "//definitions/binding[@name=\"%s\"]", binding_type); /* Find Bindings */ xpathObj = xpath_eval(doc, query); @@ -442,8 +519,8 @@ void handleBinding(xmlDocPtr doc, const char *type) handlePortType(doc, port_name); } - if (name != NULL) xmlFree(name); - if (btype != NULL) xmlFree(btype); +/* if (name != NULL) xmlFree(name); + if (btype != NULL) xmlFree(btype); */ } } @@ -456,7 +533,11 @@ void wsdlParse(xmlDocPtr doc) xmlXPathObjectPtr xpathObj; char query[255]; - sprintf(query, "//wsdl:definitions/wsdl:service/wsdl:port[%s:address]", soap_prefix); + if (wsdl_prefix[0]) + sprintf(query, "//%s:definitions/%s:service/%s:port[%s:address]", wsdl_prefix, wsdl_prefix, wsdl_prefix, soap_prefix); + else + sprintf(query, "//definitions/service/port[%s:address]", soap_prefix); + /* Find Soap Service */ xpathObj = xpath_eval(doc, query); @@ -490,8 +571,8 @@ void wsdlParse(xmlDocPtr doc) handleBinding(doc, binding); - if (name != NULL) xmlFree(name); - if (binding != NULL) xmlFree(binding); +/* if (name != NULL) xmlFree(name); + if (binding != NULL) xmlFree(binding);*/ } @@ -509,6 +590,10 @@ int main(int argc, char *argv[]) xmlNodePtr xsdRoot; xmlNsPtr default_ns; xmlNsPtr soap_ns; + xmlNsPtr wsdl_ns; + xmlNsPtr tar_ns; + xmlChar *tar_ns_str; + int i; char outDir[1054]; char fname[255]; @@ -567,7 +652,7 @@ int main(int argc, char *argv[]) } if (soap_ns != NULL && soap_ns->prefix != NULL) { - fprintf(stdout, "Wsdl Soap prefix: '%s'\n", soap_ns->prefix); + fprintf(stdout, "Wsdl SOAP prefix: '%s'\n", soap_ns->prefix); strcpy(soap_prefix, soap_ns->prefix); } else { fprintf(stderr,"Namespace 'http://schemas.xmlsoap.org/wsdl/soap/' expected\n"); @@ -576,34 +661,84 @@ int main(int argc, char *argv[]) } + /* Search for wsdl namespace + http://schemas.xmlsoap.org/wsdl/ + */ + wsdl_ns = xmlSearchNsByHref(doc, node, "http://schemas.xmlsoap.org/wsdl/"); + if (wsdl_ns == NULL) { + wsdl_ns = xmlSearchNsByHref(doc, node, "http://schemas.xmlsoap.org/wsdl"); + } - /* search default namespace */ - default_ns = xmlSearchNs(doc, node, NULL); - if (default_ns == NULL || ( default_ns != NULL && default_ns->href == NULL)) { - fprintf(stdout, "Adding default namespace 'http://schemas.xmlsoap.org/wsdl/'\n"); - xmlNewDocProp(doc, "xmlns", "http://schemas.xmlsoap.org/wsdl/"); - } - if (!xsdInitTrModule(xsdRoot)) - return 1; + if (wsdl_ns != NULL && wsdl_ns->prefix != NULL) { + fprintf(stdout, "Wsdl prefix: '%s'\n", wsdl_ns->prefix); + strcpy(wsdl_prefix, wsdl_ns->prefix); + } else { + /* search default namespace */ + default_ns = xmlSearchNs(doc, node, NULL); + if (default_ns == NULL || ( default_ns != NULL && default_ns->href == NULL)) { + fprintf(stdout, "Adding default namespace 'http://schemas.xmlsoap.org/wsdl/'\n"); + xmlNewDocProp(doc, "xmlns", "http://schemas.xmlsoap.org/wsdl/"); + } + + /*strcpy(wsdl_prefix, "wsdl");*/ + wsdl_prefix[0] = '\0'; + } + + + + /* search targetNamespace */ + tar_ns_str = xmlGetProp(node, "targetNamespace"); + if (tar_ns_str) { + strcpy(target_ns, tar_ns_str); + /* Search for target namespace + */ + tar_ns = xmlSearchNsByHref(doc, node, target_ns); + if (tar_ns && tar_ns->prefix) { + strcpy(target_prefix, tar_ns->prefix); + printf("Target namespace: %s\nTarget NS Prefix: %s\n", + target_ns, target_prefix); + } else { + target_prefix[0] = '\0'; + } + /*xmlFree*/ + } else { + target_ns[0] = '\0'; + target_prefix[0] = '\0'; + } - if (!xsdInitObjModule(xsdRoot)) - return 1; + xsdRoot = xsdRoot?xsdRoot:node; + while (xsdRoot) { + + if (xsdRoot->type != XML_ELEMENT_NODE) { + xsdRoot = xsdRoot->next; + continue; + } + + if (!xsdInitTrModule(xsdRoot)) + return 1; - - if (xsdEngineRun(xsdRoot, outDir)) { - fprintf(stderr, "xsd2c engine error\n"); - return 1; - } + if (!xsdInitObjModule(xsdRoot)) + return 1; + + printf("Calling xsd engine\n"); + if (xsdEngineRun(xsdRoot, outDir)) { + fprintf(stderr, "xsd2c engine error\n"); + return 1; + } + xsdRoot = xsdRoot->next; + } callList = CallList_Create(); wsdlParse(doc); - CallList_Sax_Serialize(callList, "CallList", +/* CallList_Sax_Serialize(callList, "CallList", Writer_StartElement, Writer_Characters, Writer_EndElement, 0); +*/ + codeWriteStubHeader(callList, "test_stub.h"); CallList_Free(callList); -- cgit v1.1-32-gdbae