| 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 | 29 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 36 | 29 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 37 | 29 | ▶ | use XML::LibXML qw(:all); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 38 | 29 | ▶ | use XML::LibXML::XPathContext; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 39 | 29 | ▶ | use LaTeXML::Util::Pathname; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 40 | 29 | ▶ | use Encode; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 41 | 29 | ▶ | 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 | 29 | ▶ | 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 | ||||||||||||||||||||||||||||
| 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 | sub element_nodes { | ||||||||||||||||||||||||||||
| 93 | 0 | ▶ | my ($node) = @_; | ||||||||||||||||||||||||||
|
Subroutine
element_nodes: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 94 | 0 | return grep { $_->nodeType == XML_ELEMENT_NODE } $node->childNodes; } | |||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | sub text_in_node { | ||||||||||||||||||||||||||||
| 97 | 0 | ▶ | my ($node) = @_; | ||||||||||||||||||||||||||
|
Subroutine
text_in_node: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 98 | 0 | return join("\n", map { $_->data } grep { $_->nodeType == XML_TEXT_NODE } $node->childNodes); } | |||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | sub isTextNode { | ||||||||||||||||||||||||||||
| 101 | 0 | ▶ | my ($node) = @_; | ||||||||||||||||||||||||||
|
Subroutine
isTextNode: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 102 | 0 | return $node->nodeType == XML_TEXT_NODE; } | |||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | sub isElementNode { | ||||||||||||||||||||||||||||
| 105 | 0 | ▶ | my ($node) = @_; | ||||||||||||||||||||||||||
|
Subroutine
isElementNode: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 106 | 0 | return $node->nodeType == XML_ELEMENT_NODE; } | |||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | # Is $child a child of $parent? | ||||||||||||||||||||||||||||
| 109 | sub isChild { | ||||||||||||||||||||||||||||
| 110 | 0 | ▶ | my ($child, $parent) = @_; | ||||||||||||||||||||||||||
|
Subroutine
isChild: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 111 | 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
| |||||||||||||||||||||||||||||
| 112 | 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)
| |||||||||||||||||||||||||||||
| 113 | 0 | return 0; } | |||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | # Is $child a descendant of $parent? | ||||||||||||||||||||||||||||
| 116 | sub isDescendant { | ||||||||||||||||||||||||||||
| 117 | 0 | ▶ | my ($child, $parent) = @_; | ||||||||||||||||||||||||||
|
Subroutine
isDescendant: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 118 | 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
| |||||||||||||||||||||||||||||
| 119 | 0 | ▶ | while ($p) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 120 | 0 | ▶ | return 1 if $p->isSameNode($parent); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 121 | 0 | $p = $p->parentNode; } | |||||||||||||||||||||||||||
| 122 | 0 | return 0; } | |||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | # Is $child the same as $parent, or a descendent of $parent? | ||||||||||||||||||||||||||||
| 125 | sub isDescendantOrSelf { | ||||||||||||||||||||||||||||
| 126 | 0 | ▶ | my ($child, $parent) = @_; | ||||||||||||||||||||||||||
|
Subroutine
isDescendantOrSelf: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 127 | 0 | my $p = $child; | |||||||||||||||||||||||||||
| 128 | 0 | ▶ | while ($p) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 129 | 0 | ▶ | return 1 if $p->isSameNode($parent); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 130 | 0 | $p = $p->parentNode; } | |||||||||||||||||||||||||||
| 131 | 0 | return 0; } | |||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | sub new_node { | ||||||||||||||||||||||||||||
| 134 | 0 | ▶ | my ($nsURI, $tag, $children, %attributes) = @_; | ||||||||||||||||||||||||||
|
Subroutine
new_node: not called CC=6 SCAR=37.4
| |||||||||||||||||||||||||||||
| 135 | # print "\n\n\nnsURI: $nsURI, tag: $tag, children: $children\n"; | ||||||||||||||||||||||||||||
| 136 | 0 | my ($nspre, $rawtag) = (undef, $tag); | |||||||||||||||||||||||||||
| 137 | 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
| |||||||||||||||||||||||||||||
| 138 | 0 | my $node = XML::LibXML::Element->new($rawtag); | |||||||||||||||||||||||||||
| 139 | # my $node=$LaTeXML::Post::DOC->createElement($tag); | ||||||||||||||||||||||||||||
| 140 | # my $node=$LaTeXML::Post::DOC->createElementNS($nsURI,$tag); | ||||||||||||||||||||||||||||
| 141 | 0 | ▶ | if ($nspre) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 142 | 0 | $node->setNamespace($nsURI, $nspre, 1); } | |||||||||||||||||||||||||||
| 143 | else { | ||||||||||||||||||||||||||||
| 144 | 0 | $node->setNamespace($nsURI); } | |||||||||||||||||||||||||||
| 145 | 0 | append_nodes($node, $children); | |||||||||||||||||||||||||||
| 146 | 0 | foreach my $key (sort keys %attributes) { | |||||||||||||||||||||||||||
| 147 | 0 | ▶ | $node->setAttribute($key, $attributes{$key}) if defined $attributes{$key}; } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 148 | 0 | return $node; } | |||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | # Append the given nodes (which might also be array ref's of nodes, or even strings) | ||||||||||||||||||||||||||||
| 151 | # to $node. This takes care to clone any node that already has a parent. | ||||||||||||||||||||||||||||
| 152 | sub append_nodes { | ||||||||||||||||||||||||||||
| 153 | 0 | ▶ | my ($node, @children) = @_; | ||||||||||||||||||||||||||
|
Subroutine
append_nodes: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||
| 154 | 0 | foreach my $child (@children) { | |||||||||||||||||||||||||||
| 155 | 0 | ▶ | if (ref $child eq 'ARRAY') { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 156 | 0 | append_nodes($node, @$child); } | |||||||||||||||||||||||||||
| 157 | elsif (ref $child) { #eq 'XML::LibXML::Element'){ | ||||||||||||||||||||||||||||
| 158 | 0 | $node->appendChild(maybe_clone($child)); } | |||||||||||||||||||||||||||
| 159 | elsif (defined $child) { | ||||||||||||||||||||||||||||
| 160 | 0 | $node->appendText($child); } } | |||||||||||||||||||||||||||
| 161 | 0 | return $node; } | |||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | sub clear_node { | ||||||||||||||||||||||||||||
| 164 | 0 | ▶ | my ($node) = @_; | ||||||||||||||||||||||||||
|
Subroutine
clear_node: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 165 | 0 | return map { $node->removeChild($_) } | |||||||||||||||||||||||||||
| 166 | 0 | ▶ | grep { ($_->nodeType == XML_ELEMENT_NODE) || ($_->nodeType == XML_TEXT_NODE) } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 167 | $node->childNodes; } | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | # We have to be _extremely_ careful when rearranging trees when using XML::LibXML!!! | ||||||||||||||||||||||||||||
| 170 | # If we add one node to another, it is _silently_ removed from it's previous | ||||||||||||||||||||||||||||
| 171 | # parent, if any! | ||||||||||||||||||||||||||||
| 172 | # Hopefully, this test is sufficient? | ||||||||||||||||||||||||||||
| 173 | sub maybe_clone { | ||||||||||||||||||||||||||||
| 174 | 0 | ▶ | my ($node) = @_; | ||||||||||||||||||||||||||
|
Subroutine
maybe_clone: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 175 | 0 | ▶ | return ($node->parentNode ? $node->cloneNode(1) : $node); } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | # the attributes list may contain undefined values | ||||||||||||||||||||||||||||
| 178 | # and attributes with no name (?) | ||||||||||||||||||||||||||||
| 179 | sub valid_attributes { | ||||||||||||||||||||||||||||
| 180 | 0 | ▶ | my ($node) = @_; | ||||||||||||||||||||||||||
|
Subroutine
valid_attributes: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 181 | 0 | ▶ | return grep { $_ && $_->getName } $node->attributes; } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | # copy @attr attributes from $from to $to | ||||||||||||||||||||||||||||
| 184 | sub copy_attributes { | ||||||||||||||||||||||||||||
| 185 | 0 | ▶ | my ($to, $from) = @_; | ||||||||||||||||||||||||||
|
Subroutine
copy_attributes: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 186 | 0 | foreach my $attr ($from->attributes) { | |||||||||||||||||||||||||||
| 187 | 0 | my $key = $attr->getName; | |||||||||||||||||||||||||||
| 188 | 0 | $to->setAttribute($key, $from->getAttribute($key)); } | |||||||||||||||||||||||||||
| 189 | 0 | return; } | |||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | sub rename_attribute { | ||||||||||||||||||||||||||||
| 192 | 0 | ▶ | my ($node, $from, $to) = @_; | ||||||||||||||||||||||||||
|
Subroutine
rename_attribute: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 193 | 0 | $node->setAttribute($to, $node->getAttribute($from)); | |||||||||||||||||||||||||||
| 194 | 0 | $node->removeAttribute($from); | |||||||||||||||||||||||||||
| 195 | 0 | return; } | |||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | sub remove_attr { | ||||||||||||||||||||||||||||
| 198 | 0 | ▶ | my ($node, @attr) = @_; | ||||||||||||||||||||||||||
|
Subroutine
remove_attr: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 199 | 0 | map { $node->removeAttribute($_) } @attr; | |||||||||||||||||||||||||||
| 200 | 0 | return; } | |||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | sub get_attr { | ||||||||||||||||||||||||||||
| 203 | 0 | ▶ | my ($node, @attr) = @_; | ||||||||||||||||||||||||||
|
Subroutine
get_attr: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 204 | 0 | return map { $node->getAttribute($_) } @attr; } | |||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | # NOTE: This really should be part of some top-level 'common' initialization | ||||||||||||||||||||||||||||
| 207 | # and probably should accommodate catalogs being given as configuration options! | ||||||||||||||||||||||||||||
| 208 | # However, it presumably sets some global state in XML::LibXML, | ||||||||||||||||||||||||||||
| 209 | # so it's safe to do ( record! ) once, even across Daemon calls. | ||||||||||||||||||||||||||||
| 210 | my $catalogs_initialized = 0; # [CONFIGURATION] | ||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | sub initialize_catalogs { | ||||||||||||||||||||||||||||
| 213 | 0 | ▶ | return if $catalogs_initialized; | ||||||||||||||||||||||||||
|
Subroutine
initialize_catalogs: not called CC=3 SCAR=24.8
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 214 | 0 | $catalogs_initialized = 1; | |||||||||||||||||||||||||||
| 215 | 0 | foreach my $catalog (pathname_findall('LaTeXML.catalog', installation_subdir => '.')) { | |||||||||||||||||||||||||||
| 216 | 0 | XML::LibXML->load_catalog($catalog); } | |||||||||||||||||||||||||||
| 217 | 0 | return; } | |||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | # FINISH THIS EXPERIMENT LATER.... | ||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||
| 221 | # We need to be able to find various XML resources: XSLT, RelaxNG and other random xml. | ||||||||||||||||||||||||||||
| 222 | # Catalogs provide one means to provide a level of abstraction in pathname location. | ||||||||||||||||||||||||||||
| 223 | # But at least at the top level, files ought to be searched for according to the current | ||||||||||||||||||||||||||||
| 224 | # search paths (being command line arguments, relative to source files, etc); | ||||||||||||||||||||||||||||
| 225 | # Possibly files might be referred to within XML files that libxml is already parsing | ||||||||||||||||||||||||||||
| 226 | # and these could benefit from the searchpath approach? | ||||||||||||||||||||||||||||
| 227 | # | ||||||||||||||||||||||||||||
| 228 | # We can also provide InputCallbacks to the various XML::LibXML objects that allow | ||||||||||||||||||||||||||||
| 229 | # us to programatically find & read these XML items according to the searchpaths. | ||||||||||||||||||||||||||||
| 230 | # One problem is that we don't have (from this level of the API) | ||||||||||||||||||||||||||||
| 231 | # a clean method of accessing the current search paths! | ||||||||||||||||||||||||||||
| 232 | # Another problem is that embedded references to oddly-located relative files will usually | ||||||||||||||||||||||||||||
| 233 | # get turned into paths relative to the top-level document that libxml is currently reading! | ||||||||||||||||||||||||||||
| 234 | # So, we'll be given an absolute path before we have a chance to search the searchpaths for it! | ||||||||||||||||||||||||||||
| 235 | # | ||||||||||||||||||||||||||||
| 236 | # Perhaps we should even handle the catalog functionality here? | ||||||||||||||||||||||||||||
| 237 | # | ||||||||||||||||||||||||||||
| 238 | # How should we find the searchpaths????? | ||||||||||||||||||||||||||||
| 239 | # Note also that if you use relative pathnames to refer to xml objects from within another, | ||||||||||||||||||||||||||||
| 240 | # that libxml2 will already have _assumed_ that it is relative to the base document! | ||||||||||||||||||||||||||||
| 241 | # That is, we're getting an absolute path, here. Of course, the original request | ||||||||||||||||||||||||||||
| 242 | # could have been an absolute path, so we probably shouldn't be blithely rewriting abs paths!!! | ||||||||||||||||||||||||||||
| 243 | # We could take over the whole catalog business, however... | ||||||||||||||||||||||||||||
| 244 | # sub initialize_input_callbacks { | ||||||||||||||||||||||||||||
| 245 | # my($object,%options) = @_; | ||||||||||||||||||||||||||||
| 246 | # # return; | ||||||||||||||||||||||||||||
| 247 | # # THIS IS TOTALLY WRONG!!!! Figure out how we'll find out about search paths! | ||||||||||||||||||||||||||||
| 248 | # my $paths = $LaTeXML::SEARCHPATHS; | ||||||||||||||||||||||||||||
| 249 | # # options might be installation_subdirs, or such pathname_find things. | ||||||||||||||||||||||||||||
| 250 | # my $cb = XML::LibXML::InputCallback->new(); | ||||||||||||||||||||||||||||
| 251 | # $cb->register_callbacks([ | ||||||||||||||||||||||||||||
| 252 | # sub { # Matcher | ||||||||||||||||||||||||||||
| 253 | # my($uri)=@_; | ||||||||||||||||||||||||||||
| 254 | # print STDERR "INPUT CHECK: $uri\n"; | ||||||||||||||||||||||||||||
| 255 | # # We don't want to do the search here, 'cause we'll have to do it again in open! | ||||||||||||||||||||||||||||
| 256 | # return 0 if $uri =~ m|^file://|; # pass on absolute pathnames | ||||||||||||||||||||||||||||
| 257 | # return 0 if pathname_is_absolute($uri); # a | ||||||||||||||||||||||||||||
| 258 | # # if($uri =~ /^urn:x-LaTeXML:([^:]*):(.*)$/){ | ||||||||||||||||||||||||||||
| 259 | # print STDERR "INPUT ACCEPT: $uri\n"; | ||||||||||||||||||||||||||||
| 260 | # return 1; }, | ||||||||||||||||||||||||||||
| 261 | # sub { # Opener | ||||||||||||||||||||||||||||
| 262 | # my($uri)=@_; | ||||||||||||||||||||||||||||
| 263 | # my $handle; | ||||||||||||||||||||||||||||
| 264 | # $uri =~ s|^file://||; | ||||||||||||||||||||||||||||
| 265 | # # WARNING!!! Kludge alert! | ||||||||||||||||||||||||||||
| 266 | # my @paths = ('.'); | ||||||||||||||||||||||||||||
| 267 | # push(@paths, @$LaTeXML::SEARCHPATHS) if $LaTeXML::SEARCHPATHS; | ||||||||||||||||||||||||||||
| 268 | # push(@paths, @{$LaTeXML::POST{searchpaths}}) if $LaTeXML::POST; | ||||||||||||||||||||||||||||
| 269 | # push(@paths, $LaTeXML::DOCUMENT->getSearchPaths) if $LaTeXML::DOCUMENT; | ||||||||||||||||||||||||||||
| 270 | # if(my $pathname = pathname_find($uri, | ||||||||||||||||||||||||||||
| 271 | # # types => ['xsl'], installation_subdir => 'resources/XSLT', | ||||||||||||||||||||||||||||
| 272 | # paths=>[@paths])){ | ||||||||||||||||||||||||||||
| 273 | # open($handle,$pathname); | ||||||||||||||||||||||||||||
| 274 | # return $handle; } | ||||||||||||||||||||||||||||
| 275 | # else { | ||||||||||||||||||||||||||||
| 276 | # Error('missing-file',$uri,undef, | ||||||||||||||||||||||||||||
| 277 | # "Couldn't find file '$uri' in search paths", | ||||||||||||||||||||||||||||
| 278 | # "Search paths were ".join(',',@paths)); | ||||||||||||||||||||||||||||
| 279 | # return; }}, | ||||||||||||||||||||||||||||
| 280 | # sub { # Reader | ||||||||||||||||||||||||||||
| 281 | # my($handle,$length)=@_; | ||||||||||||||||||||||||||||
| 282 | # my $buffer; | ||||||||||||||||||||||||||||
| 283 | # read($handle,$buffer,$length); | ||||||||||||||||||||||||||||
| 284 | # return $buffer; }, | ||||||||||||||||||||||||||||
| 285 | # sub { # Closer | ||||||||||||||||||||||||||||
| 286 | # my($handle)=@_; | ||||||||||||||||||||||||||||
| 287 | # close($handle); | ||||||||||||||||||||||||||||
| 288 | # return; }]); | ||||||||||||||||||||||||||||
| 289 | # $object->input_callbacks($cb); | ||||||||||||||||||||||||||||
| 290 | # return; } | ||||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||
| 292 | #====================================================================== | ||||||||||||||||||||||||||||
| 293 | # Odd place for this utility, but it is needed in both conversion & post | ||||||||||||||||||||||||||||
| 294 | # ALSO needs error reporting capability. | ||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||
| 296 | my @RDF_TERM_ATTRIBUTES = ( # [CONSTANT] | ||||||||||||||||||||||||||||
| 297 | qw(about resource property typeof rel rev datatype)); | ||||||||||||||||||||||||||||
| 298 | my %NON_RDF_PREFIXES = map { ($_ => 1) } qw(http https ftp); # [CONSTANT] | ||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||
| 300 | sub set_RDFa_prefixes { | ||||||||||||||||||||||||||||
| 301 | 0 | ▶ | my ($document, $map) = @_; | ||||||||||||||||||||||||||
|
Subroutine
set_RDFa_prefixes: not called CC=17 SCAR=57.2
| |||||||||||||||||||||||||||||
| 302 | 0 | my $root = $document->documentElement; | |||||||||||||||||||||||||||
| 303 | 0 | my %prefixes = (); | |||||||||||||||||||||||||||
| 304 | 0 | my %localmap = map { ($_ => $$map{$_}) } keys %$map; | |||||||||||||||||||||||||||
| 305 | 0 | ▶ | if (my $prefixes = $root->getAttribute('prefix')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 306 | 0 | my @x = split(/\s/, $prefixes); | |||||||||||||||||||||||||||
| 307 | 0 | ▶ | while (@x) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 308 | 0 | my ($prefix, $uri) = (shift(@x), shift(@x)); | |||||||||||||||||||||||||||
| 309 | 0 | $prefix =~ s/:$//; | |||||||||||||||||||||||||||
| 310 | 0 | $prefixes{$prefix} = 1; | |||||||||||||||||||||||||||
| 311 | 0 | ▶ | if (!$localmap{$prefix}) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 312 | 0 | $localmap{$prefix} = $uri; } | |||||||||||||||||||||||||||
| 313 | elsif ($localmap{$prefix} ne $uri) { | ||||||||||||||||||||||||||||
| 314 | 0 | carp "Clash of RDFa prefix '$prefix' ('$uri' vs '$localmap{$prefix}'); " | |||||||||||||||||||||||||||
| 315 | . "Skipping RDFa prefix management"; | ||||||||||||||||||||||||||||
| 316 | 0 | return; } } } | |||||||||||||||||||||||||||
| 317 | 0 | ▶ | if (my @n = $document->findnodes('descendant::*[@prefix]')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 318 | 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])
| |||||||||||||||||||||||||||||
| 319 | 0 | carp "RDFa attribute 'prefix' on non-root node; " | |||||||||||||||||||||||||||
| 320 | . "Skipping RDFa prefix management"; | ||||||||||||||||||||||||||||
| 321 | 0 | return; } } | |||||||||||||||||||||||||||
| 322 | 0 | ▶ | if (my @n = $document->findnodes('descendant::*[@vocab]')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 323 | 0 | carp "RDFa attribute 'vocab' on non-root node; " | |||||||||||||||||||||||||||
| 324 | . "Skipping RDFa prefix management"; | ||||||||||||||||||||||||||||
| 325 | 0 | return; } | |||||||||||||||||||||||||||
| 326 | 0 | my $xpath = 'descendant::*[' . join(' or ', map { '@' . $_ } @RDF_TERM_ATTRIBUTES) . ']'; | |||||||||||||||||||||||||||
| 327 | 0 | foreach my $node ($document->findnodes($xpath)) { | |||||||||||||||||||||||||||
| 328 | 0 | foreach my $k (@RDF_TERM_ATTRIBUTES) { | |||||||||||||||||||||||||||
| 329 | 0 | ▶ | if (my $v = $node->getAttribute($k)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 330 | 0 | foreach my $term (split(/\s/, $v)) { | |||||||||||||||||||||||||||
| 331 | 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}
| |||||||||||||||||||||||||||||
| 332 | 0 | ▶ | $prefixes{$1} = 1 if $localmap{$1}; } } } } } # A prefix is a prefix IFF there is a mapping!! | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 333 | 0 | ▶ | if (my $prefixes = join(' ', map { $_ . ": " . $localmap{$_} } sort keys %prefixes)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 334 | 0 | $root->setAttribute(prefix => $prefixes); } | |||||||||||||||||||||||||||
| 335 | 0 | return; } | |||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||
| 337 | ###################################################################### | ||||||||||||||||||||||||||||
| 338 | # PATCH Section | ||||||||||||||||||||||||||||
| 339 | ###################################################################### | ||||||||||||||||||||||||||||
| 340 | # Various versions of XML::LibXML have introduced incompatable improvements | ||||||||||||||||||||||||||||
| 341 | # We can run using older versions, but have to patch things up to | ||||||||||||||||||||||||||||
| 342 | # a consistent level. | ||||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||
| 344 | our $original_XML_LibXML_Document_toString; # [CONFIGURATION] | ||||||||||||||||||||||||||||
| 345 | our $original_XML_LibXML_Element_getAttribute; # [CONFIGURATION] | ||||||||||||||||||||||||||||
| 346 | our $original_XML_LibXML_Element_hasAttribute; # [CONFIGURATION] | ||||||||||||||||||||||||||||
| 347 | our $original_XML_LibXML_Element_setAttribute; # [CONFIGURATION] | ||||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||
| 349 | BEGIN { | ||||||||||||||||||||||||||||
| 350 | 29 | ▶ | *original_XML_LibXML_Document_toString = *XML::LibXML::Document::toString; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 351 | 29 | *original_XML_LibXML_Element_getAttribute = *XML::LibXML::Element::getAttribute; | |||||||||||||||||||||||||||
| 352 | 29 | *original_XML_LibXML_Element_hasAttribute = *XML::LibXML::Element::hasAttribute; | |||||||||||||||||||||||||||
| 353 | 29 | *original_XML_LibXML_Element_setAttribute = *XML::LibXML::Element::setAttribute; | |||||||||||||||||||||||||||
| 354 | } | ||||||||||||||||||||||||||||
| 355 | |||||||||||||||||||||||||||||
| 356 | # As of 1.63, LibXML converts a document "to String" as bytes, not characters (?) | ||||||||||||||||||||||||||||
| 357 | sub encoding_XML_LibXML_Document_toString { | ||||||||||||||||||||||||||||
| 358 | 0 | ▶ | my ($self, $depth) = @_; | ||||||||||||||||||||||||||
|
Subroutine
encoding_XML_LibXML_Document_toString: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 359 | # Encode::encode("utf-8", $self->original_XML_LibXML_Document_toString($depth)); } | ||||||||||||||||||||||||||||
| 360 | 0 | return Encode::encode("utf-8", original_XML_LibXML_Document_toString($self, $depth)); } | |||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||
| 362 | # As of 1.59, element attribute methods accept attributes names as "xml:foo" | ||||||||||||||||||||||||||||
| 363 | # (in particular, xml:id), without explicitly calling the NS versions. | ||||||||||||||||||||||||||||
| 364 | # The new form is considerably more convenient. | ||||||||||||||||||||||||||||
| 365 | sub xmlns_XML_LibXML_Element_getAttribute { | ||||||||||||||||||||||||||||
| 366 | 0 | ▶ | my ($self, $name) = @_; | ||||||||||||||||||||||||||
|
Subroutine
xmlns_XML_LibXML_Element_getAttribute: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 367 | 0 | ▶ | if ($name =~ /^xml:(.*)$/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 368 | 0 | my $attr = $1; | |||||||||||||||||||||||||||
| 369 | 0 | return $self->getAttributeNS($LaTeXML::Common::XML::XML_NS, $attr); } | |||||||||||||||||||||||||||
| 370 | else { | ||||||||||||||||||||||||||||
| 371 | 0 | return original_XML_LibXML_Element_getAttribute($self, $name); } } | |||||||||||||||||||||||||||
| 372 | |||||||||||||||||||||||||||||
| 373 | sub xmlns_XML_LibXML_Element_hasAttribute { | ||||||||||||||||||||||||||||
| 374 | 0 | ▶ | my ($self, $name) = @_; | ||||||||||||||||||||||||||
|
Subroutine
xmlns_XML_LibXML_Element_hasAttribute: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 375 | 0 | ▶ | if ($name =~ /^xml:(.*)$/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 376 | 0 | my $attr = $1; | |||||||||||||||||||||||||||
| 377 | 0 | return $self->hasAttributeNS($LaTeXML::Common::XML::XML_NS, $attr); } | |||||||||||||||||||||||||||
| 378 | else { | ||||||||||||||||||||||||||||
| 379 | 0 | return original_XML_LibXML_Element_hasAttribute($self, $name); } } | |||||||||||||||||||||||||||
| 380 | |||||||||||||||||||||||||||||
| 381 | sub xmlns_XML_LibXML_Element_setAttribute { | ||||||||||||||||||||||||||||
| 382 | 0 | ▶ | my ($self, $name, $value) = @_; | ||||||||||||||||||||||||||
|
Subroutine
xmlns_XML_LibXML_Element_setAttribute: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 383 | 0 | ▶ | if ($name =~ /^xml:(.*)$/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 384 | 0 | my $attr = $1; | |||||||||||||||||||||||||||
| 385 | 0 | return $self->setAttributeNS($LaTeXML::Common::XML::XML_NS, $attr, $value); } | |||||||||||||||||||||||||||
| 386 | else { | ||||||||||||||||||||||||||||
| 387 | 0 | return original_XML_LibXML_Element_setAttribute($self, $name, $value); } } | |||||||||||||||||||||||||||
| 388 | |||||||||||||||||||||||||||||
| 389 | our $xml_libxml_version; # [CONFIGURATION] | ||||||||||||||||||||||||||||
| 390 | |||||||||||||||||||||||||||||
| 391 | BEGIN { | ||||||||||||||||||||||||||||
| 392 | 29 | ▶ | $xml_libxml_version = $XML::LibXML::VERSION; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=3 SCAR=14.2
| |||||||||||||||||||||||||||||
| 393 | 29 | $xml_libxml_version =~ s/_\d+$//; | |||||||||||||||||||||||||||
| 394 | ### print STDERR "XML::LibXML Version $XML::LibXML::VERSION => $xml_libxml_version\n"; | ||||||||||||||||||||||||||||
| 395 | |||||||||||||||||||||||||||||
| 396 | 29 | ▶ | if ($xml_libxml_version < 1.63) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 397 | 0 | *XML::LibXML::Document::toString = *encoding_XML_LibXML_Document_toString; } | |||||||||||||||||||||||||||
| 398 | 29 | ▶ | if ($xml_libxml_version < 1.59) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 399 | 0 | *XML::LibXML::Element::getAttribute = *xmlns_XML_LibXML_Element_getAttribute; | |||||||||||||||||||||||||||
| 400 | 0 | *XML::LibXML::Element::hasAttribute = *xmlns_XML_LibXML_Element_hasAttribute; | |||||||||||||||||||||||||||
| 401 | 0 | *XML::LibXML::Element::setAttribute = *xmlns_XML_LibXML_Element_setAttribute; } | |||||||||||||||||||||||||||
| 402 | } | ||||||||||||||||||||||||||||
| 403 | |||||||||||||||||||||||||||||
| 404 | #====================================================================== | ||||||||||||||||||||||||||||
| 405 | 1; | ||||||||||||||||||||||||||||