Bio::DB::TFBS namespace has been moved to its own distribution named after itself
[bioperl-live.git] / Bio / OntologyIO / Handlers / BaseSAXHandler.pm
blobdffd9d8064a1844486a44c1e5108ae5727f88575
2 # BioPerl module for BaseSAXHandler
4 # Please direct questions and support issues to <bioperl-l@bioperl.org>
6 # Cared for by Juguang Xiao, juguang@tll.org.sg
8 # Copyright Juguang Xiao
10 # You may distribute this module under the same terms as perl itself
12 # POD documentation - main docs before the code
14 =head1 NAME
16 Bio::OntologyIO::Handlers::BaseSAXHandler - base class for SAX Handlers
18 =head1 SYNOPSIS
20 See description.
22 =head1 DESCRIPTION
24 This module is an abstract module, serving as the base of any SAX Handler
25 implementation. It tries to offer the framework that SAX handlers generally
26 need, such as tag_stack, char_store, etc.
28 In the implementation handler, you can take advantage of this based module by
29 the following suggestions.
31 1) In start_element,
33 sub start_element {
34 my $self=shift;
35 my $tag=$_[0]->{Name};
36 my %args=%{$_[0]->{Attributes}};
37 # Your code here.
39 # Before you conclude the method, write these 2 line.
40 $self->_visited_count_inc($tag);
41 $self->_push_tag($tag);
44 2) In end_element,
46 sub end_element {
47 my $self=shift;
48 my $tag=shift->{Name};
49 # Your code here.
51 # Before you conclude the method, write these 2 lines.
52 $self->_visited_count_dec($tag);
53 $self->_pop_tag;
56 3) In characters, or any other methods where you may use the tag
57 stack or count
59 sub characters {
60 my $self=shift;
61 my $text=shift->{Data};
63 $self->_chars_hash->{$self->_top_tag} .= $text;
66 $count = $self->_visited_count('myTag');
67 $tag = $self->_top_tag;
70 =head1 FEEDBACK
72 =head2 Mailing Lists
74 User feedback is an integral part of the evolution of this and other
75 Bioperl modules. Send your comments and suggestions preferably to one
76 of the Bioperl mailing lists.
78 Your participation is much appreciated.
80 bioperl-l@bioperl.org - General discussion
81 http://bioperl.org/wiki/Mailing_lists - About the mailing lists
83 =head2 Support
85 Please direct usage questions or support issues to the mailing list:
87 I<bioperl-l@bioperl.org>
89 rather than to the module maintainer directly. Many experienced and
90 reponsive experts will be able look at the problem and quickly
91 address it. Please include a thorough description of the problem
92 with code and data examples if at all possible.
94 =head2 Reporting Bugs
96 Report bugs to the Bioperl bug tracking system to help us keep track
97 the bugs and their resolution. Bug reports can be submitted via the
98 web:
100 https://github.com/bioperl/bioperl-live/issues
102 =head1 AUTHOR
104 Juguang Xiao, juguang@tll.org.sg
106 =head2 APPENDIX
108 The rest of the documentation details each of the object methods.
109 Internal methods are usually preceded with a _
111 =cut
113 package Bio::OntologyIO::Handlers::BaseSAXHandler;
114 use strict;
115 use base qw(Bio::Root::Root);
118 sub new {
119 my ($class, @args) = @_;
120 my $self=$class->SUPER::new(@args);
121 $self->_initialize(@args);
122 return $self;
125 sub _initialize {
126 my $self = shift;
127 $self->{_tag_stack} = [];
128 $self->{_visited_count} = {};
129 $self->{_chars_hash} = {};
130 $self->{_current_hash} = {};
133 =head2 _tag_stack
135 Title : _tag_stack
136 Usage : @tags = $self->_tag_stack;
137 Function: Get an array of tags that have been accessed but not enclosed.
138 Return :
139 Args :
141 =cut
143 sub _tag_stack {
144 return @{shift->{_tag_stack}};
147 =head2 _push_tag
149 =cut
151 sub _push_tag {
152 my($self,$tag)=@_;
153 push @{$self->{_tag_stack}}, $tag;
156 =head2 _pop_tag
158 =cut
160 sub _pop_tag {
161 my $self=shift;
162 return pop @{$self->{_tag_stack}};
165 =head2 _top_tag
167 Title : _top_tag
168 Usage : $top = $self->_top_tag;
169 Function: get the top tag in the tag stack.
170 Return : a tag name
171 Args : [none]
173 =cut
175 sub _top_tag {
176 my $self = shift;
177 my @stack=@{$self->{_tag_stack}};
178 return $stack[-1];
179 # get the last element in an array while remaining it in. There are few ways
180 # 1) $stack[-1]
181 # 2) $stack[$#stack]
182 # 3) $stack[@stack-1]
186 =head2 _chars_hash
188 Title : _chars_hash
189 Usage : $hash= $self->_chars_hash;
190 Function: return the character cache for the specific tag
191 Return : a hash reference, which is intent for character storage for tags
192 Args : [none]
194 =cut
196 sub _chars_hash {
197 return shift->{_chars_hash};
200 =head2 _current_hash
202 =cut
204 sub _current_hash {
205 return shift->{_current_hash};
208 =head2 _visited_count_inc
210 Title : _vistied_count_inc
211 Usage : $self->vistied_count_inc($tag); # the counter for the tag increase
212 Function: the counter for the tag increase
213 Return : the current count after this increment
214 Args : the tag name [scalar]
216 =cut
218 sub _visited_count_inc {
219 my ($self, $tag) = @_;
220 my $visited_count=$self->{_visited_count};
221 if(exists $visited_count->{$tag}){
222 $visited_count->{$tag}++;
223 }else{
224 $visited_count->{$tag}=1;
226 return $visited_count->{$tag};
229 =head2 _visited_count_dec
231 Title : _visited_count_dec
232 Usage : $self->_visited_count_dec($tag);
233 Function: the counter for the tag decreases by one
234 Return : the current count for the specific tag after the decrement
235 Args : the tag name [scalar]
237 =cut
239 sub _visited_count_dec {
240 my ($self, $tag) = @_;
241 my $visited_count=$self->{_visited_count};
242 if(exists $visited_count->{$tag}){
243 $visited_count->{$tag}--;
244 }else{
245 $self->throw("'$tag' has not been visited yet. How to decrease it?!");
247 return $visited_count->{$tag};
250 =head2 _visited_count
252 Title : _visited_count
253 Usage : $count = $self->_visited_count
254 Function: return the counter for the tag
255 Return : the current counter for the specific tag
256 Args : the tag name [scalar]
258 =cut
260 sub _visited_count {
261 my ($self, $tag) = @_;
262 return $self->{_visited_count}->{$tag};