| 1 | package Froody::Response::PerlDS; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | 5 | ▶ | use base qw(Froody::Response::Content); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 3 | 5 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||
| 6 | 5 | ▶ | use XML::LibXML; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 5 | ▶ | use Encode qw(encode); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | 5 | ▶ | use Params::Validate qw(SCALAR ARRAYREF HASHREF); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||
| 10 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||
| 12 | Froody::Response::PerlDS - create a response from a Perl data structure | ||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||
| 14 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||
| 16 | my $rsp = Froody::Response::PerlDS->new(); | ||||||||||||||||||||||||||||||||||||||||||
| 17 | $rsp->structure($froody_method); | ||||||||||||||||||||||||||||||||||||||||||
| 18 | $rsp->content({ | ||||||||||||||||||||||||||||||||||||||||||
| 19 | name => "bob", | ||||||||||||||||||||||||||||||||||||||||||
| 20 | attributes => { foo => "bar" }, | ||||||||||||||||||||||||||||||||||||||||||
| 21 | value => "harry", | ||||||||||||||||||||||||||||||||||||||||||
| 22 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 23 | print $rsp->render(); | ||||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||
| 25 | # prints out | ||||||||||||||||||||||||||||||||||||||||||
| 26 | <?xml version="1.0" encoding="utf-8" ?> | ||||||||||||||||||||||||||||||||||||||||||
| 27 | <rsp stat="ok"> | ||||||||||||||||||||||||||||||||||||||||||
| 28 | <bob foo="bar">harry</bob> | ||||||||||||||||||||||||||||||||||||||||||
| 29 | </rsp> | ||||||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||||||
| 31 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||||
| 33 | DEPRECATED!!!! Please use Froody::Response::Terse | ||||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||
| 35 | This is a simple type of response that allows you to quickly create | ||||||||||||||||||||||||||||||||||||||||||
| 36 | responses from Perl data structures. | ||||||||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||||||||
| 38 | For example: | ||||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||
| 40 | $response->content({ | ||||||||||||||||||||||||||||||||||||||||||
| 41 | name => "bob", | ||||||||||||||||||||||||||||||||||||||||||
| 42 | attributes => { foo => "bar" }, | ||||||||||||||||||||||||||||||||||||||||||
| 43 | value => "harry", | ||||||||||||||||||||||||||||||||||||||||||
| 44 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||||
| 46 | will result in the XML: | ||||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||
| 48 | <?xml version="1.0" encoding="utf-8" ?> | ||||||||||||||||||||||||||||||||||||||||||
| 49 | <rsp stat="ok"> | ||||||||||||||||||||||||||||||||||||||||||
| 50 | <bob foo="bar">harry</bob> | ||||||||||||||||||||||||||||||||||||||||||
| 51 | </rsp> | ||||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||
| 53 | and | ||||||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||||||
| 55 | $response->content({ | ||||||||||||||||||||||||||||||||||||||||||
| 56 | name => "bob", | ||||||||||||||||||||||||||||||||||||||||||
| 57 | attributes => { foo => "bar" }, | ||||||||||||||||||||||||||||||||||||||||||
| 58 | children => [ | ||||||||||||||||||||||||||||||||||||||||||
| 59 | { | ||||||||||||||||||||||||||||||||||||||||||
| 60 | name => "dave", | ||||||||||||||||||||||||||||||||||||||||||
| 61 | attributes => { fuzz => "ball" }, | ||||||||||||||||||||||||||||||||||||||||||
| 62 | value => "ninepence", | ||||||||||||||||||||||||||||||||||||||||||
| 63 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 64 | ], | ||||||||||||||||||||||||||||||||||||||||||
| 65 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||||||
| 67 | will result in the XML | ||||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||
| 69 | <?xml version="1.0" encoding="utf-8" ?> | ||||||||||||||||||||||||||||||||||||||||||
| 70 | <rsp stat="ok"> | ||||||||||||||||||||||||||||||||||||||||||
| 71 | <bob foo="bar"> | ||||||||||||||||||||||||||||||||||||||||||
| 72 | <dave fuzz="ball">ninepence</dave> | ||||||||||||||||||||||||||||||||||||||||||
| 73 | </bob> | ||||||||||||||||||||||||||||||||||||||||||
| 74 | </rsp> | ||||||||||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||||||||||
| 76 | Adding content implicitly sets the status to 'ok', unless it is already | ||||||||||||||||||||||||||||||||||||||||||
| 77 | set, because this is probably what you mean, and therefore you don't | ||||||||||||||||||||||||||||||||||||||||||
| 78 | have to think about it. | ||||||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||||||
| 80 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||
| 82 | =head2 Attributes | ||||||||||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||||||||||
| 84 | In addition to the attributes inherited from Froody::Response, this class has | ||||||||||||||||||||||||||||||||||||||||||
| 85 | the following get/set methods: | ||||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||
| 87 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||||||
| 89 | =item status | ||||||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||||||
| 91 | The status of the response, should be 'ok' or 'fail' only. | ||||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||
| 93 | =item content | ||||||||||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||||||||||
| 95 | The contents of the data structure. Setting this accessor causes a deep | ||||||||||||||||||||||||||||||||||||||||||
| 96 | clone of the data structure to happen, meaning that you can add similar | ||||||||||||||||||||||||||||||||||||||||||
| 97 | content to multiple Response objects by setting on one object, altering, then | ||||||||||||||||||||||||||||||||||||||||||
| 98 | setting another object. | ||||||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||||||
| 100 | my $data = { name => "fred", attr => { fred => "wilma" } }; | ||||||||||||||||||||||||||||||||||||||||||
| 101 | $response_wives->content($data); | ||||||||||||||||||||||||||||||||||||||||||
| 102 | $data->{attr}{fred} = "barney"; | ||||||||||||||||||||||||||||||||||||||||||
| 103 | $response_buddies->content($data); | ||||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||
| 105 | Note however, this is not true for the returned value from content. Altering | ||||||||||||||||||||||||||||||||||||||||||
| 106 | that data structure really does alter the data in the response (this is | ||||||||||||||||||||||||||||||||||||||||||
| 107 | considered a feature) | ||||||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||||||
| 109 | # in the future, fred is buddies with george | ||||||||||||||||||||||||||||||||||||||||||
| 110 | $response_buddies->content->{attr}{fred} = "george"; | ||||||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||||||
| 112 | =item root_name | ||||||||||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||||||||||
| 114 | the name of the root node. If unset, this will default to 'rsp'. See | ||||||||||||||||||||||||||||||||||||||||||
| 115 | L<default_root_name>. | ||||||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||||||
| 117 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||||||
| 119 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||||||||
| 121 | 5 | __PACKAGE__->mk_accessors(qw( | |||||||||||||||||||||||||||||||||||||||||
| 122 | root_name | ||||||||||||||||||||||||||||||||||||||||||
| 123 | )); | ||||||||||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||||||||||
| 125 | =head1 Methods | ||||||||||||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||||||||||
| 127 | =over | ||||||||||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||||||||||
| 129 | =item new() | ||||||||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||||||||
| 131 | Creates a new, empty message object. Inherited from L<Froody::Base>. | ||||||||||||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||||||||||||
| 133 | =item find_by_path($xpath) | ||||||||||||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||||||||||||
| 135 | Finds the correct node in the Froody::Response structure given an | ||||||||||||||||||||||||||||||||||||||||||
| 136 | xpath (like) path. | ||||||||||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||||||||||
| 138 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||||||||||||
| 140 | 0 | ▶ | sub find_by_path { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
find_by_path: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 141 | 0 | my ($self, $target) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 142 | 0 | _find_by_path ({children => $self->{content}}, $target); | |||||||||||||||||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||||||||||
| 145 | 0 | ▶ | sub _find_by_path { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_find_by_path: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||
| 146 | 0 | my ($node, $name) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 147 | 0 | ▶ | return $node unless defined $name; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 148 | 0 | my ($lookup, $next) = split '/', $name, 2; | |||||||||||||||||||||||||||||||||||||||||
| 149 | 0 | my ($child) = grep {$_->{name} eq $lookup} @{$node->{children}}; | |||||||||||||||||||||||||||||||||||||||||
| 150 | 0 | _find_by_path ($child, $next) | |||||||||||||||||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||||||||||||
| 153 | =item default_root_name | ||||||||||||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||||||||||||
| 155 | Returns the default name of the root node. Is "rsp" for this class - you | ||||||||||||||||||||||||||||||||||||||||||
| 156 | can override this by subclassing, or by setting the 'root_name' property | ||||||||||||||||||||||||||||||||||||||||||
| 157 | on an instance. | ||||||||||||||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||||||||||||
| 159 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||||||||||||
| 161 | 6 | ▶ | sub default_root_name { "rsp" } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
default_root_name: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||||||||||||
| 163 | =item raw_render | ||||||||||||||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||||||||||||
| 165 | Internal method, designed for subclassing. Does the actual rendering. | ||||||||||||||||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||||||||||||||||
| 167 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||||||||||||||
| 169 | sub _to_xml | ||||||||||||||||||||||||||||||||||||||||||
| 170 | 6 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_to_xml: called CC=3 SCAR=11.1
| |||||||||||||||||||||||||||||||||||||||||||
| 171 | 6 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 172 | 6 | my $content = shift; | |||||||||||||||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||||||||||||||||
| 174 | 6 | my $doc = XML::LibXML::Document->new("1.0","utf-8"); | |||||||||||||||||||||||||||||||||||||||||
| 175 | 6 | ▶ | my $root = $doc->createElement($self->root_name || $self->default_root_name); | ||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$self->root_name || $self->default_root_name
MC/DC0%0/2
$self->root_name || $self->default_root_name
$self->root_name
$self->default_root_name
Truth table33%1/3
A || B
A= $self->root_nameB= $self->default_root_name
| |||||||||||||||||||||||||||||||||||||||||||
| 176 | 6 | $root->setAttribute("stat", $self->status); | |||||||||||||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||||||||||||||
| 178 | # let's recurse again....like we did last summer. | ||||||||||||||||||||||||||||||||||||||||||
| 179 | 6 | foreach ($self->_xml_nodes( | |||||||||||||||||||||||||||||||||||||||||
| 180 | doc => $doc, | ||||||||||||||||||||||||||||||||||||||||||
| 181 | list => [$content], | ||||||||||||||||||||||||||||||||||||||||||
| 182 | 5 | )) { $root->addChild($_) } | |||||||||||||||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||||||||||||||||
| 184 | # finally add the root node. | ||||||||||||||||||||||||||||||||||||||||||
| 185 | 5 | $doc->addChild($root); | |||||||||||||||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||||||||||||||||
| 187 | 5 | return $doc; | |||||||||||||||||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||||||||||||||||
| 190 | # _xml_nodes( doc => xml docuement, list => listref ) | ||||||||||||||||||||||||||||||||||||||||||
| 191 | # given a listref of content nodes, return XML::LibXML node objects | ||||||||||||||||||||||||||||||||||||||||||
| 192 | # for them. Called recursively for deep structures. | ||||||||||||||||||||||||||||||||||||||||||
| 193 | 10 | ▶ | sub _xml_nodes { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_xml_nodes: called CC=8 SCAR=20.8
| |||||||||||||||||||||||||||||||||||||||||||
| 194 | 10 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||||||||||||||||
| 196 | 10 | my %args = $self->validate_object(@_, { | |||||||||||||||||||||||||||||||||||||||||
| 197 | doc => { isa => "XML::LibXML::Document" }, | ||||||||||||||||||||||||||||||||||||||||||
| 198 | list => { type => ARRAYREF }, | ||||||||||||||||||||||||||||||||||||||||||
| 199 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||||||||||||||||
| 201 | 10 | my $enc = $args{doc}->getEncoding(); | |||||||||||||||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||||||||||||||||
| 203 | 10 | my @xml_nodes; | |||||||||||||||||||||||||||||||||||||||||
| 204 | 10 | for my $value (@{ $args{list} }) { | |||||||||||||||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||||||||||||||||
| 206 | # create the node | ||||||||||||||||||||||||||||||||||||||||||
| 207 | 11 | my $name = encode($enc, $value->{name}, 1); | |||||||||||||||||||||||||||||||||||||||||
| 208 | 11 | my $node = $args{doc}->createElement( $name ); | |||||||||||||||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||||||||||||||||
| 210 | # populate attributes, if any | ||||||||||||||||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||||||||||||||||
| 212 | # we're not doing encoding here because it doesn't work. | ||||||||||||||||||||||||||||||||||||||||||
| 213 | # Yes, I know it should, but it's got one of those hard to track | ||||||||||||||||||||||||||||||||||||||||||
| 214 | # down bugs. | ||||||||||||||||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||||||||||||||||
| 216 | 11 | ▶ | for my $att (sort keys %{ $value->{attributes} || {} }) { | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$value->{'attributes'} || {}
MC/DC100%1/1
$value->{'attributes'} || {}
$value->{'attributes'}
Truth table100%2/2
A
A= $value->{'attributes'}
| |||||||||||||||||||||||||||||||||||||||||||
| 217 | 11 | ▶ | if (defined $value->{attributes}{$att}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 218 | 11 | my $key = $att; | |||||||||||||||||||||||||||||||||||||||||
| 219 | # Encode::encode($enc, $att, 1); | ||||||||||||||||||||||||||||||||||||||||||
| 220 | 11 | my $val = $value->{attributes}{$att}; | |||||||||||||||||||||||||||||||||||||||||
| 221 | # Encode::encode($enc, $value->{attributes}{$att}, 1); | ||||||||||||||||||||||||||||||||||||||||||
| 222 | 11 | $node->setAttribute( $key, $val ) | |||||||||||||||||||||||||||||||||||||||||
| 223 | } | ||||||||||||||||||||||||||||||||||||||||||
| 224 | } | ||||||||||||||||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||||||||||||||||
| 226 | # set value, if any | ||||||||||||||||||||||||||||||||||||||||||
| 227 | 10 | ▶ | if (defined($value->{value})) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 228 | # there is a bigger rant here than I really want to think about, | ||||||||||||||||||||||||||||||||||||||||||
| 229 | # so I'm not going to bother. Come to my talk. | ||||||||||||||||||||||||||||||||||||||||||
| 230 | # essentially: you've got to add bytes here in whatever encoding | ||||||||||||||||||||||||||||||||||||||||||
| 231 | # the document is in. Dontcha just love that? | ||||||||||||||||||||||||||||||||||||||||||
| 232 | 5 | ▶ | use utf8; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 233 | 3 | utf8::upgrade( $value->{value} ); | |||||||||||||||||||||||||||||||||||||||||
| 234 | 3 | my $text = $args{doc}->createTextNode( $value->{value} ); | |||||||||||||||||||||||||||||||||||||||||
| 235 | 3 | $node->addChild( $text ); | |||||||||||||||||||||||||||||||||||||||||
| 236 | } | ||||||||||||||||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||||||||||||||||
| 238 | # add children, if any | ||||||||||||||||||||||||||||||||||||||||||
| 239 | 10 | ▶ | if (defined($value->{children})) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 240 | 4 | for ($self->_xml_nodes( | |||||||||||||||||||||||||||||||||||||||||
| 241 | doc => $args{doc}, | ||||||||||||||||||||||||||||||||||||||||||
| 242 | list => $value->{children}, | ||||||||||||||||||||||||||||||||||||||||||
| 243 | )) { | ||||||||||||||||||||||||||||||||||||||||||
| 244 | 4 | $node->addChild($_); | |||||||||||||||||||||||||||||||||||||||||
| 245 | } | ||||||||||||||||||||||||||||||||||||||||||
| 246 | } | ||||||||||||||||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||||||||||||||||
| 248 | # add to list of returned nodes | ||||||||||||||||||||||||||||||||||||||||||
| 249 | 9 | push @xml_nodes, $node; | |||||||||||||||||||||||||||||||||||||||||
| 250 | } | ||||||||||||||||||||||||||||||||||||||||||
| 251 | 8 | return @xml_nodes; | |||||||||||||||||||||||||||||||||||||||||
| 252 | } | ||||||||||||||||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||||||||||||||||
| 254 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||||||||||||||||
| 256 | =head2 Converting other Responses to Froody::Response::PerlDS objects | ||||||||||||||||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||||||||||||||||
| 258 | Once you've loaded this class you can automatically convert other | ||||||||||||||||||||||||||||||||||||||||||
| 259 | Froody::Response class instances to Froody::Response::PerlDS objects with | ||||||||||||||||||||||||||||||||||||||||||
| 260 | the C<as_perlds> method. | ||||||||||||||||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||||||||||||||||
| 262 | use Froody::Response::String; | ||||||||||||||||||||||||||||||||||||||||||
| 263 | use Froody::Response::PerlDS; | ||||||||||||||||||||||||||||||||||||||||||
| 264 | my $perlds = Froody::Response::String | ||||||||||||||||||||||||||||||||||||||||||
| 265 | ->new() | ||||||||||||||||||||||||||||||||||||||||||
| 266 | ->structure($froody_method) | ||||||||||||||||||||||||||||||||||||||||||
| 267 | ->set_string('<rsp stat="ok"><foo>bar</foo></rsp>'); | ||||||||||||||||||||||||||||||||||||||||||
| 268 | ->as_perlds; | ||||||||||||||||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||||||||||||||||
| 270 | print ref($perlds); # prints "Froody::Response::PerlDS" | ||||||||||||||||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||||||||||||||||
| 272 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||||||||||||||||
| 274 | # as_perlds is documented | ||||||||||||||||||||||||||||||||||||||||||
| 275 | 0 | ▶ | sub as_perlds { return $_[0] } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
as_perlds: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 276 | sub Froody::Response::as_perlds | ||||||||||||||||||||||||||||||||||||||||||
| 277 | 1 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
as_perlds: called CC=5 SCAR=18.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 278 | 1 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 279 | 1 | $self = $self->as_xml; # get as far as XML | |||||||||||||||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||||||||||||||||
| 281 | 1 | my $perlds = Froody::Response::PerlDS->new() | |||||||||||||||||||||||||||||||||||||||||
| 282 | ->structure($self->structure); | ||||||||||||||||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||||||||||||||||
| 284 | # find the top node | ||||||||||||||||||||||||||||||||||||||||||
| 285 | 1 | ▶ | my ($top) = $self->xml->findnodes("/rsp") | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 286 | or Froody::Error->throw("froody.convert", "no rsp!"); | ||||||||||||||||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||||||||||||||||
| 288 | 1 | my $stat = $top->getAttribute("stat"); | |||||||||||||||||||||||||||||||||||||||||
| 289 | 1 | ▶ | unless (defined($stat) && ($stat eq "ok" || $stat eq "fail")) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
defined $stat and $stat eq "ok" || $stat eq "fail"
$stat eq "ok" or $stat eq "fail"
MC/DC0%0/3
defined $stat and $stat eq "ok" || $stat eq "fail"
defined $stat
$stat eq "ok"
$stat eq "fail"
Truth table25%1/4
A and B or C
A= defined $statB= $stat eq "ok"C= $stat eq "fail"
| |||||||||||||||||||||||||||||||||||||||||||
| 290 | 0 | { Froody::Error->throw("froody.convert", "invalid stat!") } | |||||||||||||||||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||||||||||||||||
| 292 | # right, recurse down our XML and turn it into this data structure | ||||||||||||||||||||||||||||||||||||||||||
| 293 | 1 | $perlds->content(_recurse_to_ds($top->findnodes("./*"))); | |||||||||||||||||||||||||||||||||||||||||
| 294 | 1 | $perlds->status( $stat ); | |||||||||||||||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||||||||||||||||
| 296 | 1 | return $perlds; | |||||||||||||||||||||||||||||||||||||||||
| 297 | } | ||||||||||||||||||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||||||||||||||||
| 299 | sub _recurse_to_ds | ||||||||||||||||||||||||||||||||||||||||||
| 300 | 3 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_recurse_to_ds: called CC=5 SCAR=16.2
| |||||||||||||||||||||||||||||||||||||||||||
| 301 | 3 | my $node = shift; | |||||||||||||||||||||||||||||||||||||||||
| 302 | 3 | ▶ | return {} unless $node; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||||||||||||||||
| 304 | # work out the text content of this node | ||||||||||||||||||||||||||||||||||||||||||
| 305 | 3 | my $text = $node->findvalue("./text()"); | |||||||||||||||||||||||||||||||||||||||||
| 306 | 3 | $text =~ s/^\s+//; # lose leading white space | |||||||||||||||||||||||||||||||||||||||||
| 307 | 3 | $text =~ s/\s+$//; # lose trailing white space | |||||||||||||||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||||||||||||||||
| 309 | # what were the attributes? | ||||||||||||||||||||||||||||||||||||||||||
| 310 | 3 | my %attr = map { $_->nodeName => $_->getValue } $node->findnodes("./@*"); | |||||||||||||||||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||||||||||||||||
| 312 | # what were the children? | ||||||||||||||||||||||||||||||||||||||||||
| 313 | 3 | my @children = map { _recurse_to_ds($_) } $node->findnodes("./*"); | |||||||||||||||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||||||||||||||||
| 315 | return { | ||||||||||||||||||||||||||||||||||||||||||
| 316 | 3 | ▶ | name => $node->nodeName, | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||||||||||||||||
| 318 | # text, if there was any text | ||||||||||||||||||||||||||||||||||||||||||
| 319 | ((length $text) ? (value => $text) : ()), | ||||||||||||||||||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||||||||||||||||
| 321 | # attributes, if there were any attributes | ||||||||||||||||||||||||||||||||||||||||||
| 322 | ((%attr) ? (attributes => \%attr) : ()), | ||||||||||||||||||||||||||||||||||||||||||
| 323 | |||||||||||||||||||||||||||||||||||||||||||
| 324 | # children, if there were any children | ||||||||||||||||||||||||||||||||||||||||||
| 325 | ((@children) ? (children => \@children) : ()), | ||||||||||||||||||||||||||||||||||||||||||
| 326 | } | ||||||||||||||||||||||||||||||||||||||||||
| 327 | } | ||||||||||||||||||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||||||||||||||||
| 329 | =head1 BUGS | ||||||||||||||||||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||||||||||||||||
| 331 | Attribute names are not encoded (so people using non ASCII attribute names do | ||||||||||||||||||||||||||||||||||||||||||
| 332 | so at their own risk.) This doesn't work for me, and the whole attributes | ||||||||||||||||||||||||||||||||||||||||||
| 333 | vanish unexpectedly. This is either a XML::LibXML bug or a perl bug, but I | ||||||||||||||||||||||||||||||||||||||||||
| 334 | can't produce a small enough test case to make it work. Patches welcome. | ||||||||||||||||||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||||||||||||||||
| 336 | Please report any bugs you find via the CPAN RT system. | ||||||||||||||||||||||||||||||||||||||||||
| 337 | L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Froody> | ||||||||||||||||||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||||||||||||||||
| 339 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||||||||||||||||
| 341 | Copyright Fotango 2005. All rights reserved. | ||||||||||||||||||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||||||||||||||||
| 343 | Please see the main L<Froody> documentation for details of who has worked | ||||||||||||||||||||||||||||||||||||||||||
| 344 | on this project. | ||||||||||||||||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||||||||||||||||
| 346 | This module is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||||||||||||||||
| 347 | the same terms as Perl itself. | ||||||||||||||||||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||||||||||||||||
| 349 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||||||||||||||||
| 351 | L<Froody>, L<Froody::Response> | ||||||||||||||||||||||||||||||||||||||||||
| 352 | |||||||||||||||||||||||||||||||||||||||||||
| 353 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 354 | |||||||||||||||||||||||||||||||||||||||||||
| 355 | 5 | 1; | |||||||||||||||||||||||||||||||||||||||||