| 1 | # HTTP Accept-Language line; section 14.4 of RFC 2616 applies here | ||
| 2 | # qvalues are from section 3.9 | ||
| 3 | # | ||
| 4 | # subclass of HTTP:Part; see that for interface requirements | ||
| 5 | # | ||
| 6 | |||
| 7 | package IDS::DataSource::HTTP::Language; | ||
| 8 | |||
| 9 | use strict; | ||
| 10 | use warnings; | ||
| 11 | use Carp qw(carp confess); | ||
| 12 | use base qw(IDS::DataSource::HTTP::Part); | ||
| 13 | |||
| 14 | $IDS::DataSource::HTTP::Language::VERSION = "1.0"; | ||
| 15 | |||
| 16 | sub empty { | ||
| 17 | my $self = shift; | ||
| 18 | undef $self->{"data"}, $self->{"tokens"}; | ||
| 19 | } | ||
| 20 | |||
| 21 | sub parse { | ||
| 22 | my $self = shift; | ||
| 23 | my $accept = $self->{"data"}; # convenience | ||
| 24 | my ($lang, $param, @tokens); | ||
| 25 | |||
| 26 | $self->mesg(1, *parse{PACKAGE} . "::parse: data '$accept'"); | ||
| 27 | |||
| 28 | # Example line: | ||
| 29 | # Accept-Language: da, en-gb;q=0.8, en;q=0.7 | ||
| 30 | my $langpat = qr'[a-zA-Z]{1,8}(-[a-zA-Z]{1,8})?'; | ||
| 31 | my $qvalpat = qr'(0(\.\d{0,3})?)|(1(\.0{0,3})?)'; | ||
| 32 | # First, split at commas | ||
| 33 | for my $langpref (split /, */, $accept) { | ||
| 34 | $langpref =~ /^([^;]+)(;.*)?/; | ||
| 35 | $lang = $1; | ||
| 36 | $param = $2; | ||
| 37 | push @tokens, $lang =~ /^($langpat)|(x-$langpat)|\*$/ | ||
| 38 | ? "Accept language: $lang" | ||
| 39 | : "Invalid accept language: $lang"; | ||
| 40 | |||
| 41 | # See if a parameter is present (e.g., qvalue) | ||
| 42 | if (defined($param)) { | ||
| 43 | my $pobj = new IDS::DataSource::HTTP::AcceptParams($self->{"params"}, $param); | ||
| 44 | push @tokens, $pobj->tokens(); | ||
| 45 | } | ||
| 46 | } | ||
| 47 | |||
| 48 | $self->mesg(2, *parse{PACKAGE} . "::parse: tokens\n ", | ||
| 49 | "\n ", \@tokens); | ||
| 50 | $self->{"tokens"} = \@tokens; | ||
| 51 | } | ||
| 52 | |||
| 53 | # accessor functions not provided by the superclass | ||
| 54 | |||
| 55 | =head1 AUTHOR INFORMATION | ||
| 56 | |||
| 57 | Copyright 2005-2007, Kenneth Ingham. All rights reserved. | ||
| 58 | |||
| 59 | This library is free software; you can redistribute it and/or modify | ||
| 60 | it under the same terms as Perl itself. | ||
| 61 | |||
| 62 | Address bug reports and comments to: ids_test at i-pi.com. When sending | ||
| 63 | bug reports, please provide the versions of IDS::Test.pm, IDS::Algorithm.pm, | ||
| 64 | IDS::DataSource.pm, the version of Perl, and the name and version of the | ||
| 65 | operating system you are using. Since Kenneth is a PhD student, the | ||
| 66 | speed of the reponse depends on how the research is proceeding. | ||
| 67 | |||
| 68 | =head1 BUGS | ||
| 69 | |||
| 70 | Please report them. | ||
| 71 | |||
| 72 | =head1 SEE ALSO | ||
| 73 | |||
| 74 | L<IDS::Algorithm>, L<IDS::DataSource> | ||
| 75 | |||
| 76 | =cut | ||
| 77 | |||
| 78 | 1; |