/usr/share/perl5/XML/Struct/Writer.pm is in libxml-struct-perl 0.23-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 | package XML::Struct::Writer;
#ABSTRACT: Write XML data structures to XML streams
our $VERSION = '0.23'; #VERSION
use strict;
use Moo;
use XML::LibXML::SAX::Builder;
use XML::Struct::Writer::Stream;
use Scalar::Util qw(blessed reftype);
use Carp;
has attributes => (is => 'rw', default => sub { 1 });
has encoding => (is => 'rw', default => sub { 'UTF-8' });
has version => (is => 'rw', default => sub { '1.0' });
has standalone => (is => 'rw');
has pretty => (is => 'rw', default => sub { 0 }); # 0|1|2
has xmldecl => (is => 'rw', default => sub { 1 });
has root => (is => 'rw', default => sub { 'root' });
has to => (
is => 'rw',
coerce => sub {
return IO::File->new($_[0], "w") unless ref $_[0];
if (reftype($_[0]) eq 'SCALAR') {
open my $io,">:utf8",$_[0];
return $io;
} else { # IO::Handle, GLOB, ...
return $_[0];
}
}
);
has handler => (
is => 'rw',
lazy => 1,
builder => sub {
$_[0]->to ? XML::Struct::Writer::Stream->new(
fh => $_[0]->to,
encoding => $_[0]->encoding,
version => $_[0]->version,
pretty => $_[0]->pretty,
) : XML::LibXML::SAX::Builder->new( handler => $_[0] );
}
);
sub write {
my $self = shift;
$self->writeStart;
$self->writeElement($self->rootElement(@_));
$self->writeEnd;
$self->handler->can('result') ? $self->handler->result : 1;
}
*writeDocument = \&write;
sub rootElement {
my ($self, $root, $name) = @_;
if (my $type = reftype($root)) {
if ($type eq 'ARRAY') {
return $root;
} elsif ($type eq 'HASH') {
return [
$name || $self->root,
simpleChildElements($root)
]
}
}
croak "expected ARRAY or HASH as root element";
}
# convert simple format to MicroXML
sub simpleChildElements {
my ($simple) = @_;
[
map {
my ($tag, $content) = ($_, $simple->{$_});
if (!defined $content) {
();
} elsif (!ref($content)) {
[ $tag, [$content] ]
} elsif (reftype($content) eq 'ARRAY') {
@$content
? map { [ $tag, [$_] ] } @$content
: [ $tag ];
} elsif (reftype($content) eq 'HASH') {
[ $tag, {}, $content ];
} else {
();
}
} sort keys %$simple
]
}
# return child elements as array reference
sub elementChildren {
my ($self, $element) = @_;
my $children = $element->[
!$self->attributes or (reftype($element->[1]) // '') eq 'ARRAY'
? 1 : 2
] // [ ];
my $type = reftype($children);
if (!$type) {
[ $children ] # simple character content
} elsif( $type eq 'ARRAY' ) {
[
map {
(reftype($_) // '') eq 'HASH' ? @{ simpleChildElements($_) } : $_
} @$children
]
} elsif( $type eq 'HASH' ) {
simpleChildElements($children)
} else {
croak "expected ARRAY or HASH as child elements";
}
}
sub writeElement {
my $self = shift;
foreach my $element (@_) {
$self->writeStartElement($element);
my $children = $self->elementChildren($element);
foreach my $child ( @$children ) {
if (ref $child) {
$self->writeElement($child);
} else {
$self->writeCharacters($child);
}
}
$self->writeEndElement($element);
}
}
sub writeStartElement {
my ($self, $element) = @_;
my $args = { Name => $element->[0] };
if ($self->attributes) {
my $type = reftype($element->[1]);
if (defined $type and $type eq 'HASH') {
$args->{Attributes} = $element->[1];
}
}
$self->handler->start_element($args);
}
sub writeEndElement {
my ($self, $element) = @_;
$self->handler->end_element( { Name => $element->[0] } );
}
sub writeCharacters {
$_[0]->handler->characters({ Data => $_[1] });
}
sub writeStart {
my $self = shift;
$self->handler->start_document;
if ($self->handler->can('xml_decl') && $self->xmldecl) {
$self->handler->xml_decl({
Version => $self->version,
Encoding => $self->encoding,
Standalone => $self->standalone,
});
}
$self->writeStartElement( $self->rootElement(@_) ) if @_;
}
sub writeEnd {
my $self = shift;
$self->writeEndElement($_[0]) if @_;
$self->handler->end_document;
}
1;
__END__
=pod
=encoding UTF-8
=head1 NAME
XML::Struct::Writer - Write XML data structures to XML streams
=head1 VERSION
version 0.23
=head1 SYNOPSIS
use XML::Struct::Writer;
# create DOM
my $xml = XML::Struct::Writer->new->write( [
greet => { }, [
"Hello, ",
[ emph => { color => "blue" } , [ "World" ] ],
"!"
]
] );
$xml->toFile("greet.xml");
# <?xml version="1.0" encoding="UTF-8"?>
# <greet>Hello, <emph color="blue">World</emph>!</greet>
# serialize
XML::Struct::Writer->new(
attributes => 0,
pretty => 1,
to => \*STDOUT,
)->write( [
doc => [
[ name => [ "alice" ] ],
[ name => [ "bob" ] ],
]
] );
# <?xml version="1.0" encoding="UTF-8"?>
# <doc>
# <name>alice</name>
# <name>bob</name>
# </doc>
=head1 DESCRIPTION
This module writes an XML document, given as L<XML::Struct> data structure.
L<XML::Struct::Writer> can act as SAX event generator that sequentially sends
L</"SAX EVENTS"> to a SAX handler. The default handler
L<XML::LibXML::SAX::Builder> creates L<XML::LibXML::Document> that can be used
to serialize the XML document as string.
=head1 METHODS
=head2 write( $root [, $name ] ) == writeDocument( $root [, $name ] )
Write an XML document, given in form of its root element as array reference
(MicroXML) or in simple format as hash reference with an optional root element
name (simpleXML). The handler's C<result> method, if implemented, is used to
get a return value.
For most applications this is the only method one needs to care about. If the
XML document to be written is not fully given as root element, one has to
directly call the following methods. This method is basically equivalent to:
$writer->writeStart;
$writer->writeElement($root);
$writer->writeEnd;
$writer->result if $writer->can('result');
=head2 writeStart( [ $root [, $name ] ] )
Call the handler's C<start_document> and C<xml_decl> methods. An optional root
element can be passed, so C<< $writer->writeStart($root) >> is equivalent to:
$writer->writeStart;
$writer->writeStartElement($root);
=head2 writeElement( $element [, @more_elements ] )
Write one or more XML elements, including their child elements, to the handler.
=head2 writeStartElement( $element )
Directly call the handler's C<start_element> method.
=head2 writeEndElement( $element )
Directly call the handler's C<end_element> method.
=head2 writeCharacters( $string )
Directy call the handler's C<characters> method.
=head2 writeEnd( [ $root ] )
Directly call the handler's C<end_document> method. An optional root element
can be passed, so C<< $writer->writeEnd($root) >> is equivalent to:
$writer->writeEndElement($root);
$writer->writeEnd;
=head1 CONFIGURATION
=over
=item attributes
Set to true by default to expect attribute hashes in the L<XML::Struct> input
format. If set to false, XML elements must be passed as
[ $name => \@children ]
instead of
[ $name => \%attributes, \@children ]
Do B<not> set this to false when serializing simple xml in form of hashes!
=item encoding
Sets the encoding for handlers that support an explicit encoding. Set to UTF-8
by default.
=item version
Sets the XML version (C<1.0> by default).
=item xmldecl
Include XML declaration on serialization. Enabled by default.
=item standalone
Add standalone flag in the XML declaration.
=item to
Filename, L<IO::Handle>, or other kind of stream to serialize XML to.
=item handler
Specify a SAX handler that L</"SAX EVENTS"> are send to. Automatically set to
an instance of L<XML::Struct::Writer::Stream> if option C<to> has been
specified or to an instance of L<XML::LibXML::SAX::Builder> otherwise.
=back
=head1 SAX EVENTS
A SAX handler, as used by this module, is expected to implement the following
methods (two of them are optional):
=over
=item xml_decl( { Version => $version, Encoding => $encoding } )
Optionally called once at the start of an XML document, if the handler supports
this method.
=item start_document()
Called once at the start of an XML document.
=item start_element( { Name => $name, Attributes => \%attributes } )
Called at the start of an XML element to emit an XML start tag.
=item end_element( { Name => $name } )
Called at the end of an XML element to emit an XML end tag.
=item characters( { Data => $characters } )
Called for character data. Character entities and CDATA section are expanded to
strings.
=item end_document()
Called once at the end of an XML document.
=item result()
Optionally called at the end of C<write>/C<writeDocument> to return a value
from this methods. Handlers do not need to implement this method.
=back
=head1 SEE ALSO
Using a streaming SAX handler, such as L<XML::SAX::Writer>,
L<XML::Genx::SAXWriter>, L<XML::Handler::YAWriter>, and possibly L<XML::Writer>
should be more performant for serialization. Examples of other modules that
receive SAX events include L<XML::STX>, L<XML::SAX::SimpleDispatcher>, and
L<XML::SAX::Machines>,
=head1 AUTHOR
Jakob Voß
=head1 COPYRIGHT AND LICENSE
This software is copyright (c) 2014 by Jakob Voß.
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
|