| 1 | # /=====================================================================\ # | ||||||||||||||||||||||||||||
| 2 | # | LaTeXML::Common::XML | # | ||||||||||||||||||||||||||||
| 3 | # | XML representation common to LaTeXML & Post | # | ||||||||||||||||||||||||||||
| 4 | # |=====================================================================| # | ||||||||||||||||||||||||||||
| 5 | # | Part of LaTeXML: | # | ||||||||||||||||||||||||||||
| 6 | # | Public domain software, produced as part of work done by the | # | ||||||||||||||||||||||||||||
| 7 | # | United States Government & not subject to copyright in the US. | # | ||||||||||||||||||||||||||||
| 8 | # |---------------------------------------------------------------------| # | ||||||||||||||||||||||||||||
| 9 | # | Bruce Miller <bruce.miller@nist.gov> #_# | # | ||||||||||||||||||||||||||||
| 10 | # | http://dlmf.nist.gov/LaTeXML/ (o o) | # | ||||||||||||||||||||||||||||
| 11 | # \=========================================================ooo==U==ooo=/ # | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | ###################################################################### | ||||||||||||||||||||||||||||
| 14 | # This is (the beginnings of) a common interface to XML, | ||||||||||||||||||||||||||||
| 15 | # specifically XML::LibXML, used in LaTeXML and also Post processing. | ||||||||||||||||||||||||||||
| 16 | # Collecting this here will hopefully allow us to | ||||||||||||||||||||||||||||
| 17 | # * (eventually) make useful extensions to the DOM api. | ||||||||||||||||||||||||||||
| 18 | # * hide any version specific patches that have become necessary | ||||||||||||||||||||||||||||
| 19 | # Convenience Utilities to simplify using XML::LibXML | ||||||||||||||||||||||||||||
| 20 | # | ||||||||||||||||||||||||||||
| 21 | #====================================================================== | ||||||||||||||||||||||||||||
| 22 | # An eventual possibility which would be to wrap all XML::LibXML objects | ||||||||||||||||||||||||||||
| 23 | # in our own classes. This would give a cleaner way to extend the API, | ||||||||||||||||||||||||||||
| 24 | # [the extensions _should_ be methods, not random exported functions!!!] | ||||||||||||||||||||||||||||
| 25 | # and also to implement patches [currently kinda worrisome]. | ||||||||||||||||||||||||||||
| 26 | # | ||||||||||||||||||||||||||||
| 27 | # However, it would require some clumsy (& probably expensive) | ||||||||||||||||||||||||||||
| 28 | # re-blessing or wrapping of all # common LibXML accessors | ||||||||||||||||||||||||||||
| 29 | # [ie. nodeChildren would need to convert all children to the new type]. | ||||||||||||||||||||||||||||
| 30 | # | ||||||||||||||||||||||||||||
| 31 | ###################################################################### | ||||||||||||||||||||||||||||
| 32 | # One concern is to clone any nodes ..... | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | package LaTeXML::Common::XML; | ||||||||||||||||||||||||||||
| 35 | 33 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 36 | 33 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 37 | 33 | ▶ | use XML::LibXML qw(:all); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 38 | 33 | ▶ | use XML::LibXML::XPathContext; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 39 | 33 | ▶ | use LaTeXML::Util::Pathname; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 40 | 33 | ▶ | use Encode; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 41 | 33 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 42 | # ? | ||||||||||||||||||||||||||||
| 43 | require LaTeXML::Common::XML::Parser; | ||||||||||||||||||||||||||||
| 44 | require LaTeXML::Common::XML::XPath; | ||||||||||||||||||||||||||||
| 45 | require LaTeXML::Common::XML::XSLT; | ||||||||||||||||||||||||||||
| 46 | require LaTeXML::Common::XML::RelaxNG; | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | # we're too low-level to use LaTeXML's error handling, but at least use Carp....(?) | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 33 | ▶ | use base qw(Exporter); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 51 | our @EXPORT = ( | ||||||||||||||||||||||||||||
| 52 | # Export just these symbols from XML::LibXML | ||||||||||||||||||||||||||||
| 53 | # Possibly (if/when we abstract away from XML::LibXML), we should be selective? | ||||||||||||||||||||||||||||
| 54 | qw( XML_ELEMENT_NODE | ||||||||||||||||||||||||||||
| 55 | XML_ATTRIBUTE_NODE | ||||||||||||||||||||||||||||
| 56 | XML_TEXT_NODE | ||||||||||||||||||||||||||||
| 57 | XML_CDATA_SECTION_NODE | ||||||||||||||||||||||||||||
| 58 | XML_ENTITY_REF_NODE | ||||||||||||||||||||||||||||
| 59 | XML_ENTITY_NODE | ||||||||||||||||||||||||||||
| 60 | XML_PI_NODE | ||||||||||||||||||||||||||||
| 61 | XML_COMMENT_NODE | ||||||||||||||||||||||||||||
| 62 | XML_DOCUMENT_NODE | ||||||||||||||||||||||||||||
| 63 | XML_DOCUMENT_TYPE_NODE | ||||||||||||||||||||||||||||
| 64 | XML_DOCUMENT_FRAG_NODE | ||||||||||||||||||||||||||||
| 65 | XML_NOTATION_NODE | ||||||||||||||||||||||||||||
| 66 | XML_HTML_DOCUMENT_NODE | ||||||||||||||||||||||||||||
| 67 | XML_DTD_NODE | ||||||||||||||||||||||||||||
| 68 | XML_ELEMENT_DECL | ||||||||||||||||||||||||||||
| 69 | XML_ATTRIBUTE_DECL | ||||||||||||||||||||||||||||
| 70 | XML_ENTITY_DECL | ||||||||||||||||||||||||||||
| 71 | XML_NAMESPACE_DECL | ||||||||||||||||||||||||||||
| 72 | XML_XINCLUDE_END | ||||||||||||||||||||||||||||
| 73 | XML_XINCLUDE_START | ||||||||||||||||||||||||||||
| 74 | encodeToUTF8 | ||||||||||||||||||||||||||||
| 75 | decodeFromUTF8 ), | ||||||||||||||||||||||||||||
| 76 | @XML::LibXML::EXPORT, | ||||||||||||||||||||||||||||
| 77 | # Possibly (later) export these utility functions | ||||||||||||||||||||||||||||
| 78 | qw(&element_nodes &text_in_node &new_node &element_next &element_prev | ||||||||||||||||||||||||||||
| 79 | &append_nodes &clear_node &maybe_clone | ||||||||||||||||||||||||||||
| 80 | &valid_attributes ©_attributes &rename_attribute &remove_attr | ||||||||||||||||||||||||||||
| 81 | &get_attr &isTextNode &isElementNode &isChild &isDescendant &isDescendantOrSelf | ||||||||||||||||||||||||||||
| 82 | &set_RDFa_prefixes | ||||||||||||||||||||||||||||
| 83 | &initialize_catalogs) | ||||||||||||||||||||||||||||
| 84 | ); | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | # These really should be constant, but visible outside! | ||||||||||||||||||||||||||||
| 87 | our $XMLNS_NS = 'http://www.w3.org/2000/xmlns/'; # [CONSTANT] | ||||||||||||||||||||||||||||
| 88 | our $XML_NS = 'http://www.w3.org/XML/1998/namespace'; # [CONSTANT] | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | #====================================================================== | ||||||||||||||||||||||||||||
| 91 | # XML Utilities | ||||||||||||||||||||||||||||
| 92 | 0 | ▶ | sub element_nodes { | ||||||||||||||||||||||||||
|
Subroutine
element_nodes: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 93 | 0 | my ($node) = @_; | |||||||||||||||||||||||||||
| 94 | 0 | ▶ | return ($node ? grep { $_->nodeType == XML_ELEMENT_NODE } $node->childNodes : ()); } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 0 | ▶ | sub text_in_node { | ||||||||||||||||||||||||||
|
Subroutine
text_in_node: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 97 | 0 | my ($node) = @_; | |||||||||||||||||||||||||||
| 98 | 0 | ▶ | return ($node ? join("\n", map { $_->data } grep { $_->nodeType == XML_TEXT_NODE } $node->childNodes) : ''); } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 0 | ▶ | sub element_next { | ||||||||||||||||||||||||||
|
Subroutine
element_next: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 101 | 0 | my ($node) = @_; | |||||||||||||||||||||||||||
| 102 | 0 | my $next; | |||||||||||||||||||||||||||
| 103 | 0 | ▶ | while (($next = $node->nextSibling) && ($next->nodeType != XML_ELEMENT_NODE)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$next = $node->nextSibling and $next->nodeType != 1
MC/DC0%0/2
$next = $node->nextSibling and $next->nodeType != 1
$next = $node->nextSibling
$next->nodeType != 1
Truth table0%0/3
A and B
A= $next = $node->nextSiblingB= $next->nodeType != 1
| |||||||||||||||||||||||||||||
| 104 | 0 | $node = $next; } | |||||||||||||||||||||||||||
| 105 | 0 | return $next; } | |||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 0 | ▶ | sub element_prev { | ||||||||||||||||||||||||||
|
Subroutine
element_prev: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 108 | 0 | my ($node) = @_; | |||||||||||||||||||||||||||
| 109 | 0 | my $prev; | |||||||||||||||||||||||||||
| 110 | 0 | ▶ | while (($prev = $node->previousSibling) && ($prev->nodeType != XML_ELEMENT_NODE)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$prev = $node->previousSibling and $prev->nodeType != 1
MC/DC0%0/2
$prev = $node->previousSibling and $prev->nodeType != 1
$prev = $node->previousSibling
$prev->nodeType != 1
Truth table0%0/3
A and B
A= $prev = $node->previousSiblingB= $prev->nodeType != 1
| |||||||||||||||||||||||||||||
| 111 | 0 | $node = $prev; } | |||||||||||||||||||||||||||
| 112 | 0 | return $prev; } | |||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 0 | ▶ | sub isTextNode { | ||||||||||||||||||||||||||
|
Subroutine
isTextNode: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 115 | 0 | my ($node) = @_; | |||||||||||||||||||||||||||
| 116 | 0 | ▶ | return ($node ? $node->nodeType == XML_TEXT_NODE : 0); } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 0 | ▶ | sub isElementNode { | ||||||||||||||||||||||||||
|
Subroutine
isElementNode: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 119 | 0 | my ($node) = @_; | |||||||||||||||||||||||||||
| 120 | 0 | ▶ | return ($node ? $node->nodeType == XML_ELEMENT_NODE : 0); } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | # Is $child a child of $parent? | ||||||||||||||||||||||||||||
| 123 | 0 | ▶ | sub isChild { | ||||||||||||||||||||||||||
|
Subroutine
isChild: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 124 | 0 | my ($child, $parent) = @_; | |||||||||||||||||||||||||||
| 125 | 0 | ▶ | my $p = $child && $child->parentNode; | ||||||||||||||||||||||||||
|
Condition0%0/3
$child && $child->parentNode
MC/DC0%0/2
$child && $child->parentNode
$child
$child->parentNode
Truth table0%0/3
A && B
A= $childB= $child->parentNode
| |||||||||||||||||||||||||||||
| 126 | 0 | ▶ | return 1 if $p && $p->isSameNode($parent); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$p and $p->isSameNode($parent)
MC/DC0%0/2
$p and $p->isSameNode($parent)
$p
$p->isSameNode($parent)
Truth table0%0/3
A and B
A= $pB= $p->isSameNode($parent)
| |||||||||||||||||||||||||||||
| 127 | 0 | return 0; } | |||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | # Is $child a descendant of $parent? | ||||||||||||||||||||||||||||
| 130 | 0 | ▶ | sub isDescendant { | ||||||||||||||||||||||||||
|
Subroutine
isDescendant: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 131 | 0 | my ($child, $parent) = @_; | |||||||||||||||||||||||||||
| 132 | 0 | ▶ | my $p = $child && $child->parentNode; | ||||||||||||||||||||||||||
|
Condition0%0/3
$child && $child->parentNode
MC/DC0%0/2
$child && $child->parentNode
$child
$child->parentNode
Truth table0%0/3
A && B
A= $childB= $child->parentNode
| |||||||||||||||||||||||||||||
| 133 | 0 | ▶ | while ($p) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 134 | 0 | ▶ | return 1 if $p->isSameNode($parent); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 135 | 0 | $p = $p->parentNode; } | |||||||||||||||||||||||||||
| 136 | 0 | return 0; } | |||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | # Is $child the same as $parent, or a descendent of $parent? | ||||||||||||||||||||||||||||
| 139 | 0 | ▶ | sub isDescendantOrSelf { | ||||||||||||||||||||||||||
|
Subroutine
isDescendantOrSelf: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 140 | 0 | my ($child, $parent) = @_; | |||||||||||||||||||||||||||
| 141 | 0 | my $p = $child; | |||||||||||||||||||||||||||
| 142 | 0 | ▶ | while ($p) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 143 | 0 | ▶ | return 1 if $p->isSameNode($parent); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 144 | 0 | $p = $p->parentNode; } | |||||||||||||||||||||||||||
| 145 | 0 | return 0; } | |||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 0 | ▶ | sub new_node { | ||||||||||||||||||||||||||
|
Subroutine
new_node: not called CC=6 SCAR=37.4
| |||||||||||||||||||||||||||||
| 148 | 0 | my ($nsURI, $tag, $children, %attributes) = @_; | |||||||||||||||||||||||||||
| 149 | # print "\n\n\nnsURI: $nsURI, tag: $tag, children: $children\n"; | ||||||||||||||||||||||||||||
| 150 | 0 | my ($nspre, $rawtag) = (undef, $tag); | |||||||||||||||||||||||||||
| 151 | 0 | ▶ | if ($tag =~ /^(\w+):(.*)$/) { ($nspre, $rawtag) = ($1, $2 || $tag); } | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$2 || $tag
MC/DC0%0/2
$2 || $tag
$2
$tag
Truth table0%0/3
A || B
A= $2B= $tag
| |||||||||||||||||||||||||||||
| 152 | 0 | my $node = XML::LibXML::Element->new($rawtag); | |||||||||||||||||||||||||||
| 153 | # my $node=$LaTeXML::Post::DOC->createElement($tag); | ||||||||||||||||||||||||||||
| 154 | # my $node=$LaTeXML::Post::DOC->createElementNS($nsURI,$tag); | ||||||||||||||||||||||||||||
| 155 | 0 | ▶ | if ($nspre) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 156 | 0 | $node->setNamespace($nsURI, $nspre, 1); } | |||||||||||||||||||||||||||
| 157 | else { | ||||||||||||||||||||||||||||
| 158 | 0 | $node->setNamespace($nsURI); } | |||||||||||||||||||||||||||
| 159 | 0 | append_nodes($node, $children); | |||||||||||||||||||||||||||
| 160 | 0 | foreach my $key (sort keys %attributes) { | |||||||||||||||||||||||||||
| 161 | 0 | ▶ | $node->setAttribute($key, $attributes{$key}) if defined $attributes{$key}; } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 162 | 0 | return $node; } | |||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | # Append the given nodes (which might also be array ref's of nodes, or even strings) | ||||||||||||||||||||||||||||
| 165 | # to $node. This takes care to clone any node that already has a parent. | ||||||||||||||||||||||||||||
| 166 | 0 | ▶ | sub append_nodes { | ||||||||||||||||||||||||||
|
Subroutine
append_nodes: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||
| 167 | 0 | my ($node, @children) = @_; | |||||||||||||||||||||||||||
| 168 | 0 | foreach my $child (@children) { | |||||||||||||||||||||||||||
| 169 | 0 | ▶ | if (ref $child eq 'ARRAY') { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 170 | 0 | append_nodes($node, @$child); } | |||||||||||||||||||||||||||
| 171 | elsif (ref $child) { #eq 'XML::LibXML::Element'){ | ||||||||||||||||||||||||||||
| 172 | 0 | $node->appendChild(maybe_clone($child)); } | |||||||||||||||||||||||||||
| 173 | elsif (defined $child) { | ||||||||||||||||||||||||||||
| 174 | 0 | $node->appendText($child); } } | |||||||||||||||||||||||||||
| 175 | 0 | return $node; } | |||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | 0 | ▶ | sub clear_node { | ||||||||||||||||||||||||||
|
Subroutine
clear_node: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 178 | 0 | my ($node) = @_; | |||||||||||||||||||||||||||
| 179 | 0 | return map { $node->removeChild($_) } | |||||||||||||||||||||||||||
| 180 | 0 | ▶ | grep { ($_->nodeType == XML_ELEMENT_NODE) || ($_->nodeType == XML_TEXT_NODE) } | ||||||||||||||||||||||||||
|
Condition0%0/3
$_->nodeType == 1 || $_->nodeType == 3
MC/DC0%0/2
$_->nodeType == 1 || $_->nodeType == 3
$_->nodeType == 1
$_->nodeType == 3
Truth table0%0/3
A || B
A= $_->nodeType == 1B= $_->nodeType == 3
| |||||||||||||||||||||||||||||
| 181 | $node->childNodes; } | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | # We have to be _extremely_ careful when rearranging trees when using XML::LibXML!!! | ||||||||||||||||||||||||||||
| 184 | # If we add one node to another, it is _silently_ removed from it's previous | ||||||||||||||||||||||||||||
| 185 | # parent, if any! | ||||||||||||||||||||||||||||
| 186 | # Hopefully, this test is sufficient? | ||||||||||||||||||||||||||||
| 187 | 0 | ▶ | sub maybe_clone { | ||||||||||||||||||||||||||
|
Subroutine
maybe_clone: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 188 | 0 | my ($node) = @_; | |||||||||||||||||||||||||||
| 189 | 0 | ▶ | return ($node->parentNode ? $node->cloneNode(1) : $node); } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | # the attributes list may contain undefined values | ||||||||||||||||||||||||||||
| 192 | # and attributes with no name (?) | ||||||||||||||||||||||||||||
| 193 | 0 | ▶ | sub valid_attributes { | ||||||||||||||||||||||||||
|
Subroutine
valid_attributes: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 194 | 0 | my ($node) = @_; | |||||||||||||||||||||||||||
| 195 | 0 | ▶ | return grep { $_ && $_->getName } $node->attributes; } | ||||||||||||||||||||||||||
|
Condition0%0/3
$_ && $_->getName
MC/DC0%0/2
$_ && $_->getName
$_
$_->getName
Truth table0%0/3
A && B
A= $_B= $_->getName
| |||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | # copy @attr attributes from $from to $to | ||||||||||||||||||||||||||||
| 198 | 0 | ▶ | sub copy_attributes { | ||||||||||||||||||||||||||
|
Subroutine
copy_attributes: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 199 | 0 | my ($to, $from) = @_; | |||||||||||||||||||||||||||
| 200 | 0 | foreach my $attr ($from->attributes) { | |||||||||||||||||||||||||||
| 201 | 0 | my $key = $attr->getName; | |||||||||||||||||||||||||||
| 202 | 0 | $to->setAttribute($key, $from->getAttribute($key)); } | |||||||||||||||||||||||||||
| 203 | 0 | return; } | |||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | 0 | ▶ | sub rename_attribute { | ||||||||||||||||||||||||||
|
Subroutine
rename_attribute: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 206 | 0 | my ($node, $from, $to) = @_; | |||||||||||||||||||||||||||
| 207 | 0 | $node->setAttribute($to, $node->getAttribute($from)); | |||||||||||||||||||||||||||
| 208 | 0 | $node->removeAttribute($from); | |||||||||||||||||||||||||||
| 209 | 0 | return; } | |||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | 0 | ▶ | sub remove_attr { | ||||||||||||||||||||||||||
|
Subroutine
remove_attr: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 212 | 0 | my ($node, @attr) = @_; | |||||||||||||||||||||||||||
| 213 | 0 | map { $node->removeAttribute($_) } @attr; | |||||||||||||||||||||||||||
| 214 | 0 | return; } | |||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | 0 | ▶ | sub get_attr { | ||||||||||||||||||||||||||
|
Subroutine
get_attr: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 217 | 0 | my ($node, @attr) = @_; | |||||||||||||||||||||||||||
| 218 | 0 | return map { $node->getAttribute($_) } @attr; } | |||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | # NOTE: This really should be part of some top-level 'common' initialization | ||||||||||||||||||||||||||||
| 221 | # and probably should accommodate catalogs being given as configuration options! | ||||||||||||||||||||||||||||
| 222 | # However, it presumably sets some global state in XML::LibXML, | ||||||||||||||||||||||||||||
| 223 | # so it's safe to do ( record! ) once, even across Daemon calls. | ||||||||||||||||||||||||||||
| 224 | my $catalogs_initialized = 0; # [CONFIGURATION] | ||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||
| 226 | 0 | ▶ | sub initialize_catalogs { | ||||||||||||||||||||||||||
|
Subroutine
initialize_catalogs: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 227 | 0 | ▶ | return if $catalogs_initialized; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 228 | 0 | $catalogs_initialized = 1; | |||||||||||||||||||||||||||
| 229 | 0 | foreach my $catalog (pathname_findall('LaTeXML.catalog', installation_subdir => '.')) { | |||||||||||||||||||||||||||
| 230 | 0 | XML::LibXML->load_catalog($catalog); } | |||||||||||||||||||||||||||
| 231 | 0 | return; } | |||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||
| 233 | #====================================================================== | ||||||||||||||||||||||||||||
| 234 | # Odd place for this utility, but it is needed in both conversion & post | ||||||||||||||||||||||||||||
| 235 | # ALSO needs error reporting capability. | ||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||
| 237 | my @RDF_TERM_ATTRIBUTES = ( # [CONSTANT] | ||||||||||||||||||||||||||||
| 238 | qw(about resource property typeof rel rev datatype)); | ||||||||||||||||||||||||||||
| 239 | my %NON_RDF_PREFIXES = map { ($_ => 1) } qw(http https ftp); # [CONSTANT] | ||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||
| 241 | 0 | ▶ | sub set_RDFa_prefixes { | ||||||||||||||||||||||||||
|
Subroutine
set_RDFa_prefixes: not called CC=17 SCAR=57.2
| |||||||||||||||||||||||||||||
| 242 | 0 | my ($document, $map) = @_; | |||||||||||||||||||||||||||
| 243 | 0 | my $root = $document->documentElement; | |||||||||||||||||||||||||||
| 244 | 0 | my %prefixes = (); | |||||||||||||||||||||||||||
| 245 | 0 | my %localmap = map { ($_ => $$map{$_}) } keys %$map; | |||||||||||||||||||||||||||
| 246 | 0 | ▶ | if (my $prefixes = $root->getAttribute('prefix')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 247 | 0 | my @x = split(/\s/, $prefixes); | |||||||||||||||||||||||||||
| 248 | 0 | ▶ | while (@x) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 249 | 0 | my ($prefix, $uri) = (shift(@x), shift(@x)); | |||||||||||||||||||||||||||
| 250 | 0 | $prefix =~ s/:$//; | |||||||||||||||||||||||||||
| 251 | 0 | $prefixes{$prefix} = 1; | |||||||||||||||||||||||||||
| 252 | 0 | ▶ | if (!$localmap{$prefix}) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 253 | 0 | $localmap{$prefix} = $uri; } | |||||||||||||||||||||||||||
| 254 | elsif ($localmap{$prefix} ne $uri) { | ||||||||||||||||||||||||||||
| 255 | 0 | carp "Clash of RDFa prefix '$prefix' ('$uri' vs '$localmap{$prefix}'); " | |||||||||||||||||||||||||||
| 256 | . "Skipping RDFa prefix management"; | ||||||||||||||||||||||||||||
| 257 | 0 | return; } } } | |||||||||||||||||||||||||||
| 258 | 0 | ▶ | if (my @n = $document->findnodes('descendant::*[@prefix]')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 259 | 0 | ▶ | if ((scalar(@n) > 1) || !$root->isSameNode($n[0])) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
scalar @n > 1 or not $root->isSameNode($n[0])
MC/DC0%0/2
scalar @n > 1 or not $root->isSameNode($n[0])
scalar @n > 1
not $root->isSameNode($n[0])
Truth table0%0/3
A or not(B)
A= scalar @n > 1B= not $root->isSameNode($n[0])
| |||||||||||||||||||||||||||||
| 260 | 0 | carp "RDFa attribute 'prefix' on non-root node; " | |||||||||||||||||||||||||||
| 261 | . "Skipping RDFa prefix management"; | ||||||||||||||||||||||||||||
| 262 | 0 | return; } } | |||||||||||||||||||||||||||
| 263 | 0 | ▶ | if (my @n = $document->findnodes('descendant::*[@vocab]')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 264 | 0 | carp "RDFa attribute 'vocab' on non-root node; " | |||||||||||||||||||||||||||
| 265 | . "Skipping RDFa prefix management"; | ||||||||||||||||||||||||||||
| 266 | 0 | return; } | |||||||||||||||||||||||||||
| 267 | 0 | my $xpath = 'descendant::*[' . join(' or ', map { '@' . $_ } @RDF_TERM_ATTRIBUTES) . ']'; | |||||||||||||||||||||||||||
| 268 | 0 | foreach my $node ($document->findnodes($xpath)) { | |||||||||||||||||||||||||||
| 269 | 0 | foreach my $k (@RDF_TERM_ATTRIBUTES) { | |||||||||||||||||||||||||||
| 270 | 0 | ▶ | if (my $v = $node->getAttribute($k)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 271 | 0 | foreach my $term (split(/\s/, $v)) { | |||||||||||||||||||||||||||
| 272 | 0 | ▶ | if (($term =~ /^(\w+):/) && !$NON_RDF_PREFIXES{$1}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$term =~ /^(\w+):/ and not $NON_RDF_PREFIXES{$1}
MC/DC0%0/2
$term =~ /^(\w+):/ and not $NON_RDF_PREFIXES{$1}
$term =~ /^(\w+):/
not $NON_RDF_PREFIXES{$1}
Truth table0%0/3
A and not(B)
A= $term =~ /^(\w+):/B= not $NON_RDF_PREFIXES{$1}
| |||||||||||||||||||||||||||||
| 273 | 0 | ▶ | $prefixes{$1} = 1 if $localmap{$1}; } } } } } # A prefix is a prefix IFF there is a mapping!! | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 274 | 0 | ▶ | if (my $prefixes = join(' ', map { $_ . ": " . $localmap{$_} } sort keys %prefixes)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 275 | 0 | $root->setAttribute(prefix => $prefixes); } | |||||||||||||||||||||||||||
| 276 | 0 | return; } | |||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||
| 278 | ###################################################################### | ||||||||||||||||||||||||||||
| 279 | # PATCH Section | ||||||||||||||||||||||||||||
| 280 | ###################################################################### | ||||||||||||||||||||||||||||
| 281 | # Various versions of XML::LibXML have introduced incompatable improvements | ||||||||||||||||||||||||||||
| 282 | # We can run using older versions, but have to patch things up to | ||||||||||||||||||||||||||||
| 283 | # a consistent level. | ||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||
| 285 | our $original_XML_LibXML_Document_toString; # [CONFIGURATION] | ||||||||||||||||||||||||||||
| 286 | our $original_XML_LibXML_Element_getAttribute; # [CONFIGURATION] | ||||||||||||||||||||||||||||
| 287 | our $original_XML_LibXML_Element_hasAttribute; # [CONFIGURATION] | ||||||||||||||||||||||||||||
| 288 | our $original_XML_LibXML_Element_setAttribute; # [CONFIGURATION] | ||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||
| 290 | 33 | ▶ | BEGIN { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 291 | 33 | *original_XML_LibXML_Document_toString = *XML::LibXML::Document::toString; | |||||||||||||||||||||||||||
| 292 | 33 | *original_XML_LibXML_Element_getAttribute = *XML::LibXML::Element::getAttribute; | |||||||||||||||||||||||||||
| 293 | 33 | *original_XML_LibXML_Element_hasAttribute = *XML::LibXML::Element::hasAttribute; | |||||||||||||||||||||||||||
| 294 | 33 | *original_XML_LibXML_Element_setAttribute = *XML::LibXML::Element::setAttribute; | |||||||||||||||||||||||||||
| 295 | } | ||||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||
| 297 | # As of 1.63, LibXML converts a document "to String" as bytes, not characters (?) | ||||||||||||||||||||||||||||
| 298 | 0 | ▶ | sub encoding_XML_LibXML_Document_toString { | ||||||||||||||||||||||||||
|
Subroutine
encoding_XML_LibXML_Document_toString: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 299 | 0 | my ($self, $depth) = @_; | |||||||||||||||||||||||||||
| 300 | # Encode::encode("utf-8", $self->original_XML_LibXML_Document_toString($depth)); } | ||||||||||||||||||||||||||||
| 301 | 0 | return Encode::encode("utf-8", original_XML_LibXML_Document_toString($self, $depth)); } | |||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||
| 303 | # As of 1.59, element attribute methods accept attributes names as "xml:foo" | ||||||||||||||||||||||||||||
| 304 | # (in particular, xml:id), without explicitly calling the NS versions. | ||||||||||||||||||||||||||||
| 305 | # The new form is considerably more convenient. | ||||||||||||||||||||||||||||
| 306 | 0 | ▶ | sub xmlns_XML_LibXML_Element_getAttribute { | ||||||||||||||||||||||||||
|
Subroutine
xmlns_XML_LibXML_Element_getAttribute: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 307 | 0 | my ($self, $name) = @_; | |||||||||||||||||||||||||||
| 308 | 0 | ▶ | if ($name =~ /^xml:(.*)$/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 309 | 0 | my $attr = $1; | |||||||||||||||||||||||||||
| 310 | 0 | return $self->getAttributeNS($LaTeXML::Common::XML::XML_NS, $attr); } | |||||||||||||||||||||||||||
| 311 | else { | ||||||||||||||||||||||||||||
| 312 | 0 | return original_XML_LibXML_Element_getAttribute($self, $name); } } | |||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||
| 314 | 0 | ▶ | sub xmlns_XML_LibXML_Element_hasAttribute { | ||||||||||||||||||||||||||
|
Subroutine
xmlns_XML_LibXML_Element_hasAttribute: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 315 | 0 | my ($self, $name) = @_; | |||||||||||||||||||||||||||
| 316 | 0 | ▶ | if ($name =~ /^xml:(.*)$/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 317 | 0 | my $attr = $1; | |||||||||||||||||||||||||||
| 318 | 0 | return $self->hasAttributeNS($LaTeXML::Common::XML::XML_NS, $attr); } | |||||||||||||||||||||||||||
| 319 | else { | ||||||||||||||||||||||||||||
| 320 | 0 | return original_XML_LibXML_Element_hasAttribute($self, $name); } } | |||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||
| 322 | 0 | ▶ | sub xmlns_XML_LibXML_Element_setAttribute { | ||||||||||||||||||||||||||
|
Subroutine
xmlns_XML_LibXML_Element_setAttribute: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 323 | 0 | my ($self, $name, $value) = @_; | |||||||||||||||||||||||||||
| 324 | 0 | ▶ | if ($name =~ /^xml:(.*)$/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 325 | 0 | my $attr = $1; | |||||||||||||||||||||||||||
| 326 | 0 | return $self->setAttributeNS($LaTeXML::Common::XML::XML_NS, $attr, $value); } | |||||||||||||||||||||||||||
| 327 | else { | ||||||||||||||||||||||||||||
| 328 | 0 | return original_XML_LibXML_Element_setAttribute($self, $name, $value); } } | |||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||
| 330 | our $xml_libxml_version; # [CONFIGURATION] | ||||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||
| 332 | 33 | ▶ | BEGIN { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=3 SCAR=14.2
| |||||||||||||||||||||||||||||
| 333 | 33 | $xml_libxml_version = $XML::LibXML::VERSION; | |||||||||||||||||||||||||||
| 334 | 33 | $xml_libxml_version =~ s/_\d+$//; | |||||||||||||||||||||||||||
| 335 | 33 | ▶ | if ($xml_libxml_version < 1.63) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 336 | 0 | *XML::LibXML::Document::toString = *encoding_XML_LibXML_Document_toString; } | |||||||||||||||||||||||||||
| 337 | 33 | ▶ | if ($xml_libxml_version < 1.59) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 338 | 0 | *XML::LibXML::Element::getAttribute = *xmlns_XML_LibXML_Element_getAttribute; | |||||||||||||||||||||||||||
| 339 | 0 | *XML::LibXML::Element::hasAttribute = *xmlns_XML_LibXML_Element_hasAttribute; | |||||||||||||||||||||||||||
| 340 | 0 | *XML::LibXML::Element::setAttribute = *xmlns_XML_LibXML_Element_setAttribute; } | |||||||||||||||||||||||||||
| 341 | } | ||||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||
| 343 | #====================================================================== | ||||||||||||||||||||||||||||
| 344 | 1; | ||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||
| 346 | __END__ | ||||||||||||||||||||||||||||