| 1 | package XML::DOMBacked; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 2 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 2 | ▶ | use XML::LibXML; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 2 | ▶ | use LWP::UserAgent; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | 2 | ▶ | no warnings 'redefine'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | 2 | our $VERSION = '1.00'; | |||||||||||||||||||
| 11 | 2 | our $NSMAP = {}; | |||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | use overload | ||||||||||||||||||||
| 14 | 2 | ▶ | 'eq' => 'check_equality', | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 15 | '==' => 'check_equality', | ||||||||||||||||||||
| 16 | 2 | fallback => 1; | |||||||||||||||||||
| 17 | |||||||||||||||||||||
| 18 | 3 | ▶ | sub check_equality { | ||||||||||||||||||
|
Subroutine
check_equality: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 19 | 3 | my $lhs = shift; | |||||||||||||||||||
| 20 | 3 | my $rhs = shift; | |||||||||||||||||||
| 21 | 3 | $lhs->dom->isSameNode( $rhs->dom ); | |||||||||||||||||||
| 22 | } | ||||||||||||||||||||
| 23 | |||||||||||||||||||||
| 24 | 7 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=13.6
Pod
documented
| |||||||||||||||||||||
| 25 | 7 | my $class = shift; | |||||||||||||||||||
| 26 | 7 | my $self = {}; | |||||||||||||||||||
| 27 | 7 | bless $self => $class; | |||||||||||||||||||
| 28 | 7 | my $init = eval { $self->init( @_ ) }; | |||||||||||||||||||
| 29 | 7 | ▶ | if (!$init) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 30 | 0 | my $mesg = "could not initialise object"; | |||||||||||||||||||
| 31 | 0 | ▶ | if ( $@ ) { $mesg .= ': ' . $@ } | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 32 | 0 | die $mesg; | |||||||||||||||||||
| 33 | } | ||||||||||||||||||||
| 34 | 7 | return $self; | |||||||||||||||||||
| 35 | } | ||||||||||||||||||||
| 36 | |||||||||||||||||||||
| 37 | 1 | ▶ | sub from_uri { | ||||||||||||||||||
|
Subroutine
from_uri: called CC=3 SCAR=11.7
Pod
documented
| |||||||||||||||||||||
| 38 | 1 | my $class = shift; | |||||||||||||||||||
| 39 | 1 | my $uri = shift; | |||||||||||||||||||
| 40 | 1 | ▶ | if (!$uri) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 41 | 0 | die "need to have URI to load"; | |||||||||||||||||||
| 42 | } | ||||||||||||||||||||
| 43 | 1 | my $ua = LWP::UserAgent->new; | |||||||||||||||||||
| 44 | 1 | my $r = $ua->get( $uri ); | |||||||||||||||||||
| 45 | 1 | ▶ | if ( !$r->is_success ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 46 | 0 | die "load failed: " . $r->status_line; | |||||||||||||||||||
| 47 | } | ||||||||||||||||||||
| 48 | 1 | my $doc = XML::LibXML->new->parse_string( $r->content ); | |||||||||||||||||||
| 49 | 1 | $class->new->dom( $doc->documentElement ); | |||||||||||||||||||
| 50 | } | ||||||||||||||||||||
| 51 | |||||||||||||||||||||
| 52 | |||||||||||||||||||||
| 53 | 7 | ▶ | sub init { | ||||||||||||||||||
|
Subroutine
init: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 54 | 7 | my $self = shift; | |||||||||||||||||||
| 55 | 7 | $self->dom( XML::LibXML::Element->new( $self->nodename ) ); | |||||||||||||||||||
| 56 | 7 | XML::LibXML::Document->new('1.0','UTF-8')->addChild( $self->dom ); | |||||||||||||||||||
| 57 | 7 | return "init_call_passed"; | |||||||||||||||||||
| 58 | } | ||||||||||||||||||||
| 59 | |||||||||||||||||||||
| 60 | 4 | ▶ | sub nodename { | ||||||||||||||||||
|
Subroutine
nodename: called CC=3 SCAR=11.8
Pod
undocumented
| |||||||||||||||||||||
| 61 | 4 | my $self = shift; | |||||||||||||||||||
| 62 | 4 | ▶ | my $class = ref( $self ) || $self; | ||||||||||||||||||
|
Condition66%2/3
ref $self || $self
MC/DC0%0/2
ref $self || $self
ref $self
$self
Truth table66%2/3
A || B
A= ref $selfB= $self
| |||||||||||||||||||||
| 63 | 4 | ▶ | if ( $class =~ /:/ ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 64 | ## we just want to xmlify the last bit | ||||||||||||||||||||
| 65 | 0 | return lc( substr($class, rindex( $class, ':') + 1 ) ); | |||||||||||||||||||
| 66 | } | ||||||||||||||||||||
| 67 | 4 | return lc( $class ); | |||||||||||||||||||
| 68 | } | ||||||||||||||||||||
| 69 | |||||||||||||||||||||
| 70 | 110 | ▶ | sub dom { | ||||||||||||||||||
|
Subroutine
dom: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 71 | 110 | my $self = shift; | |||||||||||||||||||
| 72 | 110 | ▶ | if ( @_ ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 73 | 8 | $self->{ dom } = shift; | |||||||||||||||||||
| 74 | 8 | return $self; | |||||||||||||||||||
| 75 | } | ||||||||||||||||||||
| 76 | 102 | return $self->{ dom }; | |||||||||||||||||||
| 77 | } | ||||||||||||||||||||
| 78 | |||||||||||||||||||||
| 79 | 1 | ▶ | sub has_many { | ||||||||||||||||||
|
Subroutine
has_many: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||
| 80 | 1 | my $class = shift; | |||||||||||||||||||
| 81 | 1 | my $pairs = { @_ }; | |||||||||||||||||||
| 82 | 1 | foreach my $key ( keys %$pairs ) { | |||||||||||||||||||
| 83 | 2 | ▶ | if ( ref( $pairs->{ $key } ) ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 84 | ## this is a complicated multivalue thing | ||||||||||||||||||||
| 85 | 1 | $class->setup_has_many_complex( $key, $pairs->{ $key }->{class} ); | |||||||||||||||||||
| 86 | } else { | ||||||||||||||||||||
| 87 | ## this is a simple multivalue thing. | ||||||||||||||||||||
| 88 | ## What am I talking about they're _all_ complicated multivalue things! | ||||||||||||||||||||
| 89 | 1 | $class->setup_has_many_simple( $key, $pairs->{ $key } ); | |||||||||||||||||||
| 90 | } | ||||||||||||||||||||
| 91 | } | ||||||||||||||||||||
| 92 | } | ||||||||||||||||||||
| 93 | |||||||||||||||||||||
| 94 | 1 | ▶ | sub setup_has_many_complex { | ||||||||||||||||||
|
Subroutine
setup_has_many_complex: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 95 | 1 | my $class = shift; | |||||||||||||||||||
| 96 | 1 | my $key = shift; | |||||||||||||||||||
| 97 | 1 | my $val = shift; | |||||||||||||||||||
| 98 | 1 | my $name = $val->nodename; | |||||||||||||||||||
| 99 | 1 | ▶ | if ( $name =~ /:/ ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 100 | 1 | my ($ns, $attr) = split(/:/, $name); | |||||||||||||||||||
| 101 | 1 | $name = $attr; | |||||||||||||||||||
| 102 | } | ||||||||||||||||||||
| 103 | # print "Creating thing: $name\n"; | ||||||||||||||||||||
| 104 | 2 | ▶ | no strict 'refs'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 105 | 1 | ▶ | *{$class.'::add_'.$name} = sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 106 | 2 | my $self = shift; | |||||||||||||||||||
| 107 | 2 | my $obj = shift; | |||||||||||||||||||
| 108 | 2 | $self->dom->addChild( $obj->dom ); | |||||||||||||||||||
| 109 | 1 | }; | |||||||||||||||||||
| 110 | 1 | ▶ | *{$class.'::'.$key} = sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 111 | 2 | my $self = shift; | |||||||||||||||||||
| 112 | 2 | map { bless( { dom => $_ }, $val ) } $self->dom->getChildrenByTagName( $val->nodename ) | |||||||||||||||||||
| 113 | 1 | }; | |||||||||||||||||||
| 114 | 1 | ▶ | *{$class.'::remove_'.$name} = sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 115 | 2 | my $self = shift; | |||||||||||||||||||
| 116 | 2 | my $obj = shift; | |||||||||||||||||||
| 117 | 2 | $self->dom->removeChild( $obj->dom ); | |||||||||||||||||||
| 118 | } | ||||||||||||||||||||
| 119 | 1 | } | |||||||||||||||||||
| 120 | |||||||||||||||||||||
| 121 | 1 | ▶ | sub setup_has_many_simple { | ||||||||||||||||||
|
Subroutine
setup_has_many_simple: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||
| 122 | 1 | my $class = shift; | |||||||||||||||||||
| 123 | 1 | my $key = shift; | |||||||||||||||||||
| 124 | 1 | my $val = shift; | |||||||||||||||||||
| 125 | 1 | my $name = $val; | |||||||||||||||||||
| 126 | 1 | ▶ | if ( $val =~ /:/ ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 127 | 1 | my ($ns, $attr) = split(/:/, $val); | |||||||||||||||||||
| 128 | 1 | ▶ | if (!$class->lookup_namespace( $ns )) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 129 | 0 | die "can't create a property with unknown namespace ($ns)"; | |||||||||||||||||||
| 130 | } | ||||||||||||||||||||
| 131 | 1 | $name = $attr; | |||||||||||||||||||
| 132 | } | ||||||||||||||||||||
| 133 | 2 | ▶ | no strict 'refs'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 134 | 1 | ▶ | *{$class .'::add_'. $name} = sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 135 | 2 | my $self = shift; | |||||||||||||||||||
| 136 | 2 | my $data = shift; | |||||||||||||||||||
| 137 | 2 | my $elem = XML::LibXML::Element->new( $val ); | |||||||||||||||||||
| 138 | 2 | $elem->appendText( $data ); | |||||||||||||||||||
| 139 | 2 | $self->dom->addChild( $elem ); | |||||||||||||||||||
| 140 | 1 | }; | |||||||||||||||||||
| 141 | 1 | ▶ | *{$class .'::'.$key} = sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 142 | 2 | my $self = shift; | |||||||||||||||||||
| 143 | 2 | map { $_->findvalue('.') } $self->dom->getChildrenByTagName($val); | |||||||||||||||||||
| 144 | 1 | }; | |||||||||||||||||||
| 145 | 1 | ▶ | *{$class .'::remove_'.$name} = sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||
| 146 | 1 | my $self = shift; | |||||||||||||||||||
| 147 | 1 | my $data = shift; | |||||||||||||||||||
| 148 | 1 | my @list = grep { $_->findvalue('.') eq $data } $self->dom->getChildrenByTagName( $val ); | |||||||||||||||||||
| 149 | 1 | $self->dom->removeChild( $_ ) for @list; | |||||||||||||||||||
| 150 | 1 | 1; | |||||||||||||||||||
| 151 | 1 | }; | |||||||||||||||||||
| 152 | } | ||||||||||||||||||||
| 153 | |||||||||||||||||||||
| 154 | 1 | ▶ | sub has_a { | ||||||||||||||||||
|
Subroutine
has_a: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||
| 155 | 1 | my $class = shift; | |||||||||||||||||||
| 156 | 1 | foreach my $key ( @_ ) { | |||||||||||||||||||
| 157 | 2 | ▶ | if ( $key->nodename =~ /:/ ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 158 | ## we're in magic namespace land | ||||||||||||||||||||
| 159 | 1 | my ($ns, $rkey) = split(/:/, $key->nodename); | |||||||||||||||||||
| 160 | 1 | my $uri = $class->lookup_namespace( $ns ); | |||||||||||||||||||
| 161 | 1 | ▶ | if (!$uri) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 162 | 0 | die "can't create a property in an unknown namespace( $ns )"; | |||||||||||||||||||
| 163 | } | ||||||||||||||||||||
| 164 | 2 | ▶ | no strict 'refs'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 165 | 1 | ▶ | *{ $class . '::' . $rkey } = sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||
| 166 | 2 | my $self = shift; | |||||||||||||||||||
| 167 | 2 | ▶ | if ( @_ ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 168 | 1 | $self->set_dom_object( $key, @_ ); | |||||||||||||||||||
| 169 | 1 | return $self; | |||||||||||||||||||
| 170 | } | ||||||||||||||||||||
| 171 | 1 | return $self->get_dom_object( $key ); | |||||||||||||||||||
| 172 | 1 | }; | |||||||||||||||||||
| 173 | } else { | ||||||||||||||||||||
| 174 | 2 | ▶ | no strict 'refs'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 175 | # print "Creating in class $key\n"; | ||||||||||||||||||||
| 176 | 1 | ▶ | *{ $class . '::' . $key } = sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||
| 177 | 2 | my $self = shift; | |||||||||||||||||||
| 178 | 2 | ▶ | if ( @_ ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 179 | 1 | $self->set_dom_object( $key, @_ ); | |||||||||||||||||||
| 180 | 1 | return $self; | |||||||||||||||||||
| 181 | } | ||||||||||||||||||||
| 182 | 1 | return $self->get_dom_object( $key ); | |||||||||||||||||||
| 183 | 1 | }; | |||||||||||||||||||
| 184 | } | ||||||||||||||||||||
| 185 | } | ||||||||||||||||||||
| 186 | } | ||||||||||||||||||||
| 187 | |||||||||||||||||||||
| 188 | 2 | ▶ | sub get_dom_object { | ||||||||||||||||||
|
Subroutine
get_dom_object: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 189 | 2 | my $self = shift; | |||||||||||||||||||
| 190 | 2 | my $prop = shift; | |||||||||||||||||||
| 191 | 2 | my $elem = $self->get_property_object( $prop->nodename ); | |||||||||||||||||||
| 192 | 2 | my $thing = bless( { dom => $elem }, $prop ); | |||||||||||||||||||
| 193 | 2 | return $thing; | |||||||||||||||||||
| 194 | } | ||||||||||||||||||||
| 195 | |||||||||||||||||||||
| 196 | 2 | ▶ | sub set_dom_object { | ||||||||||||||||||
|
Subroutine
set_dom_object: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 197 | 2 | my $self = shift; | |||||||||||||||||||
| 198 | 2 | my $prop = shift; | |||||||||||||||||||
| 199 | 2 | my $val = shift; | |||||||||||||||||||
| 200 | |||||||||||||||||||||
| 201 | # print "Setting dom object\n"; | ||||||||||||||||||||
| 202 | # print $val->as_string; | ||||||||||||||||||||
| 203 | |||||||||||||||||||||
| 204 | 2 | foreach my $ns ( $val->dom->getNamespaces ) { | |||||||||||||||||||
| 205 | 2 | my $prefix = $ns->name; | |||||||||||||||||||
| 206 | 2 | my $uri = $ns->getNamespaceURI; | |||||||||||||||||||
| 207 | 2 | $self->dom->setNamespace( $uri, $prefix, 0 ); | |||||||||||||||||||
| 208 | } | ||||||||||||||||||||
| 209 | 2 | $self->dom->addChild( $val->dom ); | |||||||||||||||||||
| 210 | } | ||||||||||||||||||||
| 211 | |||||||||||||||||||||
| 212 | 2 | ▶ | sub has_attributes { | ||||||||||||||||||
|
Subroutine
has_attributes: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||
| 213 | 2 | my $class = shift; | |||||||||||||||||||
| 214 | 2 | foreach my $attribute ( @_ ) { | |||||||||||||||||||
| 215 | 3 | ▶ | if ( $attribute =~ /:/ ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 216 | ## has a namespace attached | ||||||||||||||||||||
| 217 | 2 | my ($ns,$rattr) = split(/:/, $attribute); | |||||||||||||||||||
| 218 | 2 | my $uri = $class->lookup_namespace( $ns ); | |||||||||||||||||||
| 219 | # if (! $uri ) { | ||||||||||||||||||||
| 220 | # die "can't create an attribute for an unknown namespace ($ns)"; | ||||||||||||||||||||
| 221 | # } | ||||||||||||||||||||
| 222 | 2 | ▶ | no strict 'refs'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 223 | 2 | ▶ | *{ $class . '::' . $rattr } = sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||
| 224 | 5 | my $self = shift; | |||||||||||||||||||
| 225 | 5 | ▶ | if ( @_ ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 226 | 2 | $self->set_dom_attribute( $attribute, @_ ); | |||||||||||||||||||
| 227 | 2 | return $self; | |||||||||||||||||||
| 228 | } | ||||||||||||||||||||
| 229 | 3 | return $self->get_dom_attribute( $attribute ); | |||||||||||||||||||
| 230 | 2 | }; | |||||||||||||||||||
| 231 | } else { | ||||||||||||||||||||
| 232 | ## straightforward attribute. | ||||||||||||||||||||
| 233 | 2 | ▶ | no strict 'refs'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 234 | 1 | ▶ | *{ $class . '::' . $attribute } = sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||
| 235 | 2 | my $self = shift; | |||||||||||||||||||
| 236 | 2 | ▶ | if ( @_ ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 237 | 1 | $self->set_dom_attribute( $attribute, @_ ); | |||||||||||||||||||
| 238 | 1 | return $self; | |||||||||||||||||||
| 239 | } | ||||||||||||||||||||
| 240 | 1 | return $self->get_dom_attribute( $attribute ); | |||||||||||||||||||
| 241 | } | ||||||||||||||||||||
| 242 | 1 | } | |||||||||||||||||||
| 243 | } | ||||||||||||||||||||
| 244 | } | ||||||||||||||||||||
| 245 | |||||||||||||||||||||
| 246 | 3 | ▶ | sub has_properties { | ||||||||||||||||||
|
Subroutine
has_properties: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||
| 247 | 3 | my $class = shift; | |||||||||||||||||||
| 248 | 3 | foreach my $property ( @_ ) { | |||||||||||||||||||
| 249 | 6 | ▶ | if ( $property =~ /:/ ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 250 | ## this has a namespace attached | ||||||||||||||||||||
| 251 | 5 | my ($ns, $realprop) = split(/:/, $property); | |||||||||||||||||||
| 252 | 5 | my $uri = $class->lookup_namespace( $ns ); | |||||||||||||||||||
| 253 | 5 | ▶ | if ( ! $uri ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 254 | 0 | die "can't create a property for an unknown namespace"; | |||||||||||||||||||
| 255 | } | ||||||||||||||||||||
| 256 | 2 | ▶ | no strict 'refs'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 257 | 5 | ▶ | *{ $class . '::' . $realprop } = sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||
| 258 | 13 | my $self = shift; | |||||||||||||||||||
| 259 | 13 | ▶ | if (@_) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 260 | 6 | $self->set_dom_property( $property, @_ ); | |||||||||||||||||||
| 261 | 6 | return $self; | |||||||||||||||||||
| 262 | } | ||||||||||||||||||||
| 263 | 7 | return $self->get_dom_property( $property ); | |||||||||||||||||||
| 264 | 5 | }; | |||||||||||||||||||
| 265 | } else { | ||||||||||||||||||||
| 266 | ## this is in the default namespace | ||||||||||||||||||||
| 267 | 2 | ▶ | no strict 'refs'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 268 | 1 | ▶ | *{ $class . '::' . $property } = sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||
| 269 | 4 | my $self = shift; | |||||||||||||||||||
| 270 | 4 | ▶ | if (@_) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 271 | 3 | $self->set_dom_property( $property, @_ ); | |||||||||||||||||||
| 272 | 3 | return $self; | |||||||||||||||||||
| 273 | } | ||||||||||||||||||||
| 274 | 1 | return $self->get_dom_property( $property ); | |||||||||||||||||||
| 275 | 1 | }; | |||||||||||||||||||
| 276 | } | ||||||||||||||||||||
| 277 | } | ||||||||||||||||||||
| 278 | } | ||||||||||||||||||||
| 279 | |||||||||||||||||||||
| 280 | 3 | ▶ | sub set_dom_attribute { | ||||||||||||||||||
|
Subroutine
set_dom_attribute: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||
| 281 | 3 | my $self = shift; | |||||||||||||||||||
| 282 | 3 | my $prop = shift; | |||||||||||||||||||
| 283 | 3 | my $val = shift; | |||||||||||||||||||
| 284 | 3 | ▶ | if ( $prop =~ /:/ ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 285 | 2 | my ($ns, $realprop) = split(/:/, $prop); | |||||||||||||||||||
| 286 | 2 | ▶ | my $uri = $self->lookup_namespace( $ns ) || ''; | ||||||||||||||||||
|
Condition50%1/2
$self->lookup_namespace($ns) || ''
MC/DC0%0/1
$self->lookup_namespace($ns) || ''
$self->lookup_namespace($ns)
Truth table50%1/2
A
A= $self->lookup_namespace($ns)
| |||||||||||||||||||||
| 287 | 2 | ▶ | if ( !$self->dom->lookupNamespaceURI( $ns ) ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 288 | 1 | $self->dom->setNamespace( $uri, $ns, 0 ); | |||||||||||||||||||
| 289 | } else { | ||||||||||||||||||||
| 290 | 1 | $uri = $self->dom->lookupNamespaceURI( $ns ); | |||||||||||||||||||
| 291 | } | ||||||||||||||||||||
| 292 | 2 | $self->dom->setAttributeNS( $uri, $realprop, $val ); | |||||||||||||||||||
| 293 | } else { | ||||||||||||||||||||
| 294 | 1 | $self->dom->setAttribute( $prop, $val ); | |||||||||||||||||||
| 295 | } | ||||||||||||||||||||
| 296 | } | ||||||||||||||||||||
| 297 | |||||||||||||||||||||
| 298 | 4 | ▶ | sub get_dom_attribute { | ||||||||||||||||||
|
Subroutine
get_dom_attribute: called CC=3 SCAR=11.1
Pod
undocumented
| |||||||||||||||||||||
| 299 | 4 | my $self = shift; | |||||||||||||||||||
| 300 | 4 | my $prop = shift; | |||||||||||||||||||
| 301 | 4 | ▶ | if ( $prop =~ /:/ ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 302 | 3 | my ($ns, $propname) = split(/:/, $prop); | |||||||||||||||||||
| 303 | 3 | my $uri = $self->lookup_namespace( $ns ); | |||||||||||||||||||
| 304 | 3 | ▶ | if ( $self->dom->hasAttributeNS( $self->lookup_namespace( $ns ), $propname ) ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 305 | 3 | my $val = $self->dom->getAttributeNS( $uri, $propname ); | |||||||||||||||||||
| 306 | 3 | return $val | |||||||||||||||||||
| 307 | } else { | ||||||||||||||||||||
| 308 | 0 | die "no such property $prop"; | |||||||||||||||||||
| 309 | } | ||||||||||||||||||||
| 310 | } else { | ||||||||||||||||||||
| 311 | 1 | return $self->dom->getAttribute( $prop ); | |||||||||||||||||||
| 312 | } | ||||||||||||||||||||
| 313 | } | ||||||||||||||||||||
| 314 | |||||||||||||||||||||
| 315 | 8 | ▶ | sub get_dom_property { | ||||||||||||||||||
|
Subroutine
get_dom_property: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 316 | 8 | my $self = shift; | |||||||||||||||||||
| 317 | 8 | my $prop = shift; | |||||||||||||||||||
| 318 | 8 | my $elem = $self->get_property_object( $prop ); | |||||||||||||||||||
| 319 | 8 | return $elem->findvalue( '.' ); | |||||||||||||||||||
| 320 | } | ||||||||||||||||||||
| 321 | |||||||||||||||||||||
| 322 | 9 | ▶ | sub set_dom_property { | ||||||||||||||||||
|
Subroutine
set_dom_property: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||||||||||||
| 323 | 9 | my $self = shift; | |||||||||||||||||||
| 324 | 9 | my $prop = shift; | |||||||||||||||||||
| 325 | 9 | my $data = shift; | |||||||||||||||||||
| 326 | 9 | my $elem = $self->get_property_object( $prop ); | |||||||||||||||||||
| 327 | 9 | my $text = XML::LibXML::Text->new( $data ); | |||||||||||||||||||
| 328 | 9 | ▶ | if ( $elem->hasChildNodes() ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 329 | 0 | $elem->removeChild( $elem->firstChild ); | |||||||||||||||||||
| 330 | } | ||||||||||||||||||||
| 331 | 9 | $elem->addChild( $text ); | |||||||||||||||||||
| 332 | } | ||||||||||||||||||||
| 333 | |||||||||||||||||||||
| 334 | 19 | ▶ | sub get_property_object { | ||||||||||||||||||
|
Subroutine
get_property_object: called CC=5 SCAR=16.1
Pod
undocumented
| |||||||||||||||||||||
| 335 | 19 | my $self = shift; | |||||||||||||||||||
| 336 | 19 | my $prop = shift; | |||||||||||||||||||
| 337 | |||||||||||||||||||||
| 338 | 19 | ▶ | if ( $prop =~ /:/ ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 339 | 14 | my ($ns, $rprop) = split(/:/, $prop); | |||||||||||||||||||
| 340 | 14 | my $uri = $self->lookup_namespace( $ns ); | |||||||||||||||||||
| 341 | 14 | ▶ | if ( !$self->dom->lookupNamespacePrefix( $uri ) ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 342 | 5 | $self->dom->setNamespace( $uri, $ns, 1 ); | |||||||||||||||||||
| 343 | } | ||||||||||||||||||||
| 344 | 14 | my $node = ($self->dom->getChildrenByTagNameNS( $uri, $rprop ))[0]; | |||||||||||||||||||
| 345 | 14 | ▶ | if (!$node) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 346 | 6 | $node = $self->dom->addNewChild( $uri, $rprop ); | |||||||||||||||||||
| 347 | } | ||||||||||||||||||||
| 348 | 14 | return $node; | |||||||||||||||||||
| 349 | |||||||||||||||||||||
| 350 | } else { | ||||||||||||||||||||
| 351 | 5 | my $node = ($self->dom->getChildrenByTagName($prop))[0]; | |||||||||||||||||||
| 352 | 5 | ▶ | if (! $node ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 353 | 3 | $node = XML::LibXML::Element->new( $prop ); | |||||||||||||||||||
| 354 | 3 | $self->dom->addChild( $node ); | |||||||||||||||||||
| 355 | } | ||||||||||||||||||||
| 356 | 5 | return $node; | |||||||||||||||||||
| 357 | |||||||||||||||||||||
| 358 | } | ||||||||||||||||||||
| 359 | } | ||||||||||||||||||||
| 360 | |||||||||||||||||||||
| 361 | 36 | ▶ | sub lookup_namespace { | ||||||||||||||||||
|
Subroutine
lookup_namespace: called CC=7 SCAR=19.8
Pod
undocumented
| |||||||||||||||||||||
| 362 | 36 | my $self = shift; | |||||||||||||||||||
| 363 | 36 | my $ns = shift; | |||||||||||||||||||
| 364 | 36 | ▶ | if (!$ns) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 365 | 0 | die "need an namespace parameter"; | |||||||||||||||||||
| 366 | } | ||||||||||||||||||||
| 367 | 36 | ▶ | my $class = ref( $self ) || $self; | ||||||||||||||||||
|
Condition66%2/3
ref $self || $self
MC/DC0%0/2
ref $self || $self
ref $self
$self
Truth table66%2/3
A || B
A= ref $selfB= $self
| |||||||||||||||||||||
| 368 | 36 | ▶ | if ( exists $NSMAP->{ $class }->{namespaces}->{ $ns } ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 369 | ## it belongs to this class, which makes life eeee-zeee! | ||||||||||||||||||||
| 370 | 35 | return $NSMAP->{$class}->{namespaces}->{ $ns }; | |||||||||||||||||||
| 371 | } else { | ||||||||||||||||||||
| 372 | ## we're in the land of recursion, la la la la lah! | ||||||||||||||||||||
| 373 | ## first we get the IS-A var. | ||||||||||||||||||||
| 374 | 2 | ▶ | no strict 'refs'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 375 | 1 | my @isa = @{ $class . '::ISA' }; | |||||||||||||||||||
| 376 | 2 | ▶ | use strict 'refs'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 377 | 1 | my $uri; | |||||||||||||||||||
| 378 | |||||||||||||||||||||
| 379 | ## loop through it until we get an answer | ||||||||||||||||||||
| 380 | 1 | foreach my $isa ( @isa ) { | |||||||||||||||||||
| 381 | 1 | my $result = eval { $isa->lookup_namespace( $ns ) }; | |||||||||||||||||||
| 382 | 1 | ▶ | if ( $result ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 383 | 1 | $uri = $result; | |||||||||||||||||||
| 384 | 1 | last; | |||||||||||||||||||
| 385 | } | ||||||||||||||||||||
| 386 | } | ||||||||||||||||||||
| 387 | |||||||||||||||||||||
| 388 | 1 | ▶ | if ( !exists $NSMAP->{ $class }->{ namespaces }->{ $ns } ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 389 | 1 | $NSMAP->{ $class }->{ namespaces }->{ $ns } = $uri; | |||||||||||||||||||
| 390 | } | ||||||||||||||||||||
| 391 | |||||||||||||||||||||
| 392 | 1 | return $uri; | |||||||||||||||||||
| 393 | } | ||||||||||||||||||||
| 394 | 0 | return 0; | |||||||||||||||||||
| 395 | } | ||||||||||||||||||||
| 396 | |||||||||||||||||||||
| 397 | 6 | ▶ | sub uses_namespace { | ||||||||||||||||||
|
Subroutine
uses_namespace: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 398 | 6 | my $class = shift; | |||||||||||||||||||
| 399 | 6 | my $pairs = { @_ }; | |||||||||||||||||||
| 400 | 6 | foreach my $key (keys %$pairs) { | |||||||||||||||||||
| 401 | 6 | my $ns = $key; | |||||||||||||||||||
| 402 | 6 | my $uri = $pairs->{$key}; | |||||||||||||||||||
| 403 | 6 | $NSMAP->{$class}->{ namespaces }->{ $ns } = $uri; | |||||||||||||||||||
| 404 | } | ||||||||||||||||||||
| 405 | } | ||||||||||||||||||||
| 406 | |||||||||||||||||||||
| 407 | 2 | ▶ | sub as_string { | ||||||||||||||||||
|
Subroutine
as_string: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 408 | 2 | my $self = shift; | |||||||||||||||||||
| 409 | 2 | return $self->dom->toString( 1 ); | |||||||||||||||||||
| 410 | } | ||||||||||||||||||||
| 411 | |||||||||||||||||||||
| 412 | 2 | ▶ | sub as_xml { | ||||||||||||||||||
|
Subroutine
as_xml: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 413 | 2 | my $self = shift; | |||||||||||||||||||
| 414 | 2 | my $doc = XML::LibXML::Document->new('1.0', 'UTF-8'); | |||||||||||||||||||
| 415 | 2 | $doc->setDocumentElement( $self->dom ); | |||||||||||||||||||
| 416 | 2 | return $doc->toString( 1 ); | |||||||||||||||||||
| 417 | } | ||||||||||||||||||||
| 418 | |||||||||||||||||||||
| 419 | 2 | 1; | |||||||||||||||||||
| 420 | |||||||||||||||||||||
| 421 | =head1 NAME | ||||||||||||||||||||
| 422 | |||||||||||||||||||||
| 423 | XML::DOMBacked - objects backed by a DOM | ||||||||||||||||||||
| 424 | |||||||||||||||||||||
| 425 | =head1 SYNOPSIS | ||||||||||||||||||||
| 426 | |||||||||||||||||||||
| 427 | package Person; | ||||||||||||||||||||
| 428 | |||||||||||||||||||||
| 429 | use base 'XML::DOMBacked'; | ||||||||||||||||||||
| 430 | |||||||||||||||||||||
| 431 | Person->uses_namespace( | ||||||||||||||||||||
| 432 | 'foaf' => 'http://xmlns.com/foaf/0.1/', | ||||||||||||||||||||
| 433 | 'rdf' => 'http://www.w3.org/1999/02/22-rdf-syntax-ns#', | ||||||||||||||||||||
| 434 | ); | ||||||||||||||||||||
| 435 | Person->has_properties( 'foaf:name','foaf:title','foaf:nick' ); | ||||||||||||||||||||
| 436 | Person->has_attributes( 'rdf:nodeID' ); | ||||||||||||||||||||
| 437 | Person->has_a( 'Person::Knows' ); | ||||||||||||||||||||
| 438 | |||||||||||||||||||||
| 439 | sub nodename { "foaf:Person" } | ||||||||||||||||||||
| 440 | |||||||||||||||||||||
| 441 | package Person::Knows; | ||||||||||||||||||||
| 442 | |||||||||||||||||||||
| 443 | use base 'XML::DOMBacked'; | ||||||||||||||||||||
| 444 | |||||||||||||||||||||
| 445 | Person::Knows->has_many( people => { class => 'Person' } ); | ||||||||||||||||||||
| 446 | |||||||||||||||||||||
| 447 | package main; | ||||||||||||||||||||
| 448 | |||||||||||||||||||||
| 449 | my $p = Person->new; | ||||||||||||||||||||
| 450 | $p->nodeID("me"); | ||||||||||||||||||||
| 451 | $p->name('A. N. Other'); | ||||||||||||||||||||
| 452 | $p->title('Mr'); | ||||||||||||||||||||
| 453 | $p->nick('another'); | ||||||||||||||||||||
| 454 | |||||||||||||||||||||
| 455 | my $a = Person->new; | ||||||||||||||||||||
| 456 | $a->name('Yet Another'); | ||||||||||||||||||||
| 457 | |||||||||||||||||||||
| 458 | $p->Knows->add_Person( $a ); | ||||||||||||||||||||
| 459 | print $p->as_xml; | ||||||||||||||||||||
| 460 | |||||||||||||||||||||
| 461 | $p = Person->from_uri( 'file:person.xml' ); | ||||||||||||||||||||
| 462 | |||||||||||||||||||||
| 463 | =head1 DESCRIPTION | ||||||||||||||||||||
| 464 | |||||||||||||||||||||
| 465 | The C<XML::DOMBacked> class lets you back an object on an XML DOM. Think of it as Class::DBI | ||||||||||||||||||||
| 466 | for XML files. You can specifiy things you want to be properties (nodes), attributes, and | ||||||||||||||||||||
| 467 | other objects. XML::DOMBacked takes care of the heavy lifting so that you don't have to. | ||||||||||||||||||||
| 468 | |||||||||||||||||||||
| 469 | =head1 CONSTRUCTORS | ||||||||||||||||||||
| 470 | |||||||||||||||||||||
| 471 | =over 4 | ||||||||||||||||||||
| 472 | |||||||||||||||||||||
| 473 | =item new() | ||||||||||||||||||||
| 474 | |||||||||||||||||||||
| 475 | Constructs a new object. | ||||||||||||||||||||
| 476 | |||||||||||||||||||||
| 477 | =item from_uri() | ||||||||||||||||||||
| 478 | |||||||||||||||||||||
| 479 | Loads an object from a URI. Expects XML at the other end. | ||||||||||||||||||||
| 480 | |||||||||||||||||||||
| 481 | =back | ||||||||||||||||||||
| 482 | |||||||||||||||||||||
| 483 | =head1 METHODS | ||||||||||||||||||||
| 484 | |||||||||||||||||||||
| 485 | =over 4 | ||||||||||||||||||||
| 486 | |||||||||||||||||||||
| 487 | =item uses_namespace( prefix => uri ) | ||||||||||||||||||||
| 488 | |||||||||||||||||||||
| 489 | Adds an XML namespace to the object. | ||||||||||||||||||||
| 490 | |||||||||||||||||||||
| 491 | =item has_properties( ARRAY ) | ||||||||||||||||||||
| 492 | |||||||||||||||||||||
| 493 | Adds XML Elements to the object. These become accessors. | ||||||||||||||||||||
| 494 | |||||||||||||||||||||
| 495 | =item has_attributes( ARRAY ) | ||||||||||||||||||||
| 496 | |||||||||||||||||||||
| 497 | Adds XML Attributes to the object. These become accessors. | ||||||||||||||||||||
| 498 | |||||||||||||||||||||
| 499 | =item has_a( ARRAY ) | ||||||||||||||||||||
| 500 | |||||||||||||||||||||
| 501 | Adds 1..1 relationships with other classes to the object. The other classes | ||||||||||||||||||||
| 502 | must also inherit from XML::DOMBacked. | ||||||||||||||||||||
| 503 | |||||||||||||||||||||
| 504 | =item has_many( PLURAL => SINGULAR ) | ||||||||||||||||||||
| 505 | |||||||||||||||||||||
| 506 | Adds add_SINGULAR, remove_SINGLUAR and PLURAL methods to the class. | ||||||||||||||||||||
| 507 | |||||||||||||||||||||
| 508 | =item has_many( PLURAL => { class => CLASS } ) | ||||||||||||||||||||
| 509 | |||||||||||||||||||||
| 510 | Looks up the NODENAME for the class, then creates add_NODENAME, remove_NODENAME, and PLURAL methods to the class. | ||||||||||||||||||||
| 511 | |||||||||||||||||||||
| 512 | =back | ||||||||||||||||||||
| 513 | |||||||||||||||||||||
| 514 | =head1 BUGS | ||||||||||||||||||||
| 515 | |||||||||||||||||||||
| 516 | Probably loads. This is really funky, crazy code. I'd be surprised if there aren't bugs. | ||||||||||||||||||||
| 517 | |||||||||||||||||||||
| 518 | =head1 AUTHOR | ||||||||||||||||||||
| 519 | |||||||||||||||||||||
| 520 | James A. Duncan <jduncan@fotango.com> | ||||||||||||||||||||
| 521 | |||||||||||||||||||||
| 522 | =head1 COPYRIGHT | ||||||||||||||||||||
| 523 | |||||||||||||||||||||
| 524 | Copyright 2005 Fotango Ltd. All Rights Reserved. | ||||||||||||||||||||
| 525 | |||||||||||||||||||||
| 526 | =cut | ||||||||||||||||||||