| 1 |
package XML::LibXML::AttributeHash; |
| 2 |
|
| 3 |
use strict; |
| 4 |
use warnings; |
| 5 |
use Scalar::Util qw//; |
| 6 |
use Tie::Hash; |
| 7 |
our @ISA = qw/Tie::Hash/; |
| 8 |
|
| 9 |
use vars qw($VERSION); |
| 10 |
$VERSION = "2.0134"; # VERSION TEMPLATE: DO NOT CHANGE |
| 11 |
|
| 12 |
BEGIN |
| 13 |
{ |
| 14 |
*__HAS_WEAKEN = defined(&Scalar::Util::weaken) |
| 15 |
? sub () { 1 } |
| 16 |
: sub () { 0 }; |
| 17 |
}; |
| 18 |
|
| 19 |
sub element |
| 20 |
{ |
| 21 |
return $_[0][0]; |
| 22 |
} |
| 23 |
|
| 24 |
sub from_clark |
| 25 |
{ |
| 26 |
my ($self, $str) = @_; |
| 27 |
if ($str =~ m! \{ (.+) \} (.+) !x) |
| 28 |
{ |
| 29 |
return ($1, $2); |
| 30 |
} |
| 31 |
return (undef, $str); |
| 32 |
} |
| 33 |
|
| 34 |
sub to_clark |
| 35 |
{ |
| 36 |
my ($self, $ns, $local) = @_; |
| 37 |
defined $ns ? "{$ns}$local" : $local; |
| 38 |
} |
| 39 |
|
| 40 |
sub all_keys |
| 41 |
{ |
| 42 |
my ($self, @keys) = @_; |
| 43 |
|
| 44 |
my $elem = $self->element; |
| 45 |
|
| 46 |
foreach my $attr (defined($elem) ? $elem->attributes : ()) |
| 47 |
{ |
| 48 |
if (! $attr->isa('XML::LibXML::Namespace')) |
| 49 |
{ |
| 50 |
push @keys, $self->to_clark($attr->namespaceURI, $attr->localname); |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
return sort @keys; |
| 55 |
} |
| 56 |
|
| 57 |
sub TIEHASH |
| 58 |
{ |
| 59 |
my ($class, $element, %args) = @_; |
| 60 |
my $self = bless [$element, undef, \%args], $class; |
| 61 |
if (__HAS_WEAKEN and $args{weaken}) |
| 62 |
{ |
| 63 |
Scalar::Util::weaken( $self->[0] ); |
| 64 |
} |
| 65 |
return $self; |
| 66 |
} |
| 67 |
|
| 68 |
sub STORE |
| 69 |
{ |
| 70 |
my ($self, $key, $value) = @_; |
| 71 |
my ($key_ns, $key_local) = $self->from_clark($key); |
| 72 |
if (defined $key_ns) |
| 73 |
{ |
| 74 |
return $self->element->setAttributeNS($key_ns, "xxx:$key_local", "$value"); |
| 75 |
} |
| 76 |
else |
| 77 |
{ |
| 78 |
return $self->element->setAttribute($key_local, "$value"); |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
sub FETCH |
| 83 |
{ |
| 84 |
my ($self, $key) = @_; |
| 85 |
my ($key_ns, $key_local) = $self->from_clark($key); |
| 86 |
if (defined $key_ns) |
| 87 |
{ |
| 88 |
return $self->element->getAttributeNS($key_ns, "$key_local"); |
| 89 |
} |
| 90 |
else |
| 91 |
{ |
| 92 |
return $self->element->getAttribute($key_local); |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
sub EXISTS |
| 97 |
{ |
| 98 |
my ($self, $key) = @_; |
| 99 |
my ($key_ns, $key_local) = $self->from_clark($key); |
| 100 |
if (defined $key_ns) |
| 101 |
{ |
| 102 |
return $self->element->hasAttributeNS($key_ns, "$key_local"); |
| 103 |
} |
| 104 |
else |
| 105 |
{ |
| 106 |
return $self->element->hasAttribute($key_local); |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
sub DELETE |
| 111 |
{ |
| 112 |
my ($self, $key) = @_; |
| 113 |
my ($key_ns, $key_local) = $self->from_clark($key); |
| 114 |
if (defined $key_ns) |
| 115 |
{ |
| 116 |
return $self->element->removeAttributeNS($key_ns, "$key_local"); |
| 117 |
} |
| 118 |
else |
| 119 |
{ |
| 120 |
return $self->element->removeAttribute($key_local); |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
sub FIRSTKEY |
| 125 |
{ |
| 126 |
my ($self) = @_; |
| 127 |
my @keys = $self->all_keys; |
| 128 |
$self->[1] = \@keys; |
| 129 |
if (wantarray) |
| 130 |
{ |
| 131 |
return ($keys[0], $self->FETCH($keys[0])); |
| 132 |
} |
| 133 |
$keys[0]; |
| 134 |
} |
| 135 |
|
| 136 |
sub NEXTKEY |
| 137 |
{ |
| 138 |
my ($self, $lastkey) = @_; |
| 139 |
my @keys = defined $self->[1] ? @{ $self->[1] } : $self->all_keys; |
| 140 |
my $found; |
| 141 |
foreach my $k (@keys) |
| 142 |
{ |
| 143 |
if ($k gt $lastkey) |
| 144 |
{ |
| 145 |
$found = $k and last; |
| 146 |
} |
| 147 |
} |
| 148 |
if (!defined $found) |
| 149 |
{ |
| 150 |
$self->[1] = undef; |
| 151 |
return; |
| 152 |
} |
| 153 |
if (wantarray) |
| 154 |
{ |
| 155 |
return ($found, $self->FETCH($found)); |
| 156 |
} |
| 157 |
return $found; |
| 158 |
} |
| 159 |
|
| 160 |
sub SCALAR |
| 161 |
{ |
| 162 |
my ($self) = @_; |
| 163 |
return $self->element; |
| 164 |
} |
| 165 |
|
| 166 |
sub CLEAR |
| 167 |
{ |
| 168 |
my ($self) = @_; |
| 169 |
foreach my $k ($self->all_keys) |
| 170 |
{ |
| 171 |
$self->DELETE($k); |
| 172 |
} |
| 173 |
return $self; |
| 174 |
} |
| 175 |
|
| 176 |
__PACKAGE__ |
| 177 |
__END__ |
| 178 |
|
| 179 |
=head1 NAME |
| 180 |
|
| 181 |
XML::LibXML::AttributeHash - tie an XML::LibXML::Element to a hash to access its attributes |
| 182 |
|
| 183 |
=head1 SYNOPSIS |
| 184 |
|
| 185 |
tie my %hash, 'XML::LibXML::AttributeHash', $element; |
| 186 |
$hash{'href'} = 'http://example.com/'; |
| 187 |
print $element->getAttribute('href') . "\n"; |
| 188 |
|
| 189 |
=head1 DESCRIPTION |
| 190 |
|
| 191 |
This class allows an element's attributes to be accessed as if they were a |
| 192 |
plain old Perl hash. Attribute names become hash keys. Namespaced attributes |
| 193 |
are keyed using Clark notation. |
| 194 |
|
| 195 |
my $XLINK = 'http://www.w3.org/1999/xlink'; |
| 196 |
tie my %hash, 'XML::LibXML::AttributeHash', $element; |
| 197 |
$hash{"{$XLINK}href"} = 'http://localhost/'; |
| 198 |
print $element->getAttributeNS($XLINK, 'href') . "\n"; |
| 199 |
|
| 200 |
There is rarely any need to use XML::LibXML::AttributeHash directly. In |
| 201 |
general, it is possible to take advantage of XML::LibXML::Element's |
| 202 |
overloading. The example in the SYNOPSIS could have been written: |
| 203 |
|
| 204 |
$element->{'href'} = 'http://example.com/'; |
| 205 |
print $element->getAttribute('href') . "\n"; |
| 206 |
|
| 207 |
The tie interface allows the passing of additional arguments to |
| 208 |
XML::LibXML::AttributeHash: |
| 209 |
|
| 210 |
tie my %hash, 'XML::LibXML::AttributeHash', $element, %args; |
| 211 |
|
| 212 |
Currently only one argument is supported, the boolean "weaken" which (if |
| 213 |
true) indicates that the tied object's reference to the element should be |
| 214 |
a weak reference. This is used by XML::LibXML::Element's overloading. The |
| 215 |
"weaken" argument is ignored if you don't have a working Scalar::Util::weaken. |