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( )
102 =head2 InputEncoding( )
110 =head2 OutputEncoding( )
114 =head2 SampleSearch( )
118 =head2 SyndicationRight( )
128 =item * Tatsuhiko Miyagawa E<lt>miyagawa@bulknews.netE<gt>
130 =item * Brian Cassidy E<lt>bricas@cpan.orgE<gt>
134 =head1 COPYRIGHT AND LICENSE
136 Copyright 2005-2010 by Tatsuhiko Miyagawa and Brian Cassidy
138 This library is free software; you can redistribute it and/or modify
139 it under the same terms as Perl itself.
153 my $self = $class->SUPER::new;
155 eval { $self->load( $xml ); } if $xml;
157 croak "Error while parsing Description XML: $@";
167 my $parser = XML::LibXML->new;
168 my $doc = $parser->parse_string( $xml );
169 my $element = $doc->documentElement;
170 my $nodename = $element->nodeName;
172 croak "Node should be OpenSearchDescription: $nodename"
173 if $nodename ne 'OpenSearchDescription';
175 my $ns = $element->getNamespace->value;
177 if ( $ns eq 'http://a9.com/-/spec/opensearchdescription/1.0/' ) {
178 $self->ns( 'http://a9.com/-/spec/opensearchrss/1.0/' );
183 ( $version ) = $ns =~ m{([^/]+)/?$};
185 $self->version( $version );
187 for my $column ( @columns ) {
188 my $node = $doc->documentElement->getChildrenByTagName( $column )
190 if ( $column eq 'Url' ) {
191 if ( $version eq '1.0' ) {
193 [ WWW::OpenSearch::Url->new(
194 template => $node->string_value,
195 type => 'application/rss+xml',
204 for my $urlnode ( $node->get_nodelist ) {
205 my $type = $urlnode->getAttributeNode( 'type' )->value;
206 my $url = $urlnode->getAttributeNode( 'template' )->value;
207 $url =~ s/\?}/}/g; # optional
208 my $method = $urlnode->getAttributeNode( 'method' );
209 $method = $method->value if $method;
212 for ( $urlnode->getChildrenByTagName( 'Param' ) ) {
213 my $param = $_->getAttributeNode( 'name' )->value;
214 my $value = $_->getAttributeNode( 'value' )->value;
215 $value =~ s/\?}/}/g; # optional
216 $params{ $param } = $value;
220 WWW::OpenSearch::Url->new(
230 elsif ( $version eq '1.1' and $column eq 'Query' ) {
231 my $queries = $self->query || [];
233 for my $node ( $node->get_nodelist ) {
234 my $query = WWW::OpenSearch::Query->new(
235 { map { $_ => $node->getAttributeNode( $_ )->value }
236 qw( role searchTerms )
240 push @$queries, $query;
243 $self->query( $queries );
245 elsif ( $version eq '1.1' and $column eq 'Image' ) {
246 my $images = $self->image || [];
248 for my $node ( $node->get_nodelist ) {
249 my $image = WWW::OpenSearch::Image->new(
251 my $attr = $node->getAttributeNode( $_ );
252 $attr ? ( $_ => $attr->value ) : ()
253 } qw( height width type )
255 url => $node->string_value
259 push @$images, $image;
262 $self->image( $images );
265 $self->$column( $node->string_value );
274 $self->get_url_by_type( 'application/atom+xml' )
275 || $self->get_url_by_type( 'application/rss+xml' )
276 || $self->get_url_by_type( 'text/xml' )
277 || $self->url->[ 0 ];
280 sub get_url_by_type {
284 for ( $self->urls ) {
285 return $_ if $_->type eq $type;
293 return @{ $self->url };