File: ENode.pl

package info (click to toggle)
entity 0.7.2-6
  • links: PTS
  • area: main
  • in suites: woody
  • size: 5,352 kB
  • ctags: 5,272
  • sloc: ansic: 61,707; sh: 7,921; makefile: 732; perl: 399
file content (513 lines) | stat: -rw-r--r-- 10,277 bytes parent folder | download
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
package ENode;

use Carp;

# This lets us printout and veiw $node as the $node->{path}
# as long as we are using it as a string.
use overload 
    '""' 	=> \&path,
    '!'		=> \&isnotnode,
    'bool' 	=> \&isanode;

#print ("Loading ENode.pl\n");

# Used internally to export methods to other namespaces.
sub import
{
    my ($caller_package) = caller;

    foreach $method (@_) {
        *{"${caller_package}::${method}"} = *{"ENode::${method}"};
    }
}

# Create a new node given a path (this one is exported)
sub enode
{
    my ($path) = @_;
    return (ENode::new ('ENode', $path));
}

sub enode_rx
{
    my ($regex) = @_;
    
    my $obj = ENode::new ('ENode', "object");
    if (!$obj) {
	return undef;
    }
    
    return ($obj->child_rx ($regex));
}

sub elist
{
    my ($basename, $search) = @_;

    my $obj = ENode::new ('ENode', "object");
    if (!$obj) {
	return undef;
    }
    
    return ($obj->children ($basename, $search));
}

sub elist_rx
{
    my ($regex) = @_;

    my $obj = ENode::new ('ENode', "object");
    if (!$obj) {
	return undef;
    }
    
    return ($obj->children_rx ($regex));
}


# Create a new enode object.
sub new
{
    my ($class, $path) = @_;
    my ($mytype, $myname);

    my $__enodeptr = Entity::enode_ptr ($path);
    
    my $self = {};
    bless ($self, $class);
    
    $self->{__enodeptr} = $__enodeptr;
    
    if ($self->{__enodeptr} != 0) {
        Entity::enode_ref ($self->{__enodeptr});
    }

    return ($self);
}

# for use from inside entity 
sub new_from_ptr
{
    my ($class, $ptr) = @_;
   
    if ($ptr == 0) {
	return;
    }

    my $self = {};
    bless ($self, $class);
    
    $self->{__enodeptr} = $ptr;

    Entity::enode_ref ($self->{__enodeptr});
    
    return ($self);
}

# destructor for releasing node reference 
sub DESTROY
{
    my ($self) = @_;
    
    if ($self->{__enodeptr} != 0) {
        Entity::enode_unref ($self->{__enodeptr});
    }
}

# Call a function in another object/lang.
sub call
{
    my $self = shift;
    node_broken ($self);
    
    return ( Entity::enode_call ($self->{__enodeptr}, @_) );
}


# Get/set attributes for a node
sub attrib
{
    my $self = shift;
    node_broken ($self);

    my ($attribute, $value, $i);
    
    # I _think_ this way is more efficient as it avoids excessive copies.
    # We are already copying all the attributes in when we call the method...
    
    if (@_ == 1) {
	$attribute = shift;
	return (Entity::enode_attrib ($self->{__enodeptr}, $attribute));
    } else {
	for ($i = 0; $i < @_; $i += 2) {
	    $attribute = $_[$i];
	    $value = $_[$i + 1];
	    Entity::enode_attrib ($self->{__enodeptr}, $attribute, $value);
	}
    }
}

sub attrib_quiet
{
    my $self = shift;
    node_broken ($self);

    my ($attribute, $value, $i);
    
    # I _think_ this way is more efficient as it avoids excessive copies.
    # We are already copying all the attributes in when we call the method...
    
    if (@_ == 1) {
	$attribute = shift;
	return (Entity::enode_attrib_quiet ($self->{__enodeptr}, $attribute));
    } else {
	for ($i = 0; $i < @_; $i += 2) {
	    $attribute = $_[$i];
	    $value = $_[$i + 1];
	    Entity::enode_attrib_quiet ($self->{__enodeptr}, $attribute, $value);
	}
    }
}

