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 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714
|
package XML::Generator::PerlData;
use strict;
use warnings;
use XML::SAX::Base;
use vars qw($VERSION @ISA $NS_XMLNS $NS_XML);
use Scalar::Util qw(refaddr);
# some globals
$VERSION = '0.95';
@ISA = qw( XML::SAX::Base );
$NS_XML = 'http://www.w3.org/XML/1998/namespace';
$NS_XMLNS = 'http://www.w3.org/2000/xmlns/';
sub new {
my $proto = shift;
my $self = $proto->SUPER::new( @_ );
my %args = @_;
delete $args{Handler} if defined $args{Handler};
$self->{Namespaces} = { $NS_XMLNS => 'xmlns',
$NS_XML => 'xml'
};
$self->{DeclaredNamespaces} = {$NS_XMLNS => 'xmlns',
$NS_XML => 'xml'
};
$self->{InScopeNamespaceStack} = [];
# _Parents needed for attribute vs. element fixing;
$self->{_Parents} = [];
$self->init( %args );
return $self;
}
sub init {
my $self = shift;
my %args = @_;
$self->{Keymap} = $args{keymap} if defined $args{keymap};
$self->{RootName} = $args{rootname} if defined $args{rootname};
$self->{SkipRoot} = $args{skiproot} if defined $args{skiproot};
$self->{DefaultElementName} = $args{defaultname} if defined $args{defaultname};
$self->{BindAttrs} = 1 if defined $args{bindattrs};
$self->{Keymap} ||= {};
$self->{RootName} ||= 'document';
$self->{DefaultElementName} ||= 'default';
$self->{TokenReplacementChar} ||= '_';
$self->{Seen} ||= {};
if ( defined $args{namespaces} ) {
foreach my $uri ( keys( %{$args{namespaces}} )) {
$self->{Namespaces}->{"$uri"} = $args{namespaces}->{"$uri"};
}
}
# allow perlified PIs
if ( defined( $args{processing_instructions} )) {
$self->{ProcessingInstructions} = [];
if ( ref( $args{processing_instructions} ) eq 'ARRAY' ) {
$self->{ProcessingInstructions} = $args{processing_instructions};
}
elsif ( ref( $args{processing_instructions} ) eq 'HASH' ) {
foreach my $k ( keys( %{$args{processing_instructions}} )) {
push @{$self->{ProcessingInstructions}}, ( $k => $args{processing_instructions}->{$k} );
}
}
}
# let 'em change handlers if they want.
if ( defined $args{Handler} ) {
$self->set_handler( $args{Handler} );
}
if ( defined( $args{attrmap} ) ) {
$self->{Attrmap} = {};
while ( my ($k, $v) = ( each( %{$args{attrmap}} ) )) {
push @{$self->{Attrmap}->{$k}}, ref( $v ) ? @{$v} : $v;
}
}
$self->{Attrmap} ||= {};
if ( defined( $args{namespacemap} ) ) {
$self->{Namespacemap} = {};
while ( my ($k, $v) = ( each( %{$args{namespacemap}} ) )) {
push @{$self->{Namespacemap}->{$k}}, ref( $v ) ? @{$v} : $v;
}
}
$self->{Namespacemap} ||= {};
if ( defined( $args{charmap} ) ) {
$self->{Charmap} = {};
while ( my ($k, $v) = ( each( %{$args{charmap}} ) )) {
push @{$self->{Charmap}->{$k}}, ref( $v ) ? @{$v} : $v;
}
}
$self->{Charmap} ||= {};
# Skipelements:
# Makes sense from an interface standpoint for the user
# to pass an array ref, but it makes it more efficient to
# implement if its a hash ref. Let's pull a little juju.
my %skippers = ();
if ( $args{skipelements} ) {
%skippers = map { $_, 1} @{$args{skipelements}}
}
$self->{Skipelements} = \%skippers;
}
sub parse_start {
my $self = shift;
$self->init( @_ ) if scalar @_;
$self->start_document( {} );
if ( defined( $self->{ProcessingInstructions} ) && scalar( @{$self->{ProcessingInstructions}}) > 0 ) {
my $pis = delete $self->{ProcessingInstructions};
while ( my ( $target, $data ) = ( splice( @$pis, 0, 2)) ) {
$self->parse_pi( $target, $data );
}
}
unless ( defined $self->{SkipRoot} ) {
$self->start_element( $self->_start_details( $self->{RootName} ) );
push @{$self->{_Parents}}, $self->{RootName};
}
}
sub parse_end {
my $self = shift;
unless ( defined $self->{SkipRoot} ) {
$self->end_element( $self->_end_details( $self->{RootName} ) );
}
foreach my $uri ( keys( %{$self->{DeclaredNamespaces}} )) {
next if $uri eq $NS_XMLNS;
next if $uri eq $NS_XML;
next if not defined $self->{DeclaredNamespaces}->{$uri};
$self->end_prefix_mapping({ Prefix => $self->{DeclaredNamespaces}->{$uri},
NamespaceURI => $uri
});
}
return $self->end_document();
}
sub parse {
my $self = shift;
my $wtf = shift || die "No Data Passed!";
$self->init( @_ );
my $type = $self->get_type( $wtf );
if ( defined $type ) {
my $processor = lc( $type ) . 'ref2SAX';
# process the document...
$self->parse_start;
$self->$processor( $wtf );
$self->parse_end;
}
else {
die "Data passed must be a reference.";
}
}
sub parse_chunk {
my $self = shift;
my $wtf = shift || die "No Data Passed!";
my $type = $self->get_type( $wtf );
if ( defined $type ) {
my $processor = lc( $type ) . 'ref2SAX';
$self->$processor( $wtf );
}
else {
die "Data passed must be a reference.";
}
}
# Check if we have visited a given reference before
sub circular {
my($self, $ref) = @_;
my $addr = refaddr($ref);
my $result = $self->{Seen}->{$addr};
$self->{Seen}->{$addr} = 1;
return $result;
}
sub hashref2SAX {
my $self = shift;
my $hashref= shift;
my $char_data = '';
return if $self->circular($hashref);
ELEMENT: foreach my $key (keys (%{$hashref} )) {
my $value = $hashref->{$key};
my $element_name = $self->_keymapped_name( $key );
next if defined $self->{Skipelements}->{$element_name};
if ( defined $self->{_Parents}->[-1] and defined $self->{Attrmap}->{$self->{_Parents}->[-1]} ) {
foreach my $name ( @{$self->{Attrmap}->{$self->{_Parents}->[-1]}} ) {
next ELEMENT if $name eq $element_name;
}
}
if ( defined $self->{_Parents}->[-1] and defined $self->{Charmap}->{$self->{_Parents}->[-1]} ) {
if ( grep {$_ eq $element_name} @{$self->{Charmap}->{$self->{_Parents}->[-1]}} ) {
$self->characters( {Data => $value });
next ELEMENT;
}
}
my $type = $self->get_type( $value );
if ( $type eq 'ARRAY' ) {
push @{$self->{_Parents}}, $element_name;
$self->arrayref2SAX( $value );
pop (@{$self->{_Parents}});
}
elsif ( $type eq 'HASH' ) {
# attr mojo
my %attrs = ();
if ( defined $self->{Attrmap}->{$element_name} ) {
my @attr_names = ();
ATTR: foreach my $child ( keys( %{$value} )) {
my $name = $self->_keymapped_name( $child );
if ( grep {$_ eq $name} @{$self->{Attrmap}->{$element_name}} ) {
if ( ref( $value->{$child} ) ) {
warn "Cannot use a reference value " . $value->{$child} . " for key '$child' as XML attribute\n";
next ATTR;
}
$attrs{$name} = $value->{$child};
}
}
}
$self->start_element( $self->_start_details( $element_name, \%attrs ) );
push @{$self->{_Parents}}, $element_name;
$self->hashref2SAX( $value );
pop (@{$self->{_Parents}});
$self->end_element( $self->_end_details( $element_name ) );
}
else {
$self->start_element( $self->_start_details( $element_name ) );
$self->characters( {Data => $value} );
$self->end_element( $self->_end_details( $element_name ) );
}
}
}
sub arrayref2SAX {
my $self = shift;
my $arrayref= shift;
my $passed_name = shift || $self->{_Parents}->[-1];
my $temp_name = $self->_keymapped_name( $passed_name );
return if $self->circular($arrayref);
my $element_name;
my $i;
ELEMENT: for ( $i = 0; $i < @{$arrayref}; $i++ ) {
if ( ref( $temp_name ) eq 'ARRAY' ) {
my $ntest = $temp_name->[$i] || $self->{DefaultElementName};
if ( ref( $ntest ) eq 'CODE' ) {
$element_name = &{$ntest}();
}
else {
$element_name = $self->_keymapped_name( $ntest );
}
}
else {
$element_name = $temp_name;
}
next if defined $self->{Skipelements}->{$element_name};
my $type = $self->get_type( $arrayref->[$i] );
my $value = $arrayref->[$i];
if ( $type eq 'ARRAY' ) {
push @{$self->{_Parents}}, $element_name;
$self->arrayref2SAX( $value );
pop (@{$self->{_Parents}});
}
elsif ( $type eq 'HASH' ) {
# attr mojo
my %attrs = ();
if ( defined $self->{Attrmap}->{$element_name} ) {
my @attr_names = ();
ATTR: foreach my $child ( keys( %{$value} )) {
my $name = $self->_keymapped_name( $child );
if ( grep {$_ eq $name} @{$self->{Attrmap}->{$element_name}} ) {
if ( ref( $value->{$child} ) ) {
warn "Cannot use a reference value " . $value->{$child} . " for key '$child' as XML attribute\n";
next ATTR;
}
$attrs{$name} = $value->{$child};
}
}
}
$self->start_element( $self->_start_details( $element_name, \%attrs ) );
push @{$self->{_Parents}}, $element_name;
$self->hashref2SAX( $arrayref->[$i] );
pop (@{$self->{_Parents}});
$self->end_element( $self->_end_details( $element_name ) );
}
else {
$self->start_element( $self->_start_details( $element_name ) );
$self->characters( {Data => $arrayref->[$i]} );
$self->end_element( $self->_end_details( $element_name ) );
}
}
}
sub get_type {
my $self = shift;
my $wtf = shift;
my $type = ref( $wtf );
if ( $type ) {
if ( $type eq 'ARRAY' or $type eq 'HASH' or $type eq 'SCALAR') {
return $type;
}
else {
# we were passed an object, yuk.
# props to barrie slaymaker for the tip here... mine was much fuglier. ;-)
if ( UNIVERSAL::isa( $wtf, "HASH" ) ) {
return 'HASH';
}
elsif ( UNIVERSAL::isa( $wtf, "ARRAY" ) ) {
return 'ARRAY';
}
elsif ( UNIVERSAL::isa( $wtf, "SCALAR" ) ) {
return 'SCALAR';
}
else {
die "Unhandlable reference passed: $type \n";
}
}
}
else {
return '_plain';
}
}
###
# Interface helpers
###
sub add_namespace {
my $self = shift;
my %args = @_;
unless ( defined $args{prefix} and defined $args{uri} ) {
warn "Invalid arguments passed to add_namespace, skipping.";
return;
}
$self->{Namespaces}->{"$args{uri}"} = $args{prefix};
}
sub namespacemap {
my $self = shift;
my %nsmap;
if ( scalar( @_ ) > 0 ) {
if ( ref( $_[0] )) {
%nsmap = %{$_[0]};
}
else {
%nsmap = @_;
}
while ( my ($k, $v) = each ( %nsmap ) ) {
if ( ref( $v ) ) {
$self->{Namespacemap}->{$k} = $v;
}
else {
$self->{Namespacemap}->{$k} = [ $v ];
}
}
}
return wantarray ? %{$self->{Namespacemap}} : $self->{Namespacemap};
}
sub add_namespacemap {
my $self = shift;
my %args = @_;
foreach my $uri ( keys( %args )) {
push @{$self->{Namespacemap}->{"$uri"}}, $args{$uri};
}
}
sub delete_namespacemap {
my $self = shift;
my @mapped;
if ( scalar( @_ ) > 0 ) {
if ( ref( $_[0] )) {
@mapped = @{$_[0]};
}
else {
@mapped = @_;
}
foreach my $name ( @mapped ) {
foreach my $uri ( keys( %{$self->{Namespacemap}} )) {
my $i;
for ($i = 0; $i < scalar @{$self->{Namespacemap}->{$uri}}; $i++) {
splice @{$self->{Namespacemap}->{$uri}}, $i, 1 if $self->{Namespacemap}->{$uri}->[$i] eq $name;
}
delete $self->{Namespacemap}->{$uri} unless scalar @{$self->{Namespacemap}->{$uri}} > 0;
}
}
}
}
sub attrmap {
my $self = shift;
my %attrmap;
if ( scalar( @_ ) > 0 ) {
if ( ref( $_[0] )) {
%attrmap = %{$_[0]};
}
else {
%attrmap = @_;
}
while ( my ($k, $v) = each( %attrmap )) {
if ( ref( $v ) ) {
$self->{Attrmap}->{$k} = $v;
}
else {
$self->{Attrmap}->{$k} = [ $v ];
}
}
}
return wantarray ? %{$self->{Attrmap}} : $self->{Attrmap};
}
sub add_attrmap {
my $self = shift;
my %attrmap;
if ( scalar( @_ ) > 0 ) {
if ( ref( $_[0] )) {
%attrmap = %{$_[0]};
}
else {
%attrmap = @_;
}
while ( my ($k, $v) = each ( %attrmap ) ) {
if ( ref( $v ) ) {
$self->{Attrmap}->{$k} = $v;
}
else {
$self->{Attrmap}->{$k} = [ $v ];
}
}
}
}
sub delete_attrmap {
my $self = shift;
my @mapped;
if ( scalar( @_ ) > 0 ) {
if ( ref( $_[0] )) {
@mapped = @{$_[0]};
}
else {
@mapped = @_;
}
foreach my $name ( @mapped ) {
delete $self->{Attrmap}->{$name} if $self->{Attrmap}->{$name};
}
}
}
sub charmap {
my $self = shift;
my %charmap;
if ( scalar( @_ ) > 0 ) {
if ( ref( $_[0] )) {
%charmap = %{$_[0]};
}
else {
%charmap = @_;
}
while ( my ($k, $v) = each( %charmap )) {
if ( ref( $v ) ) {
$self->{Charmap}->{$k} = $v;
}
else {
$self->{Charmap}->{$k} = [ $v ];
}
}
}
return wantarray ? %{$self->{Charmap}} : $self->{Charmap};
}
sub add_charmap {
my $self = shift;
my %charmap;
if ( scalar( @_ ) > 0 ) {
if ( ref( $_[0] )) {
%charmap = %{$_[0]};
}
else {
%charmap = @_;
}
while ( my ($k, $v) = each ( %charmap ) ) {
if ( ref( $v ) ) {
$self->{Charmap}->{$k} = $v;
}
else {
$self->{Charmap}->{$k} = [ $v ];
}
}
}
}
sub delete_charmap {
my $self = shift;
my @mapped;
if ( scalar( @_ ) > 0 ) {
if ( ref( $_[0] )) {
@mapped = @{$_[0]};
}
else {
@mapped = @_;
}
foreach my $name ( @mapped ) {
delete $self->{Charmap}->{$name} if $self->{Charmap}->{$name};
}
}
}
sub add_keymap {
my $self = shift;
my %keymap;
if ( scalar( @_ ) > 0 ) {
if ( ref( $_[0] )) {
%keymap = %{$_[0]};
}
else {
%keymap = @_;
}
foreach my $name ( keys( %keymap )) {
$self->{Keymap}->{$name} = $keymap{$name};
}
}
}
sub delete_keymap {
my $self = shift;
my @mapped;
if ( scalar( @_ ) > 0 ) {
if ( ref( $_[0] )) {
@mapped = @{$_[0]};
}
else {
@mapped = @_;
}
foreach my $name ( @mapped ) {
delete $self->{Keymap}->{$name} if $self->{Keymap}->{$name};
}
}
}
sub add_skipelements {
my $self = shift;
my @skippers;
if ( scalar( @_ ) > 0 ) {
if ( ref( $_[0] )) {
@skippers = @{$_[0]};
}
else {
@skippers = @_;
}
foreach my $name ( @skippers ) {
$self->{Skipelements}->{$name} = 1;
}
}
}
sub delete_skipelements {
my $self = shift;
my @skippers;
if ( scalar( @_ ) > 0 ) {
if ( ref( $_[0] )) {
@skippers = @{$_[0]};
}
else {
@skippers = @_;
}
foreach my $name ( @skippers ) {
delete $self->{Skipelements}->{$name} if $self->{Skipelements}->{$name};
}
}
}
sub rootname {
my ($self, $rootname) = @_;
# ubu: add a check to warn them if the processing has already begun?
if ( defined $rootname ) {
$self->{RootName} = $rootname;
}
return $self->{RootName};
}
sub bindattrs {
my $self = shift;
my $flag = shift;
if ( defined($flag) ) {
if ($flag == 0) {
$self->{BindAttrs} = undef;
}
else {
$self->{BindAttrs} = 1;
}
}
return $self->{BindAttrs};
}
sub defaultname {
my ($self, $dname) = @_;
if ( defined $dname ) {
$self->{DefaultElementName} = $dname;
}
return $self->{DefaultElementName};
}
sub keymap {
my $self = shift;
my %keymap;
if ( scalar( @_ ) > 0 ) {
if ( ref( $_[0] )) {
%keymap = %{$_[0]};
}
else {
%keymap = @_;
}
$self->{Keymap} = \%keymap;
}
return wantarray ? %{$self->{Keymap}} : $self->{Keymap};
}
sub skipelements {
my $self = shift;
my @skippers;
if ( scalar( @_ ) > 0 ) {
if ( ref( $_[0] )) {
@skippers = @{$_[0]};
}
else {
@skippers = @_;
}
my %skippers = map { $_, 1} @skippers;
$self->{Skipelements} = \%skippers;
}
my @skippers_out = keys %{$self->{Skipelements}} || ();
return wantarray ? @skippers_out : \@skippers_out;
}
#XXX
sub parse_pi {
my $self = shift;
my ( $target, $data_in ) = @_;
my $data_out = '';
my $ref = $self->get_type( $data_in );
if ( $ref eq 'SCALAR' ) {
$data_out = $$data_in;
}
elsif ( $ref eq 'ARRAY' ) {
$data_out = join ' ', @{$data_in};
}
elsif ( $ref eq 'HASH' ) {
foreach my $k (keys( %{$data_in} )) {
$data_out .= qq|$k="| . $data_in->{$k} . qq|" |;
}
}
else {
$data_out = $data_in;
}
$self->processing_instruction({ Target => $target, Data => $data_out });
}
###
# Convenience helpers to make 'stream style' friendly
###
sub start_tag {
my $self = shift;
my $element_name = shift;
my %attrs = @_;
$self->start_element( $self->_start_details( $element_name, \%attrs ) );
push @{$self->{_Parents}}, $element_name;
}
sub end_tag {
my ($self, $tagname) = @_;
$self->end_element( $self->_end_details( $tagname ) );
pop (@{$self->{_Parents}});
}
####
# Internal Helpers
###
sub _keymapped_name {
my ($self, $name) = @_;
my $element_name;
if ( defined $self->{Keymap}->{$name} ) {
my $temp_name = $self->{Keymap}->{$name};
if ( ref( $temp_name ) eq 'CODE' ) {
$element_name = &{$temp_name}( $name );
}
else {
$element_name = $temp_name;
}
}
elsif ( defined $self->{Keymap}->{'*'} ) {
my $temp_name = $self->{Keymap}->{'*'};
if ( ref( $temp_name ) eq 'CODE' ) {
$element_name = &{$temp_name}( $name );
}
else {
$element_name = $temp_name;
}
}
else {
$element_name = $name;
}
}
sub _start_details {
my $self = shift;
my ($element_name, $attrs) = @_;
my %real_attrs;
foreach my $attr (keys(%{$attrs})) {
my $uri;
my $prefix;
my $qname;
my $lname;
if ( defined $self->{BindAttrs} ) {
($uri, $prefix, $qname, $lname) = $self->_namespace_fixer( $attr );
}
else {
$lname = $self->_name_fixer( $attr );
$qname = $lname;
}
my $key_uri = $uri || "";
$real_attrs{"\{$key_uri\}$lname"} = {
Name => $qname,
LocalName => $lname,
Prefix => $prefix,
NamespaceURI => $uri,
Value => $attrs->{$attr} };
}
if ( scalar( keys( %{$self->{Namespaces}} )) > scalar( keys( %{$self->{DeclaredNamespaces}} )) ) {
my @unseen_uris = grep { not defined $self->{DeclaredNamespaces}->{$_} } keys( %{$self->{Namespaces}} );
foreach my $uri ( @unseen_uris ) {
my $qname;
my $prefix;
my $lname;
my $key_uri;
my $ns_uri;
# this, like the Java version of SAX2, explicitly follows production 5.2 of the
# W3C Namespaces rec.-- specifically:
# http://www.w3.org/TR/1999/REC-xml-names-19990114/#defaulting
if ( $self->{Namespaces}->{$uri} eq '#default' ) {
$qname = 'xmlns';
$lname = 'xmlns';
$prefix = undef;
$key_uri = "";
$ns_uri = undef;
}
else {
$lname = $self->{Namespaces}->{$uri};
$prefix = 'xmlns';
$qname = $prefix . ':' . $lname;
#$key_uri = "";
$key_uri = $NS_XMLNS;
$ns_uri = $NS_XMLNS;
}
$real_attrs{"\{$key_uri\}$lname"} = {
Name => $qname,
LocalName => $lname,
Prefix => $prefix,
NamespaceURI => $ns_uri,
Value => $uri };
# internal
$self->{DeclaredNamespaces}->{$uri} = $prefix;
# fire events if needed.
if ( defined $prefix ) {
$self->start_prefix_mapping( { Prefix => $self->{Namespaces}->{$uri},
NamespaceURI => $uri
});
}
}
}
my ($uri, $prefix, $qname, $lname) = $self->_namespace_fixer( $element_name );
my %element = (LocalName => $lname,
Name => $qname,
Prefix => $prefix,
NamespaceURI => $uri,
Attributes => \%real_attrs,
);
if ( defined $uri and grep { $element_name eq $_ } @{$self->{Namespacemap}->{$uri}} ) {
push @{$self->{InScopeNamespaceStack}}, [$uri, $prefix];
}
return \%element;
}
sub _end_details {
my $self = shift;
my ($element_name) = @_;
my ( $uri, $prefix, $qname, $lname ) = $self->_namespace_fixer( $element_name );
my %element = (LocalName => $lname,
Name => $qname,
Prefix => $prefix,
NamespaceURI => $uri,
);
if ( defined $uri and grep { $element_name eq $_ } @{$self->{Namespacemap}->{$uri}} ) {
pop @{$self->{InScopeNamespaceStack}};
}
return \%element;
}
sub _namespace_fixer {
my ( $self, $node_name ) = @_;
my $prefix;
my $qname;
my $uri;
my $lname = $self->_name_fixer( $node_name );
foreach my $ns ( keys( %{$self->{Namespacemap}} )) {
if ( grep { $node_name eq $_ } @{$self->{Namespacemap}->{"$ns"}} ) {
$uri = $ns;
}
}
if ( defined( $uri ) ) {
$prefix = $self->{Namespaces}->{"$uri"};
if ( $prefix eq '#default' ) {
$prefix = undef;
}
else {
$qname = $prefix . ':' . $lname;
}
$qname ||= $lname;
}
else {
if ( defined $self->{InScopeNamespaceStack}->[-1] ) {
($uri, $prefix) = @{$self->{InScopeNamespaceStack}->[-1]};
if ( $prefix ) {
$qname = $prefix . ':' . $lname;
}
}
}
$qname ||= $lname;
return ($uri, $prefix, $qname, $lname);
}
sub _name_fixer {
my ($self, $name) = @_;
# UNICODE WARNING
$name =~ s|^[^a-zA-Z_:]{1}|_|g;
$name =~ tr|a-zA-Z0-9._:-|_|c;
return $name;
}
1;
__END__
=head1 NAME
XML::Generator::PerlData - Perl extension for generating SAX2 events from nested Perl data structures.
=head1 SYNOPSIS
use XML::Generator::PerlData;
use SomeSAX2HandlerOrFilter;
## Simple style ##
# get a deeply nested Perl data structure...
my $hash_ref = $obj->getScaryNestedDataStructure();
# create an instance of a handler class to forward events to...
my $handler = SomeSAX2HandlerOrFilter->new();
# create an instance of the PerlData driver...
my $driver = XML::Generator::PerlData->new( Handler => $handler );
# generate XML from the data structure...
$driver->parse( $hash_ref );
## Or, Stream style ##
use XML::Generator::PerlData;
use SomeSAX2HandlerOrFilter;
# create an instance of a handler class to forward events to...
my $handler = SomeSAX2HandlerOrFilter->new();
# create an instance of the PerlData driver...
my $driver = XML::Generator::PerlData->new( Handler => $handler );
# start the event stream...
$driver->parse_start();
# pass the data through in chunks
# (from a database handle here)
while ( my $array_ref = $dbd_sth->fetchrow_arrayref ) {
$driver->parse_chunk( $array_ref );
}
# end the event stream...
$driver->parse_end();
and you're done...
=head1 DESCRIPTION
XML::Generator::PerlData provides a simple way to generate SAX2 events
from nested Perl data structures, while providing finer-grained control
over the resulting document streams.
Processing comes in two flavors: B<Simple Style> and B<Stream Style>:
In a nutshell, 'simple style' is best used for those cases where you have a single
Perl data structure that you want to convert to XML as quickly and painlessly as possible. 'Stream
style' is more useful for cases where you are receiving chunks of data (like from a DBI handle)
and you want to process those chunks as they appear. See B<PROCESSING METHODS> for more info about
how each style works.
=head1 CONSTRUCTOR METHOD AND CONFIGURATION OPTIONS
=head2 new
(class constructor)
B<Accepts:> An optional hash of configuration options.
B<Returns:> A new instance of the XML::Generator::PerlData class.
Creates a new instance of XML::Generator::PerlData.
While basic usage of this module is designed to be simple and straightforward,
there is a small host of options available to help ensure that the SAX event streams
(and by extension the XML documents) that are created from the data structures you
pass are in just the format that you want.
=head3 Options
=over 4
=item * B<Handler> (required)
XML::Generator::PerlData is a SAX Driver/Generator. As such, it
needs a SAX Handler or Filter class to forward its events to. The value for this
option must be an instance of a SAX2-aware Handler or Filter.
=item * B<rootname> (optional)
Sets the name of the top-level (root) element. The default is 'document'.
=item * B<defaultname> (optional)
Sets the default name to be used for elements when no other logical name
is available (think lists-of-lists). The default is 'default'.
=item * B<keymap> (optional)
Often, the names of the keys in a given hash do not map directly to the XML
elements names that you want to appear in the resulting document. The option
contains a set of keyname->element name mappings for the current process.
=item * B<skipelements> (optional)
Passed in as an array reference, this option sets the internal list of keynames
that will be skipped over during processing. Note that any descendant structures
belonging to those keys will also be skipped.
=item * B<attrmap> (optional)
Used to determine which 'children' of a given hash key/element-name will
be forwarded as attributes of that element rather than as child elements.
(see CAVEATS for a discussion of the limitations of this method.)
=item * B<namespaces> (optional)
Sets the internal list of namespace/prefix pairs for the current process. It takes
the form of a hash, where the keys are the URIs of the given namespace and the
values are the associated prefix.
To set a default (unprefixed) namespace, set the prefix to '#default'.
=item * B<namespacemap> (optional)
Sets which elements in the result will be bound to which declared namespaces. It
takes the form of a hash of key/value pairs where the keys are one of the declared
namespace URIs that are relevant to the current process and the values are either
single key/element names or an array reference of key/element names.
=item * B<skiproot> (optional)
When set to a defined value, this option blocks the generator from adding
the top-level root element when parse() or parse_start() and parse_end()
are called.
I<Do not> use this option unless you are absolutely sure you know what you
are doing and why, since the resulting event stream will most likely
produce non-well-formed XML.
=item * B<bindattrs> (optional)
When set to a defined value, this option tells the generator to bind attributes
to the same namespace as element that contains them. By default attributes will
be unbound and unprefixed.
=item * B<processing_instructions> (optional)
This option provides a way to include XML processing instructions events into
the generated stream before the root element is emitted. The value of this key
can be either a hash reference or an array reference of hash references.
For example, when connected to L<XML::SAX::Writer>:
$pd->new( Handler => $writer_instance,
rootname => 'document',
processing_instructions => {
'xml-stylesheet' => {
href => '/path/to/stylesheet.xsl',
type => 'text/xml',
},
});
would generate
<?xml version="1.0"?>
<?xml-stylesheet href="/path/to/stylesheet.xsl" type="text/xsl" ?>
<document>
...
Where multiple processing instructions will have the same target and/or where the document order of those PIs matter, an array reference should be used instead. For example:
$pd->new( Handler => $writer_instance,
rootname => 'document',
processing_instructions => [
'xml-stylesheet' => {
href => '/path/to/stylesheet.xsl',
type => 'text/xml',
},
'xml-stylesheet' => {
href => '/path/to/second/stylesheet.xsl',
type => 'text/xml',
}
]);
would produce:
<?xml version="1.0"?>
<?xml-stylesheet href="/path/to/stylesheet.xsl" type="text/xsl" ?>
<?xml-stylesheet href="/path/to/second/stylesheet.xsl" type="text/xsl" ?>
<document>
...
=back
=head1 PROCESSING METHODS
=head2 Simple style processing
=over 4
=item B<parse>
B<Accepts:> A reference to a Perl data structure. Optionally, a hash of config options.
B<Returns:> [none]
The core method used during 'simple style' processing, this method accepts a reference
to a Perl data structure and, based on the options passed, produces a stream of SAX events
that can be used to transform that structure into XML. The optional second argument is
a hash of config options identical to those detailed in the OPTIONS section of the
the new() constructor description.
B<Examples:>
$pd->parse( \%my_hash );
$pd->parse( \%my_hash, rootname => 'recordset' );
$pd->parse( \@my_list, %some_options );
$pd->parse( $my_hashref );
$pd->parse( $my_arrayref, keymap => { default => ['foo', 'bar', 'baz'] } );
=back
=head2 Stream style processing
=over 4
=item B<parse_start>
B<Accepts:> An optional hash of config options.
B<Returns:> [none]
Starts the SAX event stream and (unless configured not to)
fires the event the top-level root element. The optional argument is
a hash of config options identical to those detailed in the OPTIONS section of the
the new() constructor description.
B<Example:>
$pd->parse_start();
=item B<parse_end>
B<Accepts:> [none].
B<Returns:> Varies. Returns what the final Handler returns.
Ends the SAX event stream and (unless configured not to)
fires the event to close the top-level root element.
B<Example:>
$pd->parse_end();
=item B<parse_chunk>
B<Accepts:> A reference to a Perl data structure.
B<Returns:> [none]
The core method used during 'stream style' processing, this method accepts a reference
to a Perl data structure and, based on the options passed, produces a stream of SAX events
that can be used to transform that structure into XML.
B<Examples:>
$pd->parse_chunk( \%my_hash );
$pd->parse_chunk( \@my_list );
$pd->parse_chunk( $my_hashref );
$pd->parse_chunk( $my_arrayref );
=back
=head1 CONFIGURATION METHODS
All config options can be passed to calls to the new() constructor using the
typical "hash of named properties" syntax. The methods below offer direct
access to the individual options (or ways to add/remove the smaller definitions
contained by those options).
=over 4
=item B<init>
B<Accepts:> The same configuration options that can be passed to the new() constructor.
B<Returns:> [none]
See the list of B<OPTIONS> above in the definition of new() for details.
=item B<rootname>
B<Accepts:> A string or [none].
B<Returns:> The current root name.
When called with an argument, this method sets the name of the top-level (root) element. It
always returns the name of the current (or new) root name.
B<Examples:>
$pd->rootname( $new_name );
my $current_root = $pd->rootname();
=item B<defaultname>
B<Accepts:> A string or [none]
B<Returns:> The current default element name.
When called with an argument, this method sets the name of the default element. It
always returns the name of the current (or new) default name.
B<Examples:>
$pd->defaultname( $new_name );
my $current_default = $pd->defaultname();
=item B<keymap>
B<Accepts:> A hash (or hash reference) containing a series of keyname->elementname mappings or [none].
B<Returns:> The current keymap hash (as a plain hash, or hash reference depending on caller context).
When called with a hash (hash reference) as its argument, this method sets/resets the entire internal
keyname->elementname mappings definitions (where 'keyname' means the name of a given
key in the hash and 'elementname' is the name used when firing SAX events for that key).
In addition to simple name->othername mappings, value of a keymap option can also a reference
to a subroutine (or an anonymous sub). The keyname will be passed as the sole argument to
this subroutine and the sub is expected to return the new element name. In the cases of nested
arrayrefs, no keyname will be passed, but you can still generate the name from scratch.
Extending that idea, keymap will also accept a default mapping using the key '*' that will
be applied to all elements that do have an explicit mapping configured.
To add new mappings or remove existing ones without having to reset the whole list of
mappings, see add_keymap() and delete_keymap() respectively.
If your are using "stream style" processing, this method should be used with caution since
altering this mapping during processing may result in not-well-formed XML.
B<Examples:>
$pd->keymap( keyname => 'othername',
anotherkey => 'someothername' );
$pd->keymap( \%mymap );
# make all tags lower case
$pd->keymap( '*' => sub{ return lc( $_[0];} );
# process keys named 'keyname' with a local sub
$pd->keymap( keyname => \&my_namer,
my %kmap_hash = $pd->keymap();
my $kmap_hashref = $pd->keymap();
=item B<add_keymap>
B<Accepts:> A hash (or hash reference) containing a series of keyname->elementname mappings.
B<Returns:> [none]
Adds a series of keyname->elementname mappings (where 'keyname' means the name of a given
key in the hash and 'elementname' is the name used when firing SAX events for that key).
B<Examples:>
$pd->add_keymap( keyname => 'othername' );
$pd->add_keymap( \%hash_of_mappings );
=item B<delete_keymap>
B<Accepts:> A list (or array reference) of element/keynames.
B<Returns:> [none]
Deletes a list of keyname->elementname mappings (where 'keyname' means the name of a given
key in the hash and 'elementname' is the name used when firing SAX events for that key).
This method should be used with caution since altering this mapping during processing
may result in not-well-formed XML.
B<Examples:>
$pd->delete_keymap( 'some', 'key', 'names' );
$pd->delete_keymap( \@keynames );
=item B<skipelements>
B<Accepts:> A list (or array reference) containing a series of key/element names or [none].
B<Returns:> The current skipelements array (as a plain list, or array reference depending on caller context).
When called with an array (array reference) as its argument, this method sets/resets the entire internal
skipelement definitions (which determines which keys will not be 'parsed' during processing).
To add new mappings or remove existing ones without having to reset the whole list of
mappings, see add_skipelements() and delete_skipelements() respectively.
B<Examples:>
$pd->skipelements( 'elname', 'othername', 'thirdname' );
$pd->skipelements( \@skip_names );
my @skiplist = $pd->skipelements();
my $skiplist_ref = $pd->skipelements();
=item B<add_skipelements>
B<Accepts:> A list (or array reference) containing a series of key/element names.
B<Returns:> [none]
Adds a list of key/element names to skip during processing.
B<Examples:>
$pd->add_skipelements( 'some', 'key', 'names' );
$pd->add_skipelements( \@keynames );
=item B<delete_skipelements>
B<Accepts:> A list (or array reference) containing a series of key/element names.
B<Returns:> [none]
Deletes a list of key/element names to skip during processing.
B<Examples:>
$pd->delete_skipelements( 'some', 'key', 'names' );
$pd->delete_skipelements( \@keynames );
=item B<charmap>
B<Accepts:> A hash (or hash reference) containing a series of parent/child keyname pairs or [none].
B<Returns:> The current charmap hash (as a plain hash, or hash reference depending on caller context).
When called with a hash (hash reference) as its argument, this method sets/resets the entire internal
keyname/elementname->characters children mappings definitions (where 'keyname' means the name of a given
key in the hash and 'characters children' is list containing the nested keynames that should be passed as
the text children of the element named 'keyname' (instead of being processed as child elements or attributes).
To add new mappings or remove existing ones without having to reset the whole list of
mappings, see add_charmap() and delete_charmap() respectively.
See CAVEATS for the limitations that relate to this method.
B<Examples:>
$pd->charmap( elname => ['list', 'of', 'nested', 'keynames' );
$pd->charmap( \%mymap );
my %charmap_hash = $pd->charmap();
my $charmap_hashref = $pd->charmap();
=item B<add_charmap>
B<Accepts:> A hash or hash reference containing a series of parent/child keyname pairs.
B<Returns:> [none]
Adds a series of parent-key -> child-key relationships that define which of the
possible child keys will be processed as text children of the created 'parent'
element.
B<Examples:>
$pd->add_charmap( parentname => ['list', 'of', 'child', 'keys'] );
$pd->add_charmap( parentname => 'childkey' );
$pd->add_charmap( \%parents_and_kids );
=item B<delete_charmap>
B<Accepts:> A list (or array reference) of element/keynames.
B<Returns:> [none]
Deletes a list of parent-key -> child-key relationships from the instance-wide
hash of "parent->nested names to pass as text children definitions. If you
need to alter the list of child names (without deleting the parent key) use
add_charmap() to reset the parent-key's definition.
B<Examples:>
$pd->delete_charmap( 'some', 'parent', 'keys' );
$pd->delete_charmap( \@parentkeynames );
=item B<attrmap>
B<Accepts:> A hash (or hash reference) containing a series of parent/child keyname pairs or [none].
B<Returns:> The current attrmap hash (as a plain hash, or hash reference depending on caller context).
When called with a hash (hash reference) as its argument, this method sets/resets the entire internal
keyname/elementname->attr children mappings definitions (where 'keyname' means the name of a given
key in the hash and 'attr children' is list containing the nested keynames that should be passed as
attributes of the element named 'keyname' (instead of as child elements).
To add new mappings or remove existing ones without having to reset the whole list of
mappings, see add_attrmap() and delete_attrmap() respectively.
See CAVEATS for the limitations that relate to this method.
B<Examples:>
$pd->attrmap( elname => ['list', 'of', 'nested', 'keynames' );
$pd->attr( \%mymap );
my %attrmap_hash = $pd->attrmap();
my $attrmap_hashref = $pd->attrmap();
=item B<add_attrmap>
B<Accepts:> A hash or hash reference containing a series of parent/child keyname pairs.
B<Returns:> [none]
Adds a series of parent-key -> child-key relationships that define which of the
possible child keys will be processed as attributes of the created 'parent'
element.
B<Examples:>
$pd->add_attrmap( parentname => ['list', 'of', 'child', 'keys'] );
$pd->add_attrmap( parentname => 'childkey' );
$pd->add_attrmap( \%parents_and_kids );
=item B<delete_attrmap>
B<Accepts:> A list (or array reference) of element/keynames.
B<Returns:> [none]
Deletes a list of parent-key -> child-key relationships from the instance-wide
hash of "parent->nested names to pass as attributes" definitions. If you
need to alter the list of child names (without deleting the parent key) use
add_attrmap() to reset the parent-key's definition.
B<Examples:>
$pd->delete_attrmap( 'some', 'parent', 'keys' );
$pd->delete_attrmap( \@parentkeynames );
=item B<bindattrs>
B<Accepts:> 1 or 0 or [none].
B<Returns:> undef or 1 based on the current state of the bindattrs option.
Consider:
<myns:foo bar="quux"/>
and
<myns:foo myns:bar="quux"/>
are I<not> functionally equivalent.
By default, attributes will be forwarded as I<not> being bound to the namespace
of the containing element (like the first example above). Setting this
option to a true value alters that behavior.
B<Examples:>
$pd->bindattrs(1); # attributes now bound and prefixed.
$pd->bindattrs(0);
my $is_binding = $pd->bindattrs();
=item B<add_namespace>
B<Accepts:> A hash containing the defined keys 'uri' and 'prefix'.
B<Returns:> [none]
Add a namespace URI/prefix pair to the instance-wide list of XML namespaces
that will be used while processing. The reserved prefix '#default' can
be used to set the default (unprefixed) namespace declaration for elements.
B<Examples:>
$pd->add_namespace( uri => 'http://myhost.tld/myns',
prefix => 'myns' );
$pd->add_namespace( uri => 'http://myhost.tld/default',
prefix => '#default' );
See namespacemap() or the namespacemap option detailed in new() for details
about how to associate key/element name with a given namespace.
=item B<namespacemap>
B<Accepts:> A hash (or hash reference) containing a series of uri->key/element name mappings or [none].
B<Returns:> The current namespacemap hash (as a plain hash, or hash reference depending on caller context).
When called with a hash (hash reference) as its argument, this method sets/resets the entire internal
namespace URI->keyname/elementname mappings definitions (where 'keyname' means the name of a given
key in the hash and 'namespace URI' is a declared namespace URI for the given process).
To add new mappings or remove existing ones without having to reset the whole list of
mappings, see add_namespacemap() and delete_namespacemap() respectively.
If your are using "stream style" processing, this method should be used with caution since
altering this mapping during processing may result in not-well-formed XML.
B<Examples:>
$pd->add_namespace( uri => 'http://myhost.tld/myns',
prefix => 'myns' );
$pd->namespacemap( 'http://myhost.tld/myns' => elname );
$pd->namespacemap( 'http://myhost.tld/myns' => [ 'list', 'of', 'elnames' ] );
$pd->namespacemap( \%mymap );
my %nsmap_hash = $pd->namespacemap();
my $nsmap_hashref = $pd->namespacemap();
=item B<add_namespacemap>
B<Accepts:> A hash (or hash reference) containing a series of uri->key/element name mappings
B<Returns:> [none]
Adds one or more namespace->element/keyname rule to the instance-wide
list of mappings.
B<Examples:>
$pd->add_namespacemap( 'http://myhost.tld/foo' => ['some', 'list', 'of' 'keys'] );
$pd->add_namespacemap( %new_nsmappings );
=item B<remove_namespacemap>
B<Accepts:> A list (or array reference) of element/keynames.
B<Returns:> [none]
Removes a list of namespace->element/keyname rules to the instance-wide
list of mappings.
B<Examples:>
$pd->delete_namespacemap( 'foo', 'bar', 'baz' );
$pd->delete_namespacemap( \@list_of_keynames );
=back
=head1 SAX EVENT METHODS
As a subclass of XML::SAX::Base, XML::Generator::PerlData allows you to
call all of the SAX event methods directly to insert arbitrary events
into the stream as needed. While its use in this way is probably a
I<Bad Thing> (and only relevant to "stream style" processing) it is
good to know that such fine-grained access is there if you need it.
With that aside, there may be cases (again, using the "stream style") where
you'll want to insert single elements into the output (wrapping each
array in series of arrays in single 'record' elements, for example).
The following methods may be used to simplify this task by allowing you
to pass in simple element name strings and have the result 'just work' without
requiring an expert knowledge of the Perl SAX2 implementation or
forcing you to keep track of things like namespace context.
Take care to ensure that every call to start_tag() has a corresponding call to end_tag()
or your documents will not be well-formed.
=over 4
=item B<start_tag>
B<Accepts:> A string containing an element name and an optional hash of simple key/value attributes.
B<Returns:> [none]
B<Examples:>
$pd->start_tag( $element_name );
$pd->start_tag( $element_name, id => $generated_id );
$pd->start_tag( $element_name, %some_attrs );
=item B<end_tag>
B<Accepts:> A string containing an element name.
B<Returns:> [none]
B<Examples:>
$pd->end_tag( $element_name );
=back
=head1 CAVEATS
In general, XML is based on the idea that every bit of data is going to have a
corresponding name (Elements, Attributes, etc.). While this is not at all a
Bad Thing, it means that some Perl data structures do not map cleanly onto
an XML representation.
Consider:
my %hash = ( foo => ['one', 'two', 'three'] );
How do you represent that as XML? Is it three 'foo' elements, or
is it a 'foo' parent element with 3 mystery children? XML::Generator::PerlData
chooses the former. Or:
<foo>one</foo>
<foo>two</foo>
<foo>three</foo>
Now consider:
my @lol = ( ['one', 'two', 'three'], ['four', 'five', 'six'] );
In this case you wind up with a pile of elements named 'default'. You can
work around this by doing $pd->add_keymap( default => ['list', 'of', 'names'] )
but that only works if you know how many entries are going to be in each nested
list.
The practical implication here is that the current version of XML::Generator::PerlData
favors data structures that are based on hashes of hashes for deeply nested structures (especally
when using B<Simple Style> processing) and some options like C<attrmap> do not work for
arrays at all. Future versions will address these issues if sanely possible.
=head1 AUTHOR
Kip Hampton, khampton@totalcinema.com
=head1 COPYRIGHT
(c) Kip Hampton, 2002-2014, All Rights Reserved.
=head1 LICENSE
This module is released under the Perl Artistic Licence and
may be redistributed under the same terms as perl itself.
=head1 SEE ALSO
L<XML::SAX>, L<XML::SAX::Writer>.
=cut
|