Project

General

Profile

« Previous | Next » 

Revision 3

Added by Matt Wheeler over 14 years ago

Inital import for script that converts vegx files into django objects.

View differences:

makeVegxModel/trunk/makeModel.php
1
<?php
2
require_once('objects/types.php');
3
require_once('objects/Entity.php');
4
require_once('objects/EntityList.php');
5
require_once('util/utilityFunctions.php');
6

  
7
$files = array(
8
               'eml-access.xsd',
9
               'eml-coverage.xsd',
10
               'eml-documentation.xsd',
11
               'eml-literature.xsd',
12
               'eml-party.xsd',
13
               'eml-project.xsd',
14
               'eml-resource.xsd',
15
               'eml-text.xsd',
16
               'tcsv101.xsd',
17
               'tdwg_basetypes.xsd',
18
#               'tdwg_dw_element.xsd',
19
               'tdwg_dw_geospatial.xsd',
20
               'veg-misc.xsd',
21
               'veg-organismobservation.xsd',
22
               'veg-plotobservation.xsd',
23
               'veg.xsd',
24
               'veg-plot.xsd');
25

  
26

  
27
$eList = new EntityList();
28
foreach($files as $file) {
29
  global $eList;
30

  
31
  $xmlDoc = new DOMDocument();
32
  $xmlDoc->load($file);
33
  $xpath = new DOMXPath($xmlDoc);
34
  $nodes = $xpath->query("child::node()",$xmlDoc->documentElement);
35
  foreach($nodes as $node) {
36
    $nodeName = preg_replace("/.*:/","",$node->nodeName);
37
    if(isIgnoreType($nodeName) || isPrimitiveType($node->getAttribute('name'))) {
38
      continue;
39
    }
40
    switch($nodeName) { 
41
      case 'group':
42
        if(!hasImportantNodes($node)) {
43
          die("Group nodes currently must define an entity. Group node in file: $file does not. Exiting.\n");
44
        }
45
      case 'simpleType':
46
      case 'complexType':
47
        $e = $eList->newEntity($node->getAttribute('name'),$node);
48
        break;
49
      case 'element':
50
        $eName = preg_replace("/\..*$/","",$file);
51
        $eName = preg_replace("/-/","_",$eName);
52
        $eParent = $eList->getEntityForReference($eName);
53

  
54
        $eParent->handleElementCase($node);
55
        break;
56
      default:
57
          die("Unsupported node type: $nodeName found in file: $file.\n");
58
    }
59
  }
60
}
61

  
62
$entities = $eList->getEntities();
63
print "#There are " . count($entities) . " defined entities.\n";
64
print "import string\n";
65
print "import xml.dom.minidom\n";
66
print "from random import choice\n";
67
print "from django.db import models\n\n";
68
print "def getRandom(length=8, chars=string.letters + string.digits):\n".
69
      "  return ''.join([choice(chars) for i in range(length)])\n\n".
70
      "def getRandomString():\n".
71
      "  return getRandom(45)\n\n".
72
      "def getRandomText():\n".
73
      "  return getRandom(450)\n\n".
74
      "def getRandomInt():\n".
75
      "  return int(getRandom(4,string.digits))\n\n".
76
      "def getRandomFloat():\n".
77
      "  return float(getRandom(4,string.digits))\n\n".
78
      "def getRandomBool():\n".
79
      "  return choice(['true','false'])\n\n".
80
      "#Not actually random, but not worth the time\n".
81
      "def getRandomDate():\n".
82
      "  return '2007-07-16'\n\n".
83
      "def getRandomTime():\n".
84
      "  return '08:30:00'\n\n";
