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->{lib} = $lib if ($lib);
48 $self->{base} = $base if ($base);
55 $self->{root} = $root if ($root);
62 $self->{unapi} = $unapi if ($unapi);
63 return $self->{unapi};
68 push @{ $self->{items} }, @_;
73 return @{ $self->{items} } if (wantarray);
74 return $self->{items};
83 for my $node ($self->{doc}->findnodes($xpath)) {
84 $node->appendChild($new);
98 for my $node ($self->{doc}->findnodes($xpath)) {
99 my $new = $self->{doc}->createElement($name) if (!$ns);
100 $new = $self->{doc}->createElementNS($ns,$name) if ($ns);
102 $new->appendChild( $self->{doc}->createTextNode( $text ) )
106 for my $key (keys %$attrs) {
107 $new->setAttribute( $key => $$attrs{$key} );
111 $node->appendChild( $new );
119 my $class = ref($self) || $self;
122 my $item_xml = shift;
123 my $entry = $class->new($item_xml);
125 $entry->base($self->base);
126 $entry->unapi($self->unapi);
128 $self->push_item($entry);
134 for my $root ( $self->{doc}->findnodes($self->{item_xpath}) ) {
135 for my $item ( $self->items ) {
136 $root->appendChild( $item->{doc}->documentElement );
145 return $self->{doc}->toString(1);
154 #----------------------------------------------------------
156 package OpenILS::WWW::SuperCat::Feed::atom;
157 use base 'OpenILS::WWW::SuperCat::Feed';
161 my $self = $class->SUPER::build('<atom:feed xmlns:atom="http://www.w3.org/2005/Atom"/>');
162 $self->{type} = 'application/xml';
163 $self->{item_xpath} = '/atom:feed';
170 $self->_create_node('/atom:feed','http://www.w3.org/2005/Atom','atom:title', $text);
176 $self->_create_node('/atom:feed','http://www.w3.org/2005/Atom','atom:updated', $text);
182 $self->_create_node('/atom:feed','http://www.w3.org/2005/Atom','atom:author');
183 $self->_create_node('/atom:feed/atom:author', 'http://www.w3.org/2005/Atom','atom:name', $text);
190 my $mime = shift || "application/x-$type+xml";
193 $type = 'self' if ($type eq 'atom');
197 'http://www.w3.org/2005/Atom',
212 $self->_create_node( '/atom:feed', 'http://www.w3.org/2005/Atom', 'atom:id', $id );
215 package OpenILS::WWW::SuperCat::Feed::atom::item;
216 use base 'OpenILS::WWW::SuperCat::Feed::atom';
221 my $self = $class->SUPER::build($xml);
222 $self->{doc}->documentElement->setNamespace('http://www.w3.org/2005/Atom', 'atom');
223 $self->{item_xpath} = '/atom:entry';
224 $self->{type} = 'application/xml';
229 #----------------------------------------------------------
231 package OpenILS::WWW::SuperCat::Feed::rss2;
232 use base 'OpenILS::WWW::SuperCat::Feed';
236 my $self = $class->SUPER::build('<rss version="2.0"><channel/></rss>');
237 $self->{type} = 'application/xml';
238 $self->{item_xpath} = '/rss/channel';
245 $self->_create_node('/rss/channel',undef,'title', $text);
251 $self->_create_node('/rss/channel',undef,'lastBuildDate', $text);
257 $self->_create_node('/rss/channel', undef,'generator', $text);
264 my $mime = shift || "application/x-$type+xml";
266 $type = 'self' if ($type eq 'rss2');
279 package OpenILS::WWW::SuperCat::Feed::rss2::item;
280 use base 'OpenILS::WWW::SuperCat::Feed::rss2';
285 my $self = $class->SUPER::build($xml);
286 $self->{type} = 'application/xml';
287 $self->{item_xpath} = '/item';
292 #----------------------------------------------------------
294 package OpenILS::WWW::SuperCat::Feed::mods;
295 use base 'OpenILS::WWW::SuperCat::Feed';
299 my $self = $class->SUPER::build('<mods:modsCollection version="3.0" xmlns:mods="http://www.loc.gov/mods/"/>');
300 $self->{type} = 'application/xml';
301 $self->{item_xpath} = '/mods:modsCollection';
305 package OpenILS::WWW::SuperCat::Feed::mods::item;
306 use base 'OpenILS::WWW::SuperCat::Feed::mods';
311 my $self = $class->SUPER::build($xml);
312 $self->{doc}->documentElement->setNamespace('http://www.loc.gov/mods/', 'mods');
313 $self->{type} = 'application/xml';
324 if ($type eq 'unapi' || $type eq 'opac') {
327 'http://www.loc.gov/mods/',
330 { type => 'otherFormat', id => 'link-'.$linkid }
333 "mods:mods/mods:relatedItem[\@id='link-$linkid']",
334 'http://www.loc.gov/mods/',
335 'mods:recordIdentifier',
343 #----------------------------------------------------------
345 package OpenILS::WWW::SuperCat::Feed::mods3;
346 use base 'OpenILS::WWW::SuperCat::Feed';
350 my $self = $class->SUPER::build('<mods:modsCollection version="3.0" xmlns:mods="http://www.loc.gov/mods/v3"/>');
351 $self->{type} = 'application/xml';
352 $self->{item_xpath} = '/mods:modsCollection';
356 package OpenILS::WWW::SuperCat::Feed::mods3::item;
357 use base 'OpenILS::WWW::SuperCat::Feed::mods3';
362 my $self = $class->SUPER::build($xml);
363 $self->{doc}->documentElement->setNamespace('http://www.loc.gov/mods/v3', 'mods');
364 $self->{type} = 'application/xml';
369 #----------------------------------------------------------
371 package OpenILS::WWW::SuperCat::Feed::marcxml;
372 use base 'OpenILS::WWW::SuperCat::Feed';
376 my $self = $class->SUPER::build('<marc:collection xmlns:marc="http://www.loc.gov/MARC21/slim"/>');
377 $self->{type} = 'application/xml';
378 $self->{item_xpath} = '/marc:collection';
382 package OpenILS::WWW::SuperCat::Feed::marcxml::item;
383 use base 'OpenILS::WWW::SuperCat::Feed::marcxml';
388 my $self = $class->SUPER::build($xml);
389 $self->{doc}->documentElement->setNamespace('http://www.loc.gov/MARC21/slim', 'marc');
390 $self->{type} = 'application/xml';
394 #----------------------------------------------------------
396 package OpenILS::WWW::SuperCat::Feed::html;
397 use base 'OpenILS::WWW::SuperCat::Feed::atom';
401 my $self = $class->SUPER::new;
402 $self->{type} = 'text/html';
406 our ($_parser, $_xslt, $xslt_file);
410 my $base = $self->base;
411 my $root = $self->root;
412 my $lib = $self->lib || '-';
416 $_parser ||= new XML::LibXML;
417 $_xslt ||= new XML::LibXSLT;
420 OpenSRF::Utils::SettingsClient
422 ->config_value( dirs => 'xsl' ).
425 # parse the MODS xslt ...
426 my $atom2html_xslt = $_xslt->parse_stylesheet( $_parser->parse_file($xslt_file) );
428 my $new_doc = $atom2html_xslt->transform($self->{doc}, base_dir => "'$root'", lib => "'$lib'");
429 return $new_doc->toString(1);
433 package OpenILS::WWW::SuperCat::Feed::html::item;
434 use base 'OpenILS::WWW::SuperCat::Feed::atom::item';