| 1 | package Slackware::Slackget::Network::Backend::XML; | ||
| 2 | |||
| 3 | use warnings; | ||
| 4 | use strict; | ||
| 5 | require Slackware::Slackget::Network::Message ; | ||
| 6 | require XML::Simple; | ||
| 7 | require Data::Dumper; | ||
| 8 | |||
| 9 | =head1 NAME | ||
| 10 | |||
| 11 | Slackware::Slackget::Network::Backend::XML - XML backend for slack-get network protocol | ||
| 12 | |||
| 13 | =head1 VERSION | ||
| 14 | |||
| 15 | Version 0.9.0 | ||
| 16 | |||
| 17 | =cut | ||
| 18 | |||
| 19 | our $VERSION = '0.9.0'; | ||
| 20 | |||
| 21 | =head1 SYNOPSIS | ||
| 22 | |||
| 23 | This module implements the XML backend for slack-get (sg_daemon, slack-get and more generally all Perl written parts of the slack-get project). | ||
| 24 | |||
| 25 | You should not use this module directly but you should read this documentation ;-) | ||
| 26 | |||
| 27 | This backend is a low-level one, it means that it needs access to the data structure of the Slackware::Slackget::Network::Message object it will encode (or decode but it's less critical in the decoding process). A bad idea is to encode the message with another backend which make the data structure unreadable by this one, before calling this XML backend (the Base64 backend for example)... | ||
| 28 | |||
| 29 | You should *always* remember this fact in your development. If backend_encode() cannot access to the data structure you can expect some funny behaviors... | ||
| 30 | |||
| 31 | =cut | ||
| 32 | |||
| 33 | sub new | ||
| 34 | { | ||
| 35 | my ($class,%args) = @_ ; | ||
| 36 | my $self = {%args}; | ||
| 37 | bless($self,$class); | ||
| 38 | return $self; | ||
| 39 | } | ||
| 40 | |||
| 41 | =head1 DEBUG | ||
| 42 | |||
| 43 | This module is affected by the envirronement variable SG_DAEMON_DEBUG. If it's set to a true value, it will output some debug messages. | ||
| 44 | |||
| 45 | =head1 CONSTRUCTOR | ||
| 46 | |||
| 47 | =head2 new | ||
| 48 | |||
| 49 | Still to do | ||
| 50 | |||
| 51 | =head1 FUNCTIONS | ||
| 52 | |||
| 53 | All methods return a Slackware::Slackget::Network::Message (L<Slackware::Slackget::Network::Message>) object, and if the remote slack-getd return some data they are accessibles via the data() accessor of the Slackware::Slackget::Network::Message object. | ||
| 54 | |||
| 55 | =cut | ||
| 56 | |||
| 57 | =head2 backend_decode | ||
| 58 | |||
| 59 | =cut | ||
| 60 | |||
| 61 | sub backend_decode { | ||
| 62 | my $self = shift; | ||
| 63 | my $xml_msg = shift; | ||
| 64 | print "[Slackware::Slackget::Network::Backend::XML] call backend_decode($xml_msg).\n" if($ENV{SG_DAEMON_DEBUG}) ; | ||
| 65 | print "[Slackware::Slackget::Network::Backend::XML] for message $xml_msg, data are :\n".$xml_msg->data."\n" if($ENV{SG_DAEMON_DEBUG}) ; | ||
| 66 | my $data = XML::Simple::XMLin( $xml_msg->data, ForceArray => ['li'], ForceContent => 1 ); | ||
| 67 | delete($data->{version}); | ||
| 68 | return Slackware::Slackget::Network::Message->new( | ||
| 69 | action => $data->{Enveloppe}->{Action}->{content}, | ||
| 70 | action_id => $data->{Enveloppe}->{Action}->{id}, | ||
| 71 | raw_data => $data | ||
| 72 | ); | ||
| 73 | } | ||
| 74 | |||
| 75 | =head2 backend_encode | ||
| 76 | |||
| 77 | =cut | ||
| 78 | |||
| 79 | sub backend_encode { | ||
| 80 | my $self = shift; | ||
| 81 | my $message = shift ; | ||
| 82 | print "[Slackware::Slackget::Network::Backend::XML] call backend_encode($message).\n" if($ENV{SG_DAEMON_DEBUG}) ; | ||
| 83 | sub _data_to_string { | ||
| 84 | my $ref = shift; | ||
| 85 | my $str = ''; | ||
| 86 | foreach my $k ( keys(%{$ref}) ){ | ||
| 87 | my $end_tag=1; | ||
| 88 | if(ref($ref->{$k})){ | ||
| 89 | if( ref($ref->{$k}) eq 'ARRAY' ){ | ||
| 90 | $end_tag=0; | ||
| 91 | foreach my $ai (@{$ref->{$k}}){ | ||
| 92 | if(ref($ai) eq ''){ | ||
| 93 | $str .= "<$k>$ai</$k>\n"; | ||
| 94 | }else{ | ||
| 95 | $str .= "<$k>"._data_to_string($ai)."</$k>"; | ||
| 96 | } | ||
| 97 | } | ||
| 98 | } | ||
| 99 | elsif(defined($ref->{$k}->{'content'})){ | ||
| 100 | $str .= "<$k "; | ||
| 101 | foreach my $sk ( keys(%{$ref->{$k}}) ){ | ||
| 102 | next if($sk eq 'content'); | ||
| 103 | $str .= "$sk=\"$ref->{$k}->{$sk}\" "; | ||
| 104 | } | ||
| 105 | $str .= ">$ref->{$k}->{'content'}"; | ||
| 106 | }else{ | ||
| 107 | $str .= "<$k>\n"; | ||
| 108 | $str .= _data_to_string($ref->{$k}); | ||
| 109 | } | ||
| 110 | } | ||
| 111 | $str .= "</$k>\n" if($end_tag); | ||
| 112 | } | ||
| 113 | return $str; | ||
| 114 | } | ||
| 115 | |||
| 116 | my $xml = "<?xml version=\"1.0\" ?>\n<SlackGetProtocol version=\"".Slackware::Slackget::Network::SLACK_GET_PROTOCOL_VERSION."\">\n"; | ||
| 117 | $xml .= _data_to_string($message->data()); | ||
| 118 | $xml .= "</SlackGetProtocol>\n"; | ||
| 119 | print "[Slackware::Slackget::Network::Backend::XML] encoded XML:\n$xml\n" if($ENV{SG_DAEMON_DEBUG}); | ||
| 120 | return Slackware::Slackget::Network::Message->new( | ||
| 121 | action => $message->data()->{Enveloppe}->{Action}->{content}, | ||
| 122 | action_id => $message->data()->{Enveloppe}->{Action}->{id}, | ||
| 123 | raw_data => $xml, | ||
| 124 | ); | ||
| 125 | } | ||
| 126 | |||
| 127 | |||
| 128 | |||
| 129 | =head1 AUTHOR | ||
| 130 | |||
| 131 | DUPUIS Arnaud, C<< <a.dupuis@infinityperl.org> >> | ||
| 132 | |||
| 133 | =head1 BUGS | ||
| 134 | |||
| 135 | Please report any bugs or feature requests to | ||
| 136 | C<bug-Slackware-Slackget@rt.cpan.org>, or through the web interface at | ||
| 137 | L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Slackware-Slackget>. | ||
| 138 | I will be notified, and then you'll automatically be notified of progress on | ||
| 139 | your bug as I make changes. | ||
| 140 | |||
| 141 | =head1 SUPPORT | ||
| 142 | |||
| 143 | You can find documentation for this module with the perldoc command. | ||
| 144 | |||
| 145 | perldoc Slackware::Slackget | ||
| 146 | |||
| 147 | |||
| 148 | You can also look for information at: | ||
| 149 | |||
| 150 | =over 4 | ||
| 151 | |||
| 152 | =item * Infinity Perl website | ||
| 153 | |||
| 154 | L<http://www.infinityperl.org/category/slack-get> | ||
| 155 | |||
| 156 | =item * slack-get specific website | ||
| 157 | |||
| 158 | L<http://slackget.infinityperl.org> | ||
| 159 | |||
| 160 | =item * RT: CPAN's request tracker | ||
| 161 | |||
| 162 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Slackware-Slackget> | ||
| 163 | |||
| 164 | =item * AnnoCPAN: Annotated CPAN documentation | ||
| 165 | |||
| 166 | L<http://annocpan.org/dist/Slackware-Slackget> | ||
| 167 | |||
| 168 | =item * CPAN Ratings | ||
| 169 | |||
| 170 | L<http://cpanratings.perl.org/d/Slackware-Slackget> | ||
| 171 | |||
| 172 | =item * Search CPAN | ||
| 173 | |||
| 174 | L<http://search.cpan.org/dist/Slackware-Slackget> | ||
| 175 | |||
| 176 | =back | ||
| 177 | |||
| 178 | =head1 ACKNOWLEDGEMENTS | ||
| 179 | |||
| 180 | Thanks to Bertrand Dupuis (yes my brother) for his contribution to the documentation. | ||
| 181 | |||
| 182 | =head1 SEE ALSO | ||
| 183 | |||
| 184 | L<Slackware::Slackget::Network::Message>, L<Slackware::Slackget::Status>, L<Slackware::Slackget::Network::Connection> | ||
| 185 | |||
| 186 | =head1 COPYRIGHT & LICENSE | ||
| 187 | |||
| 188 | Copyright 2005 DUPUIS Arnaud, All Rights Reserved. | ||
| 189 | |||
| 190 | This program is free software; you can redistribute it and/or modify it | ||
| 191 | under the same terms as Perl itself. | ||
| 192 | |||
| 193 | =cut | ||
| 194 | |||
| 195 | 1; # End of Slackware::Slackget::Network::Backend::XML |