line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
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 takes perl data structures, |
20
|
|
|
|
|
|
|
serializes them (see L) 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
|
|
|
2
|
|
25118
|
use strict; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
82
|
|
29
|
2
|
|
|
2
|
|
10
|
use warnings; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
56
|
|
30
|
|
|
|
|
|
|
|
31
|
2
|
|
|
2
|
|
13
|
use base qw/Data::Plist::Writer/; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
1599
|
|
32
|
2
|
|
|
2
|
|
2073
|
use XML::Writer; |
|
2
|
|
|
|
|
38897
|
|
|
2
|
|
|
|
|
90
|
|
33
|
2
|
|
|
2
|
|
2241
|
use MIME::Base64 qw//; |
|
2
|
|
|
|
|
1641
|
|
|
2
|
|
|
|
|
1249
|
|
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
=head2 write_fh $fh, $data |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
Takes a perl data structure C<$data>, serializes it (see |
38
|
|
|
|
|
|
|
L) and passes it to |
39
|
|
|
|
|
|
|
L 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
|
|
|
|
|
|
|
sub write_fh { |
46
|
9
|
|
|
9
|
1
|
14
|
my $self = shift; |
47
|
9
|
50
|
|
|
|
25
|
$self = $self->new() unless ref $self; |
48
|
|
|
|
|
|
|
|
49
|
9
|
|
|
|
|
14
|
my ( $fh, $object ) = @_; |
50
|
9
|
|
|
|
|
54
|
local $self->{x} |
51
|
|
|
|
|
|
|
= XML::Writer->new( OUTPUT => $fh, DATA_MODE => 1, DATA_INDENT => 8 ); |
52
|
9
|
|
|
|
|
1312
|
$self->{x}->xmlDecl("UTF-8"); |
53
|
9
|
|
|
|
|
340
|
$self->{x}->doctype( |
54
|
|
|
|
|
|
|
"plist", |
55
|
|
|
|
|
|
|
"-//Apple//DTD PLIST 1.0//EN", |
56
|
|
|
|
|
|
|
"http://www.apple.com/DTDs/PropertyList-1.0.dtd" |
57
|
|
|
|
|
|
|
); |
58
|
9
|
|
|
|
|
287
|
$self->{x}->startTag( plist => version => "1.0" ); |
59
|
9
|
100
|
|
|
|
708
|
$object = $self->serialize($object) if ( $self->{serialize} ); |
60
|
9
|
|
|
|
|
24
|
$self->xml_write($object); |
61
|
9
|
|
|
|
|
424
|
$self->{x}->endTag("plist"); |
62
|
9
|
|
|
|
|
276
|
$self->{x}->end(); |
63
|
|
|
|
|
|
|
|
64
|
9
|
|
|
|
|
920
|
return 1; |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
=head2 xml_write |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
Takes serialized perl structures (see |
70
|
|
|
|
|
|
|
L) and recursively checks tags |
71
|
|
|
|
|
|
|
and writes the data to the filehandle. |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
=cut |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
sub xml_write { |
76
|
13
|
|
|
13
|
1
|
191
|
my $self = shift; |
77
|
13
|
|
|
|
|
16
|
my $data = shift; |
78
|
|
|
|
|
|
|
|
79
|
13
|
100
|
|
|
|
91
|
if ( $data->[0] =~ /^(true|false|fill|null)$/ ) { |
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
80
|
4
|
|
|
|
|
15
|
$self->{x}->emptyTag( $data->[0] ); |
81
|
|
|
|
|
|
|
} elsif ( $data->[0] =~ /^(integer|real|date|string|ustring)$/ ) { |
82
|
4
|
|
|
|
|
13
|
$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
|
|
|
|
|
5
|
$self->{x}->startTag("dict"); |
88
|
1
|
|
|
|
|
45
|
$self->{x}->dataElement( "key", 'CF$UID' ); |
89
|
1
|
|
|
|
|
83
|
$self->{x}->dataElement( "integer", $data->[1] ); |
90
|
1
|
|
|
|
|
84
|
$self->{x}->endTag("dict"); |
91
|
|
|
|
|
|
|
} elsif ( $data->[0] eq "data" ) { |
92
|
1
|
|
|
|
|
19
|
$self->{x}->dataElement( "data", |
93
|
|
|
|
|
|
|
MIME::Base64::encode_base64( $data->[1] ) ); |
94
|
|
|
|
|
|
|
} elsif ( $data->[0] eq "dict" ) { |
95
|
1
|
|
|
|
|
4
|
$self->{x}->startTag("dict"); |
96
|
1
|
|
|
|
|
46
|
for my $k ( sort keys %{ $data->[1] } ) { |
|
1
|
|
|
|
|
5
|
|
97
|
1
|
|
|
|
|
12
|
$self->{x}->dataElement( "key", $k ); |
98
|
1
|
|
|
|
|
96
|
$self->xml_write( $data->[1]{$k} ); |
99
|
|
|
|
|
|
|
} |
100
|
1
|
|
|
|
|
90
|
$self->{x}->endTag("dict"); |
101
|
|
|
|
|
|
|
} elsif ( $data->[0] eq "array" ) { |
102
|
1
|
|
|
|
|
4
|
$self->{x}->startTag("array"); |
103
|
1
|
|
|
|
|
49
|
$self->xml_write($_) for @{ $data->[1] }; |
|
1
|
|
|
|
|
6
|
|
104
|
1
|
|
|
|
|
90
|
$self->{x}->endTag("array"); |
105
|
|
|
|
|
|
|
} else { |
106
|
1
|
|
|
|
|
7
|
$self->{x}->comment( $data->[0] ); |
107
|
|
|
|
|
|
|
} |
108
|
|
|
|
|
|
|
} |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
1; |