/usr/share/perl5/XML/Handler/Composer.pm is in libxml-handler-composer-perl 0.01-8.
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 | package XML::Handler::Composer;
use strict;
use XML::UM;
use Carp;
use vars qw{ $VERSION %DEFAULT_QUOTES %XML_MAPPING_CRITERIA };
$VERSION = '0.01';
%DEFAULT_QUOTES = (
XMLDecl => '"',
Attr => '"',
Entity => '"',
SystemLiteral => '"',
);
%XML_MAPPING_CRITERIA =
(
Text =>
{
'<' => '<',
'&' => '&',
']]>' => ']]>',
},
CDataSection =>
{
']]>' => ']]>', # NOTE: this won't be translated back correctly
},
Attr => # attribute value (assuming double quotes "" are used)
{
# '"' => '"', # Use ("'" => ''') when using single quotes
'<' => '<',
'&' => '&',
},
Entity => # entity value (assuming double quotes "" are used)
{
# '"' => '"', # Use ("'" => ''') when using single quotes
'%' => '%',
'&' => '&',
},
Comment =>
{
'--' => '--', # NOTE: this won't be translated back correctly
},
ProcessingInstruction =>
{
'?>' => '?>', # not sure if this will be translated back correctly
},
# The SYSTEM and PUBLIC identifiers in DOCTYPE declaration (quoted strings)
SystemLiteral =>
{
# '"' => '"', # Use ("'" => ''') when using single quotes
},
);
sub new
{
my ($class, %options) = @_;
my $self = bless \%options, $class;
$self->{EndWithNewline} = 1 unless defined $self->{EndWithNewline};
if (defined $self->{Newline})
{
$self->{ConvertNewlines} = 1;
}
else
{
# Use this when printing newlines in case the user didn't specify one
$self->{Newline} = "\x0A";
}
$self->{DocTypeIndent} = $self->{Newline} . " "
unless defined $self->{DocTypeIndent};
$self->{IndentAttlist} = " " unless defined $self->{IndentAttlist};
$self->{Print} = sub { print @_ } unless defined $self->{Print};
$self->{Quote} ||= {};
for my $q (keys %DEFAULT_QUOTES)
{
$self->{Quote}->{$q} ||= $DEFAULT_QUOTES{$q};
}
# Convert to UTF-8 by default, i.e. when <?xml encoding=...?> is missing
# and no {Encoding} is specified.
# Note that the internal representation *is* UTF-8, so we
# simply return the (string) parameter.
$self->{Encode} = sub { shift } unless defined $self->{Encode};
# Convert unmapped characters to hexadecimal constants a la '号'
$self->{EncodeUnmapped} = \&XML::UM::encode_unmapped_hex
unless defined $self->{EncodeUnmapped};
my $encoding = $self->{Encoding};
$self->setEncoding ($encoding) if defined $encoding;
$self->initMappers;
$self;
}
#
# Setup the mapping routines that convert '<' to '<' etc.
# for the specific XML constructs.
#
sub initMappers
{
my $self = shift;
my %escape;
my $convert_newlines = $self->{ConvertNewlines};
for my $n (qw{ Text Comment CDataSection Attr SystemLiteral
ProcessingInstruction Entity })
{
$escape{$n} = $self->create_utf8_mapper ($n, $convert_newlines);
}
# Text with xml:space="preserve", should not have newlines converted.
$escape{TextPreserveNL} = $self->create_utf8_mapper ('Text', 0);
# (If newline conversion is inactive, $escape{TextPreserveNL} does the
# same as $escape{Text} defined above ...)
$self->{Escape} = \%escape;
}
sub setEncoding
{
my ($self, $encoding) = @_;
$self->{Encode} = XML::UM::get_encode (
Encoding => $encoding, EncodeUnmapped => $self->{EncodeUnmapped});
}
sub create_utf8_mapper
{
my ($self, $construct, $convert_newlines) = @_;
my $c = $XML_MAPPING_CRITERIA{$construct};
croak "no XML mapping criteria defined for $construct"
unless defined $c;
my %hash = %$c;
# If this construct appears between quotes in the XML document
# (and it has a quoting character defined),
# ensure that the quoting character is appropriately converted
# to " or '
my $quote = $self->{Quote}->{$construct};
if (defined $quote)
{
$hash{$quote} = $quote eq '"' ? '"' : ''';
}
if ($convert_newlines)
{
$hash{"\x0A"} = $self->{Newline};
}
gen_utf8_subst (%hash);
}
#
# Converts a string literal e.g. "ABC" into '\x41\x42\x43'
# so it can be stuffed into a regular expression
#
sub str_to_hex # static
{
my $s = shift;
$s =~ s/(.)/ sprintf ("\\x%02x", ord ($1)) /egos;
$s;
}
#
# In later perl versions (5.005_55 and up) we can simply say:
#
# use utf8;
# $literals = join ("|", map { str_to_hex ($_) } keys %hash);
# $s =~ s/($literals)/$hash{$1}/ego;
#
sub gen_utf8_subst # static
{
my (%hash) = @_;
my $code = 'sub { my $s = shift; $s =~ s/(';
$code .= join ("|", map { str_to_hex ($_) } keys %hash);
$code .= ')|(';
$code .= '[\\x00-\\xBF]|[\\xC0-\\xDF].|[\\xE0-\\xEF]..|[\\xF0-\\xFF]...';
$code .= ')/ defined ($1) ? $hash{$1} : $2 /ego; $s }';
my $f = eval $code;
croak "XML::Handler::Composer - can't eval code: $code\nReason: $@" if $@;
$f;
}
# This should be optimized!
sub print
{
my ($self, $str) = @_;
$self->{Print}->($self->{Encode}->($str));
}
# Used by start_element. It determines the style in which empty elements
# are printed. The default implementation returns "/>" so they are printed
# like this: <a/>
# Override this method to support e.g. XHTML style tags.
sub get_compressed_element_suffix
{
my ($self, $event) = @_;
"/>";
# return " />" for XHTML style, or
# "><$tagName/>" for uncompressed tags (where $tagName is $event->{Name})
}
#----- PerlSAX handlers -------------------------------------------------------
sub start_document
{
my ($self) = @_;
$self->{InCDATA} = 0;
$self->{DTD} = undef;
$self->{PreserveWS} = 0; # root element has xml:space="default"
$self->{PreserveStack} = [];
$self->{PrintedXmlDecl} = 0; # whether <?xml ...?> was printed
}
sub end_document
{
my ($self) = @_;
# Print final Newline at the end of the XML document (if desired)
$self->print ($self->{Newline}) if $self->{EndWithNewline};
}
# This event is received *AFTER* the Notation, Element, Attlist etc. events
# that are contained within the DTD.
sub doctype_decl
{
my ($self, $event) = @_;
$self->flush_xml_decl;
my $q = $self->{Quote}->{SystemLiteral};
my $escape_literal = $self->{Escape}->{SystemLiteral};
my $name = $event->{Name};
my $sysId = $event->{SystemId};
$sysId = &$escape_literal ($sysId) if defined $sysId;
my $pubId = $event->{PublicId};
$pubId = &$escape_literal ($pubId) if defined $pubId;
my $str = "<!DOCTYPE $name";
if (defined $pubId)
{
$str .= " PUBLIC $q$pubId$q $q$sysId$q";
}
elsif (defined $sysId)
{
$str .= " SYSTEM $q$sysId$q";
}
my $dtd_contents = $self->{DTD};
my $nl = $self->{Newline};
if (defined $dtd_contents)
{
delete $self->{DTD};
$str .= " [$dtd_contents$nl]>$nl";
}
else
{
$str .= ">$nl";
}
$self->print ($str);
}
sub start_element
{
my ($self, $event) = @_;
my $preserve_stack = $self->{PreserveStack};
if (@$preserve_stack == 0)
{
# This is the root element. Print the <?xml ...?> declaration now if
# it wasn't printed and it should be.
$self->flush_xml_decl;
}
my $str = "<" . $event->{Name};
my $suffix = ">";
if ($event->{Compress})
{
$suffix = $self->get_compressed_element_suffix ($event);
}
# Push PreserveWS state of parent element on the stack
push @{ $preserve_stack }, $self->{PreserveWS};
$self->{PreserveWS} = $event->{PreserveWS};
my $ha = $event->{Attributes};
my @attr;
if (exists $event->{AttributeOrder})
{
my $defaulted = $event->{Defaulted};
if (defined $defaulted && !$self->{PrintDefaultAttr})
{
if ($defaulted > 0)
{
@attr = @{ $event->{AttributeOrder} }[0 .. $defaulted - 1];
}
# else: all attributes are defaulted i.e. @attr = ();
}
else # no attr are defaulted
{
@attr = @{ $event->{AttributeOrder} };
}
}
else # no attr order defined
{
@attr = keys %$ha;
}
my $escape = $self->{Escape}->{Attr};
my $q = $self->{Quote}->{Attr};
for (my $i = 0; $i < @attr; $i++)
{
#?? could print a newline every so often...
my $name = $attr[$i];
my $val = &$escape ($ha->{$name});
$str .= " $name=$q$val$q";
}
$str .= $suffix;
$self->print ($str);
}
sub end_element
{
my ($self, $event) = @_;
$self->{PreserveWS} = pop @{ $self->{PreserveStack} };
return if $event->{Compress};
$self->print ("</" . $event->{Name} . ">");
}
sub characters
{
my ($self, $event) = @_;
if ($self->{InCDATA})
{
#?? should this use $self->{PreserveWS} ?
my $esc = $self->{Escape}->{CDataSection};
$self->print (&$esc ($event->{Data}));
}
else # regular text
{
my $esc = $self->{PreserveWS} ?
$self->{Escape}->{TextPreserveNL} :
$self->{Escape}->{Text};
$self->print (&$esc ($event->{Data}));
}
}
sub start_cdata
{
my $self = shift;
$self->{InCDATA} = 1;
$self->print ("<![CDATA[");
}
sub end_cdata
{
my $self = shift;
$self->{InCDATA} = 0;
$self->print ("]]>");
}
sub comment
{
my ($self, $event) = @_;
$self->flush_xml_decl;
my $esc = $self->{Escape}->{Comment};
#?? still need to support comments in the DTD
$self->print ("<!--" . &$esc ($event->{Data}) . "-->");
}
sub entity_reference
{
my ($self, $event) = @_;
$self->flush_xml_decl;
my $par = $event->{Parameter} ? '%' : '&';
#?? parameter entities (like %par;) are NOT supported!
# PerlSAX::handle_default should be fixed!
$self->print ($par . $event->{Name} . ";");
}
sub unparsed_entity_decl
{
my ($self, $event) = @_;
$self->flush_xml_decl;
$self->entity_decl ($event);
}
sub notation_decl
{
my ($self, $event) = @_;
$self->flush_xml_decl;
my $name = $event->{Name};
my $sysId = $event->{SystemId};
my $pubId = $event->{PublicId};
my $q = $self->{Quote}->{SystemLiteral};
my $escape = $self->{Escape}->{SystemLiteral};
$sysId = &$escape ($sysId) if defined $sysId;
$pubId = &$escape ($pubId) if defined $pubId;
my $str = $self->{DocTypeIndent} . "<!NOTATION $name";
if (defined $pubId)
{
$str .= " PUBLIC $q$pubId$q";
}
if (defined $sysId)
{
$str .= " SYSTEM $q$sysId$q";
}
$str .= ">";
$self->{DTD} .= $str;
}
sub element_decl
{
my ($self, $event) = @_;
$self->flush_xml_decl;
my $name = $event->{Name};
my $model = $event->{Model};
$self->{DTD} .= $self->{DocTypeIndent} . "<!ELEMENT $name $model>";
}
sub entity_decl
{
my ($self, $event) = @_;
$self->flush_xml_decl;
my $name = $event->{Name};
my $par = "";
if ($name =~ /^%/)
{
# It's a parameter entity (i.e. %ent; instead of &ent;)
$name = substr ($name, 1);
$par = "% ";
}
my $str = $self->{DocTypeIndent} . "<!ENTITY $par$name";
my $value = $event->{Value};
my $sysId = $event->{SysId};
my $pubId = $event->{PubId};
my $ndata = $event->{Ndata};
my $q = $self->{Quote}->{SystemLiteral};
my $escape = $self->{Escape}->{SystemLiteral};
if (defined $value)
{
#?? use {Entity} quote etc...
my $esc = $self->{Escape}->{Entity};
my $p = $self->{Quote}->{Entity};
$str .= " $p" . &$esc ($value) . $p;
}
if (defined $pubId)
{
$str .= " PUBLIC $q" . &$escape ($pubId) . $q;
}
elsif (defined $sysId)
{
$str .= " SYSTEM";
}
if (defined $sysId)
{
$str .= " $q" . &$escape ($sysId) . $q;
}
$str .= " NDATA $ndata" if defined $ndata;
$str .= ">";
$self->{DTD} .= $str;
}
sub attlist_decl
{
my ($self, $event) = @_;
$self->flush_xml_decl;
my $elem = $event->{ElementName};
my $str = $event->{AttributeName} . " " . $event->{Type};
$str .= " #FIXED" if defined $event->{Fixed};
$str = $str;
my $def = $event->{Default};
if (defined $def)
{
$str .= " $def";
# Note sometimes Default is a value with quotes.
# We'll use the existing quotes in that case...
}
my $indent;
if (!exists($event->{First}) || $event->{First})
{
$self->{DTD} .= $self->{DocTypeIndent} . "<!ATTLIST $elem";
if ($event->{MoreFollow})
{
$indent = $self->{Newline} . $self->{IndentAttlist};
}
else
{
$indent = " ";
}
}
else
{
$indent = $self->{Newline} . $self->{IndentAttlist};
}
$self->{DTD} .= $indent . $str;
unless ($event->{MoreFollow})
{
$self->{DTD} .= '>';
}
}
sub xml_decl
{
my ($self, $event) = @_;
return if $self->{PrintedXmlDecl}; # already printed it
my $version = $event->{Version};
my $encoding = $event->{Encoding};
if (defined $self->{Encoding})
{
$encoding = $self->{Encoding};
}
else
{
$self->setEncoding ($encoding) if defined $encoding;
}
my $standalone = $event->{Standalone};
$standalone = ($standalone ? "yes" : "no") if defined $standalone;
my $q = $self->{Quote}->{XMLDecl};
my $nl = $self->{Newline};
my $str = "<?xml";
$str .= " version=$q$version$q" if defined $version;
$str .= " encoding=$q$encoding$q" if defined $encoding;
$str .= " standalone=$q$standalone$q" if defined $standalone;
$str .= "?>$nl$nl";
$self->print ($str);
$self->{PrintedXmlDecl} = 1;
}
#
# Prints the <xml ...?> declaration if it wasn't already printed
# *and* the user wanted it to be printed (because s/he set $self->{Encoding})
#
sub flush_xml_decl
{
my ($self) = @_;
return if $self->{PrintedXmlDecl};
if (defined $self->{Encoding})
{
$self->xml_decl ({ Version => '1.0', Encoding => $self->{Encoding} });
}
# If it wasn't printed just now, it doesn't need to be printed at all,
# so pretend we did print it.
$self->{PrintedXmlDecl} = 1;
}
sub processing_instruction
{
my ($self, $event) = @_;
$self->flush_xml_decl;
my $escape = $self->{Escape}->{ProcessingInstruction};
my $str = "<?" . $event->{Target} . " " .
&$escape ($event->{Data}). "?>";
$self->print ($str);
}
1; # package return code
__END__
=head1 NAME
XML::Handler::Composer - Another XML printer/writer/generator
=head1 SYNOPSIS
use XML::Handler::Composer;
my $composer = new XML::Handler::Composer ( [OPTIONS] );
=head1 DESCRIPTION
XML::Handler::Composer is similar to XML::Writer, XML::Handler::XMLWriter,
XML::Handler::YAWriter etc. in that it generates XML output.
This implementation may not be fast and it may not be the best solution for
your particular problem, but it has some features that may be missing in the
other implementations:
=over 4
=item * Supports every output encoding that L<XML::UM> supports
L<XML::UM> supports every encoding for which there is a mapping file
in the L<XML::Encoding> distribution.
=item * Pretty printing
When used with L<XML::Filter::Reindent>.
=item * Fine control over which kind of quotes are used
See options below.
=item * Supports PerlSAX interface
=back
=head1 Constructor Options
=over 4
=item * EndWithNewline (Default: 1)
Whether to print a newline at the end of the file (i.e. after the root element)
=item * Newline (Default: undef)
If defined, which newline to use for printing.
(Note that XML::Parser etc. convert newlines into "\x0A".)
If undef, newlines will not be converted and XML::Handler::Composer will
use "\x0A" when printing.
A value of "\n" will convert the internal newlines into the platform
specific line separator.
See the PreserveWS option in the characters event (below) for finer control
over when newline conversion is active.
=item * DocTypeIndent (Default: a Newline and 2 spaces)
Newline plus indent that is used to separate lines inside the DTD.
=item * IndentAttList (Default: 8 spaces)
Indent used when printing an <!ATTLIST> declaration that has more than one
attribute definition, e.g.
<!ATTLIST my_elem
attr1 CDATA "foo"
attr2 CDATA "bar"
>
=item * Quote (Default: { XMLDecl => '"', Attr => '"', Entity => '"', SystemLiteral => '"' })
Quote contains a reference to a hash that defines which quoting characters
to use when printing XML declarations (XMLDecl), attribute values (Attr),
<!ENTITY> values (Entity) and system/public literals (SystemLiteral)
as found in <!DOCTYPE>, <!ENTITY> declarations etc.
=item * PrintDefaultAttr (Default: 0)
If 1, prints attribute values regardless of whether they are default
attribute values (as defined in <!ATTLIST> declarations.)
Normally, default attributes are not printed.
=item * Encoding (Default: undef)
Defines the output encoding (if specified.)
Note that future calls to the xml_decl() handler may override this setting
(if they contain an Encoding definition.)
=item * EncodeUnmapped (Default: \&XML::UM::encode_unmapped_dec)
Defines how Unicode characters not found in the mapping file (of the
specified encoding) are printed.
By default, they are converted to decimal entity references, like '{'
Use \&XML::UM::encode_unmapped_hex for hexadecimal constants, like '«'
=item * Print (Default: sub { print @_ }, which prints to stdout)
The subroutine that is used to print the encoded XML output.
The default prints the string to stdout.
=back
=head1 Method: get_compressed_element_suffix ($event)
Override this method to support the different styles for printing
empty elements in compressed notation, e.g. <p/>, <p></p>, <p />, <p>.
The default returns "/>", which results in <p/>.
Use " />" for XHTML style elements or ">" for certain HTML style elements.
The $event parameter is the hash reference that was received from the
start_element() handler.
=head1 Extra PerlSAX event information
XML::Handler::Composer relies on hints from previous SAX filters to
format certain parts of the XML.
These SAX filters (e.g. XML::Filter::Reindent) pass extra information by adding
name/value pairs to the appropriate PerlSAX events (the events themselves are
hash references.)
=over 4
=item * entity_reference: Parameter => 1
If Parameter is 1, it means that it is a parameter entity reference.
A parameter entity is referenced with %ent; instead of &ent; and the
entity declaration starts with <!ENTITY % ent ...> instead of <!ENTITY ent ...>
NOTE: This should be added to the PerlSAX interface!
=item * start_element/end_element: Compress => 1
If Compress is 1 in both the start_element and end_element event, the element
will be printed in compressed form, e.g. <a/> instead of <a></a>.
=item * start_element: PreserveWS => 1
If newline conversion is active (i.e. Newline was defined in the constructor),
then newlines will *NOT* be converted in text (character events) within this
element.
=item * attlist_decl: First, MoreFollow
The First and MoreFollow options can be used to force successive <!ATTLIST>
declarations for the same element to be merged, e.g.
<!ATTLIST my_elem
attr1 CDATA "foo"
attr2 CDATA "bar"
attr3 CDATA "quux"
>
In this example, the attlist_decl event for foo should contain
(First => 1, MoreFollow => 1) and the event for bar should contain
(MoreFollow => 1). The quux event should have no extra info.
'First' indicates that the event is the first of a sequence.
'MoreFollow' indicates that more events will follow in this sequence.
If neither option is set by the preceding PerlSAX filter, each attribute
definition will be printed as a separate <!ATTLIST> line.
=back
=head1 CAVEATS
This code is highly experimental!
It has not been tested well and the API may change.
=head1 AUTHOR
Enno Derksen is the original author.
Send bug reports, hints, tips, suggestions to T.J. Mather at
<F<tjmather@tjmather.com>>.
=cut
|