| 1 | package XML::DOM2::Element; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 4 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | =head1 NAME | ||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | XML::DOM2::Element - XML Element level control | ||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | =head1 DISCRIPTION | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | Element base class represents an element at the XML level. | ||||||||||||||||||||||||||||
| 13 | More specific element classes control the xml functionality which is abstracted from the xml. | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | =head1 METHODS | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | =cut | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 4 | ▶ | use base "XML::DOM2::DOM::Element"; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | 4 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 4 | ▶ | use XML::DOM2::Attribute; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 23 | 4 | ▶ | use XML::DOM2::Element::CDATA; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | =head2 $element->new( $name, %options ) | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | Create a new element object. | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | =cut | ||||||||||||||||||||||||||||
| 30 | sub new | ||||||||||||||||||||||||||||
| 31 | 32 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.2
Pod
documented
| |||||||||||||||||||||||||||||
| 32 | 32 | my ($proto, $name, %opts) = @_; | |||||||||||||||||||||||||||
| 33 | 32 | ▶ | my $class = ref($proto) || $proto; | ||||||||||||||||||||||||||
|
Condition33%1/3
ref $proto || $proto
MC/DC0%0/2
ref $proto || $proto
ref $proto
$proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
| |||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 32 | my $self = bless \%opts, $class; | |||||||||||||||||||||||||||
| 36 | 32 | $self->{'name'} = $name; | |||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 32 | return $self; | |||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | =head2 $element->xmlify() | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | Returns the element and all it's sub elements as a serialised xml string (serialisation) | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | =cut | ||||||||||||||||||||||||||||
| 46 | sub xmlify | ||||||||||||||||||||||||||||
| 47 | 10 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
xmlify: called CC=7 SCAR=19.6
Pod
documented
| |||||||||||||||||||||||||||||
| 48 | 10 | my ($self, %p) = @_; | |||||||||||||||||||||||||||
| 49 | 10 | my ($ns, $indent, $level, $sep) = @p{qw/namespace indent level seperator/}; | |||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 10 | ▶ | $indent = ' ' if not $indent; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | 10 | ▶ | $level = 0 if not $level; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 10 | my $xml = $sep; | |||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 10 | $xml .= $indent x $level; | |||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 10 | ▶ | if($self->hasChildren or $self->hasCDATA) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->hasChildren or $self->hasCDATA
MC/DC0%0/2
$self->hasChildren or $self->hasCDATA
$self->hasChildren
$self->hasCDATA
Truth table66%2/3
A or B
A= $self->hasChildrenB= $self->hasCDATA
| |||||||||||||||||||||||||||||
| 59 | 10 | $xml .= $self->_serialise_open_tag($ns); | |||||||||||||||||||||||||||
| 60 | 10 | ▶ | if($self->hasChildren()) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | 4 | foreach my $child ($self->getChildren) { | |||||||||||||||||||||||||||
| 62 | 9 | $xml .= $child->xmlify( | |||||||||||||||||||||||||||
| 63 | indent => $indent, | ||||||||||||||||||||||||||||
| 64 | level => $level+1, | ||||||||||||||||||||||||||||
| 65 | seperator => $sep, | ||||||||||||||||||||||||||||
| 66 | ); | ||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | 4 | $xml .= $sep.($indent x $level); | |||||||||||||||||||||||||||
| 69 | } else { | ||||||||||||||||||||||||||||
| 70 | 6 | $xml .= $self->cdata->text(); | |||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | 10 | $xml .= $self->_serialise_close_tag(); | |||||||||||||||||||||||||||
| 73 | } else { | ||||||||||||||||||||||||||||
| 74 | 0 | $xml .= $self->_serialise_tag(); | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | 10 | return $xml; | |||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | =head2 $element->_element_handle() | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | Inherited method, returns element which is the specific kind | ||||||||||||||||||||||||||||
| 82 | of child object required for this element. | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | =cut | ||||||||||||||||||||||||||||
| 85 | sub _element_handle | ||||||||||||||||||||||||||||
| 86 | 42 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_element_handle: called CC=3 SCAR=11.3
| |||||||||||||||||||||||||||||
| 87 | 42 | my ($self, $name, %opts) = @_; | |||||||||||||||||||||||||||
| 88 | 42 | ▶ | if(defined($self->getParent)) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 89 | 24 | $self->getParent->_element_handle($name, %opts); | |||||||||||||||||||||||||||
| 90 | } elsif($self->document) { | ||||||||||||||||||||||||||||
| 91 | 18 | $self->document->createElement($name, %opts); | |||||||||||||||||||||||||||
| 92 | } else { | ||||||||||||||||||||||||||||
| 93 | 0 | croak "Unable to create element, no document or parent node to create against"; | |||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | =head2 $element->_attribute_handle() | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | Inherited method, returns attribute as new object or undef. | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | $attribute = $element->_attribute_handle( $attribute_name, $ns ); | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | Used by XML::DOM2::DOM for auto attribute object handlers. | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | =cut | ||||||||||||||||||||||||||||
| 106 | sub _attribute_handle | ||||||||||||||||||||||||||||
| 107 | 6 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_attribute_handle: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 108 | 6 | my ($self, $name, %opts) = @_; | |||||||||||||||||||||||||||
| 109 | 6 | return XML::DOM2::Attribute->new( name => $name, owner => $self, %opts ); | |||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | =head2 $element->_has_attribute() | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | Inherited method, returns true if attribute has an object. | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | Used by XML::DOM2::DOM for auto attribute object handlers. | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | =cut | ||||||||||||||||||||||||||||
| 119 | 6 | ▶ | sub _has_attribute { 1 } | ||||||||||||||||||||||||||
|
Subroutine
_has_attribute: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | =head2 $element->_can_contain_elements() | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | Inherited method, returns true if the element can contain sub elements | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | =cut | ||||||||||||||||||||||||||||
| 126 | 0 | ▶ | sub _can_contain_elements { 1 } | ||||||||||||||||||||||||||
|
Subroutine
_can_contain_elements: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | =head2 $element->_can_contain_attributes() | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | Inherited method, returns true if the element can have attributes. | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | =cut | ||||||||||||||||||||||||||||
| 134 | 6 | ▶ | sub _can_contain_attributes { 1 } | ||||||||||||||||||||||||||
|
Subroutine
_can_contain_attributes: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | =head2 $element->_serialise_open_tag() | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | XML ELement serialisation, Open Tag. | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | =cut | ||||||||||||||||||||||||||||
| 141 | sub _serialise_open_tag | ||||||||||||||||||||||||||||
| 142 | 10 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_serialise_open_tag: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 143 | 10 | my ($self) = @_; | |||||||||||||||||||||||||||
| 144 | 10 | my $name = $self->name(); | |||||||||||||||||||||||||||
| 145 | 10 | ▶ | my $at = $self->hasAttributes() ? ' '.$self->_serialise_attributes() : ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 146 | 10 | ▶ | return '' if not defined $name; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 147 | 10 | return "<$name$at>"; | |||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | =head2 $element->_serialise_tag() | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | XML ELement serialisation, Self contained tag. | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | =cut | ||||||||||||||||||||||||||||
| 155 | sub _serialise_tag | ||||||||||||||||||||||||||||
| 156 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_serialise_tag: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 157 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 158 | 0 | my $name = $self->name(); | |||||||||||||||||||||||||||
| 159 | 0 | ▶ | my $at= $self->hasAttributes ? ' '.$self->_serialise_attributes : ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 160 | 0 | return "<$name$at \/>"; | |||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | =head2 $element->_serialise_close_tag() | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | XML ELement serialisation, Close Tag. | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | =cut | ||||||||||||||||||||||||||||
| 168 | sub _serialise_close_tag | ||||||||||||||||||||||||||||
| 169 | 10 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_serialise_close_tag: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 170 | 10 | my ($self) = @_; | |||||||||||||||||||||||||||
| 171 | 10 | my $name = $self->name(); | |||||||||||||||||||||||||||
| 172 | 10 | return "</$name>"; | |||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | =head2 $element->_serialise_attributes() | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | XML ELement serialisation, Attributes. | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | =cut | ||||||||||||||||||||||||||||
| 180 | sub _serialise_attributes | ||||||||||||||||||||||||||||
| 181 | 3 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_serialise_attributes: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 182 | 3 | my ($self) = @_; | |||||||||||||||||||||||||||
| 183 | 3 | return $self->getAttributes(3); | |||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | =head2 $element->error( $command, $error ) | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | Raise an error. | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | =cut | ||||||||||||||||||||||||||||
| 191 | 0 | ▶ | sub error ($$$) { | ||||||||||||||||||||||||||
|
Subroutine
error: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||||||||||
| 192 | 0 | my ($self,$command,$error)=@_; | |||||||||||||||||||||||||||
| 193 | 0 | ▶ | confess "Error requires both command and error" if not $command or not $error; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$command and $error
MC/DC0%0/2
$command and $error
$command
$error
Truth table0%0/3
A and B
A= $commandB= $error
| |||||||||||||||||||||||||||||
| 194 | 0 | ▶ | if($self->document) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 195 | 0 | ▶ | if ($self->document->{-raiseerror}) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 196 | 0 | die "$command: $error\n"; | |||||||||||||||||||||||||||
| 197 | } elsif ($self->document->{-printerror}) { | ||||||||||||||||||||||||||||
| 198 | 0 | print STDERR "$command: $error\n"; | |||||||||||||||||||||||||||
| 199 | } | ||||||||||||||||||||||||||||
| 200 | } | ||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | 0 | $self->{errors}{$command}=$error; | |||||||||||||||||||||||||||
| 203 | } | ||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | =head1 OVERLOADED | ||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | =head2 $object->auto_string() | ||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | =cut | ||||||||||||||||||||||||||||
| 210 | 0 | ▶ | sub auto_string { return $_[0]->hasCDATA() ? $_[0]->cdata() : '' } | ||||||||||||||||||||||||||
|
Subroutine
auto_string: not called CC=2 SCAR=17.9
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | =head2 $object->auto_eq( $string ) | ||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | =cut | ||||||||||||||||||||||||||||
| 215 | 0 | ▶ | sub auto_eq { return shift->auto_string() eq shift } | ||||||||||||||||||||||||||
|
Subroutine
auto_eq: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | =head2 BEGIN() | ||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | POD Catch, imagened method. | ||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||
| 221 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||
| 223 | Martin Owens <doctormo@cpan.org> (Fork) | ||||||||||||||||||||||||||||
| 224 | Ronan Oger <ronan@roasp.com> | ||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||
| 226 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||
| 228 | perl(1),L<XML::DOM2>,L<XML::DOM2::Parser> | ||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||
| 230 | =cut | ||||||||||||||||||||||||||||
| 231 | 4 | 1; | |||||||||||||||||||||||||||