Debian Codegraph @

lib/XML/SAX/Writer/XML.pm

raw
1 package XML::SAX::Writer::XML;
2 $XML::SAX::Writer::XML::VERSION = '0.57';
3 use strict;
4 use warnings;
5 use XML::NamespaceSupport qw();
6 @XML::SAX::Writer::XML::ISA = qw(XML::SAX::Writer);
7
8 # ABSTRACT: XML::SAX::Writer's SAX Handler
9
10 ###
11 # Robin Berjon <[email protected]>
12 ###
13
14
15 #-------------------------------------------------------------------#
16 # start_document
17 #-------------------------------------------------------------------#
18 sub start_document {
19 my $self = shift;
20
21 $self->setConverter;
22 $self->setEscaperRegex;
23 $self->setAttributeEscaperRegex;
24 $self->setCommentEscaperRegex;
25
26 $self->{NSDecl} = [];
27 $self->{NSHelper} = XML::NamespaceSupport->new({ xmlns => 1, fatal_errors => 0 });
28 $self->{NSHelper}->pushContext;
29
30 $self->setConsumer;
31 }
32 #-------------------------------------------------------------------#
33
34 #-------------------------------------------------------------------#
35 # end_document
36 #-------------------------------------------------------------------#
37 sub end_document {
38 my $self = shift;
39 # we may need to do a little more here
40 $self->{NSHelper}->popContext;
41 return $self->{Consumer}->finalize
42 if $self->{Consumer}->can( 'finalize' );
43 }
44 #-------------------------------------------------------------------#
45
46 #-------------------------------------------------------------------#
47 # start_element
48 #-------------------------------------------------------------------#
49 sub start_element {
50 my $self = shift;
51 my $data = shift;
52 $self->_output_element;
53 my $attr = $data->{Attributes};
54
55 # fix the namespaces and prefixes of what we're receiving, in case
56 # something is wrong
57 if ($data->{NamespaceURI}) {
58 my $uri = $self->{NSHelper}->getURI($data->{Prefix}) || '';
59 if ($uri ne $data->{NamespaceURI}) { # ns has precedence
60 $data->{Prefix} = $self->{NSHelper}->getPrefix($data->{NamespaceURI}); # random, but correct
61 $data->{Name} = $data->{Prefix} ? "$data->{Prefix}:$data->{LocalName}" : "$data->{LocalName}";
62 }
63 }
64 elsif ($data->{Prefix}) { # we can't have a prefix and no NS
65 $data->{Name} = $data->{LocalName};
66 $data->{Prefix} = '';
67 }
68
69 # create a hash containing the attributes so that we can ensure there is
70 # no duplication. Also, we check that ns are properly declared, that the
71 # Name is good, etc...
72 my %attr_hash;
73 for my $at (values %$attr) {
74 next unless length $at->{Name}; # people have trouble with autovivification
75 if ($at->{NamespaceURI}) {
76 my $uri = $self->{NSHelper}->getURI($at->{Prefix});
77 warn "Well formed error: prefix '$at->{Prefix}' is not bound to any URI" unless defined $uri;
78 if (defined $uri and $uri ne $at->{NamespaceURI}) { # ns has precedence
79 $at->{Prefix} = $self->{NSHelper}->getPrefix($at->{NamespaceURI}); # random, but correct
80 $at->{Name} = $at->{Prefix} ? "$at->{Prefix}:$at->{LocalName}" : "$at->{LocalName}";
81 }
82 }
83 elsif ($at->{Prefix}) { # we can't have a prefix and no NS
84 $at->{Name} = $at->{LocalName};
85 $at->{Prefix} = '';
86 }
87 $attr_hash{$at->{Name}} = $at->{Value};
88 }
89
90 for my $nd (@{$self->{NSDecl}}) {
91 if ($nd->{Prefix}) {
92 $attr_hash{'xmlns:' . $nd->{Prefix}} = $nd->{NamespaceURI};
93 }
94 else {
95 $attr_hash{'xmlns'} = $nd->{NamespaceURI};
96 }
97 }
98 $self->{NSDecl} = [];
99
100 # build a string from what we have, and buffer it
101 my $el = '<' . $data->{Name};
102 for my $k (keys %attr_hash) {
103 $el .= ' ' . $k . qq[=$self->{QuoteCharacter}] . $self->escapeAttribute($attr_hash{$k}) . qq[$self->{QuoteCharacter}];
104 }
105
106 $self->{BufferElement} = $el;
107 $self->{NSHelper}->pushContext;
108 }
109 #-------------------------------------------------------------------#
110
111 #-------------------------------------------------------------------#
112 # end_element
113 #-------------------------------------------------------------------#
114 sub end_element {
115 my $self = shift;
116 my $data = shift;
117
118 my $el;
119 if ($self->{BufferElement}) {
120 $el = $self->{BufferElement} . ' />';
121 }
122 else {
123 $el = '</' . $data->{Name} . '>';
124 }
125 $el = $self->safeConvert($el);
126 $self->{Consumer}->output($el);
127 $self->{NSHelper}->popContext;
128 $self->{BufferElement} = '';
129 }
130 #-------------------------------------------------------------------#
131
132 #-------------------------------------------------------------------#
133 # characters
134 #-------------------------------------------------------------------#
135 sub characters {
136 my $self = shift;
137 my $data = shift;
138 $self->_output_element;
139
140 my $char = $data->{Data};
141 if ($self->{InCDATA}) {
142 # we must scan for ]]> in the CDATA and escape it if it
143 # is present by close--opening
144 # we need to have buffer text in front of this...
145 $char = join ']]>]]&lt;<![CDATA[', split ']]>', $char;
146 }
147 else {
148 $char = $self->escape($char);
149 }
150 $char = $self->safeConvert($char);
151 $self->{Consumer}->output($char);
152 }
153 #-------------------------------------------------------------------#
154
155 #-------------------------------------------------------------------#
156 # start_prefix_mapping
157 #-------------------------------------------------------------------#
158 sub start_prefix_mapping {
159 my $self = shift;
160 my $data = shift;
161
162 push @{$self->{NSDecl}}, $data;
163 $self->{NSHelper}->declarePrefix($data->{Prefix}, $data->{NamespaceURI});
164 }
165 #-------------------------------------------------------------------#
166
167 #-------------------------------------------------------------------#
168 # end_prefix_mapping
169 #-------------------------------------------------------------------#
170 sub end_prefix_mapping {}
171 #-------------------------------------------------------------------#
172
173 #-------------------------------------------------------------------#
174 # processing_instruction
175 #-------------------------------------------------------------------#
176 sub processing_instruction {
177 my $self = shift;
178 my $data = shift;
179 $self->_output_element;
180 $self->_output_dtd;
181
182 my $pi = "<?$data->{Target} $data->{Data}?>";
183 $pi = $self->safeConvert($pi);
184 $self->{Consumer}->output($pi);
185 }
186 #-------------------------------------------------------------------#
187
188 #-------------------------------------------------------------------#
189 # ignorable_whitespace
190 #-------------------------------------------------------------------#
191 sub ignorable_whitespace {
192 my $self = shift;
193 my $data = shift;
194 $self->_output_element;
195
196 my $char = $data->{Data};
197 $char = $self->escape($char);
198 $char = $self->safeConvert($char);
199 $self->{Consumer}->output($char);
200 }
201 #-------------------------------------------------------------------#
202
203 #-------------------------------------------------------------------#
204 # skipped_entity
205 #-------------------------------------------------------------------#
206 sub skipped_entity {
207 my $self = shift;
208 my $data = shift;
209 $self->_output_element;
210 $self->_output_dtd;
211
212 my $ent;
213 if ($data->{Name} =~ m/^%/) {
214 $ent = $data->{Name} . ';';
215
216 } elsif ($data->{Name} eq '[dtd]') {
217 # ignoring
218
219 } else {
220 $ent = '&' . $data->{Name} . ';';
221 }
222
223 $ent = $self->safeConvert($ent);
224 $self->{Consumer}->output($ent);
225
226 }
227 #-------------------------------------------------------------------#
228
229 #-------------------------------------------------------------------#
230 # notation_decl
231 #-------------------------------------------------------------------#
232 sub notation_decl {
233 my $self = shift;
234 my $data = shift;
235 $self->_output_dtd;
236
237 # I think that param entities are normalized before this
238 my $not = " <!NOTATION " . $data->{Name};
239 if ($data->{PublicId} and $data->{SystemId}) {
240 $not .= ' PUBLIC \'' . $self->escape($data->{PublicId}) . '\' \'' . $self->escape($data->{SystemId}) . '\'';
241 }
242 elsif ($data->{PublicId}) {
243 $not .= ' PUBLIC \'' . $self->escape($data->{PublicId}) . '\'';
244 }
245 else {
246 $not .= ' SYSTEM \'' . $self->escape($data->{SystemId}) . '\'';
247 }
248 $not .= " >\n";
249
250 $not = $self->safeConvert($not);
251 $self->{Consumer}->output($not);
252 }
253 #-------------------------------------------------------------------#
254
255 #-------------------------------------------------------------------#
256 # unparsed_entity_decl
257 #-------------------------------------------------------------------#
258 sub unparsed_entity_decl {
259 my $self = shift;
260 my $data = shift;
261 $self->_output_dtd;
262
263 # I think that param entities are normalized before this
264 my $ent = " <!ENTITY " . $data->{Name};
265 if ($data->{PublicId}) {
266 $ent .= ' PUBLIC \'' . $self->escape($data->{PublicId}) . '\' \'' . $self->escape($data->{SystemId}) . '\'';
267 }
268 else {
269 $ent .= ' SYSTEM \'' . $self->escape($data->{SystemId}) . '\'';
270 }
271 $ent .= " NDATA $data->{Notation} >\n";
272
273 $ent = $self->safeConvert($ent);
274 $self->{Consumer}->output($ent);
275 }
276 #-------------------------------------------------------------------#
277
278 #-------------------------------------------------------------------#
279 # element_decl
280 #-------------------------------------------------------------------#
281 sub element_decl {
282 my $self = shift;
283 my $data = shift;
284 $self->_output_dtd;
285
286 # I think that param entities are normalized before this
287 my $eld = " <!ELEMENT " . $data->{Name} . ' ' . $data->{Model} . " >\n";
288
289 $eld = $self->safeConvert($eld);
290 $self->{Consumer}->output($eld);
291 }
292 #-------------------------------------------------------------------#
293
294 #-------------------------------------------------------------------#
295 # attribute_decl
296 #-------------------------------------------------------------------#
297 sub attribute_decl {
298 my $self = shift;
299 my $data = shift;
300 $self->_output_dtd;
301
302 # to be backward compatible with Perl SAX 2.0
303 $data->{Mode} = $data->{ValueDefault}
304 if not(exists $data->{Mode}) and exists $data->{ValueDefault};
305
306 # I think that param entities are normalized before this
307 my $atd = " <!ATTLIST " . $data->{eName} . ' ' . $data->{aName} . ' ';
308 $atd .= $data->{Type} . ' ' . $data->{Mode} . ' ';
309 $atd .= $data->{Value} . ' ' if $data->{Value};
310 $atd .= " >\n";
311
312 $atd = $self->safeConvert($atd);
313 $self->{Consumer}->output($atd);
314 }
315 #-------------------------------------------------------------------#
316
317 #-------------------------------------------------------------------#
318 # internal_entity_decl
319 #-------------------------------------------------------------------#
320 sub internal_entity_decl {
321 my $self = shift;
322 my $data = shift;
323 $self->_output_dtd;
324
325 # I think that param entities are normalized before this
326 my $ent = " <!ENTITY " . $data->{Name} . ' \'' . $self->escape($data->{Value}) . "' >\n";
327 $ent = $self->safeConvert($ent);
328 $self->{Consumer}->output($ent);
329 }
330 #-------------------------------------------------------------------#
331
332 #-------------------------------------------------------------------#
333 # external_entity_decl
334 #-------------------------------------------------------------------#
335 sub external_entity_decl {
336 my $self = shift;
337 my $data = shift;
338 $self->_output_dtd;
339
340 # I think that param entities are normalized before this
341 my $ent = " <!ENTITY " . $data->{Name};
342 if ($data->{PublicId}) {
343 $ent .= ' PUBLIC \'' . $self->escape($data->{PublicId}) . '\' \'' . $self->escape($data->{SystemId}) . '\'';
344 }
345 else {
346 $ent .= ' SYSTEM \'' . $self->escape($data->{SystemId}) . '\'';
347 }
348 $ent .= " >\n";
349
350 $ent = $self->safeConvert($ent);
351 $self->{Consumer}->output($ent);
352 }
353 #-------------------------------------------------------------------#
354
355 #-------------------------------------------------------------------#
356 # comment
357 #-------------------------------------------------------------------#
358 sub comment {
359 my $self = shift;
360 my $data = shift;
361 $self->_output_element;
362 $self->_output_dtd;
363
364 my $cmt = '<!--' . $self->escapeComment($data->{Data}) . '-->';
365 $cmt = $self->safeConvert($cmt);
366 $self->{Consumer}->output($cmt);
367 }
368 #-------------------------------------------------------------------#
369
370 #-------------------------------------------------------------------#
371 # start_dtd
372 #-------------------------------------------------------------------#
373 sub start_dtd {
374 my $self = shift;
375 my $data = shift;
376
377 my $dtd = '<!DOCTYPE ' . $data->{Name};
378 if ($data->{PublicId}) {
379 $dtd .= ' PUBLIC \'' . $self->escape($data->{PublicId}) . '\' \'' . $self->escape($data->{SystemId}) . '\'';
380 }
381 elsif ($data->{SystemId}) {
382 $dtd .= ' SYSTEM \'' . $self->escape($data->{SystemId}) . '\'';
383 }
384
385 $self->{BufferDTD} = $dtd;
386 }
387 #-------------------------------------------------------------------#
388
389 #-------------------------------------------------------------------#
390 # end_dtd
391 #-------------------------------------------------------------------#
392 sub end_dtd {
393 my $self = shift;
394 my $data = shift;
395
396 my $dtd;
397 if ($self->{BufferDTD}) {
398 $dtd = $self->{BufferDTD} . ' >';
399 }
400 else {
401 $dtd = ' ]>';
402 }
403 $dtd = $self->safeConvert($dtd);
404 $self->{Consumer}->output($dtd);
405 $self->{BufferDTD} = '';
406 }
407 #-------------------------------------------------------------------#
408
409 #-------------------------------------------------------------------#
410 # start_cdata
411 #-------------------------------------------------------------------#
412 sub start_cdata {
413 my $self = shift;
414 $self->_output_element;
415
416 $self->{InCDATA} = 1;
417 my $cds = $self->{Encoder}->convert('<![CDATA[');
418 $self->{Consumer}->output($cds);
419 }
420 #-------------------------------------------------------------------#
421
422 #-------------------------------------------------------------------#
423 # end_cdata
424 #-------------------------------------------------------------------#
425 sub end_cdata {
426 my $self = shift;
427
428 $self->{InCDATA} = 0;
429 my $cds = $self->{Encoder}->convert(']]>');
430 $self->{Consumer}->output($cds);
431 }
432 #-------------------------------------------------------------------#
433
434 #-------------------------------------------------------------------#
435 # start_entity
436 #-------------------------------------------------------------------#
437 sub start_entity {
438 my $self = shift;
439 my $data = shift;
440 $self->_output_element;
441 $self->_output_dtd;
442
443 my $ent;
444 if ($data->{Name} eq '[dtd]') {
445 # we ignore the fact that we're dealing with an external
446 # DTD entity here, and probably shouldn't write the DTD
447 # events unless explicitly told to
448 # this will probably change
449 }
450 elsif ($data->{Name} =~ m/^%/) {
451 $ent = $data->{Name} . ';';
452 }
453 else {
454 $ent = '&' . $data->{Name} . ';';
455 }
456
457 $ent = $self->safeConvert($ent);
458 $self->{Consumer}->output($ent);
459 }
460 #-------------------------------------------------------------------#
461
462 #-------------------------------------------------------------------#
463 # end_entity
464 #-------------------------------------------------------------------#
465 sub end_entity {
466 # depending on what is done above, we might need to do sth here
467 }
468 #-------------------------------------------------------------------#
469
470
471 ### SAX1 stuff ######################################################
472
473 #-------------------------------------------------------------------#
474 # xml_decl
475 #-------------------------------------------------------------------#
476 sub xml_decl {
477 my $self = shift;
478 my $data = shift;
479
480 # version info is compulsory, contrary to what some seem to think
481 # also, there's order in the pseudo-attr
482 my $xd = '';
483 if ($data->{Version}) {
484 $xd .= "<?xml version=\"$data->{Version}\"";
485 if ($data->{Encoding}) {
486 $xd .= " encoding=\"$data->{Encoding}\"";
487 }
488 if ($data->{Standalone}) {
489 $xd .= " standalone=\"$data->{Standalone}\"";
490 }
491 $xd .= "?>\n";
492 }
493
494 #$xd = $self->{Encoder}->convert($xd); # this may blow up
495 $self->{Consumer}->output($xd);
496 }
497 #-------------------------------------------------------------------#
498
499
500 #,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,#
501 #`,`, Helpers `,`,`,`,`,`,`,`,`,`,`,`,`,`,`,`,`,`,`,`,`,`,`,`,`,`,`,#
502 #```````````````````````````````````````````````````````````````````#
503
504 #-------------------------------------------------------------------#
505 # _output_element
506 #-------------------------------------------------------------------#
507 sub _output_element {
508 my $self = shift;
509
510 if ($self->{BufferElement}) {
511 my $el = $self->{BufferElement} . '>';
512 $el = $self->safeConvert($el);
513 $self->{Consumer}->output($el);
514 $self->{BufferElement} = '';
515 }
516 }
517 #-------------------------------------------------------------------#
518
519 #-------------------------------------------------------------------#
520 # _output_dtd
521 #-------------------------------------------------------------------#
522 sub _output_dtd {
523 my $self = shift;
524
525 if ($self->{BufferDTD}) {
526 my $dtd = $self->{BufferDTD} . " [\n";
527 $dtd = $self->safeConvert($dtd);
528 $self->{Consumer}->output($dtd);
529 $self->{BufferDTD} = '';
530 }
531 }
532 #-------------------------------------------------------------------#
533
534 1;
535 #,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,#
536 #`,`, Documentation `,`,`,`,`,`,`,`,`,`,`,`,`,`,`,`,`,`,`,`,`,`,`,`,#
537 #```````````````````````````````````````````````````````````````````#
538
539 __END__
540
541 =pod
542
543 =encoding UTF-8
544
545 =head1 NAME
546
547 XML::SAX::Writer::XML - XML::SAX::Writer's SAX Handler
548
549 =head1 VERSION
550
551 version 0.57
552
553 =head1 SYNOPSIS
554
555 ...
556
557 =head1 DESCRIPTION
558
559 ...
560
561 =head1 SEE ALSO
562
563 XML::SAX::*
564
565 =head1 AUTHORS
566
567 =over 4
568
569 =item *
570
571 Robin Berjon <[email protected]>
572
573 =item *
574
575 Chris Prather <[email protected]>
576
577 =back
578
579 =head1 COPYRIGHT AND LICENSE
580
581 This software is copyright (c) 2014 by Robin Berjon.
582
583 This is free software; you can redistribute it and/or modify it under
584 the same terms as the Perl 5 programming language system itself.
585
586 =cut