Source

p5-rdf-rdfa-api / lib / RDF / RDFa / API / RDFNode.pm

package RDF::RDFa::API::RDFNode;

use 5.010;
use Moose;
use namespace::clean -except => 'meta';
use common::sense;

has trineNode   => (is => 'ro', required => 1, isa => 'RDF::Trine::Node');
has dataContext => (is => 'rw', isa => 'RDF::RDFa::API::DataContext');

use overload '""'  => \&toString;
use overload 'eq'  => \&isEqual;

sub value
{
	my ($self) = @_;
	
	return undef
		unless blessed($self->trineNode);
	
	return $self->trineNode->uri
		if $self->trineNode->is_resource;
	
	return $self->trineNode->literal_value
		if $self->trineNode->is_literal;
	
	return $self->trineNode->blank_identifier
		if $self->trineNode->is_blank;
	
	return undef;
}

sub toString
{
	my ($self) = @_;
	
	return undef
		unless blessed($self->trineNode);
	
	return $self->trineNode->uri
		if $self->trineNode->is_resource;
	
	return $self->trineNode->literal_value
		if $self->trineNode->is_literal;
	
	return '_:' . $self->trineNode->blank_identifier
		if $self->trineNode->is_blank;
	
	return undef;
}

sub isEqual
{
	my ($this, $that) = @_;
	
	if (blessed($that) && $that->isa('RDF::RDFa::API::RDFNode'))
	{
		return $this->trineNode->as_ntriples eq $that->trineNode->as_ntriples;
	}
	return $this->trineNode eq "$that";
}

sub fromTrine
{
	my ($class, $node) = @_;
	
	if ($node->is_resource)
	{
		return RDF::RDFa::API::IRI->construct($node->uri);
	}
	elsif ($node->is_blank)
	{
		return RDF::RDFa::API::IRI->construct($node->blank_identifier);
	}
	elsif ($node->is_literal && $node->has_datatype)
	{
		return RDF::RDFa::API::TypedLiteral->construct($node->literal_value, $node->literal_datatype);
	}
	elsif ($node->is_literal)
	{
		return RDF::RDFa::API::PlainLiteral->construct($node->literal_value, $node->literal_value_language);
	}
	
	die "Could not build RDF::RDFa::API::RDFNode.\n";
}

1;

__END__

=head1 NAME

RDF::RDFa::API::RDFNode - An RDFNode is anything that can be an object of an RDFTriple.

=head1 WEBIDL

	[NoInterfaceObject]
	interface RDFNode {
		 readonly attribute stringifier DOMString value;
	};

=head1 PERL SPECIFICS

Stringification ('""') and equality ('eq') are overloaded, so stuff like
C<< $a eq $b >> should "just work".

=head1 ADDITIONS

In addition to the standard RDFa API interface, this module supports the
following methods:

=over

=item * C<trineNode>

Returns the node as an RDF::Trine::Node.

=item * C<isEqual>

Returns true iff the nodes are equivalent (same value, type, language, etc).

=back

=head1 SEE ALSO

L<RDF::RDFa::API>.

=head1 BUGS

Please report any bugs to L<http://rt.cpan.org/>.

=head1 AUTHOR

Toby Inkster E<lt>tobyink@cpan.orgE<gt>.

=head1 COPYRIGHT

Copyright 2010 Toby Inkster

This library is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.