WebSource/Format.pm untested

filter: Statementstmt 0.0% 0 / 45 Branchbran 0.0% 0 / 16 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 72 CC 9 SCAR 26.7
CC
9
Cov
0%
handle
30.0
convertToXML
12.0
_init_
6.0
CRAP 14.4
1 package WebSource::Format;
2  
3 use WebSource::Module;
4  
5 use strict;
6 use Carp;
7  
8 our @ISA = ('WebSource::Module');
9  
10 =head1 NAME
11  
12 WebSource::Format - Format XML Nodes
13  
14 =head1 DESCRIPTION
15  
16 A format operator allows to prepare its input items for output to the user.
17  
18 The following formats exist :
19  
20 =over 2
21  
22 =item B<string> : returns the data as a string
23  
24 =item B<xml> : returns the data as XML
25  
26 =item B<select> : returns a selected (via the B<select> parameter) meta-information item on the data
27  
28 =item B<details> : returns both meta-information and data in an XML format
29  
30 =item B<replace> : returns the same data with a given string (B<find> parameter)
31 replaced by an other (B<replace> parameter)
32  
33 =back
34  
35 The C<format> attribut of the format declaration allows to determine which
36 format to use. Depending on the format different parameters are available.
37 The parameters are declared using the generic parameters/param elements.
38  
39 A typical replacement formatting is declared as follows :
40  
41 <ws:format name="opname" forward-to="ops" format="replace">
42 <parameters>
43 <param name="find" value="string-to-find" />
44 <param name="replace" value="string-to-replace" />
45 </parameters>
46 </ws:format>
47  
48 =cut
49  
50 sub _init_ {
51 my $self = shift;
52 $self->SUPER::_init_;
53 my $wsd = $self->{wsdnode};
54 if($wsd) {
55 $self->{format} = $wsd->getAttribute("format");
56 }
57 return $self;
58 }
59  
60 sub convertToXML {
61 my $env = shift;
62 my $t = $env->type;
63 if($t eq "object/dom-node") {
64 return $env->data->toString(1);
65 } elsif($t eq "object/http-request") {
66 return "<http-request>\n\t" . $env->data->as_string . "\n</http-request>";
67 } else {
68 return "<result>" . $env->dataString . "</result>";
69 }
70 }
71  
72 sub convertToDetails {
73 my $env = shift;
74 my $attrstr;
75 foreach my $key (sort(keys(%$env))) {
76 if(!($key eq 'data')) {
77 my $val = $env->{$key};
78 $val =~ s/"/&#34;/g; # "
79 $val =~ s/&/&#38;/g;
80 $attrstr .= "\n " . $key . '="' . $val . '"';
81 }
82 }
83 return "<result" . $attrstr . ">" . $env->dataString. "</result>";
84 }
85  
86 sub handle {
87 my $self = shift;
88 my $env = shift;
89 my $f = $self->{format};
90 my $t = $env->type;
91 if($f =~ /^xml$/) {
92 return WebSource::Envelope->new(type => "text/xml", data => convertToXML($env));
93 } elsif($f =~ "replace") {
94 my $s = $self->{find};
95 my $r = $self->{replace};
96 my $data = $env->dataString;
97 $data =~ s/$s/$r/;
98 return WebSource::Envelope->new(type => "text/string", "data" => $data);
99 } elsif($f =~ "details") {
100 return WebSource::Envelope->new(type => "text/xml", "data" => convertToDetails($env));
101 } elsif($f =~ "select") {
102 return WebSource::Envelope->new(
103 type => "text/string",
104 data => $env->{$self->{select}}
105 );
106 } else {
107 return WebSource::Envelope->new(type => "text/string", "data" => $env->dataString ."\n");
108 }
109 }
110  
111 =head1 SEE ALSO
112  
113 WebSource
114  
115 =cut
116  
117 1;