File: write_xml.c

package info (click to toggle)
ggobi 2.1.9~20091212-3
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 19,340 kB
  • ctags: 5,083
  • sloc: ansic: 57,242; xml: 30,604; cpp: 833; makefile: 355; java: 225; perl: 201; sh: 122; python: 23
file content (584 lines) | stat: -rw-r--r-- 15,291 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
/* write_xml.c */
/*
 * ggobi
 * Copyright (C) AT&T, Duncan Temple Lang, Dianne Cook 1999-2005
 *
 * ggobi is free software; you may use, redistribute, and/or modify it
 * under the terms of the Common Public License, which is distributed
 * with the source code and displayed on the ggobi web site, 
 * www.ggobi.org.  For more information, contact the authors:
 *
 *   Deborah F. Swayne   dfs@research.att.com
 *   Di Cook             dicook@iastate.edu
 *   Duncan Temple Lang  duncan@wald.ucdavis.edu
 *   Andreas Buja        andreas.buja@wharton.upenn.edu
*/

#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!)  Alas, it can't be relied upon for all file conversions,
  because ggobi doesn't read all xgobi-style data files, with
  .lines as a prominent example.  ggobi also doesn't read
  xgobi-style .colors files.
 */
#include <string.h>
#include <strings.h>
#include "writedata.h"


XmlWriteInfo *updateXmlWriteInfo(GGobiData *d, ggobid *gg, XmlWriteInfo *info);

/* if a string contains an ampersand, write it as &amp; ... etc ... --*/
static void
write_xml_string(FILE *f, gchar *str)
{
  gchar *fmtstr = g_markup_printf_escaped("%s", str);
  fprintf(f, fmtstr);
  g_free(fmtstr);
}
static void
write_xml_string_fmt(FILE *f, gchar *fmt, gchar *str)
{
  gchar *fmtstr = g_markup_printf_escaped(fmt, str);
  fprintf(f, fmtstr);
  g_free(fmtstr);
}

gboolean
write_xml (const gchar *filename,  ggobid *gg, XmlWriteInfo *xmlWriteInfo)
{
  FILE *f;
  gboolean ok = false;
/*
 *GGobiData *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;
 GGobiData *d;
 numDatasets = g_slist_length(gg->d);
 // g_printerr ("numDatasets %d\n", numDatasets);

 write_xml_header (f, -1, gg, xmlWriteInfo);

  for(i = 0; i < numDatasets; i++) {
    d = (GGobiData *) 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, GGobiData *d, ggobid *gg, XmlWriteInfo *xmlWriteInfo)
{
  if (d->edge.n && !d->ncols) {
    write_xml_edges(f, d, gg, xmlWriteInfo);
  } else {
    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);
    write_dataset_footer(f, gg, xmlWriteInfo);
  }

  return(true);
}

gboolean
write_xml_header (FILE *f, int numDatasets, ggobid *gg, XmlWriteInfo *xmlWriteInfo)
{
 fprintf(f, "<?xml version=\"1.0\"?>");
 fprintf(f, "\n");
 fprintf(f, "<!DOCTYPE ggobidata SYSTEM \"ggobi.dtd\">");
 fprintf(f, "\n\n");

 if(numDatasets < 0)
    numDatasets = g_slist_length(gg->d);

 fprintf(f, "<ggobidata count=\"%d\">\n", numDatasets);

/* fflush(f);*/

 return(true);
}

gboolean
write_xml_description (FILE *f, ggobid *gg, XmlWriteInfo *xmlWriteInfo)
{
 fprintf(f,"<description>\n");
/*XXX*/
 fprintf(f, "This is XML created by GGobi\n");

 fprintf(f,"</description>\n");

 return(true);
}

gboolean
write_xml_variables (FILE *f, GGobiData *d, ggobid *gg, XmlWriteInfo *xmlWriteInfo)
{
  gint j;

  if (gg->save.column_ind == ALLCOLS) {
    fprintf(f,"<variables count=\"%d\">\n", d->ncols); 
    for(j = 0; j < d->ncols; j++) {
      write_xml_variable (f, d, gg, j, xmlWriteInfo);
      fprintf(f,"\n");
    }
  } else if (gg->save.column_ind == SELECTEDCOLS) {
    /*-- work out which columns to save --*/
    gint *cols = (gint *) g_malloc (d->ncols * sizeof (gint));
    gint ncols = selected_cols_get (cols, d, gg);
    if (ncols == 0)
      ncols = plotted_cols_get (cols, d, gg);
    fprintf(f,"<variables count=\"%d\">\n", ncols); 
    for(j = 0; j < ncols; j++) {
      write_xml_variable (f, d, gg, cols[j], xmlWriteInfo);
      fprintf(f,"\n");
    }

    g_free (cols);
  }

  fprintf(f,"</variables>\n"); 

  return(true);
}

