aboutsummaryrefslogtreecommitdiff
path: root/src/rdf.cc
blob: d6bf8ac01066c2efa4d2a9adc57da75a94613971 (plain)
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
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
// $Header$

#include <cassert>
#include <cctype>
#include <cerrno>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <string>
#include <sstream>

#include <unistd.h>

#include "cctk.h"
#include "cctk_Parameters.h"
#include "cctk_Version.h"
#include "util_String.h"
#include "util_Network.h"

#include "rdf.hh"


// number of space chars for indentation
#define NUM_INDENT_SPACES 2

namespace Formaline
{

  using namespace std;


  static bool
  is_clean_for_shell (char const * str);

#if 0
  static list<string>
  parse (char const * const key, string& node);
#endif


  rdf::
  rdf (char const * const id,
       enum state const st)
    : storage (st)
  {
    //
    // This code was copied over from function Formaline_AnnounceInitial()
    // in announce.cc and modified here to create a valid RDF/XML (rather then
    // an unstructured plain XML document).
    //
    DECLARE_CCTK_PARAMETERS;

    if (verbose) CCTK_INFO ("Announcing initial RDF metadata information");

    //
    // RDF/XML document header with some namespace definitions
    //
    msgbuf
<< "<?xml version=\"1.0\" encoding=\"utf-8\"?>" << endl
<< "<!DOCTYPE owl [" << endl
// << "\t<!ENTITY dc   'http://purl.org/dc/elements/1.1/'>" << endl
// << "\t<!ENTITY doap 'http://usefulinc.com/ns/doap#'>" << endl
// << "\t<!ENTITY foaf 'http://xmlns.com/foaf/0.1/'>" << endl
<< "\t<!ENTITY rdf  'http://www.w3.org/1999/02/22-rdf-syntax-ns#'>" << endl
<< "\t<!ENTITY xsd  'http://www.w3.org/2001/XMLSchema#'>" << endl
// << "\t<!ENTITY rdfs 'http://www.w3.org/2000/01/rdf-schema#'>" << endl
<< "\t<!ENTITY cctk 'http://www.cct.lsu.edu/~dstark/cctk/0.1/'>" << endl
// << "\t<!ENTITY form 'http://www.aei.mpg.de/form#'>" << endl
<< "]>" << endl
<< "<rdf:RDF xmlns:rdf=\"&rdf;\""
<< endl << "\txmlns:xsd=\"&xsd;\""
// << endl << "\txmlns:dc=\"&dc;\""
// << endl << "\txmlns:doap=\"&doap;\""
// << endl << "\txmlns:foaf=\"&foaf;\""
// << endl << "\txmlns:rdfs=\"&rdfs;\""
<< endl << "\txmlns:cctk=\"&cctk;\""
// << endl << "\txmlns:form=\"&form;\""
<< endl << ">" << endl << endl;

    //
    // general metadata with inlined attribute values
    //
    const string jobID = clean (string (id));
    char hostbuf[512] = "";
    Util_GetHostName (hostbuf, sizeof (hostbuf));
    const string host = clean (hostbuf);
    const cGH* const cctkGH = NULL;
    const int nprocs = CCTK_nProcs (cctkGH);
#if 0
    const string user = clean (CCTK_RunUser());
#else
    const string user = clean (getenv ("USER"));
#endif
    char** argv;
    CCTK_CommandLine (&argv);
    const string executable = clean (argv[0]);
    char parfilebuf[512] = "";
    CCTK_ParameterFilename (sizeof (parfilebuf), parfilebuf);
    const string parfile = clean (parfilebuf);
    const string version = clean (CCTK_FullVersion ());
    ostringstream compiled_at_buf;
    compiled_at_buf << CCTK_CompileDate () << " " << CCTK_CompileTime ();
    const string compiled_at (clean (compiled_at_buf.str()));
    char rundatebuf[32] = "";
    char runtimebuf[32] = "";
    Util_CurrentDate (sizeof (rundatebuf), rundatebuf);
    Util_CurrentTime (sizeof (runtimebuf), runtimebuf);
    ostringstream started_at_buf;
    started_at_buf << rundatebuf << " " << runtimebuf;
    const string started_at (clean (started_at_buf.str()));
    char cwdbuf[512];
    getcwd (cwdbuf, sizeof (cwdbuf));
    const string cwd (clean (cwdbuf));


    msgbuf
<< "<cctk:Simulation rdf:about=\"#" << jobID << "\">" << endl
<< "\t<cctk:simulationID>"  << jobID       << "</cctk:simulationID>" << endl
<< "\t<cctk:host>"          << host        << "</cctk:host>" << endl
<< "\t<cctk:nProcs>"        << nprocs      << "</cctk:nProcs>" << endl
<< "\t<cctk:user>"          << user        << "</cctk:user>" << endl
<< "\t<cctk:executable>"    << executable  << "</cctk:executable>" << endl
<< "\t<cctk:parameterFile>" << parfile     << "</cctk:parameterFile>" << endl
<< "\t<cctk:hasVersion>"    << version     << "</cctk:hasVersion>" << endl
<< "\t<cctk:compiledAt>"    << compiled_at << "</cctk:compiledAt>" << endl
<< "\t<cctk:startedAt>"     << started_at  << "</cctk:startedAt>" << endl
<< "\t<cctk:cwd>"           << cwd         << "</cctk:cwd>" << endl
<< "</cctk:Simulation>" << endl << endl;


    //
    // metadata as references to other nodes
    //
    msgbuf << "<cctk:Simulation rdf:about=\"#" << jobID << "\">" << endl
           << "\t<cctk:hasThornList rdf:resource=\"#ThornList\"/>" << endl
           << "\t<cctk:hasParameterFile rdf:resource=\"#ParameterFile\"/>" << endl
           << "</cctk:Simulation>" << endl << endl;

    // store thorn list
    msgbuf << "<cctk:ThornList rdf:about=\"#ThornList\">" << endl;
    const int numthorns = CCTK_NumCompiledThorns ();
    for (int thorn = 0; thorn < numthorns; ++ thorn) {
      const char* const thornname = CCTK_CompiledThorn (thorn);

      msgbuf << "\t<cctk:containsThorn rdf:resource=\"#Thorns/"
             << thornname << "\"/>" << endl;
    }
    msgbuf << "</cctk:ThornList>" << endl << endl;

    // store parameter file contents
    msgbuf << "<cctk:ParameterFile rdf:about=\"#ParameterFile\">" << endl
           << "\t<rdf:value>";
    ifstream file (parfile.c_str());
    char c;
    ostringstream filebuf;
    while (filebuf and file.get (c)) filebuf.put (c);
    file.close();
    msgbuf << clean (filebuf.str());
    filebuf.clear();
    msgbuf << "</rdf:value>" << endl
           << "</cctk:ParameterFile>" << endl << endl;

    // store all parameters which have been set in the parfile
    msgbuf
<< "<!-- ============================================================ -->" << endl
<< "<!-- thorn graphs with their parameters                           -->" << endl
<< "<!-- ============================================================ -->" << endl;
    ostringstream parambuf;
    parambuf
<< "<!-- ============================================================ -->" << endl
<< "<!-- list of parameters and their values                          -->" << endl
<< "<!-- ============================================================ -->" << endl;

    const bool list_all_parameters = CCTK_Equals (out_save_parameters, "all");

    for (int thorn = 0; thorn < numthorns; ++ thorn) {
      const char* const thornname = CCTK_CompiledThorn (thorn);

      msgbuf << "<cctk:Thorn rdf:about=\"#Thorns/"
             << thornname << "\">" << endl;
      msgbuf << "\t<cctk:hasName>" << thornname << "</cctk:hasName>" << endl;

      // skip parameters that belong to inactive thorns
      const bool is_active = CCTK_IsThornActive (thornname);
      msgbuf << "\t<cctk:isActive>" << (is_active ? "true" : "false")
             << "</cctk:isActive>" << endl;

      // loop over all parameters of this thorn (if it is active)
      if (is_active) {
        for (int first = 1; ; first = 0) {
          char* fullname = NULL;
          const cParamData* pdata = NULL;

          // get the first/next parameter
          const int ierr = CCTK_ParameterWalk (first, thornname,&fullname,&pdata);
          assert (ierr >= 0);
          if (ierr > 0) break;

          msgbuf << "\t<cctk:hasParameter rdf:resource=\"#Parameters/"
                 << pdata->thorn << "/" << pdata->name << "\"/>" << endl;

          // get its value
          const void* const pvalue
            = CCTK_ParameterGet (pdata->name, pdata->thorn, NULL);
          assert (pvalue);

          if (pdata->n_set or list_all_parameters) {
            const char* paramtype;
            ostringstream paramvaluebuf;

            switch (pdata->type) {
              case PARAMETER_BOOLEAN:
              {
                paramtype = "BooleanParameter";
                const CCTK_INT v = *static_cast<const CCTK_INT*> (pvalue);
                paramvaluebuf << (v ? "true" : "false");
              }
              break;

              case PARAMETER_INT:
              {
                paramtype = "IntegerParameter";
                const CCTK_INT v = *static_cast<const CCTK_INT*> (pvalue);
                paramvaluebuf << v;
              }
              break;

              case PARAMETER_REAL:
              {
                paramtype = "RealParameter";
                CCTK_REAL const v = *static_cast<const CCTK_REAL*> (pvalue);
                paramvaluebuf << v;
              }
              break;

              case PARAMETER_KEYWORD:
              {
                paramtype = "KeywordParameter";
                const char* const v = *static_cast<const char* const*> (pvalue);
                paramvaluebuf << clean (v);
              }
              break;

              case PARAMETER_STRING:
              {
                paramtype = "StringParameter";
                const char* const v = *static_cast<const char* const*> (pvalue);
                paramvaluebuf << clean (v);
              }
              break;

              default: assert (0 and "invalid parameter type");

            } // switch (pdata->type)

            parambuf << "<cctk:" << paramtype << " rdf:about=\"#Parameters/"
                     << pdata->thorn << "/" << pdata->name << "\">" << endl
                     << "\t<cctk:hasName>" << fullname
                     << "</cctk:hasName>" << endl
                     << "\t<cctk:hasValue>" << paramvaluebuf.str()
                     << "</cctk:hasValue>" << endl
                     << "</cctk:" << paramtype << "Parameter>" << endl;

          } // if (pdata->n_set or list_all_parameters)

          free (fullname);

        } // loop over all parameters of this thorn
      } // if (is_active)

      msgbuf << "</cctk:Thorn>" << endl;

    } // loop over all thorns

    // FIXME: is there some better method for concatenation ??
    msgbuf << endl << parambuf.str();

    //
    // close the RDF/XML document
    //
    msgbuf << endl << "</rdf:RDF>" << endl;
  }



