File Coverage

blib/lib/Catalyst/View/Download/XML.pm
Criterion Covered Total %
statement 17 23 73.9
branch 1 4 25.0
condition 1 3 33.3
subroutine 4 5 80.0
pod 2 2 100.0
total 25 37 67.5


line stmt bran cond sub pod time code
1             package Catalyst::View::Download::XML;
2              
3 1     1   1228 use Moose;
  1         1  
  1         7  
4 1     1   4689 use namespace::autoclean;
  1         2  
  1         8  
5              
6             extends 'Catalyst::View';
7              
8 1     1   977 use XML::Simple;
  1         6200  
  1         5  
9              
10             =head1 NAME
11              
12             Catalyst::View::Download::XML
13              
14             =head1 VERSION
15              
16             0.04
17              
18             =cut
19              
20             our $VERSION = "0.04";
21             $VERSION = eval $VERSION;
22              
23             __PACKAGE__->config( 'stash_key' => 'xml' );
24              
25             sub process {
26 0     0 1 0 my $self = shift;
27 0         0 my ($c) = @_;
28              
29 0         0 my $template = $c->stash->{ 'template' };
30 0         0 my $content = $self->render( $c, $template, $c->stash );
31              
32 0 0       0 $c->res->headers->header( "Content-Type" => "text/xml" )
33             if ( $c->res->headers->header("Content-Type") eq "" );
34 0         0 $c->res->body( $content );
35             }
36              
37             sub render {
38 1     1 1 706 my $self = shift;
39 1         2 my ( $c, $template, $args ) = @_;
40              
41 1         18 my $stash_key = $self->config->{ 'stash_key' };
42 1   33     46 my $content = $c->stash->{ $stash_key } || $c->response->body;
43              
44 1 50       71 if(ref($content) =~ /HASH/) {
45 1         12 my $xs = new XML::Simple( keeproot => 1, XMLDecl => "<?xml version='1.0' ?>" );
46 1         73 $content = $xs->XMLout( $content );
47             }
48              
49 1         297 return $content;
50             }
51              
52             __PACKAGE__->meta->make_immutable;
53              
54             1;
55              
56             __END__
57              
58             =head1 SYNOPSIS
59              
60             # lib/MyApp/View/Download/XML.pm
61             package MyApp::View::Download::XML;
62             use base qw( Catalyst::View::Download::XML );
63             1;
64              
65             # lib/MyApp/Controller/SomeController.pm
66             sub example_action_1 : Local {
67             my ($self, $c) = @_;
68              
69             # supported content can be an xml document
70             my $content = "<?xml version="1.0"?>\n<root>\n<text>Some Text\n</text>\n</root>";
71              
72             # supported content can also be a hashref which will converted into xml
73             # using XMLout from L<XML::Simple>
74             $content = {
75             'root' => {
76             'text' => 'Some Text'
77             }
78             };
79              
80             # To output your data just pass your content into the 'xml' key of the stash
81             $c->stash->{'xml'} = $content;
82              
83             # Or into the body of the response for this action
84             $c->response->body($content);
85              
86             # Finally forward processing to the XML View
87             $c->forward('MyApp::View::Download::XML');
88             }
89              
90             =head1 DESCRIPTION
91              
92             Takes content and outputs the content as html text.
93              
94             =head1 SUBROUTINES
95              
96             =head2 process
97              
98             This method will be called by Catalyst if it is asked to forward to a component
99             without a specified action.
100              
101             =head2 render
102              
103             Allows others to use this view for much more fine-grained content generation.
104              
105             =head3 XMLout
106              
107             If a hashref is passed as content it will be converted using the XMLout
108             function from the L<XML::Simple> module. The following options are used to
109             instantiate the L<XML::Simple> object.
110              
111             =over
112              
113             =item keeproot => 1
114              
115             =item XMLDecl => "<?xml version='1.0' ?>"
116              
117             =back
118              
119             =head1 CONFIG
120              
121             =over
122              
123             =item stash_key
124              
125             Determines the key in the stash this view will look for when attempting to
126             retrieve content to process. If this key isn't found it will then look at
127             $c->response->body for content.
128              
129             $c->view('MyApp::View::Download::XML')->config->{'stash_key'} = 'content';
130              
131             =back
132              
133             =head1 AUTHOR
134              
135             Travis Chase, C<< <gaudeon at cpan dot org> >>
136              
137             =head1 SEE ALSO
138              
139             L<Catalyst> L<Catalyst::View> L<Catalyst::View::Download> L<XML::Simple>
140              
141             =head1 LICENSE
142              
143             This program is free software. You can redistribute it and/or modify it
144             under the same terms as Perl itself.
145              
146             =cut