| 1 | package MS::Reader::XML::Record; | ||
| 2 | |||
| 3 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | |||
| 6 | 1 | ▶ | use Carp; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 1 | ▶ | use Data::Dumper; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 1 | ▶ | use XML::Parser; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 9 | |||
| 10 | |||
| 11 | sub new { | ||
| 12 | |||
| 13 | my ($class, %args) = @_; | ||
| 14 | my $self = bless {}, $class; | ||
| 15 | |||
| 16 | $self->{__use_cache} = $args{use_cache} ? 1 : 0; | ||
| 17 | |||
| 18 | # parse XML into object | ||
| 19 | if (defined $args{xml}) { | ||
| 20 | |||
| 21 | # initialize pointer | ||
| 22 | $self->{_curr_ref} = $self; | ||
| 23 | $self->{filter} = $args{filter}; # may be undef | ||
| 24 | |||
| 25 | $self->_pre_load(); | ||
| 26 | |||
| 27 | my $p = XML::Parser->new(); | ||
| 28 | $p->setHandlers( | ||
| 29 | Start => sub{ $self->_handle_start( @_ ) }, | ||
| 30 | End => sub{ $self->_handle_end( @_ ) }, | ||
| 31 | Char => sub{ $self->_handle_char( @_ ) }, | ||
| 32 | ); | ||
| 33 | $p->parse($args{xml}); | ||
| 34 | |||
| 35 | $self->_post_load(); | ||
| 36 | |||
| 37 | delete $self->{_curr_ref}; # avoid circular reference mem leak | ||
| 38 | delete $self->{filter}; | ||
| 39 | |||
| 40 | # strip toplevel | ||
| 41 | my $toplevel = $self->{_toplevel}; | ||
| 42 | if (defined $toplevel) { | ||
| 43 | $self->{$_} = $self->{$toplevel}->{$_} | ||
| 44 | for (keys %{ $self->{$toplevel} }); | ||
| 45 | delete $self->{$toplevel}; | ||
| 46 | } | ||
| 47 | |||
| 48 | # delete temporary entries (start with "_") | ||
| 49 | for (keys %{$self}) { | ||
| 50 | delete $self->{$_} if ($_ =~ /^_/); | ||
| 51 | } | ||
| 52 | |||
| 53 | } | ||
| 54 | return $self; | ||
| 55 | |||
| 56 | } | ||
| 57 | |||
| 58 | |||
| 59 | sub _handle_start { | ||
| 60 | |||
| 61 | my ($self, $p, $el, %attrs) = @_; | ||
| 62 | |||
| 63 | my $new_ref = \%attrs; | ||
| 64 | $new_ref->{back} = $self->{_curr_ref}; | ||
| 65 | |||
| 66 | # Elements that should be grouped by name/id | ||
| 67 | if (defined $self->{_make_named_array}->{ $el }) { | ||
| 68 | |||
| 69 | my $id_name = $self->{_make_named_array}->{ $el }; | ||
| 70 | my $id = $attrs{$id_name}; | ||
| 71 | delete $new_ref->{$id_name}; | ||
| 72 | push @{ $self->{_curr_ref}->{$el}->{$id} }, $new_ref; | ||
| 73 | |||
| 74 | # filters are used to short-circuit parses that don't match a given | ||
| 75 | # criteria. In some cases this can speed up sequential parsing | ||
| 76 | # significantly | ||
| 77 | if ($el eq 'cvParam' && defined $self->{filter}) { | ||
| 78 | if ($id eq $self->{filter}->[0] | ||
| 79 | && $attrs{value} != $self->{filter}->[1]) { | ||
| 80 | $self->{filtered} = 1; | ||
| 81 | |||
| 82 | # remove circular references | ||
| 83 | delete $self->{_curr_ref}->{back}; | ||
| 84 | delete $new_ref->{back}; | ||
| 85 | |||
| 86 | $p->finish; | ||
| 87 | } | ||
| 88 | } | ||
| 89 | |||
| 90 | } | ||
| 91 | |||
| 92 | # Elements that should be grouped by name/id | ||
| 93 | elsif (defined $self->{_make_named_hash}->{ $el }) { | ||
| 94 | my $id_name = $self->{_make_named_hash}->{ $el }; | ||
| 95 | my $id = $attrs{$id_name}; | ||
| 96 | die "Colliding ID $id" | ||
| 97 | if (defined $self->{_curr_ref}->{$el}->{$id}); | ||
| 98 | delete $new_ref->{$id_name}; | ||
| 99 | $self->{_curr_ref}->{$el}->{$id} = $new_ref; | ||
| 100 | } | ||
| 101 | |||
| 102 | # Elements that should be grouped with no name | ||
| 103 | elsif (defined $self->{_make_anon_array}->{ $el } ) { | ||
| 104 | push @{ $self->{_curr_ref}->{$el} }, $new_ref; | ||
| 105 | } | ||
| 106 | |||
| 107 | # Everything else | ||
| 108 | else { | ||
| 109 | $self->{_curr_ref}->{$el} = $new_ref; | ||
| 110 | } | ||
| 111 | |||
| 112 | # Step up linked list | ||
| 113 | $self->{_curr_ref} = $new_ref; | ||
| 114 | |||
| 115 | return; | ||
| 116 | |||
| 117 | } | ||
| 118 | |||
| 119 | sub _handle_end { | ||
| 120 | |||
| 121 | my ($self, $p, $el) = @_; | ||
| 122 | |||
| 123 | # step back down linked list | ||
| 124 | my $last_ref = $self->{_curr_ref}->{back}; | ||
| 125 | delete $self->{_curr_ref}->{back}; # avoid memory leak! | ||
| 126 | $self->{_curr_ref} = $last_ref; | ||
| 127 | |||
| 128 | return; | ||
| 129 | |||
| 130 | } | ||
| 131 | |||
| 132 | sub _handle_char { | ||
| 133 | |||
| 134 | my ($self, $p, $data) = @_; | ||
| 135 | $self->{_curr_ref}->{pcdata} .= $data | ||
| 136 | if ($data =~ /\S/); | ||
| 137 | return; | ||
| 138 | |||
| 139 | } | ||
| 140 | |||
| 141 | sub dump { | ||
| 142 | |||
| 143 | my ($self) = @_; | ||
| 144 | my $copy = {}; | ||
| 145 | %$copy = %$self; | ||
| 146 | |||
| 147 | delete $copy->{$_} | ||
| 148 | for qw/count md5sum version fh offsets fn index fh pos lengths/; | ||
| 149 | |||
| 150 | my $dump = ''; | ||
| 151 | |||
| 152 | { | ||
| 153 | local $Data::Dumper::Indent = 1; | ||
| 154 | local $Data::Dumper::Terse = 1; | ||
| 155 | local $Data::Dumper::Sortkeys = 1; | ||
| 156 | $dump = Dumper $copy; | ||
| 157 | } | ||
| 158 | |||
| 159 | return $dump; | ||
| 160 | |||
| 161 | } | ||
| 162 | |||
| 163 | sub _pre_load {} # can be defined by subclass | ||
| 164 | sub _post_load {} # can be defined by subclass | ||
| 165 | |||
| 166 | 1; | ||
| 167 | |||
| 168 | |||
| 169 | __END__ | ||