This file is indexed.

/usr/share/perl5/XML/SAX/Machine.pm is in libxml-sax-machines-perl 0.46-1.

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
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
package XML::SAX::Machine;
{
  $XML::SAX::Machine::VERSION = '0.46';
}
# ABSTRACT: Manage a collection of SAX processors



use strict;

use constant has_named_regexp_character_classes => $] > 5.006000;

use Carp;
use UNIVERSAL;
use XML::SAX::EventMethodMaker qw( :all );
use XML::SAX::Machines;

## Tell the config stuff what options we'll be requesting, so we
## don't get typoes in this code.  Very annoying, but I mispelt it
## so often, that adding one statement like this seemed like a low
## pain solution, since testing options like this can be long and
## bothersome.
XML::SAX::Machines->expected_processor_class_options(qw(
    ConstructWithHashedOptions
));



sub new {
    my $proto = shift;
    my $class = ref $proto || $proto;

    my @options_if_any = @_ && ref $_[-1] eq "HASH" ? %{pop()} : ();
    my $self = bless { @options_if_any }, $class;

    $self->{Parts} = [];
    $self->{PartsByName} = {};  ## Mapping of names to parts

    $self->_compile_specs( @_ );

    ## Set this last in case any specs have handler "Exhaust"
    $self->set_handler( $self->{Handler} ) if $self->{Handler};

    return $self;
}