# Check the truth of an atrrib. I wonder if we really need this function.
sub attrib_is_true
{
    my $self = shift;
    node_broken ($self);

    Entity::enode_attrib_is_true ($self->{__enodeptr}, shift);
}

# Setup the attribs, only needed by userrend.
sub attribs_sync
{
    my $self = shift;
    node_broken ($self);

    Entity::enode_attribs_sync ($self->{__enodeptr});
}

# Get data of a node
sub get_xml
{
    my $self = shift;
    node_broken ($self);
    return (Entity::enode_get_xml ($self->{__enodeptr}));
}

# Get data of a node
sub get_child_xml
{
    my $self = shift;
    node_broken ($self);
    return (Entity::enode_get_child_xml ($self->{__enodeptr}));
}

# Append new xml
sub append_xml
{
    my ($self, $xml) = @_;
    node_broken ($self);
    Entity::enode_append_xml ($self->{__enodeptr}, $xml);
}

# Delete tree
sub delete
{
    my ($self) = @_;
    node_broken ($self);

    print ("The delete method of the ENode class is depricated, please use destroy instead.\n");
    Entity::enode_delete ($self->{__enodeptr});
    # Normally, you'd think that setting the pointer to NULL
    # at this point would make sense, but we have to let the
    # reference counting handle this, or the node will never
    # really get deleted.
    #$self->{__enodeptr} = 0;
}

sub delete_children
{
    my ($self) = @_;
    node_broken ($self);
    
    print ("The delete_children method of the ENode class is depricated, please use destroy_children instead.\n");

    Entity::enode_delete_children ($self->{__enodeptr});
}

# Delete tree
sub destroy
{
    my ($self) = @_;
    node_broken ($self);

    Entity::enode_destroy ($self->{__enodeptr});
    # Normally, you'd think that setting the pointer to NULL
    # at this point would make sense, but we have to let the
    # reference counting handle this, or the node will never
    # really get deleted.
    #$self->{__enodeptr} = 0;
}

sub destroy_children
{
    my ($self) = @_;
    node_broken ($self);

    Entity::enode_destroy_children ($self->{__enodeptr});
}

sub new_child
{
    my $self = shift;
    my $type = shift;    
    my $__enodeptr;
    node_broken ($self);

    my $__enodeptr = Entity::enode_new_child ($self->{__enodeptr}, $type, @_);
    return (ENode::new_from_ptr ('ENode', $__enodeptr));
}

sub child
{
    my ($self, $search) = @_; 
    node_broken ($self);
   
    my $__enodeptr = Entity::enode_child ($self->{__enodeptr}, $search);
    return (ENode::new_from_ptr ('ENode', $__enodeptr));
}

sub child_rx
{
    my ($self, $search) = @_; 
    node_broken ($self);
   
    my $__enodeptr = Entity::enode_child_rx ($self->{__enodeptr}, $search);
    return (ENode::new_from_ptr ('ENode', $__enodeptr));
}

sub children
{
    my ($self, $search) = @_;
    my (@nodes, $enodeptr, @list);
    node_broken ($self);
    
    if (defined ($search)) {
	@list = Entity::enode_children ($self->{__enodeptr}, $search);
    } else {
	@list = Entity::enode_children ($self->{__enodeptr});
    }
    
    foreach $enodeptr (@list) {
	if ($enodeptr != 0) {
	    push @nodes, ENode::new_from_ptr ('ENode', $enodeptr);
	}
    }
    
    return @nodes;
}

sub children_rx
{
    my ($self, $regex) = @_;
    my (@nodes, $__enodeptr, @list);
    node_broken ($self);
    
    @list = Entity::enode_children_rx ($self->{__enodeptr}, $regex);
	
    foreach $__enodeptr (@list) {
	if ($__enodeptr != 0) {
	    push @nodes, ENode::new_from_ptr ('ENode', $__enodeptr);
	}
    }
    
    return @nodes;
}