85

  
86
$eList->printAll();
87

  
88
?>
89

  
makeVegxModel/trunk/objects/Entity.php
1
<?php
2
require_once('util/utilityFunctions.php');
3
require_once('objects/types.php');
4

  
5
class Entity {
6
  private $references = array();
7
  private $members = array();
8
  private $referrers = array();
9
  private $tableName;
10
  private $extensionType;
11
  private $inited;
12

  
13
  public function __construct($tableName,$node = null) {
14
    global $eList;
15
    // Entity name = table name
16
    $this->tableName = $tableName;
17
    $this->inited = False;
18
    $this->extensionType = '';
19

  
20
    if(!is_null($node)) {
21
      $this->init($tableName,$node);
22
      $this->inited = True;
23
    }
24
  }
25

  
26
  public function init($tableName,$node) {
27
    global $primitiveTypes;
28
    global $eList;
29
    $xpath = new DOMXPath($node->ownerDocument);
30
    $childNodesXPath = $xpath->query("child::node()",$node);
31
    $childNodes = array();
32
    foreach($childNodesXPath as $childNodeXPath) {
33
      $cNodeName = preg_replace("/.*:/","",$childNode->nodeName);
34
      if(!isIgnoreType($cNodeName)) {
35
        $childNodes[] = $childNodeXPath;
36
      }
37
    }
38

  
39
    for($i = 0; $i < count($childNodes); $i++) {
40
      $childNode = $childNodes[$i];
41

  
42
      $cNodeName = preg_replace("/.*:/","",$childNode->nodeName);
43

  
44
      if(isIgnoreType($cNodeName)) {
45
        continue;
46
      }
47

  
48
      #Very inefficient, but I want to be sure that I've handled all possible
49
      #attribute types.
50
      $attrs = $xpath->query("attribute::*",$childNode);
51
      foreach($attrs as $attr) {
52
        $attrName = $attr->name;
53
        if(!isKnownAttribute($attrName)) {
54
          throw new Exception("Encountered unkown attribute: $attrName in element $this->tableName.");
55
        }
56
      }
57

  
58
      switch($cNodeName) {
59
        #Untested
60
        case 'any':
61
          $any_e = $eList->newEntity($tableName . "_any_nodes");
62
          $any_e->addMember("node_name","models.CharField(max_length=255)");
63
          $any_e->addMember("node_value","models.TextField()");
64
          $any_e->addOneToManyReference($tableName . "_id",$this);
65
          $this->referrers['anyNode'] = $tableName . '_any_nodes';
66
          break;
67
        case 'anyAttribute':
68
          $any_a = $eList->newEntity($tableName . "_any_attr");
69
          $any_a->addMember("attr_name","models.CharField(max_length=255)");
70
          $any_a->addMember("attr_value","models.CharField(max_length=255)");
71
          $any_a->addOneToManyReference($tableName . "_id",$this);
72
          $this->referrers['anyAttribute'] = $tableName . '_any_attr';
73
          break;
74
        case 'attribute':
75
          $a_name = $childNode->getAttribute('name');
76
          if($a_name == '') {
77
            throw new Exception("Nodes defining attributes must have a name.  " .
78
                                "Unnamed attribute node found in $this->tableName.");
79
          }
80
          $default = $childNode->getAttribute('default');
81
          $use = $childNode->getAttribute('use');
82
          $type = preg_replace("/.*:/","",$childNode->getAttribute('type'));
83
          $guess = False;
84
          if($primitiveTypes[$type] != '') {
85
            $def = $primitiveTypes[$type];
86
          } else {
87
            $def = "models.CharField(max_length=255";
88
          }
89
          if($use == 'optional') {
90
            if($def == 'models.CharField(max_length=255') { $def .= ", "; }
91
            $def .= "null=True, blank=True";
92
          }
93
          if($default != '') {
94
            if($use == 'optional' ||
95
               $def == 'models.CharField(max_length=255') { 
96
              $def .= ", "; 
97
            }
98
            $def .= "default = '$default'";
99
          }
100
          $def .= ")";
101
          if($guess) { $def .= " # This is a guess"; }
102
          $this->addMember("attr_$a_name",$def);
103
          break;
104
        #Untested
105
        case 'group':
106
          if(hasImportantNodes($childNode)) {
107
            throw new Exception("No defined functionality for group nodes defining complex entities " .
108
                                "within other complex entities.");
109
          }
110
          $ref = preg_replace("/.*:/","",$childNode->getAttribute('ref'));
111
          if($ref == '') {
112
            throw new Exception("No defined functionality for group nodes " .
113
                                "that don't have a 'ref' attribute.");
114
          }
115
          $this->determineRelationship($ref,$ref,$childNode);
116
          break;
117
        case 'complexType':
118
          $name = $childNode->getAttribute('name');
119
          if($name != '') {
120
            throw new Exception("Named complexType definition within entity definition. " .
121
                                "This functionality is not yet implemented.");
122
          }
123
        case 'simpleType':
124
          $name = $childNode->getAttribute('name');
125
          if(isPrimitiveType($name)) {
126
            break;
127
          }
128
          if($name != '') {
129
            throw new Exception("Named simpleType definition within entity definition. " .
130
                                "This functionality is not yet implemented.");
131
          }
132
        case 'choice':
133
          # Fracking hack
134
          $isChoice = True;
135
        case 'complexContent':
136
        case 'simpleContent':
137
        case 'sequence':
138
          $minOccurs = $childNode->getAttribute('minOccurs');
139
          if($isChoice) {
140
            $minOccurs = 0;
141
          }
142
          $maxOccurs = $childNode->getAttribute('maxOccurs');
143
         
144
          #If defined, need to use pass these attribues through to child nodes.
145
          #Then, we add the grandchid nodes to the node list since we need the
146
          #grandchild nodes to complete the entity definition.
147
          $grandchildNodesXPath = $xpath->query("child::node()",$childNode);
148
          foreach($grandchildNodesXPath as $grandchildNodeXPath) {
149
            $gcNodeName = preg_replace("/.*:/","",$grandchildNodeXPath->nodeName);
150
            if(isIgnoreType($gcNodeName)) {
151
              continue;
152
            }
153

  
154
            if(((string)$minOccurs) != '') {
155
              $grandchildNodeXPath->setAttribute("minOccurs",$minOccurs);
156
            }
157
            if(((string)$maxOccurs) != '') {
158
              $grandchildNodeXPath->setAttribute("maxOccurs",$maxOccurs);
159
            }
160
            $childNodes[] = $grandchildNodeXPath;
161
          }
162

  
163
          #None of these have implemented functionality, need to fail if we reach
164
          #them during exection.
165
          $err_type = '';
166
          $default = $childNode->getAttribute('default');
167
          if($default != '' ) {
168
            $err_type = "default";
169
          }
170
          $use = $childNode->getAttribute('use');
171
          if($use != '' ) {
172
            $err_type = "use";
173
          }
174
          $mixed = $childNode->getAttribute('mixed');
175
          if($mixed != '' ) {
176
            $err_type = "mixed";
177
          }
178
          $type = $childNode->getAttribute('type');
179
          if($type != '' ) {
180
            $err_type = "type";
181
          }
182
          $namespace = $childNode->getAttribute('namespace');
183
          if($namespace != '' ) {
184
            $err_type = "namespace";
185
          }
186
          $nillable = $childNode->getAttribute('nillable');
187
          if($nillable != '' ) {
188
            $err_type = "nillable";
189
          }
190
          $ref = $childNode->getAttribute('ref');
191
          if($ref != '' ) {
192
            $err_type = "ref";
193
          }
194
          $base = $childNode->getAttribute('base');
195
          if($base != '' ) {
196
            $err_type = "base";
197
          }
198
          if($err_type != '') {
199
            throw new Exception("$err_type not defined for pass-through.");
200
          }
201
          break;
202
        case 'element':
203
          $this->handleElementCase($childNode);
204
          break;
205
        case 'extension':
206
          $name = findFirstAncestorName($childNode);
207
          $base = preg_replace("/.*:/","",$childNode->getAttribute('base'));
208
          if(isPrimitiveType($base)) {
209
            $grandchildNodesXPath = $xpath->query("child::node()",$childNode);
210
            foreach($grandchildNodesXPath as $grandchildNodeXPath) {
211
              $gcNodeName = preg_replace("/.*:/","",$grandchildNodeXPath->nodeName);
212
              if(isIgnoreType($gcNodeName)) {
213
                continue;
214
              }
215
              if($gcNodeName != 'attribute') {
216
                throw new Exception("Extension of primitive types can only add attributes, ".
217
                                    "found $gcNodeName.");
218
              }
219
              $a_name = $grandchildNodeXPath->getAttribute('name');
220
              if($a_name == '') {
221
                throw new Exception("Nodes defining attributes must have a name.  " .
222
                                    "Unnamed attribute node found in $this->tableName.");
223
              }
224
              $default = $grandchildNodeXPath->getAttribute('default');
225
              $use = $grandchildNodeXPath->getAttribute('use');
226
              $type = preg_replace("/.*:/","",$grandchildNodeXPath->getAttribute('type'));
227
              $guess = False;
228
              if($primitiveTypes[$type] != '') {
229
                $def = $primitiveTypes[$type];
230
              } else {
231
                $def = "models.CharField(max_length=255";
232
                $guess = True;
233
              }
234
              if($use == 'optional') {     
235
                if($def == 'models.CharField(max_length=255') { $def .= ", "; }
236
                $def .= "null=True, blank=True";
237
              }
238
              if($default != '') {
239
                if($use == 'optional' ||
240
                   $def == 'models.CharField(max_length=255') { $def .= ", "; }
241
                $def .= "default = '$default'";
242
              }
243
              $def .= ")";
244
              if($guess) { $def .= " # This is a guess"; }
245
              $this->addMember("attr_$a_name",$def);
246
            }
247
            $def = $primitiveTypes[$base] . ")";
248
            $this->addMember("primitive_type_value",$def);
249
          } else {
250
            $ref_e = $eList->getEntityForReference($base);
251
            $this->addOneToOneReference($base . "_extend",$ref_e);
252
            $this->extensionType = $base;
253

  
254
            $grandchildNodesXPath = $xpath->query("child::node()",$childNode);
255
            foreach($grandchildNodesXPath as $grandchildNodeXPath) {
256
              $gcNodeName = preg_replace("/.*:/","",$grandchildNodeXPath->nodeName);
257
              if(isIgnoreType($gcNodeName)) {
258
                continue;
259
              }
260
              $childNodes[] = $grandchildNodeXPath;
261
            }
262
          }
263
          break;
264
        case 'restriction':
265
        case 'union':
266
        case 'maxInclusive':
267
        case 'minInclusive':
268
        case 'minExclusive':
269
        case 'list':
270
        case 'pattern':
271
        case 'fractionDigits':
272
          throw new Exception("No functionality defined for nodes of type: $cNodeName");
273
          break;
274
        default:
275
          throw new Exception("Encountered uknown nodes type: $cNodeName");
276
          break;
277
      }
278
    }
279
  }
280

  
281
  public function handleElementCase($childNode) {
282
    global $eList;
283
    global $primitiveTypes;
284
    $ref = preg_replace("/.*:/","",$childNode->getAttribute('ref'));
285
    $type = preg_replace("/.*:/","",$childNode->getAttribute('type'));
286
    if($ref != '') { 
287
      $type = $ref; 
288
    }
289
    $name = $childNode->getAttribute('name');
290
    if($ref != '' && $name == '') {
291
      $name = $this->tableName . "_" . $ref;
292
    }
293

  
294
    if($name == '') {
295
      throw new Exception("All element nodes must have a name attribute, " .
296
                          "or a ref attribute to serve as the name.");
297
    }
298

  
299
    if($type != '') {
300
      $this->determineRelationship($name,$type,$childNode);
301
    } else {
302
      #Very hacky, but there a some cases where the element
303
      #has no type, and either 1) has some children nodes, but none of the
304
      #children nodes define a type, or 2) Has no useful nodes at all.
305
      # In this case the node is considered to be an anyType.
306
      $isDefined = False;
307
      $xpath = new DOMXPath($childNode->ownerDocument);
308
      $grandchildNodesXPath = $xpath->query("child::node()",$childNode);
309
      foreach($grandchildNodesXPath as $grandchildNodeXPath) {
310
        $gcNodeName = preg_replace("/.*:/","",$grandchildNodeXPath->nodeName);
311
        if(isIgnoreType($gcNodeName)) {
312
          continue;
313
        }
314
        if(hasImportantNodes($grandchildNodeXPath) || $grandchildNodeXPath->hasAttributes()) {
315
          $isDefined = True;
316
        }
317
      }
318
      if(!$isDefined) {
319
        $primitiveTypes[$name] = 'models.TextField(';
320
      }
321
      
322
      if(!isPrimitiveType($name)) {
323
        $eList->newEntity($name,$childNode);
324
      }
325
      $this->determineRelationship($name,$name,$childNode);
326
    }
327
  }
328

  
329
  public function determineRelationship($colName,$typeName,$node) {
330
    global $eList;
331
    global $primitiveTypes;
332

  
333
    $tableName = $this->tableName;
334

  
335
    $minOccurs = $node->getAttribute('minOccurs');
336
    $optional = $minOccurs == 0 ? True : False;
337
    $maxOccurs = $node->getAttribute('maxOccurs');
338
    $default = $node->getAttribute('default');
339
    $default = $node->getAttribute('use');
340

  
341
    if(isPrimitiveType($typeName)) {
342
      $def = $primitiveTypes[$typeName];
343
      if($use == 'optional' || $optional) {
344
        if($def == 'models.CharField(max_length=255') { $def .= ", "; }
345
        $def .= "null=True, blank=True";
346
      }
347
      if($default != '') {
348
        if($use == 'optional' || $optional || 
349
           $def == 'models.CharField(max_length=255') { 
350
          $def .= ", "; 
351
        }
352
        $def .= "default = '$default'";
353
      }
354
      $def .= ")";
355

  
356
      if(((string)$maxOccurs) == '' || $maxOccurs == 1) {
357
        $this->addMember($colName,$def);
358
      } else {
359
        $refName = $this->tableName . "_" . $colName;
360
        $new_e = $eList->getEntityForReference($refName);
361
        $new_e->addMember("primitive_type_value",$def);
362
        $new_e->addOneToManyReference($tableName . "_id",$this);
363
        $this->referrers[$colName] = $refName;
364
      }
365
    } else {
366
      $ref_e = $eList->getEntityForReference($typeName);
367
      if(((string)$maxOccurs) == '' || $maxOccurs == 1) {
368
        $this->addOneToOneReference($colName,$ref_e,$optional);
369
      } else {
370
#        echo "Need to define relationship between:\n  $tableName <--> $typeName\n";
371
#        echo "    (O)ne to Many\n    (M)any to Many\n";
372
#        echo "    Your choice:  ";
373
#        $handle = fopen ("php://stdin","r");
374
#        $line = fgets($handle);
375
$line = 'M';
376
        if(trim($line) == 'O' || trim($line) == 'o'){
377
          $ref_e->addOneToManyReference($colName,$this);
378
          $this->referrers[$colName] = $ref_e->getName();
379
        } else {
380
          $this->addManyToManyReference($colName,$ref_e,$tableName . "_" . $typeName . "_" . $colName);
381
        }
382
      }
383
    }
384
  }
385

  
386
  private function addReference($colName,$definition) {
387
    # Probably shouldn't allow duplicate entries, most likely points to a problem.
388
    if(array_key_exists($colName,$this->references)) {
389
     $curDef = $this->references[$colName];
390
     throw new Exception("Attempting to overwrite/reinsert member $colName:$curDef with $colName:$definition in entity " . $this->tableName . ".");
391
    }
392
    $this->references[$colName] = $definition;
393
  }
394

  
395
  public function isInited() {
396
    return $this->inited;
397
  }
398

  
399
  public function addOneToOneReference($colName,$e,$optional = False) {
400
    $eName = $e->getName();
401
    $relatedName = $this->tableName . "_" . $colName;
402
    #$def = "models.OneToOneField('$eName', primary_key=True,related_name='$relatedName'";
403
    $def = "models.ForeignKey('$eName', unique=True,related_name='$relatedName'";
404
    if($optional) {
405
      $def .= ", null=True, blank=True";
406
    }
407
    $def .= ")";
408
    $this->addReference($colName,$def); 
409
  }
410

  
411
  public function addOneToManyReference($colName,$e,$optional = False) {
412
    $eName = $e->getName();
413
    if($eName == $this->tableName) { 
414
      $eName = 'self'; 
415
    }
416
    $relatedName = $this->tableName . "_" . $colName;
417
    $def = "models.ForeignKey('$eName',related_name='$relatedName'";
418
    if($optional) {
419
      $def .= ", null=True, blank=True";
420
    }
421
    $def .= ")";
422
    $this->addReference($colName,$def); 
423
  }
424

  
425
  public function addManyToManyReference($colName,$e,$db_table = '') {
426
    $eName = $e->getName();
427
    $relatedName = $this->tableName . "_" . $colName;
428

  
429
    $def = "models.ManyToManyField('$eName',related_name='$relatedName'";
430

  
431
    #Django chokes if the table/relation names are too long.  54 characters seems to
432
    #be a decent magic number.
433
    if($db_table != '' && strlen($db_table) > 54) {
434
      $db_table = 'v_' . substr($db_table,0,54);
435
      $def .= ",db_table = '$db_table'";
436
    }
437
    $def .= ")";
438
    $this->addReference($colName,$def); 
439
  }
440

  
441
  public function addMember($colName, $definition) {
442
    # Probably shouldn't allow duplicate entries, most likely points to a problem.
443
    if(array_key_exists($colName,$this->members)) {
444
      $curDef = $this->members[$colName];
445
      if($curDef != $definition) {
446
        throw new Exception("Attempting to overwrite/reinsert member $colName:$curDef with $colName:$definition.");
447
      }
448
    }
449
    $this->members[$colName] = $definition;
450
  }
451

  
452
  public function getName() {
453
    return $this->tableName;
454
  }
455

  
456
  public function getReferences() {
457
    return $this->references;
458
  }
459

  
460
  public function getMembers() {
461
    return $this->members;
462
  }
463

  
464
  public function getReferrers() {
465
    return $this->referrers;
466
  }
467

  
468
  public function postProcess() {
469
    global $eList;
470

  
471
    #If we're dealing with extensions we need to pull the members, references, &
472
    #referrers of the base class.
473
    if($this->extensionType != '') {
474
      $base = $eList->getEntityForReference($this->extensionType);
475
      $baseReferences = $base->getReferences();
476
      $baseMembers = $base->getMembers();
477
      $baseReferrers = $base->getReferrers();
478

  
479
      $this->references = array_merge($this->references,$baseReferences);
480
      foreach(array_keys($this->references) as $k) {
481
        $def = $this->references[$k];
482
        $baseToken = $base->getName() . "_";
483
        $tableToken = $this->tableName . "_";
484
        $def = preg_replace("/$baseToken/","$tableToken",$def);
485
        $this->references[$k] = $def;
486
      }
487
        
488
      $this->members = array_merge($this->members,$baseMembers);
489
      $this->referrers = array_merge($this->referrers,$baseReferrers);
490

  
491
    }
492

  
493
  }
494

  
495
  public function toString() {
496
    $str = "class " . $this->tableName . "(models.Model):\n";
497
    #Needed to keep Django from choking:
498
    if(count($this->references) == 0 &&
499
       count($this->members) == 0) {
500
      $str .= "  id = models.AutoField(primary_key=True)";
501
    }
502

  
503
    foreach(array_keys($this->references) as $colName) {
504
      $def = $this->references[$colName];
505
      $str .= "  $colName = $def\n";
506
    }
507
    foreach(array_keys($this->members) as $colName) {
508
      $def = $this->members[$colName];
509
      $str .= "  $colName = $def\n";
510
    }
511

  
512
    $str .= "\n" . $this->makeExportFunction();
513
    $str .= "\n" . $this->makeImportFunction();
514
    $str .= "\n" . $this->makeExportDummyDataFunction();
515

  
516
    #truncate table names that are too long for Django/Postgre to support
517
    if(strlen($this->tableName) > 54) {
518
      $tabName = 'v_' . substr($this->tableName,0,54);
519
      $str .= "\n\n  class Meta:\n".
520
              "    db_table = u'$tabName'\n\n";
521
    }
522

  
523
    return $str;
524
  }
525

  
526
  public function makeImportFunction() {
527
    #Slightly different if there are no members
528
    if(count($this->references) == 0 &&
529
       count($this->members) == 0 &&
530
       count($this->referrers) == 0) {
531
      return "  def importVegX(self,node):\n"."    self.save()\n";
532
    }
533

  
534
    $def =  "  def importVegX(self,node):\n";
535

  
536
    if(preg_match_all("/([a-zA-Z]*)_extend/",implode(",",array_keys($this->references)),$matches) > 0) {
537
      $base = $matches[1][0];
538
      $def .= "    extendedEl = $base()\n".
539
              "    extendedEl.importVegX(node)\n".
540
              "    self.$base"."_extend = extendedEl\n\n";
541
    }
542

  
543
    $def .= "    self.save()\n";
544

  
545
    #Fun hack for anyAttribute & anyNode nodes
546
    if(preg_match("/_any_attr/",$this->tableName,$matches) > 0 ||
547
       preg_match("/_any_nodes/",$this->tableName,$matches) > 0) {
548
      return $def;
549
    }
550

  
551
    if(preg_match("/attr_/",implode(",",array_keys($this->members))) > 0 ||
552
       preg_match("/anyAttribute/",implode(",",array_keys($this->referrers))) > 0) {
553
      $def .= "    #attributes\n".
554
              "    for attr in node.attributes.keys():\n".
555
              "      aName = 'attr_' + attr\n".
556
              "      aVal = node.attributes[attr].nodeValue\n";
557
      $i = 0;
558
      foreach(array_keys($this->members) as $colName) {
559
        $ifStmt = $i == 0 ? 'if' : 'elif';
560
        if(preg_match("/^attr_/",$colName,$matches) > 0) {
561
          $i += 1;
562
          if(preg_match("/BooleanField/",$this->members[$colName],$matches) > 0) {
563
            $def .= "      $ifStmt aName == '$colName' and (aVal == 'true' or\n".
564
                    "      aVal == 'false' or aVal == 0 or aVal == 1):\n".
565
                    "        self.$colName = (aVal == 'true' or aVal == 1)\n";
566
          } else {
567
            $def .= "      $ifStmt aName == '$colName':\n".
568
                    "        self.$colName = aVal\n";
569
          }
570
        }
571
      }
572
    }
573
    if(preg_match("/anyAttribute/",implode(",",array_keys($this->referrers))) > 0) {
574
      $type = $this->referrers['anyAttribute'];
575
      $tIDName = $this->tableName . "_id";
576
      if(preg_match_all("/attr_/",implode(",",array_keys($this->members)),$matches) > 0) {
577
        $def .= "      else:\n".
578
                "        newEl = $type()\n".
579
                "        newEl.$tIDName = self\n".
580
                "        newEl.attr_name = attr\n".
581
                "        newEl.attr_value = aVal\n".
582
                "        newEl.save()\n";
583
      } else {
584
        $def .= "      newEl = $type()\n".
585
                "      newEl.$tIDName = self\n".
586
                "      newEl.attr_name = attr\n".
587
                "      newEl.attr_value = aVal\n".
588
                "      newEl.save()\n";
589
      }
590
    }
591
  
592
    if(count($this->members) - 
593
       preg_match_all("/attr_/",implode(",",array_keys($this->members)),$matches) -
594
       preg_match_all("/primitive_type_value/",implode(",",array_keys($this->members)),$matches) > 0 ||
595
       count($this->references) > 0 || count($this->referrers) > 0) {
596
      $def .= "    #members & relationships\n".
597
              "    for child in node.childNodes:\n".
598
              "      cName = child.nodeName\n";
599
    }
600

  
601
    $i = 0;
602
    if(count($this->members) - 
603
       preg_match_all("/attr_/",implode(",",array_keys($this->members)),$matches) -
604
       preg_match_all("/primitive_type_value/",implode(",",array_keys($this->members)),$matches) > 0) {
605
      foreach(array_keys($this->members) as $colName) {
606
        if(preg_match("/^attr_/",$colName) > 0) { continue; }
607
        $ifStmt = $i == 0 ? 'if' : 'elif';
608
        $i += 1;
609
        $def .= "      $ifStmt cName == '$colName':\n".
610
                "        cVal = child.childNodes[0].nodeValue\n".
611
                "        self.$colName = cVal\n";
612
      }
613
    }
614

  
615
    if(preg_match_all("/primitive_type_value/",implode(",",array_keys($this->members)),$matches) > 0) {
616
      $def .= "    cVal = node.childNodes[0].nodeValue\n".
617
              "    self.primitive_type_value = cVal\n";
618
    }
619

  
620
    foreach(array_keys($this->references) as $colName) {
621
      $ifStmt = $i == 0 ? 'if' : 'elif';
622
      $colDef = $this->references[$colName];
623

  
624
      if(preg_match("/(ForeignKey\(')([a-zA-Z]*)/",$colDef,$matches) > 0 &&
625
         preg_match("/unique/",$colDef) > 0 && preg_match("/_extend/",$colName) == 0) {
626
        $i += 1;
627
        $def .= "      $ifStmt cName == '$colName':\n".
628
                "        newEl = $matches[2]()\n".
629
                "        newEl.importVegX(child)\n".
630
                "        self.$colName = newEl\n";
631
      }
632
      if(preg_match("/(ManyToManyField\(')([a-zA-Z]*)/",$colDef,$matches) > 0) {
633
        $i += 1;
634
        $def .= "      $ifStmt cName == '$colName':\n".
635
                "        newEl = $matches[2]()\n".
636
                "        newEl.importVegX(child)\n".
637
                "        self.$colName.add(newEl)\n";
638
      }
639
    }
640

  
641
    if(count($this->referrers) > 0) {
642
      $def .= "      #These are the elements that refer to this one\n";
643
    }
644
    $allowAnyNode = False;
645
    foreach(array_keys($this->referrers) as $colName) {
646
      if($colName == 'anyAttribute' || $colName == 'anyNode') { continue; }
647
      $ifStmt = $i == 0 ? 'if' : 'elif';
648
      $i += 1;
649
      $colDef = $this->referrers[$colName];
650
      $elReferenceBack = $this->tableName . '_id';
651
      $def .= "      $ifStmt cName == '$colName':\n".
652
              "        newEl = $colDef()\n".
653
              "        newEl.$elReferenceBack = self\n".
654
              "        newEl.importVegX(child)\n";
655
    }
656
    if(preg_match("/anyNode/",implode(",",array_keys($this->referrers))) > 0) {
657
      $type = $this->referrers['anyNode'];
658
      $tIDName = $this->tableName . "_id";
659
      if($i > 0) {
660
        $def .= "      else:\n".
661
                "        newEl = $type()\n".
662
                "        newEl.$tIDName = self\n".
663
                "        newEl.node_name = cName\n".
664
                "        for grandchild in child.childNodes:\n".
665
                "          newEl.node_value += grandchild.toxml()\n".
666
                "        newEl.save()\n";
667
      } else {
668
        $def .= "      newEl = $type()\n".
669
                "      newEl.$tIDName = self\n".
670
                "      newEl.node_name = cName\n".
671
                "      for grandchild in child.childNodes:\n".
672
                "        newEl.node_value += grandchild.toxml()\n".
673
                "      newEl.save()\n";
674
      }
675
    }
676

  
677
    $def .= "\n    self.save()\n"; 
678
    return $def;
679
    
680
  }
681

  
682
  public function makeExportFunction() {
683
    #Slightly different if there are no members
684
    if(count($this->references) == 0 &&
685
       count($this->members) == 0 &&
686
       count($this->referrers) == 0) {
687
      return "  def exportVegX(self,myNode,doc):\n".
688
             "    return myNode\n";
689
    }
690

  
691
    $def =  "  def exportVegX(self,myNode,doc):\n";
692

  
693
    #Fun hack for anyAttribute & anyNode nodes
694
    if(preg_match("/_any_attr/",$this->tableName,$matches) > 0) {
695
      $def .= "    newAttr = doc.createAttribute(self.attr_name)\n".
696
              "    newAttr.value = self.attr_value\n".
697
              "    return newAttr\n";
698
      return $def;
699
    }
700
    if(preg_match("/_any_nodes/",$this->tableName,$matches) > 0) {
701
      $def .= "    newEl = doc.createElement(self.node_name)\n".
702
              "    newElText = doc.createTextNode(self.node_value)\n".
703
              "    newEl.appendChild(newElText)\n".
704
              "    return newEl\n\n";
705
      return $def;
706
    }
707

  
708
    foreach(array_keys($this->members) as $colName) {
709
      $nodeName = $colName;
710
      $nodeText = "str(self.$colName)";
711
      //Python's string representation of booleans is True/False but xsd requires
712
      //true/false (lowercase) so we need this hack.
713
      if(preg_match("/NullBooleanField/",$this->members[$colName],$matches) > 0) {
714
        $nodeText = "string.lower(str(self.$colName))";
715
      }
716
      if(preg_match("/^attr_/",$colName,$matches) > 0) {
717
        $nodeName = preg_replace("/^attr_/","",$colName);
718
        $def .= "    if self.$colName != None:\n".
719
                "      newAttr = doc.createAttribute('$nodeName')\n".
720
                "      newAttr.value = $nodeText\n".
721
                "      myNode.setAttributeNode(newAttr)\n\n";
722
      } else if(preg_match("/primitive_type_value/",$colName,$matches) > 0) {
723
        $def .= "    if self.$colName != None:\n".
724
                "      newElText = doc.createTextNode(self.$colName)\n".
725
                "      myNode.appendChild(newElText)\n\n";
726
      }else {
727
/*
728
        if(preg_match("/DateField/",$this->members[$colName],$matches) > 0 ||
729
           preg_match("/TimeField/",$this->members[$colName],$matches) > 0 ||
730
           preg_match("/PositiveIntegerField/",$this->members[$colName],$matches) > 0 ||
731
           preg_match("/NullBooleanField/",$this->members[$colName],$matches) > 0 ||
732
           preg_match("/FloatField/",$this->members[$colName],$matches) > 0) {
733
          $nodeText = "str(self.$colName)";
734
        }
735
*/
736
        $def .= "    if self.$colName != None:\n".
737
                "      newEl = doc.createElement('$colName')\n".
738
                "      newElText = doc.createTextNode($nodeText)\n".
739
                "      newEl.appendChild(newElText)\n".
740
                "      myNode.appendChild(newEl)\n\n";
741
      }
742
    }
743

  
744
    foreach(array_keys($this->references) as $colName) {
745
      $colDef = $this->references[$colName];
746

  
747
      if(preg_match("/(ForeignKey\(')([a-zA-Z]*)/",$colDef,$matches) > 0 &&
748
         preg_match("/unique/",$colDef) > 0) {
749
        if(preg_match("/_extend/",$colName) == 0) {
750
          $def .= "    if self.$colName != None:\n".
751
                  "      newEl = doc.createElement('$colName')\n".
752
                  "      myNode.appendChild(self.$colName.exportVegX(newEl,doc))\n\n";
753
        } else {
754
          $def .= "    if self.$colName != None:\n".
755
                  "      myNode = self.$colName.exportVegX(myNode,doc)\n\n";
756
        }
757
      }
758
      if(preg_match("/(ManyToManyField\(')([a-zA-Z]*)/",$colDef,$matches) > 0) {
759
        $def .= "    for childRef in self.$colName.all():\n".
760
                "      newEl = doc.createElement('$colName')\n".
761
                "      myNode.appendChild(childRef.exportVegX(newEl,doc))\n\n";
762
      }
763
    }
764

  
765
    foreach(array_keys($this->referrers) as $nodeName) {
766
      $nodeNameToUse = $nodeName;
767
      if($nodeName == 'anyAttribute') {
768
        $nodeNameToUse = 'any_attr';
769
        $relatedName = $this->tableName . '_' . $nodeNameToUse . '_' . $this->tableName . '_id';
770
        $def .= "    for childRef in self.$relatedName.all():\n".
771
                "      newEl = doc.createAttribute('$nodeName')\n".
772
                "      myNode.setAttributeNode(childRef.exportVegX(newEl,doc))\n\n";
773
      } else {
774
        if($nodeName == 'anyNode') {
775
          $nodeNameToUse = 'any_nodes';
776
        }
777
        $relatedName = $this->tableName . '_' . $nodeNameToUse . '_' . $this->tableName . '_id';
778
        $def .= "    for childRef in self.$relatedName.all():\n".
779
                "      newEl = doc.createElement('$nodeName')\n".
780
                "      myNode.appendChild(childRef.exportVegX(newEl,doc))\n\n";
781
      }
782
    }
783

  
784
    $def .= "    return myNode\n";
785
    return $def;
786
  }
787

  
788
  public function makeExportDummyDataFunction() {
789
    #Slightly different if there are no members
790
    if(count($this->references) == 0 &&
791
       count($this->members) == 0 &&
792
       count($this->referrers) == 0) {
793
      return "  def exportDummyVegX(self,myNode,doc,usedObjectsStack):\n".
794
             "    return myNode\n";
795
    }
796

  
797
    $def =  "  def exportDummyVegX(self,myNode,doc,usedObjectsStack):\n";
798

  
799
    #Fun hack for anyAttribute & anyNode nodes
800
    if(preg_match("/_any_attr/",$this->tableName,$matches) > 0) {
801
      $def .= "    newAttr = doc.createAttribute(getRandomString())\n".
802
              "    newAttr.value = getRandomString()\n".
803
              "    return newAttr\n";
804
      return $def;
805
    }
806
    if(preg_match("/_any_nodes/",$this->tableName,$matches) > 0) {
807
      $def .= "    newEl = doc.createElement(getRandomString())\n".
808
              "    newElText = doc.createTextNode(getRandomText())\n".
809
              "    newEl.appendChild(newElText)\n".
810
              "    return newEl\n\n";
811
      return $def;
812
    }
813

  
814
    foreach(array_keys($this->members) as $colName) {
815
      $nodeName = $colName;
816
      $colDef = $this->members[$colName];
817
      $randomFun = "getRandomString";
818
      if(preg_match("/FloatField/",$colDef,$matches) > 0) {
819
        $randomFun = "getRandomFloat";
820
      } else if(preg_match("/IntegerField/",$colDef,$matches) > 0) {
821
        $randomFun = "getRandomInt";
822
      } else if(preg_match("/TextField/",$colDef,$matches) > 0) {
823
        $randomFun = "getRandomText";
824
      } else if(preg_match("/DateField/",$colDef,$matches) > 0) {
825
        $randomFun = "getRandomDate";
826
      } else if(preg_match("/TimeField/",$colDef,$matches) > 0) {
827
        $randomFun = "getRandomTime";
828
      } else if(preg_match("/NullBooleanField/",$colDef,$matches) > 0) {
829
        $randomFun = "getRandomBool";
830
      }
831

  
832
      #Need to explicitly convert to string so minidom doesn't
833
      #roll over in nasty convulsions
834
      $randomFun = "str($randomFun())";
835
      if(preg_match("/^attr_/",$colName,$matches) > 0) {
836
        $nodeName = preg_replace("/^attr_/","",$colName);
837
        $def .= "    newAttr = doc.createAttribute('$nodeName')\n".
838
                "    newAttr.value = $randomFun\n".
839
                "    myNode.setAttributeNode(newAttr)\n\n";
840
      } else if(preg_match("/primitive_type_value/",$colName,$matches) > 0) {
841
        $def .= "    newElText = doc.createTextNode($randomFun)\n".
842
                "    myNode.appendChild(newElText)\n\n";
843
      }else {
844
        $def .= "    newEl = doc.createElement('$colName')\n".
845
                "    newElText = doc.createTextNode($randomFun)\n".
846
                "    newEl.appendChild(newElText)\n".
847
                "    myNode.appendChild(newEl)\n\n";
848
      }
849
    }
850

  
851
    foreach(array_keys($this->references) as $colName) {
852
      $colDef = $this->references[$colName];
853

  
854
      if(preg_match("/(ForeignKey\(')([a-zA-Z]*)/",$colDef,$matches) > 0 &&
855
         preg_match("/unique/",$colDef) > 0) {
856
        if(preg_match("/_extend/",$colName) == 0) {
857
          $def .= "    if '$matches[2]' not in usedObjectsStack:\n".
858
                  "      usedObjectsStack.append('$matches[2]')\n".
859
                  "      newEl = doc.createElement('$colName')\n".
860
                  "      newElObj = $matches[2]()\n".
861
                  "      myNode.appendChild(newElObj.exportDummyVegX(newEl,doc,usedObjectsStack))\n".
862
                  "      usedObjectsStack.pop()\n\n";
863
        } else {
864
          $def .= "    if '$matches[2]' not in usedObjectsStack:\n".
865
                  "      usedObjectsStack.append('$matches[2]')\n".
866
                  "      newElObj = $matches[2]()\n".
867
                  "      myNode = newElObj.exportDummyVegX(myNode,doc,usedObjectsStack)\n".
868
                  "      usedObjectsStack.pop()\n\n";
869
        }
870
      }
871
      if(preg_match("/(ManyToManyField\(')([a-zA-Z]*)/",$colDef,$matches) > 0) {
872
        $def .= "    if '$matches[2]' not in usedObjectsStack:\n".
873
                "      usedObjectsStack.append('$matches[2]')\n".
874
                "      newEl = doc.createElement('$colName')\n".
875
                "      newElObj = $matches[2]()\n".
876
                "      myNode.appendChild(newElObj.exportDummyVegX(newEl,doc,usedObjectsStack))\n".
877
                "      usedObjectsStack.pop()\n\n";
878
      }
879
    }
880

  
881
    foreach(array_keys($this->referrers) as $nodeName) {
882
      $colDef = $this->referrers[$nodeName];
883
      if($nodeName == 'anyAttribute') {
884
        $def .= "    if '$colDef' not in usedObjectsStack:\n".
885
                "      usedObjectsStack.append('$colDef')\n".
886
                "      newEl = doc.createAttribute(getRandomText())\n".
887
                "      childRef = $colDef()\n".
888
                "      myNode.setAttributeNode(childRef.exportDummyVegX(newEl,doc,usedObjectsStack))\n".
889
                "      usedObjectsStack.pop()\n\n";
890
      } else {
891
        $def .= "    if '$colDef' not in usedObjectsStack:\n".
892
                "      usedObjectsStack.append('$colDef')\n\n".
893
                "      newEl = doc.createElement('$nodeName')\n".
894
                "      childRef = $colDef()\n".
895
                "      myNode.appendChild(childRef.exportDummyVegX(newEl,doc,usedObjectsStack))\n".
896
                "      usedObjectsStack.pop()\n\n";
897
      }
898
    }
899

  
900
    $def .= "    return myNode\n";
901
    return $def;
902
  }
903

  
904
  public function makeSetupTestFunction() {
905
    if(isPrimitiveType($this->tableName)) { return ""; }
906

  
907
    $def = "  def setUp(self):\n".
908
           "    self.impl = getDOMImplementation()\n".
909
           "    self.newdoc = self.impl.createDocument(None, " . $this->tableName . ", None)\n".
910
           "    top_element = newdoc.documentElement\n";
911
  }
912

  
913
  public function makeImportTestFunction() {
914
  }
915

  
916
  public function makeExportTestFunction() {
917
  }
918

  
919
}
920
?>
makeVegxModel/trunk/objects/types.php
1
<?php
2

  
3
$primitiveTypes = array(
4
  'DEFCATEGORIE' => 'models.CharField(max_length=255',
5
  'decimalLatitudeDataType' => 'models.FloatField(', #'xs:double [-90,90]',
6
  'DecimalLatitude' => 'models.FloatField(', #xs:double [-90,90]',
7
  'decimalLongitudeDataType' => 'models.FloatField(', #xs:double [-180,180]',
8
  'DecimalLongitude' => 'models.FloatField(', #xs:double [-180,180]',
9
  'spatialFitDataType' => 'models.FloatField(', #xs:double (0 or >1 or undefined)',
10
  'FootprintSpatialFit' => 'models.FloatField(', #xs:double (0 or >1 or undefined)',
11
  'GeodeticDatum' => 'models.CharField(max_length=255',
12
  'VerbatimCoordinates' => 'models.CharField(max_length=255',
13
  'VerbatimLatitude' => 'models.CharField(max_length=255',
14
  'VerbatimLongitude' => 'models.CharField(max_length=255',
15
  'VerbatimCoordinateSystem' => 'models.CharField(max_length=255',
16
  'GeoreferenceProtocol' => 'models.CharField(max_length=255',
17
  'GeoreferenceSources' => 'models.CharField(max_length=255',
18
  'GeoreferenceVerificationStatus' => 'models.CharField(max_length=255',
19
  'GeoreferenceRemarks' => 'models.CharField(max_length=255',
20
  'FootprintWKT' => 'models.CharField(max_length=255',
21
  'CoordinateUncertaintyInMeters' => 'models.PositiveIntegerField(', #'positiveInteger',
22
  'PointRadiusSpatialFit' => 'models.FloatField(', #'xs:double (0 or >1 or undefined)',
23
  'PERCENT' => 'models.FloatField(', #'xsd:decimal [0,100]',
24
  'RoleType' => 'models.CharField(max_length=255',
25
  'TaxonomicRankEnum' => 'models.CharField(max_length=255',
26
  'TINYINT' => 'models.FloatField(', #'xsd:decimal [-128,128]',
27
  'yearDate' => 'models.DateField(', #'xs:gYear xs:date (union of)',
28
  'anyType' => 'models.TextField(', #'xsd:anyType',
29
  'InlineType' => 'models.TextField(', #'xs:anyType',
30
  'date' => 'models.DateField(', #'xsd:date',
31
  'anyURI' => 'models.CharField(max_length=255', #'xs:anyURI',
32
  'boolean' => 'models.NullBooleanField(',
33
  'token' => 'models.CharField(max_length=255',
34
  'language' => 'models.CharField(max_length=255',
35
  'decimal' => 'models.FloatField(', #'xsd:decimal',
36
  'gYear' => 'models.DateField(', #'xsd:gYear',
37
  'string' => 'models.CharField(max_length=255',
38
  'permission' => 'models.CharField(max_length=255',
39
  'positiveInteger' => 'models.PositiveIntegerField(', #'xs:positiveInteger',
40
  'time' => 'models.TimeField(', #'xs:time',
41
  'double' => 'models.FloatField(', #'xs:double',
42
  'integer' => 'models.IntegerField(', #'xs:integer',
43
  'Name' => 'models.CharField(max_length=255', #'xs:Name',
44
  'NMTOKEN' => 'models.CharField(max_length=255', #'xs:NMTOKEN',
45
  'DateTimeISO' => 'models.CharField(max_length=255',
46
  'dayOfYearDataType' => 'models.IntegerField(', #'xs:integer',
47
  'DescriptorType' => 'models.CharField(max_length=255',
48
  'FunctionType' => 'models.CharField(max_length=255',
49
  'GRingType' => 'models.TextField(', #'xs:anyType',
50
  'IDType' => 'models.CharField(max_length=255',
51
  'KeyTypeCode' => 'models.CharField(max_length=255',
52
  'MEDIUMINT' => 'models.FloatField(', #'xs:decimal',
53
  'NomenclaturalCodesEnum' => 'models.CharField(max_length=255', #'xsd:Name',
54
  'NomenclaturalTypeStatusOfUnitsEnum' => 'models.CharField(max_length=255', #'xsd:Name',
55
  'positiveDouble' => 'models.FloatField(', #'xs:double',
56
  'probabilityType' => 'models.FloatField(', #'xs:double',
57
  'ScopeType' => 'models.CharField(max_length=255',
58
  'SMALLINT' => 'models.FloatField(', #'xs:decimal',
59
  'STRATUMINDEX' => 'models.CharField(max_length=255',
60
  'SystemType' => 'models.CharField(max_length=255',
61
  'TaxonomicRankAboveSuperfamilyEnum' => 'models.CharField(max_length=255', #'xsd:Name',
62
  'TaxonomicRankBelowSubspeciesEnum' => 'models.CharField(max_length=255', #'xsd:Name',
63
  'TaxonomicRankCultivatedPlants' => 'models.CharField(max_length=255', #'xsd:Name',
64
  'TaxonomicRankFamilyGroupEnum' => 'models.CharField(max_length=255', #'xsd:Name',
65
  'TaxonomicRankFamilySubdivisionEnum' => 'models.CharField(max_length=255', #'xsd:Name',
66
  'TaxonomicRankGenusGroupEnum' => 'models.CharField(max_length=255', #'xsd:Name',
67
  'TaxonomicRankGenusSubdivisionEnum' => 'models.CharField(max_length=255', #'xsd:Name',
68
  'TaxonomicRankSpeciesGroupEnum' => 'models.CharField(max_length=255', #'xsd:Name',
69
);
70

  
71
$ignoreTypes = array(
72
  "description"         => true,
73
  "example"             => true,
74
  "moduleDescription"   => true,
75
  "moduleDocs"          => true,
76
  "moduleName"          => true,
77
  "recommendedUsage"    => true,
78
  "standAlone"          => true,
79
  "summary"             => true,
80
  "tooltip"             => true,
81
  "annotation"          => true,
82
  "appinfo"             => true,
83
  "documentation"       => true,
84
  "import"              => true,
85
  "include"             => true,
86
  "schema"              => true,
87
  "AdditionalAbbreviations" => true,
88
  "BacteriaStatus"          => true,
89
  "BioCodeStatus"           => true,
90
  "BotanyStatus"            => true,
91
  "li"                      => true,
92
  "p"                       => true,
93
  "para"                    => true,
94
  "PreferredAbbreviation"   => true,
95
  "section"                 => true,
96
  "Specification"           => true,
97
  "SuffixAlgae"             => true,
98
  "SuffixAnimalia"          => true,
99
  "SuffixBacteriae"         => true,
100
  "SuffixFungi"             => true,
101
  "SuffixFungi"             => true,
102
  "SuffixPlantae"           => true,
103
  "title"                   => true,
104
  "ul"                      => true,
105
  "#text"                   => true,
106
  "#comment"                => true,
107
  "ZoologyStatus"           => true
108
);
109

  
110

  
111
$knownAttributes = array(
112
  "minOccurs" => True,
113
  "maxOccurs" => True,
114
  "default"   => True,
115
  "use"       => True,
116
  "mixed"     => True,
117
  "name"      => True,
118
  "type"      => True,
119
  "namespace" => True,
120
  "ref"       => True,
121
  "nillable"  => True,
122

  
123
  "base"      => True,
124
  #not sure about what to do with these, probably nothing
125
  "processContents"  => True
126
);
127

  
128
?>
makeVegxModel/trunk/objects/EntityList.php
1
<?php
2
require_once('objects/Entity.php');
3

  
4
class EntityList {
5
  private $entities = array();
6

  
7
  public function newEntity($name,$node = null) {
8
    if($name == '') {
9
      throw new Exception("Empty strings not allowed as entity names.");
10
    }
11

  
12
    if(array_key_exists($name,$this->entities)) {
13
      if($this->entities[$name]->isInited()) {
14
        throw new Exception("Entity $name already exists!");
15
      } else {
16
        $this->entities[$name]->init($name,$node);
17
        return $this->entities[$name];
18
      }
19
    }
20
    $e = new Entity($name,$node);
21
    $this->entities[$name] = $e;
22
    return $this->entities[$name];
23
  }
24

  
25
  public function getEntityForReference($name,$node = null) {
26
    if($name == '') {
27
      throw new Exception("Empty strings not allowed as entity names.");
28
    }
29

  
30
    if(array_key_exists($name,$this->entities)) {
31
      return $this->entities[$name];
32
    }
33
    $e = new Entity($name,$node);
34
    $this->entities[$name] = $e;
35
    return $this->entities[$name];
36
  }
37

  
38
  public function printAll() {
39
    foreach(array_keys($this->entities) as $k) {
40
      print $this->entities[$k]->toString() . "\n\n";
41
    }
42
  }
43

  
44
  public function getEntities() {
45
    return $this->entities;
46
  }
47
}
48
?>
makeVegxModel/trunk/util/zzz2
1
<doc:description> <-- IGNORE FOR DATA MODEL
2
<doc:example> <-- IGNORE FOR DATA MODEL
3
<doc:moduleDescription> <-- IGNORE FOR DATA MODEL
4
<doc:moduleDocs> <-- IGNORE FOR DATA MODEL
5
<doc:moduleName> <-- IGNORE FOR DATA MODEL
6
<doc:recommendedUsage/> <-- IGNORE FOR DATA MODEL
7
<doc:standAlone> <-- IGNORE FOR DATA MODEL
8
<doc:summary> <-- IGNORE FOR DATA MODEL
9
<doc:tooltip> <-- IGNORE FOR DATA MODEL
10
<xsd:annotation> <-- IGNORE FOR DATA MODEL
11
<xsd:any *
12
<xsd:anyAttribute *
13
<xsd:appinfo <-- IGNORE FOR DATA MODEL
14
<xsd:attribute *
15
<xsd:choice  *
16
<xsd:complexContent  *
17
<xsd:complexType <-- should fail if no name attribute & not nested in another complexType
18
<xsd:documentation <-- IGNORE FOR DATA MODEL
19
<xsd:element *
20
<xsd:enumeration <-- MAY IMPLEMENT ENUMS LATER, CAN IGNORE FOR NOW
21
<xsd:extension *
22
<xsd:fractionDigits <-- should not be encountered
23
<xsd:group *
24
<xsd:import <-- IGNORE FOR DATA MODEL
25
<xsd:include <-- IGNORE FOR DATA MODEL
26
<xsd:list *
27
<xsd:maxInclusive *
28
<xsd:minExclusive *
29
<xsd:minInclusive *
30
<xsd:pattern *
31
<xsd:restriction *
32
<xsd:schema <-- IGNORE FOR DATA MODEL
33
<xsd:sequence *
34
<xsd:simpleContent> *
35
<xsd:simpleType *
36
<xsd:union *
makeVegxModel/trunk/util/utilityFunctions.php
1
<?php
2
require_once('objects/types.php');
3

  
4
function isIgnoreType($nodeName) {
5
  global $ignoreTypes;
6
  return $ignoreTypes[$nodeName];
7
}
8

  
9
function isPrimitiveType($nodeName) {
10
  global $primitiveTypes;
11
  return $primitiveTypes[$nodeName];
12
}
13

  
14
function isKnownAttribute($attrName) {
15
  global $knownAttributes;
16
  return $knownAttributes[$attrName];
17
}
18

  
19
function hasImportantNodes($node) {
20
  $nodes = $node->getElementsbyTagName("*");
21
  foreach($nodes as $node) {
22
    $nodeName = preg_replace("/.*:/","",$node->nodeName);
23
    if(!isIgnoreType($nodeName)) {
24
      return true;
25
    }
26
  }
27
  return false;
28
}
29

  
30
function stringXMLNode($node) {
31
  return $node->ownerDocument->saveXML($node);
32
}
33

  
34
function findFirstAncestorName($node) {
35
  $xpath = new DOMXPath($node->ownerDocument);
36
  $parentNodes = $xpath->query("parent::*",$node);
37
  $parentNode = $parentNodes->item(0);
38
  $name = $parentNode->getAttribute('name');
39
  if($name != '') {
40
    return $name;
41
  } else {
42
    return findFirstAncestorName($parentNode);
43
  }
44
}
45

  
46
?>
makeVegxModel/trunk/util/allUsedTypes.php
1
<?php
2
$allUsedTypes = array(
3
'AccessRule' => 1,
4
'AccessType' => 1,
5
'AccordingToType' => 1,
6
'Address' => 1,
7
'AgentNames' => 1,
8
'anyType' => 1,
9
'anyURI' => 1,
10
'Article' => 1,
11
'AudioVisual' => 1,
12
'Book' => 1,
13
'CanonicalAuthorship' => 1,
14
'CanonicalName' => 1,
15
'Chapter' => 1,
16
'CitationType' => 1,
17
'complexUserDefinedType' => 1,
18
'ConferenceProceedings' => 1,
19
'ConnectionDefinitionType' => 1,
20
'Coverage' => 1,
21
'date' => 1,
22
'decimal' => 1,
23
'decimalLatitudeDataType' => 1,
24
'decimalLongitudeDataType' => 1,
25
'DEFCATEGORIE' => 1,
26
'DistributionType' => 1,
27
'double' => 1,
28
'Generic' => 1,
29
'GeographicCoverage' => 1,
30
'geospatialType' => 1,
31
'GRingPointType' => 1,
32
'gYear' => 1,
33
'InlineType' => 1,
34
'integer' => 1,
35
'ListType' => 1,
36
'Manuscript' => 1,
37
'Map' => 1,
38
'MeasurementType' => 1,
39
'Name' => 1,
40
'NameCitation' => 1,
41
'NMTOKEN' => 1,
42
'NomenclaturalNoteType' => 1,
43
'noteType' => 1,
44
'ParagraphType' => 1,
45
'partyWithRoleType' => 1,
46
'PERCENT' => 1,
47
'percentCoverType' => 1,
48
'Person' => 1,
49
'PersonalCommunication' => 1,
50
'PlaceholderType' => 1,
51
'positiveInteger' => 1,
52
'Presentation' => 1,
53
'ReferenceType' => 1,
54
'relatedItemType' => 1,
55
'relatedPlotType' => 1,
56
'RelationshipType' => 1,
57
'relativeSpatialCoordinateType' => 1,
58
'Report' => 1,
59
'ResearchProjectType' => 1,
60
'ResponsibleParty' => 1,
61
'RoleType' => 1,
62
'ScientificName' => 1,
63
'SectionType' => 1,
64
'simpleUserdefinedType' => 1,
65
'SingleDateTimeType' => 1,
66
'spatialFitDataType' => 1,
67
'string' => 1,
68
'SubSuperScriptType' => 1,
69
'TaxonConceptType' => 1,
70
'TaxonomicClassificationType' => 1,
71
'TaxonomicCoverage' => 1,
72
'TaxonomicRank' => 1,
73
'TemporalCoverage' => 1,
74
'TextType' => 1,
75
'Thesis' => 1,
76
'time' => 1,
77
'TINYINT' => 1,
78
'vegetationStructureType' => 1,
79
'yearDate' => 1
80
);
81

  
82
?>
makeVegxModel/trunk/util/dataTypes.php
1
<?php
2
$ignoreTypes = array(
3
  "description"         => true,
4
  "example"             => true,
5
  "moduleDescription"   => true,
6
  "moduleDocs"          => true,
7
  "moduleName"          => true,
8
  "recommendedUsage"    => true,
9
  "standAlone"          => true,
10
  "summary"             => true,
11
  "tooltip"             => true,
12
  "annotation"          => true,
13
  "appinfo"             => true,
14
  "documentation"       => true,
15
  "import"              => true,
16
  "include"             => true,
17
  "schema"              => true,
18
  "AdditionalAbbreviations" => true,
19
  "BacteriaStatus"          => true,
20
  "BioCodeStatus"           => true,
21
  "BotanyStatus"            => true,
22
  "li"                      => true,
23
  "p"                       => true,
24
  "para"                    => true,
25
  "PreferredAbbreviation"   => true,
26
  "section"                 => true,
27
  "Specification"           => true,
28
  "SuffixAlgae"             => true,
29
  "SuffixAnimalia"          => true,
30
  "SuffixBacteriae"         => true,
31
  "SuffixFungi"             => true,
32
  "SuffixFungi"             => true,
33
  "SuffixPlantae"           => true,
34
  "title"                   => true,
35
  "ul"                      => true,
36
  "#text"                   => true,
37
  "#comment"                => true,
38
  "ZoologyStatus"           => true
39
);
40
            
