/usr/share/perl5/RDF/Trine/Node/Literal/XML.pm is in librdf-trine-node-literal-xml-perl 0.16-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 | # RDF::Trine::Node::Literal::XML
# -----------------------------------------------------------------------------
=head1 NAME
RDF::Trine::Node::Literal::XML - RDF Node class for XMLLiterals
=cut
package RDF::Trine::Node::Literal::XML;
use strict;
use warnings;
use base qw(RDF::Trine::Node::Literal);
use RDF::Trine::Error qw(:try);
use Scalar::Util qw(blessed refaddr);
use Carp qw(carp croak confess);
use XML::LibXML qw(:ns);
######################################################################
our ( $VERSION, %XML_FRAGMENTS );
BEGIN {
$VERSION = '0.16';
}
######################################################################
=head1 METHODS
=over 4
=item C<< new ( $node ) >>
=item C<< new ( $string [ , $lang, $datatype ] ) >>
Returns a new XML Literal object. This method can be used in two different ways:
It can either be passed a string or an XML::LibXML node.
In the case of passing a string, this method follows the same API as the
RDF::Trine::Node::Literal constructor, but:
=over
=item * $string must be a well-balanced XML fragment
=item * $lang is optional, but if a language code is present it will be used as the value of C<< xml:lang >> attribute(s) on the root XML element(s) of the literal. If the element already has an C<< xml:lang >> attribute it will be overwritten. For the node types that doesn't support adding a language, text and CData, a warning will be issued.
=item * $datatype will be ignored and set to 'http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral'
=back
In the case of using a XML::LibXML node C<< $node >>,
the Node may be one of these types or a subclass thereof:
=over
=item * XML::LibXML::Document
=item * XML::LibXML::DocumentFragment
=item * XML::LibXML::Element
=item * XML::LibXML::CDATASection
=item * XML::LibXML::NodeList
=item * XML::LibXML::Text
=back
If the string is not a valid XML fragment, and the C<< $node >> is not
of one of the above types, this method throws a RDF::Trine::Error exception.
=cut
sub new {
my $class = shift;
my $input = shift;
my $lang = shift;
my $typeok = _check_type($input); # First check if we have a valid node
if ($typeok) { # Then use it
my $literal;
if ( $input->isa('XML::LibXML::NodeList') ) {
foreach my $context ( $input->get_nodelist ) {
if ($lang) {
$context->setAttributeNS( XML_XML_NS, 'lang', $lang );
}
if ( $context->ownerDocument ) {
$literal .= $context->toStringEC14N;
}
else {
$literal .= $context->toString;
}
}
}
else {
if ($lang) {
if ( $input->isa('XML::LibXML::Element') ) {
$input->setAttributeNS( XML_XML_NS, 'lang', $lang );
}
elsif ( $input->isa('XML::LibXML::Document') ) {
my $root = $input->documentElement();
$root->setAttributeNS( XML_XML_NS, 'lang', $lang );
}
elsif ( $input->isa('XML::LibXML::DocumentFragment') ) {
foreach my $context ( $input->childNodes ) {
$context->setAttributeNS( XML_XML_NS, 'lang', $lang );
}
}
else {
carp ref($input) . " doesn't support xml:lang attributes";
}
}
if ( $input->ownerDocument ) {
$literal = $input->toStringEC14N;
}
else {
$literal = $input->toString;
}
}
my $self = $class->SUPER::new( $literal, undef,
'http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral' );
$XML_FRAGMENTS{ refaddr($self) } = $input;
return $self;
}
if ( ref($input) && ( !$typeok ) )
{ # Then it is neither a string nor a good type
throw RDF::Trine::Error -text => ref($input) . " is not a valid type.";
}
# If we have an empty string, just create an empty text node
if ( length($input) == 0 ) {
my $self = $class->SUPER::_new( $input, undef,
'http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral' );
$XML_FRAGMENTS{ refaddr($self) } = XML::LibXML::Text->new($input);
return $self;
}
# Last chance is that it is a string with valid XML
my $parser = XML::LibXML->new();
my $doc = eval { $parser->parse_balanced_chunk($input) };
if ($@) { # Didn't parse, so invalid XML string
throw RDF::Trine::Error -text => "$@";
}
if ($lang) {
foreach my $context ( $doc->childNodes ) {
$context->setAttributeNS( XML_XML_NS, 'lang', $lang );
}
$input = $doc->toString;
}
my $self = $class->SUPER::_new( $input, undef,
'http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral' );
$XML_FRAGMENTS{ refaddr($self) } = $doc;
return $self;
}
=item C<< xml_element >>
Returns the XML::LibXML node for the XML Literal.
=cut
sub xml_element {
my $self = shift;
my $node = $XML_FRAGMENTS{ refaddr($self) };
unless ( blessed($node) ) {
throw RDF::Trine::Error -text => "No XML element found for object";
}
return $node;
}
# Check if we have an acceptable type
sub _check_type {
my $type = shift;
return 0 unless blessed($type);
return ( $type->isa('XML::LibXML::Document')
|| $type->isa('XML::LibXML::DocumentFragment')
|| $type->isa('XML::LibXML::Element')
|| $type->isa('XML::LibXML::CDATASection')
|| $type->isa('XML::LibXML::Text')
|| $type->isa('XML::LibXML::NodeList') );
}
sub DESTROY {
my $self = shift;
delete $XML_FRAGMENTS{ refaddr($self) };
if ( $self->can('SUPER::DESTROY') ) {
$self->SUPER::DESTROY();
}
return;
}
1;
__END__
=back
=head1 AUTHOR
First created by Gregory Todd Williams <gwilliams@cpan.org>, modfied
and maintained by Kjetil Kjernsmo <kjetilk@cpan.org>
=cut
|