Line 0
Link Here
|
|
|
1 |
#!/usr/bin/perl |
2 |
package Koha::DublinCoreTransformer; |
3 |
|
4 |
# |
5 |
# This file is part of Koha. |
6 |
# |
7 |
# Copyright (C) 2014 Universidad de El Salvador, |
8 |
# Facultad Multidisciplinaria Oriental |
9 |
# |
10 |
# Koha is free software; you can redistribute it and/or modify it |
11 |
# under the terms of the GNU General Public License as published by |
12 |
# the Free Software Foundation; either version 3 of the License, or |
13 |
# (at your option) any later version. |
14 |
# |
15 |
# Koha is distributed in the hope that it will be useful, but |
16 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
17 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
18 |
# GNU General Public License for more details. |
19 |
# |
20 |
# You should have received a copy of the GNU General Public License |
21 |
# along with Koha; if not, see <http://www.gnu.org/licenses>. |
22 |
# |
23 |
|
24 |
use Modern::Perl; |
25 |
use strict; |
26 |
use warnings::register; |
27 |
use version; |
28 |
use v5.10.1; |
29 |
|
30 |
BEGIN { |
31 |
require Exporter; |
32 |
|
33 |
# set the version for version checking |
34 |
our $VERSION = version->declare("v0.01"); |
35 |
|
36 |
# Inherit from Exporter to export functions and variables |
37 |
our @ISA = qw(Exporter); |
38 |
|
39 |
# Functions and variables which are exported by default |
40 |
our @EXPORT = qw(new get_xml_data get_rdf_data write_xml write_rdf); |
41 |
|
42 |
# Functions and variables which can be optionally exported |
43 |
our @EXPORT_OK = |
44 |
qw(type qualified get_marc_record root xsi_schemaLocation rdf_subject rdf_format entities optional_namespace namespace_url); |
45 |
|
46 |
} |
47 |
|
48 |
use utf8; |
49 |
use Encode qw(is_utf8 encode decode); |
50 |
use MARC::File::USMARC; |
51 |
use MARC::Crosswalk::DublinCore; |
52 |
use XML::LibXML; |
53 |
use HTML::Entities; |
54 |
use XML::Entities; |
55 |
use RDF::Helper; |
56 |
use base qw(Class::Accessor); |
57 |
__PACKAGE__->mk_accessors( |
58 |
qw(type qualified get_marc_record root xsi_schemaLocation rdf_subject rdf_format entities optional_namespace namespace_url) |
59 |
); |
60 |
|
61 |
# Defining some constants |
62 |
my $xsi_schema = qq(http://www.w3.org/2001/XMLSchema-instance); |
63 |
my $unqualified = qq(http://purl.org/dc/elements/1.1/); |
64 |
my $qualified = qq(http://purl.org/dc/terms/); |
65 |
my $dcam = qq(http://purl.org/dc/dcam/); |
66 |
my $rdfNS = qq(http://www.w3.org/1999/02/22-rdf-syntax-ns#); |
67 |
my @elementupcase = |
68 |
qw/tableOfContents dateAccepted dateCopyrighted dateSubmitted isVersionOf hasVersion isReplacedBy isRequiredBy isPartOf hasPart isReferencedBy isFormatOf hasFormat conformsTo accrualMethod accrualPeriodicity accrualPolicy instructionalMethod rightsHolder educationLevel accessRights bibliographicCitation/; |
69 |
|
70 |
#USAGE: Koha::DublinCoreTransformer->new({ |
71 |
# type => 'xml|rdf', |
72 |
# qualified => 0|1, |
73 |
# get_marc_record => $record |
74 |
#}); |
75 |
|
76 |
sub new { |
77 |
my ( $class, $args ) = @_; |
78 |
$args = {} unless defined $args; |
79 |
return bless( $args, $class ); |
80 |
} |
81 |
|
82 |
sub get_xml_data { |
83 |
|
84 |
#USAGE: oop->get_xml_data({ |
85 |
# root => 'root', |
86 |
# xsi_schemaLocation => 'http://example.org/myapp/ http://example.org/myapp/schema.xsd', |
87 |
# entities => 0|1, |
88 |
# optional_namespace => 'oai_dc' |
89 |
# namespace_url => 'http://www.openarchives.org/OAI/2.0/oai_dc/' |
90 |
#}); |
91 |
my $self = shift; |
92 |
my ($args) = @_; |
93 |
|
94 |
if (@_) { |
95 |
$self->root( $args->{root} ); |
96 |
$self->xsi_schemaLocation( $args->{xsi_schemaLocation} ); |
97 |
$self->entities( $args->{entities} ); |
98 |
$self->optional_namespace( $args->{optional_namespace} ); |
99 |
$self->namespace_url( $args->{namespace_url} ); |
100 |
} |
101 |
|
102 |
#if data not filled |
103 |
$self->{root} = "metadata" unless ( defined $self->{root} ); |
104 |
$self->{entities} = 0 unless ( defined $self->{entities} ); |
105 |
my %dataxml = ( |
106 |
xmlroot => $self->{root}, |
107 |
entities => $self->{entities}, |
108 |
optional_namespace => $self->{optional_namespace}, |
109 |
namespace_url => $self->{namespace_url}, |
110 |
); |
111 |
|
112 |
if ( exists $self->{xsi_schemaLocation} ) { |
113 |
$dataxml{'xsi_schemaLocation'} = $self->{xsi_schemaLocation}; |
114 |
} |
115 |
if ( exists $self->{xsi_schemaLocation} ) { |
116 |
$dataxml{'optional_namespace'} = $self->{optional_namespace}; |
117 |
} |
118 |
if ( exists $self->{xsi_schemaLocation} ) { |
119 |
$dataxml{'namespace_url'} = $self->{namespace_url}; |
120 |
} |
121 |
return \%dataxml; |
122 |
} |
123 |
|
124 |
sub get_rdf_data { |
125 |
|
126 |
#USAGE: oop->get_rdf_data({ |
127 |
# rdf_subject => 'http://opac.mydomain.edu/cgi-bin/koha/opac-detail.pl?biblionumber=17589', |
128 |
# rdf_format => 'ntriples | nquads| rdfxml | rdfjson | ntriples-canonical | turtle', |
129 |
# entities => 0|1, |
130 |
#}) |
131 |
|
132 |
my $self = shift; |
133 |
my ($args) = @_; |
134 |
|
135 |
if (@_) { |
136 |
$self->rdf_subject( $args->{rdf_subject} ); |
137 |
$self->rdf_format( $args->{rdf_format} ); |
138 |
$self->entities( $args->{entities} ); |
139 |
} |
140 |
|
141 |
#if data not filled |
142 |
$self->{rdf_subject} = "http://koha-community.org/" |
143 |
unless ( defined $self->{rdf_subject} ); |
144 |
$self->{rdf_format} = "rdfxml" unless ( defined $self->{rdf_format} ); |
145 |
$self->{entities} = 0 unless ( defined $self->{entities} ); |
146 |
my %rdfdescription = ( |
147 |
rdf_subject => $self->{rdf_subject}, |
148 |
rdf_format => $self->{rdf_format}, |
149 |
entities => $self->{entities}, |
150 |
); |
151 |
|
152 |
return \%rdfdescription; |
153 |
} |
154 |
|
155 |
sub conversion_to_dc { |
156 |
|
157 |
#Get the MARC record and return a Dublin Core elements, passed as argument in dc_order_execution method. |
158 |
#USAGE: |
159 |
#my dc = $self->conversion_to_dc(); |
160 |
#$self->dc_order_execution ($dc, $dcxml, $root); |
161 |
my $self = shift; |
162 |
my $marc_record_obj = $self->get_marc_record(); |
163 |
my $crosswalk; |
164 |
my $dc; |
165 |
|
166 |
#if data not filled |
167 |
$self->qualified(0) unless ( exists $self->{qualified} ); |
168 |
if ( ( $self->qualified() != 0 and $self->qualified() != 1 ) |
169 |
&& warnings::enabled($self) ) |
170 |
{ |
171 |
warnings::warn( $self, "Only the 0 or 1 values are accepted" ); |
172 |
die "Set qualified => " . $self->qualified() . " is no good, stopped"; |
173 |
} |
174 |
if ( $self->qualified() == 1 ) { |
175 |
$crosswalk = MARC::Crosswalk::DublinCore->new( qualified => 1 ); |
176 |
} |
177 |
elsif ( $self->qualified() == 0 ) { |
178 |
$crosswalk = MARC::Crosswalk::DublinCore->new( qualified => 0 ); |
179 |
} |
180 |
if ( $marc_record_obj =~ /^MARC::Record/ ) { |
181 |
$dc = $crosswalk->as_dublincore($marc_record_obj); |
182 |
} |
183 |
else { |
184 |
warnings::warn( $self, |
185 |
"MARC::Record object failed or not a MARC::Record" ); |
186 |
die "Stopped due get_marc_record => is not a MARC::Record"; |
187 |
} |
188 |
|
189 |
return $dc; |
190 |
|
191 |
} |
192 |
|
193 |
sub write_xml { |
194 |
my $self = shift; |
195 |
my $dataxml = $self->get_xml_data(); |
196 |
my $dc = $self->conversion_to_dc(); |
197 |
|
198 |
#Looking for bad data |
199 |
if ( ( $self->type() ne 'xml' and $self->type() ne 'rdf' ) |
200 |
&& warnings::enabled($self) ) |
201 |
{ |
202 |
warnings::warn( $self, "Only the xml or rdf values are accepted" ); |
203 |
die "Set type => " . $self->type() . " is no good, stopped"; |
204 |
} |
205 |
elsif ( $self->type() eq 'rdf' && warnings::enabled($self) ) { |
206 |
warnings::warn( $self, "The constructor specified rdf" ); |
207 |
die "Cannot use this method with rdf. This is no good, stopped"; |
208 |
} |
209 |
|
210 |
if ( ( $self->entities() != 0 and $self->entities() != 1 ) |
211 |
&& warnings::enabled($self) ) |
212 |
{ |
213 |
warnings::warn( $self, "Only the 0 or 1 values are accepted" ); |
214 |
die "Set entities => " . $self->entities() . " is no good, stopped"; |
215 |
} |
216 |
|
217 |
#Creating XML object |
218 |
my $doc = XML::LibXML::Document->new( '1.0', 'utf-8' ); |
219 |
my $root = $doc->createElement( $dataxml->{'xmlroot'} ); |
220 |
$root->setNamespace( $xsi_schema, 'xsi', 0 ); |
221 |
|
222 |
if (defined $dataxml->{'optional_namespace'}){ |
223 |
$root->setNamespace( $dataxml->{'namespace_url'}, |
224 |
$dataxml->{'optional_namespace'}, 0 ); |
225 |
} |
226 |
|
227 |
if ( $self->type() eq 'xml' ) { |
228 |
if ( $self->qualified() == 0 ) { |
229 |
if ( defined $dataxml->{'xsi_schemaLocation'} ) { |
230 |
$root->setAttribute( |
231 |
'xsi:schemaLocation' => $dataxml->{'xsi_schemaLocation'} ); |
232 |
$root->setNamespace( $unqualified, 'dc', 0 ); |
233 |
$self->dc_order_execution( $dc, $doc, $root ); |
234 |
} |
235 |
else { |
236 |
$root->setNamespace( $unqualified, 'dc', 0 ); |
237 |
$self->dc_order_execution( $dc, $doc, $root ); |
238 |
} |
239 |
} |
240 |
elsif ( $self->qualified() == 1 ) { |
241 |
if ( defined $dataxml->{'xsi_schemaLocation'} ) { |
242 |
$root->setAttribute( |
243 |
'xsi:schemaLocation' => $dataxml->{'xsi_schemaLocation'} ); |
244 |
$root->setNamespace( $unqualified, 'dc', 0 ); |
245 |
$root->setNamespace( $qualified, 'dcterms', 0 ); |
246 |
$self->dc_order_execution( $dc, $doc, $root ); |
247 |
} |
248 |
else { |
249 |
$root->setNamespace( $unqualified, 'dc', 0 ); |
250 |
$root->setNamespace( $qualified, 'dcterms', 0 ); |
251 |
$self->dc_order_execution( $dc, $doc, $root ); |
252 |
} |
253 |
} |
254 |
} |
255 |
|
256 |
$doc->setDocumentElement($root); |
257 |
|
258 |
#Enabling or desabling entities and print the xml |
259 |
if ( $dataxml->{'entities'} == 1 ) { |
260 |
my $serialized_data = $doc->toString(1); |
261 |
my ($dcxml_entity_encoded) = XML_entity_encode( $serialized_data ); |
262 |
$serialized_data = $dcxml_entity_encoded; |
263 |
return $serialized_data; |
264 |
} |
265 |
else { |
266 |
return $doc->toString(1); |
267 |
} |
268 |
} |
269 |
|
270 |
sub write_rdf { |
271 |
my $self = shift; |
272 |
my $rdfdescription = $self->get_rdf_data(); |
273 |
my $dc = $self->conversion_to_dc(); |
274 |
my $rdf; |
275 |
my $subject = $rdfdescription->{'rdf_subject'}; |
276 |
|
277 |
#Looking for bad data |
278 |
if ( ( $self->type() ne 'xml' and $self->type() ne 'rdf' ) |
279 |
&& warnings::enabled($self) ) |
280 |
{ |
281 |
warnings::warn( $self, "Only the xml or rdf values are accepted" ); |
282 |
die "Set type => " . $self->type() . " is no good, stopped"; |
283 |
} |
284 |
elsif ( $self->type() eq 'xml' && warnings::enabled($self) ) { |
285 |
warnings::warn( $self, "The constructor specified xml" ); |
286 |
die "Cannot use this method with xml. This is no good, stopped"; |
287 |
} |
288 |
|
289 |
if ( ( $self->entities() != 0 and $self->entities() != 1 ) |
290 |
&& warnings::enabled($self) ) |
291 |
{ |
292 |
warnings::warn( $self, "Only the 0 or 1 values are accepted" ); |
293 |
die "Set entities => " . $self->entities() . " is no good, stopped"; |
294 |
} |
295 |
|
296 |
if ( $self->qualified() == 0 ) { |
297 |
|
298 |
#Creating RDF::Helper object |
299 |
$rdf = RDF::Helper->new( |
300 |
BaseInterface => 'RDF::Trine', |
301 |
namespaces => { |
302 |
rdf => $rdfNS, |
303 |
dc => $unqualified, |
304 |
}, |
305 |
ExpandQNames => 1, |
306 |
base_uri => '', |
307 |
); |
308 |
} |
309 |
else { |
310 |
$rdf = RDF::Helper->new( |
311 |
BaseInterface => 'RDF::Trine', |
312 |
namespaces => { |
313 |
rdf => $rdfNS, |
314 |
dcterms => $qualified, |
315 |
dcam => $dcam, |
316 |
}, |
317 |
ExpandQNames => 1, |
318 |
base_uri => '', |
319 |
); |
320 |
} |
321 |
|
322 |
if ( $self->type() eq 'rdf' ) { |
323 |
$self->dc_order_execution( $dc, $rdf, $subject ); |
324 |
} |
325 |
|
326 |
#Enabling or desabling entities and print the xml, fixing encode with rdfjson |
327 |
if ( $rdfdescription->{'entities'} == 1 |
328 |
&& $rdfdescription->{'rdf_format'} eq 'rdfxml' ) |
329 |
{ |
330 |
my $serialized_data = $rdf->serialize( format => $rdfdescription->{'rdf_format'} ); |
331 |
my ($dcxml_entity_encoded) = XML_entity_encode( $serialized_data ); |
332 |
$serialized_data = $dcxml_entity_encoded; |
333 |
return $serialized_data; |
334 |
} |
335 |
elsif ( $rdfdescription->{'rdf_format'} eq 'rdfjson' ) { |
336 |
my $encoding = |
337 |
$rdf->serialize( format => $rdfdescription->{'rdf_format'} ); |
338 |
my $enc = Encode::find_encoding('iso-8859-1'); |
339 |
Encode::is_utf8 ( $encoding ) ? $encoding : $enc->decode ( $encoding ); |
340 |
return $encoding; |
341 |
} |
342 |
else { |
343 |
return $rdf->serialize( format => $rdfdescription->{'rdf_format'} ); |
344 |
} |
345 |
} |
346 |
|
347 |
sub upper_or_lowercase { |
348 |
|
349 |
#This subroutine define if is necessary to convert lowercase some qualifiers or let them as crosswalk method basis |
350 |
#USAGE: upper_or_lowercase (element->qualifier()) |
351 |
my $dcelement = $_[0]; |
352 |
my $count = 0; |
353 |
my $result; |
354 |
while ( $count <= $#elementupcase ) { |
355 |
if ( $elementupcase[$count] eq $dcelement ) { |
356 |
$result = $dcelement; |
357 |
last; |
358 |
} |
359 |
elsif ( $dcelement ne $elementupcase[$count] ) { |
360 |
$result = lc($dcelement); |
361 |
$count++; |
362 |
next; |
363 |
} |
364 |
} |
365 |
|
366 |
return $result; |
367 |
|
368 |
} |
369 |
|
370 |
sub XML_entity_encode { |
371 |
|
372 |
#This subroutine change characteres to XML entities. Since XML::LibXML only encode the five basics |
373 |
#USAGE: This subroutine is called when get_xml_data is populated with entities => 1 |
374 |
my @strings = @_; |
375 |
my @strings_entity_encoded; |
376 |
foreach my $string (@strings) { |
377 |
utf8::decode($string); |
378 |
my $html_entities = |
379 |
HTML::Entities::encode_entities( $string, '^\n\t\x20-\x26\x28-\x7e' ); |
380 |
my $xml_entities = XML::Entities::numify( 'all', $html_entities ); |
381 |
push @strings_entity_encoded, $xml_entities; |
382 |
} |
383 |
return @strings_entity_encoded; |
384 |
} |
385 |
|
386 |
sub xml_constructor { |
387 |
|
388 |
#xml_constructor ( doc, root, namespace, attr, element_name, element_content, element_qualifier, element_scheme ) |
389 |
#USAGE: define all posible elements (name, content, qualifier, scheme) as neccesary |
390 |
#doc: the XML object |
391 |
#root: append root to doc |
392 |
#namespace: defined as "dc" or "dcterms" |
393 |
#attr: a boolean value to determinate xsi:type xml namespace for dcterms |
394 |
#examples: |
395 |
#xml_constructor ( $dcxml, $root, "dc", 0, element->name(), element->content() ); |
396 |
#xml_constructor ( $dcxml, $root, "dcterms", 1, element->name(), element->content(), $element->qualifier() ); |
397 |
|
398 |
my ( $doc, $root, $namespace, $attr, $element_name, $element_content, |
399 |
$element_qualifier, $element_scheme ) |
400 |
= @_; |
401 |
|
402 |
# This scalar is used for deleting spaces in scheme elements |
403 |
my $deletespaces; |
404 |
|
405 |
# This scalar is used for making upper or lowercase the qualifier elements |
406 |
my $lowercase; |
407 |
my $tagxml; |
408 |
|
409 |
given ($namespace) { |
410 |
when ( $namespace eq "dc" ) { |
411 |
$lowercase = lc($element_name); |
412 |
} |
413 |
when ( $namespace eq "dcterms" ) { |
414 |
$lowercase = upper_or_lowercase($element_qualifier); |
415 |
|
416 |
#replacing spacial to spatial, bug from dublin or crosswalk. |
417 |
$lowercase =~ s/spacial/spatial/; |
418 |
} |
419 |
} |
420 |
|
421 |
if ( ( $namespace eq "dc" or "dcterms" ) and $attr == 0 ) { |
422 |
|
423 |
# This will do something like: <dc:title>Title</dc:title> |
424 |
$tagxml = XML::LibXML::Element->new($lowercase); |
425 |
$tagxml->setNamespace( undef, $namespace, 1 ); |
426 |
$tagxml->appendTextNode($element_content); |
427 |
$root->appendChild($tagxml); |
428 |
} |
429 |
elsif ( ( $namespace eq "dcterms" ) and $attr == 1 ) { |
430 |
|
431 |
# This will do something like: <dcterms:spatial xsi:type="dcterms:ISO3166">us</dcterms:spatial> |
432 |
$deletespaces = $element_scheme; |
433 |
$deletespaces =~ s/\s//; |
434 |
$tagxml = XML::LibXML::Element->new($lowercase); |
435 |
$tagxml->setNamespace( undef, $namespace, 1 ); |
436 |
my $attribute = $namespace . ':' . $deletespaces; |
437 |
$tagxml->setAttribute( 'xsi:type', $attribute ); |
438 |
$tagxml->appendTextNode($element_content); |
439 |
$root->appendChild($tagxml); |
440 |
} |
441 |
elsif ( ( $namespace eq "dc" ) and $attr == 1 ) { |
442 |
given ($element_scheme) { |
443 |
|
444 |
# This will do something like: <dc:subject xsi:type="dcterms:LCSH">Water supply.</dc:subject> |
445 |
when ( $element_scheme ne "DCMI Type Vocabulary" ) |
446 |
{ #Dublin core returns DCMI Type Vocabulary |
447 |
$deletespaces = $element_scheme; |
448 |
$deletespaces =~ s/\s//; |
449 |
$tagxml = XML::LibXML::Element->new($lowercase); |
450 |
$tagxml->setNamespace( undef, $namespace, 1 ); |
451 |
my $attribute = 'dcterms:' . $deletespaces; |
452 |
$tagxml->setAttribute( 'xsi:type', $attribute ); |
453 |
$tagxml->appendTextNode($element_content); |
454 |
$root->appendChild($tagxml); |
455 |
} |
456 |
when ( $element_scheme eq "DCMI Type Vocabulary" ) |
457 |
{ #Changing for DCMIType |
458 |
# This will do something like: <dc:type xsi:type="dcterms:DCMIType">Text</dc:type> |
459 |
$tagxml = XML::LibXML::Element->new($lowercase); |
460 |
$tagxml->setNamespace( undef, $namespace, 1 ); |
461 |
$tagxml->setAttribute( 'xsi:type', 'dcterms:DCMIType' ); |
462 |
$tagxml->appendTextNode($element_content); |
463 |
$root->appendChild($tagxml); |
464 |
} |
465 |
} |
466 |
} #if...elsif |
467 |
|
468 |
return $tagxml; |
469 |
|
470 |
} #subroutine |
471 |
|
472 |
sub rdf_constructor { |
473 |
|
474 |
#xml_constructor ( rdf, subject, namespace, attr, element_name, element_content, element_qualifier, element_scheme ) |
475 |
#USAGE: define all posible elements (name, content, qualifier, scheme) as neccesary |
476 |
#rdf: the RDF object |
477 |
#subject: the subject for the triples |
478 |
#namespace: defined as "dc" or "dcterms". This is for Simple DC or DC-RDF recommendations. |
479 |
#attr: a boolean value to determine the nodeID attribute for dcterms |
480 |
|
481 |
my ( $rdf, $subject, $namespace, $attr, $element_name, $element_content, |
482 |
$element_qualifier, $element_scheme ) |
483 |
= @_; |
484 |
|
485 |
#This scalar is used for deleting spaces in scheme elements |
486 |
my $deletespaces; |
487 |
|
488 |
#This scalar is used for making upper or lowercase the qualifier elements |
489 |
my $lowercase; |
490 |
|
491 |
given ($namespace) { |
492 |
when ( $namespace eq "dc" ) { |
493 |
$lowercase = lc($element_name); |
494 |
} |
495 |
when ( $namespace eq "dcterms" ) { |
496 |
$lowercase = upper_or_lowercase($element_qualifier); |
497 |
|
498 |
#replacing spacial to spatial, bug from dublin or crosswalk. |
499 |
$lowercase =~ s/spacial/spatial/; |
500 |
} |
501 |
} |
502 |
|
503 |
if ( ( $namespace eq "dc" or "dcterms" ) and $attr == 0 ) { |
504 |
|
505 |
# This will do something like: <dc:title>Title</dc:title> |
506 |
my $predicate = $namespace . ':' . $lowercase; |
507 |
my $object = $element_content; |
508 |
$rdf->assert_literal( $subject, $predicate, $object ); |
509 |
} |
510 |
elsif ( ( $namespace eq "dc" ) and $attr == 2 ) { |
511 |
|
512 |
# This will do something like: <dcterms:title>Title</dcterms:title> |
513 |
my $predicate = 'dcterms:' . $lowercase; |
514 |
my $object = $element_content; |
515 |
$rdf->assert_literal( $subject, $predicate, $object ); |
516 |
} |
517 |
elsif ( ( $namespace eq "dc" ) and $attr == 1 ) { |
518 |
$deletespaces = $element_scheme; |
519 |
$deletespaces =~ s/\s//; |
520 |
my $predicate = 'dcterms:' . $lowercase; |
521 |
my $object = $element_content; |
522 |
SWITCH: |
523 |
for ($element_scheme) { |
524 |
if ( /^ISO 3166/ ) { |
525 |
|
526 |
# Dublin core returns DCMI Type Vocabulary |
527 |
# This will do something like: |
528 |
# <rdf:Description rdf:nodeID="country"> |
529 |
# <dcam:memberOf rdf:resource="http://purl.org/dc/terms/ISO3166"/> |
530 |
# <rdf:value>us</rdf:value> |
531 |
# </rdf:Description> |
532 |
# <dcterms:spatial rdf:nodeID="country"/> |
533 |
my $bnode = $rdf->new_bnode("country"); |
534 |
my $uri_resource = join "", $qualified, $deletespaces; |
535 |
my $resource = $rdf->new_resource($uri_resource); |
536 |
$rdf->assert_literal( $subject, $predicate, $bnode ); |
537 |
$rdf->assert_literal( $bnode, 'dcam:memberOf', $resource ); |
538 |
$rdf->assert_literal( $bnode, 'rdf:value', $object ); |
539 |
last SWITCH; |
540 |
} |
541 |
if ( /^DCMI Type Vocabulary/ ) |
542 |
{ #Changing for DCMIType |
543 |
my $bnode = $rdf->new_bnode('DCMIType'); |
544 |
my $uri_resource = $qualified . 'DCMIType'; |
545 |
my $resource = $rdf->new_resource($uri_resource); |
546 |
$rdf->assert_literal( $subject, $predicate, $bnode ); |
547 |
$rdf->assert_literal( $bnode, 'dcam:memberOf', $resource ); |
548 |
$rdf->assert_literal( $bnode, 'rdf:value', $object ); |
549 |
last SWITCH; |
550 |
} |
551 |
if ( /^ISO 639-2/ ) |
552 |
{ #Changing bnodeId ISO639-2 for language |
553 |
my $bnode = $rdf->new_bnode('language'); |
554 |
my $uri_resource = join "", $qualified, $deletespaces; |
555 |
my $resource = $rdf->new_resource($uri_resource); |
556 |
$rdf->assert_literal( $subject, $predicate, $bnode ); |
557 |
$rdf->assert_literal( $bnode, 'dcam:memberOf', $resource ); |
558 |
$rdf->assert_literal( $bnode, 'rdf:value', $object ); |
559 |
last SWITCH; |
560 |
} |
561 |
if ( /^RFC 1766/ ) |
562 |
{ #Changing bnodeId RFC1766 for languagetag |
563 |
my $bnode = $rdf->new_bnode('languagetag'); |
564 |
my $uri_resource = join "", $qualified, $deletespaces; |
565 |
my $resource = $rdf->new_resource($uri_resource); |
566 |
$rdf->assert_literal( $subject, $predicate, $bnode ); |
567 |
$rdf->assert_literal( $bnode, 'dcam:memberOf', $resource ); |
568 |
$rdf->assert_literal( $bnode, 'rdf:value', $object ); |
569 |
last SWITCH; |
570 |
} |
571 |
if ( /^LCSH/ || /^MeSH/ || /^LCC/ || /^DDC/ || |
572 |
/^UDC/ || /^IMT/ || /^URI/ || /^TGN/ ) |
573 |
{ #Scheme sharing common elements |
574 |
my $bnode = $rdf->new_bnode($deletespaces); |
575 |
my $uri_resource = join "", $qualified, $deletespaces; |
576 |
my $resource = $rdf->new_resource($uri_resource); |
577 |
$rdf->assert_literal( $subject, $predicate, $bnode ); |
578 |
$rdf->assert_literal( $bnode, 'dcam:memberOf', $resource ); |
579 |
$rdf->assert_literal( $bnode, 'rdf:value', $object ); |
580 |
last SWITCH; |
581 |
} |
582 |
} |
583 |
} #if-elsif |
584 |
return $rdf; |
585 |
} #subroutine |
586 |
|
587 |
sub dc_order_execution { |
588 |
|
589 |
#USAGE: USAGE: Get the parameters for xml or rdf and execute in order. |
590 |
my $self = shift; |
591 |
|
592 |
if ( $self->type() eq 'xml' ) { #Order for xml |
593 |
my ( $dc, $dcxml, $root ) = @_; |
594 |
foreach my $element ( $dc->elements() ) { |
595 |
SWITCH: { |
596 |
if ( $element->name() |
597 |
and not defined $element->scheme() |
598 |
and not defined $element->qualifier() ) |
599 |
{ |
600 |
$dcxml = |
601 |
xml_constructor( $dcxml, $root, "dc", 0, $element->name(), |
602 |
$element->content() ); |
603 |
last SWITCH; |
604 |
} |
605 |
|
606 |
if ( $element->name() |
607 |
and not defined $element->scheme() |
608 |
and not defined $element->qualifier() ) |
609 |
{ |
610 |
$dcxml = |
611 |
xml_constructor( $dcxml, $root, "dcterms", 0, |
612 |
$element->name(), $element->content() ); |
613 |
last SWITCH; |
614 |
} |
615 |
|
616 |
if ( $element->name() |
617 |
and $element->scheme() |
618 |
and $element->qualifier() ) |
619 |
{ |
620 |
$dcxml = xml_constructor( |
621 |
$dcxml, $root, |
622 |
"dcterms", 1, |
623 |
$element->name(), $element->content(), |
624 |
$element->qualifier(), $element->scheme() |
625 |
); |
626 |
last SWITCH; |
627 |
} |
628 |
|
629 |
if ( $element->name() |
630 |
and $element->scheme() |
631 |
and ( $element->scheme ne "DCMI Type Vocabulary" ) ) |
632 |
{ |
633 |
$dcxml = |
634 |
xml_constructor( $dcxml, $root, "dc", 1, $element->name(), |
635 |
$element->content(), $element->qualifier(), |
636 |
$element->scheme() ); |
637 |
last SWITCH; |
638 |
} |
639 |
|
640 |
if ( $element->name() |
641 |
and $element->scheme() |
642 |
and ( $element->scheme() eq "DCMI Type Vocabulary" ) ) |
643 |
{ |
644 |
$dcxml = |
645 |
xml_constructor( $dcxml, $root, "dc", 1, $element->name(), |
646 |
$element->content(), $element->qualifier(), |
647 |
$element->scheme() ); |
648 |
last SWITCH; |
649 |
} |
650 |
|
651 |
if ( $element->name() and $element->qualifier() ) { |
652 |
$dcxml = xml_constructor( |
653 |
$dcxml, $root, |
654 |
"dcterms", 0, |
655 |
$element->name(), $element->content(), |
656 |
$element->qualifier(), $element->scheme() |
657 |
); |
658 |
last SWITCH; |
659 |
} #if |
660 |
} #SWITCH |
661 |
} #foreach 1 |
662 |
} |
663 |
elsif ( $self->type() eq 'rdf' ) { #Order for rdf/xml |
664 |
my ( $dc, $rdf, $subject ) = @_; |
665 |
foreach my $element ( $dc->elements() ) { |
666 |
SWITCH: { |
667 |
if ( $element->name() |
668 |
and not defined $element->scheme() |
669 |
and not defined $element->qualifier() ) |
670 |
{ |
671 |
rdf_constructor( $rdf, $subject, "dc", 0, $element->name(), |
672 |
$element->content() ) |
673 |
unless $self->qualified() == 1; |
674 |
rdf_constructor( $rdf, $subject, "dc", 2, $element->name(), |
675 |
$element->content() ) |
676 |
unless $self->qualified() == 0; |
677 |
last SWITCH; |
678 |
} |
679 |
|
680 |
if ( $element->name() |
681 |
and $element->scheme() |
682 |
and ( $element->scheme ne "DCMI Type Vocabulary" ) ) |
683 |
{ |
684 |
rdf_constructor( $rdf, $subject, "dc", 1, $element->name(), |
685 |
$element->content(), $element->qualifier(), |
686 |
$element->scheme() ); |
687 |
last SWITCH; |
688 |
} |
689 |
|
690 |
if ( $element->name() |
691 |
and $element->scheme() |
692 |
and ( $element->scheme() eq "DCMI Type Vocabulary" ) ) |
693 |
{ |
694 |
rdf_constructor( $rdf, $subject, "dc", 1, $element->name(), |
695 |
$element->content(), $element->qualifier(), |
696 |
$element->scheme() ); |
697 |
last SWITCH; |
698 |
} |
699 |
|
700 |
if ( $element->name() and $element->qualifier() ) { |
701 |
rdf_constructor( |
702 |
$rdf, $subject, |
703 |
"dcterms", 0, |
704 |
$element->name(), $element->content(), |
705 |
$element->qualifier(), $element->scheme() |
706 |
); |
707 |
last SWITCH; |
708 |
} #if |
709 |
} #SWITCH |
710 |
} #foreach 2 |
711 |
} |
712 |
return; |
713 |
} |
714 |
|
715 |
END { |
716 |
|
717 |
#When opened with MARC::File::USMARC, close marc record if not closed |
718 |
sub DESTROY { |
719 |
my $self = shift; |
720 |
if ( $self->{get_marc_record} =~ /^MARC:File::USMARC/ ) { |
721 |
$self->{get_marc_record}->close(); |
722 |
undef $self->{get_marc_record}; |
723 |
} |
724 |
} |
725 |
} |
726 |
1; |
727 |
__END__ |
728 |
|
729 |
=pod |
730 |
|
731 |
=encoding UTF-8 |
732 |
|
733 |
=head1 NAME |
734 |
|
735 |
Koha::DublinCoreTransformer - transform the values of MARC::Crosswalk::DublinCore |
736 |
into metadata in XML or RDF format. |
737 |
|
738 |
=head1 SYNOPSIS |
739 |
|
740 |
=head2 XML-DC |
741 |
|
742 |
use Koha::DublinCoreTransformer; |
743 |
use MARC::File::USMARC; |
744 |
my $batch = MARC::File::USMARC->in('in.mrc'); |
745 |
|
746 |
## the $record will be a MARC::Record object. |
747 |
my $record = $batch->next(); |
748 |
|
749 |
#For Simple DC-XML |
750 |
my $objectDC = DublinCoreTransformer->new({ |
751 |
type => 'xml', |
752 |
qualified => 0, |
753 |
get_marc_record => $record}); |
754 |
|
755 |
#For Qualified DC-XML |
756 |
my $objectDC = DublinCoreTransformer->new({ |
757 |
type => 'xml', |
758 |
qualified => 1, |
759 |
get_marc_record => $record}); |
760 |
|
761 |
#After call constructor you need to pass the root element and |
762 |
#the schema in .xsd format. If you prefer or you don't know nothing |
763 |
#about it pass the method in blank, i.e., without arguments. |
764 |
#Also you can determine if you want to encode with xml entities or |
765 |
#just with the five basics entities. |
766 |
#Set entities to 0 for the five basics or 1 to encode with the |
767 |
#complete xml entities. |
768 |
$objectDC->get_xml_data({ |
769 |
root => 'metadata', |
770 |
xsi_schemaLocation => 'http://example.org/myapp/ http://example.org/myapp/schema.xsd', |
771 |
}); |
772 |
#This print your xml file in STDOUT |
773 |
my $XML-DC = $objectDC->write_xml(); |
774 |
print $XML-DC; |
775 |
|
776 |
#Close the file according to MARC::File::USMARC |
777 |
$batch->close(); |
778 |
undef $batch; |
779 |
|
780 |
#Or if you want to generate a file |
781 |
open (OUTPUT, '> dc2xml.xml') or die $!; |
782 |
print OUTPUT $XML-DC; |
783 |
close(OUTPUT); |
784 |
|
785 |
=head2 DC-RDF |
786 |
|
787 |
use Koha::DublinCoreTransformer; |
788 |
use MARC::File::USMARC; |
789 |
my $marc = MARC::File::USMARC->in('in.mrc'); |
790 |
my $record = $marc->next(); |
791 |
|
792 |
#Simple DC-RDF is not used but not deprecated, DublinCoreTransformer |
793 |
#can generate it as follow: |
794 |
my $objectDC = DublinCoreTransformer->new({ |
795 |
type => 'rdf', |
796 |
qualified => 0, |
797 |
get_marc_record => $record}); |
798 |
|
799 |
#DC-RDF |
800 |
my $objectDC = DublinCoreTransformer->new({ |
801 |
type => 'rdf', |
802 |
qualified => 1, |
803 |
get_marc_record => $record}); |
804 |
|
805 |
#The get_rdf_data method for RDF does not need to use the root element |
806 |
#and the schema, but you need to declare the resource being described, |
807 |
#i.e., the record. |
808 |
#Note: For Koha is the IRI identifier of the record |
809 |
$objectDC->get_rdf_data({ |
810 |
rdf_subject => 'http://opac.mydomain.edu/cgi-bin/koha/opac-detail.pl?biblionumber=17589', |
811 |
}); |
812 |
|
813 |
my $XML-RDF = $objectDC->write_rdf(); |
814 |
print $XML-RDF; |
815 |
#Close the file according to MARC::File::USMARC |
816 |
$batch->close(); |
817 |
undef $marc; |
818 |
|
819 |
=head1 DESCRIPTION |
820 |
|
821 |
The Koha::DublinCoreTransformer defines the interface to transform |
822 |
and manipulate MARC records into DC-XML or DC-RDF recommendation. |
823 |
This package transform MARC records to XML or RDF/XML Dublin Core |
824 |
and follows the recommendations listed below: |
825 |
|
826 |
L<DC-XML-GUIDELINES [DCMI Recommendation]|http://dublincore.org/documents/dc-xml-guidelines/> |
827 |
|
828 |
L<Expressing Simple Dublin Core in RDF-XML|http://dublincore.org/documents/dcmes-xml/> |
829 |
This recommendation is superseded by DC-RDFrecommendation but not obsoleted at all. |
830 |
|
831 |
L<DC-RDF [DCMI Recommendation]|http://dublincore.org/documents/dc-rdf/> |
832 |
|
833 |
=head2 Methods |
834 |
|
835 |
=over 12 |
836 |
|
837 |
=item C<new> |
838 |
|
839 |
Return a new Koha::DublinCoreTransformer object. |
840 |
This constructor sets the output type (RDF or XML), the level of the standard (Simple DC or Qualified DC) and the MARC record to be transformed to DC. |
841 |
|
842 |
my $objectDC = DublinCoreTransformer->new({ |
843 |
type => 'rdf', |
844 |
qualified => 0, |
845 |
get_marc_record => $record}); |
846 |
|
847 |
=item C<get_xml_data> |
848 |
|
849 |
This method is just for DC-XML: Fetch the xml root element and the schema in .xsd format. |
850 |
|
851 |
$objectDC->get_xml_data({ |
852 |
root => 'metadata', |
853 |
xsi_schemaLocation => 'http://example.org/myapp/ http://example.org/myapp/schema.xsd', |
854 |
entities => 1, |
855 |
}); |
856 |
|
857 |
The entities argument is for extended xml entities, i.e., placing entities to zero will codify only the five basic entities e.g., <, >, ", &, and &apos. Conversely if it is encoded to one, all entities will coded into decimal. |
858 |
|
859 |
Additionally, has been included the optional_namespace and namespace_url arguments to include another XML name space. |
860 |
|
861 |
NOTE: This method is only for use of the DC-XML recommendation, i.e., type => 'xml'. |
862 |
See C<new> method or the section SYNOPSIS. |
863 |
|
864 |
=item C<get_rdf_data> |
865 |
|
866 |
This method is only for DC-RDF: Fetch the IRI identifier of the resource being described. As well, you can pass the format you want to generate with the rdf, by default the DC-RDF specify RDF/XML but RDF can represent in other formats listed below: |
867 |
|
868 |
. ntriples |
869 |
|
870 |
. nquads |
871 |
|
872 |
. rdfxml |
873 |
|
874 |
. rdfjson |
875 |
|
876 |
. ntriples-canonical |
877 |
|
878 |
. turtle |
879 |
|
880 |
$objectDC->get_rdf_data({ |
881 |
rdf_subject => 'http://opac.fmoues.edu.sv/cgi-bin/koha/opac-detail.pl?biblionumber=17589', |
882 |
rdf_format => 'rdfxml', |
883 |
entities => 0|1, |
884 |
}); |
885 |
|
886 |
NOTE: The entities is needed only for rdfxml value and is used like C<get_xml_data> method. |
887 |
|
888 |
=item C<write_xml> |
889 |
|
890 |
Return the xml object as string. |
891 |
|
892 |
NOTE: This method is only for use of the DC-XML recommendation, i.e., type => 'xml'. |
893 |
See C<new> and C<get_xml_data> methods or the section SYNOPSIS. |
894 |
|
895 |
=item C<write_rdf> |
896 |
|
897 |
Return the rdf object as string. |
898 |
|
899 |
NOTE: This method is only for use of the DC-RDF recommendation, i.e., type => 'rdf'. |
900 |
See C<new> and C<get_rdf_data> methods or the section SYNOPSIS. |
901 |
|
902 |
=back |
903 |
|
904 |
=head1 VERSION |
905 |
|
906 |
version 0.01 |
907 |
|
908 |
=head1 RELATES MODULES |
909 |
|
910 |
This package requires the following modules: |
911 |
|
912 |
DublinCore::Record |
913 |
|
914 |
MARC::Record |
915 |
|
916 |
MARC::File::USMARC |
917 |
|
918 |
MARC::Crosswalk::DublinCore |
919 |
|
920 |
HTML::Entities |
921 |
|
922 |
XML::Entities |
923 |
|
924 |
XML::LibXML |
925 |
|
926 |
RDF::Helper |
927 |
|
928 |
=head2 SEE ALSO |
929 |
|
930 |
Dublin Core Web Page L<http://dublincore.org>. |
931 |
|
932 |
=head1 LICENSE |
933 |
|
934 |
Copyright (c) 2014 Koha Community. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. |
935 |
|
936 |
=head1 AUTHOR |
937 |
|
938 |
Hector Eduardo Catro Avalos E<lt> hector.hecaxmmx at gmail dot com E<gt> |
939 |
and the Koha Development Team. |
940 |
|
941 |
=cut |
942 |
|