1 package OpenILS::WWW::SuperCat::Feed;
2 use strict; use warnings;
4 use OpenSRF::EX qw(:try);
7 use OpenSRF::Utils::SettingsClient;
14 return 1 if UNIVERSAL::can("OpenILS::WWW::SuperCat::Feed::$type" => 'new');
25 throw OpenSRF::EX::ERROR ("I need a feed type!") ;
31 return undef unless $xml;
33 $parser = new XML::LibXML if (!$parser);
35 my $self = { doc => $parser->parse_string($xml), items => [] };
37 return bless $self => $class;
43 $self->{type} = $type if ($type);
50 $self->{search} = $search if ($search);
51 return $self->{search};
57 $self->{class} = $search if ($search);
58 return $self->{class};
64 $self->{lib} = $lib if ($lib);
71 $self->{base} = $base if ($base);
78 $self->{root} = $root if ($root);
85 $self->{unapi} = $unapi if ($unapi);
86 return $self->{unapi};
91 push @{ $self->{items} }, @_;
96 return @{ $self->{items} } if (wantarray);
97 return $self->{items};
106 for my $node ($self->{doc}->findnodes($xpath)) {
107 $node->appendChild($new);
121 for my $node ($self->{doc}->findnodes($xpath)) {
122 my $new = $self->{doc}->createElement($name) if (!$ns);
123 $new = $self->{doc}->createElementNS($ns,$name) if ($ns);
125 $new->appendChild( $self->{doc}->createTextNode( $text ) )
129 for my $key (keys %$attrs) {
130 next unless $$attrs{$key};
131 $new->setAttribute( $key => $$attrs{$key} );
135 $node->appendChild( $new );
143 my $class = ref($self) || $self;
146 my $item_xml = shift;
147 my $entry = $class->new($item_xml);
148 return undef unless $entry;
150 $entry->base($self->base);
151 $entry->unapi($self->unapi);
153 $self->push_item($entry);
159 my $holdings_xml = shift;
161 $parser = new XML::LibXML if (!$parser);
162 my $new_doc = $parser->parse_string($holdings_xml);
164 for my $root ( $self->{doc}->findnodes($self->{holdings_xpath}) ) {
165 $root->appendChild($new_doc->documentElement);
173 for my $root ( $self->{doc}->findnodes($self->{item_xpath}) ) {
174 for my $item ( $self->items ) {
175 $root->appendChild( $item->{doc}->documentElement );
184 return $self->{doc}->toString(1);
193 #----------------------------------------------------------
195 package OpenILS::WWW::SuperCat::Feed::atom;
196 use base 'OpenILS::WWW::SuperCat::Feed';
200 my $self = $class->SUPER::build('<feed xmlns:atom="http://www.w3.org/2005/Atom"/>');
201 $self->{doc}->documentElement->setNamespace('http://www.w3.org/2005/Atom', undef);
202 $self->{type} = 'application/xml';
203 $self->{item_xpath} = '/atom:feed';
210 $self->_create_node('/atom:feed','http://www.w3.org/2005/Atom','title', $text);
216 $self->_create_node($self->{item_xpath},'http://www.w3.org/2005/Atom','updated', $text);
222 $self->_create_node('/atom:feed','http://www.w3.org/2005/Atom','author');
223 $self->_create_node('/atom:feed/atom:author', 'http://www.w3.org/2005/Atom','name', $text);
230 my $mime = shift || "application/x-$type+xml";
233 $type = 'self' if ($type eq 'atom');
237 'http://www.w3.org/2005/Atom',
252 $self->_create_node( '/atom:feed', 'http://www.w3.org/2005/Atom', 'id', $id );
255 package OpenILS::WWW::SuperCat::Feed::atom::item;
256 use base 'OpenILS::WWW::SuperCat::Feed::atom';
261 my $self = $class->SUPER::build($xml);
262 $self->{doc}->documentElement->setNamespace('http://www.w3.org/2005/Atom', 'atom');
263 $self->{item_xpath} = '/atom:entry';
264 $self->{holdings_xpath} = '/atom:entry';
265 $self->{type} = 'application/xml';
270 #----------------------------------------------------------
272 package OpenILS::WWW::SuperCat::Feed::rss2;
273 use base 'OpenILS::WWW::SuperCat::Feed';
277 my $self = $class->SUPER::build('<rss version="2.0"><channel/></rss>');
278 $self->{type} = 'application/xml';
279 $self->{item_xpath} = '/rss/channel';
286 $self->_create_node('/rss/channel',undef,'title', $text);
292 $self->_create_node($self->{item_xpath},undef,'lastBuildDate', $text);
298 $self->_create_node('/rss/channel', undef,'generator', $text);
305 my $mime = shift || "application/x-$type+xml";
307 $type = 'self' if ($type eq 'rss2');
320 package OpenILS::WWW::SuperCat::Feed::rss2::item;
321 use base 'OpenILS::WWW::SuperCat::Feed::rss2';
326 my $self = $class->SUPER::build($xml);
327 $self->{type} = 'application/xml';
328 $self->{item_xpath} = '/item';
329 $self->{holdings_xpath} = '/item';
336 $self->_create_node($self->{item_xpath},undef,'pubDate', $text);
341 #----------------------------------------------------------
343 package OpenILS::WWW::SuperCat::Feed::mods;
344 use base 'OpenILS::WWW::SuperCat::Feed';
348 my $self = $class->SUPER::build('<mods:modsCollection version="3.0" xmlns:mods="http://www.loc.gov/mods/"/>');
349 $self->{type} = 'application/xml';
350 $self->{item_xpath} = '/mods:modsCollection';
354 package OpenILS::WWW::SuperCat::Feed::mods::item;
355 use base 'OpenILS::WWW::SuperCat::Feed::mods';
360 my $self = $class->SUPER::build($xml);
361 $self->{doc}->documentElement->setNamespace('http://www.loc.gov/mods/', 'mods');
362 $self->{type} = 'application/xml';
363 $self->{holdings_xpath} = '/mods:mods';
374 if ($type eq 'unapi' || $type eq 'opac') {
377 'http://www.loc.gov/mods/',
380 { type => 'otherFormat', id => 'link-'.$linkid }
383 "mods:mods/mods:relatedItem[\@id='link-$linkid']",
384 'http://www.loc.gov/mods/',
385 'mods:recordIdentifier',
393 #----------------------------------------------------------
395 package OpenILS::WWW::SuperCat::Feed::mods3;
396 use base 'OpenILS::WWW::SuperCat::Feed::mods';
400 my $self = $class->SUPER::build('<mods:modsCollection version="3.0" xmlns:mods="http://www.loc.gov/mods/v3"/>');
401 $self->{type} = 'application/xml';
402 $self->{item_xpath} = '/mods:modsCollection';
406 package OpenILS::WWW::SuperCat::Feed::mods3::item;
407 use base 'OpenILS::WWW::SuperCat::Feed::mods::item';
412 my $self = $class->SUPER::build($xml);
413 $self->{doc}->documentElement->setNamespace('http://www.loc.gov/mods/v3', 'mods');
414 $self->{type} = 'application/xml';
415 $self->{holdings_xpath} = '/mods:mods';
424 if ($type eq 'unapi' || $type eq 'opac') {
427 'http://www.loc.gov/mods/v3',
430 { type => 'otherFormat', id => 'link-'.$linkid }
433 "mods:mods/mods:relatedItem[\@id='link-$linkid']",
434 'http://www.loc.gov/mods/v3',
435 'mods:recordIdentifier',
443 #----------------------------------------------------------
445 package OpenILS::WWW::SuperCat::Feed::marcxml;
446 use base 'OpenILS::WWW::SuperCat::Feed';
450 my $self = $class->SUPER::build('<marc:collection xmlns:marc="http://www.loc.gov/MARC21/slim"/>');
451 $self->{type} = 'application/xml';
452 $self->{item_xpath} = '/marc:collection';
460 if ($type eq 'unapi') {
463 'http://www.w3.org/1999/xhtml',
466 { rel => 'meta', href => $id, title => "unapi" }
473 package OpenILS::WWW::SuperCat::Feed::marcxml::item;
474 use base 'OpenILS::WWW::SuperCat::Feed::marcxml';
479 my $self = $class->SUPER::build($xml);
480 return undef unless $self;
481 $self->{doc}->documentElement->setNamespace('http://www.loc.gov/MARC21/slim', 'marc');
482 $self->{type} = 'application/xml';
483 $self->{holdings_xpath} = '/marc:record';
492 if ($type eq 'opac') {
495 'http://www.w3.org/1999/xhtml',
498 { rel => 'otherFormat', href => $id, title => "Dynamic Details" }
501 } elsif ($type eq 'unapi-id') {
504 'http://www.w3.org/1999/xhtml',
507 { title => $id, class => "unapi-id" }
514 #----------------------------------------------------------
516 package OpenILS::WWW::SuperCat::Feed::html;
517 use base 'OpenILS::WWW::SuperCat::Feed::atom';
521 my $self = $class->SUPER::new;
522 $self->type('text/html');
526 our ($_parser, $_xslt, $xslt_file);
530 my $base = $self->base || '';
531 my $root = $self->root || '';
532 my $search = $self->search || '';
533 my $class = $self->class || '';
534 my $lib = $self->lib || '-';
538 $_parser ||= new XML::LibXML;
539 $_xslt ||= new XML::LibXSLT;
542 OpenSRF::Utils::SettingsClient
544 ->config_value( dirs => 'xsl' ).
547 # parse the MODS xslt ...
548 my $atom2html_xslt = $_xslt->parse_stylesheet( $_parser->parse_file($xslt_file) );
550 my $new_doc = $atom2html_xslt->transform(
552 base_dir => "'$root'",
554 searchTerms => "'$search'",
555 searchClass => "'$class'",
558 return $new_doc->toString(1);
562 package OpenILS::WWW::SuperCat::Feed::html::item;
563 use base 'OpenILS::WWW::SuperCat::Feed::atom::item';
565 #----------------------------------------------------------
567 package OpenILS::WWW::SuperCat::Feed::htmlcard;
568 use base 'OpenILS::WWW::SuperCat::Feed::marcxml';
572 my $self = $class->SUPER::new;
573 $self->type('text/html');
574 $self->{xsl} = "/MARC21slim2HTMLCard.xsl";
578 our ($_parser, $_xslt, $xslt_file);
582 my $base = $self->base || '';
583 my $root = $self->root || '';
584 my $search = $self->search || '';
585 my $lib = $self->lib || '-';
589 $_parser ||= new XML::LibXML;
590 $_xslt ||= new XML::LibXSLT;
593 OpenSRF::Utils::SettingsClient
595 ->config_value( dirs => 'xsl' ).$self->{xsl};
597 # parse the MODS xslt ...
598 my $atom2html_xslt = $_xslt->parse_stylesheet( $_parser->parse_file($xslt_file) );
600 my $new_doc = $atom2html_xslt->transform(
602 base_dir => "'$root'",
604 searchTerms => "'$search'",
607 return $new_doc->toString(1);
610 package OpenILS::WWW::SuperCat::Feed::htmlcard::item;
611 use base 'OpenILS::WWW::SuperCat::Feed::marcxml::item';
613 package OpenILS::WWW::SuperCat::Feed::htmlholdings;
614 use base 'OpenILS::WWW::SuperCat::Feed::htmlcard';
618 my $self = $class->SUPER::new;
619 $self->{xsl} = "/MARC21slim2HTMLCard-holdings.xsl";
623 package OpenILS::WWW::SuperCat::Feed::htmlholdings::item;
624 use base 'OpenILS::WWW::SuperCat::Feed::htmlcard::item';