This file is indexed.

/usr/share/perl5/XML/XQL/DirXQL.pm is in libxml-xql-perl 0.68-6.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
# Attibute Definitions:
#
#  name: Text - name of file, dir, ...
#  ext: Text - file extension
#  no_ext: Text - name without ext
#  full: Text - full path name
#  abs: Text - absolute path name
#
#  M,age: Number - Age of file (in days)
#                   [since script started says man(perlfunc)??]
#  cre,create: Date (see age)
#  A,acc_in_days: Number - Last access time in days
#  acc,access: Date (see A)
#    set with utime()
#  f,is_file: Boolean
#  d,is_dir: Boolean
#  l,is_link: Boolean
#  p,is_pipe: Boolean
#  e,exists: Boolean
#  z,is_zero: Boolean - whether size equals zero bytes
#  r,readable: Boolean
#  w,writable: Boolean
#  x,executable: Boolean
#  o,owned: Boolean - whether it is owned (by effective uid)
#
#---------------------------------------------------------------------------
# Todo: 
# - implement abs(): absolute filepath
# - support links: use lstat(), @link 
# - flags: -R,-W,-X,-O (by real uid/gid instead of effective uid,
#          -S (is_socket), -b (block special file), -c (char. special file),
#          -t  Filehandle is opened to a tty.
#          -u  File has setuid bit set.
#          -g  File has setgid bit set.
#          -k  File has sticky bit set.
#          -T  File is a text file.
#          -B  File is a binary file (opposite of -T).
#          -C  inode change time in days.
#              set with utime() ??
#
# stat() fields:
#
#         0 dev      device number of filesystem
#         1 ino      inode number
#         2 mode     file mode  (type and permissions)
#	    add mode_str ??: "rwxr-xr--"
#         3 nlink    number of (hard) links to the file
#         4 uid      numeric user ID of file's owner
#           add uname
#         5 gid      numeric group ID of file's owner
#           add gname
#         6 rdev     the device identifier (special files only)
# x       7 size     total size of file, in bytes
# -       8 atime    last access time since the epoch
# -       9 mtime    last modify time since the epoch
# -      10 ctime    inode change time (NOT creation time!) since the epoch
#        11 blksize  preferred block size for file system I/O
#        12 blocks   actual number of blocks allocated

package XML::XQL::DirXQL;

use strict;
use XML::XQL;
use XML::XQL::Date;

sub dirxql
{
    my ($context, $list, $filepath) = @_;

    $filepath = XML::XQL::toList ($filepath->solve ($context, $list));
    my @result;
    for my $file (@$filepath)
    {
	push @result, XML::XQL::DirDoc->new (Root => $file->xql_toString)->root;
    }
    \@result;
}

XML::XQL::defineFunction ("dirxql", \&XML::XQL::DirXQL::dirxql, 1, 1);

package XML::XQL::DirNode;
# extended by: DirDoc, DirAttr, DirElem (File, Dir), FileContents

use vars qw{ @ISA $SEP };
@ISA = qw{ XML::XQL::Node };

# Directory path separator (default: Unix)
$SEP = "/";

if ((defined $^O and
     $^O =~ /MSWin32/i ||
     $^O =~ /Windows_95/i ||
     $^O =~ /Windows_NT/i) ||
    (defined $ENV{OS} and
     $ENV{OS} =~ /MSWin32/i ||
     $ENV{OS} =~ /Windows_95/i ||
     $ENV{OS} =~ /Windows_NT/i))
{
    $SEP = "\\";	# Win32
}
elsif  ((defined $^O and $^O =~ /MacOS/i) ||
	(defined $ENV{OS} and $ENV{OS} =~ /MacOS/i))
{
    $SEP = ":";		# Mac
}

sub isElementNode { 0 }
sub isTextNode    { 0 }
sub xql_parent    { $_[0]->{Parent} }
#sub xql_document { $_[0]->{Doc} }
sub xml_xqlString { $_[0]->toString }

sub xql
{
    my $self = shift;

    # Odd number of args, assume first is XQL expression without 'Expr' key
    unshift @_, 'Expr' if (@_ % 2 == 1);
    my $query = new XML::XQL::Query (@_);
    $query->solve ($self);
}

