| 1 | # | ||||||||||||||||||||
| 2 | # Copyright (c) 2013-2020 Christian Jaeger, copying@christianjaeger.ch | ||||||||||||||||||||
| 3 | # | ||||||||||||||||||||
| 4 | # This is free software, offered under either the same terms as perl 5 | ||||||||||||||||||||
| 5 | # or the terms of the Artistic License version 2 or the terms of the | ||||||||||||||||||||
| 6 | # MIT License (Expat version). See the file COPYING.md that came | ||||||||||||||||||||
| 7 | # bundled with this file. | ||||||||||||||||||||
| 8 | # | ||||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | =head1 NAME | ||||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | PXML::Element - base class for PXML elements | ||||||||||||||||||||
| 13 | |||||||||||||||||||||
| 14 | =head1 SYNOPSIS | ||||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | =head1 DESCRIPTION | ||||||||||||||||||||
| 17 | |||||||||||||||||||||
| 18 | |||||||||||||||||||||
| 19 | =head1 NOTE | ||||||||||||||||||||
| 20 | |||||||||||||||||||||
| 21 | This is alpha software! Read the status section in the package README | ||||||||||||||||||||
| 22 | or on the L<website|http://functional-perl.org/>. | ||||||||||||||||||||
| 23 | |||||||||||||||||||||
| 24 | =cut | ||||||||||||||||||||
| 25 | |||||||||||||||||||||
| 26 | package PXML::Element; | ||||||||||||||||||||
| 27 | |||||||||||||||||||||
| 28 | 8 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 29 | 8 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 30 | 8 | ▶ | use warnings FATAL => 'uninitialized'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 31 | |||||||||||||||||||||
| 32 | 8 | ▶ | use FP::Stream qw(stream_mixed_flatten stream_map); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 33 | 8 | ▶ | use FP::Hash qw($empty_hash hash_set); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 34 | |||||||||||||||||||||
| 35 | # XX Depend on these? PXML::Serialize uses these, so any app that | ||||||||||||||||||||
| 36 | # serializes PXML would require them anyway. | ||||||||||||||||||||
| 37 | 8 | ▶ | use FP::Lazy; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 38 | 8 | ▶ | use FP::List; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 39 | 8 | ▶ | use Chj::xIO qw(capture_stdout); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 40 | 8 | ▶ | use Scalar::Util qw(blessed); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 41 | |||||||||||||||||||||
| 42 | 8 | ▶ | use Chj::NamespaceCleanAbove; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 43 | 8 | ▶ | use FP::Carp; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: not called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 44 | |||||||||||||||||||||
| 45 | # [ name, attributes, body ] | ||||||||||||||||||||
| 46 | BEGIN { | ||||||||||||||||||||
| 47 | sub NAME () {0} | ||||||||||||||||||||
| 48 | sub ATTRIBUTES () {1} | ||||||||||||||||||||
| 49 | sub BODY () {2} | ||||||||||||||||||||
| 50 | } | ||||||||||||||||||||
| 51 | |||||||||||||||||||||
| 52 | 282 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||||||||||||
| 53 | 282 | my $cl = shift; | |||||||||||||||||||
| 54 | 282 | ▶ | @_ == 3 or fp_croak_arity 3; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 55 | 282 | bless [@_], $cl | |||||||||||||||||||
| 56 | } | ||||||||||||||||||||
| 57 | |||||||||||||||||||||
| 58 | 382 | ▶ | sub name { | ||||||||||||||||||
|
Subroutine
name: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 59 | 382 | $_[0][NAME] | |||||||||||||||||||
| 60 | } | ||||||||||||||||||||
| 61 | |||||||||||||||||||||
| 62 | 0 | ▶ | sub lcname { | ||||||||||||||||||
|
Subroutine
lcname: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 63 | 0 | lc($_[0][NAME]) | |||||||||||||||||||
| 64 | } | ||||||||||||||||||||
| 65 | |||||||||||||||||||||
| 66 | 244 | ▶ | sub maybe_attributes { | ||||||||||||||||||
|
Subroutine
maybe_attributes: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 67 | 244 | $_[0][ATTRIBUTES] | |||||||||||||||||||
| 68 | } | ||||||||||||||||||||
| 69 | |||||||||||||||||||||
| 70 | # NOTE that $empty_hash gives exceptions for accesses to any field! | ||||||||||||||||||||
| 71 | # (With the current implementation of locked / const hashes in perl.) | ||||||||||||||||||||
| 72 | # `exists` works as expected though. But then, PXML (as all of FP) may | ||||||||||||||||||||
| 73 | # move to restricted hashes and arrays everywhere anyway, so this | ||||||||||||||||||||
| 74 | # should be consistent then. | ||||||||||||||||||||
| 75 | 0 | ▶ | sub attributes { | ||||||||||||||||||
|
Subroutine
attributes: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||
| 76 | 0 | ▶ | $_[0][ATTRIBUTES] // $empty_hash | ||||||||||||||||||
|
Condition0%0/3
$_[0][1] // $PXML::Element::empty_hash
MC/DC0%0/2
$_[0][1] // $PXML::Element::empty_hash
$_[0][1]
$PXML::Element::empty_hash
Truth table0%0/3
A // B
A= $_[0][1]B= $PXML::Element::empty_hash
| |||||||||||||||||||||
| 77 | } | ||||||||||||||||||||
| 78 | |||||||||||||||||||||
| 79 | 296 | ▶ | sub body { | ||||||||||||||||||
|
Subroutine
body: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 80 | |||||||||||||||||||||
| 81 | # could be undef, too, but then undef is the empty list when | ||||||||||||||||||||
| 82 | # interpreted as a FP::List, thus no need for the maybe_ | ||||||||||||||||||||
| 83 | # prefix. -- XXX that's not true anymore, null is not undef anymore. | ||||||||||||||||||||
| 84 | 296 | $_[0][BODY] | |||||||||||||||||||
| 85 | } | ||||||||||||||||||||
| 86 | |||||||||||||||||||||
| 87 | 26 | ▶ | sub maybe_attribute { | ||||||||||||||||||
|
Subroutine
maybe_attribute: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||
| 88 | 26 | ▶ | @_ == 2 or fp_croak_arity 2; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 89 | 26 | my $s = shift; | |||||||||||||||||||
| 90 | 26 | my ($name) = @_; | |||||||||||||||||||
| 91 | 26 | ▶ | defined($$s[ATTRIBUTES]) ? $$s[ATTRIBUTES]{$name} : undef | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 92 | } | ||||||||||||||||||||
| 93 | |||||||||||||||||||||
| 94 | 7 | ▶ | sub perhaps_attribute { | ||||||||||||||||||
|
Subroutine
perhaps_attribute: called CC=4 SCAR=15.2
Pod
undocumented
| |||||||||||||||||||||
| 95 | 7 | ▶ | @_ == 2 or fp_croak_arity 2; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 96 | 7 | my $s = shift; | |||||||||||||||||||
| 97 | 7 | my ($name) = @_; | |||||||||||||||||||
| 98 | 7 | ▶ | if (defined(my $h = $$s[ATTRIBUTES])) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 99 | 7 | ▶ | exists $$h{$name} ? $$h{$name} : () | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 100 | } else { | ||||||||||||||||||||
| 101 | () | ||||||||||||||||||||
| 102 | 0 | } | |||||||||||||||||||
| 103 | } | ||||||||||||||||||||
| 104 | |||||||||||||||||||||
| 105 | # functional setters (following the convention I've started to use of | ||||||||||||||||||||
| 106 | # "trailing _set means functional, leading set_ means mutation")) | ||||||||||||||||||||
| 107 | |||||||||||||||||||||
| 108 | 0 | ▶ | sub name_set { | ||||||||||||||||||
|
Subroutine
name_set: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||
| 109 | 0 | my $s = shift; | |||||||||||||||||||
| 110 | 0 | ▶ | @_ == 1 or fp_croak_arity 1; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 111 | 0 | bless [$_[0], $$s[ATTRIBUTES], $$s[BODY]], ref $s | |||||||||||||||||||
| 112 | } | ||||||||||||||||||||
| 113 | |||||||||||||||||||||
| 114 | 2 | ▶ | sub attributes_set { | ||||||||||||||||||
|
Subroutine
attributes_set: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||||||||||||
| 115 | 2 | my $s = shift; | |||||||||||||||||||
| 116 | 2 | ▶ | @_ == 1 or fp_croak_arity 1; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 117 | 2 | bless [$$s[NAME], $_[0], $$s[BODY]], ref $s | |||||||||||||||||||
| 118 | } | ||||||||||||||||||||
| 119 | |||||||||||||||||||||
| 120 | 3 | ▶ | sub attribute_set { | ||||||||||||||||||
|
Subroutine
attribute_set: called CC=3 SCAR=11.4
Pod
undocumented
| |||||||||||||||||||||
| 121 | 3 | my $s = shift; | |||||||||||||||||||
| 122 | 3 | ▶ | @_ == 2 or fp_croak_arity 2; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 123 | 3 | my ($nam, $v) = @_; | |||||||||||||||||||
| 124 | 3 | ▶ | bless [$$s[NAME], hash_set($$s[1] // {}, $nam, $v), $$s[BODY]], ref $s | ||||||||||||||||||
|
Condition50%1/2
$s->[1] // {}
MC/DC0%0/1
$s->[1] // {}
$s->[1]
Truth table50%1/2
A
A= $s->[1]
| |||||||||||||||||||||
| 125 | } | ||||||||||||||||||||
| 126 | |||||||||||||||||||||
| 127 | 23 | ▶ | sub body_set { | ||||||||||||||||||
|
Subroutine
body_set: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||||||||||||
| 128 | 23 | my $s = shift; | |||||||||||||||||||
| 129 | 23 | ▶ | @_ == 1 or fp_croak_arity 1; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 130 | 23 | bless [$$s[NAME], $$s[ATTRIBUTES], $_[0]], ref $s | |||||||||||||||||||
| 131 | } | ||||||||||||||||||||
| 132 | |||||||||||||||||||||
| 133 | # functional updaters | ||||||||||||||||||||
| 134 | |||||||||||||||||||||
| 135 | 2 | ▶ | sub name_update { | ||||||||||||||||||
|
Subroutine
name_update: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||
| 136 | 2 | my $s = shift; | |||||||||||||||||||
| 137 | 2 | ▶ | @_ == 1 or fp_croak_arity 1; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 138 | 2 | my ($fn) = @_; | |||||||||||||||||||
| 139 | 2 | bless [&$fn($$s[NAME]), $$s[ATTRIBUTES], $$s[BODY]], ref $s | |||||||||||||||||||
| 140 | } | ||||||||||||||||||||
| 141 | |||||||||||||||||||||
| 142 | 0 | ▶ | sub attributes_update { | ||||||||||||||||||
|
Subroutine
attributes_update: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||
| 143 | 0 | my $s = shift; | |||||||||||||||||||
| 144 | 0 | ▶ | @_ == 1 or fp_croak_arity 1; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 145 | 0 | my ($fn) = @_; | |||||||||||||||||||
| 146 | 0 | bless [$$s[NAME], &$fn($$s[ATTRIBUTES]), $$s[BODY]], ref $s | |||||||||||||||||||
| 147 | } | ||||||||||||||||||||
| 148 | |||||||||||||||||||||
| 149 | 50 | ▶ | sub body_update { | ||||||||||||||||||
|
Subroutine
body_update: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||
| 150 | 50 | my $s = shift; | |||||||||||||||||||
| 151 | 50 | ▶ | @_ == 1 or fp_croak_arity 1; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 152 | 50 | my ($fn) = @_; | |||||||||||||||||||
| 153 | 50 | bless [$$s[NAME], $$s[ATTRIBUTES], &$fn($$s[BODY])], ref $s | |||||||||||||||||||
| 154 | } | ||||||||||||||||||||
| 155 | |||||||||||||||||||||
| 156 | # mapping | ||||||||||||||||||||
| 157 | |||||||||||||||||||||
| 158 | 35 | ▶ | sub body_map { | ||||||||||||||||||
|
Subroutine
body_map: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||
| 159 | 35 | my $s = shift; | |||||||||||||||||||
| 160 | 35 | ▶ | @_ == 1 or fp_croak_arity 1; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 161 | 35 | my ($fn) = @_; | |||||||||||||||||||
| 162 | 35 | ▶ | $s->body_update(sub { stream_map $fn, stream_mixed_flatten $_[0] }) | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 163 | 35 | } | |||||||||||||||||||
| 164 | |||||||||||||||||||||
| 165 | # "body text", a string, dropping tags; not having knowledge about | ||||||||||||||||||||
| 166 | # which XML tags have 'relevant body text', this returns all of it. | ||||||||||||||||||||
| 167 | |||||||||||||||||||||
| 168 | # XX ugly: this is replicating part of the serializer. But don't want | ||||||||||||||||||||
| 169 | # to touch the code there... so, here goes. Really, better languages | ||||||||||||||||||||
| 170 | # have been created to write code in. | ||||||||||||||||||||
| 171 | |||||||||||||||||||||
| 172 | 78 | ▶ | sub _text { | ||||||||||||||||||
|
Subroutine
_text: called CC=9 SCAR=22.8
| |||||||||||||||||||||
| 173 | 78 | my ($v) = @_; | |||||||||||||||||||
| 174 | 78 | ▶ | if (defined $v) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 175 | 77 | ▶ | if (length ref $v) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 176 | 45 | ▶ | if (defined blessed $v) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 177 | 27 | ▶ | if ($v->isa("PXML::Element")) { | ||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||
| 178 | 2 | $v->text | |||||||||||||||||||
| 179 | } elsif ($v->isa("FP::Abstract::Sequence")) { | ||||||||||||||||||||
| 180 | 13 | join("", map { _text($_) } $v->values) | |||||||||||||||||||
| 181 | } elsif (is_promise $v) { | ||||||||||||||||||||
| 182 | 12 | _text(force $v); | |||||||||||||||||||
| 183 | } else { | ||||||||||||||||||||
| 184 | 0 | die "don't know how to get text of: $v"; | |||||||||||||||||||
| 185 | } | ||||||||||||||||||||
| 186 | } else { | ||||||||||||||||||||
| 187 | 18 | ▶ | if (UNIVERSAL::isa($v, "ARRAY")) { | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 188 | 18 | join("", map { _text($_) } @$v); | |||||||||||||||||||
| 189 | } elsif (UNIVERSAL::isa($v, "CODE")) { | ||||||||||||||||||||
| 190 | |||||||||||||||||||||
| 191 | # correct? XX why does A(string_to_stream("You're | ||||||||||||||||||||
| 192 | # great."))->text trigger this case? | ||||||||||||||||||||
| 193 | 0 | _text(&$v()); | |||||||||||||||||||
| 194 | } else { | ||||||||||||||||||||
| 195 | 0 | die "don't know how to get text of: $v"; | |||||||||||||||||||
| 196 | } | ||||||||||||||||||||
| 197 | } | ||||||||||||||||||||
| 198 | } else { | ||||||||||||||||||||
| 199 | 32 | $v | |||||||||||||||||||
| 200 | } | ||||||||||||||||||||
| 201 | } else { | ||||||||||||||||||||
| 202 | 1 | "" | |||||||||||||||||||
| 203 | } | ||||||||||||||||||||
| 204 | } | ||||||||||||||||||||
| 205 | |||||||||||||||||||||
| 206 | 18 | ▶ | sub text { | ||||||||||||||||||
|
Subroutine
text: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 207 | 18 | my $s = shift; | |||||||||||||||||||
| 208 | 18 | _text($s->body) | |||||||||||||||||||
| 209 | } | ||||||||||||||||||||
| 210 | |||||||||||||||||||||
| 211 | # only for debugging? Doesn't emit XML/XHTML prologues! Also, ugly | ||||||||||||||||||||
| 212 | # monkey-access to PXML::Serialize. Circular dependency, too. | ||||||||||||||||||||
| 213 | |||||||||||||||||||||
| 214 | 44 | ▶ | sub string { | ||||||||||||||||||
|
Subroutine
string: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 215 | 44 | my $s = shift; | |||||||||||||||||||
| 216 | 44 | require PXML::Serialize; | |||||||||||||||||||
| 217 | 44 | ▶ | capture_stdout { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 218 | 44 | PXML::Serialize::pxml_print_fragment_fast($s, *STDOUT); | |||||||||||||||||||
| 219 | } | ||||||||||||||||||||
| 220 | 44 | } | |||||||||||||||||||
| 221 | |||||||||||||||||||||
| 222 | # XML does not distinguish between void elements and non-void ones in | ||||||||||||||||||||
| 223 | # its syntactical representation; whether an element is printed in | ||||||||||||||||||||
| 224 | # self-closing representation is orthogonal and can rely simply on | ||||||||||||||||||||
| 225 | # whether the content of the particular element ('at runtime') is | ||||||||||||||||||||
| 226 | # empty. | ||||||||||||||||||||
| 227 | |||||||||||||||||||||
| 228 | 4 | ▶ | sub require_printing_nonvoid_elements_nonselfreferential { | ||||||||||||||||||
|
Subroutine
require_printing_nonvoid_elements_nonselfreferential: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 229 | 4 | 0 | |||||||||||||||||||
| 230 | } | ||||||||||||||||||||
| 231 | |||||||||||||||||||||
| 232 | #sub void_element_h { | ||||||||||||||||||||
| 233 | # undef | ||||||||||||||||||||
| 234 | #} | ||||||||||||||||||||
| 235 | |||||||||||||||||||||
| 236 | 8 | _END_ | |||||||||||||||||||