~ubuntu-branches/ubuntu/hardy/steam/hardy

« back to all changes in this revision

Viewing changes to server/libraries/XMLCodegen.pmod

  • Committer: Bazaar Package Importer
  • Author(s): Alain Schroeder
  • Date: 2006-11-21 16:03:12 UTC
  • mfrom: (2.1.4 feisty)
  • Revision ID: james.westby@ubuntu.com-20061121160312-nf96y6nihzsyd2uv
Tags: 2.2.31-3
Add patch to prevent inconsistent data after shutdown.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* Copyright (C) 2000-2005  Thomas Bopp
 
1
/* Copyright (C) 2000-2006  Thomas Bopp
2
2
 *
3
3
 *  This program is free software; you can redistribute it and/or modify
4
4
 *  it under the terms of the GNU General Public License as published by
39
39
"object XML = OBJ(\"/scripts/xmltools.pike\");\n"+
40
40
"mapping selection = ([ ]);\n"+
41
41
"static mapping   configs = ([ ]);\n"+
42
 
"object obj, active,last, back;\n\n"+
 
42
"object obj, active,last;\n\n"+
 
43
"mixed err;\n"+
43
44
"object tuser;\n\n"+
44
45
"mapping read_configs(object obj)\n"+
45
46
"{\n"+
47
48
"         string configStr = obj->get_content();\n"+
48
49
"         NodeXML cfg = parse_data(configStr);\n"+
49
50
"         if ( objectp(cfg) ) {\n"+
50
 
"           array(NodeXML) cfgs = cfg->get_sibling();\n"+
 
51
"           array(NodeXML) cfgs = cfg->get_children();\n"+
51
52
"           foreach(cfgs, object cnf)\n"+
52
53
"             configs[cnf->get_name()] = cnf->get_data();\n"+
53
54
"         }\n"+
56
57
"string class_id_to_name(int id)\n"+
57
58
"{\n"+
58
59
"    object factory = _Server->get_factory(id);\n"+
59
 
"    if ( objectp(factory) )\n"+
 
60
"    if ( id & CLASS_SCRIPT ) \n"+
 
61
"        return \"'Script'\";\n"+
 
62
"    else if ( objectp(factory) )\n"+
60
63
"        return \"'\"+factory->get_class_name() + \"'\";\n"+
61
 
"    else\n"+
62
 
"       return \"'Object'\";\n"+
 
64
"    return \"'Object'\";\n"+
63
65
"}\n\n"+
64
66
"string xmlify(object __obj, mapping vars)\n"+
65
67
"{\n"+
66
68
"    string xml=\"<?xml version='1.0' encoding='utf-8'?>\";\n"+
67
69
"    string out;\n"+
 
70
"    object back;\n"+
68
71
"    mixed res;\n\n"+
69
72
"    if ( vars->this_user )\n"+
70
73
"        tuser = find_object(vars->this_user);\n"+
159
162
            return "/* This is not an integer: "+node->data + "*/";
160
163
        return node->data;
161
164
    }
 
165
    else if ( node->name == "key" ) {
 
166
        return "data";
 
167
    }
 
168
    else if ( node->name == "value" ) {
 
169
        return "value";
 
170
    }
 
171
    else if ( node->name == "expression" )
 
172
        return node->data;
162
173
    else if ( node->name == "var" )
163
174
        return get_var(node);
164
175
    return ::unserialize(node);
248
259
{
249
260
    string pcode = "";
250
261
    string fcall = generate_func(node, "active");
251
 
    pcode += "    if ( catch(res = " + fcall + ") )\n";
252
 
    pcode += "    { res = \"\";  xml += \"<!-- error calling function: "+replace(fcall,"\"","\\\"")+"-->\"; }\n";
 
262
    pcode += "    if ( err=catch(res = " + fcall + ") )\n";
 
263
    pcode += "    { xml += \"<!-- error calling function: "+replace(fcall,"\"","\\\"")+"\\n\"+err[0]+\"-->\"; werror(\"xmlgen: %s\\n%O\\n\", err[0],err[1]); res=\"\";}\n";
253
264
    return pcode;
254
265
}
255
266
 
326
337
      m = node->get_node("structure");
