-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathMagicElement.pm
More file actions
127 lines (96 loc) · 2.85 KB
/
MagicElement.pm
File metadata and controls
127 lines (96 loc) · 2.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# $Id$
#
# Copyright (c) 2005 Daisuke Maki <dmaki@cpan.org>
# All rights reserved.
package XML::RSS::LibXML::MagicElement;
use strict;
use warnings;
use overload
bool => sub { 1 },
'""' => \&toString,
fallback => 1
;
use vars qw($VERSION);
$VERSION = '0.3105';
# Make UNIVERSAL::isa happy
sub isa { __PACKAGE__ eq ($_[1] || '') }
sub new
{
my $class = shift;
my %args = @_;
my %attrs;
my @attrs;
my $attrs = $args{attributes};
if (ref($attrs) eq 'ARRAY') {
%attrs = map { (
$_->prefix && $_->prefix ne 'xmlns' ?
sprintf('%s:%s', $_->prefix, $_->localname || '') :
$_->localname || ''
, $_->getData
) } @$attrs;
@attrs = map { $_->getName } @$attrs;
} elsif (ref($attrs) eq 'HASH') {
%attrs = %$attrs;
@attrs = keys %$attrs;
} else {
die "'attributes' must be an arrayref of XML::LibXML::Attr objects, or a hashref of scalars";
}
return bless {
%attrs,
_attributes => \@attrs,
_content => $args{content},
}, $class;
}
sub attributes
{
my $self = shift;
return wantarray ? @{$self->{_attributes}} : $self->{_attributes};
}
sub toString
{
my $self = shift;
return (defined $self->{_content} && length $self->{_content}) ?
$self->{_content} :
join('', map { $self->{$_} || '' } $self->attributes);
}
1;
__END__
=head1 NAME
XML::RSS::LibXML::MagicElement - Represent A Non-Trivial RSS Element
=head1 SYNOPSIS
us XML::RS::LibXML::MagicElement;
my $xml = XML::RSS::LibXML::MagicElement->new(
content => $textContent,
attributes => \@attributes
);
=head1 DESCRIPTION
This module is a handy object that allows users to access non-trivial
RSS elements in XML::RSS style. For example, suppose you have an RSS
feed with an element like the following:
<channel>
<title>Example</title>
<tag attr1="foo" attr2="bar">baz</tag>
...
</channel>
While it is simple to access the title element like this:
$rss->{channel}->{title};
It was slightly non-trivial for the second tag. With this module, E<lt>tagE<gt>
is parsed as a XML::RSS::LibXML::MagicElement object and then you can access
all the elements like so:
$rss->{channel}->{tag}; # "baz"
$rss->{channel}->{tag}->{attr1}; # "foo"
$rss->{channel}->{tag}->{attr2}; # "bar"
=head1 METHODS
=head2 new
Create a new MagicElement object.
=head2 attributes
Returns the list of attributes associated with this element
=head2 toString
Returns the string representation of this object.
By default we use the "text content" of the found tag, but for XML::RSS
compatibility, we use the concatenation of the attributes if no content is
found.
=head1 AUTHOR
Copyright 2005 Daisuke Maki E<lt>dmaki@cpan.orgE<gt>. All rights reserved.
Development partially funded by Brazil, Ltd. E<lt>http://b.razil.jpE<gt>
=cut