sub xql_sortKey
{
    my $key = $_[0]->{SortKey};
    return $key if defined $key;

    $key = XML::XQL::createSortKey ($_[0]->{Parent}->xql_sortKey, 
				    $_[0]->xql_childIndex, 1);
#print "xql_sortKey $_[0] ind=" . $_[0]->xql_childIndex . " key=$key str=" . XML::XQL::keyStr($key) . "\n";
    $_[0]->{SortKey} = $key;
}

sub xql_node
{
    my $self = shift;
    $self->build unless $self->{Built};

    $self->{C};
}

sub getChildIndex
{
    my ($self, $kid) = @_;
    my $i = 0;
    for (@{ $self->xql_node })
    {
	return $i if $kid == $_;
	$i++;
    }
    return -1;
}

sub xql_childIndex
{
    $_[0]->{Parent}->getChildIndex ($_[0]);
}

# As it appears in the XML document
sub xql_xmlString
{
    $_[0]->toString;
#?? impl.
}

sub create_date_from_days
{
    my ($days, $srcNode) = @_;
    my $secs = int (0.5 + $days * 24 * 3600 );

    my $internal = Date::Manip::DateCalc ("today", "- $secs seconds");

    new XML::XQL::Date (SourceNode => $srcNode,
			Internal => $internal,
			String => $internal );
}

#------ WHITESPACE STUFF (DELETE??)

# Find previous sibling that is not a text node with ignorable whitespace
sub xql_prevNonWS
{
    my $self = shift;
    my $parent = $self->{Parent};
    return unless $parent;

    for (my $i = $parent->getChildIndex ($self) - 1; $i >= 0; $i--)
    {
	my $node = $parent->getChildAtIndex ($i);
	return $node unless $node->xql_isIgnorableWS;	# skip whitespace
    }
    undef;
}

# True if it's a Text node with just whitespace and xml::space != "preserve"
sub xql_isIgnorableWS
{
    0;
}

# Whether the node should preserve whitespace
# It should if it has attribute xml:space="preserve"
sub xql_preserveSpace
{
    $_[0]->{Parent}->xql_preserveSpace;
}

#---------------------------------------------------------------------------
package XML::XQL::DirDoc;		# The Document
use vars qw{ @ISA };
@ISA = qw{ XML::XQL::DirNode };

sub new
{
    my ($type, %hash) = @_;
    my $self = bless \%hash, $type;

    $self->{Root} = "." unless exists $self->{Root};

    my $dirname;
    if ($self->{Root} =~ /^(.+)\Q${XML::XQL::DirNode::SEP}\E(.+)$/)
    {
	$self->{Prefix} = $1;
	$dirname = $2;
    }
    else
    {
	$self->{Prefix} = "";
	$dirname = $self->{Root};
    }

    $self->{Dir} = new XML::XQL::Dir (TagName => $dirname, Parent => $self);
    $self->{Built} = 1;

    return $self;
}

sub xql
{
    shift->root->xql (@_);
}

sub root           { $_[0]->{Dir} }

sub isElementNode  { 0 }
sub xql_nodeType   { 9 }
sub xql_childCount { 1 }
sub fullname       { $_[0]->{Prefix} }
sub xql_sortKey    { "" }
sub xql_parent     { undef }
sub xql_nodeName   { "#document" }
sub depth          { 0 }
sub xql_node       { [ $_[0]->{Dir} ] }

sub xql_element
{
    my ($self, $elem) = @_;

    my $dir = $self->{Dir};
    if (defined $elem)
    {
	return [ $dir ] if $dir->{TagName} eq $elem;
    }
    else
    {
	return [ $dir ];
    }
}

# By default the elements in a document don't preserve whitespace
sub xql_preserveSpace
{
    0;
}

sub toString
{
    $_[0]->root->toString;
}

#----------------------------------------------------------------------------
package XML::XQL::DirAttrDef;	# Definitions for DirAttr nodes

sub new
{
    my ($type, %hash) = @_;
    bless \%hash, $type;
}

sub dump
{
    print $_[0]->toString . "\n";
}

