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!") ;
24 $parser = new XML::LibXML if (!$parser);
26 my $self = { doc => $parser->parse_string($xml), items => [] };
28 return bless $self => $class;
34 $self->{type} = $type if ($type);
41 $self->{search} = $search if ($search);
42 return $self->{search};
48 $self->{lib} = $lib if ($lib);
55 $self->{base} = $base if ($base);
62 $self->{root} = $root if ($root);
69 $self->{unapi} = $unapi if ($unapi);
70 return $self->{unapi};
75 push @{ $self->{items} }, @_;
80 return @{ $self->{items} } if (wantarray);
81 return $self->{items};
90 for my $node ($self->{doc}->findnodes($xpath)) {
91 $node->appendChild($new);
105 for my $node ($self->{doc}->findnodes($xpath)) {
106 my $new = $self->{doc}->createElement($name) if (!$ns);
107 $new = $self->{doc}->createElementNS($ns,$name) if ($ns);
109 $new->appendChild( $self->{doc}->createTextNode( $text ) )
113 for my $key (keys %$attrs) {
114 $new->setAttribute( $key => $$attrs{$key} );
118 $node->appendChild( $new );
126 my $class = ref($self) || $self;
129 my $item_xml = shift;
130 my $entry = $class->new($item_xml);
132 $entry->base($self->base);
133 $entry->unapi($self->unapi);
135 $self->push_item($entry);
141 for my $root ( $self->{doc}->findnodes($self->{item_xpath}) ) {
142 for my $item ( $self->items ) {
143 $root->appendChild( $item->{doc}->documentElement );
152 return $self->{doc}->toString(1);
161 #----------------------------------------------------------
163 package OpenILS::WWW::SuperCat::Feed::atom;
164 use base 'OpenILS::WWW::SuperCat::Feed';
168 my $self = $class->SUPER::build('<atom:feed xmlns:atom="http://www.w3.org/2005/Atom"/>');
169 $self->{type} = 'application/xml';
170 $self->{item_xpath} = '/atom:feed';
177 $self->_create_node('/atom:feed','http://www.w3.org/2005/Atom','atom:title', $text);
183 $self->_create_node('/atom:feed','http://www.w3.org/2005/Atom','atom:updated', $text);
189 $self->_create_node('/atom:feed','http://www.w3.org/2005/Atom','atom:author');
190 $self->_create_node('/atom:feed/atom:author', 'http://www.w3.org/2005/Atom','atom:name', $text);
197 my $mime = shift || "application/x-$type+xml";
200 $type = 'self' if ($type eq 'atom');
204 'http://www.w3.org/2005/Atom',
219 $self->_create_node( '/atom:feed', 'http://www.w3.org/2005/Atom', 'atom:id', $id );
222 package OpenILS::WWW::SuperCat::Feed::atom::item;
223 use base 'OpenILS::WWW::SuperCat::Feed::atom';
228 my $self = $class->SUPER::build($xml);
229 $self->{doc}->documentElement->setNamespace('http://www.w3.org/2005/Atom', 'atom');
230 $self->{item_xpath} = '/atom:entry';
231 $self->{type} = 'application/xml';
236 #----------------------------------------------------------
238 package OpenILS::WWW::SuperCat::Feed::rss2;
239 use base 'OpenILS::WWW::SuperCat::Feed';
243 my $self = $class->SUPER::build('<rss version="2.0"><channel/></rss>');
244 $self->{type} = 'application/xml';
245 $self->{item_xpath} = '/rss/channel';
252 $self->_create_node('/rss/channel',undef,'title', $text);
258 $self->_create_node('/rss/channel',undef,'lastBuildDate', $text);
264 $self->_create_node('/rss/channel', undef,'generator', $text);
271 my $mime = shift || "application/x-$type+xml";
273 $type = 'self' if ($type eq 'rss2');
286 package OpenILS::WWW::SuperCat::Feed::rss2::item;
287 use base 'OpenILS::WWW::SuperCat::Feed::rss2';
292 my $self = $class->SUPER::build($xml);
293 $self->{type} = 'application/xml';
294 $self->{item_xpath} = '/item';
299 #----------------------------------------------------------
301 package OpenILS::WWW::SuperCat::Feed::mods;
302 use base 'OpenILS::WWW::SuperCat::Feed';
306 my $self = $class->SUPER::build('<mods:modsCollection version="3.0" xmlns:mods="http://www.loc.gov/mods/"/>');
307 $self->{type} = 'application/xml';
308 $self->{item_xpath} = '/mods:modsCollection';
312 package OpenILS::WWW::SuperCat::Feed::mods::item;
313 use base 'OpenILS::WWW::SuperCat::Feed::mods';
318 my $self = $class->SUPER::build($xml);
319 $self->{doc}->documentElement->setNamespace('http://www.loc.gov/mods/', 'mods');
320 $self->{type} = 'application/xml';
331 if ($type eq 'unapi' || $type eq 'opac') {
334 'http://www.loc.gov/mods/',
337 { type => 'otherFormat', id => 'link-'.$linkid }
340 "mods:mods/mods:relatedItem[\@id='link-$linkid']",
341 'http://www.loc.gov/mods/',
342 'mods:recordIdentifier',
350 #----------------------------------------------------------
352 package OpenILS::WWW::SuperCat::Feed::mods3;
353 use base 'OpenILS::WWW::SuperCat::Feed';
357 my $self = $class->SUPER::build('<mods:modsCollection version="3.0" xmlns:mods="http://www.loc.gov/mods/v3"/>');
358 $self->{type} = 'application/xml';
359 $self->{item_xpath} = '/mods:modsCollection';
363 package OpenILS::WWW::SuperCat::Feed::mods3::item;
364 use base 'OpenILS::WWW::SuperCat::Feed::mods3';
369 my $self = $class->SUPER::build($xml);
370 $self->{doc}->documentElement->setNamespace('http://www.loc.gov/mods/v3', 'mods');
371 $self->{type} = 'application/xml';
376 #----------------------------------------------------------
378 package OpenILS::WWW::SuperCat::Feed::marcxml;
379 use base 'OpenILS::WWW::SuperCat::Feed';
383 my $self = $class->SUPER::build('<marc:collection xmlns:marc="http://www.loc.gov/MARC21/slim"/>');
384 $self->{type} = 'application/xml';
385 $self->{item_xpath} = '/marc:collection';
389 package OpenILS::WWW::SuperCat::Feed::marcxml::item;
390 use base 'OpenILS::WWW::SuperCat::Feed::marcxml';
395 my $self = $class->SUPER::build($xml);
396 $self->{doc}->documentElement->setNamespace('http://www.loc.gov/MARC21/slim', 'marc');
397 $self->{type} = 'application/xml';
401 #----------------------------------------------------------
403 package OpenILS::WWW::SuperCat::Feed::html;
404 use base 'OpenILS::WWW::SuperCat::Feed::atom';
408 my $self = $class->SUPER::new;
409 $self->type('text/html');
413 our ($_parser, $_xslt, $xslt_file);
417 my $base = $self->base || '';
418 my $root = $self->root || '';
419 my $search = $self->search || '';
420 my $lib = $self->lib || '-';
424 $_parser ||= new XML::LibXML;
425 $_xslt ||= new XML::LibXSLT;
428 OpenSRF::Utils::SettingsClient
430 ->config_value( dirs => 'xsl' ).
433 # parse the MODS xslt ...
434 my $atom2html_xslt = $_xslt->parse_stylesheet( $_parser->parse_file($xslt_file) );
436 my $new_doc = $atom2html_xslt->transform(
438 base_dir => "'$root'",
440 searchTerms => "'$search'",
443 return $new_doc->toString(1);
447 package OpenILS::WWW::SuperCat::Feed::html::item;
448 use base 'OpenILS::WWW::SuperCat::Feed::atom::item';
450 #----------------------------------------------------------
452 package OpenILS::WWW::SuperCat::Feed::htmlcard;
453 use base 'OpenILS::WWW::SuperCat::Feed::marcxml';
457 my $self = $class->SUPER::new;
458 $self->type('text/html');
462 our ($_parser, $_xslt, $xslt_file);
466 my $base = $self->base || '';
467 my $root = $self->root || '';
468 my $search = $self->search || '';
469 my $lib = $self->lib || '-';
473 $_parser ||= new XML::LibXML;
474 $_xslt ||= new XML::LibXSLT;
477 OpenSRF::Utils::SettingsClient
479 ->config_value( dirs => 'xsl' ).
480 "/MARC21slim2HTMLCard.xsl";
482 # parse the MODS xslt ...
483 my $atom2html_xslt = $_xslt->parse_stylesheet( $_parser->parse_file($xslt_file) );
485 my $new_doc = $atom2html_xslt->transform(
487 base_dir => "'$root'",
489 searchTerms => "'$search'",
492 return $new_doc->toString(1);
496 package OpenILS::WWW::SuperCat::Feed::htmlcard::item;
497 use base 'OpenILS::WWW::SuperCat::Feed::marcxml::item';