| 1 | |||||||||||||||||||||||||||||
| 2 | ### | ||||||||||||||||||||||||||||
| 3 | # XML::Filter::BufferText - Filter to put all characters() in one event | ||||||||||||||||||||||||||||
| 4 | # Robin Berjon <robin@knowscape.com> | ||||||||||||||||||||||||||||
| 5 | # 04/07/2003 - v1.01 | ||||||||||||||||||||||||||||
| 6 | # 26/11/2001 - v0.01 | ||||||||||||||||||||||||||||
| 7 | ### | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | package XML::Filter::BufferText; | ||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use base qw(XML::SAX::Base); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 1 | ▶ | use vars qw($VERSION $AUTOLOAD); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 1 | $VERSION = '1.01'; | |||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | #-------------------------------------------------------------------# | ||||||||||||||||||||||||||||
| 16 | # now who said SAX was complex... | ||||||||||||||||||||||||||||
| 17 | #-------------------------------------------------------------------# | ||||||||||||||||||||||||||||
| 18 | 4 | ▶ | sub characters { $_[0]->{_CharBuffer} .= $_[1]->{Data};} | ||||||||||||||||||||||||||
|
Subroutine
characters: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 6 | ▶ | sub AUTOLOAD { | ||||||||||||||||||||||||||
|
Subroutine
AUTOLOAD: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 21 | 6 | my $self = shift; | |||||||||||||||||||||||||||
| 22 | 6 | my $data = shift; | |||||||||||||||||||||||||||
| 23 | 6 | my $call = $AUTOLOAD; | |||||||||||||||||||||||||||
| 24 | 6 | $call =~ s/^.*:://; | |||||||||||||||||||||||||||
| 25 | 6 | ▶ | return if $call eq 'DESTROY'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 5 | ▶ | if (defined $self->{_CharBuffer} and length $self->{_CharBuffer}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $self->{"_CharBuffer"} and length $self->{"_CharBuffer"}
MC/DC100%2/2
defined $self->{"_CharBuffer"} and length $self->{"_CharBuffer"}
defined $self->{"_CharBuffer"}
length $self->{"_CharBuffer"}
Truth table100%3/3
A and B
A= defined $self->{"_CharBuffer"}B= length $self->{"_CharBuffer"}
| |||||||||||||||||||||||||||||
| 28 | 3 | $self->SUPER::characters( { Data => $self->{_CharBuffer} } ); | |||||||||||||||||||||||||||
| 29 | 3 | $self->{_CharBuffer} = ''; | |||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||
| 31 | 5 | $call = 'SUPER::' . $call; | |||||||||||||||||||||||||||
| 32 | 5 | $self->$call($data); | |||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||
| 34 | #-------------------------------------------------------------------# | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | ### AUTOLOAD and inheritance don't work all that well, this is a | ||||||||||||||||||||||||||||
| 37 | ### workaround for that problem. | ||||||||||||||||||||||||||||
| 38 | sub start_document; sub end_document; sub start_element; | ||||||||||||||||||||||||||||
| 39 | sub end_element; sub processing_instruction; sub comment; | ||||||||||||||||||||||||||||
| 40 | sub skipped_entity; sub ignorable_whitespace; sub end_entity; | ||||||||||||||||||||||||||||
| 41 | sub start_entity; sub entity_reference; | ||||||||||||||||||||||||||||
| 42 | sub start_cdata; sub end_cdata; | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 1 | 1; | |||||||||||||||||||||||||||
| 45 | #,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,# | ||||||||||||||||||||||||||||
| 46 | #`,`, Documentation `,`,`,`,`,`,`,`,`,`,`,`,`,`,`,`,`,`,`,`,`,`,`,`,# | ||||||||||||||||||||||||||||
| 47 | #```````````````````````````````````````````````````````````````````# | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | =pod | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | =head1 NAME | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | XML::Filter::BufferText - Filter to put all characters() in one event | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | my $h = SomeHandler->new; | ||||||||||||||||||||||||||||
| 58 | my $f = XML::Filter::BufferText->new( Handler => $h ); | ||||||||||||||||||||||||||||
| 59 | my $p = SomeParser->new( Handler => $f ); | ||||||||||||||||||||||||||||
| 60 | $p->parse; | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | This is a very simple filter. One common cause of grief (and programmer | ||||||||||||||||||||||||||||
| 65 | error) is that XML parsers aren't required to provide character events in one | ||||||||||||||||||||||||||||
| 66 | chunk. They can, but are not forced to, and most don't. This filter does the | ||||||||||||||||||||||||||||
| 67 | trivial but oft-repeated task of putting all characters into a single event. | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | Note that this won't help you cases such as: | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | <foo> blah <!-- comment --> phubar </foo> | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | In the above case, given the interleaving comment, there will be two | ||||||||||||||||||||||||||||
| 74 | C<character()> events. This may be worked around in the future if there is | ||||||||||||||||||||||||||||
| 75 | demand for it. | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | An interesting way to use this filter, instead of telling users to use it, | ||||||||||||||||||||||||||||
| 78 | is to return it from your handler's constructor, already configured and all. | ||||||||||||||||||||||||||||
| 79 | That'll make the buffering totally transparent to them (C<XML::SAX::Writer> | ||||||||||||||||||||||||||||
| 80 | does that). | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | Robin Berjon, robin@knowscape.com | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | =head1 COPYRIGHT | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | Copyright (c) 2001-2002 Robin Berjon. All rights reserved. This program is | ||||||||||||||||||||||||||||
| 89 | free software; you can redistribute it and/or modify it under the same | ||||||||||||||||||||||||||||
| 90 | terms as Perl itself. | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | XML::SAX::*, XML::Generator::*, XML::Handler::*, XML::Filter::* | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | =cut | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||