sub toString
{
    my $self = shift;
    print "DirAttrDef $self\n";
    my $i = 0;
    for my $attrName ($self->in_order)
    {
	my $a = $self->{$attrName};
	print "[$i] name=$attrName"; $i++;
	print " order=" . $a->{Order};
	print " get=" . $a->{Get} if defined $a->{Get};
	print " set=" . $a->{Set} if defined $a->{Set};
	if (defined $a->{Alias})
	{
	    print " alias=" . join (",", @{ $a->{Alias} });
	}
	print "\n";
    }
    if (defined $self->{'@ALIAS'})
    {
	print "Alias: ";
	my $alias = $self->{'@ALIAS'};
	
	print join (",", map { "$_=" . $alias->{$_} } keys %$alias);
	print "\n";
    }
}

sub clone
{
    my $self = shift;
    my $n = new XML::XQL::DirAttrDef;
    $n->{'@IN_ORDER'} = [ @{ $self->{'@IN_ORDER'} } ];

    for my $a (@{ $self->{'@IN_ORDER'} })
    {
	$n->{$a} = { %{ $self->{$a} } };
	$n->{$a}->{Alias} = [ @{ $self->{$a}->{Alias} } ]
	    if defined $self->{$a}->{Alias};
    }
    $n->{'@ALIAS'} = { %{ $self->{'@ALIAS'} } }
	    if defined $self->{'@ALIAS'};

    return $n;
}

sub in_order { defined $_[0]->{'@IN_ORDER'} ? @{ $_[0]->{'@IN_ORDER'} } : () }
sub alias    { $_[0]->{'@ALIAS'}->{$_[1]} }
sub order    { $_[0]->{$_[1]}->{Order} }
sub get      { $_[0]->{$_[1]}->{Get} }
sub set      { $_[0]->{$_[1]}->{Set} }

sub remove_attr
{
    my ($self, $name) = @_;
    next unless defined $self->{$name};

    my $order = $self->{$name}->{Order};
    my @in_order = $self->in_order;
    splice @in_order, $order, 1;
    
    # Reassign Order numbers
    for (my $i = 0; $i < @in_order; $i++)
    {
	$self->{$name}->{Order} = $i;
    }
    $self->{'@IN_ORDER'} = \@in_order;
    
    delete $self->{$name};
}

sub define_attr
{
    my ($self, %hash) = @_;
    my $name = $hash{Name};

    if (defined $self->{$name})
    {
	$hash{Order} = $self->{$name}->{Order} unless defined $hash{Order};
	$self->remove_attr ($name);
    }

    my @in_order = $self->in_order;
    $hash{Order} = -1
	if $hash{Order} >= @in_order;
    
    if ($hash{Order} == -1)
    {
	push @in_order, $name;
    }
    else
    {
	splice @in_order, $hash{Order}, 0, $name;
    }
    $self->{$name} = \%hash;

    # Reassign Order numbers
    for (my $i = 0; $i < @in_order; $i++)
    {
	$self->{$name}->{Order} = $i;
    }
    $self->{'@IN_ORDER'} = \@in_order;

    my @alias = defined $hash{Alias} ? @{ $hash{Alias} } : ();
    for (@alias)
    {
	$self->{'@ALIAS'}->{$_} = $name;
    }
}

#----------------------------------------------------------------------------
package XML::XQL::DirAttr;	# Attr node
use vars qw{ @ISA %GET_ATTR_FUNC %SET_ATTR_FUNC };
@ISA = qw{ XML::XQL::DirNode };

sub new
{
    my ($type, %hash) = @_;
    my $self = bless \%hash, $type;
    
    $self->{xql_value} = $self->{Parent}->{AttrDef}->get ($hash{Name});
    $self->{xql_setValue} = $self->{Parent}->{AttrDef}->set ($hash{Name});
    $self;
}

sub isElementNode  { 0 }
sub xql_nodeType   { 2 }
sub xql_nodeName   { $_[0]->{Name} }
sub xql_childIndex { $_[0]->{Parent}->attrIndex ($_[0]->{Name}) }
sub xql_childCount { 0 }
sub xql_node       { [] }
sub is_defined     { exists $_[0]->{Value} }

