| 1 | package App::hopen::Util::XML::FromPerl; | ||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||
| 3 | 1 | our $VERSION = '0.000015'; | |||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||
| 8 | # === Warnings ============================================================== | ||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||
| 10 | # Set up for warnings. We can't do this in a separate package because | ||||||||||||||||||||||||||||||||
| 11 | # warnings::enabled() and related rely on caller being the package that | ||||||||||||||||||||||||||||||||
| 12 | # invoked this one, not this package itself. | ||||||||||||||||||||||||||||||||
| 13 | 1 | ▶ | use if $] ge '5.014', qw(warnings::register undefined); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 14 | 1 | ▶ | use if $] lt '5.014', qw(warnings::register); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||
| 16 | # @_warning_category is the category in which we will warn, or an empty list. | ||||||||||||||||||||||||||||||||
| 17 | # @_warning_categories is the list of categories we need to check to see | ||||||||||||||||||||||||||||||||
| 18 | # if we should warn. | ||||||||||||||||||||||||||||||||
| 19 | 1 | ▶ | use vars qw(@_warning_category @_warning_categories); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||
| 21 | 1 | ▶ | if($] ge '5.014') { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 22 | 1 | @_warning_category = (__PACKAGE__ . '::undefined'); | |||||||||||||||||||||||||||||||
| 23 | 1 | @_warning_categories = (__PACKAGE__, @_warning_category); | |||||||||||||||||||||||||||||||
| 24 | } else { | ||||||||||||||||||||||||||||||||
| 25 | 0 | @_warning_category = (); | |||||||||||||||||||||||||||||||
| 26 | 0 | @_warning_categories = __PACKAGE__; | |||||||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||
| 29 | # Emit a warning and return a value. Call via goto. Usage: | ||||||||||||||||||||||||||||||||
| 30 | # @_ = ("warning message", $return_value); | ||||||||||||||||||||||||||||||||
| 31 | # goto &_emit_warning; | ||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||
| 33 | 0 | ▶ | sub _emit_warning { | ||||||||||||||||||||||||||||||
|
Subroutine
_emit_warning: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||||||
| 34 | 0 | my ($message, $retval) = @_; | |||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||
| 36 | # Are all the categories of interest enabled? | ||||||||||||||||||||||||||||||||
| 37 | 0 | my $should_emit = 1; | |||||||||||||||||||||||||||||||
| 38 | 0 | foreach(@_warning_categories) { | |||||||||||||||||||||||||||||||
| 39 | 0 | ▶ | if(!warnings::enabled($_)) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 40 | 0 | $should_emit = 0; | |||||||||||||||||||||||||||||||
| 41 | 0 | last; | |||||||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||
| 45 | 0 | ▶ | warnings::warn(@_warning_category, $message) if $should_emit; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||
| 47 | 0 | return $retval; | |||||||||||||||||||||||||||||||
| 48 | } #_emit_warning | ||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||
| 50 | # === Code ================================================================== | ||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||
| 52 | 1 | ▶ | use XML::LibXML; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||
| 54 | 1 | ▶ | use parent 'Exporter'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 55 | 1 | our @EXPORT_OK = qw(xml_from_perl xml_node_from_perl); | |||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||
| 57 | # Fill in the children of the given node from the passed value. | ||||||||||||||||||||||||||||||||
| 58 | # No return value. | ||||||||||||||||||||||||||||||||
| 59 | 5 | ▶ | sub _fill_node_children { | ||||||||||||||||||||||||||||||
|
Subroutine
_fill_node_children: called CC=15 SCAR=30.0
| |||||||||||||||||||||||||||||||||
| 60 | 5 | my ($doc, $parent, $data) = @_; | |||||||||||||||||||||||||||||||
| 61 | 5 | ▶ | unless(defined $data) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 62 | 0 | @_ = ("I can't create an XML node from undefined data", undef); | |||||||||||||||||||||||||||||||
| 63 | 0 | goto &_emit_warning; | |||||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||
| 66 | 5 | my ($one, $has_attrs); | |||||||||||||||||||||||||||||||
| 67 | 5 | ▶ | if(ref $data eq 'ARRAY') { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 68 | 5 | $one = $data->[1]; | |||||||||||||||||||||||||||||||
| 69 | 5 | $has_attrs = ref $one eq 'HASH'; | |||||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||
| 72 | 5 | my $new_node; | |||||||||||||||||||||||||||||||
| 73 | 5 | ▶ | if (ref $data eq 'ARRAY' && $data->[0] eq '!--') { # Comment | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition66%2/3
ref $data eq "ARRAY" and $data->[0] eq "!--"
MC/DC50%1/2
ref $data eq "ARRAY" and $data->[0] eq "!--"
ref $data eq "ARRAY"
$data->[0] eq "!--"
Truth table66%2/3
A and B
A= ref $data eq "ARRAY"B= $data->[0] eq "!--"
| |||||||||||||||||||||||||||||||||
| 74 | 1 | ▶ | my $separ = defined $, ? $, : ' '; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||
| 76 | # Grab the plain text nodes and paste them together. | ||||||||||||||||||||||||||||||||
| 77 | my $text = join $separ, | ||||||||||||||||||||||||||||||||
| 78 | 1 | map { $data->[$_] } | |||||||||||||||||||||||||||||||
| 79 | 1 | ▶ | grep { defined $data->[$_] and not ref $data->[$_] } | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $data->[$_] && !ref($data->[$_])
MC/DC0%0/2
defined $data->[$_] && !ref($data->[$_])
defined $data->[$_]
!ref($data->[$_])
Truth table33%1/3
A && not(B)
A= defined $data->[$_]B= !ref($data->[$_])
| |||||||||||||||||||||||||||||||||
| 80 | (($has_attrs ? 2 : 1) .. $#$data); | ||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||
| 82 | 1 | $new_node = $doc->createComment($text); | |||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||
| 84 | } elsif (ref $data eq 'ARRAY') { # Regular node | ||||||||||||||||||||||||||||||||
| 85 | 4 | $new_node = $doc->createElement($data->[0]); | |||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||
| 87 | 4 | ▶ | if ($has_attrs) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 88 | 4 | my @keys = keys %$one; | |||||||||||||||||||||||||||||||
| 89 | 4 | ▶ | @keys = sort @keys unless tied %$one; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 90 | 4 | for (@keys) { | |||||||||||||||||||||||||||||||
| 91 | 5 | ▶ | if (defined (my $v = $one->{$_})) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 92 | 5 | $new_node->setAttribute($_, $v); | |||||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||
| 97 | _fill_node_children($doc, $new_node, $data->[$_]) | ||||||||||||||||||||||||||||||||
| 98 | 4 | ▶ | for grep { defined $data->[$_] } (($has_attrs ? 2 : 1) .. $#$data); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||
| 100 | } else { # Text node | ||||||||||||||||||||||||||||||||
| 101 | 0 | $new_node = $doc->createTextNode("$data"); | |||||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||
| 104 | 5 | $parent->appendChild($new_node); | |||||||||||||||||||||||||||||||
| 105 | 5 | return undef; | |||||||||||||||||||||||||||||||
| 106 | } #_fill_node_children | ||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||
| 108 | # Create a phony element we can use as a temporary parent node | ||||||||||||||||||||||||||||||||
| 109 | 1 | ▶ | sub _create_phony { | ||||||||||||||||||||||||||||||
|
Subroutine
_create_phony: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 110 | 1 | my $doc = shift; | |||||||||||||||||||||||||||||||
| 111 | 1 | return $doc->createElementNS('https://metacpan.org/pod/XML::FromPerl', | |||||||||||||||||||||||||||||||
| 112 | 'phony_root'); | ||||||||||||||||||||||||||||||||
| 113 | } #_create_phony | ||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||
| 115 | 0 | ▶ | sub xml_node_from_perl { | ||||||||||||||||||||||||||||||
|
Subroutine
xml_node_from_perl: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 116 | 0 | my $doc = shift; | |||||||||||||||||||||||||||||||
| 117 | 0 | my $data = shift; | |||||||||||||||||||||||||||||||
| 118 | 0 | my $parent; | |||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||
| 120 | 0 | $parent = _create_phony($doc); | |||||||||||||||||||||||||||||||
| 121 | 0 | _fill_node_children $doc, $parent, $data; | |||||||||||||||||||||||||||||||
| 122 | 0 | return $parent->firstChild; | |||||||||||||||||||||||||||||||
| 123 | } #xml_node_from_perl | ||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||
| 125 | 1 | ▶ | sub xml_from_perl { | ||||||||||||||||||||||||||||||
|
Subroutine
xml_from_perl: called CC=2 SCAR=7.2
Pod
documented
| |||||||||||||||||||||||||||||||||
| 126 | 1 | my $data = shift; | |||||||||||||||||||||||||||||||
| 127 | 1 | my $doc = XML::LibXML::Document->new(@_); | |||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||
| 129 | 1 | ▶ | unless(defined $data) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 130 | 0 | @_ = ("I can't create an XML document from undefined data", $doc); | |||||||||||||||||||||||||||||||
| 131 | 0 | goto &_emit_warning; | |||||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||
| 134 | 1 | my $parent = _create_phony($doc); | |||||||||||||||||||||||||||||||
| 135 | 1 | $doc->setDocumentElement($parent); | |||||||||||||||||||||||||||||||
| 136 | 1 | _fill_node_children $doc, $parent, $data; | |||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||
| 138 | 1 | $doc->setDocumentElement($parent->firstChild); | |||||||||||||||||||||||||||||||
| 139 | 1 | return $doc; | |||||||||||||||||||||||||||||||
| 140 | } #xml_from_perl | ||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||
| 142 | 1 | 1; | |||||||||||||||||||||||||||||||
| 143 | __END__ | ||||||||||||||||||||||||||||||||