| 1 | package Tags::Output::LibXML; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | # Pragmas. | ||||||||||||||||||||||||||||
| 4 | 13 | ▶ | use base qw(Tags::Output); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 13 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 13 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | # Modules. | ||||||||||||||||||||||||||||
| 9 | 13 | ▶ | use Error::Pure qw(err); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 13 | ▶ | use Readonly; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 13 | ▶ | use XML::LibXML; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | # Constants. | ||||||||||||||||||||||||||||
| 14 | 13 | Readonly::Scalar my $EMPTY_STR => q{}; | |||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | # Version. | ||||||||||||||||||||||||||||
| 17 | 13 | our $VERSION = 0.01; | |||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | # Flush tags in object. | ||||||||||||||||||||||||||||
| 20 | sub flush { | ||||||||||||||||||||||||||||
| 21 | 28 | ▶ | my ($self, $reset_flag) = @_; | ||||||||||||||||||||||||||
|
Subroutine
flush: called CC=6 SCAR=21.9
Pod
documented
| |||||||||||||||||||||||||||||
| 22 | 28 | my $ret; | |||||||||||||||||||||||||||
| 23 | 28 | my $ouf = $self->{'output_handler'}; | |||||||||||||||||||||||||||
| 24 | 28 | ▶ | if ($ouf) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 25 | 13 | ▶ | no warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 26 | 0 | ▶ | print {$ouf} $self->{'doc'}->toString( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 27 | 0 | ▶ | $self->{'set_indent'} ? 2 : 0) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 28 | or err 'Cannot write to output handler.'; | ||||||||||||||||||||||||||||
| 29 | } else { | ||||||||||||||||||||||||||||
| 30 | $ret = $self->{'doc'}->toString( | ||||||||||||||||||||||||||||
| 31 | 28 | ▶ | $self->{'set_indent'} ? 2 : 0); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | # Reset. | ||||||||||||||||||||||||||||
| 35 | 28 | ▶ | if ($reset_flag) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 36 | 0 | $self->reset; | |||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 28 | return $ret; | |||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | # Resets internal variables. | ||||||||||||||||||||||||||||
| 43 | sub reset { | ||||||||||||||||||||||||||||
| 44 | 29 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
reset: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | # Root node. | ||||||||||||||||||||||||||||
| 47 | $self->{'doc'} = XML::LibXML::Document->new( | ||||||||||||||||||||||||||||
| 48 | $self->{'xml_version'}, | ||||||||||||||||||||||||||||
| 49 | 29 | $self->{'encoding'}, | |||||||||||||||||||||||||||
| 50 | ); | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | # First node = root node. | ||||||||||||||||||||||||||||
| 53 | 29 | $self->{'first'} = 0; | |||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | # Printed tags. | ||||||||||||||||||||||||||||
| 56 | 29 | $self->{'printed_tags'} = []; | |||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 29 | return; | |||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | # Check parameters to rigth values. | ||||||||||||||||||||||||||||
| 62 | sub _check_params { | ||||||||||||||||||||||||||||
| 63 | 14 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_check_params: called CC=3 SCAR=13.3
| |||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | # Check to output handler. | ||||||||||||||||||||||||||||
| 66 | 14 | ▶ | if (defined $self->{'output_handler'} | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $self->{"output_handler"} and ref $self->{"output_handler"} ne "GLOB"
MC/DC0%0/2
defined $self->{"output_handler"} and ref $self->{"output_handler"} ne "GLOB"
defined $self->{"output_handler"}
ref $self->{"output_handler"} ne "GLOB"
Truth table33%1/3
A and B
A= defined $self->{"output_handler"}B= ref $self->{"output_handler"} ne "GLOB"
| |||||||||||||||||||||||||||||
| 67 | && ref $self->{'output_handler'} ne 'GLOB') { | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 0 | err 'Output handler is bad file handler.'; | |||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 14 | return; | |||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | # Default parameters. | ||||||||||||||||||||||||||||
| 76 | sub _default_parameters { | ||||||||||||||||||||||||||||
| 77 | 16 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_default_parameters: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | # CDATA callback. | ||||||||||||||||||||||||||||
| 80 | 16 | $self->{'cdata_callback'} = undef; | |||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | # Data callback. | ||||||||||||||||||||||||||||
| 83 | 16 | $self->{'data_callback'} = undef; | |||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | # Document encoding. | ||||||||||||||||||||||||||||
| 86 | 16 | $self->{'encoding'} = 'UTF-8'; | |||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | # No simple tags. | ||||||||||||||||||||||||||||
| 89 | # TODO not implemented. | ||||||||||||||||||||||||||||
| 90 | 16 | $self->{'no_simple'} = []; | |||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | # Set output handler. | ||||||||||||||||||||||||||||
| 93 | 16 | $self->{'output_handler'} = undef; | |||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | # Preserved tags. | ||||||||||||||||||||||||||||
| 96 | # TODO not implemented. | ||||||||||||||||||||||||||||
| 97 | 16 | $self->{'preserved'} = []; | |||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | # Set indent. | ||||||||||||||||||||||||||||
| 100 | 16 | $self->{'set_indent'} = 0; | |||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | # Skip bad tags. | ||||||||||||||||||||||||||||
| 103 | 16 | $self->{'skip_bad_tags'} = 0; | |||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | # XML version. | ||||||||||||||||||||||||||||
| 106 | 16 | $self->{'xml_version'} = '1.1'; | |||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 16 | return; | |||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | # Attributes. | ||||||||||||||||||||||||||||
| 112 | sub _put_attribute { | ||||||||||||||||||||||||||||
| 113 | 14 | ▶ | my ($self, $attr, $value) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_put_attribute: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 114 | 14 | $self->{'printed_tags'}->[0]->setAttribute($attr, $value); | |||||||||||||||||||||||||||
| 115 | 14 | return; | |||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | # Begin of tag. | ||||||||||||||||||||||||||||
| 119 | sub _put_begin_of_tag { | ||||||||||||||||||||||||||||
| 120 | 28 | ▶ | my ($self, $tag) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_put_begin_of_tag: called CC=3 SCAR=11.1
| |||||||||||||||||||||||||||||
| 121 | 28 | my $begin_node = $self->{'doc'}->createElement($tag); | |||||||||||||||||||||||||||
| 122 | 28 | ▶ | if ($self->{'first'} == 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 123 | 24 | $self->{'doc'}->setDocumentElement($begin_node); | |||||||||||||||||||||||||||
| 124 | 24 | $self->{'first'} = 1; | |||||||||||||||||||||||||||
| 125 | } else { | ||||||||||||||||||||||||||||
| 126 | 4 | ▶ | if (! $self->{'printed_tags'}->[0]) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 127 | 0 | err "Second root tag '$tag' is bad."; | |||||||||||||||||||||||||||
| 128 | } else { | ||||||||||||||||||||||||||||
| 129 | 4 | $self->{'printed_tags'}->[0]->addChild($begin_node); | |||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | 28 | unshift @{$self->{'printed_tags'}}, $begin_node; | |||||||||||||||||||||||||||
| 133 | 28 | return; | |||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | # CData. | ||||||||||||||||||||||||||||
| 137 | sub _put_cdata { | ||||||||||||||||||||||||||||
| 138 | 5 | ▶ | my ($self, @cdata) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_put_cdata: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 139 | 5 | $self->_process_callback(\@cdata, 'cdata_callback'); | |||||||||||||||||||||||||||
| 140 | 5 | my $cdata = join($EMPTY_STR, @cdata); | |||||||||||||||||||||||||||
| 141 | 5 | my $cdata_node = $self->{'doc'}->createCDATASection($cdata); | |||||||||||||||||||||||||||
| 142 | 5 | $self->{'printed_tags'}->[0]->addChild($cdata_node); | |||||||||||||||||||||||||||
| 143 | 5 | return; | |||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | # Comment. | ||||||||||||||||||||||||||||
| 147 | sub _put_comment { | ||||||||||||||||||||||||||||
| 148 | 11 | ▶ | my ($self, @comments) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_put_comment: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 149 | 11 | my $comment = join($EMPTY_STR, @comments); | |||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | # HACK LibXML has a bug. | ||||||||||||||||||||||||||||
| 152 | 11 | ▶ | if ($comment =~ m/-$/ms) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 153 | 1 | $comment .= ' '; | |||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | 11 | my $comment_node = $self->{'doc'}->createComment($comment); | |||||||||||||||||||||||||||
| 157 | 11 | ▶ | if (! defined $self->{'printed_tags'}->[0]) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 158 | 4 | $self->{'doc'}->appendChild($comment_node); | |||||||||||||||||||||||||||
| 159 | } else { | ||||||||||||||||||||||||||||
| 160 | 7 | $self->{'printed_tags'}->[0]->addChild($comment_node); | |||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||
| 162 | 11 | return; | |||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | # Data. | ||||||||||||||||||||||||||||
| 166 | sub _put_data { | ||||||||||||||||||||||||||||
| 167 | 10 | ▶ | my ($self, @data) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_put_data: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 168 | 10 | $self->_process_callback(\@data, 'data_callback'); | |||||||||||||||||||||||||||
| 169 | 10 | my $data = join($EMPTY_STR, @data); | |||||||||||||||||||||||||||
| 170 | 10 | my $data_node = $self->{'doc'}->createTextNode($data); | |||||||||||||||||||||||||||
| 171 | 10 | $self->{'printed_tags'}->[0]->addChild($data_node); | |||||||||||||||||||||||||||
| 172 | 10 | return; | |||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | # End of tag. | ||||||||||||||||||||||||||||
| 176 | sub _put_end_of_tag { | ||||||||||||||||||||||||||||
| 177 | 28 | ▶ | my ($self, $tag) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_put_end_of_tag: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 178 | 28 | shift @{$self->{'printed_tags'}}; | |||||||||||||||||||||||||||
| 179 | 28 | return; | |||||||||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | # Instruction. | ||||||||||||||||||||||||||||
| 183 | sub _put_instruction { | ||||||||||||||||||||||||||||
| 184 | 1 | ▶ | my ($self, $target, $code) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_put_instruction: called CC=2 SCAR=7.2
| |||||||||||||||||||||||||||||
| 185 | 1 | my $instruction_node = $self->{'doc'}->createProcessingInstruction( | |||||||||||||||||||||||||||
| 186 | $target, $code, | ||||||||||||||||||||||||||||
| 187 | ); | ||||||||||||||||||||||||||||
| 188 | 1 | ▶ | if (! defined $self->{'printed_tags'}->[0]) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 189 | 1 | $self->{'doc'}->appendChild($instruction_node); | |||||||||||||||||||||||||||
| 190 | } else { | ||||||||||||||||||||||||||||
| 191 | 0 | $self->{'printed_tags'}->[0]->addChild($instruction_node); | |||||||||||||||||||||||||||
| 192 | } | ||||||||||||||||||||||||||||
| 193 | 1 | return; | |||||||||||||||||||||||||||
| 194 | } | ||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | # Raw data. | ||||||||||||||||||||||||||||
| 197 | sub _put_raw { | ||||||||||||||||||||||||||||
| 198 | 2 | ▶ | my ($self, @raw_data) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_put_raw: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 199 | 2 | return; | |||||||||||||||||||||||||||
| 200 | } | ||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | 13 | 1; | |||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | __END__ | ||||||||||||||||||||||||||||