1 package OpenILS::WWW::SuperCat::Feed;
2 use strict; use warnings;
4 use OpenSRF::EX qw(:try);
7 use OpenSRF::Utils::SettingsClient;
17 throw OpenSRF::EX::ERROR ("I need a feed type!") ;
23 return undef unless $xml;
25 $parser = new XML::LibXML if (!$parser);
27 my $self = { doc => $parser->parse_string($xml), items => [] };
29 return bless $self => $class;
35 $self->{type} = $type if ($type);
42 $self->{search} = $search if ($search);
43 return $self->{search};
49 $self->{class} = $search if ($search);
50 return $self->{class};
56 $self->{lib} = $lib if ($lib);
63 $self->{base} = $base if ($base);
70 $self->{root} = $root if ($root);
77 $self->{unapi} = $unapi if ($unapi);
78 return $self->{unapi};
83 push @{ $self->{items} }, @_;
88 return @{ $self->{items} } if (wantarray);
89 return $self->{items};
98 for my $node ($self->{doc}->findnodes($xpath)) {
99 $node->appendChild($new);
113 for my $node ($self->{doc}->findnodes($xpath)) {
114 my $new = $self->{doc}->createElement($name) if (!$ns);
115 $new = $self->{doc}->createElementNS($ns,$name) if ($ns);
117 $new->appendChild( $self->{doc}->createTextNode( $text ) )
121 for my $key (keys %$attrs) {
122 next unless $$attrs{$key};
123 $new->setAttribute( $key => $$attrs{$key} );
127 $node->appendChild( $new );
135 my $class = ref($self) || $self;
138 my $item_xml = shift;
139 my $entry = $class->new($item_xml);
140 return undef unless $entry;
142 $entry->base($self->base);
143 $entry->unapi($self->unapi);
145 $self->push_item($entry);
151 my $holdings_xml = shift;
153 $parser = new XML::LibXML if (!$parser);
154 my $new_doc = $parser->parse_string($holdings_xml);
156 for my $root ( $self->{doc}->findnodes($self->{holdings_xpath}) ) {
157 $root->appendChild($new_doc->documentElement);
165 for my $root ( $self->{doc}->findnodes($self->{item_xpath}) ) {
166 for my $item ( $self->items ) {
167 $root->appendChild( $item->{doc}->documentElement );
176 return $self->{doc}->toString(1);
185 #----------------------------------------------------------
187 package OpenILS::WWW::SuperCat::Feed::atom;
188 use base 'OpenILS::WWW::SuperCat::Feed';
192 my $self = $class->SUPER::build('<feed xmlns:atom="http://www.w3.org/2005/Atom"/>');
193 $self->{doc}->documentElement->setNamespace('http://www.w3.org/2005/Atom', undef);
194 $self->{type} = 'application/xml';
195 $self->{item_xpath} = '/atom:feed';
202 $self->_create_node('/atom:feed','http://www.w3.org/2005/Atom','title', $text);
208 $self->_create_node($self->{item_xpath},'http://www.w3.org/2005/Atom','updated', $text);
214 $self->_create_node('/atom:feed','http://www.w3.org/2005/Atom','author');
215 $self->_create_node('/atom:feed/atom:author', 'http://www.w3.org/2005/Atom','name', $text);
222 my $mime = shift || "application/x-$type+xml";
225 $type = 'self' if ($type eq 'atom');
229 'http://www.w3.org/2005/Atom',
244 $self->_create_node( '/atom:feed', 'http://www.w3.org/2005/Atom', 'id', $id );
247 package OpenILS::WWW::SuperCat::Feed::atom::item;
248 use base 'OpenILS::WWW::SuperCat::Feed::atom';
253 my $self = $class->SUPER::build($xml);
254 $self->{doc}->documentElement->setNamespace('http://www.w3.org/2005/Atom', 'atom');
255 $self->{item_xpath} = '/atom:entry';
256 $self->{holdings_xpath} = '/atom:entry';
257 $self->{type} = 'application/xml';
262 #----------------------------------------------------------
264 package OpenILS::WWW::SuperCat::Feed::rss2;
265 use base 'OpenILS::WWW::SuperCat::Feed';
269 my $self = $class->SUPER::build('<rss version="2.0"><channel/></rss>');
270 $self->{type} = 'application/xml';
271 $self->{item_xpath} = '/rss/channel';
278 $self->_create_node('/rss/channel',undef,'title', $text);
284 $self->_create_node($self->{item_xpath},undef,'lastBuildDate', $text);
290 $self->_create_node('/rss/channel', undef,'generator', $text);
297 my $mime = shift || "application/x-$type+xml";
299 $type = 'self' if ($type eq 'rss2');
312 package OpenILS::WWW::SuperCat::Feed::rss2::item;
313 use base 'OpenILS::WWW::SuperCat::Feed::rss2';
318 my $self = $class->SUPER::build($xml);
319 $self->{type} = 'application/xml';
320 $self->{item_xpath} = '/item';
321 $self->{holdings_xpath} = '/item';
328 $self->_create_node($self->{item_xpath},undef,'pubDate', $text);
333 #----------------------------------------------------------
335 package OpenILS::WWW::SuperCat::Feed::mods;
336 use base 'OpenILS::WWW::SuperCat::Feed';
340 my $self = $class->SUPER::build('<mods:modsCollection version="3.0" xmlns:mods="http://www.loc.gov/mods/"/>');
341 $self->{type} = 'application/xml';
342 $self->{item_xpath} = '/mods:modsCollection';
346 package OpenILS::WWW::SuperCat::Feed::mods::item;
347 use base 'OpenILS::WWW::SuperCat::Feed::mods';
352 my $self = $class->SUPER::build($xml);
353 $self->{doc}->documentElement->setNamespace('http://www.loc.gov/mods/', 'mods');
354 $self->{type} = 'application/xml';
355 $self->{holdings_xpath} = '/mods:mods';
366 if ($type eq 'unapi' || $type eq 'opac') {
369 'http://www.loc.gov/mods/',
372 { type => 'otherFormat', id => 'link-'.$linkid }
375 "mods:mods/mods:relatedItem[\@id='link-$linkid']",
376 'http://www.loc.gov/mods/',
377 'mods:recordIdentifier',
385 #----------------------------------------------------------
387 package OpenILS::WWW::SuperCat::Feed::mods3;
388 use base 'OpenILS::WWW::SuperCat::Feed::mods';
392 my $self = $class->SUPER::build('<mods:modsCollection version="3.0" xmlns:mods="http://www.loc.gov/mods/v3"/>');
393 $self->{type} = 'application/xml';
394 $self->{item_xpath} = '/mods:modsCollection';
398 package OpenILS::WWW::SuperCat::Feed::mods3::item;
399 use base 'OpenILS::WWW::SuperCat::Feed::mods::item';
404 my $self = $class->SUPER::build($xml);
405 $self->{doc}->documentElement->setNamespace('http://www.loc.gov/mods/v3', 'mods');
406 $self->{type} = 'application/xml';
407 $self->{holdings_xpath} = '/mods:mods';
416 if ($type eq 'unapi' || $type eq 'opac') {
419 'http://www.loc.gov/mods/v3',
422 { type => 'otherFormat', id => 'link-'.$linkid }
425 "mods:mods/mods:relatedItem[\@id='link-$linkid']",
426 'http://www.loc.gov/mods/v3',
427 'mods:recordIdentifier',
435 #----------------------------------------------------------
437 package OpenILS::WWW::SuperCat::Feed::marcxml;
438 use base 'OpenILS::WWW::SuperCat::Feed';
442 my $self = $class->SUPER::build('<marc:collection xmlns:marc="http://www.loc.gov/MARC21/slim"/>');
443 $self->{type} = 'application/xml';
444 $self->{item_xpath} = '/marc:collection';
448 package OpenILS::WWW::SuperCat::Feed::marcxml::item;
449 use base 'OpenILS::WWW::SuperCat::Feed::marcxml';
454 my $self = $class->SUPER::build($xml);
455 return undef unless $self;
456 $self->{doc}->documentElement->setNamespace('http://www.loc.gov/MARC21/slim', 'marc');
457 $self->{type} = 'application/xml';
458 $self->{holdings_xpath} = '/marc:record';
467 if ($type eq 'unapi' || $type eq 'opac') {
470 'http://www.w3.org/1999/xhtml',
473 { rel => 'otherFormat', href => $id, title => "Dynamic Details" }
480 #----------------------------------------------------------
482 package OpenILS::WWW::SuperCat::Feed::html;
483 use base 'OpenILS::WWW::SuperCat::Feed::atom';
487 my $self = $class->SUPER::new;
488 $self->type('text/html');
492 our ($_parser, $_xslt, $xslt_file);
496 my $base = $self->base || '';
497 my $root = $self->root || '';
498 my $search = $self->search || '';
499 my $class = $self->class || '';
500 my $lib = $self->lib || '-';
504 $_parser ||= new XML::LibXML;
505 $_xslt ||= new XML::LibXSLT;
508 OpenSRF::Utils::SettingsClient
510 ->config_value( dirs => 'xsl' ).
513 # parse the MODS xslt ...
514 my $atom2html_xslt = $_xslt->parse_stylesheet( $_parser->parse_file($xslt_file) );
516 my $new_doc = $atom2html_xslt->transform(
518 base_dir => "'$root'",
520 searchTerms => "'$search'",
521 searchClass => "'$class'",
524 return $new_doc->toString(1);
528 package OpenILS::WWW::SuperCat::Feed::html::item;
529 use base 'OpenILS::WWW::SuperCat::Feed::atom::item';
531 #----------------------------------------------------------
533 package OpenILS::WWW::SuperCat::Feed::htmlcard;
534 use base 'OpenILS::WWW::SuperCat::Feed::marcxml';
538 my $self = $class->SUPER::new;
539 $self->type('text/html');
540 $self->{xsl} = "/MARC21slim2HTMLCard.xsl";
544 our ($_parser, $_xslt, $xslt_file);
548 my $base = $self->base || '';
549 my $root = $self->root || '';
550 my $search = $self->search || '';
551 my $lib = $self->lib || '-';
555 $_parser ||= new XML::LibXML;
556 $_xslt ||= new XML::LibXSLT;
559 OpenSRF::Utils::SettingsClient
561 ->config_value( dirs => 'xsl' ).$self->{xsl};
563 # parse the MODS xslt ...
564 my $atom2html_xslt = $_xslt->parse_stylesheet( $_parser->parse_file($xslt_file) );
566 my $new_doc = $atom2html_xslt->transform(
568 base_dir => "'$root'",
570 searchTerms => "'$search'",
573 return $new_doc->toString(1);
576 package OpenILS::WWW::SuperCat::Feed::htmlcard::item;
577 use base 'OpenILS::WWW::SuperCat::Feed::marcxml::item';
579 package OpenILS::WWW::SuperCat::Feed::htmlholdings;
580 use base 'OpenILS::WWW::SuperCat::Feed::htmlcard';
584 my $self = $class->SUPER::new;
585 $self->{xsl} = "/MARC21slim2HTMLCard-holdings.xsl";
589 package OpenILS::WWW::SuperCat::Feed::htmlholdings::item;
590 use base 'OpenILS::WWW::SuperCat::Feed::htmlcard::item';