| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package XML::ExtOn::Attributes; | 
| 2 |  |  |  |  |  |  |  | 
| 3 |  |  |  |  |  |  | #$Id: Attributes.pm 385 2008-12-30 15:46:21Z zag $ | 
| 4 |  |  |  |  |  |  |  | 
| 5 | 9 |  |  | 9 |  | 31290 | use Carp; | 
|  | 9 |  |  |  |  | 18 |  | 
|  | 9 |  |  |  |  | 535 |  | 
| 6 | 9 |  |  | 9 |  | 46 | use Data::Dumper; | 
|  | 9 |  |  |  |  | 17 |  | 
|  | 9 |  |  |  |  | 606 |  | 
| 7 | 9 |  |  | 9 |  | 459 | use XML::ExtOn::TieAttrs; | 
|  | 9 |  |  |  |  | 18 |  | 
|  | 9 |  |  |  |  | 178 |  | 
| 8 | 9 |  |  | 9 |  | 4745 | use XML::ExtOn::TieAttrsName; | 
|  | 9 |  |  |  |  | 23 |  | 
|  | 9 |  |  |  |  | 349 |  | 
| 9 |  |  |  |  |  |  | for my $key (qw/ _context _a_stack/) { | 
| 10 | 9 |  |  | 9 |  | 55 | no strict 'refs'; | 
|  | 9 |  |  |  |  | 24 |  | 
|  | 9 |  |  |  |  | 829 |  | 
| 11 |  |  |  |  |  |  | *{ __PACKAGE__ . "::$key" } = sub { | 
| 12 | 26 |  |  | 26 |  | 39 | my $self = shift; | 
| 13 | 26 | 100 |  |  |  | 74 | $self->{$key} = $_[0] if @_; | 
| 14 | 26 |  |  |  |  | 130 | return $self->{$key}; | 
| 15 |  |  |  |  |  |  | } | 
| 16 |  |  |  |  |  |  | } | 
| 17 | 9 |  |  | 9 |  | 43 | use strict; | 
|  | 9 |  |  |  |  | 15 |  | 
|  | 9 |  |  |  |  | 247 |  | 
| 18 | 9 |  |  | 9 |  | 42 | use warnings; | 
|  | 9 |  |  |  |  | 15 |  | 
|  | 9 |  |  |  |  | 6522 |  | 
| 19 |  |  |  |  |  |  |  | 
| 20 |  |  |  |  |  |  | sub new { | 
| 21 | 4 |  |  | 4 | 0 | 22 | my ( $class, %attr ) = @_; | 
| 22 | 4 |  |  |  |  | 13 | my $self = bless {}, $class; | 
| 23 | 4 | 50 |  |  |  | 17 | $self->_context( $attr{context} ) or die "not exists context parametr"; | 
| 24 | 4 |  |  |  |  | 8 | my @a_stack = (); | 
| 25 | 4 | 50 |  |  |  | 23 | if ( my $sax2 = $attr{sax2} ) { | 
| 26 |  |  |  |  |  |  |  | 
| 27 |  |  |  |  |  |  | #walk through sax2 attrs | 
| 28 |  |  |  |  |  |  | # and register namespaces | 
| 29 | 4 |  |  |  |  | 15 | for ( values %$sax2 ) { | 
| 30 | 13 |  |  |  |  | 31 | my ( $prefix, $ns_uri ) = ( $_->{Prefix}, $_->{NamespaceURI} ); | 
| 31 | 13 | 100 | 100 |  |  | 57 | if ( defined $prefix && $prefix eq 'xmlns' ) { | 
| 32 | 4 |  |  |  |  | 13 | $self->_context->declare_prefix( $_->{LocalName}, $_->{Value} ); | 
| 33 |  |  |  |  |  |  | } | 
| 34 |  |  |  |  |  |  |  | 
| 35 |  |  |  |  |  |  | #set default namespace | 
| 36 | 13 | 100 |  |  |  | 44 | if ( $_->{Name} eq 'xmlns' ) { | 
| 37 |  |  |  |  |  |  |  | 
| 38 |  |  |  |  |  |  | #warn "register deafault ns".$a->{Value}; | 
| 39 | 1 |  |  |  |  | 4 | $self->_context->declare_prefix( '', $_->{Value} ); | 
| 40 |  |  |  |  |  |  | } | 
| 41 |  |  |  |  |  |  | } | 
| 42 |  |  |  |  |  |  |  | 
| 43 |  |  |  |  |  |  | #now set default namespaces | 
| 44 |  |  |  |  |  |  | # and | 
| 45 | 4 |  |  |  |  | 14 | my $default_uri = $self->_context->get_uri(''); | 
| 46 | 4 |  |  |  |  | 174 | for ( values %$sax2 ) { | 
| 47 |  |  |  |  |  |  |  | 
| 48 |  |  |  |  |  |  | #save original data from changes | 
| 49 | 13 |  |  |  |  | 18 | my %val = %{$_}; | 
|  | 13 |  |  |  |  | 67 |  | 
| 50 | 13 | 100 | 100 |  |  | 73 | $val{NamespaceURI} = $default_uri | 
| 51 |  |  |  |  |  |  | unless $val{Prefix} || $val{Name} eq 'xmlns'; | 
| 52 | 13 |  |  |  |  | 31 | push @a_stack, \%val; | 
| 53 |  |  |  |  |  |  | } | 
| 54 |  |  |  |  |  |  |  | 
| 55 |  |  |  |  |  |  | } | 
| 56 | 4 |  |  |  |  | 15 | $self->_a_stack( \@a_stack ); | 
| 57 | 4 |  |  |  |  | 18 | return $self; | 
| 58 |  |  |  |  |  |  | } | 
| 59 |  |  |  |  |  |  |  | 
| 60 |  |  |  |  |  |  | =head2 to_sax2 | 
| 61 |  |  |  |  |  |  |  | 
| 62 |  |  |  |  |  |  | Export attributes to sax2 structures | 
| 63 |  |  |  |  |  |  |  | 
| 64 |  |  |  |  |  |  | =cut | 
| 65 |  |  |  |  |  |  |  | 
| 66 |  |  |  |  |  |  | sub to_sax2 { | 
| 67 | 1 |  |  | 1 | 1 | 4 | my $self  = shift; | 
| 68 | 1 |  |  |  |  | 3 | my $attrs = $self->_a_stack; | 
| 69 | 1 |  |  |  |  | 3 | my %res   = (); | 
| 70 | 1 |  |  |  |  | 3 | foreach my $rec (@$attrs) { | 
| 71 | 5 |  |  |  |  | 8 | my %val = %{$rec}; | 
|  | 5 |  |  |  |  | 25 |  | 
| 72 |  |  |  |  |  |  |  | 
| 73 |  |  |  |  |  |  | #clean default uri | 
| 74 | 5 | 100 |  |  |  | 15 | $val{NamespaceURI} = undef unless $val{Prefix}; | 
| 75 |  |  |  |  |  |  |  | 
| 76 | 5 |  | 100 |  |  | 24 | my $key = "{" . ( $val{NamespaceURI} || '' ) . "}$val{LocalName}"; | 
| 77 | 5 |  |  |  |  | 16 | $res{$key} = \%val | 
| 78 |  |  |  |  |  |  |  | 
| 79 |  |  |  |  |  |  | #        warn Dumper $rec; | 
| 80 |  |  |  |  |  |  | } | 
| 81 | 1 |  |  |  |  | 5 | return \%res; | 
| 82 |  |  |  |  |  |  | } | 
| 83 |  |  |  |  |  |  |  | 
| 84 |  |  |  |  |  |  | sub ns { | 
| 85 | 2 |  |  | 2 | 0 | 5 | return $_[0]->_context; | 
| 86 |  |  |  |  |  |  | } | 
| 87 |  |  |  |  |  |  |  | 
| 88 |  |  |  |  |  |  | =head2 by_prefix $prefix | 
| 89 |  |  |  |  |  |  |  | 
| 90 |  |  |  |  |  |  | Create hash for attributes by prefix $prefix | 
| 91 |  |  |  |  |  |  |  | 
| 92 |  |  |  |  |  |  | =cut | 
| 93 |  |  |  |  |  |  |  | 
| 94 |  |  |  |  |  |  | sub by_prefix { | 
| 95 | 1 |  |  | 1 | 1 | 2 | my $self   = shift; | 
| 96 | 1 |  |  |  |  | 1 | my $prefix = shift; | 
| 97 | 1 |  |  |  |  | 2 | my %hash   = (); | 
| 98 | 1 | 50 |  |  |  | 3 | my $ns_uri = $self->ns->get_uri($prefix) | 
| 99 |  |  |  |  |  |  | or die "get_uri($prefix) return undef"; | 
| 100 | 1 |  |  |  |  | 22 | tie %hash, 'XML::ExtOn::TieAttrs', $self->_a_stack, | 
| 101 |  |  |  |  |  |  | by       => 'Prefix', | 
| 102 |  |  |  |  |  |  | value    => $prefix, | 
| 103 |  |  |  |  |  |  | template => { | 
| 104 |  |  |  |  |  |  | Value        => '', | 
| 105 |  |  |  |  |  |  | NamespaceURI => $ns_uri, | 
| 106 |  |  |  |  |  |  | Name         => '', | 
| 107 |  |  |  |  |  |  | LocalName    => '', | 
| 108 |  |  |  |  |  |  | Prefix       => '' | 
| 109 |  |  |  |  |  |  | }; | 
| 110 | 1 |  |  |  |  | 10 | return \%hash; | 
| 111 |  |  |  |  |  |  | } | 
| 112 |  |  |  |  |  |  |  | 
| 113 |  |  |  |  |  |  | =head2 by_ns_uri $ns_uri | 
| 114 |  |  |  |  |  |  |  | 
| 115 |  |  |  |  |  |  | Create hash for attributes for namespace $ns_uri | 
| 116 |  |  |  |  |  |  |  | 
| 117 |  |  |  |  |  |  | =cut | 
| 118 |  |  |  |  |  |  |  | 
| 119 |  |  |  |  |  |  | sub by_ns_uri { | 
| 120 | 1 |  |  | 1 | 1 | 2 | my $self   = shift; | 
| 121 | 1 |  |  |  |  | 2 | my $ns_uri = shift; | 
| 122 | 1 |  |  |  |  | 7 | my %hash   = (); | 
| 123 | 1 |  |  |  |  | 3 | my $prefix = $self->ns->get_prefix($ns_uri); | 
| 124 | 1 | 50 |  |  |  | 134 | die "get_prefix($ns_uri) return undef" unless defined($prefix); | 
| 125 | 1 |  |  |  |  | 3 | tie %hash, 'XML::ExtOn::TieAttrs', $self->_a_stack, | 
| 126 |  |  |  |  |  |  | by       => 'NamespaceURI', | 
| 127 |  |  |  |  |  |  | value    => $ns_uri, | 
| 128 |  |  |  |  |  |  | template => { | 
| 129 |  |  |  |  |  |  | Value        => '', | 
| 130 |  |  |  |  |  |  | NamespaceURI => '', | 
| 131 |  |  |  |  |  |  | Name         => '', | 
| 132 |  |  |  |  |  |  | LocalName    => '', | 
| 133 |  |  |  |  |  |  | Prefix       => $prefix | 
| 134 |  |  |  |  |  |  | }; | 
| 135 | 1 |  |  |  |  | 6 | return \%hash | 
| 136 |  |  |  |  |  |  |  | 
| 137 |  |  |  |  |  |  | } | 
| 138 |  |  |  |  |  |  |  | 
| 139 |  |  |  |  |  |  | =head2 by_name | 
| 140 |  |  |  |  |  |  |  | 
| 141 |  |  |  |  |  |  | Create hash for attributes by name | 
| 142 |  |  |  |  |  |  |  | 
| 143 |  |  |  |  |  |  | =cut | 
| 144 |  |  |  |  |  |  |  | 
| 145 |  |  |  |  |  |  | sub by_name { | 
| 146 | 1 |  |  | 1 | 1 | 2 | my $self = shift; | 
| 147 | 1 |  |  |  |  | 2 | my %hash = (); | 
| 148 | 1 |  |  |  |  | 2 | tie %hash, 'XML::ExtOn::TieAttrsName', $self->_a_stack, | 
| 149 |  |  |  |  |  |  | context => $self->_context; | 
| 150 | 1 |  |  |  |  | 4 | return \%hash; | 
| 151 |  |  |  |  |  |  | } | 
| 152 |  |  |  |  |  |  |  | 
| 153 |  |  |  |  |  |  | 1; |