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/$type+xml";
185 $type = 'self' if ($type eq 'atom');
189 'http://www.w3.org/2005/Atom',
203 $self->_create_node( '/atom:feed', 'http://www.w3.org/2005/Atom', 'atom:id', $id );
206 package OpenILS::WWW::SuperCat::Feed::atom::item;
207 use base 'OpenILS::WWW::SuperCat::Feed::atom';
212 my $self = $class->SUPER::build($xml);
213 $self->{doc}->documentElement->setNamespace('http://www.w3.org/2005/Atom', 'atom');
214 $self->{item_xpath} = '/atom:entry';
215 $self->{type} = 'application/xml';
220 #----------------------------------------------------------
222 package OpenILS::WWW::SuperCat::Feed::rss2;
223 use base 'OpenILS::WWW::SuperCat::Feed';
227 my $self = $class->SUPER::build('<rss version="2.0"><channel/></rss>');
228 $self->{type} = 'application/xml';
229 $self->{item_xpath} = '/rss/channel';
236 $self->_create_node('/rss/channel',undef,'title', $text);
242 $self->_create_node('/rss/channel',undef,'lastBuildDate', $text);
248 $self->_create_node('/rss/channel', undef,'generator', $text);
255 my $mime = shift || "application/$type+xml";
257 $type = 'self' if ($type eq 'rss2');
270 package OpenILS::WWW::SuperCat::Feed::rss2::item;
271 use base 'OpenILS::WWW::SuperCat::Feed::rss2';
276 my $self = $class->SUPER::build($xml);
277 $self->{type} = 'application/xml';
278 $self->{item_xpath} = '/item';
283 #----------------------------------------------------------
285 package OpenILS::WWW::SuperCat::Feed::mods;
286 use base 'OpenILS::WWW::SuperCat::Feed';
290 my $self = $class->SUPER::build('<mods:modsCollection version="3.0" xmlns:mods="http://www.loc.gov/mods/"/>');
291 $self->{type} = 'application/xml';
292 $self->{item_xpath} = '/mods:modsCollection';
296 package OpenILS::WWW::SuperCat::Feed::mods::item;
297 use base 'OpenILS::WWW::SuperCat::Feed::mods';
302 my $self = $class->SUPER::build($xml);
303 $self->{doc}->documentElement->setNamespace('http://www.loc.gov/mods/', 'mods');
304 $self->{type} = 'application/xml';
315 if ($type eq 'unapi' || $type eq 'opac') {
318 'http://www.loc.gov/mods/',
321 { type => 'otherFormat', id => 'link-'.$linkid }
324 "mods:mods/mods:relatedItem[\@id='link-$linkid']",
325 'http://www.loc.gov/mods/',
326 'mods:recordIdentifier',
334 #----------------------------------------------------------
336 package OpenILS::WWW::SuperCat::Feed::marcxml;
337 use base 'OpenILS::WWW::SuperCat::Feed';
341 my $self = $class->SUPER::build('<marc:collection xmlns:marc="http://www.loc.gov/MARC21/slim"/>');
342 $self->{type} = 'application/xml';
343 $self->{item_xpath} = '/marc:collection';
347 package OpenILS::WWW::SuperCat::Feed::marcxml::item;
348 use base 'OpenILS::WWW::SuperCat::Feed::marcxml';
353 my $self = $class->SUPER::build($xml);
354 $self->{doc}->documentElement->setNamespace('http://www.loc.gov/MARC21/slim', 'marc');
355 $self->{type} = 'application/xml';
359 #----------------------------------------------------------
361 package OpenILS::WWW::SuperCat::Feed::html;
362 use base 'OpenILS::WWW::SuperCat::Feed::atom';
366 my $self = $class->SUPER::new;
367 $self->{type} = 'application/xhtml+xml';
371 our ($_parser, $_xslt, $xslt_file);
375 my $base = $self->base;
376 my $root = $self->root;
380 $_parser ||= new XML::LibXML;
381 $_xslt ||= new XML::LibXSLT;
384 OpenSRF::Utils::SettingsClient
386 ->config_value( dirs => 'xsl' ).
389 # parse the MODS xslt ...
390 my $atom2html_xslt = $_xslt->parse_stylesheet( $_parser->parse_file($xslt_file) );
392 my $new_doc = $atom2html_xslt->transform($self->{doc}, base_dir => "'$root'");
393 return $new_doc->toString(1);
397 package OpenILS::WWW::SuperCat::Feed::html::item;
398 use base 'OpenILS::WWW::SuperCat::Feed::atom::item';