| 1 | package Mojolicious::Plugin::XML::LX; | ||
| 2 | 1 | ▶ | use Mojo::Base 'Mojolicious::Plugin'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | |||
| 4 | 1 | ▶ | use 5.006; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | |||
| 8 | our $VERSION = '1.0'; | ||
| 9 | |||
| 10 | 1 | ▶ | use XML::LibXML; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | 0 | ▶ | use XML::Hash::LX qw(hash2xml); |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 12 | |||
| 13 | use Encode qw(decode_utf8); | ||
| 14 | use Mojo::Util qw(camelize xml_escape); | ||
| 15 | |||
| 16 | sub register { | ||
| 17 | my ($self, $app, $conf) = @_; | ||
| 18 | |||
| 19 | $conf ||= {}; | ||
| 20 | |||
| 21 | # Add XML type if not exists | ||
| 22 | $app->types->type(xml => 'application/xml') | ||
| 23 | unless $app->types->type('xml'); | ||
| 24 | |||
| 25 | # http://mojolicious.org/perldoc/Mojolicious/Guides/Rendering | ||
| 26 | # #Adding-a-handler-to-generate-binary-data | ||
| 27 | |||
| 28 | # Add XML handler | ||
| 29 | $app->renderer->add_handler(xml => sub { | ||
| 30 | my ($renderer, $c, $output, $options) = @_; | ||
| 31 | |||
| 32 | my %opts = %$conf; | ||
| 33 | $opts{encoding} = $options->{encoding} if $options->{encoding}; | ||
| 34 | |||
| 35 | my $data = delete $c->stash->{xml}; | ||
| 36 | my $dom = hash2xml $data, doc => 1, %opts; | ||
| 37 | $$output = $dom->toString( 2 ); | ||
| 38 | }); | ||
| 39 | |||
| 40 | # Automatic apply XML handler | ||
| 41 | $app->hook(before_render => sub { | ||
| 42 | my ($c, $args) = @_; | ||
| 43 | $args->{handler} = 'xml' | ||
| 44 | if exists $args->{xml} || exists $c->stash->{xml}; | ||
| 45 | }); | ||
| 46 | } | ||
| 47 | |||
| 48 | =head1 NAME | ||
| 49 | |||
| 50 | Mojolicious::Plugin::XML::LX - is a plugin | ||
| 51 | to support simple XML response from HASH. | ||
| 52 | |||
| 53 | |||
| 54 | =head1 SYNOPSIS | ||
| 55 | |||
| 56 | # Mojolicious | ||
| 57 | $app->plugin('XML::LX'); | ||
| 58 | |||
| 59 | # Mojolicious::Lite | ||
| 60 | plugin 'XML::LX'; | ||
| 61 | |||
| 62 | # Controller | ||
| 63 | $self->render(xml => { | ||
| 64 | response => { | ||
| 65 | -status => 'ok', | ||
| 66 | message => 'hello world!', | ||
| 67 | } | ||
| 68 | }); | ||
| 69 | |||
| 70 | # You get: | ||
| 71 | |||
| 72 | <?xml version="1.0" encoding="utf-8"?> | ||
| 73 | <response status="ok"> | ||
| 74 | <message>hello world!</message> | ||
| 75 | </response> | ||
| 76 | |||
| 77 | |||
| 78 | =head1 DESCRIPTION | ||
| 79 | |||
| 80 | L<Mojolicious::Plugin::XML::LX> based on L<XML::Hash::LX> | ||
| 81 | companion for L<XML::LibXML>. | ||
| 82 | |||
| 83 | All configuration parameters apply to L<XML::Hash::LX::hash2xml()>. | ||
| 84 | |||
| 85 | =head1 AUTHOR | ||
| 86 | |||
| 87 | Roman V. Nikolaev, C<< <rshadow at rambler.ru> >> | ||
| 88 | |||
| 89 | =head1 BUGS | ||
| 90 | |||
| 91 | Please report any bugs or feature requests to C<bug-mojolicious-plugin-xml-lx at rt.cpan.org>, or through | ||
| 92 | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Mojolicious-Plugin-XML-LX>. I will be notified, and then you'll | ||
| 93 | automatically be notified of progress on your bug as I make changes. | ||
| 94 | |||
| 95 | |||
| 96 | |||
| 97 | |||
| 98 | =head1 SUPPORT | ||
| 99 | |||
| 100 | You can find documentation for this module with the perldoc command. | ||
| 101 | |||
| 102 | perldoc Mojolicious::Plugin::XML::LX | ||
| 103 | |||
| 104 | |||
| 105 | You can also look for information at: | ||
| 106 | |||
| 107 | =over 4 | ||
| 108 | |||
| 109 | =item * RT: CPAN's request tracker (report bugs here) | ||
| 110 | |||
| 111 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Mojolicious-Plugin-XML-LX> | ||
| 112 | |||
| 113 | =item * AnnoCPAN: Annotated CPAN documentation | ||
| 114 | |||
| 115 | L<http://annocpan.org/dist/Mojolicious-Plugin-XML-LX> | ||
| 116 | |||
| 117 | =item * CPAN Ratings | ||
| 118 | |||
| 119 | L<http://cpanratings.perl.org/d/Mojolicious-Plugin-XML-LX> | ||
| 120 | |||
| 121 | =item * Search CPAN | ||
| 122 | |||
| 123 | L<http://search.cpan.org/dist/Mojolicious-Plugin-XML-LX/> | ||
| 124 | |||
| 125 | =back | ||
| 126 | |||
| 127 | |||
| 128 | =head1 ACKNOWLEDGEMENTS | ||
| 129 | |||
| 130 | |||
| 131 | =head1 LICENSE AND COPYRIGHT | ||
| 132 | |||
| 133 | Copyright 2016 Roman V. Nikolaev. | ||
| 134 | |||
| 135 | This program is free software; you can redistribute it and/or modify it | ||
| 136 | under the terms of the the Artistic License (2.0). You may obtain a | ||
| 137 | copy of the full license at: | ||
| 138 | |||
| 139 | L<http://www.perlfoundation.org/artistic_license_2_0> | ||
| 140 | |||
| 141 | Any use, modification, and distribution of the Standard or Modified | ||
| 142 | Versions is governed by this Artistic License. By using, modifying or | ||
| 143 | distributing the Package, you accept this license. Do not use, modify, | ||
| 144 | or distribute the Package, if you do not accept this license. | ||
| 145 | |||
| 146 | If your Modified Version has been derived from a Modified Version made | ||
| 147 | by someone other than you, you are nevertheless required to ensure that | ||
| 148 | your Modified Version complies with the requirements of this license. | ||
| 149 | |||
| 150 | This license does not grant you the right to use any trademark, service | ||
| 151 | mark, tradename, or logo of the Copyright Holder. | ||
| 152 | |||
| 153 | This license includes the non-exclusive, worldwide, free-of-charge | ||
| 154 | patent license to make, have made, use, offer to sell, sell, import and | ||
| 155 | otherwise transfer the Package with respect to any patent claims | ||
| 156 | licensable by the Copyright Holder that are necessarily infringed by the | ||
| 157 | Package. If you institute patent litigation (including a cross-claim or | ||
| 158 | counterclaim) against any party alleging that the Package constitutes | ||
| 159 | direct or contributory patent infringement, then this Artistic License | ||
| 160 | to you shall terminate on the date that such litigation is filed. | ||
| 161 | |||
| 162 | Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER | ||
| 163 | AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. | ||
| 164 | THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR | ||
| 165 | PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY | ||
| 166 | YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR | ||
| 167 | CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR | ||
| 168 | CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE, | ||
| 169 | EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| 170 | |||
| 171 | |||
| 172 | =cut | ||
| 173 | |||
| 174 | 1; # End of Mojolicious::Plugin::XML::LX | ||