| 1 | package XML::Atom::Syndication::Object; | ||||||||||||||
| 2 | 22 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 3 | |||||||||||||||
| 4 | 22 | ▶ | use base qw( Class::ErrorHandler ); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | |||||||||||||||
| 6 | 22 | ▶ | use constant XMLNS => 'http://www.w3.org/XML/1998/namespace'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | |||||||||||||||
| 8 | 22 | ▶ | use Carp; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 9 | 22 | ▶ | use XML::Elemental 2.01; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 10 | 22 | ▶ | use XML::Elemental::Util qw( process_name ); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 11 | 22 | ▶ | use XML::Atom::Syndication::Util qw( nodelist utf8_off ); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 12 | 22 | ▶ | use XML::Atom::Syndication::Writer; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 13 | |||||||||||||||
| 14 | 339 | ▶ | sub new { | ||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||
| 15 | 339 | my $class = shift; | |||||||||||||
| 16 | 339 | my $atom = bless {}, $class; | |||||||||||||
| 17 | 339 | ▶ | $atom->init(@_) or return $class->error($atom->errstr); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 18 | 339 | $atom; | |||||||||||||
| 19 | } | ||||||||||||||
| 20 | |||||||||||||||
| 21 | 126 | ▶ | sub init { | ||||||||||||
|
Subroutine
init: called CC=6 SCAR=18.6
Pod
undocumented
| |||||||||||||||
| 22 | 126 | my $atom = shift; | |||||||||||||
| 23 | 126 | ▶ | my %param = @_ == 1 ? (Elem => $_[0]) : @_; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 24 | 126 | $atom->set_ns(\%param); | |||||||||||||
| 25 | 126 | ▶ | unless ($atom->{elem} = $param{Elem}) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 26 | 5 | ▶ | unless ($atom->element_name) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 27 | $atom->{name} = $param{Name} | ||||||||||||||
| 28 | 0 | ▶ | or croak('An Elem or Name parameter is required.'); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 29 | } | ||||||||||||||
| 30 | 5 | $atom->{elem} = XML::Elemental::Element->new; | |||||||||||||
| 31 | 5 | $atom->{elem}->name('{' . $atom->ns . '}' . $atom->element_name); | |||||||||||||
| 32 | } else { | ||||||||||||||
| 33 | 121 | ▶ | unless ($atom->element_name) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 34 | 71 | my ($ns, $name) = process_name($atom->{elem}->name); | |||||||||||||
| 35 | 71 | $atom->{name} = $name; | |||||||||||||
| 36 | } | ||||||||||||||
| 37 | } | ||||||||||||||
| 38 | 126 | $atom; | |||||||||||||
| 39 | } | ||||||||||||||
| 40 | |||||||||||||||
| 41 | 524 | ▶ | sub ns { $_[0]->{ns} } | ||||||||||||
|
Subroutine
ns: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 42 | 413 | ▶ | sub elem { $_[0]->{elem} } | ||||||||||||
|
Subroutine
elem: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 43 | 71 | ▶ | sub element_name { $_[0]->{name} } | ||||||||||||
|
Subroutine
element_name: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 44 | |||||||||||||||
| 45 | 1 | ▶ | sub remove { | ||||||||||||
|
Subroutine
remove: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 46 | 1 | my $atom = shift; | |||||||||||||
| 47 | 1 | _remove($atom->elem, @_); | |||||||||||||
| 48 | } | ||||||||||||||
| 49 | |||||||||||||||
| 50 | 6 | ▶ | sub as_xml { | ||||||||||||
|
Subroutine
as_xml: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 51 | 6 | my $w = XML::Atom::Syndication::Writer->new; | |||||||||||||
| 52 | 6 | $w->set_prefix('', $_[0]->ns); | |||||||||||||
| 53 | 6 | $w->as_xml($_[0]->elem, 1); | |||||||||||||
| 54 | } | ||||||||||||||
| 55 | |||||||||||||||
| 56 | #--- Atom common attributes | ||||||||||||||
| 57 | |||||||||||||||
| 58 | 1 | ▶ | sub base { | ||||||||||||
|
Subroutine
base: called CC=2 SCAR=7.6
Pod
documented
| |||||||||||||||
| 59 | 1 | ▶ | @_ > 1 | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 60 | ? $_[0]->set_attribute(XMLNS, 'base', @_[1 .. $#_]) | ||||||||||||||
| 61 | : $_[0]->get_attribute(XMLNS, 'base'); | ||||||||||||||
| 62 | } | ||||||||||||||
| 63 | |||||||||||||||
| 64 | 1 | ▶ | sub lang { | ||||||||||||
|
Subroutine
lang: called CC=2 SCAR=7.6
Pod
documented
| |||||||||||||||
| 65 | 1 | ▶ | @_ > 1 | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 66 | ? $_[0]->set_attribute(XMLNS, 'lang', @_[1 .. $#_]) | ||||||||||||||
| 67 | : $_[0]->get_attribute(XMLNS, 'lang'); | ||||||||||||||
| 68 | } | ||||||||||||||
| 69 | |||||||||||||||
| 70 | #--- accessors | ||||||||||||||
| 71 | |||||||||||||||
| 72 | 367 | ▶ | sub mk_accessors { | ||||||||||||
|
Subroutine
mk_accessors: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||
| 73 | 367 | my $class = shift; | |||||||||||||
| 74 | 367 | my $type = shift; | |||||||||||||
| 75 | 22 | ▶ | no strict 'refs'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 76 | 367 | foreach my $e (@_) { | |||||||||||||
| 77 | 596 | my $accessor = join '::', $class, $e; | |||||||||||||
| 78 | 596 | ▶ | if ($type eq 'element') { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 79 | 37 | ▶ | *$accessor = sub { | ||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||
| 80 | 37 | ▶ | @_ > 1 | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 81 | ? $_[0]->set_element($_[0]->ns, $e, @_[1 .. $#_]) | ||||||||||||||
| 82 | : $_[0]->get_element($_[0]->ns, $e); | ||||||||||||||
| 83 | 242 | }; | |||||||||||||
| 84 | } elsif ($type eq 'attribute') { | ||||||||||||||
| 85 | 85 | ▶ | *$accessor = sub { | ||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||
| 86 | 85 | ▶ | @_ > 1 | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 87 | ? $_[0]->set_attribute($_[0]->ns, $e, @_[1 .. $#_]) | ||||||||||||||
| 88 | : $_[0]->get_attribute($_[0]->ns, $e); | ||||||||||||||
| 89 | 81 | }; | |||||||||||||
| 90 | } else { # type is the class to instaniate | ||||||||||||||
| 91 | 129 | ▶ | *$accessor = sub { | ||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||
| 92 | 129 | ▶ | @_ > 1 | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 93 | ? $_[0]->set_element($_[0]->ns, $e, @_[1 .. $#_]) | ||||||||||||||
| 94 | : $_[0]->get_class($type, $_[0]->ns, $e); | ||||||||||||||
| 95 | 273 | }; | |||||||||||||
| 96 | } | ||||||||||||||
| 97 | } | ||||||||||||||
| 98 | } | ||||||||||||||
| 99 | |||||||||||||||
| 100 | 33 | ▶ | sub get_element { | ||||||||||||
|
Subroutine
get_element: called CC=4 SCAR=15.2
Pod
documented
| |||||||||||||||
| 101 | 33 | my ($atom, $ns, $name) = @_; | |||||||||||||
| 102 | my $ns_uri = | ||||||||||||||
| 103 | 33 | ▶ | ref($ns) eq 'XML::Atom::Syndication::Namespace' ? $ns->{uri} : $ns; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 104 | 33 | my @nodes = nodelist($atom, $ns_uri, $name); | |||||||||||||
| 105 | 33 | ▶ | return unless @nodes; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 106 | wantarray | ||||||||||||||
| 107 | 0 | ▶ | ? map { utf8_off($_->text_content) } @nodes | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 108 | : utf8_off($nodes[0]->text_content); | ||||||||||||||
| 109 | } | ||||||||||||||
| 110 | |||||||||||||||
| 111 | 119 | ▶ | sub get_class { | ||||||||||||
|
Subroutine
get_class: called CC=5 SCAR=16.4
Pod
documented
| |||||||||||||||
| 112 | 119 | my ($atom, $class, $ns, $name) = @_; | |||||||||||||
| 113 | my $ns_uri = | ||||||||||||||
| 114 | 119 | ▶ | ref($ns) eq 'XML::Atom::Syndication::Namespace' ? $ns->{uri} : $ns; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 115 | 119 | my @nodes = nodelist($atom, $ns_uri, $name); | |||||||||||||
| 116 | 119 | ▶ | return unless @nodes; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 117 | 119 | eval "require $class"; | |||||||||||||
| 118 | 119 | ▶ | croak("Error creating accessor {$ns}$name: $@") if $@; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 119 | wantarray | ||||||||||||||
| 120 | 119 | ▶ | ? map { $class->new(Elem => $_, Namespace => $ns_uri) } @nodes | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 121 | : $class->new(Elem => $nodes[0], Namespace => $ns_uri); | ||||||||||||||
| 122 | } | ||||||||||||||
| 123 | |||||||||||||||
| 124 | 78 | ▶ | sub get_attribute { | ||||||||||||
|
Subroutine
get_attribute: called CC=4 SCAR=14.6
Pod
documented
| |||||||||||||||
| 125 | 78 | my $atom = shift; | |||||||||||||
| 126 | 78 | my ($val); | |||||||||||||
| 127 | 78 | ▶ | if (@_ == 1) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 128 | 0 | my ($attr) = @_; | |||||||||||||
| 129 | 0 | $val = $atom->{elem}->attributes->{"{}$attr"}; | |||||||||||||
| 130 | } elsif (@_ == 2) { | ||||||||||||||
| 131 | 78 | my ($ns, $attr) = @_; | |||||||||||||
| 132 | 78 | ▶ | $ns = '' if $atom->ns eq $ns; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 133 | 78 | $val = $atom->{elem}->attributes->{"{$ns}$attr"}; | |||||||||||||
| 134 | } | ||||||||||||||
| 135 | 78 | utf8_off($val); | |||||||||||||
| 136 | } | ||||||||||||||
| 137 | |||||||||||||||
| 138 | 17 | ▶ | sub set_element { | ||||||||||||
|
Subroutine
set_element: called CC=10 SCAR=23.3
Pod
documented
| |||||||||||||||
| 139 | 17 | my $atom = shift; | |||||||||||||
| 140 | 17 | my ($ns, $name, $val, $attr, $add) = @_; | |||||||||||||
| 141 | 17 | ▶ | $add = $attr if ref $val; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 142 | my $ns_uri = | ||||||||||||||
| 143 | 17 | ▶ | ref($ns) eq 'XML::Atom::Syndication::Namespace' ? $ns->{uri} : $ns; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 144 | 17 | ▶ | unless ($add) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 145 | 11 | my @nodes = nodelist($atom, $ns_uri, $name); | |||||||||||||
| 146 | 11 | foreach my $node (@nodes) { | |||||||||||||
| 147 | 0 | ▶ | _remove($node) || return $atom->error($node->errstr); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 148 | } | ||||||||||||||
| 149 | } | ||||||||||||||
| 150 | 17 | ▶ | if (my $class = ref $val) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 151 | 8 | ▶ | $val = $val->elem if $class =~ /^XML::Atom::Syndication::/; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 152 | 8 | $val->parent($atom->elem); | |||||||||||||
| 153 | 8 | push @{$atom->elem->contents}, $val; | |||||||||||||
| 154 | } elsif (defined $val) { | ||||||||||||||
| 155 | 9 | my $elem = XML::Elemental::Element->new; | |||||||||||||
| 156 | 9 | $elem->name("{$ns_uri}$name"); | |||||||||||||
| 157 | 9 | ▶ | $elem->attributes($attr) if $attr; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 158 | 9 | $elem->parent($atom->elem); | |||||||||||||
| 159 | 9 | push @{$atom->elem->contents}, $elem; | |||||||||||||
| 160 | 22 | ▶ | use XML::Elemental::Characters; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 161 | 9 | my $chars = XML::Elemental::Characters->new; | |||||||||||||
| 162 | 9 | $chars->data($val); | |||||||||||||
| 163 | 9 | $chars->parent($elem); | |||||||||||||
| 164 | 9 | push @{$elem->contents}, $chars; | |||||||||||||
| 165 | } | ||||||||||||||
| 166 | 17 | $val; | |||||||||||||
| 167 | } | ||||||||||||||
| 168 | |||||||||||||||
| 169 | 9 | ▶ | sub set_attribute { | ||||||||||||
|
Subroutine
set_attribute: called CC=5 SCAR=18.4
Pod
documented
| |||||||||||||||
| 170 | 9 | my $atom = shift; | |||||||||||||
| 171 | 9 | ▶ | if (@_ == 2) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 172 | 0 | my ($attr, $val) = @_; | |||||||||||||
| 173 | 0 | $atom->{elem}->attributes->{"{}$attr"} = $val; | |||||||||||||
| 174 | } elsif (@_ == 3) { | ||||||||||||||
| 175 | 9 | my ($ns, $attr, $val) = @_; | |||||||||||||
| 176 | my $ns_uri = | ||||||||||||||
| 177 | 9 | ▶ | ref($ns) eq 'XML::Atom::Syndication::Namespace' ? $ns->{uri} : $ns; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 178 | 9 | ▶ | $ns_uri = '' if $atom->ns eq $ns_uri; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 179 | 9 | $atom->{elem}->attributes->{"{$ns_uri}$attr"} = $val; | |||||||||||||
| 180 | } | ||||||||||||||
| 181 | } | ||||||||||||||
| 182 | |||||||||||||||
| 183 | #--- utility | ||||||||||||||
| 184 | |||||||||||||||
| 185 | 1 | ▶ | sub _remove { | ||||||||||||
|
Subroutine
_remove: called CC=2 SCAR=7.0
| |||||||||||||||
| 186 | 1 | my $elem = shift; | |||||||||||||
| 187 | 1 | ▶ | my $parent = $elem->parent | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 188 | or die 'Element parent is not defined'; | ||||||||||||||
| 189 | 1 | my @contents = grep { $elem ne $_ } @{$parent->contents}; | |||||||||||||
| 190 | 1 | $parent->contents(\@contents); | |||||||||||||
| 191 | 1 | $elem->parent(undef); | |||||||||||||
| 192 | 1 | 1; | |||||||||||||
| 193 | } | ||||||||||||||
| 194 | |||||||||||||||
| 195 | 22 | our %NS_MAP = ( | |||||||||||||
| 196 | '0.3' => 'http://purl.org/atom/ns#', | ||||||||||||||
| 197 | '1.0' => 'http://www.w3.org/2005/Atom', | ||||||||||||||
| 198 | ); | ||||||||||||||
| 199 | 22 | our %NS_VERSION = reverse %NS_MAP; | |||||||||||||
| 200 | |||||||||||||||
| 201 | 339 | ▶ | sub set_ns { | ||||||||||||
|
Subroutine
set_ns: called CC=5 SCAR=16.1
Pod
undocumented
| |||||||||||||||
| 202 | 339 | my $atom = shift; | |||||||||||||
| 203 | 339 | my $param = shift; | |||||||||||||
| 204 | 339 | ▶ | if (my $ns = delete $param->{Namespace}) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 205 | 207 | $atom->{ns} = $ns; | |||||||||||||
| 206 | 207 | $atom->{version} = $NS_VERSION{$ns}; | |||||||||||||
| 207 | } else { | ||||||||||||||
| 208 | 132 | ▶ | my $version = delete $param->{Version} || '1.0'; | ||||||||||||
|
Condition100%2/2
delete $param->{'Version'} || '1.0'
MC/DC100%1/1
delete $param->{'Version'} || '1.0'
delete $param->{'Version'}
Truth table100%2/2
A
A= delete $param->{'Version'}
| |||||||||||||||
| 209 | 132 | ▶ | $version = '1.0' if $version == 1; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 210 | 132 | ▶ | my $ns = $NS_MAP{$version} | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 211 | or return $atom->error("Unknown version: $version"); | ||||||||||||||
| 212 | 132 | $atom->{ns} = $ns; | |||||||||||||
| 213 | 132 | $atom->{version} = $version; | |||||||||||||
| 214 | } | ||||||||||||||
| 215 | } | ||||||||||||||
| 216 | |||||||||||||||
| 217 | 22 | 1; | |||||||||||||
| 218 | |||||||||||||||
| 219 | __END__ | ||||||||||||||