sub create	{ XML::XQL::DirNode::create_date_from_days ($_[0]->{Parent}->age, $_[0]) }
sub age		{ new XML::XQL::Number ($_[0]->{Parent}->age, $_[0]) }
sub size	{ new XML::XQL::Text ($_[0]->{Parent}->size, $_[0]) }
sub ext		{ new XML::XQL::Text ($_[0]->{Parent}->ext, $_[0]) }
sub no_ext	{ new XML::XQL::Text ($_[0]->{Parent}->no_ext, $_[0]) }
sub name	{ new XML::XQL::Text ($_[0]->{Parent}->name, $_[0]) }
sub full	{ new XML::XQL::Text ($_[0]->{Parent}->full, $_[0]) }
sub abs 	{ new XML::XQL::Text ($_[0]->{Parent}->abs, $_[0]) }
sub is_file	{ new XML::XQL::Boolean ($_[0]->{Parent}->is_file, $_[0]) }
sub is_dir	{ new XML::XQL::Boolean ($_[0]->{Parent}->is_dir, $_[0]) }
sub is_link	{ new XML::XQL::Boolean ($_[0]->{Parent}->is_link, $_[0]) }
sub is_pipe	{ new XML::XQL::Boolean ($_[0]->{Parent}->is_pipe, $_[0]) }
sub it_exists	{ new XML::XQL::Boolean ($_[0]->{Parent}->it_exists, $_[0]) }
sub is_zero	{ new XML::XQL::Boolean ($_[0]->{Parent}->is_zero, $_[0]) }
sub readable	{ new XML::XQL::Boolean ($_[0]->{Parent}->readable, $_[0]) }
sub writable	{ new XML::XQL::Boolean ($_[0]->{Parent}->writable, $_[0]) }
sub executable	{ new XML::XQL::Boolean ($_[0]->{Parent}->executable, $_[0]) }
sub owned	{ new XML::XQL::Boolean ($_[0]->{Parent}->owned, $_[0]) }

sub last_access_in_days
{
    new XML::XQL::Number ($_[0]->{Parent}->last_access_in_days, $_[0]);
}

sub last_access
{ 
  XML::XQL::DirNode::create_date_from_days ($_[0]->{Parent}->last_access_in_days, $_[0]);
}

sub toString       
{ 
    my $old = ""; #$_[0]->is_defined ? "" : " (undef)";
    my $val = $_[0]->xql_value->xql_toString; #exists $_[0]->{Value} ? $_[0]->{Value}->xql_toString : "(undef)";
    $_[0]->{Name} . "=\"$val$old\""
#?? encodeAttrValue
}

sub xql_value
{
    $_[0]->{Value} ||= &{ $_[0]->{xql_value} } (@_);
}

sub xql_setValue
{
    my ($self, $text) = @_;
    my $set = $_[0]->{xql_setValue};
    if (defined $set)
    {
	&$set ($self, $text);
    }
    else
    {
	warn "xql_setValue not defined for DirAttr name=" . $self->{TagName};
    }
}

sub set_name
{
    my ($attr, $text) = @_;
    $attr->{Parent}->set_name ($text);
}

sub set_ext
{
    my ($attr, $text) = @_;
    $attr->{Parent}->set_ext ($text);
}

sub set_no_ext
{
    my ($attr, $text) = @_;
    $attr->{Parent}->set_no_ext ($text);
}

#----------------------------------------------------------------------------
package XML::XQL::DirElem;	# File or Dir
use vars qw{ @ISA $ATTRDEF };
@ISA = qw( XML::XQL::DirNode );

$ATTRDEF = new XML::XQL::DirAttrDef;
$ATTRDEF->define_attr (Name => 'name', Get => \&XML::XQL::DirAttr::name, 
		       Set => \&XML::XQL::DirAttr::set_name);
$ATTRDEF->define_attr (Name => 'full', Get => \&XML::XQL::DirAttr::full);
$ATTRDEF->define_attr (Name => 'abs', Get => \&XML::XQL::DirAttr::abs);
$ATTRDEF->define_attr (Name => 'no_ext', Get => \&XML::XQL::DirAttr::no_ext, 
		       Set => \&XML::XQL::DirAttr::set_no_ext);
$ATTRDEF->define_attr (Name => 'ext', Get => \&XML::XQL::DirAttr::ext, 
		       Set => \&XML::XQL::DirAttr::set_ext);

$ATTRDEF->define_attr (Name => 'age', Get => \&XML::XQL::DirAttr::age, 
		       Alias => [ 'M' ] );