41
function isIgnoreType($nodeName) {
42
  global $ignoreTypes;
43
  return $ignoreTypes[$nodeName];
44
}
45

  
46
$files = array(
47
               'eml-access.xsd',
48
               'eml-coverage.xsd',
49
               'eml-documentation.xsd',
50
               'eml-literature.xsd',
51
               'eml-party.xsd',
52
               'eml-project.xsd',
53
               'eml-resource.xsd',
54
               'eml-text.xsd',
55
               'tcsv101.xsd',
56
               'tdwg_basetypes.xsd',
57
               'tdwg_dw_element.xsd',
58
               'tdwg_dw_geospatial.xsd',
59
               'veg-misc.xsd',
60
               'veg-organismobservation.xsd',
61
               'veg-plotobservation.xsd',
62
               'veg.xsd',
63
               'veg-plot.xsd');
64

  
65

  
66
function hasImportantNodes($node) {
67
  $nodes = $node->getElementsbyTagName("*");
68
  foreach($nodes as $node) {
69
    $nodeName = preg_replace("/.*:/","",$node->nodeName);
70
    if(!isIgnoreType($nodeName)) {
71
      return true;
72
    }
73
  }
74
  return false;
75
}
76

  
77
foreach($files as $file) {
78
  $xmlDoc = new DOMDocument();
79
  $xmlDoc->load($file);
80
  $nodes = $xmlDoc->getElementsByTagName("*");
81
  foreach($nodes as $node) {
82
    $nodeName = preg_replace("/.*:/","",$node->nodeName);
83
    if(!isIgnoreType($nodeName)) {
84
      #if($nodeName == 'element' || $nodeName == 'restriction' || $nodeName == 'extension') {
85
      if($nodeName == 'attribute') {
86
        $baseName = $node->getAttribute('base');
87
        $baseName = preg_replace("/.*:/","",$baseName);
88
        $typeName = $node->getAttribute('type');
89
        $typeName = preg_replace("/.*:/","",$typeName);
90
        if($baseName != '') {
91
          print $baseName . "\n";
92
        } else if($typeName != '') {
93
          print $typeName . "\n";
94
        }
95
      }
96
    }
97
  }
98
}
99
?>
100

  
makeVegxModel/trunk/util/nodeCount.php
1
<?php
2
$ignoreTypes = array(
3
                     "doc:description"         => true,
4
                     "doc:example"             => true,
5
                     "doc:moduleDescription"   => true,
6
                     "doc:moduleDocs"          => true,
7
                     "doc:moduleName"          => true,
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff