| 1 | package Froody::Response::XML; | ||||||||||||||||||||||||||||
| 2 | 25 | ▶ | use base qw(Froody::Response); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | 25 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 25 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 25 | ▶ | use Encode; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 25 | ▶ | use Scalar::Util qw(blessed); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 25 | ▶ | use XML::LibXML; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | =head1 NAME | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | Froody::Response::XML - create a response from a XML::LibXML document | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | my $response = Froody::Response::XML->new() | ||||||||||||||||||||||||||||
| 18 | ->structure($froody_method) | ||||||||||||||||||||||||||||
| 19 | ->xml($xml_doc); | ||||||||||||||||||||||||||||
| 20 | print $response->render; | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | This is a concrete implementation of Froody::Response. It takes its input | ||||||||||||||||||||||||||||
| 25 | from an XML::LibXML::Document. | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | use XML::LibXML; | ||||||||||||||||||||||||||||
| 28 | my $xml_doc = XML::LibXML::Document->new( "1.0", "utf-8" ); | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | # create the rsp | ||||||||||||||||||||||||||||
| 31 | my $rsp = $xml_doc->createElement("rsp"); | ||||||||||||||||||||||||||||
| 32 | $rsp->setAttribute("stat", "ok"); | ||||||||||||||||||||||||||||
| 33 | $xml_doc->setDocumentElement($rsp); | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | # add the child node foo | ||||||||||||||||||||||||||||
| 36 | my $foo = $xml_doc->createElement("foo"); | ||||||||||||||||||||||||||||
| 37 | $foo->appendText("bar"); # note, must pass bytes in the above encoding | ||||||||||||||||||||||||||||
| 38 | $rsp->appendChild($foo); | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | my $rsp = Froody::Response::XML->new() | ||||||||||||||||||||||||||||
| 41 | ->structure($froody_method) | ||||||||||||||||||||||||||||
| 42 | ->xml($xml_doc); | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | You can get and set the current XML document by usinc C<xml>. We only hold | ||||||||||||||||||||||||||||
| 45 | a reference to the data so you can modify the XML after you've assigned it | ||||||||||||||||||||||||||||
| 46 | to the response and it'll still effect that response. This means the | ||||||||||||||||||||||||||||
| 47 | above could be re-ordered as: | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | use XML::LibXML; | ||||||||||||||||||||||||||||
| 50 | my $xml_doc = XML::LibXML::Document->new( "1.0", "utf-8" ); | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | my $rsp = Froody::Response::XML->new() | ||||||||||||||||||||||||||||
| 53 | ->structure($froody_method) | ||||||||||||||||||||||||||||
| 54 | ->xml($xml_doc); | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | # create the rsp | ||||||||||||||||||||||||||||
| 57 | my $rsp = $xml_doc->createElement("rsp"); | ||||||||||||||||||||||||||||
| 58 | $rsp->setAttribute("stat", "ok"); | ||||||||||||||||||||||||||||
| 59 | $xml_doc->setDocumentElement($rsp); | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | # add the child node foo | ||||||||||||||||||||||||||||
| 62 | my $foo = $xml_doc->createElement("foo"); | ||||||||||||||||||||||||||||
| 63 | $foo->appendText("bar"); # note, must pass bytes in the above encoding | ||||||||||||||||||||||||||||
| 64 | $rsp->appendChild($foo); | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | And it'll work just as fine. This does however mean you should be careful | ||||||||||||||||||||||||||||
| 67 | about re-using XML::LibXML objects between responses. | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | =cut | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | # simple get/set accessor, returns self on set and checks what you're setting | ||||||||||||||||||||||||||||
| 72 | # xml is documented | ||||||||||||||||||||||||||||
| 73 | sub xml | ||||||||||||||||||||||||||||
| 74 | 78 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
xml: called CC=4 SCAR=15.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 75 | 78 | my $self = shift; | |||||||||||||||||||||||||||
| 76 | 78 | ▶ | return $self->{xml} unless @_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | 29 | ▶ | unless (blessed($_[0]) && $_[0]->isa("XML::LibXML::Document")) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
builtin::blessed($_[0]) and $_[0]->isa("XML::LibXML::Document")
MC/DC0%0/2
builtin::blessed($_[0]) and $_[0]->isa("XML::LibXML::Document")
builtin::blessed($_[0])
$_[0]->isa("XML::LibXML::Document")
Truth table33%1/3
A and B
A= builtin::blessed($_[0])B= $_[0]->isa("XML::LibXML::Document")
| |||||||||||||||||||||||||||||
| 78 | 0 | { Froody::Error->throw("perl.methodcall.param", | |||||||||||||||||||||||||||
| 79 | "xml only accepts XML::LibXML::Document instances") } | ||||||||||||||||||||||||||||
| 80 | 29 | $self->{xml} = shift; | |||||||||||||||||||||||||||
| 81 | 29 | return $self; | |||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | sub render | ||||||||||||||||||||||||||||
| 85 | 1 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
render: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 86 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 87 | 1 | my $string = $self->xml->toString(@_); | |||||||||||||||||||||||||||
| 88 | 1 | my $encoded = Encode::encode("utf-8", $string); | |||||||||||||||||||||||||||
| 89 | 1 | return $encoded; | |||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | # status is documented | ||||||||||||||||||||||||||||
| 93 | sub status | ||||||||||||||||||||||||||||
| 94 | 11 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
status: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 95 | 11 | my $self = shift; | |||||||||||||||||||||||||||
| 96 | 11 | $self->xml->findvalue('/rsp/@stat'); | |||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | =head2 Converting other Responses to Froody::Response::XML objects | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | Once you've loaded this class you can automatically convert other | ||||||||||||||||||||||||||||
| 101 | Froody::Response class instances to Froody::Response::XML objects with | ||||||||||||||||||||||||||||
| 102 | the C<as_xml> method. | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | use Froody::Response::String; | ||||||||||||||||||||||||||||
| 105 | use Froody::Response::XML; | ||||||||||||||||||||||||||||
| 106 | my $string = Froody::Response::String | ||||||||||||||||||||||||||||
| 107 | ->new() | ||||||||||||||||||||||||||||
| 108 | ->structure($froody_method) | ||||||||||||||||||||||||||||
| 109 | ->set_bytes($bytes); | ||||||||||||||||||||||||||||
| 110 | ->as_xml; | ||||||||||||||||||||||||||||
| 111 | print ref($string); # prints "Froody::Response::XML" | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | =cut | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | # rendering this class | ||||||||||||||||||||||||||||
| 116 | # as_xml is documented | ||||||||||||||||||||||||||||
| 117 | 9 | ▶ | sub as_xml { return $_[0] } | ||||||||||||||||||||||||||
|
Subroutine
as_xml: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 118 | sub Froody::Response::as_xml | ||||||||||||||||||||||||||||
| 119 | 23 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
as_xml: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 120 | 23 | my $self = shift; | |||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 23 | my $parser = XML::LibXML->new(); | |||||||||||||||||||||||||||
| 123 | 23 | ▶ | my $rendered = $self->render | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 124 | or Froody::Error->throw('froody.invoke.badresponse', "No XML returned from call"); | ||||||||||||||||||||||||||||
| 125 | 23 | ▶ | my $doc = eval { $parser->parse_string($rendered) } or die "$rendered"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 22 | my $xml = Froody::Response::XML->new(); | |||||||||||||||||||||||||||
| 128 | 22 | $xml->xml($doc); | |||||||||||||||||||||||||||
| 129 | 22 | ▶ | $xml->structure($self->structure) if $self->structure; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | 22 | return $xml; | |||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | =head1 BUGS | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | None known. | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | Please report any bugs you find via the CPAN RT system. | ||||||||||||||||||||||||||||
| 140 | L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Froody> | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | Copyright Fotango 2005. All rights reserved. | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | Please see the main L<Froody> documentation for details of who has worked | ||||||||||||||||||||||||||||
| 147 | on this project. | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | This module is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||
| 150 | the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | L<Froody>, L<Froody::Response> | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | =cut | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | 25 | 1; | |||||||||||||||||||||||||||