  rdf::
  ~ rdf ()
  {
    DECLARE_CCTK_PARAMETERS;

    string const socket_script = "socket-client.pl";
    string const socket_data = "socket-data";



    // Write the data
    string const msgstr = msgbuf.str();

    ostringstream databuf;
    databuf << "POST HTTP/1.0 200\r\n"
            << "Content-Type: text/xml\r\n"
            << "Content-Length: " << msgstr.length() << "\r\n"
            << "\r\n"
            << msgstr
            << "\r\n"
            << "\r\n";
    string const datastr = databuf.str();

    ostringstream datafilenamebuf;
    datafilenamebuf << out_dir << "/" << socket_data;
    string const datafilenamestr = datafilenamebuf.str();
    char const * const datafilename = datafilenamestr.c_str();

    ofstream datafile;
    datafile.open (datafilename, ios::out);
    datafile << datastr;
    datafile.close ();



    // Write the script
    ostringstream scriptbuf;
    scriptbuf
<< "#! /usr/bin/perl -w" << endl
<< endl
<< "use strict;" << endl
<< "use Socket;" << endl
<< endl
<< "my $input = '" << datafilename << "';" << endl
<< "my @hostlist = (";

    // NUM_RDF_ENTRIES must match the size of the
    // Formaline::rdf_hostname and Formaline::rdf_port parameter arrays
#define NUM_RDF_ENTRIES 5

    // add all array parameters which have been set
    for (int i = 0; i < NUM_RDF_ENTRIES; i++) {
      if (*rdf_hostname[i]) {
        if (i) scriptbuf << "," << endl << "                ";
        scriptbuf << "'" << rdf_hostname[i] << ":" << rdf_port[i] << "'";
      }
    }
    scriptbuf
<< ");" << endl
<< endl
<< "foreach my $entry (@hostlist) {" << endl
<< "  next if ($entry !~ /^(.+):(\\d+)$/);" << endl
<< endl
<< "  my $host = $1;" << endl
<< "  my $port = $2;" << endl
<< endl
<< "  my $SH;" << endl
<< endl
<< "  # try to use IO::Socket::INET if the module exists;" << endl
<< "  # it accepts a timeout for its internal connect call" << endl
<< "  eval 'use IO::Socket::INET;" << endl
<< endl
<< "        $SH = IO::Socket::INET->new (PeerAddr => $host," << endl
<< "                                     PeerPort => $port," << endl
<< "                                     Proto    => \\'tcp\\'," << endl
<< "                                     Type     => SOCK_STREAM," << endl
<< "                                     Timeout  => 0.2);';" << endl
<< "  # if that failed, fall back to making the standard socket/connect calls" << endl
<< "  # (with their built-in fixed timeout)" << endl
<< "  if ($@) {" << endl
<< "    my $iaddr = inet_aton ($host);" << endl
<< "    next if (not $iaddr);" << endl
<< "" << endl
<< "    socket ($SH, PF_INET, SOCK_STREAM, getprotobyname ('tcp'));" << endl
<< "    my $sin = sockaddr_in ($port, $iaddr);" << endl
<< "    connect ($SH, $sin) || next;" << endl
<< "  }" << endl
<< endl
<< "  # send off the data" << endl
<< "  if (defined $SH) {" << endl
<< "    open (my $FH, '<' . $input);" << endl
<< "    print $SH $_ while (<$FH>);" << endl
<< "    close $FH;" << endl
<< "    close $SH;" << endl
<< "  }" << endl
<< "}" << endl
<< endl;
    string const scriptstr = scriptbuf.str();

    ostringstream scriptfilenamebuf;
    scriptfilenamebuf << out_dir << "/" << socket_script;
    string const scriptfilenamestr = scriptfilenamebuf.str();
    char const * const scriptfilename = scriptfilenamestr.c_str();

    ofstream scriptfile;
    scriptfile.open (scriptfilename, ios::out);
    scriptfile << scriptstr;
    scriptfile.close ();



    // Check that the file name is sane
    if (! is_clean_for_shell (scriptfilename))
    {
      static bool did_complain = false;
      if (! did_complain)
      {
        did_complain = true;
        CCTK_WARN (1, "Strange character in file name -- not calling system()");
        return;
      }
    }



    // Make the script executable
    ostringstream chmodbuf;
    chmodbuf << "chmod a+x " << scriptfilenamestr
             << " < /dev/null > /dev/null 2> /dev/null";
    string const chmodstr = chmodbuf.str();
    char const * const chmod = chmodstr.c_str();
    system (chmod);



    bool my_use_relay_host = use_relay_host;
    char const * my_relay_host = 0;
    if (my_use_relay_host)
    {
      my_relay_host = relay_host;
      if (strcmp (my_relay_host, "") == 0)
      {
        // Determine a good relay host
        char run_host [1000];
        Util_GetHostName (run_host, sizeof run_host);
        if (strncmp (run_host, "ic", 2) == 0 && strlen (run_host) == 6)
        {
          // Peyote or Lagavulin
          int const node = atoi (run_host + 2);
          if (node < 192)
          {
            // Peyote
            my_relay_host = "peyote";
          }
          else
          {
            // Lagavulin
            my_relay_host = "lagavulin";
          }
        }
        else if (strncmp (run_host, "mike", 4) == 0 && strlen (run_host) == 7)
        {
          // Supermike
          my_use_relay_host = false;
        }
        else
        {
          // Don't know a good relay host; try without
          my_use_relay_host = false;
        }

        if (verbose)
        {
          if (my_use_relay_host)
          {
            CCTK_VInfo (CCTK_THORNSTRING,
                        "Using \"%s\" as relay host", my_relay_host);
          }
          else
          {
            CCTK_INFO ("Announcing without relay host");
          }
        }
      }
    }

    if (my_use_relay_host)
    {
      // Check that the relay host name is sane
      if (! is_clean_for_shell (my_relay_host))
      {
        static bool did_complain = false;
        if (! did_complain)
        {
          did_complain = true;
          CCTK_WARN (1, "Strange character in relay host name -- not calling system()");
          return;
        }
      }
    }



    char cwd[10000];
    if (my_use_relay_host)
    {
      // Get the current directory
      char * const cwderr = getcwd (cwd, sizeof cwd);
      if (cwderr == NULL) {
        static bool did_complain = false;
        if (! did_complain)
        {
          did_complain = true;
          CCTK_WARN (1, "Cannot determine current working directory");
          return;
        }
      }

      // Check that the current directory name is sane
      if (! is_clean_for_shell (cwd))
      {
        static bool did_complain = false;
        if (! did_complain)
        {
          did_complain = true;
          CCTK_WARN (1, "Strange character in current directory -- not calling system()");
          return;
        }
      }
    }
    else
    {
      cwd[0] = '\0';
    }



    // Send the data
    ostringstream cmdbuf;
    if (my_use_relay_host)
    {
      cmdbuf << "ssh " << my_relay_host << " '"
             << "cd " << cwd << " && ";
    }
    cmdbuf << scriptfilenamestr << " < /dev/null > /dev/null 2> /dev/null";
    if (my_use_relay_host)
    {
      cmdbuf << "'";
    }
    string const cmdstr = cmdbuf.str();
    char const * const cmd = cmdstr.c_str();

    int const ierr = system (cmd);
    if (ierr != 0)
    {
      static bool did_complain = false;
      if (! did_complain)
      {
        did_complain = true;
        CCTK_WARN (1, "Failed to send data to the rdf");
      }
    }

//    remove (datafilename);
    remove (scriptfilename);
  }