327
338
    else {
328
339
      // handle def: compatibility
329
 
      defs = m->get_nodes("def");
330
 
      foreach(defs, object def) {
331
 
        string fname = "/xsl_tags/"+def->data+".xml";
332
 
        string str = __reader(fname);
333
 
        pcode += " // import def: " + def->data+"\n";
334
 
        if ( !stringp(str) )
335
 
          pcode += "    // " + fname + " not found !\n";
336
 
        else {
337
 
          if ( !def->replace_node("<xml>"+str+"</xml>", "*") )
338
 
            pcode += "/* failed to parse import: " + fname + "*/\n";
339
 
        }
340
 
        
341
 
        if ( def->data == "objects" )
342
 
          pcode += "/* \n"+str+"\n*/";
343
 
        
 
340
      defs = copy_value(m->get_nodes("def"));
 
341
      foreach(defs, object def) 
 
342
      {
 
343
          string fname = "/xsl_tags/"+def->data+".xml";
 
344
          string str = __reader(fname);
 
345
          pcode += " // import def: " + def->data+"\n";
 
346
          if ( !stringp(str) )
 
347
              pcode += "    // " + fname + " not found !\n";
 
348
          else {
 
349
              mixed result = def->replace_node("<xml>"+str+"</xml>", "*");
 
350
              if ( !arrayp(result) )
 
351
                  pcode += "/* failed to parse import: " + fname + "*/\n";
 
352
          }
344
353
      }
345
 
#if 0
346
354
      pcode += "/*\n";
347
355
      pcode += m->get_xml();
348
356
      pcode += "*/\n";
349
 
#endif
350
357
    }
351
358
    if ( !objectp(m) )
352
359
      return "";
371
378
   
372
379
    object attr = node->get_node("attribute");
373
380
    object call = node->get_node("call");
 
381
    object ifnode = node->get_node("if");
 
382
 
 
383
    if ( objectp(ifnode) ) 
 
384
        pcode += " if ( " + ifnode->data + " ) {\n";
 
385
 
 
386
 
374
387
    if ( objectp(attr) )
375
388
        pcode += generate_attribute(attr);
376
389
    else if ( objectp(call) )
383
396
          pcode += generate_tag(t);
384
397
        }
385
398
        pcode += "    xml += \"</"+tag+">\";\n";
 
399
        if ( objectp(ifnode) )
 
400
            pcode += " }\n";
386
401
        return pcode;
387
402
      }
388
403
      else
394
409
    pcode += generate_show(node->get_node("show"));
395
410
 
396
411
    // xml wandlung
 
412
 
397
413
    pcode += "    xml += \"<" + tag + ">\" + res + \"</"+tag+">\";\n";
 
414
    if ( objectp(ifnode) )
 
415
        pcode += " }\n";
 
416
 
398
417
    return pcode;
399
418
}
400
419
 
412
431
            steam_error("Unable to find factory for " + type);   
413
432
    }
414
433
    
415
 
    pcode += "    if ( (active->get_object_class() & " + t + ") ) {\n";
 
434
    pcode += "    if ( objectp(active) && (active->get_object_class() & " + t + ") ) {\n";
416
435
    foreach(node->get_nodes("tag"), object tag) {
417
436
        pcode += generate_tag(tag);
418
437
    }
429
448
        xmlDom.Node n = xmlDom.RootNode("root", ([ ]));
430
449
        xmlDom.Node s = xmlDom.Node("structure", ([ ]), n, n);
431
450
        foreach(node, object sn)
432
 
          s->add_children(sn->get_sibling());
 
451
          s->add_children(sn->get_children());
433
452
        node = s;
434
453
        pcode += "/* new structure created: \n"+node->dump()+"*/\n";
435
454
    }
440
459
            node->get_xml());
441
460
    
442
461
    if ( objectp(node->get_node("class")) ) 
443
 
        pcode += "    xml += \"<Object type=\"+class_id_to_name(active->get_object_class())+\" selected=\"+(selection[active] ? \"'true'\":\"'false'\")+\">\";\n";
 
462
        pcode += "    if ( objectp(active) ) { xml += \"<Object type=\"+class_id_to_name(active->get_object_class())+\" selected=\"+(selection[active] ? \"'true'\":\"'false'\")+\">\";\n";
444
463
 
445
 
    foreach(node->sibling, object n) {
 
464
    foreach(node->children, object n) {
446
465
        if ( n->name == "class" ) {
447
466
            pcode += generate_class(n);
448
467
        }
454
473
        pcode += generate_tag(tag);
455
474
    }
456
475
    if ( objectp(node->get_node("class")) ) 
457
 
        pcode += "    xml += \"</Object>\";\n";
 
476
        pcode += "    xml += \"</Object>\";\n}\n";
458
477
    return pcode;
459
478
}
460
479
 
466
485
 * @param string xml - the xml code
467
486
 * @returns pike code (mostly function calls) for xml generation
468
487
 */
469
 
string codegen(string xml, function read_import)
 
488
string codegen(string|object xml, function read_import)
470
489
{
 
490
    // add header and footer.
 
491
    string pcode = pike_header;
 
492
    if ( objectp(xml) ) {
 
493
      pcode += "xml += \"<!-- XGL File: " + _FILEPATH->object_to_filename(xml)+
 
494
        "-->\";\n";
 
495
      xml = xml->get_content();
 
496
    }
 
497
 
 
498
 
471
499
    NodeXML root = parse_data(xml);
472
500
    __reader    = read_import;
473
501
    __functions = 0;
479
507
 
480
508
    functions = ([ ]);
481
509
 
482
 
    // add header and footer.
483
 
    string pcode = pike_header;
484
510
 
 
511
    
485
512
    // read configs...
486
513
    mapping pi = root->get_pi();
487
514
    if ( mappingp(pi) ) {
505
532
        pcode += "\nstring " + func + "(mixed data, mixed value, mapping vars) {\n";
506
533
 
507
534
        pcode += "    mixed res;\n    string xml = \"\";\n";
 
535
        pcode += "    object back;\n";
508
536
        pcode += "    string out;\n";
509
537
        pcode += "    if ( objectp(active) ) last = active;\n";
510
538
        pcode += "    if ( objectp(data) )\n";