XML::NodeFilter - Generic XML::NodeFilter Class
use XML::NodeFilter;
my $filter = XML::NodeFilter->new();
$your_iterator->set_filter( $filter );
This definition is given by the DOM Traversal and Range Specification. It explains pretty well, what this class is for: A XML::NodeFilter will recieve a node from a traversal object, such as XML::LibXML::Iterator is one and tells if the given node should be returned to the caller or not.
Although I refere only to XML::LibXML here, XML::NodeFilter is implemented more open, so it can be used with other DOM implementations as well.
Also XML::NodeFilter adds two constants which are not part of the specification. The first one is FILTER_DECLINED. It tells the traversal logic, that it should apply another filter in order to decide if the node should be visible or not. While the spec only defines the traversal logic to have either one or no filter applied, it showed that it leads to cleaner code if more filter could be used in conjunktion. If a traversal logic finds a single filter that returns FILTER_DECLINED, it should be handled as a synonym of FILTER_ACCEPT. While FILTER_ACCEPT is finite and would cause all other not to be executed, FILTER_DECLINED gives one more flexibility.
The second extension of the specification is the SHOW_NONE symbol. It was added for operational completeness, so one can explicitly switch the node type filter off (means all node types are rejected). This will cause the two calls of what_to_show have a different result:
$filter->what_to_show( undef ); # will set SHOW_ALL $filter->what_to_show( SHOW_NONE ); # will not set SHOW_ALL
Infact SHOW_NONE is a NULL flag, that means it can be added to any list of flags without altering it.
$filter->what_to_show( SHOW_ELEMENT | SHOW_TEXT | SHOW_NONE );
is therefore identical to
$filter->what_to_show( SHOW_ELEMENT | SHOW_TEXT );
SHOW_NONE is espacially usefull to avoid numerically or even more ugly unintialized values while building such flag lists dynamically.
Example:
package My::NodeFilter;
use XML::NodeFilter qw(:results); use vars qw(@ISA); @ISA = qw(XML::NodeFilter);
use XML::LibXML::Common;
sub accept_node { my $filter = shift; my $node = shift;
unless ( $node->getNodeType == ELEMENT_NODE and defined $node->getNamespaceURI ) { # ignore node without a defined namespace return FILTER_REJECT; } return FILTER_DECLINED; }
1;
This example shows a simple nodefilter that will reject any element without a namespace defined. Note that FILTER_DECLINED is returned if the node was not rejected. This allows a traversal logic to apply another filter on the nodes with a namespace defined. If your application needs to use different filters on the namespaced elements depending on the state where you want to traverse your DOM but you need allways to ignore elements without a namespace such a filter will enshure that you need not to add redundant code to your filter or even to choose a base class.
The following code snippet shows how you can make use of XML::NodeFilter in your traversal logic:
use XML::NodeFilter qw(:results);
#... sub traversal_logic { my $refnode = shift; my @filters = @_; my $node = undef;
TRAVERSE: while (1) { my $state = FILTER_DECLINED; # your traversal logic # ... last TRAVERSE unless defined $node; FILTER: foreach my $filter ( @filters ) { $state = $filter->accept_node($node); last TRAVERSE if $state == FILTER_ACCEPT; last FILTER unless $state == FILTER_DECLINED; last TRAVERSE if $state == FILTER_DECLINED; }
return $node; }
As you see the traversal code will call only accept_node() on each filter. Still this will work fine with filters, that have acceptNode() implemented: XML::NodeFilter calls acceptNode() if the original accept_node() function is called. This ashures that filters that use function names conform to the specification will work as well.
Note that XML::NodeFilter uses as default return value of accept_node() FILTER_ACCEPT rather than FILTER_DECLINED. This is done so you can write 100% specification conform traversal and filter logic.
If -show is ommited SHOW_ALL is assumed as default.
If what_to_show() is called without any parameter, it simply returns the bitmaks in scalar context; if called in array context it returns a hash containing the corresponding information: If a bit is set in the bitmask the corresponding key has the value 1; otherwise it has the value 0.
FILTER_DECLINED to indicate that the filter itself would accept if no other (less significant) filters rejects or skips it. NOTE FILTER_DECLINED is not defined by the spec.
FILTER_ACCEPT to indicate that a node is accepted regardless what other filters may indicate.
FILTER_SKIP to indicate a node is skipped, but its descendants should be still available.
FILTER_REJECT to indicate a node and all its descendants should be skipped by the traversal logic.
By default accept_node() returns FILTER_ACCEPT.
NOTE: @FLAGNAMES and %FALGMAPPING are not exported. To make use of them you have to use the fully quallified namespace as follows
# gives the value of the SHOW_ELEMENT. my $flag = $XML::NodeFilter::FLAGMAPPING{SHOW_ELEMENT};
Alternativly you might import ':all' to get all symbols exported by both of the tags just described.
W3C DOM Level 2 Traversal and Range Specification
This package is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
Закладки на сайте Проследить за страницей |
Created 1996-2024 by Maxim Chirkov Добавить, Поддержать, Вебмастеру |