| 1 | package Data::XML::Variant::Build; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use HTML::Entities (); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use aliased 'Data::XML::Variant::Output'; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | # cache for all tag methods added | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 1 | my %METHOD_CACHE; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | Data::XML::Variant::Build - Data::XML::Variant "build" class. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | =head1 VERSION | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | Version 0.01 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | 1 | our $VERSION = '0.01'; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | Do not use this class directly. L<Data::XML::Variant> will return an instance of | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | this class and autogenerate the requested tag methods. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | =head1 EXPORT | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | None. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | =head1 METHODS | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | ############################################################################## | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | =head2 New | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | my $build = Data::XML::Variant::Build->New; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | Returns a new L<Data::XML::Variant::Build> object. Takes no arguments. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | 0 | ▶ | sub New { bless {}, shift } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
New: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | ############################################################################## | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | =head2 Add | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | $xml->Add($tag); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | $xml->Add($tag, $method); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | This method will create three tag methods for you for the start tag, end tag, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | and just "tag". For example: | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | $xml->Add('foo'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | print $xml->start_foo; # <foo> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | print $xml->foo('message'); # <foo>message</foo> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | print $xml->end_foo; # </foo> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | The base name of the methods defaults to to tag name, but a second argument | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | will allow you to specify a different method base name name if the tag name is | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | not a legal method name. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | For example: | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | $xml->Add('foo'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | $xml->Add('florp:bar', 'bar'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | print $xml->start_bar([ id => 3 ]), #attributes to 'bar' | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | $xml->foo('message'), | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | $xml->end_bar; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | # or | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | print $xml->bar( [ id => 3 ], $xml->foo('message') ); # same thing | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | That should output: | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | <florp:bar id="3"><foo>message</foo></florp:bar> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | Method names must be legal for Perl and B<must> begin with a lower-case | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | letter. The latter restriction ensures no collision with the pre-existing | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | methods in this class, all of which begin with an upper-case letter. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | This method will croak if the method already exists in this class or if you | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | attempt to override a method in C<UNIVERSAL>. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | Any arguments passed to the C<end_$tag> method will cause that method to | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | croak. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | B<Note>: because this method adds new methods directly into this namespace, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | all instances of this object will have access to the same methods. See the | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | C<Remove> and C<Methods> methods to see how to manage them. This may change | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | in the future. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | See L<ATTRIBUTES> for information about how attributes are handled. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | 0 | ▶ | sub Add { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
Add: not called CC=5 SCAR=32.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | 0 | my ( $self, $tag, $method_name ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | 0 | ▶ | $method_name ||= $tag; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$method_name ||= $tag
MC/DC0%0/2
$method_name ||= $tag
$method_name
$tag
Truth table0%0/3
A ||= B
A= $method_nameB= $tag
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | 0 | ▶ | if ( UNIVERSAL->can($method_name) ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | 0 | $self->_croak('Cannot override UNIVERSAL methods'); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | 0 | ▶ | unless ( $method_name =~ /^[[:lower:]]/ ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | 0 | $self->_croak( | |||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | "Added methods must begin with a lower-case letter ($method_name)"); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | 0 | ▶ | if ( $self->can($method_name) ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | 0 | $self->_croak("Method ($method_name) already added"); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | 1 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | 0 | my ( $start, $method, $end ) = $self->_tag_methods($method_name); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | 0 | $METHOD_CACHE{$method} = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | 0 | ▶ | *$start = sub { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | 0 | my ( $self, $attributes ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | 0 | ▶ | if ( $attributes && !$self->_has_attributes($attributes) ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$attributes and not $self->_has_attributes($attributes)
MC/DC0%0/2
$attributes and not $self->_has_attributes($attributes)
$attributes
not $self->_has_attributes($attributes)
Truth table0%0/3
A and not(B)
A= $attributesB= not $self->_has_attributes($attributes)
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | 0 | $self->_croak( | |||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | "Argument to start_$method must be an array ref or hash ref"); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | 0 | my $result = "<$tag"; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | 0 | ▶ | if ($attributes) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | 0 | $result .= $self->_attributes($attributes); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | 0 | $result .= ">"; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | 0 | return Output->new($result); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | 0 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | 0 | ▶ | *$method = sub { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | 0 | my ( $self, @data ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 134 | 0 | my $result; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | 0 | ▶ | $result = | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | $self->_has_attributes( $data[0] ) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | ? $self->$start( shift @data ) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | : $self->$start; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | 0 | ▶ | unless (@data) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 140 | 0 | my $closing = $self->Closing; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 141 | 0 | $result =~ s{>$}{$closing>}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 142 | 0 | return Output->new($result); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 145 | 0 | foreach my $data (@data) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 146 | 0 | ▶ | $result .= | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 147 | Output eq ref $data ? $data->output : $self->_encode($data); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 149 | 0 | $result .= $self->$end; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 150 | 0 | return Output->new($result); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 151 | 0 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 153 | 0 | ▶ | *$end = sub { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 154 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 155 | 0 | ▶ | if (@_) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 156 | 0 | $self->_croak("end_$method does not take any arguments"); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 158 | 0 | my $result = "</$tag>"; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 159 | 0 | return Output->new($result); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 160 | 0 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 163 | =head2 Encode | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 165 | $xml->Encode($sub_ref); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 167 | Don't like how the XML is encoded? Supply a subref which handles the encoding | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 168 | for you. The first argument to the subref will be the | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 169 | C<Data::XML::Variant::Build> object and the second argument will be the string | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 170 | to be encoded. For example, to eliminate all encoding: | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 172 | $xml->Encode(sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 173 | my ($self, $string) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 174 | return $string; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 175 | }); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 177 | By default, data is encoded with C<HTML::Entities::encode_entities> with | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 178 | no arguments other than the data string. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 180 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 182 | 0 | ▶ | sub Encode { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
Encode: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 183 | 0 | my ( $self, $sub ) = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 184 | 0 | ▶ | unless ( 'CODE' eq ref $sub ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 185 | 0 | $self->_croak("Argument to Encode() must be a subroutine reference."); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 187 | 0 | *_encode = $sub; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 190 | 0 | ▶ | sub _encode { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_encode: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 191 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 192 | 0 | HTML::Entities::encode_entities(shift); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 193 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 195 | 0 | ▶ | sub _croak { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_croak: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 196 | 0 | my ( $self, @error ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 197 | 0 | require Carp; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 198 | 0 | Carp::croak(@error); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 199 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 201 | ############################################################################## | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 203 | =head2 Closing | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 205 | my $closing = $xml->Closing; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 206 | $xml->Closing(' /'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 208 | This getter/setter determines how self-closing tags terminate. Generally there | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 209 | should not be a space prior to the trailing slash: | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 211 | print $xml->foo; # <foo/> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 213 | Some XML/HTML parsers do not like this and require a space before the trailing | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 214 | slash. Use this method to provide this (or any other closing). | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 216 | $xml->Closing(' /'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 217 | print $xml->foo; # <foo /> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 219 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 221 | 0 | ▶ | sub Closing { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
Closing: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 222 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 223 | 0 | ▶ | return $self->{closing} || '/' unless @_; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$self->{'closing'} || '/'
MC/DC0%0/1
$self->{'closing'} || '/'
$self->{'closing'}
Truth table0%0/2
A
A= $self->{'closing'}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 224 | 0 | $self->{closing} = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 225 | 0 | return $self; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 226 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 228 | ############################################################################## | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 230 | =head2 Quote | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 232 | my $quote = $xml->Quote; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 233 | $xml->Quote("'"); # use single quotes | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 235 | Getter/setter for attribute quote character | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 237 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 239 | 0 | ▶ | sub Quote { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
Quote: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 240 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 241 | 0 | ▶ | return $self->{quote} || '"' unless @_; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$self->{'quote'} || '"'
MC/DC0%0/1
$self->{'quote'} || '"'
$self->{'quote'}
Truth table0%0/2
A
A= $self->{'quote'}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 242 | 0 | $self->{quote} = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 243 | 0 | return $self; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 244 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 246 | ############################################################################## | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 248 | =head2 Methods | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 250 | my @methods = $xml->Methods; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 252 | Returns a list of I<tag> methods which have been added. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 254 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 256 | 0 | ▶ | sub Methods { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
Methods: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 257 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 259 | 0 | return map { $self->_tag_methods($_) } | |||||||||||||||||||||||||||||||||||||||||||||||||
| 260 | sort keys %METHOD_CACHE; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 261 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 263 | ############################################################################## | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 265 | =head2 Remove | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 267 | $xml->Remove('foo'); # remove the foo tag methods | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 268 | $xml->Remove; # remove all tag methods | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 270 | This method allows you to remove undesired methods from C<Data::XML::Build> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 271 | namespace. Specifying a tag name will remove the corresponding start, end, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 272 | and tag methods. Calling without arguments will remove all methods. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 274 | Warns if the tag name is not found. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 276 | Return true on success. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 278 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 280 | 0 | ▶ | sub Remove { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
Remove: not called CC=5 SCAR=30.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 281 | 0 | my ( $self, $tag ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 282 | 0 | my @remove; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 283 | 0 | ▶ | if ( defined $tag ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 284 | 0 | ▶ | unless (exists $METHOD_CACHE{$tag}) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 285 | 0 | require Carp; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 286 | 0 | Carp::carp("Tried to remove unknown tag ($tag)"); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 287 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 288 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 289 | 0 | @remove = $tag; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 290 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 291 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 292 | 0 | @remove = keys %METHOD_CACHE; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 293 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 294 | 0 | foreach my $remove (@remove) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 295 | 0 | foreach my $method ($self->_tag_methods($remove)) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 296 | 1 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 298 | # we need to wipe out the entire glob because "undef &$method" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 299 | # will result in a weird "Not a CODE reference" error if the | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 300 | # method is later called because the other glob slots will still | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 301 | # exist. This is arguably a bug in Perl. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 302 | 0 | undef *$method; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 303 | 0 | delete $METHOD_CACHE{$method}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 304 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 305 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 306 | 0 | return 1; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 307 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 309 | ############################################################################## | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 311 | =head2 Cdata | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 313 | my $Cdata = $xml->Cdata($string); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 315 | Returns a CDATA section for XML. Does not escape data. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 317 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 318 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 319 | 0 | ▶ | sub Cdata { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
Cdata: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 320 | 0 | my ( $self, $data ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 322 | # need to deal with the END marker | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 323 | 0 | return Output->new( '<![CDATA[' . $data . ']]>' ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 324 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 326 | ############################################################################## | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 328 | =head2 Raw | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 330 | print $xml->some_tag($xml->Raw($string)); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 332 | This method allows you to insert I<raw>, unescaped data into your output. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 334 | Use with caution. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 336 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 338 | 0 | ▶ | sub Raw { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
Raw: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 339 | 0 | my ( $self, $string ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 340 | 0 | return Output->new($string); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 341 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 343 | ############################################################################## | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 344 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 345 | =head2 Decl | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 346 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 347 | print $xml->Decl; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 348 | # <?xml version="1.0"?> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 349 | print $xml->Decl([version => '1.0', encoding => "utf-8", standalone => "yes"); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 351 | This method returns an XML declaration with a version of '1.0'. If you desire | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 352 | additional attributes, you may specify an attribute list. C<version> must be | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 353 | explicitly specified if you have attributes. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 354 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 355 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 356 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 357 | 0 | ▶ | sub Decl { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
Decl: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 358 | 0 | my ( $self, $attributes ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 359 | 0 | ▶ | if ( defined $attributes && !$self->_has_attributes($attributes) ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $attributes and not $self->_has_attributes($attributes)
MC/DC0%0/2
defined $attributes and not $self->_has_attributes($attributes)
defined $attributes
not $self->_has_attributes($attributes)
Truth table0%0/3
A and not(B)
A= defined $attributesB= not $self->_has_attributes($attributes)
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 360 | 0 | $self->_croak('You must supply valid attributes to Decl()'); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 361 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 362 | 0 | ▶ | $attributes ||= [ version => '1.0' ]; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$attributes ||= ["version", "1.0"]
MC/DC0%0/1
$attributes ||= ["version", "1.0"]
$attributes
Truth table0%0/2
A
A= $attributes
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 363 | 0 | return Output->new( '<?xml' . $self->_attributes($attributes) . '?>' ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 364 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 365 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 366 | ############################################################################## | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 367 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 368 | =head2 PI | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 369 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 370 | $xml->PI( | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 371 | 'xml-stylesheet', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 372 | [ type => 'text/xsl', href => 'http://www.example.com' ] | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 373 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 374 | # <?xml-stylesheet type="text/xsl" href="http://www.example.com"?> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 375 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 376 | Returns a process instruction. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 377 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 378 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 379 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 380 | 0 | ▶ | sub PI { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
PI: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 381 | 0 | my ( $self, $pi, $attributes ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 382 | 0 | ▶ | if ( defined $attributes && !$self->_has_attributes($attributes) ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $attributes and not $self->_has_attributes($attributes)
MC/DC0%0/2
defined $attributes and not $self->_has_attributes($attributes)
defined $attributes
not $self->_has_attributes($attributes)
Truth table0%0/3
A and not(B)
A= defined $attributesB= not $self->_has_attributes($attributes)
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 383 | 0 | $self->_croak('You must supply valid attributes to Decl()'); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 384 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 385 | 0 | return Output->new( "<?$pi" . $self->_attributes($attributes) . '?>' ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 386 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 387 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 388 | ############################################################################## | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 389 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 390 | =head2 Comment | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 391 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 392 | $xml->Comment('this is a > comment'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 393 | # <!-- this is a > comment --> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 394 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 395 | Returns an XML comment. Comment is padded with one space before and after the | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 396 | C<$comment> string. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 397 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 398 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 399 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 400 | 0 | ▶ | sub Comment { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
Comment: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 401 | 0 | my ($self, $comment) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 402 | 0 | return Output->new('<!-- '.$self->_encode($comment).' -->'); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 403 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 404 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 405 | 0 | ▶ | sub _has_attributes { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_has_attributes: not called CC=6 SCAR=37.4
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 406 | 0 | my ( $self, $data ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 407 | 0 | ▶ | return unless defined $data && ref $data; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $data and ref $data
MC/DC0%0/2
defined $data and ref $data
defined $data
ref $data
Truth table0%0/3
A and B
A= defined $dataB= ref $data
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 408 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 409 | # we're reaching into UNIVERSAL because they data may be a string, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 410 | # a normal reference or a blessed reference | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 411 | 0 | ▶ | return !UNIVERSAL::isa( $data, Output ) | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/9
!UNIVERSAL::isa($data, 'Data::XML::Variant::Output') && (UNIVERSAL::isa($data, 'ARRAY') || UNIVERSAL::isa($data, 'HASH') || UNIVERSAL::isa($data, 'SCALAR'))
UNIVERSAL::isa($data, "ARRAY") or UNIVERSAL::isa($data, "HASH") or UNIVERSAL::isa($data, "SCALAR")
UNIVERSAL::isa($data, "ARRAY") or UNIVERSAL::isa($data, "HASH")
MC/DC0%0/4
!UNIVERSAL::isa($data, 'Data::XML::Variant::Output') && (UNIVERSAL::isa($data, 'ARRAY') || UNIVERSAL::isa($data, 'HASH') || UNIVERSAL::isa($data, 'SCALAR'))
!UNIVERSAL::isa($data, 'Data::XML::Variant::Output')
UNIVERSAL::isa($data, "ARRAY")
UNIVERSAL::isa($data, "HASH")
UNIVERSAL::isa($data, "SCALAR")
Truth table0%0/5
not(A) && B or C or D
A= !UNIVERSAL::isa($data, 'Data::XML::Variant::Output')B= UNIVERSAL::isa($data, "ARRAY")C= UNIVERSAL::isa($data, "HASH")D= UNIVERSAL::isa($data, "SCALAR")
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 412 | && ( UNIVERSAL::isa( $data, 'ARRAY' ) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 413 | || UNIVERSAL::isa( $data, 'HASH' ) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 414 | || UNIVERSAL::isa( $data, 'SCALAR' ) ); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 415 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 416 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 417 | 0 | ▶ | sub _attributes { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_attributes: not called CC=7 SCAR=40.3
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 418 | 0 | my ( $self, $attrs ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 419 | 0 | ▶ | return '' unless $attrs; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 420 | 0 | ▶ | if ( ref $attrs && UNIVERSAL::isa( $attrs, 'SCALAR' ) ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
ref $attrs and UNIVERSAL::isa($attrs, "SCALAR")
MC/DC0%0/2
ref $attrs and UNIVERSAL::isa($attrs, "SCALAR")
ref $attrs
UNIVERSAL::isa($attrs, "SCALAR")
Truth table0%0/3
A and B
A= ref $attrsB= UNIVERSAL::isa($attrs, "SCALAR")
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 421 | 0 | return ' ' . $$attrs; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 422 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 423 | 0 | ▶ | my @attributes = UNIVERSAL::isa( $attrs, 'HASH' ) ? %$attrs : @$attrs; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 424 | 0 | ▶ | return '' unless @attributes; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 425 | 0 | my $result = ''; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 426 | 0 | my $quote = $self->Quote; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 427 | 0 | ▶ | for ( my $i = 0 ; $i < @attributes ; $i += 2 ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 428 | 0 | my ( $attr, $value ) = @attributes[ $i, $i + 1 ]; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 429 | 0 | $value = $self->_encode($value); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 430 | 0 | $result .= qq{ $attr=$quote$value$quote}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 431 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 432 | 0 | return $result; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 433 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 434 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 435 | 0 | ▶ | sub _tag_methods { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_tag_methods: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 436 | 0 | my ( $self, $tag ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 437 | 0 | return ( "start_$tag", $tag, "end_$tag" ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 438 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 439 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 440 | =head1 ATTRIBUTES | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 441 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 442 | Attribute handling is an annoying problem. Many tools require XML attributes | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 443 | to appear in a particular order even though this is not required. We handle | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 444 | this by allowing you to specify attributes in three different ways. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 445 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 446 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 447 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 448 | =item Array references | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 449 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 450 | This is the preferred method. Pass an array reference for attributes and the | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 451 | attributes will be added in the correct order: | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 452 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 453 | print $xml->foo( [ id => 2, class => 'none' ] ); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 454 | # <foo id="2" class="none"/> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 455 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 456 | =item Hash references | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 457 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 458 | This is the traditional method. Pass a hash reference for attributes and the | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 459 | attributes will be added, but the order is not guaranteed: | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 460 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 461 | print $xml->foo( { id => 2, class => 'none' } ); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 462 | # <foo id="2" class="none"/> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 463 | # <foo class="none" id="2"/> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 464 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 465 | =item Scalar references | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 466 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 467 | If you are forced to work with an XML variant which has unusual attribute | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 468 | requirements, you may pass a scalar reference and the attributes will be | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 469 | added to the tag exactly as you have passed them (but there will still be | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 470 | a space after the tag name): | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 471 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 472 | my $attributes = "id=2 selected"; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 473 | print $xml->foo( \$attributes ); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 474 | # <foo id=2 selected/> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 475 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 476 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 477 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 478 | =head1 NEWLINES | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 479 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 480 | Many people don't like their XML running on the same line. Because the goal | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 481 | of this module is to give you fine-grained control over how you need to produce | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 482 | your XML variant, it will not attempt to second guess where you want newlines. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 483 | You will have to insert them yourself. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 484 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 485 | Here's an example. Note how the individual method calls are joined on newlines | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 486 | but method calls inside other method calls have newlines inserted between them. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 487 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 488 | my $xml = Data::XML::Variant->new( | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 489 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 490 | 'ns:foo' => 'foo', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 491 | 'bar' => 'bar', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 492 | 'ns2:baz' => 'baz', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 493 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 494 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 495 | my $xslt_url = 'http://www.example.com/xslt/'; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 496 | my $url = 'http://www.example.com/url/'; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 497 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 498 | print join "\n" => $xml->Decl, # joining outer elements in \n | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 499 | $xml->PI( 'xml-stylesheet', [ type => 'text/xsl', href => "$xslt_url" ] ), | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 500 | $xml->foo( | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 501 | [ id => 3, 'xmlns:ns2' => $url ], "\n", | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 502 | $xml->bar('silly'), "\n", | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 503 | $xml->Comment('this is a > comment'), "\n", | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 504 | $xml->baz( [ 'asdf:some_attr' => 'value' ], 'whee!' ), "\n" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 505 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 506 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 507 | That will print the following: | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 508 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 509 | <?xml version="1.0"?> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 510 | <?xml-stylesheet type="text/xsl" href="$xslt_url"?> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 511 | <ns:foo id="3" xmlns:ns2="$url"> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 512 | <bar>silly</bar> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 513 | <!-- this is a > comment --> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 514 | <ns2:baz asdf:some_attr="value">whee!</ns2:baz> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 515 | </ns:foo> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 516 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 517 | Yes, there are an unbound prefixes in that example. This was deliberate. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 518 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 519 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 520 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 521 | Curtis "Ovid" Poe, C<< <ovid@cpan.org> >> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 522 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 523 | =head1 BUGS | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 524 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 525 | Please report any bugs or feature requests to | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 526 | C<bug-data-xml-variant@rt.cpan.org>, or through the web interface at | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 527 | L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Data-XML-Variant>. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 528 | I will be notified, and then you'll automatically be notified of progress on | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 529 | your bug as I make changes. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 530 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 531 | =head1 COPYRIGHT & LICENSE | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 532 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 533 | Copyright 2005 Curtis "Ovid" Poe, all rights reserved. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 534 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 535 | This program is free software; you can redistribute it and/or modify it | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 536 | under the same terms as Perl itself. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 537 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 538 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 539 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 540 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||