#include "write_xml.h" /* Takes the current data in the specified ggobid structure and writes the XML file corresponding to this configuration. This can be used to convert the original data formats to XML, if that is desired. (More likely to be correct than writing a Perl script!) */ #include XmlWriteInfo *updateXmlWriteInfo(datad *d, ggobid *gg, XmlWriteInfo *info); gboolean write_xml (const gchar *filename, ggobid *gg, XmlWriteInfo *xmlWriteInfo) { FILE *f; gboolean ok = false; /* *datad *d; *GSList *tmp = gg->d; */ f = fopen (filename,"w"); if (f == NULL) { return (false); } write_xml_stream (f, gg, filename, xmlWriteInfo); fclose(f); return ok; } gboolean write_xml_stream (FILE *f, ggobid *gg, const gchar *filename, XmlWriteInfo *xmlWriteInfo) { gint numDatasets, i; datad *d; numDatasets = g_slist_length(gg->d); write_xml_header (f, -1, gg, xmlWriteInfo); for(i = 0; i < numDatasets; i++) { d = (datad *) g_slist_nth_data(gg->d, i); if(xmlWriteInfo->useDefault) updateXmlWriteInfo(d, gg, xmlWriteInfo); write_xml_dataset(f, d, gg, xmlWriteInfo); } write_xml_footer(f, gg, xmlWriteInfo); return(true); } gboolean write_xml_dataset(FILE *f, datad *d, ggobid *gg, XmlWriteInfo *xmlWriteInfo) { write_dataset_header (f, d, gg, xmlWriteInfo); write_xml_description (f, gg, xmlWriteInfo); write_xml_variables (f, d, gg, xmlWriteInfo); write_xml_records (f, d, gg, xmlWriteInfo); /*-- skip for now, because there's no need to write the default edges --*/ /* write_xml_edges(f, d, gg);*/ write_dataset_footer(f, gg, xmlWriteInfo); return(true); } gboolean write_xml_header (FILE *f, int numDatasets, ggobid *gg, XmlWriteInfo *xmlWriteInfo) { fprintf(f, ""); fprintf(f, "\n"); fprintf(f, ""); fprintf(f, "\n\n"); if(numDatasets < 0) numDatasets = g_slist_length(gg->d); fprintf(f, "\n", numDatasets); /* fflush(f);*/ return(true); } gboolean write_xml_description (FILE *f, ggobid *gg, XmlWriteInfo *xmlWriteInfo) { fprintf(f,"\n"); fprintf(f,"\n"); return(true); } gboolean write_xml_variables (FILE *f, datad *d, ggobid *gg, XmlWriteInfo *xmlWriteInfo) { gint i; fprintf(f,"\n", d->ncols); for(i = 0; i < d->ncols; i++) { write_xml_variable (f, d, gg, i, xmlWriteInfo); fprintf(f,"\n"); } fprintf(f,"\n"); return(true); } gboolean write_xml_variable(FILE *f, datad *d, ggobid *gg, gint i, XmlWriteInfo *xmlWriteInfo) { /* fprintf(f, "vartable[i].collab); if(strcmp(gg->vartable[i].collab, gg->vartable[i].collab_tform) != 0) { fprintf(f," transformName=\"%s\"", gg->vartable[i].collab_tform); } fprintf(f, " />"); */ fprintf(f, ""); fprintf(f,"%s", d->vartable[i].collab); fprintf(f, ""); return(true); } gboolean write_xml_records(FILE *f, datad *d, ggobid *gg, XmlWriteInfo *xmlWriteInfo) { int i; fprintf(f, "nrows); if(xmlWriteInfo->useDefault) { fprintf(f, " glyphSize=\"%s\"", xmlWriteInfo->defaultGlyphSizeName); fprintf(f, " glyphType=\"%s\"", xmlWriteInfo->defaultGlyphTypeName); fprintf(f, " color=\"%s\"", xmlWriteInfo->defaultColorName); } fprintf(f, ">\n"); for(i = 0; i < d->nrows; i++) { write_xml_record(f, d, gg, i, xmlWriteInfo); fprintf(f, "\n"); } fprintf(f, "\n"); return(true); } gboolean write_xml_record (FILE *f, datad *d, ggobid *gg, gint i, XmlWriteInfo *xmlWriteInfo) { gint j; gchar *gstr; fprintf(f, "rowlab && d->rowlab->data && (gstr = (gchar *) g_array_index (d->rowlab, gchar *, i))) { fprintf(f, " label=\"%s\"", gstr); } if(!xmlWriteInfo->useDefault || xmlWriteInfo->defaultColor != d->color_ids.els[i]) { fprintf(f, " color=\"%d\"", d->color_ids.els[i]); } /* fprintf(f, " glyphSize=\"%d\"", d->glyph_ids[i].size); fprintf(f, " glyphType=\"%d\"", d->glyph_ids[i].type); */ if(!xmlWriteInfo->useDefault || (xmlWriteInfo->defaultGlyphType != d->glyph_ids[i].type)) { switch (d->glyph_ids[i].type) { case PLUS_GLYPH: gstr = "plus"; break; case X_GLYPH: gstr = "x"; break; case OPEN_RECTANGLE: gstr = "or"; break; case FILLED_RECTANGLE: gstr = "fr"; break; case OPEN_CIRCLE: gstr = "oc"; break; case FILLED_CIRCLE: gstr = "fc"; break; case POINT_GLYPH: gstr = "."; break; } fprintf(f, " glyphType=\"%s\"", gstr); } if(!xmlWriteInfo->useDefault || (xmlWriteInfo->defaultGlyphSize != d->glyph_ids[i].size)) { fprintf(f, " glyphSize=\"%d\"", d->glyph_ids[i].size); } fprintf(f, ">\n"); for(j = 0; j < d->ncols; j++) { writeFloat (f, d->raw.vals[i][j]); if (j < d->ncols-1 ) fprintf(f, " "); } fprintf(f, "\n"); return (true); } gboolean write_xml_edges (FILE *f, datad *d, ggobid *gg, XmlWriteInfo *xmlWriteInfo) { gint i; if (d->nedges < 1) return(true); fprintf(f, "\n", d->nedges); for(i = 0; i < d->nedges; i++) { write_xml_edge(f, d, gg, i, xmlWriteInfo); fprintf(f, "\n"); } fprintf(f, "/edges>\n"); return(true); } gboolean write_xml_edge(FILE *f, datad *d, ggobid *gg, int i, XmlWriteInfo *xmlWriteInfo) { fprintf(f, "edge_endpoints[i].a , d->edge_endpoints[i].b); fprintf(f, " />"); return(true); } void writeFloat(FILE *f, double value) { fprintf(f, "%.3f", value); } gboolean write_dataset_header (FILE *f, datad *d, ggobid *gg, XmlWriteInfo *xmlWriteInfo) { fprintf(f,"nrows); fprintf(f,">\n"); return(true); } gboolean write_dataset_footer(FILE *f, ggobid *gg, XmlWriteInfo *xmlWriteInfo) { fprintf(f,"\n"); return(true); } gboolean write_xml_footer(FILE *f, ggobid *gg, XmlWriteInfo *xmlWriteInfo) { fprintf(f,"\n"); return(true); } XmlWriteInfo * updateXmlWriteInfo(datad *d, ggobid *gg, XmlWriteInfo *info) { int i, n, numGlyphSizes; gint *colorCounts, *glyphTypeCounts, *glyphSizeCounts, count; gchar *str; colorCounts = g_malloc(sizeof(gint) * gg->ncolors); glyphTypeCounts = g_malloc(sizeof(gint) * UNKNOWN_GLYPH); numGlyphSizes = 7; glyphSizeCounts = g_malloc(sizeof(gint) * numGlyphSizes); memset(colorCounts, '\0', sizeof(gint) * gg->ncolors); memset(glyphTypeCounts, '\0', sizeof(gint) * UNKNOWN_GLYPH); memset(glyphSizeCounts, '\0', sizeof(gint) * numGlyphSizes); n = GGOBI(nrecords)(d); for(i = 0 ; i < n ; i++) { colorCounts[d->color_ids.els[i]]++; glyphSizeCounts[d->glyph_ids[i].size]++; glyphTypeCounts[d->glyph_ids[i].type]++; } count = -1; for(i = 0 ; i < gg->ncolors; i++) { if(colorCounts[i] > count) { info->defaultColor = i; count= colorCounts[i]; } } count = -1; for(i = 0 ; i < UNKNOWN_GLYPH; i++) { if(glyphTypeCounts[i] > count) { info->defaultGlyphType = i; count= glyphTypeCounts[i]; } } count = -1; for(i = 0 ; i < numGlyphSizes; i++) { if(glyphSizeCounts[i] > count) { info->defaultGlyphSize = i; count= glyphSizeCounts[i]; } } if(gg->colorNames && (str = gg->colorNames[info->defaultColor]) && str[0]) info->defaultColorName = g_strdup(str); else { info->defaultColorName = str = g_malloc(5 * sizeof(char)); sprintf(str, "%d", info->defaultColor); } info->defaultGlyphSizeName = str = g_malloc(5 * sizeof(char)); sprintf(str, "%d", info->defaultGlyphSize); str = GGOBI(getGlyphTypeName)(info->defaultGlyphType); info->defaultGlyphTypeName = g_strdup(str); return(info); }