Imported Upstream version 0.11
[libwww-opensearch-perl.git] / lib / WWW / OpenSearch / Description.pm
1 package WWW::OpenSearch::Description;
2
3 use strict;
4 use warnings;
5
6 use base qw( Class::Accessor::Fast );
7
8 use Carp;
9 use XML::LibXML;
10 use WWW::OpenSearch::Url;
11 use WWW::OpenSearch::Query;
12 use WWW::OpenSearch::Image;
13
14 my @columns = qw(
15     AdultContent Contact     Description      Developer
16     Format       Image       LongName         Query
17     SampleSearch ShortName   SyndicationRight Tags
18     Url          Attribution InputEncoding    OutputEncoding
19     Language
20 );
21
22 __PACKAGE__->mk_accessors( qw( version ns ), map { lc } @columns );
23
24 =head1 NAME
25
26 WWW::OpenSearch::Description - Encapsulate an OpenSearch Description
27 provided by an A9 OpenSearch compatible engine
28
29 =head1 SYNOPSIS
30     
31     use WWW::OpenSearch;
32     
33     my $url = "http://bulkfeeds.net/opensearch.xml";
34     my $engine = WWW::OpenSearch->new($url);
35     my $description = $engine->description;
36     
37     my $format   = $description->Format;   # or $description->format
38     my $longname = $description->LongName; # or $description->longname
39     
40 =head1 DESCRIPTION
41
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.
45
46 =head1 CONSTRUCTOR
47
48 =head2 new( [ $xml ] )
49
50 Constructs a new instance of WWW::OpenSearch::Description. If scalar
51 parameter $xml is provided, data will be automatically loaded from it
52 using load( $xml ).
53
54 =head1 METHODS
55
56 =head2 load( $xml )
57
58 Loads description data by parsing provided argument using XML::LibXML.
59
60 =head2 urls( )
61
62 Return all of the urls associated with this description in an array.
63
64 =head2 get_best_url( )
65
66 Attempts to retrieve the best URL associated with this description, based
67 on the following content types (from most preferred to least preferred):
68
69 =over 4
70
71 =item * application/atom+xml
72
73 =item * application/rss+xml
74
75 =item * text/xml
76
77 =back
78
79 =head2 get_url_by_type( $type )
80
81 Retrieves the first WWW::OpenSearch::URL associated with this description
82 whose type is equal to $type.
83
84 =head1 ACCESSORS
85
86 =head2 version( )
87
88 =head2 ns( )
89
90 =head2 AdultContent( )
91
92 =head2 Contact( )
93
94 =head2 Description( )
95
96 =head2 Developer( )
97
98 =head2 Format( )
99
100 =head2 Image( )
101
102 =head2 LongName( )
103
104 =head2 Query( )
105
106 =head2 SampleSearch( )
107
108 =head2 ShortName( )
109
110 =head2 SyndicationRight( )
111
112 =head2 Tags( )
113
114 =head2 Url( )
115
116 =head1 AUTHOR
117
118 =over 4
119
120 =item * Tatsuhiko Miyagawa E<lt>miyagawa@bulknews.netE<gt>
121
122 =item * Brian Cassidy E<lt>bricas@cpan.orgE<gt>
123
124 =back
125
126 =head1 COPYRIGHT AND LICENSE
127
128 Copyright 2007 by Tatsuhiko Miyagawa and Brian Cassidy
129
130 This library is free software; you can redistribute it and/or modify
131 it under the same terms as Perl itself. 
132
133 =cut
134
135 for( @columns ) {
136     no strict 'refs';
137     my $col = lc;
138     *$_ = \&$col;
139 }
140
141 sub new {
142     my $class = shift;
143     my $xml   = shift;
144     
145     my $self  = $class->SUPER::new;
146     
147     eval{ $self->load( $xml ); } if $xml;
148     if( $@ ) {
149         croak "Error while parsing Description XML: $@";
150     }
151
152     return $self;
153 }
154
155 sub load {
156     my $self = shift;
157     my $xml  = shift;
158     
159     my $parser   = XML::LibXML->new;
160     my $doc      = $parser->parse_string( $xml );
161     my $element  = $doc->documentElement;
162     my $nodename = $element->nodeName;
163
164     croak "Node should be OpenSearchDescription: $nodename" if $nodename ne 'OpenSearchDescription';
165
166     my $ns = $element->getNamespace->value;
167     my $version;
168     if( $ns eq 'http://a9.com/-/spec/opensearchdescription/1.0/' ) {
169         $self->ns( 'http://a9.com/-/spec/opensearchrss/1.0/' );
170         $version = '1.0';
171     }
172     else {
173         $self->ns( $ns );
174         ( $version ) = $ns =~ m{([^/]+)/?$};
175     }
176     $self->version( $version );
177
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 ) ] );
183                 next;
184             }
185
186             my @url;
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;
193
194                 my %params;
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;
200                 }
201
202                 push @url, WWW::OpenSearch::Url->new( template => $url, type => $type, method => $method, params => \%params, ns => $self->ns );
203             }
204             $self->Url( \@url );
205         }
206         elsif( $version eq '1.1' and $column eq 'Query' ) {
207             my $queries = $self->query || [];
208
209             for my $node ( $node->get_nodelist ) {
210                 my $query = WWW::OpenSearch::Query->new( {
211                     map { $_ => $node->getAttributeNode( $_ )->value } qw( role searchTerms )
212                 } );
213
214                 push @$queries, $query;
215             }
216
217             $self->query( $queries );
218         }
219         elsif( $version eq '1.1' and $column eq 'Image' ) {
220             my $images = $self->image || [];
221
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
226                 } );
227
228                 push @$images, $image;
229             }
230
231             $self->image( $images );
232         }
233         else {
234             $self->$column( $node->string_value );
235         }
236     }
237 }
238
239 sub get_best_url {
240     my $self = shift;
241     
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 ];
246 }
247
248 sub get_url_by_type {
249     my $self = shift;
250     my $type = shift;
251     
252     for( $self->urls ) {
253         return $_ if $_->type eq $type;
254     };
255     
256     return;
257 }
258
259 sub urls {
260     my $self = shift;
261     return @{ $self->url };
262 }
263
264 1;