| 1 | =head1 NAME | ||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||
| 3 | Data::Plist::XMLWriter - writes XML property lists from | ||||||||||||||||||||||||||||||
| 4 | perl data structures. | ||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||
| 6 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||
| 8 | # Create new | ||||||||||||||||||||||||||||||
| 9 | my $write = Data::Plist::XMLWriter->new; | ||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||
| 11 | # Writing to a string | ||||||||||||||||||||||||||||||
| 12 | my $str = $write->write($data); | ||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||
| 14 | # Writing to a file C<$filename> | ||||||||||||||||||||||||||||||
| 15 | $write->write($filename, $data); | ||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||
| 17 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||
| 19 | C<Data::Plist::XMLWriter> takes perl data structures, | ||||||||||||||||||||||||||||||
| 20 | serializes them (see L<Data::Plist/SERIALIZED DATA>) and | ||||||||||||||||||||||||||||||
| 21 | recursively writes to a given filehandle in Apple's XML | ||||||||||||||||||||||||||||||
| 22 | property list format. | ||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||
| 24 | =cut | ||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||
| 26 | package Data::Plist::XMLWriter; | ||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||
| 28 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 29 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||
| 31 | 2 | ▶ | use base qw/Data::Plist::Writer/; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 32 | 2 | ▶ | use XML::Writer; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 33 | 2 | ▶ | use MIME::Base64 qw//; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||
| 35 | =head2 write_fh $fh, $data | ||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||
| 37 | Takes a perl data structure C<$data>, serializes it (see | ||||||||||||||||||||||||||||||
| 38 | L<Data::Plist/SERIALIZED DATA>) and passes it to | ||||||||||||||||||||||||||||||
| 39 | L</xml_write> to be written to the filehandle C<$fh>. Also | ||||||||||||||||||||||||||||||
| 40 | writes the headers and footers for the XML document. Returns | ||||||||||||||||||||||||||||||
| 41 | 1 to indicate success. | ||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||
| 43 | =cut | ||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||
| 45 | 9 | ▶ | sub write_fh { | ||||||||||||||||||||||||||||
|
Subroutine
write_fh: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||
| 46 | 9 | my $self = shift; | |||||||||||||||||||||||||||||
| 47 | 9 | ▶ | $self = $self->new() unless ref $self; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||
| 49 | 9 | my ( $fh, $object ) = @_; | |||||||||||||||||||||||||||||
| 50 | local $self->{x} | ||||||||||||||||||||||||||||||
| 51 | 9 | = XML::Writer->new( OUTPUT => $fh, DATA_MODE => 1, DATA_INDENT => 8 ); | |||||||||||||||||||||||||||||
| 52 | 9 | $self->{x}->xmlDecl("UTF-8"); | |||||||||||||||||||||||||||||
| 53 | $self->{x}->doctype( | ||||||||||||||||||||||||||||||
| 54 | 9 | "plist", | |||||||||||||||||||||||||||||
| 55 | "-//Apple//DTD PLIST 1.0//EN", | ||||||||||||||||||||||||||||||
| 56 | "http://www.apple.com/DTDs/PropertyList-1.0.dtd" | ||||||||||||||||||||||||||||||
| 57 | ); | ||||||||||||||||||||||||||||||
| 58 | 9 | $self->{x}->startTag( plist => version => "1.0" ); | |||||||||||||||||||||||||||||
| 59 | 9 | ▶ | $object = $self->serialize($object) if ( $self->{serialize} ); | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 60 | 9 | $self->xml_write($object); | |||||||||||||||||||||||||||||
| 61 | 9 | $self->{x}->endTag("plist"); | |||||||||||||||||||||||||||||
| 62 | 9 | $self->{x}->end(); | |||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||
| 64 | 9 | return 1; | |||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||
| 67 | =head2 xml_write | ||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||
| 69 | Takes serialized perl structures (see | ||||||||||||||||||||||||||||||
| 70 | L<Data::Plist/SERIALIZED DATA>) and recursively checks tags | ||||||||||||||||||||||||||||||
| 71 | and writes the data to the filehandle. | ||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||
| 73 | =cut | ||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||
| 75 | 13 | ▶ | sub xml_write { | ||||||||||||||||||||||||||||
|
Subroutine
xml_write: called CC=9 SCAR=22.0
Pod
documented
| |||||||||||||||||||||||||||||||
| 76 | 13 | my $self = shift; | |||||||||||||||||||||||||||||
| 77 | 13 | my $data = shift; | |||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||
| 79 | 13 | ▶ | if ( $data->[0] =~ /^(true|false|fill|null)$/ ) { | ||||||||||||||||||||||||||||
|
Branch: 6 branches
| |||||||||||||||||||||||||||||||
| 80 | 4 | $self->{x}->emptyTag( $data->[0] ); | |||||||||||||||||||||||||||||
| 81 | } elsif ( $data->[0] =~ /^(integer|real|date|string|ustring)$/ ) { | ||||||||||||||||||||||||||||||
| 82 | 4 | $self->{x}->dataElement( $data->[0], $data->[1] ); | |||||||||||||||||||||||||||||
| 83 | } elsif ( $data->[0] eq "UID" ) { | ||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||
| 85 | # UIDs are only hackishly supported in the XML version. | ||||||||||||||||||||||||||||||
| 86 | # Apple's plutil converts them as follows: | ||||||||||||||||||||||||||||||
| 87 | 1 | $self->{x}->startTag("dict"); | |||||||||||||||||||||||||||||
| 88 | 1 | $self->{x}->dataElement( "key", 'CF$UID' ); | |||||||||||||||||||||||||||||
| 89 | 1 | $self->{x}->dataElement( "integer", $data->[1] ); | |||||||||||||||||||||||||||||
| 90 | 1 | $self->{x}->endTag("dict"); | |||||||||||||||||||||||||||||
| 91 | } elsif ( $data->[0] eq "data" ) { | ||||||||||||||||||||||||||||||
| 92 | 1 | $self->{x}->dataElement( "data", | |||||||||||||||||||||||||||||
| 93 | MIME::Base64::encode_base64( $data->[1] ) ); | ||||||||||||||||||||||||||||||
| 94 | } elsif ( $data->[0] eq "dict" ) { | ||||||||||||||||||||||||||||||
| 95 | 1 | $self->{x}->startTag("dict"); | |||||||||||||||||||||||||||||
| 96 | 1 | for my $k ( sort keys %{ $data->[1] } ) { | |||||||||||||||||||||||||||||
| 97 | 1 | $self->{x}->dataElement( "key", $k ); | |||||||||||||||||||||||||||||
| 98 | 1 | $self->xml_write( $data->[1]{$k} ); | |||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||||
| 100 | 1 | $self->{x}->endTag("dict"); | |||||||||||||||||||||||||||||
| 101 | } elsif ( $data->[0] eq "array" ) { | ||||||||||||||||||||||||||||||
| 102 | 1 | $self->{x}->startTag("array"); | |||||||||||||||||||||||||||||
| 103 | 1 | $self->xml_write($_) for @{ $data->[1] }; | |||||||||||||||||||||||||||||
| 104 | 1 | $self->{x}->endTag("array"); | |||||||||||||||||||||||||||||
| 105 | } else { | ||||||||||||||||||||||||||||||
| 106 | 1 | $self->{x}->comment( $data->[0] ); | |||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||
| 110 | 2 | 1; | |||||||||||||||||||||||||||||