$ATTRDEF->define_attr (Name => 'create', Get => \&XML::XQL::DirAttr::create, 
		       Alias => [ 'cre' ] );
$ATTRDEF->define_attr (Name => 'A', Get => \&XML::XQL::DirAttr::last_access_in_days,
		       Alias => [ 'acc_in_days' ] );
$ATTRDEF->define_attr (Name => 'access', Get => \&XML::XQL::DirAttr::last_access, 
		       Alias => [ 'acc' ] );

# These should only be implemented for Link and Pipe resp. !!
$ATTRDEF->define_attr (Name => 'l', Get => \&XML::XQL::DirAttr::is_link, 
		       Alias => [ 'is_link' ] );
$ATTRDEF->define_attr (Name => 'p', Get => \&XML::XQL::DirAttr::is_pipe, 
		       Alias => [ 'is_pipe' ] );

$ATTRDEF->define_attr (Name => 'e', Get => \&XML::XQL::DirAttr::it_exists, 
		       Alias => [ 'exists' ] );
$ATTRDEF->define_attr (Name => 'z', Get => \&XML::XQL::DirAttr::is_zero, 
		       Alias => [ 'is_zero' ] );
$ATTRDEF->define_attr (Name => 'r', Get => \&XML::XQL::DirAttr::readable, 
		       Alias => [ 'readable' ] );
$ATTRDEF->define_attr (Name => 'w', Get => \&XML::XQL::DirAttr::writable, 
		       Alias => [ 'writable' ] );
$ATTRDEF->define_attr (Name => 'x', Get => \&XML::XQL::DirAttr::executable, 
		       Alias => [ 'is_zero' ] );
$ATTRDEF->define_attr (Name => 'o', Get => \&XML::XQL::DirAttr::owned, 
		       Alias => [ 'owned' ] );

#dump_attr_def();

# mod => 0,
# create => 1,
# prot => 2,
# protn => 3,
# name => 4,
# path => 5,
# dir => 6,

sub isElementNode   { 1 }
sub xql_nodeType    { 1 }
sub xql_nodeName    { $_[0]->{TagName} }

sub dump_attr_def   { $ATTRDEF->dump; }
sub attrNames       { @{ $_[0]->{AttrDef}->{'@IN_ORDER'} } }
sub hasAttr         { exists $_[0]->{AttrDef}->{$_[1]} }

# Attributes set/get
sub full  		{ $_[0]->fullname }
sub abs      		{ $_[0]->abs }
sub no_ext		{ $_[0]->{TagName} }
sub set_no_ext		{ shift->set_name (@_) }
sub size		{ -s $_[0]->fullname }
sub age			{ -M $_[0]->fullname }
sub last_access_in_days	{ -A $_[0]->fullname }
sub is_file             { -f $_[0]->fullname }
sub is_dir              { -d $_[0]->fullname }
sub is_link             { -l $_[0]->fullname }
sub is_pipe             { -p $_[0]->fullname }
sub it_exists           { -e $_[0]->fullname }
sub is_zero             { -z $_[0]->fullname }
sub readable            { -r $_[0]->fullname }
sub writable            { -w $_[0]->fullname }
sub executable          { -x $_[0]->fullname }
sub owned               { -o $_[0]->fullname }

sub attr_alias    
{
    return undef unless defined $_[1];

    my $alias = $_[0]->{AttrDef}->alias ($_[1]);
    defined $alias ? $alias : $_[1];
}

sub create_path	# static
{
    my ($dir, $file) = @_;

    if ($dir =~ /\Q${XML::XQL::DirNode::SEP}\E$/)
    {
	return "$dir$file";
    }
    elsif ($dir eq "")	# e.g. when file is root directory '/'
    {
	return $file;
    }
    else
    { 
	return "$dir${XML::XQL::DirNode::SEP}$file";
    }
}

sub fullname
{ 
    my $pa = $_[0]->{Parent}->fullname;
    my $name = $_[0]->{TagName};
    create_path ($pa, $name);
}

#?? same as full name - for now
sub abs
{
    shift->fullname (@_);
}

sub parent_dir
{
    $_[0]->{Parent}->fullname;
}

# With 3 params, sets the specified attribute with $attrName to $attrValue.
# With 2 params, reinitializes the specified attribute with $attrName if
# it currently has a value.