  void rdf::
  store (char const * const key,
         bool const value)
  {
#if 0
    ostringstream valuebuf;
    valuebuf << (value ? "true" : "false");

    string node;
    list<string> const keys = parse (key, node);
    string indent_string (NUM_INDENT_SPACES, ' ');
    for (list<string>::const_iterator lsi = keys.begin();
         lsi != keys.end(); ++ lsi)
    {
      msgbuf << indent_string << "<form:" << * lsi << ">" << endl;
      indent_string.append (NUM_INDENT_SPACES, ' ');
    }

    msgbuf << indent_string
           << "<form:" << node << " rdf:datatype=\"&xsd;boolean\">"
           << clean (valuebuf.str())
           << "</form:" << node << ">" << endl;

    for (list<string>::const_reverse_iterator lsi = keys.rbegin();
         lsi != keys.rend(); ++ lsi)
    {
      indent_string.erase(0, NUM_INDENT_SPACES);
      msgbuf << indent_string << "</form:" << * lsi << ">" << endl;
    }
    msgbuf << endl;
#endif
  }



  void rdf::
  store (char const * const key,
         CCTK_INT const value)
  {
#if 0
    ostringstream valuebuf;
    valuebuf << value;

    string node;
    list<string> const keys = parse (key, node);
    string indent_string (NUM_INDENT_SPACES, ' ');
    for (list<string>::const_iterator lsi = keys.begin();
         lsi != keys.end(); ++ lsi)
    {
      msgbuf << indent_string << "<form:" << * lsi << ">" << endl;
      indent_string.append (NUM_INDENT_SPACES, ' ');
    }

    msgbuf << indent_string
           << "<form:" << node << " rdf:datatype=\"&xsd;integer\">"
           << clean (valuebuf.str())
           << "</form:" << node << ">" << endl;

    for (list<string>::const_reverse_iterator lsi = keys.rbegin();
         lsi != keys.rend(); ++ lsi)
    {
      indent_string.erase(0, NUM_INDENT_SPACES);
      msgbuf << indent_string << "</form:" << * lsi << ">" << endl;
    }
    msgbuf << endl;
#endif
  }



