| 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 | FAST::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://redmine.open-bio.org/projects/bioperl/ | ||
| 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 | Interal methods are usually preceded with a _ | ||
| 110 | |||
| 111 | =cut | ||
| 112 | |||
| 113 | package FAST::Bio::OntologyIO::Handlers::BaseSAXHandler; | ||
| 114 | use strict; | ||
| 115 | use base qw(FAST::Bio::Root::Root); | ||
| 116 | |||
| 117 | |||
| 118 | sub new { | ||
| 119 | my ($class, @args) = @_; | ||
| 120 | my $self=$class->SUPER::new(@args); | ||
| 121 | $self->_initialize(@args); | ||
| 122 | return $self; | ||
| 123 | } | ||
| 124 | |||
| 125 | sub _initialize { | ||
| 126 | my $self = shift; | ||
| 127 | $self->{_tag_stack} = []; | ||
| 128 | $self->{_visited_count} = {}; | ||
| 129 | $self->{_chars_hash} = {}; | ||
| 130 | $self->{_current_hash} = {}; | ||
| 131 | } | ||
| 132 | |||
| 133 | =head2 _tag_stack | ||
| 134 | |||
| 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 : | ||
| 140 | |||
| 141 | =cut | ||
| 142 | |||
| 143 | sub _tag_stack { | ||
| 144 | return @{shift->{_tag_stack}}; | ||
| 145 | } | ||
| 146 | |||
| 147 | =head2 _push_tag | ||
| 148 | |||
| 149 | =cut | ||
| 150 | |||
| 151 | sub _push_tag { | ||
| 152 | my($self,$tag)=@_; | ||
| 153 | push @{$self->{_tag_stack}}, $tag; | ||
| 154 | } | ||
| 155 | |||
| 156 | =head2 _pop_tag | ||
| 157 | |||
| 158 | =cut | ||
| 159 | |||
| 160 | sub _pop_tag { | ||
| 161 | my $self=shift; | ||
| 162 | return pop @{$self->{_tag_stack}}; | ||
| 163 | } | ||
| 164 | |||
| 165 | =head2 _top_tag | ||
| 166 | |||
| 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] | ||
| 172 | |||
| 173 | =cut | ||
| 174 | |||
| 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] | ||
| 183 | } | ||
| 184 | |||
| 185 | |||
| 186 | =head2 _chars_hash | ||
| 187 | |||
| 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] | ||
| 193 | |||
| 194 | =cut | ||
| 195 | |||
| 196 | sub _chars_hash { | ||
| 197 | return shift->{_chars_hash}; | ||
| 198 | } | ||
| 199 | |||
| 200 | =head2 _current_hash | ||
| 201 | |||
| 202 | =cut | ||
| 203 | |||
| 204 | sub _current_hash { | ||
| 205 | return shift->{_current_hash}; | ||
| 206 | } | ||
| 207 | |||
| 208 | =head2 _visited_count_inc | ||
| 209 | |||
| 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] | ||
| 215 | |||
| 216 | =cut | ||
| 217 | |||
| 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; | ||
| 225 | } | ||
| 226 | return $visited_count->{$tag}; | ||
| 227 | } | ||
| 228 | |||
| 229 | =head2 _visited_count_dec | ||
| 230 | |||
| 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] | ||
| 236 | |||
| 237 | =cut | ||
| 238 | |||
| 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?!"); | ||
| 246 | } | ||
| 247 | return $visited_count->{$tag}; | ||
| 248 | } | ||
| 249 | |||
| 250 | =head2 _visited_count | ||
| 251 | |||
| 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] | ||
| 257 | |||
| 258 | =cut | ||
| 259 | |||
| 260 | sub _visited_count { | ||
| 261 | my ($self, $tag) = @_; | ||
| 262 | return $self->{_visited_count}->{$tag}; | ||
| 263 | } | ||
| 264 | |||
| 265 | 1; |