gboolean
write_xml_variable(FILE *f, GGobiData *d, ggobid *gg, gint j,
  XmlWriteInfo *xmlWriteInfo)
{
  vartabled *vt = vartable_element_get (j, d);
  gchar* varname = g_strstrip(
    (gg->save.stage == TFORMDATA) ? 
      ggobi_data_get_transformed_col_name(d, j) : 
      ggobi_data_get_col_name(d, j)
  );
  
  if (vt->vartype == categorical) {
    gint k;
    write_xml_string_fmt(f, "  <categoricalvariable name=\"%s\"", varname);
    if (vt->nickname)
      write_xml_string_fmt(f, " nickname=\"%s\"", vt->nickname);
    fprintf(f, ">\n");
    fprintf(f, "    <levels count=\"%d\">\n", vt->nlevels);
    for (k=0; k<vt->nlevels; k++) {
      fprintf(f, "      <level value=\"%d\">",
              vt->level_values[k]);
      /* Add any needed html/xml markup to level names */
      write_xml_string(f, vt->level_names[k]);
      fprintf(f, "</level>\n");
    }

    fprintf(f, "    </levels>\n");
    fprintf(f, "  </categoricalvariable>");
  } else {
    fprintf(f, "   <");
    if (vt->vartype == real)    fprintf(f, "realvariable");
    if (vt->vartype == integer) fprintf(f, "integervariable");
    if (vt->vartype == counter) fprintf(f, "countervariable");

    write_xml_string_fmt(f, " name=\"%s\"", varname);
    if (vt->nickname) 
      write_xml_string_fmt(f, " nickname=\"%s\"", vt->nickname);
    fprintf(f, "/>");
  } 

  return(true);
}

/*
gboolean
write_edge_record_p (gint i, GGobiData *e, ggobid *gg)
{
 * If e is an edge set, then
 * loop over all other datads and test their rowids to decide
 * whether this case should be drawn.  Use sampled and hidden.
 * XXX  We can't really do this, because we don't know what 
 *      edgeset may have been associated with what nodeset.
  gboolean save_case = true;
  GGobiData *d;
  GSList *l;
  gint a, b;

  if (e->edge.n == e->nrows) {
    for (l = gg->d; l; l=l->next) {
      d = (GGobiData *) l->data;
      endpointsd *endpoints = resolveEdgePoints(e, d);
      if (endpoints) {
        if (!edge_endpoints_get (i, &a, &b, d, endpoints, e) ||
            !d->sampled.els[a] || !d->sampled.els[b] ||
            d->excluded.els[a] || d->excluded.els[b])
        {
          save_case = false;
          break;
        }
      }
    }
  }
  return save_case;
}
*/

static void
writeFloat(FILE *f, double value)
{
  fprintf(f, "<real>%g</real>", value); 
}
static void
writeInt(FILE *f, int value)
{
  fprintf(f, "<int>%d</int>", value); 
}

static void
writeEntry(FILE *f, vartyped vtype, double value) {
  switch (vtype) {
  case categorical:
  case integer:
  case counter:
    writeInt(f, (int) value);
  break;

  default:
    writeFloat (f, value);
  }
}