  void rdf::
  store (char const * const key,
         CCTK_REAL const value)
  {
#if 0
    int const prec = numeric_limits<CCTK_REAL>::digits10;
    ostringstream valuebuf;
    valuebuf << setprecision(prec) << value;

    string node;
    list<string> const keys = parse (key, node);
    string indent_string (NUM_INDENT_SPACES, ' ');
    for (list<string>::const_iterator lsi = keys.begin();
         lsi != keys.end(); ++ lsi)
    {
      msgbuf << indent_string << "<form:" << * lsi << ">" << endl;
      indent_string.append (NUM_INDENT_SPACES, ' ');
    }

    msgbuf << indent_string
           << "<form:" << node << " rdf:datatype=\"&xsd;double\">"
           << clean (valuebuf.str())
           << "</form:" << node << ">" << endl;

    for (list<string>::const_reverse_iterator lsi = keys.rbegin();
         lsi != keys.rend(); ++ lsi)
    {
      indent_string.erase(0, NUM_INDENT_SPACES);
      msgbuf << indent_string << "</form:" << * lsi << ">" << endl;
    }
    msgbuf << endl;
#endif
  }



  void rdf::
  store (char const * const key,
         char const * const value)
  {
#if 0
    // don't store keys with empty string values
    if (not *value) return;

    ostringstream valuebuf;
    valuebuf << value;

    string node;
    list<string> const keys = parse (key, node);
    string indent_string (NUM_INDENT_SPACES, ' ');
    for (list<string>::const_iterator lsi = keys.begin();
         lsi != keys.end(); ++ lsi)
    {
      msgbuf << indent_string << "<form:" << * lsi << ">" << endl;
      indent_string.append (NUM_INDENT_SPACES, ' ');
    }

    msgbuf << indent_string
           // FIXME: is <string> the default datatype for RDF objects ??
           << "<form:" << node << ">" // " rdf:datatype=\"&xsd;string\">"
           << clean (valuebuf.str())
           << "</form:" << node << ">" << endl;

    for (list<string>::const_reverse_iterator lsi = keys.rbegin();
         lsi != keys.rend(); ++ lsi)
    {
      indent_string.erase(0, NUM_INDENT_SPACES);
      msgbuf << indent_string << "</form:" << * lsi << ">" << endl;
    }
    msgbuf << endl;
#endif
  }



  string rdf::
  clean (string const & txt)
    const
  {
    ostringstream buf;

    for (string::const_iterator p = txt.begin(); p != txt.end(); ++ p)
    {
      switch (* p)
      {
      case '<': buf << "&lt;"; break;
      case '&': buf << "&amp;"; break;
      default: buf << * p;
      }
    }

    return buf.str();
  }



  static bool
  is_clean_for_shell (char const * const str)
  {
    for (char const * p = str; * p; ++ p)
    {
      if (! isalnum (* p))
      {
        // Allow only certain characters
        switch (* p)
        {
        case '+':
        case ',':
        case '-':
        case '.':
        case '/':
        case ':':
        case '_':
        case '~':
          break;
        default:
          // We don't like this character
          return false;
        }
      }
    }
    return true;
  }


#if 0
  static list<string>
  parse (char const * const key, string& node)
  {
    assert (key);
    string str(key);
    list<string> strs;
    size_t p = 0;
    for (;;) {
      size_t const s = str.find ("/", p);
      if (s == string::npos) break;
      strs.push_back (str.substr (p, s - p));
      p = s + 1;
    }
    node = str.substr (p);
    return strs;
  }
#endif


} // namespace Formaline