| 1 | # | ||
| 2 | # The contents of this file are subject to the Mozilla Public | ||
| 3 | # License Version 1.1 (the "License"); you may not use this file | ||
| 4 | # except in compliance with the License. You may obtain a copy of | ||
| 5 | # the License at http://www.mozilla.org/MPL/ | ||
| 6 | # | ||
| 7 | # Software distributed under the License is distributed on an "AS | ||
| 8 | # IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or | ||
| 9 | # implied. See the License for the specific language governing | ||
| 10 | # rights and limitations under the License. | ||
| 11 | # | ||
| 12 | # The Original Code is the RDF::Core module | ||
| 13 | # | ||
| 14 | # The Initial Developer of the Original Code is Ginger Alliance Ltd. | ||
| 15 | # Portions created by Ginger Alliance are | ||
| 16 | # Copyright (C) 2001 Ginger Alliance Ltd. | ||
| 17 | # All Rights Reserved. | ||
| 18 | # | ||
| 19 | # Contributor(s): | ||
| 20 | # | ||
| 21 | # Alternatively, the contents of this file may be used under the | ||
| 22 | # terms of the GNU General Public License Version 2 or later (the | ||
| 23 | # "GPL"), in which case the provisions of the GPL are applicable | ||
| 24 | # instead of those above. If you wish to allow use of your | ||
| 25 | # version of this file only under the terms of the GPL and not to | ||
| 26 | # allow others to use your version of this file under the MPL, | ||
| 27 | # indicate your decision by deleting the provisions above and | ||
| 28 | # replace them with the notice and other provisions required by | ||
| 29 | # the GPL. If you do not delete the provisions above, a recipient | ||
| 30 | # may use your version of this file under either the MPL or the | ||
| 31 | # GPL. | ||
| 32 | # | ||
| 33 | |||
| 34 | package RDF::Core::NodeFactory; | ||
| 35 | |||
| 36 | use strict; | ||
| 37 | require Exporter; | ||
| 38 | |||
| 39 | require RDF::Core::Resource; | ||
| 40 | require RDF::Core::Literal; | ||
| 41 | use Carp; | ||
| 42 | use URI; | ||
| 43 | |||
| 44 | sub new { | ||
| 45 | my ($pkg,%options) = @_; | ||
| 46 | $pkg = ref $pkg || $pkg; | ||
| 47 | my $self = {}; | ||
| 48 | if (@_ > 0) { | ||
| 49 | $self->{_options} = \%options; | ||
| 50 | } | ||
| 51 | $self->{_options}->{BaseURI} ||= 'uri:'; | ||
| 52 | bless $self, $pkg; | ||
| 53 | } | ||
| 54 | sub setOptions { | ||
| 55 | my ($self, $options) = @_; | ||
| 56 | $self->{_options} = $options; | ||
| 57 | } | ||
| 58 | sub getOptions { | ||
| 59 | return $_[0]->{_options}; | ||
| 60 | } | ||
| 61 | sub newLiteral { | ||
| 62 | my ($self, $value, $lang, $datatype) = @_; | ||
| 63 | return new RDF::Core::Literal ($value, $lang, $datatype); | ||
| 64 | } | ||
| 65 | sub newResource { | ||
| 66 | my $self = shift; | ||
| 67 | my $resource; | ||
| 68 | if (@_ gt 1) { | ||
| 69 | #more then one parameters is interpreted as ($namespace,$localValue) | ||
| 70 | #pair, unless both of them are undef | ||
| 71 | my ($namespace,$localValue) = @_; | ||
| 72 | |||
| 73 | return $self->newResource | ||
| 74 | if !defined $namespace && !defined $localValue; | ||
| 75 | |||
| 76 | if (!defined $namespace) { | ||
| 77 | croak "Resource's namespace must be defined" | ||
| 78 | } | ||
| 79 | $localValue = '' | ||
| 80 | unless defined $localValue; | ||
| 81 | my $absoluteURI = new_abs URI($namespace, $self->getOptions->{BaseURI}); | ||
| 82 | $resource = new RDF::Core::Resource($absoluteURI->as_string, $localValue); | ||
| 83 | } else { | ||
| 84 | #one parameter is URI or bNode label | ||
| 85 | my ($URI) = @_; | ||
| 86 | if (defined $URI) { | ||
| 87 | if ($URI !~ /^_:/ && defined $self->getOptions->{BaseURI}) { | ||
| 88 | my $absoluteURI = new_abs URI($URI, $self->getOptions->{BaseURI}); | ||
| 89 | $resource = new RDF::Core::Resource($absoluteURI->as_string); | ||
| 90 | } else { | ||
| 91 | $resource = new RDF::Core::Resource($URI); | ||
| 92 | } | ||
| 93 | } else { | ||
| 94 | #no parameter or parameter undef - generate bNode label | ||
| 95 | $resource = new RDF::Core::Resource($self->_generateURI); | ||
| 96 | } | ||
| 97 | } | ||
| 98 | return $resource; | ||
| 99 | } | ||
| 100 | sub _generateURI { | ||
| 101 | my $self = shift; | ||
| 102 | $self->getOptions->{GenPrefix} = '_:a' | ||
| 103 | unless defined $self->getOptions->{GenPrefix}; | ||
| 104 | $self->getOptions->{GenCounter} = 0 | ||
| 105 | unless defined $self->getOptions->{GenCounter}; | ||
| 106 | my $bNode = $self->getOptions->{GenPrefix}. | ||
| 107 | $self->getOptions->{GenCounter}++; | ||
| 108 | return $bNode | ||
| 109 | } | ||
| 110 | 1; | ||
| 111 | __END__ |