blib/lib/XML/Writer/Lazy.pm

filter: Statementstmt 36.8% 63 / 171 Branchbran 12.5% 2 / 16 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 32.5% 14 / 43 Podpod 100% 3 / 3 totaltotal 34.7% 82 / 236 SCAR 38.1
CC
11
Cov
34%
AUTOLOAD
20.0
xml_decl
12.0
start_element
12.0
CRAP 45.2
↑ Summary
1 package XML::Writer::Lazy;
2 1 $XML::Writer::Lazy::VERSION = '0.04';
3 =head1 NAME
4  
5 XML::Writer::Lazy - Pass stringified XML to XML::Writer
6  
7 =head1 VERSION
8  
9 version 0.04
10  
11 =head1 DESCRIPTION
12  
13 Pass stringified XML to XML::Writer
14  
15 =head1 SYNOPSIS
16  
17 my $writer = XML::Writer::Lazy->new( OUTPUT => 'self');
18 my $title = "My Title!";
19  
20 $writer->lazily(<<"XML");
21 <html>
22 <head>
23 <title>$title</title>
24 </head>
25 <body>
26 <p>Pipe in literal XML</p>
27 XML
28  
29 $writer->startTag( "p", "class" => "simple" );
30 $writer->characters("Alongside the usual interface");
31 $writer->characters("123456789");
32 $writer->lazily("</p></body></html>");
33  
34 =head1 WHY
35  
36 This is 2016. The computer should do the hard work. Life's too short to write
37 a bunch of C<startTag> and C<endTag> when my computer's perfectly capable of
38 figuring out the right thing to do if I give it a chunk of XML.
39  
40 =head1 HOW
41  
42 Using a SAX parser whose events are then passed back to XML::Writer.
43  
44 =head1 METHODS
45  
46 B<This is a subclass of> L<XML::Writer>.
47  
48 =cut
49  
50 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
51 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
52 1 ▶use base 'XML::Writer';
Subroutine BEGIN: called CC=1 SCAR=0.0
53 1 ▶use XML::SAX;
Subroutine BEGIN: called CC=1 SCAR=0.0
54  
55 #
56 # The SAX ChunkParser's input, and XML::Writer's output need to be kept in
57 # sync, because the ChunkParser still expects well-formed XML eventually.
58 #
59 # So that means there are two main modes here:
60 # - When `lazily` is being used, SAX ChunkParser is being used explicitly to
61 # drive XML::Writer, and this keeps it in sync
62 # - When XML::Writer's methods are being used directly, we intercept calls
63 # to `print`, and add that to a buffer that's fed to the ChunkParser each
64 # time before it's used again
65 #
66  
67 1 my $KEY = '_XML_Writer_Lazy_Parser';
68  
69 =head2 new
70  
71 Call's L<XML::Writer>'s C<new()> and then instantiates the lazy
72 parser pieces. Accepts all the same arguments as the parent method.
73  
74 =cut
75  
76 sub new {
77 1 ▶ my $classname = shift;
Subroutine new: called CC=1 SCAR=0.0
Pod documented
78 1 my $self = $classname->SUPER::new(@_);
79  
80 # Create the parser
81 1 my $parser;
82 {
83 1 local $XML::SAX::ParserPackage = 'XML::LibXML::SAX::ChunkParser';
84 1 $parser = XML::SAX::ParserFactory->parser(
85 Handler => XML::Writer::Lazy::Handler->new );
86 }
87  
88 # And the buffer...
89 1 my $buffer = '';
90  
91 # Save them both in the parent object
92 1 $self->{$KEY} = {
93 parser => $parser,
94 buffer => $buffer,
95 };
96  
97 # Capture anything print()'ed via XML::Writer
98 1 $self->wrap_output();
99 1 return $self;
100 }
101  
102 1 my $null_handler = bless {}, 'XML::Writer::Lazy::NullHandler';
103  
104 =head2 lazily
105  
106 Take a string of XML. It should be parseable, although doesn't need to be
107 balanced. C<< <foo><bar>asdf >> is fine, where C<< <foo >> is not. Exercises
108 the XML::Writer methods appropriately to re-create whatever you'd passed in.
109  
110 =cut
111  
112 sub lazily {
113 1 ▶ my ( $self, $string, $writer ) = @_;
Subroutine lazily: called CC=3 SCAR=13.7
Pod documented
114  
115 # Set the writer object that the Handler is using
116 1 ▶ local $XML::Writer::Lazy::Handler::writer
Branch: 1 branch
BranchTrueFalseTotal
defined $writer ? : 0 1 1
117 = ( defined $writer ) ? $writer : $self;
118  
119 # Whether or not we might be trying to print an XML dec
120 1 local $XML::Writer::Lazy::Handler::xml_dec
121 = ( $string =~ m/^(\xEF\xBB\xBF)?<\?xml/i );
122  
123 # First thing we do is look at anything that was output directly by
124 # XML::Writer, and pass that to the Chunk Parser
125 1 ▶ if ( length $self->{$KEY}->{'buffer'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $self->{$KEY}{"buffer"}) 0 1 1
126  
127 # Save a copy of the buffer, and then nuke the buffer
128 0 my $directly = $self->{$KEY}->{'buffer'};
129 0 $self->{$KEY}->{'buffer'} = '';
130  
131 # Re-enter this sub with the buffer as the argument
132 0 $self->lazily( $directly, $null_handler );
133 }
134  
135 {
136 # Turn off buffer collection
137 1 local $XML::Writer::Lazy::InterceptPrint::intercept = 0;
138  
139 # Push in the user's string
140 1 $self->{$KEY}->{'parser'}->parse_chunk($string);
141  
142 # Flush using a comment
143 0 local $XML::Writer::Lazy::Handler::writer = $null_handler;
144 0 $self->{$KEY}->{'parser'}->parse_chunk("<!-- -->");
145 }
146  
147 0 return $self;
148 }
149  
150 =head2 wrap_output
151  
152 Only important if you're doing strange things with the C<OUTPUT> after
153 instantiation. In order to keep track of what's been written already, this
154 class wraps the C<OUTPUT> object inside a delegate that intercepts and stores
155 the contents of C<print>. If you -- post instantiation -- replace the output
156 object, you can call this method to rewrap it. It will change the class that
157 that object belongs to.
158  
159 =cut
160  
161 sub wrap_output {
162 1 ▶ my $self = shift;
Subroutine wrap_output: called CC=1 SCAR=0.0
Pod documented
163 1 $self->setOutput(
164 XML::Writer::Lazy::InterceptPrint->___wrap(
165 $self->getOutput(), $self
166 )
167 );
168  
169 1 return $self;
170 }
171  
172 =head1 AUTHOR
173  
174 Peter Sergeant - C<pete@clueball.com>
175  
176 L<https://github.com/pjlsergeant/p5-xml-writer-lazy>
177  
178 =head1 LICENSE
179  
180 MIT - see the C<LICENSE> file included in the tar.gz of this distribution.
181  
182 =cut
183  
184 # I capture output that's produced by calling XML::Writer's methods directly
185 package XML::Writer::Lazy::InterceptPrint;
186 1 $XML::Writer::Lazy::InterceptPrint::VERSION = '0.04';
187 1 our $intercept = 1;
188  
189 1 ▶use vars '$AUTOLOAD';
Subroutine BEGIN: called CC=1 SCAR=0.0
190 1 ▶use Scalar::Util qw/weaken/;
Subroutine BEGIN: called CC=1 SCAR=0.0
191  
192 sub ___wrap {
193 1 ▶ my ( $classname, $delegate, $me ) = @_;
Subroutine ___wrap: called CC=1 SCAR=0.0
194 1 weaken $delegate;
195 1 weaken $me;
196 1 return bless [ $delegate, $me ], $classname;
197 }
198  
199 sub AUTOLOAD {
200 0 ▶ my $self = shift;
Subroutine AUTOLOAD: not called CC=4 SCAR=30.0
201  
202 0 my ($sub) = $AUTOLOAD =~ /.*::(.*?)$/;
203 0 ▶ return if $sub eq "DESTROY";
Branch: 1 branch
BranchTrueFalseTotal
if $sub eq "DESTROY" 0 0 0
204  
205 # The object we'll be executing against
206 0 my $wraps = $self->[0];
207  
208 # Get a reference to the original
209 0 my $ref = $wraps->can($sub);
210  
211 # Do something clever with print
212 0 ▶ if ( $sub eq 'print' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sub eq "print") 0 0 0
213 0 ▶ if ($intercept) {
Branch: 1 branch
BranchTrueFalseTotal
if ($XML::Writer::Lazy::InterceptPrint::intercept) 0 0 0
214 0 $self->[1]->{$KEY}->{'buffer'} .= join '', @_;
215 }
216 }
217  
218 # Add the wrapped object to the front of @_
219 0 unshift( @_, $wraps );
220  
221 # Redispatch; goto replaces the current stack frame, so it's like
222 # we were never here...
223 0 goto &$ref;
224 }
225  
226 # I'm the handling class the SAX parser calls when events are generated
227 package XML::Writer::Lazy::Handler;
228 1 $XML::Writer::Lazy::Handler::VERSION = '0.04';
229 1 our $writer;
230 1 our $xml_dec = 0;
231  
232 1 ▶use base qw(XML::SAX::Base);
Subroutine BEGIN: called CC=1 SCAR=0.0
233 1 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
234  
235 # This gets run for the first chunk
236 sub xml_decl {
237 0 ▶ my ( $self, $element ) = @_;
Subroutine xml_decl: not called CC=3 SCAR=24.8
238 0 ▶ return unless $xml_dec;
Branch: 1 branch
BranchTrueFalseTotal
unless $XML::Writer::Lazy::Handler::xml_dec 0 0 0
239 $writer->xmlDecl(
240 ( defined $element->{'Encoding'} )
241 0 ▶ ? ( $element->{'Encoding'} )
Branch: 1 branch
BranchTrueFalseTotal
defined $element->{'Encoding'} ? : 0 0 0
242 : ()
243 );
244 0 return;
245 }
246  
247 sub start_element {
248 0 ▶ my ( $self, $element ) = @_;
Subroutine start_element: not called CC=3 SCAR=24.8
249 0 my %attributes = %{ $element->{'Attributes'} };
250 0 my @attributes;
251 0 for my $attr ( keys %attributes ) {
252 0 ▶ if ( ref( $attributes{$attr} ) eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $attributes{$attr} eq 'HASH') { } 0 0 0
253 0 my $data = $attributes{$attr};
254 0 push( @attributes, [ $data->{'Name'}, $data->{'Value'} ] );
255 }
256 else {
257 0 push( @attributes, [ $attr, $attributes{$attr} ] );
258 }
259 }
260  
261 0 @attributes = map {@$_} sort { $a->[0] cmp $b->[0] } @attributes;
262 0 $writer->startTag( $element->{'Name'}, @attributes );
263 0 return;
264 }
265  
266 sub end_element {
267 0 ▶ my ( $self, $element ) = @_;
Subroutine end_element: not called CC=1 SCAR=6.9
268 0 $writer->endTag( $element->{'Name'} );
269 0 return;
270 }
271  
272 sub characters {
273 0 ▶ my ( $self, $characters ) = @_;
Subroutine characters: not called CC=1 SCAR=6.9
274 0 $writer->characters( $characters->{'Data'} );
275 0 return;
276 }
277  
278 sub processing_instruction {
279 0 ▶ my ( $self, $pi ) = @_;
Subroutine processing_instruction: not called CC=1 SCAR=6.9
280 0 $writer->pi( $pi->{'Target'}, $pi->{'Data'} );
281 0 return;
282 }
283  
284 sub comment {
285 0 ▶ my ( $self, $comment ) = @_;
Subroutine comment: not called CC=2 SCAR=17.9
286 0 ▶ $comment->{'Data'} ||= '';
Condition0%0/2
$comment->{"Data"} ||= ""
l!l
00
MC/DC0%0/1
$comment->{"Data"} ||= ""
$comment->{"Data"}
Truth table0%0/2
A
A= $comment->{"Data"}
Aresult
11
00
287 0 $comment->{'Data'} =~ s/^ //;
288 0 $comment->{'Data'} =~ s/ $//;
289 0 $writer->comment( $comment->{'Data'} );
290 0 return;
291 }
292  
293 sub start_dtd {
294 0 ▶ my ( $self, $dtd ) = @_;
Subroutine start_dtd: not called CC=1 SCAR=6.9
295 $writer->doctype( $dtd->{'Name'}, $dtd->{'PublicId'},
296 0 $dtd->{'SystemId'} );
297 0 return;
298 }
299  
300 sub start_prefix_mapping {
301 0 ▶ my ( $self, $prefix_mapping ) = @_;
Subroutine start_prefix_mapping: not called CC=1 SCAR=6.9
302 $writer->addPrefix( $prefix_mapping->{'NamespaceURI'},
303 0 $prefix_mapping->{'Prefix'} );
304 0 return;
305 }
306  
307 # No work needed, as the insides will already be magically quoted
308 0 ▶sub start_cdata { return; }
Subroutine start_cdata: not called CC=1 SCAR=6.9
309 0 ▶sub end_cdata { return; }
Subroutine end_cdata: not called CC=1 SCAR=6.9
310  
311 sub not_implemented {
312 1 ▶ my ($event) = @_;
Subroutine not_implemented: called CC=1 SCAR=0.0
313 1 return <<"MSG";
314 The XML passed to 'lazily()' triggered a `$event` SAX event,
315 but XML::Writer::Lazy doesn't yet handle those. You can either simplify your
316 input XML or alternatively you could monkey-patch `XML::Writer::Lazy::Handler`
317 to add a `$event()` handler method to it.
318 MSG
319 }
320  
321 sub set_document_locator {
322 1 ▶ my $self = shift;
Subroutine set_document_locator: called CC=1 SCAR=0.0
323 1 my $data = shift;
324 1 return croak( not_implemented('set_document_locator') );
325 }
326  
327 sub skipped_entity {
328 0 ▶ my $self = shift;
Subroutine skipped_entity: not called CC=1 SCAR=6.9
329 0 my $data = shift;
330 0 return croak( not_implemented('skipped_entity') );
331 }
332  
333 sub entity_reference {
334 0 ▶ my $self = shift;
Subroutine entity_reference: not called CC=1 SCAR=6.9
335 0 my $data = shift;
336 0 return croak( not_implemented('entity_reference') );
337 }
338  
339 sub notation_decl {
340 0 ▶ my $self = shift;
Subroutine notation_decl: not called CC=1 SCAR=6.9
341 0 my $data = shift;
342 0 return croak( not_implemented('notation_decl') );
343 }
344  
345 sub unparsed_entity_decl {
346 0 ▶ my $self = shift;
Subroutine unparsed_entity_decl: not called CC=1 SCAR=6.9
347 0 my $data = shift;
348 0 return croak( not_implemented('unparsed_entity_decl') );
349 }
350  
351 sub element_decl {
352 0 ▶ my $self = shift;
Subroutine element_decl: not called CC=1 SCAR=6.9
353 0 my $data = shift;
354 0 return croak( not_implemented('element_decl') );
355 }
356  
357 sub attlist_decl {
358 0 ▶ my $self = shift;
Subroutine attlist_decl: not called CC=1 SCAR=6.9
359 0 my $data = shift;
360 0 return croak( not_implemented('attlist_decl') );
361 }
362  
363 sub doctype_decl {
364 0 ▶ my $self = shift;
Subroutine doctype_decl: not called CC=1 SCAR=6.9
365 0 my $data = shift;
366 0 return croak( not_implemented('doctype_decl') );
367 }
368  
369 sub entity_decl {
370 0 ▶ my $self = shift;
Subroutine entity_decl: not called CC=1 SCAR=6.9
371 0 my $data = shift;
372 0 return croak( not_implemented('entity_decl') );
373 }
374  
375 sub attribute_decl {
376 0 ▶ my $self = shift;
Subroutine attribute_decl: not called CC=1 SCAR=6.9
377 0 my $data = shift;
378 0 return croak( not_implemented('attribute_decl') );
379 }
380  
381 sub internal_entity_decl {
382 0 ▶ my $self = shift;
Subroutine internal_entity_decl: not called CC=1 SCAR=6.9
383 0 my $data = shift;
384 0 return croak( not_implemented('internal_entity_decl') );
385 }
386  
387 sub external_entity_decl {
388 0 ▶ my $self = shift;
Subroutine external_entity_decl: not called CC=1 SCAR=6.9
389 0 my $data = shift;
390 0 return croak( not_implemented('external_entity_decl') );
391 }
392  
393 sub resolve_entity {
394 0 ▶ my $self = shift;
Subroutine resolve_entity: not called CC=1 SCAR=6.9
395 0 my $data = shift;
396 0 return croak( not_implemented('resolve_entity') );
397 }
398  
399 sub start_entity {
400 0 ▶ my $self = shift;
Subroutine start_entity: not called CC=1 SCAR=6.9
401 0 my $data = shift;
402 0 return croak( not_implemented('start_entity') );
403 }
404  
405 sub end_entity {
406 0 ▶ my $self = shift;
Subroutine end_entity: not called CC=1 SCAR=6.9
407 0 my $data = shift;
408 0 return croak( not_implemented('end_entity') );
409 }
410  
411 sub warning {
412 0 ▶ my $self = shift;
Subroutine warning: not called CC=1 SCAR=6.9
413 0 my $data = shift;
414 0 return croak( not_implemented('warning') );
415 }
416  
417 sub error {
418 0 ▶ my $self = shift;
Subroutine error: not called CC=1 SCAR=6.9
419 0 my $data = shift;
420 0 return croak( not_implemented('error') );
421 }
422  
423 sub fatal_error {
424 0 ▶ my $self = shift;
Subroutine fatal_error: not called CC=1 SCAR=6.9
425 0 my $data = shift;
426 0 return croak( not_implemented('fatal_error') );
427 }
428  
429 # I'm used when we feed captured output from XML::Writer to the SAX parser,
430 # and don't actually want to redispatch that to XML::Writer. I accept and
431 # silently drop all method calls.
432 package XML::Writer::Lazy::NullHandler;
433 1 $XML::Writer::Lazy::NullHandler::VERSION = '0.04';
434 ▶sub AUTOLOAD { }
Subroutine AUTOLOAD: not called CC=1 SCAR=0.0
435  
436 1 1;
↑ Summary