sub children_attrib
{
    my ($self, $attrib, $value) = @_;
    my (@nodes, $__enodeptr, @list);
    node_broken ($self);
    
    @list = Entity::enode_children_attrib ($self->{__enodeptr}, $attrib, $value);
	
    foreach $__enodeptr (@list) {
	if ($__enodeptr != 0) {
	    push @nodes, ENode::new_from_ptr ('ENode', $__enodeptr);
	}
    }
    
    return @nodes;
}

sub children_attrib_rx
{
    my ($self, $attrib, $regex) = @_;
    my (@nodes, $__enodeptr, @list);
    node_broken ($self);
    
    @list = Entity::enode_children_attrib_rx ($self->{__enodeptr}, $attrib, $regex);
	
    foreach $__enodeptr (@list) {
	if ($__enodeptr != 0) {
	    push @nodes, ENode::new_from_ptr ('ENode', $__enodeptr);
	}
    }
    
    return @nodes;
}


sub parent
{
    my ($self, $search) = @_;
    my $node;
    node_broken ($self);
    
    if (defined ($search)) {
	$node = ENode::new_from_ptr ('ENode', Entity::enode_parent ($self->{__enodeptr}, $search));
    } else {
	$node = ENode::new_from_ptr ('ENode', Entity::enode_parent ($self->{__enodeptr}));
    }
    return ($node);
}

sub get_data
{
    my ($self) = @_;
    node_broken ($self);
    
    return (Entity::enode_get_data ($self->{__enodeptr}));
}

sub set_data
{
    my ($self, $data) = @_;
    node_broken ($self);
    
    Entity::enode_set_data ($self->{__enodeptr}, $data);
}

sub append_data
{
   my ($self, $data) = @_;
   node_broken ($self);

   Entity::enode_append_data ($self->{__enodeptr}, $data);
}

sub insert_data
{
   my ($self, $offset, $data) = @_;
   node_broken ($self);

   Entity::enode_insert_data ($self->{__enodeptr}, $offset, $data);
}

sub delete_data
{
   my ($self, $offset, $count) = @_;
   node_broken ($self);

   Entity::enode_delete_data ($self->{__enodeptr}, $offset, $count);
}

sub type
{
    my ($self) = @_;
    node_broken ($self);
    return (Entity::enode_type ($self->{__enodeptr}))
}

sub basename
{
    my ($self) = @_;
    node_broken ($self);
    return (Entity::enode_type ($self->{__enodeptr}) . "." . $self->attrib ("name"))
}

sub list_set_attribs
{
    my ($self) = @_;
    node_broken ($self);
    return (Entity::enode_list_set_attribs ($self->{__enodeptr}));
}

sub supported_attribs
{
    my ($self) = @_;
    node_broken ($self);
    return (Entity::enode_supported_attribs ($self->{__enodeptr}));
}

sub attrib_description
{
    my ($self, $attrib) = @_;
    node_broken ($self);
    return (Entity::enode_attrib_description ($self->{__enodeptr}, $attrib));
}

sub attrib_value_type
{
    my ($self, $attrib) = @_;
    node_broken ($self);
    return (Entity::enode_attrib_value_type ($self->{__enodeptr}, $attrib));
}

sub attrib_possible_values
{
    my ($self, $attrib) = @_;
    node_broken ($self);
    return (Entity::enode_attrib_possible_values ($self->{__enodeptr}, $attrib));
}
    

sub write
{
    my $self = $_[0];		### Don't want to modify the args list.
    node_broken ($self);

    return (Entity::enode_attrib ($self->{__enodeptr}, "_sendq", $_[1]));
}


#### OVERLOADED OPPERATORS. ####
# Returns path of node
sub path  ## '""'
{
    my $self = shift;
    
    if (isnotnode ($self)) {
	return (undef);
    } else {
	return (Entity::enode_path ($self->{__enodeptr}));
    }
}

sub isanode  ## 'bool'
{
   my $self = shift;
   return ($self->{__enodeptr});
}

sub isnotnode ## Opposite of isanode.  '!'
{
  return ( !isanode(shift) );
}

sub node_broken  ## Called before doing anything special with a node.
{
  my $self = shift;

  if (!$self)
    {
      confess ("ENode not good.\n");
    }
}

#print ("Loaded.\n");

return 1;