| 1 | package InterMine::Model::Handler; | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | InterMine::Model::Handler - The SAX handler for reading in a model | ||
| 6 | |||
| 7 | =head1 SYNOPSIS | ||
| 8 | |||
| 9 | use InterMine::Model; | ||
| 10 | use InterMine::Model::Handler; | ||
| 11 | use XML::Parser::PerlSAX; | ||
| 12 | |||
| 13 | my $handler = InterMine::Model::Handler->new( model => $self ); | ||
| 14 | my $parser = XML::Parser::PerlSAX->new( Handler => $handler ); | ||
| 15 | |||
| 16 | my $source; | ||
| 17 | |||
| 18 | if ($source_is_string) { | ||
| 19 | $source = { String => $source_arg }; | ||
| 20 | } | ||
| 21 | else { | ||
| 22 | $source = { SystemId => $source_arg }; | ||
| 23 | } | ||
| 24 | |||
| 25 | $parser->parse( Source => $source ); | ||
| 26 | |||
| 27 | =head1 DESCRIPTION | ||
| 28 | |||
| 29 | This in a class used internally for processing the xml representation of the model | ||
| 30 | that is returned from webservices, and stored as a the model's serialised | ||
| 31 | representation. You will not need to use this class directly. | ||
| 32 | |||
| 33 | =head1 SEE ALSO | ||
| 34 | |||
| 35 | =over 4 | ||
| 36 | |||
| 37 | =item * L<XML::Parser::PerlSAX> | ||
| 38 | |||
| 39 | =back | ||
| 40 | |||
| 41 | =cut | ||
| 42 | |||
| 43 | use strict; | ||
| 44 | use warnings; | ||
| 45 | |||
| 46 | use Carp qw/confess/; | ||
| 47 | |||
| 48 | use InterMine::Model::Attribute; | ||
| 49 | use InterMine::Model::Reference; | ||
| 50 | use InterMine::Model::Collection; | ||
| 51 | use InterMine::Model::ClassDescriptor; | ||
| 52 | |||
| 53 | use constant ORIGIN => "InterMine"; | ||
| 54 | |||
| 55 | =head1 CLASS METHODS | ||
| 56 | |||
| 57 | =head2 new(model => $model) | ||
| 58 | |||
| 59 | Standard constructor. Takes options as key value pairs, and expects | ||
| 60 | just one option (model). | ||
| 61 | |||
| 62 | =cut | ||
| 63 | |||
| 64 | sub new { | ||
| 65 | my $class = shift; | ||
| 66 | @_ == 2 || confess "Not enough arguments to ${class}::new\n", | ||
| 67 | "Expected: 'model => \$model'\n", | ||
| 68 | "got: ", join('=>', @_); | ||
| 69 | my $self = {@_}; | ||
| 70 | |||
| 71 | return bless $self, $class; | ||
| 72 | } | ||
| 73 | |||
| 74 | my $serial_no = 00; | ||
| 75 | |||
| 76 | =head2 start_element | ||
| 77 | |||
| 78 | implementation of method required by XML::Parser::PerlSAX | ||
| 79 | |||
| 80 | =cut | ||
| 81 | |||
| 82 | sub start_element { | ||
| 83 | my $self = shift; | ||
| 84 | my $args = shift; | ||
| 85 | |||
| 86 | $self->{current_element} = $args->{Name}; | ||
| 87 | |||
| 88 | my $nameattr = $args->{Attributes}{name}; | ||
| 89 | |||
| 90 | if ( $args->{Name} eq "model" ) { | ||
| 91 | $self->{model}{model_name} = $nameattr; | ||
| 92 | $self->{model}{package_name} = $args->{Attributes}{package}; | ||
| 93 | } | ||
| 94 | else { | ||
| 95 | my $model = $self->{model}; | ||
| 96 | if ( $args->{Name} eq "class" ) { | ||
| 97 | my $origin = ORIGIN; | ||
| 98 | $origin .= '::' . $self->{model}{origin} if $self->{model}{origin}; | ||
| 99 | my $perl_package = $self->{model}{perl_package} ||= join('::', | ||
| 100 | $origin, $self->{model}{model_name}, sprintf("%02d", ++$serial_no)) . '::'; | ||
| 101 | my @parents = (); | ||
| 102 | if ( exists $args->{Attributes}{extends} ) { | ||
| 103 | @parents = split /\s+/, $args->{Attributes}{extends}; | ||
| 104 | @parents = grep { $_ ne 'java.lang.Object' } @parents; | ||
| 105 | |||
| 106 | # strip off any preceding package (eg. "org.intermine.") | ||
| 107 | map { s/.*\.(.*)/$1/ } @parents; | ||
| 108 | } | ||
| 109 | my $cd = InterMine::Model::ClassDescriptor->create( | ||
| 110 | $perl_package . $nameattr, | ||
| 111 | model => $model, | ||
| 112 | parents => [ map {$perl_package . $_} @parents], | ||
| 113 | is_interface => ($args->{Attributes}{'is-interface'} eq 'true'), | ||
| 114 | ); | ||
| 115 | $model->{class_hash}{$nameattr} = $cd; | ||
| 116 | $self->{current_class} = $cd; | ||
| 117 | } | ||
| 118 | else { | ||
| 119 | my $field; | ||
| 120 | if ( $args->{Name} eq "attribute" ) { | ||
| 121 | my $type = $args->{Attributes}{type}; | ||
| 122 | $field = InterMine::Model::Attribute->new( | ||
| 123 | name => $nameattr, | ||
| 124 | type => $type, | ||
| 125 | model => $model | ||
| 126 | ); | ||
| 127 | } | ||
| 128 | else { | ||
| 129 | my $referenced_type | ||
| 130 | = $args->{Attributes}{'referenced-type'}; | ||
| 131 | my $reverse_reference | ||
| 132 | = $args->{Attributes}{'reverse-reference'}; | ||
| 133 | |||
| 134 | my %args = ( | ||
| 135 | name => $nameattr, | ||
| 136 | referenced_type_name => $referenced_type, | ||
| 137 | model => $model | ||
| 138 | ); | ||
| 139 | $args{reverse_reference_name} = $reverse_reference | ||
| 140 | if $reverse_reference; | ||
| 141 | |||
| 142 | if ( $args->{Name} eq "reference" ) { | ||
| 143 | $field = InterMine::Model::Reference->new(%args); | ||
| 144 | } | ||
| 145 | elsif ( $args->{Name} eq "collection" ) { | ||
| 146 | $field = InterMine::Model::Collection->new(%args); | ||
| 147 | } | ||
| 148 | else { | ||
| 149 | confess "unexpected element: ", $args->{Name}, "\n"; | ||
| 150 | } | ||
| 151 | |||
| 152 | } | ||
| 153 | $self->{current_class}->add_field( $field, 'own' ); | ||
| 154 | } | ||
| 155 | } | ||
| 156 | } | ||
| 157 | |||
| 158 | =head2 end_element | ||
| 159 | |||
| 160 | implementation of method required by XML::Parser::PerlSAX | ||
| 161 | |||
| 162 | =cut | ||
| 163 | |||
| 164 | sub end_element { | ||
| 165 | my $self = shift; | ||
| 166 | my $args = shift; | ||
| 167 | if ( $args->{Name} eq 'class' ) { | ||
| 168 | $self->{current_class} = undef; | ||
| 169 | } | ||
| 170 | } | ||
| 171 | |||
| 172 | 1; | ||
| 173 | |||
| 174 | =head1 AUTHOR | ||
| 175 | |||
| 176 | FlyMine C<< <support@flymine.org> >> | ||
| 177 | |||
| 178 | =head1 BUGS | ||
| 179 | |||
| 180 | Please report any bugs or feature requests to C<support@flymine.org>. | ||
| 181 | |||
| 182 | =head1 SUPPORT | ||
| 183 | |||
| 184 | You can find documentation for this module with the perldoc command. | ||
| 185 | |||
| 186 | perldoc InterMine::Model | ||
| 187 | |||
| 188 | You can also look for information at: | ||
| 189 | |||
| 190 | =over 4 | ||
| 191 | |||
| 192 | =item * FlyMine | ||
| 193 | |||
| 194 | L<http://www.flymine.org> | ||
| 195 | |||
| 196 | =back | ||
| 197 | |||
| 198 | =head1 COPYRIGHT & LICENSE | ||
| 199 | |||
| 200 | Copyright 2006,2007,2008,2009, 2010, 2011 FlyMine, all rights reserved. | ||
| 201 | |||
| 202 | This program is free software; you can redistribute it and/or modify it | ||
| 203 | under the same terms as Perl itself. | ||
| 204 |