sub _find_part_rec {
    my $self = shift; 
    my ( $id ) = @_;

    if ( ref $id ) {
        return exists $self->{PartsByProcessor}->{$id}
            && $self->{PartsByProcessor}->{$id};
    }

    if ( $id =~ /^[+-]?\d+(?!\n)$/ ) {
        return undef
            if     $id >     $#{$self->{Parts}}
                || $id < - ( $#{$self->{Parts}} + 1 );
        return $self->{Parts}->[$id];
    }

    return $self->{PartsByName}->{$id}
        if exists $self->{PartsByName}->{$id};

    return undef;
}


sub find_part {
    my $self = shift; 
    my ( $spec ) = @_;

    return $self->{Handler} if $spec eq "Exhaust";

    my $part_rec;

    if ( 0 <= index $spec, "/" ) {
        ## Take the sloooow road...
        require File::Spec::Unix;
        croak "find_part() path not absolute: '$spec'"
            unless File::Spec::Unix->file_name_is_absolute( $spec );

        ## Cannonical-ize it, do /foo/../ => / conversion
        $spec = File::Spec::Unix->canonpath( $spec );
        1 while $spec =~ s{/[^/]+/\.\.(/|(?!\n\Z))}{$1};

        my @names = File::Spec::Unix->splitdir( $spec );
        pop @names while @names && ! length $names[-1];
        shift @names while @names && ! length $names[0];

        croak "invalid find_part() specification: '$spec'"
            unless File::Spec::Unix->file_name_is_absolute( $spec );

        my @audit_trail;
        my $proc = $self;
        for ( @names ) {
            push @audit_trail, $_;
            $part_rec = $proc->_find_part_rec( $_ );
            unless ( $part_rec ) {
                croak "find_path() could not find '",
                    join( "/", "", @audit_trail ),
                    "' in ", ref $self;
            }
            $proc = $part_rec->{Processor};
        }
    }
    else {
        $part_rec = $self->_find_part_rec( $spec );
    }

    croak "find_path() could not find '$spec' in ", ref $self
        unless $part_rec;

    my $proc = $part_rec->{Processor};

    ## Be paranoid here, just in case we have a bug somewhere.  I prefer
    ## getting reasonable bug reports...
    confess "find_path() found an undefined Processor reference as part '$_[0]' in ",
        ref $self
        unless defined $proc;

    confess "find_path() found '$proc' instead of a Processor reference as part '$_[0]' in ",
        ref $self
        unless ref $proc;

    confess "find_path() found a ",
        ref $proc,
        " reference instead of a Processor reference in part '$_[0]' in ",
        ref $self
        unless index( "SCALAR|ARRAY|HASH|Regexp|REF|CODE", ref $proc ) <= 0;

    return $proc;
}


use vars qw( $AUTOLOAD );

sub DESTROY {} ## Prevent AUTOLOADing of this.

my $alpha_first_char = has_named_regexp_character_classes
	    ? "^[[:alpha:]]"
	    : "^[a-zA-Z]";

sub AUTOLOAD {
    my $self = shift;

    $AUTOLOAD =~ s/.*://;

    my $fc = substr $AUTOLOAD, 0, 1;
    ## TODO: Find out how Perl determines "alphaness" and use that.
    croak ref $self, " does not provide method $AUTOLOAD"
        unless $fc eq uc $fc && $AUTOLOAD =~ /$alpha_first_char/o;

    my $found = $self->find_part( $AUTOLOAD );
    return $found;
}


sub parts {
    my $self = shift; 
    croak "Can't set parts for a '", ref( $self ), "'" if @_;
    confess "undef Parts" unless defined $self->{Parts};
    return map $_->{Processor}, @{$self->{Parts}};
}


## TODO: Detect deep recursion in _all_part_recs().  In fact, detect deep
## recursion when building the machine.

sub _all_part_recs {
    my $self = shift; 
    croak "Can't pass parms to ", ref( $self ), "->_all_part_recs" if @_;
    confess "undef Parts" unless defined $self->{Parts};
    my $proc;
    return map {
        $proc = $_->{Processor};
        UNIVERSAL::can( $proc, "all_parts" )
            ? ( $_, $proc->_all_part_recs )
            : $_;
    } @{$self->{Parts}};
}


sub all_parts {
    my $self = shift; 
    croak "Can't pass parms to ", ref( $self ), "->_all_parts" if @_;
    confess "undef Parts" unless defined $self->{Parts};
    return map $_->{Processor}, $self->_all_part_recs;
}


#=item add_parts
#
#    $m->add_parts( { Foo => $foo, Bar => $bar } );
#
#On linear machines:
#
#    $m->add_parts( @parts );
#
#Adds one or more parts to the machine.  Does not connect them, you need
#to do that manually (we need to add a $m->connect_parts() style API).
#
#=cut
#
#sub add_parts {
#    my $self = shift; 
#confess "TODO";
#}

#=item remove_parts
#
#    $m->remove_parts( qw( Foo Bar ) );
#
#Slower, but possible:
#
#    $m->remove_parts( $m->Foo, $m->Bar );
#
#On linear machines:
#
#    $m->remove_parts( 1, 3 );
#
#Removes one or more parts from the machine.  Does not connect them
#except on linear machines.  Attempts to disconnect any parts that
#point to them, and that they point to.  This attempt will fail for any
#part that does not provide a handler() or handlers() method.
#
#This is breadth-first recursive, like C<$m->find_part( $id )> is.  This
#will remove *all* parts with the given names from a complex
#machine (this does not apply to index numbers).
#
#Returns a list of the removed parts.
#
#If a name is not found, it is ignored.
#
#=cut
#
#sub remove_parts {
#    my $self = shift; 
#
#    my %names;
#    my @found;
#
#    for my $doomed ( @_ ) {
#        unless ( ref $doomed ) {
#            $names{$doomed} = undef;
#            if ( my $f = delete $self->{Parts}->{$doomed} ) {
#                push @found, $f;
#            }
#            else {
#                for my $c ( $self->parts ) {
#                    if ( $c->can( "remove_parts" ) 
#                       && ( my @f = $c->remove_parts( $doomed ) )
#                    ) {
#                        push @found, @f;
#                    }
#                }
#            }
#        }
#        else {
#            ## It's a reference.  Do this the slow, painful way.
#            for my $name ( keys %{$self->{Parts}} ) {
#                if ( $doomed == $self->{Parts}->{$name} ) {
#                    $names{$name} = undef;
#                    push @found, delete $self->{Parts}->{$name};
#                }
#            }
#
#            for my $c ( $self->parts ) {
#                if ( $c->can( "remove_parts" ) 
#                   && ( my @f = $c->remove_parts( $doomed ) )
#                ) {
#                    push @found, @f;
#                }
#            }
#        }
#    }
#
#    for my $c ( sort keys %{$self->{Connections}} ) {
#        if ( exists $names{$self->{Connections}->{$c}} ) {
###TODO: Unhook the processors if possible
#            delete $self->{Connections}->{$c};
#        }
#        if ( exists $names{$c} ) {
###TODO: Unhook the processors if possible
#            delete $self->{Connections}->{$c};
#        }
#    }
#
#    return @found;
#}


sub set_handler {
    my $self = shift;
    my ( $handler, $type ) = reverse @_;

    $type ||= "Handler";

    for my $part_rec ( @{$self->{Parts}} ) {
        my $proc = $part_rec->{Processor};
        my $hs = $part_rec->{Handlers};

        if ( grep ref $_ ? $_ == $self->{$type} : $_ eq "Exhaust", @$hs ) {
            if ( @$hs == 1 && $proc->can( "set_handler" ) ) {
                $proc->set_handler(
                    $type ne "Handler" ? $type : (),
                    $handler
                );
                next;
            }

            unless ( $proc->can( "set_handlers" ) ) {
                croak ref $proc,
                    @$hs == 1
                        ? " has no set_handler or set_handlers method"
                        : " has no set_handlers method"
            }

            $proc->set_handlers(
                map {
                    my $h;
                    my $t;
                    if ( ref $_ ) {
                        $h = $_;
                        $t = "Handler";
                    }
                    elsif ( $_ eq "Exhaust" ) {
                        $h = $handler;
                        $t = $type;
                    } else {
                        ( $h, $t ) = reverse split /=>/, $_;
                        $h = $self->find_part( $h );
                        $t = $type;
                        croak "Can't locate part $_ to be a handler for ",
                            $part_rec->string_description
                            unless $h;
                    }
                    { $type => $h }
                } @$hs
            );
        }
    }

    $self->{$type} = $handler;
}


my $warned_about_missing_sax_tracer;
sub trace_parts {
    my $self = shift;

    unless ( eval "require Devel::TraceSAX; 1" ) {
        warn $@ unless $warned_about_missing_sax_tracer++;
        return;
    }


    for ( @_ ? map $self->_find_part_rec( $_ ), @_ : @{$self->{Parts}} ) {
        Devel::TraceSAX::trace_SAX(
            $_->{Processor},
            $_->string_description
        );
    }

    ## some parts are created lazily, let's trace those, too
    $self->{TraceAdHocParts} ||= 1 unless @_;
}



sub trace_all_parts {
    my $self = shift;

    croak "Can't pass parms to trace_all_parts" if @_;

    unless ( eval "require Devel::TraceSAX; 1" ) {
        warn $@ unless $warned_about_missing_sax_tracer++;
        return;
    }

    for ( @{$self->{Parts}} ) {
        Devel::TraceSAX::trace_SAX(
            $_->{Processor},
            $_->string_description
        );
        $_->{Processor}->trace_all_parts
            if $_->{Processor}->can( "trace_all_parts" );
    }

    ## some parts are created lazily, let's trace those, too
    $self->{TraceAdHocParts} = 1;
}



sub untracify_parts {
    my $self = shift;
    for ( @_ ? map $self->find_part( $_ ), @_ : $self->parts ) {
        XML::SAX::TraceViaISA::remove_tracing_subclass( $_ );
    }
}



compile_methods __PACKAGE__, <<'EOCODE', sax_event_names "ParseMethods" ;
    sub <METHOD> {
        my $self = shift;
        my $h = $self->find_part( "Intake" );
        croak "SAX machine 'Intake' undefined"
            unless $h;

        if ( $h->can( "<METHOD>" ) ) {
            my ( $ok, @result ) = eval {
                ( 1, wantarray
                    ? $h-><METHOD>( @_ )
                    : scalar $h-><METHOD>( @_ )
                );
            };
            
            ## Not sure how/where causes me to need this next line, but
            ## in perl5.6.1 it seems necessary.
            return wantarray ? @result : $result[0] if $ok;
            die $@ unless $@ =~ /No .*routine defined/;
            undef $@;

            if ( $h->isa( "XML::SAX::Base" ) ) {
                ## Due to a bug in old versions of X::S::B, we need to reset
                ## this so that it will pass events on.
                ## TODO: when newer X::S::B's are common, jack up the
                ## version in Makefile.PL's PREREQ_PM :).
                delete $h->{ParseOptions};
            }
        }

        require XML::SAX::ParserFactory;
        $self->{Parser} = XML::SAX::ParserFactory->parser(
            Handler => $h
        );

        Devel::TraceSAX::trace_SAX(
            $self->{Parser},
            "Ad hoc parser (" . ref( $self->{Parser} ) . ")"
        ) if $self->{TraceAdHocParts};

        return $self->{Parser}-><METHOD>(@_);
    }
EOCODE


compile_methods __PACKAGE__, <<'EOCODE', sax_event_names ;
    sub <EVENT> {
        my $self = shift;
        my $h = $self->find_part( "Intake" );
        croak "SAX machine 'Intake' undefined"
            unless $h;

        return $h-><EVENT>( @_ ) if $h->can( "<EVENT>" );
    }
EOCODE



my %basic_types = (
    ARRAY  => undef,
    CODE   => undef,
    GLOB   => undef,
    HASH   => undef,
    REF    => undef,  ## Never seen this one, but it's listed in perlfunc
    Regexp => undef,
    SCALAR => undef,
);


sub _resolve_spec {
    my $self = shift;
    my ( $spec ) = @_;

    croak "undef passed instead of a filter to ", ref( $self ), "->new()"
        unless defined $spec;

    croak "Empty filter name ('') passed to ", ref( $self ), "->new()"
        unless length $spec;

    my $type = ref $spec;

    if (
           $type eq "SCALAR"
## TODO:         || $type eq "ARRAY"  <== need XML::SAX::Writer to supt this.
        || $type eq "GLOB"
        || UNIVERSAL::isa( $spec, "IO::Handle" )
        || ( ! $type && $spec =~ /^\s*([>|]|\+>)/ )
    ) {
## Cheat until XML::SAX::Writer cat grok it
if ( ! $type ) {
    use Symbol;
    my $fh = gensym;
    open $fh, $spec or croak "$! opening '$spec'" ;
    $spec = $fh;
}
        require XML::SAX::Writer;
        $spec = XML::SAX::Writer->new( Output => $spec );
    }
    elsif ( !$type ) {
        if ( $spec !~ /^\s*<|\|\s*(?!\n)$/ ) {
            ## Doesn't look like the caller wants to slurp a file
            ## Let's require it now to catch errors early, then
            ## new() it later after all requires are done.
            ## delaying the new()s might help us from doing things
            ## like blowing away output files and then finding
            ## errors, for instance.
            croak $@ unless $spec->can( "new" ) || eval "require $spec";
        }
    }
    else {
        croak "'$type' not supported in a SAX machine specification\n"
            if exists $basic_types{$type};
    }

    return $spec;
}

my $is_name_like = has_named_regexp_character_classes
	    ? '^[[:alpha:]]\w*(?!\n)$'
	    :    '^[a-zA-Z]\w*(?!\n)$';

sub _valid_name($) {
    my ( $prospect ) = @_;
    return 0 unless defined $prospect && length $prospect;
    my $fc = substr $prospect, 0, 1;
    ## I wonder how close to valid Perl method names this is?
    ( $fc eq uc $fc && $prospect =~ /$is_name_like/o ) ? 1 : 0;
}


sub _push_spec {
    my $self = shift;
    my ( $name, $spec, @handlers ) = 
        ref $_[0]
            ? ( undef, @_ )       ## Implictly unnamed: [ $obj, ... ]
            : @_;                 ## Named or explicitly unnamed: [ $name, ...]

    my $part = XML::SAX::Machine::Part->new(
        Name      => $name,
        Handlers  => \@handlers,
    );

#    if ( grep $_ eq "Exhaust", @handlers ) {
#        $self->{OverusedNames}->{Exhaust} ||= undef
#            if exists $self->{PartsByName}->{Exhaust};
#
#        $self->{PartsByName}->{Exhaust} = $self->{Parts}->[-1];
#
#        @handlers = grep $_ ne "Exhaust", @handlers;
#    }

    ## NOTE: This may
    ## still return a non-reference, which is the type of processor
    ## wanted here.  We construct those lazily below; see the docs
    ## about order of construction.
    my $proc = $self->_resolve_spec( $spec );
    $part->{Processor} = $proc;
    croak "SAX machine BUG: couldn't resolve spec '$spec'"
        unless defined $proc;

    push @{$self->{Parts}}, $part;
    $part->{Number} = $#{$self->{Parts}};

    if ( defined $name ) {
        $self->{OverusedNames}->{$name} ||= undef
            if exists $self->{PartsByName}->{$name};

        $self->{IllegalNames}->{$name} ||= undef
            unless _valid_name $name && $name ne "Exhaust";

        $self->{PartsByName}->{$name} = $self->{Parts}->[-1];
    }

    ## This HASH is used to detect cycles even if the user uses 
    ## preconstructed references instead of named parts.
    $self->{PartsByProcessor}->{$proc} = $part
        if ref $proc;
}


sub _names_err_msgs {
    my ( $s, @names ) = @_ ;
    @names = map ref $_ eq "HASH" ? keys %$_ : $_, @names;
    return () unless @names;

    @names = keys %{ { map { ( $_ => undef ) } @names } };

    if ( @names == 1 ) {
        $s =~ s/%[A-Z]+//g;
    }
    else {
        $s =~ s/%([A-Z]+)/\L$1/g;
    }

    return $s . join ", ", map "'$_'", sort @names ;
}


sub _build_part {
    my $self = shift;
    my ( $part ) = @_;

    my $part_num = $part->{Number};

    return if $self->{BuiltParts}->[$part_num];

    confess "SAX machine BUG: cycle found too late"
        if $self->{SeenParts}->[$part_num];
    ++$self->{SeenParts}->[$part_num];

    ## We retun a list of all cycles that have been discovered but
    ## not yet completed.  We don't return cycles that have been
    ## completely discovered; those are placed in DetectedCycles.
    my @open_cycles;

    eval {
        ## This eval is to make sure we decrement SeenParts so that
        ## we don't encounter spurious cycle found too late exceptions.

        ## Build any handlers, detect cycles
        my @handler_procs;

## I decided not to autolink one handler to the next in order to keep
## from causing hard to diagnose errors when unintended machines are
## passed in.  The special purpose machines, like Pipeline, have
## that logic built in.
##        ## Link any part with no handlers to the next part.
##        push @{$part->{Handlers}}, $part->{Number} + 1
##            if ! @{$part->{Handlers}} && $part->{Number} < $#{$self->{Parts}};

        for my $handler_spec ( @{$part->{Handlers}} ) {

            my $handler;

            if ( ref $handler_spec ) {
                ## The caller specified a handler with a real reference, so
                ## we don't need to build it, but we do need to do
                ## cycle detection. _build_part won't build it in this case
                ## but it will link it and do cycle detection.
                $handler = $self->{PartsByProcessor}->{$handler_spec}
                    if exists $self->{PartsByProcessor}->{$handler_spec};

                if ( ! defined $handler ) {
                    ## It's a processor not in this machine.  Hope the
                    ## caller knows what it's doing.
                    push @handler_procs, $handler_spec;
                    next;
                }
            }
            else {
                $handler = $self->_find_part_rec( $handler_spec );
                ## all handler specs were checked earlier, so "survive" this
                ## failure and let the queued error message tell the user
                ## about it.
                next unless defined $handler;
            }

            if ( $self->{SeenParts}->[$handler->{Number}] ) {
                ## Oop, a cycle, and we don't want to recurse or we'll
                ## recurse forever.
                push @open_cycles, $part eq $handler
                    ? [ $handler ] 
                    : [ $part, $handler ];
                next;
            }

            my @nested_cycles = $self->_build_part( $handler );

            my $handler_proc = $handler->{Processor};

            confess "SAX machine BUG: found a part with no processor: ",
                $handler->string_description
                unless defined $handler_proc;

            confess "SAX machine BUG: found a unbuilt '",
                $handler->{Processor},
                "' processor: ",
                $handler->string_description
                unless ref $handler_proc;

            push @handler_procs, $handler_proc;

            for my $nested_cycle ( @nested_cycles ) {
                if ( $nested_cycle->[-1] == $part ) {
                    ## the returned cycle "ended" with our part, so
                    ## we have a complete description of the cycle, log it
                    ## and move on.
                    push @{$self->{DetectedCycles}}, $nested_cycle;
                }
                else {
                    ## This part is part of this cycle but not it's "beginning"
                    push @open_cycles, [ $part, $nested_cycle ];
                }
            }
        }

        ## Create this processor if need be, otherwise just set the handlers.
        my $proc = $part->{Processor};
        confess "SAX machine BUG: undefined processor for ",
            $part->string_description
            unless defined $proc;

        unless ( ref $proc ) {
            ## TODO: Figure a way to specify the type of handler, probably
            ## using a DTDHandler=>Name syntax, not sure.  Perhaps
            ## using a hash would be best.

            if ( $proc =~ /^\s*<|\|\s*(?!\n)$/ ) {
                ## Looks like the caller wants to slurp a file
                ## We open it ourselves to get all of Perl's magical
                ## "open" goodness.  TODO: also check for a URL scheme
                ## and handle that :).

                ## TODO: Move this in to a/the parse method so it can
                ## be repeated.
                require Symbol;
                my $fh = Symbol::gensym;
                open $fh, $proc or croak "$! opening '$proc'";
                require XML::SAX::ParserFactory;
                require IO::Handle;
                $proc = XML::SAX::ParserFactory->parser(
                    Source => {
                        ByteStream => $fh,
                    },
                    map {
                        ( Handler => $_ ),
                    } @handler_procs
                );

            }
            elsif (
                XML::SAX::Machines->processor_class_option(
                    $proc,
                    "ConstructWithHashedOptions"
                )
            ) {
                ## This is designed to build options in a format compatible
                ## with SAXT style constructors when multiple handlers are
                ## defined.
                $proc = $proc->new(
                    map {
                        { Handler => $_ }, ## Hashes
                    } @handler_procs       ## 0 or more of 'em
                );
            }
            else {
                ## More common Foo->new( Handler => $h );
                croak "$proc->new doesn't allow multiple handlers.\nSet ConstructWithOptionsHashes => 1 in XML::SAX::Machines::ConfigDefaults if need be"
                    if @handler_procs > 1;
                $proc = $proc->new(
                    map {
                        ( Handler => $_ ),  ## A plain list
                    } @handler_procs        ## with 0 or 1 elts
                );
            }
            $self->{PartsByProcessor}->{$proc} = $part;
        }
        elsif ( @handler_procs ) {
            if ( $proc->can( "set_handlers" ) ) {
                $proc->set_handlers( @handler_procs );
            }
            elsif ( $proc->can( "set_handler" ) ) {
                if ( @handler_procs == 1 ) {
                    $proc->set_handler( @handler_procs );
                }
                else {
                    die "SAX machine part ", $part->string_description,
                    " can only take one handler at a time\n";
                }
            }
            else {
                die "SAX machine part ", $part->string_description,
                " does not provide a set_handler() or set_handlers() method\n"
            }
        }

        $part->{Processor} = $proc;
    };

    --$self->{SeenParts}->[$part->{Number}];
    $self->{BuiltParts}->[$part_num] = 1;


    if ( $@ ) {
        chomp $@;
        $@ .= "\n        ...while building " . $part->string_description . "\n";
        die $@;
    }

    return @open_cycles;
}


sub _compile_specs {
    my $self = shift;

    my @errors;

    ## Init the permanent structures
    $self->{Parts}            = [];
    $self->{PartsByName}      = {};
    $self->{PartsByProcessor} = {};

    ## And some temporary structures.
    $self->{IllegalNames}  = {};
    $self->{OverusedNames} = {};

    ## Scan the specs and figure out the connectivity, names and load
    ## any requirements, etc.
    for my $spec ( @_ ) {
        eval {
            $self->_push_spec(
                ref $spec eq "ARRAY"
                    ? @$spec
                    : ( undef, $spec )
            );
        };
        ## This could be ugly if $@ contains a stack trace, but it'll have
        ## to do.
        if ( $@ ) {
            chomp $@;
            push @errors, $@;
        }
    }

    push @errors, (
        _names_err_msgs(
            "illegal SAX machine part name%S ",
            $self->{IllegalNames}
        ),
        _names_err_msgs(
            "undefined SAX machine part%S specified as handler%S ",
            grep defined && ! $self->_find_part_rec( $_ ),
                grep ! ref && $_ ne "Exhaust",
                    map @{$_->{Handlers}},
                        @{$self->{Parts}}
        ),
        _names_err_msgs(
            "multiple SAX machine parts named ",
            $self->{OverusedNames}
        )
    );

    ## Free some memory and make object dumps smaller
    delete $self->{IllegalNames};
    delete $self->{OverusedNames};

    ## If we made it this far, all classes have been loaded and all
    ## non-processor refs have been converted in to processors.
    ## Now
    ## we need to build and that were specified by type name and do
    ## them in reverse order so we can pass the
    ## Handler option(s) in.
    ## If multiple handlers are defined, then
    ## we assume that the constructor takes a SAXT like parameter list.
    ## TODO: figure out how to allow DocumentHandler, etc.  Perhaps allow
    ## HASH refs in ARRAY syntax decls.
    
    ## Some temporaries
    $self->{BuiltParts}     = [];
    $self->{SeenParts}      = [];
    $self->{DetectedCycles} = [];

    ## _build_part is recursive and builds any downstream handlers
    ## needed to build a part.
    for ( @{$self->{Parts}} ) {
        eval {
            push @{$self->{DetectedCycles}}, $self->_build_part( $_ );
        };
        if ( $@ ) {
            chomp $@;
            push @errors, $@;
        }
    }

#    $self->{PartsByName}->{Intake}  ||= $self->{Parts}->[0];
#    $self->{PartsByName}->{Exhaust} ||= $self->{Parts}->[-1];

    if ( @{$self->{DetectedCycles}} ) {
        ## Remove duplicate (cycles are found once for each processor in
        ## the cycle.
        my %unique_cycles;

        for my $cycle ( @{$self->{DetectedCycles}} ) {
            my $start = 0;
            for ( 1..$#$cycle ) {
                $start = $_
                    if $cycle->[$_]->{Number} < $cycle->[$start]->{Number};
            }
            my $key = join(
                ",",
                map $_->{Number},
                    @{$cycle}[$start..($#$cycle),0..($start-1)]
            );
            $unique_cycles{$key} ||= $cycle;
        }
        
        push @errors, map {
            "Cycle detected in SAX machine: " .
                join(
                    "->",
                    map $_->string_description, $_->[-1], @$_
                );
        } map $unique_cycles{$_}, sort keys %unique_cycles;
    }

    delete $self->{SeenParts};
    delete $self->{BuiltParts};
    delete $self->{DetectedCycles};

    croak join "\n", @errors if @errors;
}


sub _SAX2_attrs {
    my %a = @_;

    return {
        map {
            defined $a{$_}
                ? ( $_ => {
                    LocalName => $_,
                    Name      => $_,
                    Value     => $a{$_},
                } )
                : () ;
        } keys %a
    };
}


my %ids;
sub _idify($) {
    $ids{$_[0]} = keys %ids unless exists $ids{$_[0]};
    return $ids{$_[0]};
}


sub pointer_elt {
    my $self = shift;
    my ( $elt_type, $h_spec, $options ) = @_;

    my $part_rec;

    $h_spec = $self->{Handler}
        if $h_spec eq "Exhaust" && defined $self->{Handler};

    ## Look locally first in case the name is not
    ## unique among parts in RootMachine.
    $part_rec = $self->_find_part_rec( $h_spec )
        if ! $part_rec;

    ## Don't look for indexes in RootMachine
    $part_rec = $options->{RootMachine}->_find_part_rec(
        $h_spec
    ) if ! $part_rec
        && defined $options->{RootMachine}
        && $h_spec != /^-?\d+$/ ;

    my %attrs;

    if ( $part_rec ) {
        %attrs = (
            name           => $part_rec->{Name} || $h_spec,
            "handler-id"   => _idify $part_rec->{Processor},
        );
    }
    else {
        if ( ref $h_spec ) {
            %attrs = (
                type         => ref $h_spec,
                "handler-id" => _idify $h_spec,
            );
        }
        else {
            %attrs = (
                name => $h_spec,
            );
        }
    }

    return {
        Name       => $elt_type,
        LocalName  => $elt_type,
        Attributes => _SAX2_attrs( %attrs ),
    };
}


sub generate_part_descriptions {
    my $self = shift;
    my ( $options ) = @_;

    my $h = $options->{Handler};
    croak "No Handler passed" unless $h;

    for my $part_rec ( @{$self->{Parts}} ) {
        my $proc = $part_rec->{Processor};

        if ( $proc->can( "generate_description" ) ) {
            $proc->generate_description( {
                %$options,
                Name        => $part_rec->{Name},
                Description => $part_rec->string_description,
            } );
        }
        else {
            my $part_elt = {
                LocalName  => "part",
                Name       => "part",
                Attributes => _SAX2_attrs(
                    id          => _idify $proc,
                    type        => ref $part_rec,
                    name        => $part_rec->{Name},
                    description => $part_rec->string_description,
                ),
            };
            $h->start_element( $part_elt );
            for my $h_spec ( @{$part_rec->{Handlers}} ) {
                my $handler_elt = $self->pointer_elt( "handler", $h_spec );

                $h->start_element( $handler_elt );
                $h->end_element(   $handler_elt );
            }
            $h->end_element( $part_elt );
        }
    }
}


sub generate_description {
    my $self = shift;

    my $options =
        @_ == 1
            ? ref $_[0] eq "HASH"
                ? { %{$_[0]} }
                : {
                    Handler =>
                        ref $_[0]
                            ? $_[0]
                            : $self->_resolve_spec( $_[0] )
                }
            : { @_ };

    my $h = $options->{Handler};
    croak "No Handler passed" unless $h;

    unless ( $options->{Depth} ) {
        %ids = ();
        $options->{RootMachine} = $self;

        $h->start_document({});
    }

    ++$options->{Depth};
    my $root_elt = {
        LocalName => "sax-machine",
        Name      => "sax-machine",
        Attributes => _SAX2_attrs(
            id          => _idify $self,
            type        => ref $self,
            name        => $options->{Name},
            description => $options->{Description},
        ),
    };

    $h->start_element( $root_elt );

    ## Listing the handler first so it doesn't look like a part's
    ## handler (which it kinda does if it's hanging out *after* a <part .../>
    ## tag :).  Also makes following the links by hand a tad easier.
    if ( defined $self->{Handler} ) {
        my $handler_elt = $self->pointer_elt( "handler", $self->{Handler} );
        $handler_elt->{Attributes}->{name} = {
            Name      => "name",
            LocalName => "name",
            Value     => "Exhaust"
        } unless exists $handler_elt->{Attributes}->{Name};
            
        $h->start_element( $handler_elt );
        $h->end_element(   $handler_elt );
    }

    for ( sort keys %{$self->{PartsByName}} ) {
        if ( $self->{PartsByName}->{$_}->{Name} ne $_ ) {
        warn $self->{PartsByName}->{$_}->{Name}, " : ", $_;
            my $handler_elt = $self->pointer_elt( "alias", $_ );
            %{$handler_elt->{Attributes}} = (
                %{$handler_elt->{Attributes}},
                %{_SAX2_attrs( alias => $_ )},
            );
            $h->start_element( $handler_elt );
            $h->end_element(   $handler_elt );
        }
    }

    $self->generate_part_descriptions( $options );
    $h->end_element( $root_elt );

    --$options->{Depth};
    $h->end_document({}) unless $options->{Depth};
}


##
## This is a private class, only this class should use it directly.
##
package XML::SAX::Machine::Part;
{
  $XML::SAX::Machine::Part::VERSION = '0.46';
}

use fields (
    'Name',       ## The caller-given name of the part
    'Number',     ## Where it sits in the parts list.
    'Processor',  ## The actual SAX processor
    'Handlers',   ## The handlers the caller specified
);


sub new {
    my $proto = shift;
    my $class = ref $proto || $proto;

    my $self = bless {}, $class;
    
    my %options = @_ ;
    $self->{$_} = $options{$_} for keys %options;

    return $self;
}


sub string_description {
    my $self = shift;

    return join( 
        "",
        $self->{Name}
            ? $self->{Name}
            : ( "#", $self->{Number} ),
        " (",
        $self->{Processor}
            ? ( ref $self->{Processor} || $self->{Processor} )
            : "<undefined processor>",
        ")"
    );
}

1;

__END__

=pod

=head1 NAME

XML::SAX::Machine - Manage a collection of SAX processors

=head1 VERSION

version 0.46

=head1 SYNOPSIS

    ## Note: See XML::SAX::Pipeline and XML::SAX::Machines first,
    ## this is the gory, detailed interface.

    use My::SAX::Machines qw( Machine );
    use My::SAX::Filter2;
    use My::SAX::Filter3;

    my $filter3 = My::SAX::Filter3->new;

    ## A simple pipeline.  My::SAX::Filter1 will be autoloaded.
    my $m = Machine(
        #
        # Name   => Class/object            => handler(s)
        #
        [ Intake => "My::SAX::Filter1"      => "B"        ],
        [ B      => My::SAX::Filter2->new() => "C"        ],
        [ C      => $filter3                => "D"        ],
        [ D      => \*STDOUT                              ],
    );

    ## A parser will be created unless My::SAX::Filter1 can parse_file
    $m->parse_file( "foo.revml" );

    my $m = Machine(
        [ Intake   => "My::SAX::Filter1"  => qw( Tee     ) ],
        [ Tee      => "XML::Filter::SAXT" => qw( Foo Bar ) ],
        [ Foo      => "My::SAX::Filter2"  => qw( Out1    ) ],
        [ Out1     => \$log                                ],
        [ Bar      => "My::SAX::Filter3"  => qw( Exhaust ) ],
    );

=head1 DESCRIPTION

B<WARNING>: This API is alpha!!!  It I<will> be changing.

A generic SAX machine (an instance of XML::SAX::Machine) is a container
of SAX processors (referred to as "parts") connected in arbitrary ways.

Each parameter to C<Machine()> (or C<XML::SAX::Machine->new()>)
represents one top level part of the machine.  Each part has a name, a
processor, and one or more handlers (usually specified by name, as shown
in the SYNOPSIS).

Since SAX machines may be passed in as single top level parts, you can
also create nested, complex machines ($filter3 in the SYNOPSIS could be
a Pipeline, for example).

A SAX machines can act as a normal SAX processors by connecting them to
other SAX processors:

    my $w = My::Writer->new();
    my $m = Machine( ...., { Handler => $w } );
    my $g = My::Parser->new( Handler => $w );

=head2 Part Names

Although it's not required, each part in a machine can be named.  This
is useful for retrieving and manipulating the parts (see L</part>, for
instance), and for debugging, since debugging output (see
L</trace_parts> and L</trace_all_parts>) includes the names.

Part names must be valid Perl subroutine names, beginning with an
uppercase character.  This is to allow convenience part accessors
methods like

    $c = $m->NameOfAFilter;

to work without ever colliding with the name of a method (all method
names are completely lower case).  Only filters named like this can be
accessed using the magical accessor functions.

=head2 Reserved Names: Intake and Exhaust

The names c<Intake> and C<Exhaust> are reserved.  C<Intake> refers to
the first part in the processing chain.  This is not necessarily the
first part in the constructor list, just the first part to receive
external events.

C<Exhaust> refers to the output of the machine; no part may be named
C<Exhaust>, and any parts with a handler named C<Exhaust> will deliver
their output to the machine's handler.  Normally, only one part should
deliver it's output to the Exhaust port.

Calling $m->set_handler() alters the Exhaust port, assuming any
processors pointing to the C<Exhaust> provide a C<set_handler()> method
like L<XML::SAX::Base>'s.

C<Intake> and C<Exhaust> are usually assigned automatically by
single-purpose machines like L<XML::SAX::Pipeline> and
L<XML::SAX::Manifold>.

=head2 SAX Processor Support

The XML::SAX::Machine class is very agnostic about what SAX processors
it supports; about the only constraint is that it must be a blessed
reference (of any type) that does not happen to be a Perl IO::Handle
(which are assumed to be input or output filehandles).

The major constraint placed on SAX processors is that they must provide
either a C<set_handler> or C<set_handlers> method (depending on how many
handlers a processor can feed) to allow the SAX::Machine to disconnect
and reconnect them.  Luckily, this is true of almost any processor
derived from XML::SAX::Base.  Unfortunately, many SAX older (SAX1)
processors do not meet this requirement; they assume that SAX processors
will only ever be connected together using their constructors.

=head2 Connections

SAX machines allow you to connect the parts however you like; each part
is given a name and a list of named handlers to feed.  The number of
handlers a part is allowed depends on the part; most filters only allow
once downstream handler, but filters like L<XML::Filter::SAXT> and
L<XML::Filter::Distributor> are meant to feed multiple handlers.

Parts may not be connected in loops ("cycles" in graph theory terms).
The machines specified by:

    [ A => "Foo" => "A" ],  ## Illegal!

and 

    [ A => "Foo" => "B" ],  ## Illegal!
    [ B => "Foo" => "A" ],

.  Configuring a machine this way would cause events to flow in an
infinite loop, and/or cause the first processor in the cycle to start
receiving events from the end of the cycle before the input document was
complete.  Besides, it's not a very useful topology :).

SAX machines detect loops at construction time.

=head1 NAME

    XML::SAX::Machine - Manage a collection of SAX processors

=head1 API

=head2 Public Methods

These methods are meant to be used by users of SAX machines.

=over

=item new()

    my $m = $self->new( @machine_spec, \%options );

Creates $self using %options, and compiles the machine spec.  This is
the longhand form of C<Machines( ... )>.

=item find_part

Gets a part contained by this machine by name, number or object reference:

    $c = $m->find_part( $name );
    $c = $m->find_part( $number );
    $c = $m->find_part( $obj );    ## useful only to see if $obj is in $m

If a machine contains other machines, parts of the contained machines
may be accessed by name using unix directory syntax:

    $c = $m->find_part( "/Intake/Foo/Bar" );

(all paths must be absolute).

Parts may also be accessed by number using array indexing:

    $c = $m->find_part(0);  ## Returns first part or undef if none
    $c = $m->find_part(-1); ## Returns last part or undef if none
    $c = $m->find_part( "Foo/0/1/-1" );

There is no way to guarantee that a part's position number means
anything, since parts can be reconnected after their position numbers
are assigned, so using a part name is recommended.

Throws an exception if the part is not found, so doing things like

   $m->find_part( "Foo" )->bar()

garner informative messages when "Foo" is not found.  If you want to
test a result code, do something like

    my $p = eval { $m->find_part };
    unless ( $p ) {
        ...handle lookup failure...
    }

=item parts

    for ( $m->parts ) { ... }

Gets an arbitrarily ordered list of top level parts in this machine.
This is all of the parts directly contained by this machine and none of
the parts that may be inside them.  So if a machine contains an
L<XML::SAX::Pipeline> as one of it's parts, the pipeline will be
returned but not the parts inside the pipeline.

=item all_parts

    for ( $m->all_parts ) { ... }

Gets all parts in this machine, not just top level ones. This includes
any machines contained by this machine and their parts.

=item set_handler

    $m->set_handler( $handler );
    $m->set_handler( DTDHandler => $handler );

Sets the machine's handler and sets the handlers for all parts that
have C<Exhaust> specified as their handlers.  Requires that any such
parts provide a C<set_handler> or (if the part has multiple handlers)
a C<set_handlers> method.

NOTE: handler types other than "Handler" are only supported if they are
supported by whatever parts point at the C<Exhaust>.  If the handler type is
C<Handler>, then the appropriate method is called as:

    $part->set_handler( $handler );
    $part->set_handlers( $handler0, $handler1, ... );

If the type is some other handler type, these are called as:

    $part->set_handler( $type => $handler );
    $part->set_handlers( { $type0 => $handler0 }, ... );

=item trace_parts

    $m->trace_parts;          ## trace all top-level parts
    $m->trace_parts( @ids );  ## trace the indicated parts

Uses Devel::TraceSAX to enable tracing of all events received by the parts of
this machine.  Does not enable tracing of parts contained in machines in this
machine; for that, see trace_all_parts.

=item trace_all_parts

    $m->trace_all_parts;      ## trace all parts

Uses Devel::TraceSAX to trace all events received by the parts of this
machine.

=item untracify_parts

    $m->untracify_parts( @ids );

Converts the indicated parts to SAX processors with tracing enabled.
This may not work with processors that use AUTOLOAD.

=back

=head1 Events and parse routines

XML::SAX::Machine provides all SAX1 and SAX2 events and delgates them to the
processor indicated by $m->find_part( "Intake" ).  This adds some overhead, so
if you are concerned about overhead, you might want to direct SAX events
directly to the Intake instead of to the machine.

It also provides parse...() routines so it can whip up a parser if need
be.  This means: parse(), parse_uri(), parse_string(), and parse_file()
(see XML::SAX::EventMethodMaker for details).  There is no way to pass
methods directly to the parser unless you know that the Intake is a
parser and call it directly.  This is not so important for parsing,
because the overhead it takes to delegate is minor compared to the
effort needed to parse an XML document.

=head2 Internal and Helper Methods

These methods are meant to be used/overridden by subclasses.

=over

=item _compile_specs

    my @comp = $self->_compile_specs( @_ );

Runs through a list of module names, output specifiers, etc., and builds
the machine.

    $scalar     --> "$scalar"->new
    $ARRAY_ref  --> pipeline @$ARRAY_ref
    $SCALAR_ref --> XML::SAX::Writer->new( Output => $SCALAR_ref )
    $GLOB_ref   --> XML::SAX::Writer->new( Output => $GLOB_ref )

=item generate_description

    $m->generate_description( $h );
    $m->generate_description( Handler => $h );
    $m->generate_description( Pipeline ... );

Generates a series of SAX events to the handler of your choice.

See L<XML::Handler::Machine2GraphViz> on CPAN for a way of visualizing
machine innards.

=back

=head1 TODO

=over

=item *

Separate initialization from construction time; there should be somthing
like a $m->connect( ....machine_spec... ) that new() calls to allow you
to delay parts speficication and reconfigure existing machines.

=item *

Allow an XML doc to be passed in as a machine spec.

=back

=head1 LIMITATIONS

=over

=back

=head1 AUTHOR

    Barrie Slaymaker <barries@slaysys.com>

=head1 LICENSE

Artistic or GPL, any version.

=head1 AUTHORS

=over 4

=item *

Barry Slaymaker

=item *

Chris Prather <chris@prather.org>

=back

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2013 by Barry Slaymaker.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.

=cut