| 1 | # | ||||||||||
| 2 | # BioPerl module for BaseSAXHandler | ||||||||||
| 3 | # | ||||||||||
| 4 | # Please direct questions and support issues to <bioperl-l@bioperl.org> | ||||||||||
| 5 | # | ||||||||||
| 6 | # Cared for by Juguang Xiao, juguang@tll.org.sg | ||||||||||
| 7 | # | ||||||||||
| 8 | # Copyright Juguang Xiao | ||||||||||
| 9 | # | ||||||||||
| 10 | # You may distribute this module under the same terms as perl itself | ||||||||||
| 11 | |||||||||||
| 12 | # POD documentation - main docs before the code | ||||||||||
| 13 | |||||||||||
| 14 | =head1 NAME | ||||||||||
| 15 | |||||||||||
| 16 | Bio::OntologyIO::Handlers::BaseSAXHandler - base class for SAX Handlers | ||||||||||
| 17 | |||||||||||
| 18 | =head1 SYNOPSIS | ||||||||||
| 19 | |||||||||||
| 20 | See description. | ||||||||||
| 21 | |||||||||||
| 22 | =head1 DESCRIPTION | ||||||||||
| 23 | |||||||||||
| 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. | ||||||||||
| 27 | |||||||||||
| 28 | In the implementation handler, you can take advantage of this based module by | ||||||||||
| 29 | the following suggestions. | ||||||||||
| 30 | |||||||||||
| 31 | 1) In start_element, | ||||||||||
| 32 | |||||||||||
| 33 | sub start_element { | ||||||||||
| 34 | my $self=shift; | ||||||||||
| 35 | my $tag=$_[0]->{Name}; | ||||||||||
| 36 | my %args=%{$_[0]->{Attributes}}; | ||||||||||
| 37 | # Your code here. | ||||||||||
| 38 | |||||||||||
| 39 | # Before you conclude the method, write these 2 line. | ||||||||||
| 40 | $self->_visited_count_inc($tag); | ||||||||||
| 41 | $self->_push_tag($tag); | ||||||||||
| 42 | } | ||||||||||
| 43 | |||||||||||
| 44 | 2) In end_element, | ||||||||||
| 45 | |||||||||||
| 46 | sub end_element { | ||||||||||
| 47 | my $self=shift; | ||||||||||
| 48 | my $tag=shift->{Name}; | ||||||||||
| 49 | # Your code here. | ||||||||||
| 50 | |||||||||||
| 51 | # Before you conclude the method, write these 2 lines. | ||||||||||
| 52 | $self->_visited_count_dec($tag); | ||||||||||
| 53 | $self->_pop_tag; | ||||||||||
| 54 | } | ||||||||||
| 55 | |||||||||||
| 56 | 3) In characters, or any other methods where you may use the tag | ||||||||||
| 57 | stack or count | ||||||||||
| 58 | |||||||||||
| 59 | sub characters { | ||||||||||
| 60 | my $self=shift; | ||||||||||
| 61 | my $text=shift->{Data}; | ||||||||||
| 62 | |||||||||||
| 63 | $self->_chars_hash->{$self->_top_tag} .= $text; | ||||||||||
| 64 | |||||||||||
| 65 | } | ||||||||||
| 66 | $count = $self->_visited_count('myTag'); | ||||||||||
| 67 | $tag = $self->_top_tag; | ||||||||||
| 68 | |||||||||||
| 69 | |||||||||||
| 70 | =head1 FEEDBACK | ||||||||||
| 71 | |||||||||||
| 72 | =head2 Mailing Lists | ||||||||||
| 73 | |||||||||||
| 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. | ||||||||||
| 77 | |||||||||||
| 78 | Your participation is much appreciated. | ||||||||||
| 79 | |||||||||||
| 80 | bioperl-l@bioperl.org - General discussion | ||||||||||
| 81 | http://bioperl.org/wiki/Mailing_lists - About the mailing lists | ||||||||||
| 82 | |||||||||||
| 83 | =head2 Support | ||||||||||
| 84 | |||||||||||
| 85 | Please direct usage questions or support issues to the mailing list: | ||||||||||
| 86 | |||||||||||
| 87 | I<bioperl-l@bioperl.org> | ||||||||||
| 88 | |||||||||||
| 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. | ||||||||||
| 93 | |||||||||||
| 94 | =head2 Reporting Bugs | ||||||||||
| 95 | |||||||||||
| 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: | ||||||||||
| 99 | |||||||||||
| 100 | https://github.com/bioperl/bioperl-live/issues | ||||||||||
| 101 | |||||||||||
| 102 | =head1 AUTHOR | ||||||||||
| 103 | |||||||||||
| 104 | Juguang Xiao, juguang@tll.org.sg | ||||||||||
| 105 | |||||||||||
| 106 | =head2 APPENDIX | ||||||||||
| 107 | |||||||||||
| 108 | The rest of the documentation details each of the object methods. | ||||||||||
| 109 | Internal methods are usually preceded with a _ | ||||||||||
| 110 | |||||||||||
| 111 | =cut | ||||||||||
| 112 | |||||||||||
| 113 | package Bio::OntologyIO::Handlers::BaseSAXHandler; | ||||||||||
| 114 | 2 | $Bio::OntologyIO::Handlers::BaseSAXHandler::VERSION = '1.7.8'; | |||||||||
| 115 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 116 | 2 | ▶ | use base qw(Bio::Root::Root); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 117 | |||||||||||
| 118 | |||||||||||
| 119 | 0 | ▶ | sub new { | ||||||||
|
Subroutine
new: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 120 | 0 | my ($class, @args) = @_; | |||||||||
| 121 | 0 | my $self=$class->SUPER::new(@args); | |||||||||
| 122 | 0 | $self->_initialize(@args); | |||||||||
| 123 | 0 | return $self; | |||||||||
| 124 | } | ||||||||||
| 125 | |||||||||||
| 126 | 0 | ▶ | sub _initialize { | ||||||||
|
Subroutine
_initialize: not called CC=1 SCAR=6.9
| |||||||||||
| 127 | 0 | my $self = shift; | |||||||||
| 128 | 0 | $self->{_tag_stack} = []; | |||||||||
| 129 | 0 | $self->{_visited_count} = {}; | |||||||||
| 130 | 0 | $self->{_chars_hash} = {}; | |||||||||
| 131 | 0 | $self->{_current_hash} = {}; | |||||||||
| 132 | } | ||||||||||
| 133 | |||||||||||
| 134 | =head2 _tag_stack | ||||||||||
| 135 | |||||||||||
| 136 | Title : _tag_stack | ||||||||||
| 137 | Usage : @tags = $self->_tag_stack; | ||||||||||
| 138 | Function: Get an array of tags that have been accessed but not enclosed. | ||||||||||
| 139 | Return : | ||||||||||
| 140 | Args : | ||||||||||
| 141 | |||||||||||
| 142 | =cut | ||||||||||
| 143 | |||||||||||
| 144 | 0 | ▶ | sub _tag_stack { | ||||||||
|
Subroutine
_tag_stack: not called CC=1 SCAR=6.9
| |||||||||||
| 145 | 0 | return @{shift->{_tag_stack}}; | |||||||||
| 146 | } | ||||||||||
| 147 | |||||||||||
| 148 | =head2 _push_tag | ||||||||||
| 149 | |||||||||||
| 150 | =cut | ||||||||||
| 151 | |||||||||||
| 152 | 0 | ▶ | sub _push_tag { | ||||||||
|
Subroutine
_push_tag: not called CC=1 SCAR=6.9
| |||||||||||
| 153 | 0 | my($self,$tag)=@_; | |||||||||
| 154 | 0 | push @{$self->{_tag_stack}}, $tag; | |||||||||
| 155 | } | ||||||||||
| 156 | |||||||||||
| 157 | =head2 _pop_tag | ||||||||||
| 158 | |||||||||||
| 159 | =cut | ||||||||||
| 160 | |||||||||||
| 161 | 0 | ▶ | sub _pop_tag { | ||||||||
|
Subroutine
_pop_tag: not called CC=1 SCAR=6.9
| |||||||||||
| 162 | 0 | my $self=shift; | |||||||||
| 163 | 0 | return pop @{$self->{_tag_stack}}; | |||||||||
| 164 | } | ||||||||||
| 165 | |||||||||||
| 166 | =head2 _top_tag | ||||||||||
| 167 | |||||||||||
| 168 | Title : _top_tag | ||||||||||
| 169 | Usage : $top = $self->_top_tag; | ||||||||||
| 170 | Function: get the top tag in the tag stack. | ||||||||||
| 171 | Return : a tag name | ||||||||||
| 172 | Args : [none] | ||||||||||
| 173 | |||||||||||
| 174 | =cut | ||||||||||
| 175 | |||||||||||
| 176 | 0 | ▶ | sub _top_tag { | ||||||||
|
Subroutine
_top_tag: not called CC=1 SCAR=6.9
| |||||||||||
| 177 | 0 | my $self = shift; | |||||||||
| 178 | 0 | my @stack=@{$self->{_tag_stack}}; | |||||||||
| 179 | 0 | return $stack[-1]; | |||||||||
| 180 | # get the last element in an array while remaining it in. There are few ways | ||||||||||
| 181 | # 1) $stack[-1] | ||||||||||
| 182 | # 2) $stack[$#stack] | ||||||||||
| 183 | # 3) $stack[@stack-1] | ||||||||||
| 184 | } | ||||||||||
| 185 | |||||||||||
| 186 | |||||||||||
| 187 | =head2 _chars_hash | ||||||||||
| 188 | |||||||||||
| 189 | Title : _chars_hash | ||||||||||
| 190 | Usage : $hash= $self->_chars_hash; | ||||||||||
| 191 | Function: return the character cache for the specific tag | ||||||||||
| 192 | Return : a hash reference, which is intent for character storage for tags | ||||||||||
| 193 | Args : [none] | ||||||||||
| 194 | |||||||||||
| 195 | =cut | ||||||||||
| 196 | |||||||||||
| 197 | 0 | ▶ | sub _chars_hash { | ||||||||
|
Subroutine
_chars_hash: not called CC=1 SCAR=6.9
| |||||||||||
| 198 | 0 | return shift->{_chars_hash}; | |||||||||
| 199 | } | ||||||||||
| 200 | |||||||||||
| 201 | =head2 _current_hash | ||||||||||
| 202 | |||||||||||
| 203 | =cut | ||||||||||
| 204 | |||||||||||
| 205 | 0 | ▶ | sub _current_hash { | ||||||||
|
Subroutine
_current_hash: not called CC=1 SCAR=6.9
| |||||||||||
| 206 | 0 | return shift->{_current_hash}; | |||||||||
| 207 | } | ||||||||||
| 208 | |||||||||||
| 209 | =head2 _visited_count_inc | ||||||||||
| 210 | |||||||||||
| 211 | Title : _vistied_count_inc | ||||||||||
| 212 | Usage : $self->vistied_count_inc($tag); # the counter for the tag increase | ||||||||||
| 213 | Function: the counter for the tag increase | ||||||||||
| 214 | Return : the current count after this increment | ||||||||||
| 215 | Args : the tag name [scalar] | ||||||||||
| 216 | |||||||||||
| 217 | =cut | ||||||||||
| 218 | |||||||||||
| 219 | 0 | ▶ | sub _visited_count_inc { | ||||||||
|
Subroutine
_visited_count_inc: not called CC=2 SCAR=17.9
| |||||||||||
| 220 | 0 | my ($self, $tag) = @_; | |||||||||
| 221 | 0 | my $visited_count=$self->{_visited_count}; | |||||||||
| 222 | 0 | ▶ | if(exists $visited_count->{$tag}){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 223 | 0 | $visited_count->{$tag}++; | |||||||||
| 224 | }else{ | ||||||||||
| 225 | 0 | $visited_count->{$tag}=1; | |||||||||
| 226 | } | ||||||||||
| 227 | 0 | return $visited_count->{$tag}; | |||||||||
| 228 | } | ||||||||||
| 229 | |||||||||||
| 230 | =head2 _visited_count_dec | ||||||||||
| 231 | |||||||||||
| 232 | Title : _visited_count_dec | ||||||||||
| 233 | Usage : $self->_visited_count_dec($tag); | ||||||||||
| 234 | Function: the counter for the tag decreases by one | ||||||||||
| 235 | Return : the current count for the specific tag after the decrement | ||||||||||
| 236 | Args : the tag name [scalar] | ||||||||||
| 237 | |||||||||||
| 238 | =cut | ||||||||||
| 239 | |||||||||||
| 240 | 0 | ▶ | sub _visited_count_dec { | ||||||||
|
Subroutine
_visited_count_dec: not called CC=2 SCAR=17.9
| |||||||||||
| 241 | 0 | my ($self, $tag) = @_; | |||||||||
| 242 | 0 | my $visited_count=$self->{_visited_count}; | |||||||||
| 243 | 0 | ▶ | if(exists $visited_count->{$tag}){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 244 | 0 | $visited_count->{$tag}--; | |||||||||
| 245 | }else{ | ||||||||||
| 246 | 0 | $self->throw("'$tag' has not been visited yet. How to decrease it?!"); | |||||||||
| 247 | } | ||||||||||
| 248 | 0 | return $visited_count->{$tag}; | |||||||||
| 249 | } | ||||||||||
| 250 | |||||||||||
| 251 | =head2 _visited_count | ||||||||||
| 252 | |||||||||||
| 253 | Title : _visited_count | ||||||||||
| 254 | Usage : $count = $self->_visited_count | ||||||||||
| 255 | Function: return the counter for the tag | ||||||||||
| 256 | Return : the current counter for the specific tag | ||||||||||
| 257 | Args : the tag name [scalar] | ||||||||||
| 258 | |||||||||||
| 259 | =cut | ||||||||||
| 260 | |||||||||||
| 261 | 0 | ▶ | sub _visited_count { | ||||||||
|
Subroutine
_visited_count: not called CC=1 SCAR=6.9
| |||||||||||
| 262 | 0 | my ($self, $tag) = @_; | |||||||||
| 263 | 0 | return $self->{_visited_count}->{$tag}; | |||||||||
| 264 | } | ||||||||||
| 265 | |||||||||||
| 266 | 2 | 1; | |||||||||