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->{base} = $base if ($base);
48 $self->{root} = $root if ($root);
55 $self->{unapi} = $unapi if ($unapi);
56 return $self->{unapi};
61 push @{ $self->{items} }, @_;
66 return @{ $self->{items} } if (wantarray);
67 return $self->{items};
76 for my $node ($self->{doc}->findnodes($xpath)) {
77 $node->appendChild($new);
91 for my $node ($self->{doc}->findnodes($xpath)) {
92 my $new = $self->{doc}->createElement($name) if (!$ns);
93 $new = $self->{doc}->createElementNS($ns,$name) if ($ns);
95 $new->appendChild( $self->{doc}->createTextNode( $text ) )
99 for my $key (keys %$attrs) {
100 $new->setAttribute( $key => $$attrs{$key} );
104 $node->appendChild( $new );
112 my $class = ref($self) || $self;
115 my $item_xml = shift;
116 my $entry = $class->new($item_xml);
118 $entry->base($self->base);
119 $entry->unapi($self->unapi);
121 $self->push_item($entry);
127 for my $root ( $self->{doc}->findnodes($self->{item_xpath}) ) {
128 for my $item ( $self->items ) {
129 $root->appendChild( $item->{doc}->documentElement );
138 return $self->{doc}->toString(1);
147 #----------------------------------------------------------
149 package OpenILS::WWW::SuperCat::Feed::atom;
150 use base 'OpenILS::WWW::SuperCat::Feed';
154 my $self = $class->SUPER::build('<atom:feed xmlns:atom="http://www.w3.org/2005/Atom"/>');
155 $self->{type} = 'application/xml';
156 $self->{item_xpath} = '/atom:feed';
163 $self->_create_node('/atom:feed','http://www.w3.org/2005/Atom','atom:title', $text);
169 $self->_create_node('/atom:feed','http://www.w3.org/2005/Atom','atom:updated', $text);
175 $self->_create_node('/atom:feed','http://www.w3.org/2005/Atom','atom:author');
176 $self->_create_node('/atom:feed/atom:author', 'http://www.w3.org/2005/Atom','atom:name', $text);
183 my $mime = shift || "application/x-$type+xml";
186 $type = 'self' if ($type eq 'atom');
190 'http://www.w3.org/2005/Atom',
205 $self->_create_node( '/atom:feed', 'http://www.w3.org/2005/Atom', 'atom:id', $id );
208 package OpenILS::WWW::SuperCat::Feed::atom::item;
209 use base 'OpenILS::WWW::SuperCat::Feed::atom';
214 my $self = $class->SUPER::build($xml);
215 $self->{doc}->documentElement->setNamespace('http://www.w3.org/2005/Atom', 'atom');
216 $self->{item_xpath} = '/atom:entry';
217 $self->{type} = 'application/xml';
222 #----------------------------------------------------------
224 package OpenILS::WWW::SuperCat::Feed::rss2;
225 use base 'OpenILS::WWW::SuperCat::Feed';
229 my $self = $class->SUPER::build('<rss version="2.0"><channel/></rss>');
230 $self->{type} = 'application/xml';
231 $self->{item_xpath} = '/rss/channel';
238 $self->_create_node('/rss/channel',undef,'title', $text);
244 $self->_create_node('/rss/channel',undef,'lastBuildDate', $text);
250 $self->_create_node('/rss/channel', undef,'generator', $text);
257 my $mime = shift || "application/x-$type+xml";
259 $type = 'self' if ($type eq 'rss2');
272 package OpenILS::WWW::SuperCat::Feed::rss2::item;
273 use base 'OpenILS::WWW::SuperCat::Feed::rss2';
278 my $self = $class->SUPER::build($xml);
279 $self->{type} = 'application/xml';
280 $self->{item_xpath} = '/item';
285 #----------------------------------------------------------
287 package OpenILS::WWW::SuperCat::Feed::mods;
288 use base 'OpenILS::WWW::SuperCat::Feed';
292 my $self = $class->SUPER::build('<mods:modsCollection version="3.0" xmlns:mods="http://www.loc.gov/mods/"/>');
293 $self->{type} = 'application/xml';
294 $self->{item_xpath} = '/mods:modsCollection';
298 package OpenILS::WWW::SuperCat::Feed::mods::item;
299 use base 'OpenILS::WWW::SuperCat::Feed::mods';
304 my $self = $class->SUPER::build($xml);
305 $self->{doc}->documentElement->setNamespace('http://www.loc.gov/mods/', 'mods');
306 $self->{type} = 'application/xml';
317 if ($type eq 'unapi' || $type eq 'opac') {
320 'http://www.loc.gov/mods/',
323 { type => 'otherFormat', id => 'link-'.$linkid }
326 "mods:mods/mods:relatedItem[\@id='link-$linkid']",
327 'http://www.loc.gov/mods/',
328 'mods:recordIdentifier',
336 #----------------------------------------------------------
338 package OpenILS::WWW::SuperCat::Feed::mods3;
339 use base 'OpenILS::WWW::SuperCat::Feed';
343 my $self = $class->SUPER::build('<mods:modsCollection version="3.0" xmlns:mods="http://www.loc.gov/mods/v3"/>');
344 $self->{type} = 'application/xml';
345 $self->{item_xpath} = '/mods:modsCollection';
349 package OpenILS::WWW::SuperCat::Feed::mods3::item;
350 use base 'OpenILS::WWW::SuperCat::Feed::mods3';
355 my $self = $class->SUPER::build($xml);
356 $self->{doc}->documentElement->setNamespace('http://www.loc.gov/mods/v3', 'mods');
357 $self->{type} = 'application/xml';
362 #----------------------------------------------------------
364 package OpenILS::WWW::SuperCat::Feed::marcxml;
365 use base 'OpenILS::WWW::SuperCat::Feed';
369 my $self = $class->SUPER::build('<marc:collection xmlns:marc="http://www.loc.gov/MARC21/slim"/>');
370 $self->{type} = 'application/xml';
371 $self->{item_xpath} = '/marc:collection';
375 package OpenILS::WWW::SuperCat::Feed::marcxml::item;
376 use base 'OpenILS::WWW::SuperCat::Feed::marcxml';
381 my $self = $class->SUPER::build($xml);
382 $self->{doc}->documentElement->setNamespace('http://www.loc.gov/MARC21/slim', 'marc');
383 $self->{type} = 'application/xml';
387 #----------------------------------------------------------
389 package OpenILS::WWW::SuperCat::Feed::html;
390 use base 'OpenILS::WWW::SuperCat::Feed::atom';
394 my $self = $class->SUPER::new;
395 $self->{type} = 'text/html';
399 our ($_parser, $_xslt, $xslt_file);
403 my $base = $self->base;
404 my $root = $self->root;
408 $_parser ||= new XML::LibXML;
409 $_xslt ||= new XML::LibXSLT;
412 OpenSRF::Utils::SettingsClient
414 ->config_value( dirs => 'xsl' ).
417 # parse the MODS xslt ...
418 my $atom2html_xslt = $_xslt->parse_stylesheet( $_parser->parse_file($xslt_file) );
420 my $new_doc = $atom2html_xslt->transform($self->{doc}, base_dir => "'$root'");
421 return $new_doc->toString(1);
425 package OpenILS::WWW::SuperCat::Feed::html::item;
426 use base 'OpenILS::WWW::SuperCat::Feed::atom::item';