Skip to content

Commit

Permalink
new option xmldecl to xml_write
Browse files Browse the repository at this point in the history
  • Loading branch information
nichtich committed Jun 23, 2014
1 parent 8bb3b4a commit 7aa46ad
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 18 deletions.
4 changes: 2 additions & 2 deletions cpanfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
requires 'Catmandu', '0.9';
requires 'XML::Struct', '0.18';
requires 'XML::LibXSLT', '1.90';
requires 'XML::Struct', '0.21';
requires 'XML::LibXSLT', '1.78';
2 changes: 1 addition & 1 deletion dist.ini
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = Catmandu-XML
license = Perl_5
version = 0.06
version = 0.07
copyright_year = 2014
author = Jakob Voß
copyright_holder = Jakob Voß
Expand Down
33 changes: 21 additions & 12 deletions lib/Catmandu/Fix/xml_write.pm
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,17 @@ with 'Catmandu::Fix::Base';
has field => (is => 'ro', required => 1);
has attributes => (is => 'ro');
has pretty => (is => 'ro');
# has encoding => (is => 'ro');
# has version => (is => 'ro');
# has standalone => (is => 'ro');
# has xmlDecl => (is => 'ro');
has encoding => (is => 'ro', default => sub { 'UTF-8' });
has version => (is => 'ro');
has standalone => (is => 'ro');
has xmldecl => (is => 'ro', default => sub { 1 });

around BUILDARGS => sub {
my ($orig,$class,$field,%opts) = @_;
$orig->($class,
field => $field,
attributes => $opts{attributes},
# encoding => $opts{encoding},
pretty => $opts{pretty},
map { $_ => $opts{$_} } grep { defined $opts{$_} }
qw(attributes pretty encoding version standalone xmldecl)
);
};

Expand All @@ -33,7 +32,7 @@ has _writer => (
builder => sub {
XML::Struct::Writer->new(
map { $_ => $_[0]->$_ } grep { defined $_[0]->$_ }
qw(attributes pretty)
qw(attributes pretty encoding version standalone xmldecl)
);
}
);
Expand All @@ -51,8 +50,10 @@ sub emit {
my $var = $_[0];
$fixer->emit_get_key($var,$key,sub{
my $var = $_[0];
return "${var} = ${writer}->write($var) if ref(${var}) !~ 'XML::LibXML::Document=';" .
"${var} = ${var}->serialize(${pretty});";
return "${var} = (ref(${var}) =~ 'XML::LibXML::Document=')" .
"? ${var}->serialize(${pretty}) : do {".
"my \$s=''; ${writer}->to(\\\$s); ${writer}->write(${var}); \$s" .
"}";
});
});
}
Expand All @@ -67,9 +68,9 @@ sub emit {
This L<Catmandu::Fix> serializes XML documents (given in MicroXML form
as used by L<XML::Struct> or as instance of L<XML::LibXML::Document>).
In short, this is a wrapper around L<XML::Struct::Reader>.
In short, this is a wrapper around L<XML::Struct::Writer>.
=head1 OPTIONS
=head1 CONFIGURATION
=over
Expand All @@ -81,6 +82,14 @@ Set to false to not expect attribute hashes in the XML structure.
Pretty-print XML if set to C<1>.
=item xmldecl
=item version
=item encoding
=item standalone
=back
=head1 SEE ALSO
Expand Down
5 changes: 2 additions & 3 deletions t/xml_write.t
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@ is_deeply $data->{xml},
'xml_write';

$data = { xml => [ foo => [ [ bar => ['doz'] ] ] ] };
serialize($data,'xml', attributes => 0, pretty => 1);
is_deeply $data->{xml}, <<XML, 'xml_write(attributes:0, pretty:1)';
<?xml version="1.0" encoding="UTF-8"?>
serialize($data,'xml', attributes => 0, pretty => 1, xmldecl => 0);
is_deeply $data->{xml}, <<XML, 'xml_write(attributes:0, pretty:1, xmldecl:0)';
<foo>
<bar>doz</bar>
</foo>
Expand Down

0 comments on commit 7aa46ad

Please sign in to comment.