1 package WWW::OpenSearch::Description;
\r
6 use base qw( Class::Accessor::Fast );
\r
10 use WWW::OpenSearch::Url;
\r
13 AdultContent Contact Description Developer
\r
14 Format Image LongName Query
\r
15 SampleSearch ShortName SyndicationRight Tags
\r
16 Url Attribution InputEncoding OutputEncoding
20 __PACKAGE__->mk_accessors( qw( version ns ), map { lc } @columns );
\r
24 WWW::OpenSearch::Description - Encapsulate an OpenSearch Description
\r
25 provided by an A9 OpenSearch compatible engine
\r
29 use WWW::OpenSearch;
\r
31 my $url = "http://bulkfeeds.net/opensearch.xml";
\r
32 my $engine = WWW::OpenSearch->new($url);
\r
33 my $description = $engine->description;
\r
35 my $format = $description->Format; # or $description->format
\r
36 my $longname = $description->LongName; # or $description->longname
\r
40 WWW::OpenSearch::Description is a module designed to encapsulate an
\r
41 OpenSearch Description provided by an A9 OpenSearch compatible engine.
\r
42 See http://opensearch.a9.com/spec/1.1/description/ for details.
\r
46 =head2 new( [ $xml ] )
\r
48 Constructs a new instance of WWW::OpenSearch::Description. If scalar
\r
49 parameter $xml is provided, data will be automatically loaded from it
\r
56 Loads description data by parsing provided argument using XML::LibXML.
60 Return all of the urls associated with this description in an array.
\r
62 =head2 get_best_url( )
\r
64 Attempts to retrieve the best URL associated with this description, based
\r
65 on the following content types (from most preferred to least preferred):
\r
69 =item * application/atom+xml
\r
71 =item * application/rss+xml
\r
77 =head2 get_url_by_type( $type )
\r
79 Retrieves the first WWW::OpenSearch::URL associated with this description
\r
80 whose type is equal to $type.
\r
88 =head2 AdultContent( )
\r
92 =head2 Description( )
\r
104 =head2 SampleSearch( )
\r
106 =head2 ShortName( )
\r
108 =head2 SyndicationRight( )
\r
118 =item * Tatsuhiko Miyagawa E<lt>miyagawa@bulknews.netE<gt>
\r
120 =item * Brian Cassidy E<lt>bricas@cpan.orgE<gt>
\r
124 =head1 COPYRIGHT AND LICENSE
\r
126 Copyright 2006 by Tatsuhiko Miyagawa and Brian Cassidy
\r
128 This library is free software; you can redistribute it and/or modify
\r
129 it under the same terms as Perl itself.
\r
143 my $self = $class->SUPER::new;
\r
145 eval{ $self->load( $xml ); } if $xml;
\r
147 croak "Error while parsing Description XML: $@";
\r
157 my $parser = XML::LibXML->new;
\r
158 my $doc = $parser->parse_string( $xml );
\r
159 my $element = $doc->documentElement;
\r
160 my $nodename = $element->nodeName;
\r
162 croak "Node should be OpenSearchDescription: $nodename" if $nodename ne 'OpenSearchDescription';
\r
164 my $ns = $element->getNamespace->value;
\r
166 if( $ns eq 'http://a9.com/-/spec/opensearch/1.1/' ) {
\r
173 $self->version( $version );
\r
175 for my $column ( @columns ) {
\r
176 my $node = $doc->documentElement->getChildrenByTagName( $column ) or next;
\r
177 if( $column eq 'Url' ) {
\r
178 if( $version eq '1.0' ) {
\r
179 $self->Url( [ WWW::OpenSearch::Url->new( template => $node->string_value, type => 'application/rss+xml' ) ] );
\r
184 for my $urlnode ( $node->get_nodelist ) {
\r
185 my $type = $urlnode->getAttributeNode( 'type' )->value;
\r
186 my $url = $urlnode->getAttributeNode( 'template' )->value;
\r
187 $url =~ s/\?}/}/g; # optional
\r
188 my $method = $urlnode->getAttributeNode( 'method' );
\r
189 $method = $method->value if $method;
\r
192 for( $urlnode->getChildrenByTagName( 'Param' ) ) {
\r
193 my $param = $_->getAttributeNode( 'name' )->value;
\r
194 my $value = $_->getAttributeNode( 'value' )->value;
195 $value =~ s/\?}/}/g; # optional
\r
196 $params{ $param } = $value;
\r
199 push @url, WWW::OpenSearch::Url->new( template => $url, type => $type, method => $method, params => \%params );
\r
201 $self->Url( \@url );
\r
203 elsif( $version eq '1.1' and $column eq 'Query' ) {
\r
204 my $query = ( $node->get_nodelist )[ 0 ];
\r
205 next if $query->getAttributeNode( 'role' )->value eq 'example';
\r
206 $self->SampleSearch( $query->getAttributeNode( 'searchTerms' )->value );
\r
208 elsif( $version eq '1.0' and $column eq 'Format' ) {
\r
209 $self->Format( $node->string_value );
\r
210 $self->ns( $self->Format );
\r
213 $self->$column( $node->string_value );
\r
221 return $self->get_url_by_type( 'application/atom+xml' )
\r
222 || $self->get_url_by_type( 'application/rss+xml' )
\r
223 || $self->get_url_by_type( 'text/xml' )
\r
224 || $self->url->[ 0 ];
\r
227 sub get_url_by_type {
\r
232 for( $self->urls ) {
\r
233 $template = $_ if $_->type eq $type;
\r
242 return @{ $self->url };