sub update_attr
{
    my ($self, $attrName, $attrValue) = @_;

    if (@_ == 3)
    {
	my $attr = $self->getAttributeNode ($attrName);
	if (defined $attr && defined $attr->{Value})
	{
	    $attr->{Value} = $attrValue;
	}
    }
    else
    {
	return unless exists $self->{A}->{$attrName};
	my $a = $self->{A}->{$attrName};
	if (exists $a->{Value})
	{
	    delete $a->{Value};
	    $a->xql_value;	# reinitialize value
	}
    }
}

sub set_name
{
    my ($self, $text) = @_;
    my $fullName = $self->fullname;
    my $newName = create_path ($self->parent_dir, $text);

    if (rename ($fullName, $newName))
    {
	$self->{TagName} = $text;
	$self->update_attr ('name', $text);
	$self->update_attr ('ext');
	$self->update_attr ('no_ext');

	return 1;
    }
    else
    {
	warn "set_name: could not rename $fullName to $newName";
	return 0;
    }
}

sub ext
{
    my $name = $_[0]->{TagName};
    $name =~ /\.([^.]+)$/;
#    print "ext name=$name ext=$1\n";
    return $1;
}

sub set_ext
{
    my ($self, $text) = @_;
#    print "set_ext $text\n";
    my $no_ext = $self->no_ext;
    $self->set_name (length ($text) ? "$no_ext.$text" : $no_ext);
}

sub no_ext
{
    my $name = $_[0]->{TagName};
    $name =~ /^(.+)\.([^.]+)$/;
#    print "no_ext name=$name no_ext=$1\n";
    return $1;
}

sub set_no_ext
{
    my ($self, $text) = @_;
#    print "set_no_ext $text\n";
    my $ext = $self->ext;
    $self->set_name (length ($ext) ? "$text.$ext" : $text);
}

sub xql_attribute
{
    my ($node, $attrName) = @_;
    if (defined $attrName)
    {
	my $attr = $node->getAttributeNode ($attrName);
	defined ($attr) ? [ $attr ] : [];
    }
    else
    {
	my @attr;
	for my $name ($node->attrNames)
	{
	    push @attr, $node->getAttributeNode ($name);
	}
	\@attr;
    }
}

sub getAttributeNode
{
    my ($self, $attrName) = @_;
    $attrName = $self->attr_alias ($attrName);

    return undef unless $self->hasAttr ($attrName);

    my $attr = $_[0]->{A}->{$attrName} ||= 
	new XML::XQL::DirAttr (Parent => $self, Name => $attrName);
    $attr;
}

sub attrIndex
{
    $_[0]->{AttrDef}->order ($_[1]);
}

sub toString       
{ 
    my ($self, $depth) = @_;
    my $indent = "  " x $depth;
    my $str = $indent;
    my $tagName = $self->{TagName};

    my $tfp = $self->tag_for_print;

    $str .= "<$tfp name=\"$tagName\"";

    for my $attrName ($self->attrNames)
    {
	next unless exists $self->{A}->{$attrName};

#?? don't print un-retrieved attributes - for now	
	my $attr = $self->{A}->{$attrName};
	next unless $attr->is_defined;
	
	$str .= " " . $attr->toString;
    }

    my $kids = $self->print_kids ? $self->xql_node : [];
    if (@$kids)
    {
	$str .= ">\n";
	for (@$kids)
	{
	    $str .= $_->toString ($depth + 1);
	}
	$str .= $indent . "</dir>\n";
    }
    else
    {
	$str .= "/>\n";
    }
}

#----------------------------------------------------------------------------
package XML::XQL::Dir;	# Element node
use vars qw{ @ISA $ATTRDEF };
@ISA = qw( XML::XQL::DirElem );

$ATTRDEF = $XML::XQL::DirElem::ATTRDEF->clone;
$ATTRDEF->define_attr (Name => 'd', Get => \&XML::XQL::DirAttr::is_dir, 
		       Alias => [ 'is_dir' ] );
#dump_attr_def();

sub tag_for_print { "dir" }
sub print_kids    { 1 }
sub dump_attr_def { $ATTRDEF->dump }

sub new
{
    my ($type, %hash) = @_;
    $hash{AttrDef} = $ATTRDEF;
    bless \%hash, $type;
}

