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