/****f* xml_element/xml_elem_free * NAME * xml_elem_free * SYNOPSIS * void xml_elem_free(xml_element* root) * FUNCTION * free an xml element and all of its child elements * INPUTS * root - the root of an xml tree you would like to free * RESULT * void * NOTES * SEE ALSO * xml_elem_free_non_recurse () * xml_elem_new () * SOURCE */ void xml_elem_free(xml_element* root) { if(root) { xml_element* kids = Q_Head(&root->children); while(kids) { xml_elem_free(kids); kids = Q_Next(&root->children); } xml_elem_free_non_recurse(root); } }
/****f* xml_element/xml_elem_parse_buf * NAME * xml_elem_parse_buf * SYNOPSIS * xml_element* xml_elem_parse_buf(const char* in_buf, int len, XML_ELEM_INPUT_OPTIONS options, XML_ELEM_ERROR error) * FUNCTION * parse a buffer containing XML into an xml_element in-memory tree * INPUTS * in_buf - buffer containing XML document * len - length of buffer * options - input options. optional * error - error result data. optional. check if result is null. * RESULT * void * NOTES * The returned data must be free'd by caller * SEE ALSO * xml_elem_serialize_to_string () * xml_elem_free () * SOURCE */ xml_element* xml_elem_parse_buf(const char* in_buf, int len, XML_ELEM_INPUT_OPTIONS options, XML_ELEM_ERROR error) { xml_element* xReturn = NULL; char buf[100] = ""; static STRUCT_XML_ELEM_INPUT_OPTIONS default_opts = {encoding_utf_8}; if(!options) { options = &default_opts; } if(in_buf) { XML_Parser parser; xml_elem_data mydata = {0}; parser = XML_ParserCreate(NULL); mydata.root = xml_elem_new(); mydata.current = mydata.root; mydata.input_options = options; mydata.needs_enc_conversion = options->encoding && strcmp(options->encoding, encoding_utf_8); XML_SetElementHandler(parser, (XML_StartElementHandler)_xmlrpc_startElement, (XML_EndElementHandler)_xmlrpc_endElement); XML_SetCharacterDataHandler(parser, (XML_CharacterDataHandler)_xmlrpc_charHandler); /* pass the xml_elem_data struct along */ XML_SetUserData(parser, (void*)&mydata); if(!len) { len = strlen(in_buf); } /* parse the XML */ if(XML_Parse(parser, in_buf, len, 1) == 0) { enum XML_Error err_code = XML_GetErrorCode(parser); int line_num = XML_GetCurrentLineNumber(parser); int col_num = XML_GetCurrentColumnNumber(parser); long byte_idx = XML_GetCurrentByteIndex(parser); /* int byte_total = XML_GetCurrentByteCount(parser); */ const char * error_str = XML_ErrorString(err_code); if(byte_idx >= 0) { snprintf(buf, sizeof(buf), "\n\tdata beginning %ld before byte index: %s\n", byte_idx > 10 ? 10 : byte_idx, in_buf + (byte_idx > 10 ? byte_idx - 10 : byte_idx)); } /* fprintf(stderr, "expat reports error code %i\n" "\tdescription: %s\n" "\tline: %i\n" "\tcolumn: %i\n" "\tbyte index: %ld\n" "\ttotal bytes: %i\n%s ", err_code, error_str, line_num, col_num, byte_idx, byte_total, buf); */ /* error condition */ if(error) { error->parser_code = (long)err_code; error->line = line_num; error->column = col_num; error->byte_index = byte_idx; error->parser_error = error_str; } } else { xReturn = (xml_element*)Q_Head(&mydata.root->children); xReturn->parent = NULL; } XML_ParserFree(parser); xml_elem_free_non_recurse(mydata.root); } return xReturn; }
/* convert XMLRPC_REQUEST to soap xml dom. */ xml_element* SOAP_REQUEST_to_xml_element(XMLRPC_REQUEST request) { xml_element* root = xml_elem_new(); /* safety first. */ if (root) { xml_element* body = xml_elem_new(); root->name = strdup("SOAP-ENV:Envelope"); /* silly namespace stuff */ Q_PushTail(&root->attrs, new_attr("xmlns:SOAP-ENV", "http://schemas.xmlsoap.org/soap/envelope/")); Q_PushTail(&root->attrs, new_attr("xmlns:xsi", "http://www.w3.org/1999/XMLSchema-instance")); Q_PushTail(&root->attrs, new_attr("xmlns:xsd", "http://www.w3.org/1999/XMLSchema")); Q_PushTail(&root->attrs, new_attr("xmlns:SOAP-ENC", "http://schemas.xmlsoap.org/soap/encoding/")); Q_PushTail(&root->attrs, new_attr("xmlns:si", "http://soapinterop.org/xsd")); Q_PushTail(&root->attrs, new_attr("xmlns:ns6", "http://testuri.org")); Q_PushTail(&root->attrs, new_attr("SOAP-ENV:encodingStyle", "http://schemas.xmlsoap.org/soap/encoding/")); /* Q_PushHead(&root->attrs, new_attr("xmlns:ks", "http://kitchen.sink.org/soap/everything/under/sun")); JUST KIDDING!! :-) ----> ------------------------------------------------- */ if (body) { /* go ahead and serialize first... */ xml_element* el_serialized = SOAP_to_xml_element_worker(request, XMLRPC_RequestGetData(request)); /* check for fault, in which case, there is no intermediate element */ if (el_serialized && !strcmp(el_serialized->name, TOKEN_FAULT)) { Q_PushTail(&body->children, el_serialized); } /* usual case: not a fault. Add Response element in between. */ else { xml_element* rpc = xml_elem_new(); if (rpc) { const char* methodname = XMLRPC_RequestGetMethodName(request); XMLRPC_REQUEST_TYPE rtype = XMLRPC_RequestGetRequestType(request); /* if we are making a request, we want to use the methodname as is. */ if (rtype == xmlrpc_request_call) { if (methodname) { rpc->name = strdup(methodname); } } /* if it's a response, we append "Response". Also, given xmlrpc-epi API/architecture, it's likely that we don't have a methodname for the response, so we have to check that. */ else { char buf[128]; snprintf(buf, sizeof(buf), "%s%s", methodname ? methodname : "", "Response"); rpc->name = strdup(buf); } /* add serialized data to method call/response. add method call/response to body element */ if (rpc->name) { if(el_serialized) { if(Q_Size(&el_serialized->children) && rtype == xmlrpc_request_call) { xml_element* iter = (xml_element*)Q_Head(&el_serialized->children); while(iter) { Q_PushTail(&rpc->children, iter); iter = (xml_element*)Q_Next(&el_serialized->children); } xml_elem_free_non_recurse(el_serialized); } else { Q_PushTail(&rpc->children, el_serialized); } } Q_PushTail(&body->children, rpc); } else { /* no method name?! TODO: fault here...? */ } } } body->name = strdup("SOAP-ENV:Body"); Q_PushTail(&root->children, body); } } return root; }