8.2 Custom Plug-ins


Referring to the order of execution introduced earlier, you can see that plug-ins are the first AxKit components to run after the ConfigReader is created. This makes a plug-in the most logical place to put custom code that alters AxKit's processing behavior based on data from the user 's request. In fact, the StyleChooser and MediaChooser modules that ship with AxKit are really only plug-ins with conventional names that hope to provide a clue about their intended use.

If the default ConfigReader class is used, plug-ins are added via the AxAddPlugin directive. Plug-ins are run in the order that they appear in the configuration file:

 AxAddPlugin Apache::AxKit::Plugin::Passthru # Allow Cookie-based style selection the 'forums' directory <Directory forums>   AxAddPlugin Apache::AxKit::StyleChooser::Cookie </Directory> # Reset the plug-ins list and add a new set for a particular application <LocationMatch /about/contact>   AxResetPlugins   AxAddPlugin MyApp::Plugin </LocationMatch> 

8.2.1 Plug-in API

All AxKit plug-in modules must implement one function, handler( ) , which is passed an Apache::Request instance for the current client request as its sole argument. Access to this request object gives plug-ins the same power to examine and alter the incoming request as a standard mod_perl content handler. Most often, though, they are used to set one or both of the preferred_style or preferred_media properties in the request object's pnotes table that AxKit uses later to determine which styles apply while delivering the content.

Example 8-1 shows one possible way to provide a friendlier environment for a site's international or multilingual visitors by selecting a different preferred style based on the relationship between the requesting client's Accept-Language header and the host's internal language priority list.

Example 8-1. StyleChooser::AcceptLanguage
 package StyleChooser::AcceptLanguage; use strict; use Apache::Constants qw(OK); use Cookbook::LanguagePriority; sub handler {     my $r = shift;     my $lp = Cookbook::LanguagePriority->get( $r );     my $lang_header = $r->header_in('accept-language');     my $chosen_language = undef;     if ( defined( $lang_header ) ) {         $chosen_language = best_language_match( $lang_header, @{$lp->priority} );     }     $chosen_language = $r->dir_config('DefaultStyleLanguage');     # Modifies the preferred style name     # to <stylename>_<language>     my $current_style = $r->notes('preferred_style')  'default';     $r->notes('preferred_style', $current_style . '_' . $chosen_language );     return OK; } sub best_language_match {      my $accept_lang = shift;      my @priority_list = @_;      # The Schwartzian Transform in action           my @user_languages =      map $_->[0],      sort { $b->[1] <=> $a->[1] }      map {         my ( $lang, $range ) = split/\s*;\s*/, $_;         $range = defined( $range ) && $range =~ /^q=["']?(.+?)["']?$/ ?  : 1;         $lang =~ s/^(.+?)-(.+?)$//;         [ $lang, $range ];      } split /\s*,\s*/, $accept_lang;     foreach my $lang ( @user_languages ) {         for ( @priority_list ) {             return $_ if $_ eq $lang;         }     } } 1; 

With this module installed, you only need to add the appropriate AxAddPlugin directive and named styles to your configuration. Again, the named styles take the form of <stylename>_<language> that your plug-in produces.

 # Add the AcceptLanguage plug-in  AxAddPlugin StyleChooser::AcceptLanguage    # Set the default language:(referenced by the plug-in)  PerlSetVar DefaultStyleLanguage en    # Now the styles  <AxStyleName style1_en>      AxAddProcessor text/xsl /styles/style_one.xsl      AxAddProcessor text/xsl /styles/english.xsl  </AxStyleName>  <AxStyleName style1_de>      AxAddProcessor text/xsl /styles/style_one.xsl      AxAddProcessor text/xsl /styles/german.xsl  </AxStyleName> <AxStyleName style2_en>      AxAddProcessor text/xsl /styles/style_two.xsl      AxAddProcessor text/xsl /styles/english.xsl  </AxStyleName>  <AxStyleName style2_de>      AxAddProcessor text/xsl /styles/style_two.xsl      AxAddProcessor text/xsl /styles/german.xsl  </AxStyleName>  . . . 

XML Publishing with AxKit
XML Publishing with Axkit
ISBN: 0596002165
EAN: 2147483647
Year: 2003
Pages: 109
Authors: Kip Hampton

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net