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 2007 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/opensearchdescription/1.0/' ) {
169 $self->ns( 'http://a9.com/-/spec/opensearchrss/1.0/' );
174 ( $version ) = $ns =~ m{([^/]+)/?$};
176 $self->version( $version );
178 for my $column ( @columns ) {
179 my $node = $doc->documentElement->getChildrenByTagName( $column ) or next;
180 if( $column eq 'Url' ) {
181 if( $version eq '1.0' ) {
182 $self->Url( [ WWW::OpenSearch::Url->new( template => $node->string_value, type => 'application/rss+xml', ns => $self->ns ) ] );
187 for my $urlnode ( $node->get_nodelist ) {
188 my $type = $urlnode->getAttributeNode( 'type' )->value;
189 my $url = $urlnode->getAttributeNode( 'template' )->value;
190 $url =~ s/\?}/}/g; # optional
191 my $method = $urlnode->getAttributeNode( 'method' );
192 $method = $method->value if $method;
195 for( $urlnode->getChildrenByTagName( 'Param' ) ) {
196 my $param = $_->getAttributeNode( 'name' )->value;
197 my $value = $_->getAttributeNode( 'value' )->value;
198 $value =~ s/\?}/}/g; # optional
199 $params{ $param } = $value;
202 push @url, WWW::OpenSearch::Url->new( template => $url, type => $type, method => $method, params => \%params, ns => $self->ns );
206 elsif( $version eq '1.1' and $column eq 'Query' ) {
207 my $queries = $self->query || [];
209 for my $node ( $node->get_nodelist ) {
210 my $query = WWW::OpenSearch::Query->new( {
211 map { $_ => $node->getAttributeNode( $_ )->value } qw( role searchTerms )
214 push @$queries, $query;
217 $self->query( $queries );
219 elsif( $version eq '1.1' and $column eq 'Image' ) {
220 my $images = $self->image || [];
222 for my $node ( $node->get_nodelist ) {
223 my $image = WWW::OpenSearch::Image->new( {
224 ( map { my $attr = $node->getAttributeNode( $_ ); $attr ? ($_ => $attr->value) : () } qw( height width type ) ),
225 url => $node->string_value
228 push @$images, $image;
231 $self->image( $images );
234 $self->$column( $node->string_value );
242 return $self->get_url_by_type( 'application/atom+xml' )
243 || $self->get_url_by_type( 'application/rss+xml' )
244 || $self->get_url_by_type( 'text/xml' )
245 || $self->url->[ 0 ];
248 sub get_url_by_type {
253 return $_ if $_->type eq $type;
261 return @{ $self->url };