1 package WWW::OpenSearch::Description;
6 use base qw( Class::Accessor::Fast );
10 use WWW::OpenSearch::Url;
11 use WWW::OpenSearch::Query;
12 use WWW::OpenSearch::Image;
15 AdultContent Contact Description Developer
16 Format Image LongName Query
17 SampleSearch ShortName SyndicationRight Tags
18 Url Attribution InputEncoding OutputEncoding
22 __PACKAGE__->mk_accessors( qw( version ns ), map { lc } @columns );
26 WWW::OpenSearch::Description - Encapsulate an OpenSearch Description
27 provided by an A9 OpenSearch compatible engine
33 my $url = "http://bulkfeeds.net/opensearch.xml";
34 my $engine = WWW::OpenSearch->new($url);
35 my $description = $engine->description;
37 my $format = $description->Format; # or $description->format
38 my $longname = $description->LongName; # or $description->longname
42 WWW::OpenSearch::Description is a module designed to encapsulate an
43 OpenSearch Description provided by an A9 OpenSearch compatible engine.
44 See http://opensearch.a9.com/spec/1.1/description/ for details.
48 =head2 new( [ $xml ] )
50 Constructs a new instance of WWW::OpenSearch::Description. If scalar
51 parameter $xml is provided, data will be automatically loaded from it
58 Loads description data by parsing provided argument using XML::LibXML.
62 Return all of the urls associated with this description in an array.
64 =head2 get_best_url( )
66 Attempts to retrieve the best URL associated with this description, based
67 on the following content types (from most preferred to least preferred):
71 =item * application/atom+xml
73 =item * application/rss+xml
79 =head2 get_url_by_type( $type )
81 Retrieves the first WWW::OpenSearch::URL associated with this description
82 whose type is equal to $type.
90 =head2 AdultContent( )
106 =head2 SampleSearch( )
110 =head2 SyndicationRight( )
120 =item * Tatsuhiko Miyagawa E<lt>miyagawa@bulknews.netE<gt>
122 =item * Brian Cassidy E<lt>bricas@cpan.orgE<gt>
126 =head1 COPYRIGHT AND LICENSE
128 Copyright 2006 by Tatsuhiko Miyagawa and Brian Cassidy
130 This library is free software; you can redistribute it and/or modify
131 it under the same terms as Perl itself.
145 my $self = $class->SUPER::new;
147 eval{ $self->load( $xml ); } if $xml;
149 croak "Error while parsing Description XML: $@";
159 my $parser = XML::LibXML->new;
160 my $doc = $parser->parse_string( $xml );
161 my $element = $doc->documentElement;
162 my $nodename = $element->nodeName;
164 croak "Node should be OpenSearchDescription: $nodename" if $nodename ne 'OpenSearchDescription';
166 my $ns = $element->getNamespace->value;
168 if( $ns eq 'http://a9.com/-/spec/opensearch/1.1/' ) {
175 $self->version( $version );
177 for my $column ( @columns ) {
178 my $node = $doc->documentElement->getChildrenByTagName( $column ) or next;
179 if( $column eq 'Url' ) {
180 if( $version eq '1.0' ) {
181 $self->Url( [ WWW::OpenSearch::Url->new( template => $node->string_value, type => 'application/rss+xml' ) ] );
186 for my $urlnode ( $node->get_nodelist ) {
187 my $type = $urlnode->getAttributeNode( 'type' )->value;
188 my $url = $urlnode->getAttributeNode( 'template' )->value;
189 $url =~ s/\?}/}/g; # optional
190 my $method = $urlnode->getAttributeNode( 'method' );
191 $method = $method->value if $method;
194 for( $urlnode->getChildrenByTagName( 'Param' ) ) {
195 my $param = $_->getAttributeNode( 'name' )->value;
196 my $value = $_->getAttributeNode( 'value' )->value;
197 $value =~ s/\?}/}/g; # optional
198 $params{ $param } = $value;
201 push @url, WWW::OpenSearch::Url->new( template => $url, type => $type, method => $method, params => \%params );
205 elsif( $version eq '1.1' and $column eq 'Query' ) {
206 my $queries = $self->query || [];
208 for my $node ( $node->get_nodelist ) {
209 my $query = WWW::OpenSearch::Query->new( {
210 map { $_ => $node->getAttributeNode( $_ )->value } qw( role searchTerms )
213 push @$queries, $query;
216 $self->query( $queries );
218 elsif( $version eq '1.1' and $column eq 'Image' ) {
219 my $images = $self->image || [];
221 for my $node ( $node->get_nodelist ) {
222 my $image = WWW::OpenSearch::Image->new( {
223 ( map { my $attr = $node->getAttributeNode( $_ ); $attr ? ($_ => $attr->value) : () } qw( height width type ) ),
224 url => $node->string_value
227 push @$images, $image;
230 $self->image( $images );
232 elsif( $version eq '1.0' and $column eq 'Format' ) {
233 $self->Format( $node->string_value );
234 $self->ns( $self->Format );
237 $self->$column( $node->string_value );
245 return $self->get_url_by_type( 'application/atom+xml' )
246 || $self->get_url_by_type( 'application/rss+xml' )
247 || $self->get_url_by_type( 'text/xml' )
248 || $self->url->[ 0 ];
251 sub get_url_by_type {
257 $template = $_ if $_->type eq $type;
266 return @{ $self->url };