gboolean
write_xml_records(FILE *f, GGobiData *d, ggobid *gg, XmlWriteInfo *xmlWriteInfo)
{
  gint i, j, m, n = 0;
  vartyped *vartypes;
  vartabled *vt;

  vartypes = (vartyped *) g_malloc(d->ncols * sizeof(vartyped));
  for (j=0; j<d->ncols; j++) {
    vt = vartable_element_get (j, d);
    vartypes[j] = vt->vartype;
  }


  /*-- figure out how many records we're about to save.  --*/
  if (gg->save.row_ind == ALLROWS)
    n = d->nrows;
  else if (gg->save.row_ind == DISPLAYEDROWS)
    n = d->nrows_in_plot;

  fprintf(f, "<records ");
  fprintf(f, "count=\"%d\"", n);
  if(xmlWriteInfo->useDefault) {
/*
   fprintf(f, " glyphSize=\"%s\"", xmlWriteInfo->defaultGlyphSizeName);
   fprintf(f, " glyphType=\"%s\"", xmlWriteInfo->defaultGlyphTypeName);
*/
    fprintf(f, " glyph=\"%s %s\"",
      xmlWriteInfo->defaultGlyphTypeName,
      xmlWriteInfo->defaultGlyphSizeName);
    fprintf(f, " color=\"%s\"", xmlWriteInfo->defaultColorName);
  }

  if (ggobi_data_has_missings(d)) {
    if (gg->save.missing_ind == MISSINGSNA)
      fprintf(f, " missingValue=\"%s\"", "na");
    /*-- otherwise write the "imputed" value --*/
  }
  fprintf(f, ">\n");


  if (gg->save.row_ind == ALLROWS) {
    for (i = 0; i < d->nrows; i++) {
      fprintf(f, "<record");
      write_xml_record (f, d, gg, i, vartypes, xmlWriteInfo);
      fprintf(f, "\n</record>\n");
    }
  } else {  /*-- if displaying visible rows only --*/
    for (i=0; i<d->nrows_in_plot; i++) {
      m = d->rows_in_plot.els[i];
      fprintf(f, "<record");
      write_xml_record (f, d, gg, m, vartypes, xmlWriteInfo);
      fprintf(f, "\n</record>\n");
    }
  }

  fprintf(f, "</records>\n");
  g_free (vartypes);
  return(true);
}


/*
 * I want this to write <edge> records as well as <record> records.
*/
gboolean
write_xml_record (FILE *f, GGobiData *d, ggobid *gg, gint i, 
  vartyped *vartypes, XmlWriteInfo *xmlWriteInfo)
{
  gint j;
  gchar *gstr, *gtypestr = NULL;

  /*-- ids if present --*/
  if (d->rowIds) {
     write_xml_string_fmt(f, " id=\"%s\"", d->rowIds[i]);
  }

  /*-- if the record is hidden, indicate that --*/
  if (d->hidden_now.els[i]) {
    fprintf(f, " hidden=\"true\"");
  }

  /*-- edges if present and requested --*/
  if (gg->save.edges_p && d->edge.n && i < d->edge.n) {
    write_xml_string_fmt(f, " source=\"%s\"", d->edge.sym_endpoints[i].a);
    write_xml_string_fmt(f, " destination=\"%s\"", d->edge.sym_endpoints[i].b);
  }

  if (d->rowlab && d->rowlab->data
      && (gstr = (gchar *) g_array_index (d->rowlab, gchar *, i))) 
  {
    fprintf(f, " label=\"");
    write_xml_string(f, gstr);
    fprintf(f, "\"");
  }

  if (!xmlWriteInfo->useDefault ||
      xmlWriteInfo->defaultColor != d->color.els[i])
  {
    fprintf(f, " color=\"%d\"", d->color.els[i]);
  }

/*
  fprintf(f, " glyphSize=\"%d\"", d->glyph[i].size);
  fprintf(f, " glyphType=\"%d\"", d->glyph[i].type);
*/
  if (!xmlWriteInfo->useDefault ||
     xmlWriteInfo->defaultGlyphType != d->glyph.els[i].type ||
     xmlWriteInfo->defaultGlyphSize != d->glyph.els[i].size)
  {
    switch (d->glyph.els[i].type) {
      case PLUS:
        gtypestr = "plus";
      break;
      case X:
        gtypestr = "x";
      break;
      case OR:
        gtypestr = "or";
      break;
      case FR:
        gtypestr = "fr";
      break;
      case OC:
        gtypestr = "oc";
      break;
      case FC:
        gtypestr = "fc";
      break;
      case DOT_GLYPH:
        gtypestr = ".";
      break;
      case UNKNOWN_GLYPH:
      default:
        gtypestr=NULL;
      break;
    }

    fprintf (f, " glyph=\"%s %d\"", gtypestr, d->glyph.els[i].size);
  }

  fprintf(f, ">\n");

  if (gg->save.column_ind == ALLCOLS && d->ncols > 0) {
    for(j = 0; j < d->ncols; j++) {
      /*-- if missing, figure out what to write --*/
      if (ggobi_data_has_missings(d) && 
          ggobi_data_is_missing(d, i, j) && 
          gg->save.missing_ind != MISSINGSIMPUTED)
      {
          fprintf (f, "<na/>");
      } else {  /*-- if not missing, just write the data --*/
        writeEntry (f, vartypes[j],
          (gg->save.stage == TFORMDATA) ? d->tform.vals[i][j] :
                                          d->raw.vals[i][j]);
      }
      if (j < d->ncols-1 )
        fprintf(f, " ");
     }
  } else if (gg->save.column_ind == SELECTEDCOLS && d->ncols > 0) {
    /*-- work out which columns to save --*/
    gint *cols = (gint *) g_malloc (d->ncols * sizeof (gint));
    gint k, ncols = selected_cols_get (cols, d, gg);
    if (ncols == 0)
      ncols = plotted_cols_get (cols, d, gg);
    for(k = 0; k < ncols; k++) {
      j = cols[k];
      if (ggobi_data_is_missing(d, i, j) &&
        gg->save.missing_ind != MISSINGSIMPUTED)
      {
          fprintf (f, "<na/>");
      } else {

        writeEntry (f, vartypes[j],
          (gg->save.stage == TFORMDATA) ? d->tform.vals[i][j] :
                                          d->raw.vals[i][cols[j]]);
      } 
      if (j < ncols-1 )
        fprintf(f, " ");
     }
     g_free (cols);
   }

 return (true);
}

