blib/lib/CGI/Form2XML.pm

filter: Statementstmt 62.1% 69 / 111 Branchbran 25.0% 10 / 40 Conditioncond 18.4% 7 / 38 MC/DCmcdc 0.0% 0 / 20 Subroutinesub 73.3% 11 / 15 Podpod 100% 8 / 8 totaltotal 45.2% 105 / 232 CC 39 SCAR 40.8
CC
39
Cov
46%
asXML
128.9
_xml_utf8_encode
30.0
destination
12.0
CRAP 59.1
↑ Summary
1 package CGI::Form2XML;
2  
3 =head1 NAME
4  
5 CGI::Form2XML - Render CGI form input as XML
6  
7 =head1 SYNOPSIS
8  
9  
10 use CGI::Form2XML;
11  
12  
13 my $x = CGI::Form2XML->new();
14  
15 $x->ns_prefix("nfd");
16  
17 print $x->asXML();
18  
19  
20 =head1 DESCRIPTION
21  
22 This module provides a method of taking CGI form input and turning it into
23 XML for further processing by another application or storage. Unlike
24 modules such CGI::XML and CGI::XMLForm it produces XML to a fixed schema
25 whose structure is not influenced by the form input. If flexibility as to
26 the structure of the XML data is required you will probably want to consider
27 one of the other modules.
28  
29 The schema is included in the distribution of this module as "xmlform.xsd".
30  
31 The module inherits from the CGI module in order to get access to the CGI
32 parameters, so any of the methods of that module can be used.
33  
34 =head2 METHODS
35  
36 =over 4
37  
38 =cut
39  
40 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
42  
43 2 ▶use CGI;
Subroutine BEGIN: called CC=1 SCAR=0.0
44 2 ▶use POSIX qw(strftime);
Subroutine BEGIN: called CC=1 SCAR=0.0
45  
46 2 ▶use base 'CGI';
Subroutine BEGIN: called CC=1 SCAR=0.0
47  
48 2 our $VERSION = '1.6';
49  
50 =item new
51  
52 The constructor for the class. Returns a blessed object of type CGI::Form2XML.
53 Any arguments provided will be passed to the constructor of CGI.
54  
55 =cut
56  
57 sub new
58 1 ▶{
Subroutine new: called CC=2 SCAR=7.2
Pod documented
59 1 my ( $proto, @args) = @_;
60  
61 1 ▶ my $class = ref($proto) || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
010
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
62  
63  
64 1 my $self = $class->SUPER::new(@args);
65  
66 1 bless $self, $class;
67  
68  
69 1 return $self;
70 }
71  
72  
73 =item asXML
74  
75 Returns the XML document that represents this CGI request.
76 It takes a hashref of arguments whose keys are :
77  
78 =over 2
79  
80 =item ns_prefix
81  
82 The namespace prefix that should be used for this document. The default
83 is no namespace.
84  
85 =item ns_url
86  
87 The URL that describes this namespace - the default is
88 'http://schemas.gellyfish.com/FormData', there is currently nothing at this
89 URL.
90  
91 =item omit_info
92  
93 If this is set to a true value then the 'header' information will not be
94 emitted by asXML().
95  
96 =back
97  
98 =cut
99  
100 sub asXML
101 1 ▶{
Subroutine asXML: called CC=22 SCAR=48.6
Pod documented
102 1 my ( $self, $args ) = @_;
103  
104 1 my $xml = '';
105 1 my $info = '';
106 1 my $items = '';
107  
108 1 my $indent = ' ' x 3;
109  
110 1 my @params = grep !/(?:destination|session_id|owner)/, $self->param();
111  
112 1 my ($referer, $handler, $time, $destination, $session_id, $owner);
113  
114 1 ▶ my $ns_prefix = $args->{ns_prefix} || $self->ns_prefix();
Condition33%1/3
$args->{'ns_prefix'} || $self->ns_prefix
l!l&&r!l&&!r
010
MC/DC0%0/2
$args->{'ns_prefix'} || $self->ns_prefix
$args->{'ns_prefix'} $self->ns_prefix
Truth table33%1/3
A || B
A= $args->{'ns_prefix'}B= $self->ns_prefix
ABresult
1X1
011
000
115 1 ▶ my $ns_url = $args->{ns_url} || $self->ns_url();
Condition33%1/3
$args->{'ns_url'} || $self->ns_url
l!l&&r!l&&!r
010
MC/DC0%0/2
$args->{'ns_url'} || $self->ns_url
$args->{'ns_url'} $self->ns_url
Truth table33%1/3
A || B
A= $args->{'ns_url'}B= $self->ns_url
ABresult
1X1
011
000
116  
117 1 my %info;
118  
119 1 ▶ my $pref = $ns_prefix ? "$ns_prefix:" : '' ;
Branch: 1 branch
BranchTrueFalseTotal
$ns_prefix ? : 1 0 1
120  
121 1 ▶ unless ( $self->omit_info() || $args->{omit_info} )
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->omit_info or $args->{"omit_info"}) 0 1 1
Condition33%1/3
$self->omit_info or $args->{"omit_info"}
l!l&&r!l&&!r
100
MC/DC0%0/2
$self->omit_info or $args->{"omit_info"}
$self->omit_info $args->{"omit_info"}
Truth table33%1/3
A or B
A= $self->omit_infoB= $args->{"omit_info"}
ABresult
1X1
011
000
122 {
123 0 my %mandatory = (
124 referer => 1,
125 handler => 1,
126 timestamp => 1
127 );
128  
129 0 ▶ $info{referer} = $self->referer() || '';
Condition0%0/2
$self->referer || ''
l!l
00
MC/DC0%0/1
$self->referer || ''
$self->referer
Truth table0%0/2
A
A= $self->referer
Aresult
11
00
130 0 ▶ $info{handler} = $self->script_name() || '';
Condition0%0/2
$self->script_name || ''
l!l
00
MC/DC0%0/1
$self->script_name || ''
$self->script_name
Truth table0%0/2
A
A= $self->script_name
Aresult
11
00
131  
132 0 $info{timestamp} = strftime("%Y-%d-%mT%H:%M:%S",localtime());
133  
134  
135 0 ▶ $info{destination} = $self->param('destination') || $self->destination()
Condition0%0/5
$self->param('destination') || $self->destination || ''
l!l
00
$self->param("destination") or $self->destination
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->param('destination') || $self->destination || ''
$self->param("destination") $self->destination
Truth table0%0/3
A or B
A= $self->param("destination")B= $self->destination
ABresult
1X1
011
000
136 || '';
137 0 ▶ $info{session_id} = $self->param('session_id') || $self->sess_id() || '';
Condition0%0/5
$self->param('session_id') || $self->sess_id || ''
l!l
00
$self->param("session_id") or $self->sess_id
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->param('session_id') || $self->sess_id || ''
$self->param("session_id") $self->sess_id
Truth table0%0/3
A or B
A= $self->param("session_id")B= $self->sess_id
ABresult
1X1
011
000
138 0 ▶ $info{owner} = $self->param('owner') || $self->owner() || '' ;
Condition0%0/5
$self->param('owner') || $self->owner || ''
l!l
00
$self->param("owner") or $self->owner
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->param('owner') || $self->owner || ''
$self->param("owner") $self->owner
Truth table0%0/3
A or B
A= $self->param("owner")B= $self->owner
ABresult
1X1
011
000
139  
140
141  
142
143 0 for my $item ( keys %info )
144 {
145 0 my $indent = $indent x 2;
146
147 0 ▶ if ( length $info{$item} )
Branch: 2 branches
BranchTrueFalseTotal
if (length $info{$item}) { } 0 0 0
elsif ($mandatory{$item}) { } 0 0 0
148 {
149 0 $info{$item} = _quote_xml($info{$item});
150
151 0 $info .= "$indent<$pref$item>$info{$item}</$pref$item>\n";
152 }
153 elsif ($mandatory{$item})
154 {
155 0 $info .= "$indent<$pref$item />\n";
156 }
157 }
158  
159 0 $info = "$indent<${pref}info>\n$info$indent</${pref}info>\n";
160 }
161  
162 1 foreach my $param ( @params )
163 {
164 1 my $indent = $indent x 2;
165  
166 1 my $value = $self->param($param);
167  
168 1 ▶ if (ref $value )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value) { } 0 1 1
169 {
170 0 my $index = 0;
171 0 foreach my $mvalue ( @{$value} )
172 {
173 0 $index++;
174 0 $mvalue = _quote_xml($mvalue);
175 0 $items .= qq%$indent<${pref}field name="$param" index="$index">%;
176 0 $items .= "$mvalue</${pref}field>\n";
177 }
178 }
179 else
180 {
181 1 $value = _quote_xml($value);
182 1 $items .= qq%$indent<${pref}field name="$param">%;
183 1 $items .= "$value</${pref}field>\n";
184 }
185 }
186  
187 1 $items = "$indent<${pref}items>\n$items$indent</${pref}items>\n";
188  
189 1 my $ns_att = '';
190  
191
192 1 ▶ if ( $ns_url )
Branch: 1 branch
BranchTrueFalseTotal
if ($ns_url) 1 0 1
193 {
194  
195 1 my $prefix_part = '';
196  
197 1 ▶ if ($ns_prefix )
Branch: 1 branch
BranchTrueFalseTotal
if ($ns_prefix) 1 0 1
198 {
199 1 $prefix_part = ":$ns_prefix";
200 }
201 1 $ns_att = qq% xmlns$prefix_part="$ns_url"%;
202 }
203  
204 1 $xml = "<${pref}form_data$ns_att>\n$info$items</${pref}form_data>\n";
205  
206 1 return $xml;
207 }
208  
209 =item ns_prefix
210  
211 Gets and/or sets the namespace prefix as described as an argument to asXML()
212 above.
213  
214 =cut
215  
216 sub ns_prefix
217 3 ▶{
Subroutine ns_prefix: called CC=3 SCAR=11.0
Pod documented
218 3 my ( $self, $ns_prefix ) = @_;
219  
220 3 ▶ if ( defined $ns_prefix )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ns_prefix) 1 2 3
221 {
222 1 $self->{_private}->{ns_prefix} = $ns_prefix;
223 }
224
225 3 ▶ return $self->{_private}->{ns_prefix} || '';
Condition50%1/2
$self->{'_private'}{'ns_prefix'} || ''
l!l
30
MC/DC0%0/1
$self->{'_private'}{'ns_prefix'} || ''
$self->{'_private'}{'ns_prefix'}
Truth table50%1/2
A
A= $self->{'_private'}{'ns_prefix'}
Aresult
11
00
226 }
227  
228 =item ns_url
229  
230 Returns and/or sets the namespace URL for the document as described as an
231 argument to asXML() above.
232  
233 =cut
234  
235 sub ns_url
236 1 ▶{
Subroutine ns_url: called CC=3 SCAR=12.7
Pod documented
237 1 my ( $self, $ns_url ) = @_;
238  
239 1 ▶ if ( defined $ns_url )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ns_url) 0 1 1
240 {
241 0 $self->{_private}->{ns_url} = $ns_url;
242 }
243
244 1 my $def_url = 'http://schemas.gellyfish.com/FormData';
245  
246 1 ▶ return $self->{_private}->{ns_url} || $def_url;
Condition33%1/3
$self->{'_private'}{'ns_url'} || $def_url
l!l&&r!l&&!r
010
MC/DC0%0/2
$self->{'_private'}{'ns_url'} || $def_url
$self->{'_private'}{'ns_url'} $def_url
Truth table33%1/3
A || B
A= $self->{'_private'}{'ns_url'}B= $def_url
ABresult
1X1
011
000
247  
248 }
249  
250 =item omit_info
251  
252 If this is set to a true value then the 'header' information will not be
253 emitted in the output document.
254  
255 =cut
256  
257 sub omit_info
258 3 ▶{
Subroutine omit_info: called CC=3 SCAR=11.0
Pod documented
259 3 my ( $self, $omit_info ) = @_;
260  
261 3 ▶ if ( defined $omit_info )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $omit_info) 1 2 3
262 {
263 1 $self->{_private}->{omit_info} = $omit_info;
264 }
265  
266 3 ▶ return $self->{_private}->{omit_info} || 0;
Condition50%1/2
$self->{'_private'}{'omit_info'} || 0
l!l
30
MC/DC0%0/1
$self->{'_private'}{'omit_info'} || 0
$self->{'_private'}{'omit_info'}
Truth table50%1/2
A
A= $self->{'_private'}{'omit_info'}
Aresult
11
00
267  
268 }
269  
270 =item destination
271  
272 This is used to set the value of the 'destination' element in the header
273 information of the output document. This may be a URL, email address or
274 some other identifier. Its content is entirely application specific.
275  
276 =cut
277  
278 sub destination
279 0 ▶{
Subroutine destination: not called CC=3 SCAR=24.8
Pod documented
280 0 my ( $self, $destination ) = @_;
281  
282 0 ▶ if ( defined $destination )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $destination) 0 0 0
283 {
284 0 $self->{_private}->{destination} = $destination;
285 }
286  
287 return exists $self->{_private}->{destination} ?
288 0 ▶ $self->{_private}->{destination} : '';
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{'_private'}{'destination'} ? : 0 0 0
289 }
290  
291 =item sess_id
292  
293 This sets the 'session id' for this CGI request, it is intended to be a
294 unique identifier for this request and may take the form of a UUID or an
295 MD5 hash or something similar. Its use is application specific.
296  
297 =cut
298  
299 sub sess_id
300 0 ▶{
Subroutine sess_id: not called CC=3 SCAR=24.8
Pod documented
301 0 my ( $self , $sess_id ) = @_;
302  
303 0 ▶ if ( defined $sess_id )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $sess_id) 0 0 0
304 {
305 0 $self->{_private}->{sess_id} = $sess_id;
306 }
307  
308 return exists $self->{_private}->{sess_id} ?
309 0 ▶ $self->{_private}->{sess_id} : '' ;
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{'_private'}{'sess_id'} ? : 0 0 0
310  
311 }
312  
313 =item owner
314  
315 This sets the value of the 'owner' element in the header information. This
316 is intended to be the e-mail address indicating the contact for this
317 application. The usage of this information is application specific.
318  
319 =cut
320  
321 sub owner
322 0 ▶{
Subroutine owner: not called CC=3 SCAR=24.8
Pod documented
323 0 my ( $self , $owner ) = @_;
324  
325 0 ▶ if ( defined $owner )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $owner) 0 0 0
326 {
327 0 $self->{_private}->{owner} = $owner;
328 }
329  
330 return exists $self->{_private}->{owner} ?
331 0 ▶ $self->{_private}->{owner} : '' ;
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{'_private'}{'owner'} ? : 0 0 0
332  
333 }
334  
335 sub _quote_xml
336 1 ▶{
Subroutine _quote_xml: called CC=1 SCAR=0.0
337 1 $_[0] =~ s/&/&amp;/g;
338 1 $_[0] =~ s/</&lt;/g;
339 1 $_[0] =~ s/>/&gt;/g;
340 1 $_[0] =~ s/'/&apos;/g;
341 1 $_[0] =~ s/"/&quot;/g;
342 0 $_[0] =~ s/([\x80-\xFF])/&XmlUtf8Encode(ord($1))/ge;
343 1 return($_[0]);
344 }
345  
346 # I borrowed this from CGI::XML which in turn said
347 # borrowed from XML::DOM
348  
349 sub _xml_utf8_encode
350 0 ▶{
Subroutine _xml_utf8_encode: not called CC=5 SCAR=34.0
351 0 my ($n) = @_;
352 0 ▶ if ($n < 0x80)
Branch: 4 branches
BranchTrueFalseTotal
if ($n < 128) { } 0 0 0
elsif ($n < 2048) { } 0 0 0
elsif ($n < 65536) { } 0 0 0
elsif ($n < 1114112) { } 0 0 0
353 {
354 0 return chr ($n);
355 }
356 elsif ($n < 0x800)
357 {
358 0 return pack ("CC", (($n >> 6) | 0xc0), (($n & 0x3f) | 0x80));
359 }
360 elsif ($n < 0x10000)
361 {
362 0 return pack ("CCC", (($n >> 12) | 0xe0), ((($n >> 6) & 0x3f) | 0x80),
363 (($n & 0x3f) | 0x80));
364 }
365 elsif ($n < 0x110000)
366 {
367 0 return pack ("CCCC", (($n >> 18) | 0xf0), ((($n >> 12) & 0x3f) | 0x80),
368 ((($n >> 6) & 0x3f) | 0x80), (($n & 0x3f) | 0x80));
369 }
370  
371 0 return $n;
372 }
373  
374 =back
375  
376 =cut
377  
378 2 1;
379 __END__
↑ Summary