sub build
{
    my ($self) = @_;
    my $dirname = $self->fullname;
#    print "dirname=$dirname\n";

    if (opendir (DIR, $dirname))
    {
	my @kids;

	my @f = readdir (DIR);
	closedir DIR;

	for my $f (@f)
	{
	    next if $f =~ /^..?$/;
#	    print "dirname=$dirname f=$f\n";

	    my $full = defined $dirname ? "$dirname${XML::XQL::DirNode::SEP}$f" : $f;
#	    print "dirname=$dirname full=$full\n";

	    if (-f $full)
	    {
		push @kids, XML::XQL::File->new (Parent => $self, 
						 TagName => $f
						);
	    }
	    elsif (-d _)
	    {
		push @kids, XML::XQL::Dir->new (Parent => $self, 
						TagName => $f
					       );
	    }
	}
	$self->{C} = \@kids;
	$self->{Built} = 1;
    }
    else
    {
	print "can't opendir $dirname: $!";
    }
}

sub xql_childCount
{
    my $self = shift;
    $self->build unless $self->{Built};
    my $ch = $self->{C};

    defined $ch ? scalar(@$ch) : 0;
}

#----------------------------------------------------------------------------
package XML::XQL::File;	# Element node
use vars qw{ @ISA $ATTRDEF };
@ISA = qw( XML::XQL::DirElem );

$ATTRDEF = $XML::XQL::DirElem::ATTRDEF->clone;
$ATTRDEF->define_attr (Name => 'f', Get => \&XML::XQL::DirAttr::is_file, 
		       Alias => [ 'is_file' ] );
$ATTRDEF->define_attr (Name => 'size', Get => \&XML::XQL::DirAttr::size, 
		       Alias => [ 's' ]);
#dump_attr_def();

sub new
{
    my ($type, %hash) = @_;
    $hash{AttrDef} = $ATTRDEF;
    bless \%hash, $type;
}

sub getChildIndex  { 0 }
sub xql_childCount { 1 }
sub contents       { $_[0]->build unless $_[0]->{Built}; $_[0]->{C}->[0] }
sub xql_text       { $_[0]->contents->xql_text }
sub xql_rawText    { $_[0]->contents->xql_text }
sub tag_for_print  { "file" }
sub print_kids     { 0 }
sub dump_attr_def  { $ATTRDEF->dump }

sub xql_rawTextBlocks
{
    my $self = shift;
    ( [ 0, 0, $self->xql_text ])
}

sub xql_setValue
{
    my ($self, $text) = @_;
    $self->contents->xql_setValue ($text);
}

sub xql_replaceBlockWithText
{
    my ($self, $start, $end, $text) = @_;
    if ($start == 0 && $end == 0)
    {
	$self->xql_setValue ($text);
    }
    else
    {
	warn "xql_setText bad index start=$start end=$end";
    }
}

sub build
{
    my $self = shift;
    push @{ $self->{C} }, XML::XQL::FileContents->new (Parent => $self);
    $self->{Built} = 1;
}

#----------------------------------------------------------------------------
package XML::XQL::FileContents;	# Text node
use vars qw{ @ISA };
@ISA = qw{ XML::XQL::DirNode };

sub new
{
    my ($type, %hash) = @_;
    bless \%hash, $type;
}

sub isTextNode     { 1 }
sub xql_nodeType   { 3 }
sub xql_nodeName   { "#contents" }
sub getChildIndex  { 0 }
sub xql_childCount { 0 }
sub xql_rawText    { $_[0]->xql_text }

sub xql_text
{
    my $self = shift;
    unless ($self->{Built})
    {
	local *FILE;
	local $/;	# slurp mode

	if (open (FILE, $self->{Parent}->fullname))
	{
	    $self->{Data} = <FILE>;
	    close FILE;
	}
	else
	{
#?? warning
	}
	$self->{Built} = 1;
    }
    $self->{Data};
}

sub xql_setValue
{
    my ($self, $text) = @_;

    my $filename = $self->{Parent}->fullname;
    local *FILE;
    if (open (FILE, ">$filename"))
    {
	print FILE $text;
	$self->{Data} = $text;
	$self->{Built} = 1;
	close FILE;
    }
    else
    {
	warn "xql_setValue could not open $filename for writing";
    }
}

return 1;