| 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::Model::Serializer; | ||
| 35 | |||
| 36 | use strict; | ||
| 37 | require Exporter; | ||
| 38 | |||
| 39 | use Carp; | ||
| 40 | require RDF::Core::Serializer; | ||
| 41 | |||
| 42 | use constant RDF_NS => 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'; | ||
| 43 | |||
| 44 | sub new { | ||
| 45 | my ($pkg,%options) = @_; | ||
| 46 | $pkg = ref $pkg || $pkg; | ||
| 47 | my $self = {}; | ||
| 48 | $self->{_options} = \%options; | ||
| 49 | $self->{_prefix} = ''; | ||
| 50 | $self->{_subjects} = undef; | ||
| 51 | $self->{_namespaces} = undef; | ||
| 52 | bless $self, $pkg; | ||
| 53 | } | ||
| 54 | sub setOptions { | ||
| 55 | my ($self,$options) = @_; | ||
| 56 | $self->{_options} = $options; | ||
| 57 | } | ||
| 58 | sub getOptions { | ||
| 59 | my $self = shift; | ||
| 60 | return $self->{_options}; | ||
| 61 | } | ||
| 62 | sub serialize { | ||
| 63 | my $self = shift; | ||
| 64 | if (@_ > 0) { | ||
| 65 | #get options if passed | ||
| 66 | $self->{_options} = $_[0]; | ||
| 67 | } | ||
| 68 | my $serializer = new RDF::Core::Serializer | ||
| 69 | (getSubjects => | ||
| 70 | #once you iterate through statements, store both subjects and namespaces | ||
| 71 | $self->getOptions->{getSubjects} || | ||
| 72 | sub { | ||
| 73 | my $subjects = {}; | ||
| 74 | my $namespaces = {}; | ||
| 75 | if (defined $self->{_subjects}) { | ||
| 76 | $subjects = $self->{_subjects}; | ||
| 77 | $self->{_subjects} = undef; #We won't call this second time anyway | ||
| 78 | } else { | ||
| 79 | my $enumerator = $self->getOptions->{Model}-> | ||
| 80 | getStmts(undef,undef,undef); | ||
| 81 | my $statement = $enumerator->getNext; | ||
| 82 | $namespaces->{+RDF_NS} = 'rdf'; | ||
| 83 | while (defined $statement) { | ||
| 84 | $subjects->{$statement->getSubject->getURI}= | ||
| 85 | [$statement->getSubject,0,0]; | ||
| 86 | $namespaces->{$statement->getPredicate->getNamespace} = | ||
| 87 | $self->_makePrefix | ||
| 88 | unless exists $namespaces->{$statement->getPredicate-> | ||
| 89 | getNamespace} ; | ||
| 90 | $statement = $enumerator->getNext; | ||
| 91 | } | ||
| 92 | $enumerator->close; | ||
| 93 | $self->{_namespaces} = $namespaces; | ||
| 94 | } | ||
| 95 | return $subjects; | ||
| 96 | }, | ||
| 97 | getNamespaces => | ||
| 98 | #once you iterate through statements, store both subjects and namespaces | ||
| 99 | $self->getOptions->{getNamespaces} || | ||
| 100 | sub { | ||
| 101 | my $subjects = {}; | ||
| 102 | my $namespaces = {}; | ||
| 103 | if (defined $self->{_namespaces}) { | ||
| 104 | $namespaces = $self->{_namespaces}; | ||
| 105 | $self->{_namespaces} = undef; #We won't call this second time anyway | ||
| 106 | } else { | ||
| 107 | my $enumerator = $self->getOptions->{Model}-> | ||
| 108 | getStmts(undef,undef,undef); | ||
| 109 | my $statement = $enumerator->getNext; | ||
| 110 | $namespaces->{+RDF_NS} = 'rdf'; | ||
| 111 | while (defined $statement) { | ||
| 112 | $subjects->{$statement->getSubject->getURI}= | ||
| 113 | [$statement->getSubject,0,0]; | ||
| 114 | $namespaces->{$statement->getPredicate->getNamespace} = | ||
| 115 | $self->_makePrefix | ||
| 116 | unless exists $namespaces->{$statement->getPredicate-> | ||
| 117 | getNamespace} ; | ||
| 118 | $statement = $enumerator->getNext; | ||
| 119 | } | ||
| 120 | $enumerator->close; | ||
| 121 | $self->{_subjects} = $subjects; | ||
| 122 | } | ||
| 123 | return $namespaces; | ||
| 124 | }, | ||
| 125 | getStatements => | ||
| 126 | $self->getOptions->{getStatements} || | ||
| 127 | sub { | ||
| 128 | my ($subject, $predicate, $object) = @_; | ||
| 129 | my $enumerator = $self->getOptions->{Model}-> | ||
| 130 | getStmts($subject,$predicate,$object); | ||
| 131 | return $enumerator; | ||
| 132 | }, | ||
| 133 | countStatements => | ||
| 134 | $self->getOptions->{countStatements} || | ||
| 135 | sub { | ||
| 136 | my ($subject, $predicate, $object) = @_; | ||
| 137 | return $self->getOptions->{Model}-> | ||
| 138 | countStmts($subject,$predicate,$object); | ||
| 139 | }, | ||
| 140 | existsStatement => | ||
| 141 | $self->getOptions->{existsStatement} || | ||
| 142 | sub { | ||
| 143 | my ($subject, $predicate, $object) = @_; | ||
| 144 | return $self->getOptions->{Model}-> | ||
| 145 | existsStmt($subject,$predicate,$object); | ||
| 146 | }, | ||
| 147 | Output => $self->getOptions->{Output}, | ||
| 148 | BaseURI => $self->getOptions->{BaseURI}, | ||
| 149 | ); | ||
| 150 | $serializer->serialize; | ||
| 151 | } | ||
| 152 | sub _makePrefix { | ||
| 153 | my $self = shift; | ||
| 154 | $self->{_prefix} ||= 'a'; | ||
| 155 | return $self->{_prefix}++; | ||
| 156 | } | ||
| 157 | 1; | ||
| 158 | __END__ |