line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Any::Renderer::XML; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
# $Id: XML.pm,v 1.8 2006/08/22 20:14:09 johna Exp $ |
4
|
|
|
|
|
|
|
|
5
|
1
|
|
|
1
|
|
6
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
34
|
|
6
|
1
|
|
|
1
|
|
4
|
use vars qw($VERSION); |
|
1
|
|
|
|
|
119
|
|
|
1
|
|
|
|
|
41
|
|
7
|
1
|
|
|
1
|
|
496
|
use XML::Simple; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
$VERSION = sprintf"%d.%03d", q$Revision: 1.8 $ =~ /: (\d+)\.(\d+)/; |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
use constant FORMAT_NAME => "XML"; |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
sub new |
14
|
|
|
|
|
|
|
{ |
15
|
|
|
|
|
|
|
my ( $class, $format, $options ) = @_; |
16
|
|
|
|
|
|
|
die("Invalid format $format") unless($format eq FORMAT_NAME); |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
my $self = { |
19
|
|
|
|
|
|
|
'options' => $options, |
20
|
|
|
|
|
|
|
}; |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
bless $self, $class; |
23
|
|
|
|
|
|
|
return $self; |
24
|
|
|
|
|
|
|
} |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
sub render |
27
|
|
|
|
|
|
|
{ |
28
|
|
|
|
|
|
|
my ( $self, $data ) = @_; |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
TRACE ( "Rendering XML data" ); |
31
|
|
|
|
|
|
|
DUMP ( $data ); |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
my $charset = $self->{options}{Encoding} || 'ISO-8859-1'; |
34
|
|
|
|
|
|
|
my %xmlopts = ( |
35
|
|
|
|
|
|
|
'noattr' => 1, |
36
|
|
|
|
|
|
|
'keyattr' => undef, |
37
|
|
|
|
|
|
|
'keeproot' => 1, |
38
|
|
|
|
|
|
|
'rootname' => 'output', |
39
|
|
|
|
|
|
|
'xmldecl' => qq{}, |
40
|
|
|
|
|
|
|
'contentkey' => undef, |
41
|
|
|
|
|
|
|
'noescape' => 0, |
42
|
|
|
|
|
|
|
); |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
while ( my ( $k, $v ) = each %{ $self->{ 'options' }->{ 'XmlOptions' } } ) |
45
|
|
|
|
|
|
|
{ |
46
|
|
|
|
|
|
|
# smash case to ensure the options override defaults |
47
|
|
|
|
|
|
|
$xmlopts { lc $k } = $v; |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
if (my $varname = $self->{ 'options' }{ 'VariableName' }) { |
51
|
|
|
|
|
|
|
# VariableName overrides the 'options' hash |
52
|
|
|
|
|
|
|
$xmlopts { 'rootname' } = $varname; |
53
|
|
|
|
|
|
|
} |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
my $out = ''; |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
{ |
58
|
|
|
|
|
|
|
# XML::Simple 1.23 produces use-of-uninitialized... warnings |
59
|
|
|
|
|
|
|
local $^W = 0; |
60
|
|
|
|
|
|
|
$out = XML::Simple::XMLout ( $data, %xmlopts ); |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
return $out; |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
sub requires_template |
67
|
|
|
|
|
|
|
{ |
68
|
|
|
|
|
|
|
my ( $format ) = @_; |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
return 0; |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
sub available_formats |
74
|
|
|
|
|
|
|
{ |
75
|
|
|
|
|
|
|
return [ FORMAT_NAME ]; |
76
|
|
|
|
|
|
|
} |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
sub TRACE {} |
79
|
|
|
|
|
|
|
sub DUMP {} |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
1; |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
=head1 NAME |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
Any::Renderer::XML - render a data structure as element-only XML |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
=head1 SYNOPSIS |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
use Any::Renderer; |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
my %xml_options = (); |
92
|
|
|
|
|
|
|
my %options = ( 'XmlOptions' => \%xml_options ); |
93
|
|
|
|
|
|
|
my $format = "XML"; |
94
|
|
|
|
|
|
|
my $r = new Any::Renderer ( $format, \%options ); |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
my $data_structure = [...]; # arbitrary structure code |
97
|
|
|
|
|
|
|
my $string = $r->render ( $data_structure ); |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
You can get a list of all formats that this module handles using the following syntax: |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
my $list_ref = Any::Renderer::XML::available_formats (); |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
Also, determine whether or not a format requires a template with requires_template: |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
my $bool = Any::Renderer::XML::requires_template ( $format ); |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
=head1 DESCRIPTION |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
Any::Renderer::XML renders any Perl data structure passed to it as element-only XML. For example: |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
perl -MAny::Renderer -e "print Any::Renderer->new('XML')->render({a => 1, b => [2,3]})" |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
results in: |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
1 |
118
|
|
|
|
|
|
|
2 |
119
|
|
|
|
|
|
|
3 |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
The rendering process comes with all the caveats cited in the XML::Simple documentation. For example if your data structure contains binary data or ASCII control characters, then the XML document that is generated may not be well-formed. |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
=head1 FORMATS |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
=over 4 |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
=item XML |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
=back |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
=head1 METHODS |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
=over 4 |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
=item $r = new Any::Renderer::XML($format,\%options) |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
C<$format> must be C. |
139
|
|
|
|
|
|
|
See L for a description of available options. |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
=item $scalar = $r->render($data_structure) |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
The main method. |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
=item $bool = Any::Renderer::XML::requires_template($format) |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
False in this case. |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
=item $list_ref = Any::Renderer::XML::available_formats() |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
Just the one - C. |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
=back |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
=head1 OPTIONS |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
=over 4 |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
=item XmlOptions |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
A hash reference of options that can be passed to XML::Simple::XMLout, see |
162
|
|
|
|
|
|
|
L for a detailed description of all of the available |
163
|
|
|
|
|
|
|
options. |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
=item VariableName |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
Set the XML root element name. You can also achieve this by setting the |
168
|
|
|
|
|
|
|
C or C options passed to XML::Simple in the C options |
169
|
|
|
|
|
|
|
hash. This is a shortcut to make this renderer behave like some of the other |
170
|
|
|
|
|
|
|
renderer backends. |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
=item Encoding |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
Character set of the generated XML document. Defaults to ISO-8859-1. |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
=back |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
=head1 SEE ALSO |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
L, L |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
=head1 VERSION |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
$Revision: 1.8 $ on $Date: 2006/08/22 20:14:09 $ by $Author: johna $ |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
=head1 AUTHOR |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
Matt Wilson |
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
=head1 COPYRIGHT |
191
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
(c) BBC 2006. This program is free software; you can redistribute it and/or modify it under the GNU GPL. |
193
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
See the file COPYING in this distribution, or http://www.gnu.org/licenses/gpl.txt |
195
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
=cut |