blib/lib/XML/Generator/XMPP.pm

filter: Statementstmt 17.1% 12 / 70 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 33.3% 3 / 9 Podpod 100% 4 / 4 totaltotal 16.6% 19 / 114 CC 16 SCAR 48.2
CC
16
Cov
12%
_handle_node
210.0
nodes
6.0
iq
6.0
CRAP 123.9
↑ Summary
1 package XML::Generator::XMPP;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 ▶use base qw(XML::SAX::Base);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 our $VERSION='0.02';
8  
9 =head1 NAME
10  
11 XML::Generator::XMPP - easily create XMPP packets
12  
13 =head1 SYNOPSIS
14  
15 my $builder = XML::SAX::IncrementalBuilder::LibXML->new(detach => 1);
16 my $xpc = XML::LibXML::XPathContext->new;
17 my $gen = XML::Generator::XMPP(
18 Handler => $builder,
19 XPC => $xpc,
20 Server => 'jabber.org',
21 );
22  
23 my $client = 'cl';
24 $xpc->registerNs($client, 'jabber:client');
25  
26 $x->start;
27  
28 $x->nodes([
29 "$client:presence" => [],
30 ]);
31  
32 $x->end;
33  
34 while (my $node = $builder->get_node()) {
35 print SOCKET $node->toString;
36 }
37  
38 =head1 DESCRIPTION
39  
40 XML::Generator::XMPP uses L<XML::SAX::IncrementalBuilder::LibXML> and
41 L<XML::LibXML::XPathContext> to create XML nodes for the XMPP packets
42 you want to send.
43  
44 As you can see in the synopsis, you use start(), end() and nodes() to
45 describe what nodes to create, and then ask the
46 L<XML::SAX::IncrementalBuilder::LibXML> object for the generated nodes.
47 the 'incremental' means you can do this at any time, so you don't need
48 to wait till after end() (which would have made this unworkable).
49  
50 IncrementalBuilder generates nodes that are suitable for printing into
51 your socket verbatim. That means the while loop from the L<SYNOPSIS>
52 would write a valid XMPP stream into the socket.
53  
54 =head1 CONSTRUCTOR
55  
56 =head2 new
57  
58 Creates a new instance. This has three named parameters, which are all
59 required:
60  
61 =over 2
62  
63 =item Handler
64  
65 The SAX builder object. Theoretically, this could be any XML::SAX class
66 that generates DOM fragments with L<XML::LibXML::Node> objects, but the
67 only real option in L<XML::SAX::IncrementalBuilder::LibXML>.
68  
69 =item XPC
70  
71 An L<XML::LibXML::XPathContext> object. This is used to get the namespaces
72 right. You should register any namespace you plan to use with this object.
73  
74 =item Server
75  
76 The name of the XMPP server you are connecting to. This is used in the
77 start() method to generate a correct <stream:stream> element.
78  
79 =back
80  
81 =head1 METHODS
82  
83 =head2 start
84  
85 Generates the initial <stream:stream> element that starts the XML stream
86  
87 =cut
88  
89 0 ▶sub start {
Subroutine start: not called CC=1 SCAR=6.9
Pod documented
90 0 my $self = shift;
91 0 $self->{Handler}->reset();
92 0 $self->start_document({});
93 0 $self->xml_decl({Version => '1.0'});
94 0 $self->start_prefix_mapping({Prefix => 'stream', NamespaceURI => 'http://etherx.jabber.org/streams'});
95 0 $self->start_prefix_mapping({Prefix => '', NamespaceURI => 'jabber:client'});
96 $self->start_element({
97 Name => 'stream:stream',
98 LocalName => 'stream',
99 Prefix => 'stream',
100 NamespaceURI => 'http://etherx.jabber.org/streams',
101 Attributes => {
102 to => $self->{Server},
103 0 version => '1.0',
104 },
105 });
106 }
107  
108 1 my $count = 0;
109 0 ▶sub _generate_id {
Subroutine _generate_id: not called CC=1 SCAR=6.9
110 0 return "id-" . $count++;
111 }
112  
113 0 ▶sub _handle_node {
Subroutine _handle_node: not called CC=14 SCAR=53.5
114 0 my ($self, $node, $data) = @_;
115  
116 0 my $nss = $self->{Handler}->{NamespaceStack};
117 0 $nss->push_context;
118  
119 0 my ($ns, $name) = split (':', $node);
120 0 my ($ns_uri, $new_mapping, $el_data);
121 0 ▶ if ($ns_uri = $nss->get_uri($ns)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ns_uri = $nss->get_uri($ns)) { } 0 0 0
122 0 ▶ my $fullname = ($ns ? "$ns:" : '') . $name;
Branch: 1 branch
BranchTrueFalseTotal
$ns ? : 0 0 0
123 0 $el_data = {Name => $fullname, LocalName => $name, Prefix => $ns, NamespaceURI => $ns_uri};
124 } else {
125 0 $new_mapping = 1;
126 0 $ns_uri = $self->{XPC}->lookupNs($ns);
127 0 $nss->declare_prefix($ns, $ns_uri); # declare
128 0 ▶ if ($nss->get_uri('') ne $ns_uri) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nss->get_uri('') ne $ns_uri) { } 0 0 0
129 0 $nss->declare_prefix('', $ns_uri); # and also set new default
130 0 $self->start_prefix_mapping({Prefix => $ns, NamespaceURI => $ns_uri});
131 0 $el_data = {Name => $name, NamespaceURI => $ns_uri};
132 } else {
133 0 $el_data = {Name => $name, Prefix => ''};
134 }
135 0 $nss->push_context;
136 }
137 0 ▶ if (@$data and $data->[0] eq 'Attributes') {
Branch: 1 branch
BranchTrueFalseTotal
if (@$data and $data->[0] eq "Attributes") 0 0 0
Condition0%0/3
@$data and $data->[0] eq "Attributes"
!ll&&!rl&&r
000
MC/DC0%0/2
@$data and $data->[0] eq "Attributes"
@$data $data->[0] eq "Attributes"
Truth table0%0/3
A and B
A= @$dataB= $data->[0] eq "Attributes"
ABresult
0X0
100
111
138 0 my ($attr, $list) = splice (@$data, 0, 2);
139 0 foreach my $fullname (keys %$list) {
140 0 my ($attr_ns, $name) = split (':', $fullname);
141 0 ▶ if ($attr_ns eq $ns) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attr_ns eq $ns) 0 0 0
142 # default attribute namespace is the namespace of the element.
143 0 $list->{$name} = delete $list->{$fullname};
144 }
145 }
146 0 $el_data->{$attr} = $list;
147 }
148 0 my $returnnode = $self->start_element($el_data);
149 0 ▶ while (@$data) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$data) 0 0 0
150 0 my ($type, $data) = splice (@$data, 0, 2);
151  
152 0 ▶ if ($type eq 'Text') {
Branch: 3 branches
BranchTrueFalseTotal
if ($type eq 'Text') { } 0 0 0
elsif ($type eq 'Child') { } 0 0 0
elsif ($type eq 'Subtree') { } 0 0 0
153 0 $self->characters({Data => $data});
154 } elsif ($type eq 'Child') {
155 0 ▶ while (@$data) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$data) 0 0 0
156 0 $self->_handle_node(splice (@$data, 0, 2));
157 }
158 } elsif ($type eq 'Subtree') {
159 0 $returnnode->appendChild($data);
160 } else {
161 0 die "unknown $type";
162 }
163 }
164 0 $self->end_element($el_data);
165 0 ▶ if ($new_mapping) {
Branch: 1 branch
BranchTrueFalseTotal
if ($new_mapping) 0 0 0
166 0 $self->end_prefix_mapping({Prefix => $ns});
167 0 $nss->pop_context;
168 }
169 0 $nss->pop_context;
170 }
171  
172 =head2 nodes
173  
174 Creates XML nodes from the list you pass it. The basic syntax is as follows
175  
176 $generator->nodes( [
177 "nsprefix:nodename" => [ ..list of options.. ]
178 ] );
179  
180 The namespace prefix is required and must be registered with the XPathContext
181 you have passed into the constructor.
182  
183 The list of options for a node is essentially a hash, but written as a list
184 so you can have more than one of the same 'key'. The following keys are
185 allowed:
186  
187 =over 2
188  
189 =item Attributes => {'nsprefix:attr_name' => 'value'}
190  
191 If present, this must be the first in the list. Again, the namespace
192 prefix is mandatory, and must be registered with the XPathContext.
193  
194 =item Text => 'text'
195  
196 Creates a Text node.
197  
198 =item Child => [ 'nsprefix:nodename' => [ ... ], ... ]
199  
200 Create one or more child nodes. The same syntax as for the main nodes
201 list applies.
202  
203 =item Subtree => XML::LibXML::Element->new('foo')
204  
205 Add an XML::LibXML::Element child node. This can be useful if you are
206 using some module that generates some XML for you, and you need to put
207 it inside an XMPP packet. Note that you are responsible for making sure
208 that the namespaces are correct.
209  
210 =back
211  
212 As you can see in the L</SYNOPSIS>, leaving the list empty is valid
213 behaviour. This creates an empty node.
214  
215 =cut
216  
217 0 ▶sub nodes {
Subroutine nodes: not called CC=2 SCAR=17.9
Pod documented
218 0 my ($self, $nodelist) = @_;
219  
220 0 ▶ while (@$nodelist) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$nodelist) 0 0 0
221 0 $self->_handle_node(splice (@$nodelist, 0, 2));
222 }
223 }
224  
225 =head2 end
226  
227 Generates the closing </stream:stream> element that closes the XML stream.
228 =cut
229  
230 0 ▶sub end {
Subroutine end: not called CC=1 SCAR=6.9
Pod documented
231 0 my $self = shift;
232  
233 0 $self->end_element({Name => 'stream:stream', LocalName => 'stream', Prefix => 'stream'});
234 0 $self->end_prefix_mapping({Prefix => 'stream', NamespaceURI => 'http://etherx.jabber.org/streams'});
235 0 $self->end_prefix_mapping({Prefix => ''});
236 0 $self->end_document({});
237 }
238  
239 =head1 CONVENIENCE METHODS
240  
241 The following are methods you can use to ease the construction of
242 the structure you pass to the nodes() method. They return a listref
243 you can pass into nodes() as the top listref it expects.
244  
245 $self->nodes( $self->iq(...) );
246  
247 =cut
248  
249 =head2 iq TYPE, TO, CHILDREN
250  
251 $iq = $self->iq(get => 'someone@example.com', [
252 "disco:query" => [],
253 ],
254 )
255  
256 Creates the structure for an 'iq' element. The TYPE and TO parameters
257 should contain the content of the attributes with the corresponding
258 name, and CHILDREN is a listref containing zero or more child
259 descriptions in the same format as you use in nodes()
260  
261 =cut
262  
263 0 ▶sub iq {
Subroutine iq: not called CC=2 SCAR=17.9
Pod documented
264 0 my ($self, $type, $destination, $child) = @_;
265  
266 0 my $attr = {
267 ':type' => $type,
268 ':id' => _generate_id(),
269 };
270 0 ▶ $attr->{':to'} = $destination if $destination;
Branch: 1 branch
BranchTrueFalseTotal
if $destination 0 0 0
271  
272 0 return [":iq" => [
273 Attributes => $attr,
274 Child => $child,
275 ]
276 ];
277 }
278  
279 1 1;
280 __END__
↑ Summary