Text-vFile-toXML
view release on metacpan or search on metacpan
lib/Text/vFile/toXML.pm view on Meta::CPAN
=head1 METHODS
=head2 new
Creates a new Text::vFile::toXML object; takes a list of key-value pairs for initialization, which must contain exactly one of the following:
filehandle => (filehandle object)
filename => (string)
data => (Text::vFile::asData struct)
=cut
sub new {
my ($type, %args) = @_;
croak "Must provide exactly one of (filehandle, filename, or data)"
unless +@{[ grep defined, @args{qw(filehandle filename data)} ]} == 1;
$args{_data} = delete $args{data}
|| Text::vFile::asData->new->parse(
$args{filehandle} || ($args{filename} &&
do { open my $fh, $args{filename} or die; $fh }
));
bless \%args, $type
}
=head2 to_xml
Wraps the convert() function; returns an XML string. Can be called as an
instance method (OO-style) or as a function (functional style), in which case
it takes a Text::vFile::asData-compatible data structure as its only parameter.
=cut
sub to_xml {
xml({ convert($_[0]->{_data}{objects} || $_[0]->{objects}) },
{ root => 'iCalendar', attrs => { 'xmlns:xCal' => $xCalNS } })
}
=head2 convert
Recursively converts Text::vFile::asData structures to XML::Quick-compatible
ones.
=cut
sub convert {
my ($data) = @_;
my %result;
for my $object (@$data) {
my ($props, $objects, $type) = @$object{qw(properties objects type)};
push @{ $result{lc $type} }, +{
convert($objects),
map {
my $propname = $_;
lc $propname => [ map { my ($p, $v) = @$_{qw(param value)}; +{
_cdata => $v,
_attrs => +{ map { $attrs{lc $_} || lc $_ => $p->{$_} } keys %$p } }
} @{ $props->{$propname} } ]
} keys %$props
};
}
return %result;
}
=head1 AUTHOR
Darren Kulp, C<< <kulp at cpan.org> >>
=head1 BUGS
Probably. Email me at the address above with bug reports.
=head1 ACKNOWLEDGEMENTS
The L<Text::vFile::asData> and L<XML::Quick> modules proved very useful, to the
point of nearly trivializing this module.
=head1 COPYRIGHT & LICENSE
Copyright 2006-2017 Darren Kulp.
This program is released under the terms of the BSD license.
=cut
1; # End of Text::vFile::toXML
__END__
# vi: set ts=4 sw=4 et ai: #
( run in 1.012 second using v1.01-cache-2.11-cpan-524268b4103 )