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 $new->setAttribute( $key => $$attrs{$key} );
126 $node->appendChild( $new );
134 my $class = ref($self) || $self;
137 my $item_xml = shift;
138 my $entry = $class->new($item_xml);
139 return undef unless $entry;
141 $entry->base($self->base);
142 $entry->unapi($self->unapi);
144 $self->push_item($entry);
150 my $holdings_xml = shift;
152 $parser = new XML::LibXML if (!$parser);
153 my $new_doc = $parser->parse_string($holdings_xml);
155 for my $root ( $self->{doc}->findnodes($self->{holdings_xpath}) ) {
156 $root->appendChild($new_doc->documentElement);
164 for my $root ( $self->{doc}->findnodes($self->{item_xpath}) ) {
165 for my $item ( $self->items ) {
166 $root->appendChild( $item->{doc}->documentElement );
175 return $self->{doc}->toString(1);
184 #----------------------------------------------------------
186 package OpenILS::WWW::SuperCat::Feed::atom;
187 use base 'OpenILS::WWW::SuperCat::Feed';
191 my $self = $class->SUPER::build('<atom:feed xmlns:atom="http://www.w3.org/2005/Atom"/>');
192 $self->{type} = 'application/xml';
193 $self->{item_xpath} = '/atom:feed';
200 $self->_create_node('/atom:feed','http://www.w3.org/2005/Atom','atom:title', $text);
206 $self->_create_node('/atom:feed','http://www.w3.org/2005/Atom','atom:updated', $text);
212 $self->_create_node('/atom:feed','http://www.w3.org/2005/Atom','atom:author');
213 $self->_create_node('/atom:feed/atom:author', 'http://www.w3.org/2005/Atom','atom:name', $text);
220 my $mime = shift || "application/x-$type+xml";
223 $type = 'self' if ($type eq 'atom');
227 'http://www.w3.org/2005/Atom',
242 $self->_create_node( '/atom:feed', 'http://www.w3.org/2005/Atom', 'atom:id', $id );
245 package OpenILS::WWW::SuperCat::Feed::atom::item;
246 use base 'OpenILS::WWW::SuperCat::Feed::atom';
251 my $self = $class->SUPER::build($xml);
252 $self->{doc}->documentElement->setNamespace('http://www.w3.org/2005/Atom', 'atom');
253 $self->{item_xpath} = '/atom:entry';
254 $self->{holdings_xpath} = '/atom:entry';
255 $self->{type} = 'application/xml';
260 #----------------------------------------------------------
262 package OpenILS::WWW::SuperCat::Feed::rss2;
263 use base 'OpenILS::WWW::SuperCat::Feed';
267 my $self = $class->SUPER::build('<rss version="2.0"><channel/></rss>');
268 $self->{type} = 'application/xml';
269 $self->{item_xpath} = '/rss/channel';
276 $self->_create_node('/rss/channel',undef,'title', $text);
282 $self->_create_node('/rss/channel',undef,'lastBuildDate', $text);
288 $self->_create_node('/rss/channel', undef,'generator', $text);
295 my $mime = shift || "application/x-$type+xml";
297 $type = 'self' if ($type eq 'rss2');
310 package OpenILS::WWW::SuperCat::Feed::rss2::item;
311 use base 'OpenILS::WWW::SuperCat::Feed::rss2';
316 my $self = $class->SUPER::build($xml);
317 $self->{type} = 'application/xml';
318 $self->{item_xpath} = '/item';
319 $self->{holdings_xpath} = '/item';
324 #----------------------------------------------------------
326 package OpenILS::WWW::SuperCat::Feed::mods;
327 use base 'OpenILS::WWW::SuperCat::Feed';
331 my $self = $class->SUPER::build('<mods:modsCollection version="3.0" xmlns:mods="http://www.loc.gov/mods/"/>');
332 $self->{type} = 'application/xml';
333 $self->{item_xpath} = '/mods:modsCollection';
337 package OpenILS::WWW::SuperCat::Feed::mods::item;
338 use base 'OpenILS::WWW::SuperCat::Feed::mods';
343 my $self = $class->SUPER::build($xml);
344 $self->{doc}->documentElement->setNamespace('http://www.loc.gov/mods/', 'mods');
345 $self->{type} = 'application/xml';
346 $self->{holdings_xpath} = '/mods:mods';
357 if ($type eq 'unapi' || $type eq 'opac') {
360 'http://www.loc.gov/mods/',
363 { type => 'otherFormat', id => 'link-'.$linkid }
366 "mods:mods/mods:relatedItem[\@id='link-$linkid']",
367 'http://www.loc.gov/mods/',
368 'mods:recordIdentifier',
376 #----------------------------------------------------------
378 package OpenILS::WWW::SuperCat::Feed::mods3;
379 use base 'OpenILS::WWW::SuperCat::Feed::mods';
383 my $self = $class->SUPER::build('<mods:modsCollection version="3.0" xmlns:mods="http://www.loc.gov/mods/v3"/>');
384 $self->{type} = 'application/xml';
385 $self->{item_xpath} = '/mods:modsCollection';
389 package OpenILS::WWW::SuperCat::Feed::mods3::item;
390 use base 'OpenILS::WWW::SuperCat::Feed::mods::item';
395 my $self = $class->SUPER::build($xml);
396 $self->{doc}->documentElement->setNamespace('http://www.loc.gov/mods/v3', 'mods');
397 $self->{type} = 'application/xml';
398 $self->{holdings_xpath} = '/mods:mods';
407 if ($type eq 'unapi' || $type eq 'opac') {
410 'http://www.loc.gov/mods/v3',
413 { type => 'otherFormat', id => 'link-'.$linkid }
416 "mods:mods/mods:relatedItem[\@id='link-$linkid']",
417 'http://www.loc.gov/mods/v3',
418 'mods:recordIdentifier',
426 #----------------------------------------------------------
428 package OpenILS::WWW::SuperCat::Feed::marcxml;
429 use base 'OpenILS::WWW::SuperCat::Feed';
433 my $self = $class->SUPER::build('<marc:collection xmlns:marc="http://www.loc.gov/MARC21/slim"/>');
434 $self->{type} = 'application/xml';
435 $self->{item_xpath} = '/marc:collection';
439 package OpenILS::WWW::SuperCat::Feed::marcxml::item;
440 use base 'OpenILS::WWW::SuperCat::Feed::marcxml';
445 my $self = $class->SUPER::build($xml);
446 return undef unless $self;
447 $self->{doc}->documentElement->setNamespace('http://www.loc.gov/MARC21/slim', 'marc');
448 $self->{type} = 'application/xml';
449 $self->{holdings_xpath} = '/marc:record';
458 if ($type eq 'unapi' || $type eq 'opac') {
461 'http://www.w3.org/1999/xhtml',
464 { rel => 'otherFormat', href => $id, title => "Dynamic Details" }
471 #----------------------------------------------------------
473 package OpenILS::WWW::SuperCat::Feed::html;
474 use base 'OpenILS::WWW::SuperCat::Feed::atom';
478 my $self = $class->SUPER::new;
479 $self->type('text/html');
483 our ($_parser, $_xslt, $xslt_file);
487 my $base = $self->base || '';
488 my $root = $self->root || '';
489 my $search = $self->search || '';
490 my $class = $self->class || '';
491 my $lib = $self->lib || '-';
495 $_parser ||= new XML::LibXML;
496 $_xslt ||= new XML::LibXSLT;
499 OpenSRF::Utils::SettingsClient
501 ->config_value( dirs => 'xsl' ).
504 # parse the MODS xslt ...
505 my $atom2html_xslt = $_xslt->parse_stylesheet( $_parser->parse_file($xslt_file) );
507 my $new_doc = $atom2html_xslt->transform(
509 base_dir => "'$root'",
511 searchTerms => "'$search'",
512 searchClass => "'$class'",
515 return $new_doc->toString(1);
519 package OpenILS::WWW::SuperCat::Feed::html::item;
520 use base 'OpenILS::WWW::SuperCat::Feed::atom::item';
522 #----------------------------------------------------------
524 package OpenILS::WWW::SuperCat::Feed::htmlcard;
525 use base 'OpenILS::WWW::SuperCat::Feed::marcxml';
529 my $self = $class->SUPER::new;
530 $self->type('text/html');
531 $self->{xsl} = "/MARC21slim2HTMLCard.xsl";
535 our ($_parser, $_xslt, $xslt_file);
539 my $base = $self->base || '';
540 my $root = $self->root || '';
541 my $search = $self->search || '';
542 my $lib = $self->lib || '-';
546 $_parser ||= new XML::LibXML;
547 $_xslt ||= new XML::LibXSLT;
550 OpenSRF::Utils::SettingsClient
552 ->config_value( dirs => 'xsl' ).$self->{xsl};
554 # parse the MODS xslt ...
555 my $atom2html_xslt = $_xslt->parse_stylesheet( $_parser->parse_file($xslt_file) );
557 my $new_doc = $atom2html_xslt->transform(
559 base_dir => "'$root'",
561 searchTerms => "'$search'",
564 return $new_doc->toString(1);
567 package OpenILS::WWW::SuperCat::Feed::htmlcard::item;
568 use base 'OpenILS::WWW::SuperCat::Feed::marcxml::item';
570 package OpenILS::WWW::SuperCat::Feed::htmlholdings;
571 use base 'OpenILS::WWW::SuperCat::Feed::htmlcard';
575 my $self = $class->SUPER::new;
576 $self->{xsl} = "/MARC21slim2HTMLCard-holdings.xsl";
580 package OpenILS::WWW::SuperCat::Feed::htmlholdings::item;
581 use base 'OpenILS::WWW::SuperCat::Feed::htmlcard::item';