XML-Filter-DetectWS

 view release on metacpan or  search on metacpan

DetectWS.pm  view on Meta::CPAN

	    sub { $handler->ignorable_whitespace (@_) };
    }
    else
    {
	$handlers{ignorable_whitespace} = $handlers{characters};
    }

    $handlers{ws} = $handlers{characters};    
#?? were should whitespace go?

    # NOTE: 'cdata' is not a valid PerlSAX callback
    if (UNIVERSAL::can ($handler, 'start_cdata') &&
	UNIVERSAL::can ($handler, 'end_cdata'))
    {
	$handlers{cdata} = sub {
	    $handler->start_cdata;
	    $handler->characters (@_);
	    $handler->end_cdata;
	}
    }
    else	# pass CDATA as regular characters
    {
	$handlers{cdata} = $handlers{characters};
    }

    $self->{Callback} = \%handlers;
}

sub start_cdata
{
    my ($self, $event) = @_;

    $self->{InCDATA} = 1;
}

sub end_cdata
{
    my ($self, $event) = @_;

    $self->{InCDATA} = 0;
}

sub entity_reference
{
    my ($self, $event) = @_;
    

DetectWS.pm  view on Meta::CPAN

    my $elem = pop @{ $self->{ParentStack} };
    delete $elem->{State};
}

sub characters
{
    my ($self, $event) = @_;

    if ($self->{InCDATA})
    {
	# NOTE: 'cdata' is not a valid PerlSAX callback
	$self->push_event ('cdata', $event);
	
	my $parent = $self->{ParentStack}->[-1];
	$parent->{State} |= ENDS_IN_NON_WS unless $parent->{State} == PRESERVE_WS;
	return;
    }

    my $text = $event->{Data};
    return unless length ($text);

    my $state = $self->{ParentStack}->[-1]->{State};

DetectWS.pm  view on Meta::CPAN

be I<ignorable>. The ignorable whitespace is passed to the PerlSAX Handler with
the B<ignorable_whitespace> handler, provided that the Handler implements this 
method. (Otherwise it is passed to the characters handler.)
If the B<SkipIgnorableWS> is set, the ignorable whitespace is simply
discarded.

XML::Filter::DetectWS also takes xml:space attributes into account. See below
for details.

CDATA sections are passed in the standard PerlSAX way (i.e. with surrounding
start_cdata and end_cdata events), unless the Handler does not implement these
methods. In that case, the CDATA section is simply passed to the characters 
method.

=head1 Constructor Options

=over 4

=item * SkipIgnorableWS (Default: 0)

When set, detected ignorable whitespace is discarded.

DetectWS.pm  view on Meta::CPAN

The idea is that the API of DetectWS will be extended, so that you can
specify/override e.g. which elements should behave as if xml:space="preserve" 
were set, and/or which elements should behave as if the <!ELEMENT> model was
defined a certain way, etc.

Please send feedback!

The current implementation also detects whitespace after an element-start tag,
whitespace before an element-end tag. 
It also detects whitespace before an element-start and after an element-end tag
and before or after comments, processing instruction, cdata sections etc.,
but this needs to be reimplemented.
In either case, the detected whitespace is split off into its own PerlSAX
characters event and an extra property 'Loc' is added. It can have 4 possible
values:

=over 4

=item * 1 (WS_START) - whitespace immediately after element-start tag

=item * 2 (WS_END) - whitespace just before element-end tag

=item * 3 (WS_ONLY) - both WS_START and WS_END, i.e. it's the only text found between the start and end tag and it's all whitespace

=item * 0 (WS_INTER) - none of the above, probably before an element-start tag,
after an element-end tag, or before or after a comment, PI, cdata section etc.

=back

Note that WS_INTER may not be that useful, so this may change.

=head1 xml:space attribute

The XML spec states that: A special attribute
named xml:space may be attached to an element
to signal an intention that in that element,

README  view on Meta::CPAN

The XML spec defines ignorable whitespace as the character data found in elements that were defined in an
<!ELEMENT> declaration with a model of 'EMPTY' or 'Children' (Children is the rule that does not contain '#PCDATA'.)

In addition, XML::Filter::DetectWS allows the user to define other whitespace to be ignorable. The ignorable whitespace
is passed to the PerlSAX Handler with the ignorable_whitespace handler, provided that the Handler implements this
method. (Otherwise it is passed to the characters handler.) If the SkipIgnorableWS is set, the ignorable whitespace is
simply discarded. 

XML::Filter::DetectWS also takes xml:space attributes into account. See below for details. 

CDATA sections are passed in the standard PerlSAX way (i.e. with surrounding start_cdata and end_cdata events),
unless the Handler does not implement these methods. In that case, the CDATA section is simply passed to the
characters method. 

INSTALLATION

To install this module type the following:

   perl Makefile.PL
   make
   make test



( run in 2.056 seconds using v1.01-cache-2.11-cpan-995e09ba956 )