This module is an abstract module, serving as the base of any SAX Handler
implementation. It tries to offer the framework that SAX handlers generally
need, such as tag_stack, char_store, etc.
In the implementation handler, you can take advantage of this based module by
the following suggestions.
1) In start_element,
sub start_element {
my $self=shift;
my $tag=$_[0]->{Name};
my %args=%{$_[0]->{Attributes}};
# Your code here.
# Before you conclude the method, write these 2 line.
$self->_visited_count_inc($tag);
$self->_push_tag($tag);
}
2) In end_element,
sub end_element {
my $self=shift;
my $tag=shift->{Name};
# Your code here.
# Before you conclude the method, write these 2 lines.
$self->_visited_count_dec($tag);
$self->_pop_tag;
}
3) In characters, or any other methods where you may use the tag
stack or count
sub characters {
my $self=shift;
my $text=shift->{Data};
$self->_chars_hash->{$self->_top_tag} .= $text;
}
$count = $self->_visited_count('myTag');
$tag = $self->_top_tag;
sub new
{ my ($class, @args) = @_;
my $self=$class->SUPER::new(@args);
$self->_initialize(@args);
return $self;} |
sub _initialize
{ my $self = shift;
$self->{_tag_stack} = [];
$self->{_visited_count} = {};
$self->{_chars_hash} = {};
$self->{_current_hash} = {};} |
sub _push_tag
{ my($self,$tag)=@_;
push @{$self->{_tag_stack}}, $tag;} |
sub _visited_count_inc
{ my ($self, $tag) = @_;
my $visited_count=$self->{_visited_count};
if(exists $visited_count->{$tag}){
$visited_count->{$tag}++;
}else{
$visited_count->{$tag}=1;
}
return $visited_count->{$tag};} |
sub _visited_count_dec
{ my ($self, $tag) = @_;
my $visited_count=$self->{_visited_count};
if(exists $visited_count->{$tag}){
$visited_count->{$tag}--;
}else{
$self->throw("'$tag' has not been visited yet. How to decrease it?!");
}
return $visited_count->{$tag};} |
sub _visited_count
{ my ($self, $tag) = @_;
return $self->{_visited_count}->{$tag};} |
The rest of the documentation details each of the object methods.
Interal methods are usually preceded with a _