File Coverage

blib/lib/XML/RAI/Object.pm
Criterion Covered Total %
statement 4 6 66.6
branch n/a
condition n/a
subroutine 2 2 100.0
pod n/a
total 6 8 75.0


line stmt bran cond sub pod time code
1             # Copyright (c) 2004-2009 Timothy Appnel
2             # http://appnel.com/
3             # This code is released under the Artistic License.
4             #
5             # XML::RAI::Object - A base class for RAI element objects.
6             #
7              
8             package XML::RAI::Object;
9              
10 1     1   5 use strict;
  1         2  
  1         47  
11              
12 1     1   478 use Date::Parse 2.26;
  0            
  0            
13             use Date::Format 2.22;
14             use Scalar::Util qw(weaken);
15              
16             sub new {
17             my $class = shift;
18             my $self = bless {}, $class;
19             $self->init(@_);
20             $self;
21             }
22              
23             sub init {
24             $_[0]->{__source} = $_[1];
25             $_[0]->{__parent} = $_[2];
26             $_[0]->{__RAI} = $_[2];
27             while ($_[0]->{__RAI}->can('parent')) {
28             $_[0]->{__RAI} = $_[0]->{__RAI}->parent;
29             }
30             weaken($_[0]->{__parent});
31             weaken($_[0]->{__RAI});
32             }
33              
34             sub src { $_[0]->{__source} }
35             sub parent { $_[0]->{__parent} }
36              
37             sub add_mapping {
38             my ($class, $var, @maps) = @_;
39             no strict 'refs';
40             ${"${class}::XMap"}->{$var} = []
41             unless (exists ${"${class}::XMap"}->{$var});
42             push @{${"${class}::XMap"}->{$var}}, @maps;
43             }
44              
45             sub generic_handler {
46             my ($this, $class, $var) = @_;
47             no strict 'refs';
48             foreach (@{${$class . '::XMap'}->{$var}}) {
49             my @nodes = $this->src->query($_);
50             if (defined($nodes[0])) {
51             @nodes = map {
52             !ref($_) ? $_
53             : substr($_->name, 0, 30) eq
54             '{http://www.w3.org/1999/xhtml}' ? join '',
55             map { as_xhtml($_) } @{$_->contents}
56             : $_->text_content;
57             } @nodes;
58             return wantarray ? @nodes : $nodes[0];
59             }
60             }
61             return undef;
62             }
63              
64             sub time_handler {
65             my @r = generic_handler(@_);
66             return undef unless $r[0];
67             my $timef = $_[0]->{__RAI}->time_format;
68             if ($timef eq 'EPOCH') {
69             map { $_ = str2time($_, 0) } @r;
70             }
71             elsif ($timef) {
72             map { $_ = time2str($timef, str2time($_, 0), 0) } @r;
73             }
74             wantarray ? @r : $r[0];
75             }
76              
77             use XML::RSS::Parser::Util qw(as_xml);
78              
79             sub as_xhtml { # a hack to get xhtml content as we'd expect.
80             my $xml = as_xml($_[0]);
81             $xml =~ s{<(/?)xhtml:}{<$1}g;
82             $xml;
83             }
84              
85             sub DESTROY { }
86              
87             use vars qw( $AUTOLOAD );
88              
89             sub AUTOLOAD {
90             (my $var = $AUTOLOAD) =~ s!.+::!!;
91             (my $class = $AUTOLOAD) =~ s!::[^:]+$!!;
92             no strict 'refs';
93             die "$var is not a recognized method."
94             unless (${$class . '::XMap'}->{$var});
95             if ($var =~ m/^(created|modified|issued|valid)(_strict)?$/) {
96             *$AUTOLOAD = sub { time_handler($_[0], $class, $var) };
97             }
98             else {
99             *$AUTOLOAD = sub { generic_handler($_[0], $class, $var) };
100             }
101             goto &$AUTOLOAD;
102             }
103              
104             1;
105              
106             __END__