/*-----------------------------------------------------------------------------------------------------------------------*/ static int32_t load_transport_protocol(FIXProtocolDescr* prot, xmlNode* parentRoot, char const* parentFile, FIXError** error) { int32_t res = FIX_SUCCESS; xmlDoc* doc = NULL; char const* transpFile = get_attr(parentRoot, "transport", parentFile); if(!strcmp(transpFile, parentFile)) // transport is the same as protocol { prot->transportVersion = _strdup(prot->version); goto ok; } char path[PATH_MAX] = {}; if (FIX_FAILED == fix_utils_make_path(parentFile, transpFile, path, PATH_MAX)) { goto err; } doc = xmlParseFile(path); if (!doc) { *error = fix_error_create(FIX_ERROR_PROTOCOL_XML_LOAD_FAILED, xmlGetLastError()->message); goto err; } if (xml_validate(doc, error) == FIX_FAILED) { goto err; } xmlNode* root = xmlDocGetRootElement(doc); prot->transportVersion = _strdup(get_attr(root, "version", NULL)); if (!strcmp(prot->version, prot->transportVersion)) // versions are the same, no need to process transport protocol { goto ok; } if (!root) { *error = fix_error_create(FIX_ERROR_PROTOCOL_XML_LOAD_FAILED, xmlGetLastError()->message); goto err; } if (load_field_types(&prot->transport_field_types, root, error) == FIX_FAILED) { goto err; } if (load_messages(prot, &prot->transport_field_types, root, error) == FIX_FAILED) { goto err; } goto ok; err: res = FIX_FAILED; ok: if (doc) { xmlFreeDoc(doc); } return res; }
static XmlType xml_locate_type (xmlDoc *doc) { XmlType ret_val = XML_TYPE_UNKNOWN; gchar *filename; filename = mrp_paths_get_dtd_dir ("mrproject-0.6.dtd"); if (xml_validate (doc, filename)) { ret_val = XML_TYPE_MRP_0_6; } else { g_free (filename); filename = mrp_paths_get_dtd_dir ("mrproject-0.5.1.dtd"); if (xml_validate (doc, filename)) { ret_val = XML_TYPE_MRP_0_5_1; } } g_free (filename); return ret_val; }
/*-----------------------------------------------------------------------------------------------------------------------*/ FIXProtocolDescr const* fix_protocol_descr_create(char const* file, FIXError** error) { FIXProtocolDescr* prot = NULL; initLibXml(error); xmlDoc* doc = xmlParseFile(file); if (!doc) { *error = fix_error_create(FIX_ERROR_PROTOCOL_XML_LOAD_FAILED, xmlGetLastError()->message); goto err; } if (xml_validate(doc, error) == FIX_FAILED) { goto err; } xmlNode* root = xmlDocGetRootElement(doc); if (!root) { *error = fix_error_create(FIX_ERROR_PROTOCOL_XML_LOAD_FAILED, xmlGetLastError()->message); goto err; } prot = (FIXProtocolDescr*)calloc(1, sizeof(FIXProtocolDescr)); prot->version = _strdup(get_attr(root, "version", NULL)); if (prot && load_transport_protocol(prot, root, file, error) == FIX_FAILED) { goto err; } else if (load_field_types(&prot->field_types, root, error) == FIX_FAILED) { goto err; } else if (load_messages(prot, &prot->field_types, root, error) == FIX_FAILED) { goto err; } goto ok; err: if (prot) { free(prot); prot = NULL; } ok: if (doc) { xmlFreeDoc(doc); } return prot; }
inline xml_element xml_parse(const char *data) { xml_element self; try { while(*data) { xml_element node; if(node.parse_body(data) == false) { break; } else { self.element.append(node); } } if(xml_validate(self) == false) throw "..."; return self; } catch(const char*) { xml_element empty; return empty; } }
static bool hidrd_xml_snk_flush(hidrd_snk *snk) { bool result = false; hidrd_xml_snk_inst *xml_snk = (hidrd_xml_snk_inst *)snk; bool valid; xmlBufferPtr xml_buf = NULL; xmlOutputBufferPtr xml_out_buf = NULL; void *new_buf; size_t new_size; XML_ERR_FUNC_BACKUP_DECL; free(xml_snk->err); xml_snk->err = strdup(""); XML_ERR_FUNC_SET(&xml_snk->err); /* Break any unfinished groups */ if (!xml_snk_group_break_branch(snk)) goto cleanup; /* Validate the document, if the schema is specified */ if (*xml_snk->schema != '\0' && (!xml_validate(&valid, xml_snk->doc, xml_snk->schema) || !valid)) goto cleanup; /* Create an XML buffer */ xml_buf = xmlBufferCreate(); if (xml_buf == NULL) goto cleanup; /* Create an XML output buffer from the generic buffer */ xml_out_buf = xmlOutputBufferCreateBuffer(xml_buf, NULL); if (xml_out_buf == NULL) goto cleanup; /* Format XML from the document */ if (xmlSaveFormatFileTo(xml_out_buf, xml_snk->doc, NULL, xml_snk->format) < 0) goto cleanup; /* xml_out_buf is closed by xmlSaveFormatFileTo */ xml_out_buf = NULL; /* Retrieve resulting size */ new_size = xmlBufferLength(xml_buf); /* If we have a location for the buffer pointer */ if (snk->pbuf != NULL) { /* Retention and update the buffer */ new_buf = realloc(*snk->pbuf, new_size); if (new_size > 0 && new_buf == NULL) XML_ERR_CLNP("failed to retention the output buffer"); memcpy(new_buf, xmlBufferContent(xml_buf), new_size); /* Update the buffer pointer */ *snk->pbuf = new_buf; } /* Output size */ if (snk->psize != NULL) *snk->psize = new_size; result = true; cleanup: if (xml_out_buf != NULL) xmlOutputBufferClose(xml_out_buf); if (xml_buf != NULL) xmlBufferFree(xml_buf); XML_ERR_FUNC_RESTORE; return result; }
static bool hidrd_xml_src_init(hidrd_src *src, char **perr, const char *schema) { bool result = false; hidrd_xml_src_inst *xml_src = (hidrd_xml_src_inst *)src; hidrd_xml_src_state *state = NULL; xmlDocPtr doc = NULL; bool valid; xmlNodePtr root; XML_ERR_FUNC_BACKUP_DECL; if (perr != NULL) *perr = strdup(""); XML_ERR_FUNC_SET(perr); /* Create item state table stack */ state = malloc(sizeof(*state)); if (state == NULL) XML_ERR_CLNP("failed to allocate memory for the state table"); state->prev = NULL; state->usage_page = HIDRD_USAGE_PAGE_UNDEFINED; /* Parse the document */ doc = xmlParseMemory(src->buf, src->size); if (doc == NULL) goto cleanup; /* Validate the document, if the schema is specified */ if (*schema != '\0' && (!xml_validate(&valid, doc, schema) || !valid)) goto cleanup; /* Retrieve the root element */ root = xmlDocGetRootElement(doc); if (root == NULL) XML_ERR_CLNP("root element not found"); /* Initialize the source */ xml_src->doc = doc; xml_src->prnt = NULL; xml_src->cur = root; xml_src->state = state; xml_src->err = strdup(""); /* Own the resources */ doc = NULL; state = NULL; /* Success */ result = true; cleanup: if (doc != NULL) xmlFreeDoc(doc); free(state); XML_ERR_FUNC_RESTORE; return result; }
static gboolean msp_plugin_transform (PlannerPlugin *plugin, const gchar *input_filename) { xsltStylesheet *stylesheet; xmlDoc *doc; xmlDoc *final_doc; gint fd; FILE *file; gchar *tmp_name, *uri; MrpProject *project; gchar *filename; /* libxml housekeeping */ xmlSubstituteEntitiesDefault (1); xmlLoadExtDtdDefaultValue = 1; exsltRegisterAll (); filename = mrp_paths_get_stylesheet_dir ("msp2planner.xsl"); stylesheet = xsltParseStylesheetFile (filename); g_free (filename); doc = xmlParseFile (input_filename); if (!doc) { xsltFreeStylesheet (stylesheet); return FALSE; } final_doc = xsltApplyStylesheet (stylesheet, doc, NULL); xmlFree (doc); if (!final_doc) { xsltFreeStylesheet (stylesheet); return FALSE; } filename = mrp_paths_get_dtd_dir ("mrproject-0.6.dtd"); if (!xml_validate (final_doc, filename)) { GtkWidget *dialog; xsltFreeStylesheet (stylesheet); xmlFree (final_doc); dialog = gtk_message_dialog_new (GTK_WINDOW (plugin->main_window), GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Couldn't import file.")); gtk_widget_show (dialog); gtk_dialog_run (GTK_DIALOG (dialog)); gtk_widget_destroy (dialog); g_free (filename); return FALSE; } g_free (filename); fd = g_file_open_tmp ("planner-msp-XXXXXX", &tmp_name, NULL); if (fd == -1) { xsltFreeStylesheet (stylesheet); xmlFree (final_doc); return FALSE; } file = fdopen (fd, "w"); if (file == NULL) { xsltFreeStylesheet (stylesheet); xmlFree (final_doc); close (fd); g_free (tmp_name); return FALSE; } if (xsltSaveResultToFile (file, final_doc, stylesheet) == -1) { xsltFreeStylesheet (stylesheet); xmlFree (final_doc); fclose (file); g_free (tmp_name); return FALSE; } xsltFreeStylesheet (stylesheet); xmlFree (final_doc); uri = g_filename_to_uri (tmp_name, NULL, NULL); if (uri) { planner_window_open_in_existing_or_new (plugin->main_window, uri, TRUE); project = planner_window_get_project (plugin->main_window) ; mrp_project_set_uri (project, NULL); } unlink (tmp_name); fclose (file); g_free (tmp_name); g_free (uri); return TRUE; }