gboolean
write_xml_edges (FILE *f, GGobiData *d, ggobid *gg, XmlWriteInfo *xmlWriteInfo)
{
  gint i, j;
  vartyped *vartypes;
  vartabled *vt;

  if (d->edge.n < 1)
    return(true);
  /*
  fprintf(f, "<edges count=\"%d\" name=\"%s\">\n", d->edge.n,
  d->name); There seems to be a need to write the defaults in the case
  where we started with ascii data and added edges; in this case the
  edges are written as <edges> rather than as a new datad, and maybe
  that's the problem.   dfs
  */
  fprintf(f, "<edges count=\"%d\" ", d->edge.n);
  write_xml_string_fmt(f, "name=\"%s\" ", (gchar *)d->name);
  fprintf(f, "color=\"%d\" glyphType=\"%s\" glyphSize=\"%s\">\n",
    xmlWriteInfo->defaultColor,
    xmlWriteInfo->defaultGlyphTypeName, 
    xmlWriteInfo->defaultGlyphSizeName);

  vartypes = (vartyped *) g_malloc(d->ncols * sizeof(vartyped));
  for (j=0; j<d->ncols; j++) {
    vt = vartable_element_get (j, d);
    vartypes[j] = vt->vartype;
  }

  for(i = 0; i < d->edge.n; i++) {
    fprintf(f, "<edge");
    write_xml_record (f, d, gg, i, vartypes, xmlWriteInfo);
    fprintf(f, "</edge>\n");
  }
  fprintf(f, "</edges>\n");

  g_free (vartypes);
 return(true);
}

gboolean
write_dataset_header (FILE *f, GGobiData *d, ggobid *gg, XmlWriteInfo *xmlWriteInfo)
{


  fprintf(f,"<data ");
  write_xml_string_fmt(f, "name=\"%s\"", (gchar *)d->name);
  fprintf(f,">\n");

  return(true);
}

gboolean
write_dataset_footer(FILE *f, ggobid *gg, XmlWriteInfo *xmlWriteInfo)
{
 fprintf(f,"</data>\n");
 return(true);
}

gboolean
write_xml_footer(FILE *f, ggobid *gg, XmlWriteInfo *xmlWriteInfo)
{
 fprintf(f,"</ggobidata>\n");
 return(true);
}

XmlWriteInfo *
updateXmlWriteInfo(GGobiData *d, ggobid *gg, XmlWriteInfo *info)
{
  int i, n, numGlyphSizes;
  gint *colorCounts, *glyphTypeCounts, *glyphSizeCounts, count;
  gchar *str;
  gint ncolors = gg->activeColorScheme->n;

  colorCounts = g_malloc0(sizeof(gint) * ncolors);
  glyphTypeCounts = g_malloc0(sizeof(gint) * UNKNOWN_GLYPH);
  numGlyphSizes = NGLYPHSIZES;
  glyphSizeCounts = g_malloc0(sizeof(gint) * numGlyphSizes);

  n = GGOBI(nrecords)(d);
  for(i = 0 ; i < n ; i++) {
    colorCounts[d->color.els[i]]++;
    glyphSizeCounts[d->glyph.els[i].size]++;
    glyphTypeCounts[d->glyph.els[i].type]++;
  }

  count = -1;
  for(i = 0 ; i < 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 = (gchar *) GGOBI(getGlyphTypeName)(info->defaultGlyphType);  
  info->defaultGlyphTypeName = g_strdup(str);

  return(info);
}