| 1 | # Creation date: 2008-12-02T09:05:03Z | ||
| 2 | # Authors: don | ||
| 3 | # $Revision: 1599 $ | ||
| 4 | |||
| 5 | # Copyright (c) 2005-2009 Don Owens | ||
| 6 | # | ||
| 7 | # All rights reserved. This program is free software; you can | ||
| 8 | # redistribute it and/or modify it under the same terms as Perl | ||
| 9 | # itself. | ||
| 10 | |||
| 11 | =pod | ||
| 12 | |||
| 13 | =head1 NAME | ||
| 14 | |||
| 15 | XML::Parser::Wrapper::SAXHandler - SAX handler for XML::Parser::Wrapper | ||
| 16 | |||
| 17 | =head1 SYNOPSIS | ||
| 18 | |||
| 19 | Not meant be used externally. | ||
| 20 | |||
| 21 | =head1 DESCRIPTION | ||
| 22 | |||
| 23 | =cut | ||
| 24 | |||
| 25 | use strict; | ||
| 26 | use warnings; | ||
| 27 | |||
| 28 | package XML::Parser::Wrapper::SAXHandler; | ||
| 29 | |||
| 30 | our $VERSION = '0.01'; | ||
| 31 | |||
| 32 | use base qw(XML::SAX::Base); | ||
| 33 | |||
| 34 | =pod | ||
| 35 | |||
| 36 | =head1 METHODS | ||
| 37 | |||
| 38 | |||
| 39 | =cut | ||
| 40 | sub new { | ||
| 41 | my ($class, $params) = @_; | ||
| 42 | $params = { } unless $params; | ||
| 43 | |||
| 44 | my $self = bless { params => { %$params }, _cur_depth => 0 }, ref($class) || $class; | ||
| 45 | |||
| 46 | unless ($params->{start_tag}) { | ||
| 47 | $self->{data} = [ ]; | ||
| 48 | $self->{stack} = [$self->{data} ]; | ||
| 49 | $self->{extra_data} = { }; | ||
| 50 | } | ||
| 51 | |||
| 52 | # print Data::Dumper->Dump([ $self ], [ 'self' ]) . "\n\n"; | ||
| 53 | |||
| 54 | return $self; | ||
| 55 | } | ||
| 56 | |||
| 57 | sub get_tree { | ||
| 58 | my ($self) = @_; | ||
| 59 | |||
| 60 | return $self->{data}; | ||
| 61 | } | ||
| 62 | |||
| 63 | sub characters { | ||
| 64 | my ($self, $chars) = @_; | ||
| 65 | |||
| 66 | if ($self->{params}{start_tag}) { | ||
| 67 | if ($self->{_in_tag} and $self->{_in_section}) { | ||
| 68 | my $data = [ 0, $chars->{Data} ]; | ||
| 69 | my $stack = $self->{stack}; | ||
| 70 | my $cur_data = $stack->[$#$stack]; | ||
| 71 | |||
| 72 | if (ref($cur_data->[$#$cur_data])) { | ||
| 73 | push @$cur_data, @$data; | ||
| 74 | } elsif ($cur_data and @$cur_data) { | ||
| 75 | $cur_data->[$#{$cur_data}] .= $chars->{Data}; | ||
| 76 | } | ||
| 77 | } | ||
| 78 | } | ||
| 79 | else { | ||
| 80 | my $data = [ 0, $chars->{Data} ]; | ||
| 81 | my $stack = $self->{stack}; | ||
| 82 | my $cur_data = $stack->[$#$stack]; | ||
| 83 | |||
| 84 | if (ref($cur_data->[$#$cur_data])) { | ||
| 85 | push @$cur_data, @$data; | ||
| 86 | } elsif ($cur_data and @$cur_data) { | ||
| 87 | $cur_data->[$#{$cur_data}] .= $chars->{Data}; | ||
| 88 | } | ||
| 89 | } | ||
| 90 | |||
| 91 | } | ||
| 92 | |||
| 93 | sub _do_section_chunk { | ||
| 94 | my ($self, $e) = @_; | ||
| 95 | |||
| 96 | my %attrs_data = %{$e->{Attributes}}; | ||
| 97 | my $name = $e->{Name}; | ||
| 98 | |||
| 99 | my %attrs = map { ($_->{Name} => XML::Parser::Wrapper::AttributeVal->new($_)) } | ||
| 100 | values %attrs_data; | ||
| 101 | |||
| 102 | my $data = [ $name, [ \%attrs ] ]; | ||
| 103 | my $stack = $self->{stack}; | ||
| 104 | my $cur_data = $stack->[$#$stack]; | ||
| 105 | push @$cur_data, @$data; | ||
| 106 | push @$stack, $data->[1]; | ||
| 107 | |||
| 108 | return 1; | ||
| 109 | } | ||
| 110 | |||
| 111 | sub _do_start_section { | ||
| 112 | my ($self, $e) = @_; | ||
| 113 | |||
| 114 | my %attrs = map { ($_->{Name} => XML::Parser::Wrapper::AttributeVal->new($_)) } | ||
| 115 | values %{$e->{Attributes}}; | ||
| 116 | my $name = $e->{Name}; | ||
| 117 | |||
| 118 | $self->{data} = [ $name, [ \%attrs ] ]; | ||
| 119 | $self->{stack} = [ $self->{data}, $self->{data}[1] ]; | ||
| 120 | |||
| 121 | return 1; | ||
| 122 | } | ||
| 123 | |||
| 124 | sub start_element { | ||
| 125 | my ($self, $e) = @_; | ||
| 126 | my $name = $e->{Name}; | ||
| 127 | |||
| 128 | $self->{_in_tag} = 1; | ||
| 129 | |||
| 130 | $self->{_cur_depth}++; | ||
| 131 | |||
| 132 | my $start_tag = $self->{params}{start_tag}; | ||
| 133 | |||
| 134 | if ($start_tag) { | ||
| 135 | if ($name eq $start_tag) { | ||
| 136 | $self->{_start_tag_levels}++; | ||
| 137 | } | ||
| 138 | |||
| 139 | if ($name eq $start_tag | ||
| 140 | and (not $self->{_start_depth} or $self->{_cur_depth} == $self->{_start_depth})) { | ||
| 141 | |||
| 142 | unless ($self->{_start_depth}) { | ||
| 143 | # how many of the same tag name deep to start | ||
| 144 | my $user_depth = $self->{params}{start_depth}; | ||
| 145 | |||
| 146 | if ($user_depth) { | ||
| 147 | if ($self->{_start_tag_levels} == $user_depth) { | ||
| 148 | $self->{_in_section} = 1; | ||
| 149 | $self->{_start_depth} = $self->{_cur_depth}; | ||
| 150 | return $self->_do_start_section($e); | ||
| 151 | } | ||
| 152 | else { | ||
| 153 | return $self->_do_section_chunk($e); | ||
| 154 | } | ||
| 155 | } | ||
| 156 | |||
| 157 | $self->{_start_depth} = $self->{_cur_depth}; | ||
| 158 | } | ||
| 159 | |||
| 160 | $self->{_in_section} = 1; | ||
| 161 | return $self->_do_start_section($e); | ||
| 162 | } elsif ($self->{_in_section}) { | ||
| 163 | return $self->_do_section_chunk($e); | ||
| 164 | } | ||
| 165 | } | ||
| 166 | else { | ||
| 167 | return $self->_do_section_chunk($e); | ||
| 168 | } | ||
| 169 | } | ||
| 170 | |||
| 171 | sub end_element { | ||
| 172 | my ($self, $e) = @_; | ||
| 173 | my $name = $e->{Name}; | ||
| 174 | |||
| 175 | my $last_entry = pop @{$self->{stack}}; | ||
| 176 | |||
| 177 | $self->{_in_tag} = 0; | ||
| 178 | |||
| 179 | my $start_tag = $self->{params}{start_tag}; | ||
| 180 | |||
| 181 | if ($start_tag and $name eq $start_tag | ||
| 182 | and defined($self->{_start_depth}) and $self->{_cur_depth} == $self->{_start_depth}) { | ||
| 183 | $self->{_in_section} = 0; | ||
| 184 | |||
| 185 | my $listing = XML::Parser::Wrapper->new_from_tree([ @{$self->{data}} ]); | ||
| 186 | |||
| 187 | my $handler = $self->{params}{handler}; | ||
| 188 | if ($handler) { | ||
| 189 | if (ref($handler) eq 'ARRAY') { | ||
| 190 | my ($obj, $method) = @$handler; | ||
| 191 | $obj->$method($listing); | ||
| 192 | } else { | ||
| 193 | $handler->($listing); | ||
| 194 | } | ||
| 195 | } | ||
| 196 | } | ||
| 197 | |||
| 198 | $self->{_cur_depth}--; | ||
| 199 | |||
| 200 | if ($start_tag eq $name) { | ||
| 201 | $self->{_start_tag_levels}--; | ||
| 202 | } | ||
| 203 | } | ||
| 204 | |||
| 205 | # sub doctype_decl { | ||
| 206 | # my ($self, @args) = @_; | ||
| 207 | |||
| 208 | # use Data::Dumper; | ||
| 209 | |||
| 210 | # print STDERR Data::Dumper->Dump([ \@args ], [ 'doctype_args' ]) . "\n\n"; | ||
| 211 | |||
| 212 | # return $self->SUPER::doctype_decl(@args); | ||
| 213 | # } | ||
| 214 | |||
| 215 | sub xml_decl { | ||
| 216 | my ($self, $data) = @_; | ||
| 217 | |||
| 218 | $self->{extra_data}{xml_decl} = { version => $data->{Version}, encoding => $data->{Encoding}, | ||
| 219 | standalone => $data->{Standalone} }; | ||
| 220 | |||
| 221 | # use Data::Dumper; | ||
| 222 | |||
| 223 | # print STDERR Data::Dumper->Dump([ $data ], [ 'xml_decl_arg' ]) . "\n\n"; | ||
| 224 | |||
| 225 | return $self->SUPER::xml_decl($data); | ||
| 226 | } | ||
| 227 | |||
| 228 | # sub notation_decl { | ||
| 229 | # my ($self, $data) = @_; | ||
| 230 | |||
| 231 | # use Data::Dumper; | ||
| 232 | # print STDERR Data::Dumper->Dump([ $data ], [ 'notation_decl' ]) . "\n\n"; | ||
| 233 | |||
| 234 | |||
| 235 | # return $self->SUPER::notation_decl($data); | ||
| 236 | # } | ||
| 237 | |||
| 238 | # sub start_document { | ||
| 239 | # my ($self, $data) = @_; | ||
| 240 | |||
| 241 | # print STDERR Data::Dumper->Dump([ $data ], [ 'start_document' ]) . "\n\n"; | ||
| 242 | |||
| 243 | |||
| 244 | # return $self->SUPER::start_document($data); | ||
| 245 | # } | ||
| 246 | |||
| 247 | =pod | ||
| 248 | |||
| 249 | =head1 AUTHOR | ||
| 250 | |||
| 251 | Don Owens <don@regexguy.com> | ||
| 252 | |||
| 253 | =head1 LICENSE AND COPYRIGHT | ||
| 254 | |||
| 255 | Copyright (c) 2008 Don Owens <don@owensnet.com>. All rights reserved. | ||
| 256 | |||
| 257 | This is free software; you can redistribute it and/or modify it | ||
| 258 | under the same terms as Perl itself. See perlartistic. | ||
| 259 | |||
| 260 | This program is distributed in the hope that it will be | ||
| 261 | useful, but WITHOUT ANY WARRANTY; without even the implied | ||
| 262 | warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR | ||
| 263 | PURPOSE. | ||
| 264 | |||
| 265 | =cut | ||
| 266 | |||
| 267 | 1; | ||
| 268 | |||
| 269 | # Local Variables: # | ||
| 270 | # mode: perl # | ||
| 271 | # tab-width: 4 # | ||
| 272 | # indent-tabs-mode: nil # | ||
| 273 | # cperl-indent-level: 4 # | ||
| 274 | # perl-indent-level: 4 # | ||
| 275 | # End: # | ||
| 276 | # vim:set ai si et sta ts=4 sw=4 sts=4: |