NAME

CGI::XMLApplication -- Object Oriented Interface for CGI Script Applications

SYNOPSIS

use CGI::XMLApplication;

$script = new CGI::XMLApplication;
$script->setStylesheetPath( "the/path/to/the/stylesheets" );

# either this for simple scripts
$script->run();
# or if you need more control ...
$script->run(%context_hash);

DESCRIPTION

CGI::XMLApplication is a CGI application class, that intends to enable perl artists to implement CGIs that make use of XML/XSLT functionality, without taking too much care about specialized errorchecking or even care too much about XML itself. It provides the power of the XML::LibXML/ XML::LibXSLT module package for content deliverment.

As well CGI::XMLApplication is designed to support project management on code level. The class allows to split web applications into several simple parts. Through this most of the code stays simple and easy to maintain. Throughout the whole lifetime of a script CGI::XMLApplication tries to keep the application stable. As well a programmer has not to bother about some of XML::LibXML/ XML::LibXSLT transformation pitfalls.

The class module extends the CGI class. While all functionality of the original CGI package is still available, it should be not such a big problem, to port existing scripts to CGI::XMLApplication, although most functions used here are the access function for client data such as param().

CGI::XMLApplication, intended to be an application class should make writing of XML enabled CGI scripts more easy. Especially because of the use of object orientated concepts, this class enables much more transparent implemententations with complex functionality compared to what is possible with standard CGI-scripts.

The main difference with common perl CGI implementation is the fact, that the client-output is not done from perl functions, but generated by an internally build XML DOM that gets processed with an XSLT stylesheet. This fact helps to remove a lot of the HTML related functions from the core code, so a script may be much easier to read, since only application relevant code is visible, while layout related information is left out (commonly in an XSLT file).

This helps to write and test a complete application faster and less layout related. The design can be appended and customized later without effecting the application code anymore.

Since the class uses the OO paradigma, it does not force anybody to implement a real life application with the complete overhead of more or less redundant code. Since most CGI-scripts are waiting for events, which is usually the abstraction of a click of a submit button or an image, CGI::XMLApplication implements a simple event system, that allows to keep event related code as separated as possible.

Therefore final application class is not ment to have a constructor anymore. All functionality should be encapsulated into implicit or explicit event handlers. Because of a lack in Perl's OO implementation the call of a superclass constructor before the current constructor call is not default behavior in Perl. For that reason I decided to have special events to enable the application to initialize correctly, excluding the danger of leaving important variables undefined. On the other hand this forces the programmer to implement scripts more problem orientated, rather than class focused.

Another design aspect for CGI::XMLApplication is the strict differentiation between CODE and PRESENTATION. IMHO this, in fact being one of the major problems in traditional CGI programming. To implement this, the XML::LibXML and XML::LibXSLT modules are used. Each CGI Script should generate an XML-DOM, that can be processed with a given stylesheet.

Pay attention that XML-DOM means the DOM of XML::LibXML and not XML::DOM!

What are Events and how to catch them

Most CGI handle the result of HTML-Forms or similar requests from clients. Analouge to GUI Programming, CGI::XMLApplication calls this an event. Spoken in CGI/HTML-Form words, a CGI-Script handles the various situations a clients causes by pushing a submit button or follows a special link.

An event of CGI::XMLApplication has the same name as the input field, that should cause the event. The following example should illustrate this a little better:

<!-- SOME HTML CODE -->
<input type="submit" name="dummy" value="whatever" />
<!-- SOME MORE HTML :) -->

If a user clicks the submitbutton and you have registered the event name dummy for your script, CGI::XMLApplication will try to call the function event_dummy(). The script module to handle the dummy event would look something like the following code:

use CGI::XMLApplication;
@ISA = qw(CGI::XMLApplication);

sub registerEvents { qw( dummy ); } # the handler list

# ...

sub event_dummy {
   my ( $self, $context ) = @_;

   # your event code goes here

   return 0;
}

During the lifecircle of a CGI script, often the implementation starts with ordinary submit buttons, which get often changed to so called input images, to fit into the UI of the Website. CGI::XMLApplication will recognize such changes, so the code has not to be changed if the presentation of the form changes. Therefore there is no need to declare separate events for input images. E.g. an event called evname makes CGI::XMLApplication look for evname and evname.x in the querystring.

Some programmer are suspious which event CGI::XMLApplication will call. The function testEvent checks all events if one is valid and returns the name of event. Much more important is the possibility to send error events from the event_init() function. This is done with the sendEvent Function. This will set a new parameter to the CGI's querystring after removing all other events. One can only send events that are already registred!.

CGI::XMLApplication doesn't implement an event queqe yet. For GUI programmers this seems like a unnessecary restriction. I terms of CGI it makes more sense to think of a script as a program, that is only able to scan its event queqe only once during runtime. The only chance to stop the script from handling a certain event is to send a new event from the event_init() function. This function is always called at first from the run method. If another event uses the sendEvent function, the event will get lost.

method run

Being the main routine this should be the only method called by the script apart from the constructor. All events are handled inside the method run(). Since this method is extremly simple and transparent to any kind of display type, there should be no need to override this function. One can pass a context hash, to pass external or prefetched information to the application. This context will be available and acessable in all events and most extra functions.

This function does all event and serialization related work. As well there is some validation done as well, so catched events, that are not implemented, will not cause any harm.

The Event System

Commonly scripts that make use of CGI::XMLApplication, will not bother about the run function anymore. All functionality is kept inside event- and (pseudo-)callback functions. This forces one to implement much more strict code than common perl would allow. What first looks like a drawback, finally makes the code much easier to understand, maintain and finally to extend.

CGI::XMLApplication knows two types of event handlers: implicit events, common to all applications and explicit events, reflecting the application logic. The class assumes that implicit events are implemented in any case. Those events have reserved names and need not be specified through registerEvents. Since the class cannot know something about the application logic by itself, names of events have to be explicitly passed to be handled by the application. As well all event functions have to be implemented as member methods of the application class right now. Because of perls OO interface a class has to be written inside its own module.

An event may return a integer value. If the event succeeds (no fatal errors, e.g. database errors) the explicit or common event function should return a value greater or eqal than 0. If the value is less than 0, CGI::XMLApplication assumes a script panic, and will not try to render a stylesheet or DOM.

There are defined panic levels:

-1

Stylesheet missing

-2

Stylesheet not available

-3

Event not defined

-4

Application panic

Apart from Application Panic the panic levels are set internally. An Application Panic should be set if the application catches an error, that does not allow any XML/XSLT processing. This can be for example, that any required perl modules are not installed on the system.

If the selectStylesheet is not implemented the CGI::XMLApplication will assume the returned value as id to a stylesheet list set by setStylesheetList(). Basicly this is done for backward compatibility reasons. An application better implements the selectStylesheet callback, to end up with a more strict structure.

There are two ways to tell the system which events are to be handled.

You can tell the system the events the client browser sends back to the script only. CGI::XMLApplication tries to call a event handler if this happens. The function name of an event handler has to have the following format:

event_<eventname>.

E.g. event_init handles the init event described below. All events that handle client responses (including the default event) should return the position of the stylesheet in the stylesheet list passed with setStylesheetList().

method registerEvents

This method is called by the class constructor. Each application should register the events it like to handle. It should return an array of eventnames such as eg. 'remove' or 'store'. This list is used to find which event a user caused on the client side.

function testEvent

If it is nesseccary to check which event is relevant for the current script one can use this function to find out in event_init(). If this function returns undef, the default event is active, otherwise it returns the eventname as defined by registerEvents.

method addEvents LIST

addEvents() also takes a list of events the application will handle. Contrary to setEventList() this does not override previously defined events.

method sendEvent SCALAR

Sometimes it could be neccessary to send an event by your own (the script's) initiative. A possible example could be if you don't have client input but path_info data, which determinates how the script should behave or session information is missing, so the client should not even get the default output.

This can only be done during the event_init() method call. Some coders would prefer the constructor, which is not a very good idea in this case: While the constructor is running, the application is not completely initialized. This fact can be only ashured in the event_init function. Therefore all script specific errorhandling and initializing should be done there.

sendEvent only can be called from event_init, because any CGI::XMLApplication script will handle just one event, plus the init and the exit event. If sendEvent is called from another event than event_init() it will take not effect.

It is possible through sendEvent() to keep the script logic clean.

Example:

sub registerEvents { qw( missing ... ) ; }

sub event_init {
   my ( $self, $context ) = @_;
   if ( not length $self->param( $paramname ) ){
      $self->sendEvent( 'missing' );
   }
   else {

  ... some more initialization ...

   }
}

... more code ...

# event_missing is an explicit event.
sub event_missing {
   my ( $self , $context ) = @_;

   ... your error handling code goes ...

   return -4 if $panic;  # just for illustration
   return 0;
}

Implicit Events

CGI::XMLApplication knows three implicit events which are more or less independent to client responses: They are 'init', 'exit', and 'default'.

If there is need to override one of these handler -- and I hope there will be ;) -- the particular event should call the related event handler of its superclass as first action. This might be skipped, if the function should do everything right by itself. I prefere the first technique, because it is more secure and makes things easier to debug.

Each event has a single Parameter, the context. This is a hash reference, where the user can store whatever needed. This context is usefull to pass scriptwide data between callbacks and event functions around.

event_init

The init event is set before the CGI::XMLApplication tries to evaluate any of script parameters. Therefore the event_init method should be used to initialize the application.

event_exit

The event_exit method is called after all other events have been processed, but just before the rendering is done. This should be used, if you need to do something independend from all events before the data is send to the user.

event_default

This event is called as a fallback mechanism if CGI::XMLApplication did not receive a stylesheet id by an other event handler; for example if no event matched.

Extra Methods

There are some extra callbacks may implemented:

  • selectStylesheet

  • getDOM

  • registerEvents

  • setHttpHeader

  • getXSLTParameter

These methods are used by the serialization function, to create the content related datastructure. Like event functions these functions have to be implemented as class member, and like event funcitons the functions will have the context passed as the single parameter.

selectStylesheet() has to return a valid path/filename for the stylesheet requested.

getDOM() has to return the DOM later used by the stylesheet processor.

the registerEvents is slightly different implemented than other event or callback functions. It will not recieve any context data, since it is called even before the run function, that creates the context. It should return an array containing the names of the explicit events handled by the script.

setHttpHeader should return a hash of headers (but not the Content-Type). This can be used to set the nocache pragma, to set or remove cookies. The keys of the hash must be the same as the named parameters of CGI.pm's header method.

The last function getXSLTParameter is called by serialization just before the xslt processing is done. This alows to pass up to 256 parameters to the processor. This function should return a hash or undefined. The hash will be transformed to fit the XML::LibXSLT interface, so one can simply pass a hash of strings to CGI::XMLApplication.

Helperfunctions for internal use

function checkPush LIST

This function searches the query string for a parameter with the passed name. The implementation is "imagesave" meaning there is no change in the code needed, if you switch from input.type=submit to input.type=image or vv. The algorithm tests wheter a full name is found in the querystring, if not it tries tests for the name expanded by a '.x'. In context of events this function interprets each item part in the query string list as an event. Because of that, the algorithm returns only the first item matched.

If you use the event interface on this function, make sure, the HTML-forms pass unique events to the script. This is neccessary to avoid confusing behaviour.

function serialization()

This method renders the data stored in the DOM with the stylesheet returned by the event handler. You should override this function if you like to use a different way of displaying your data.

For debugging purposes the parameter passthru can be used to directly pass the stringified DOM-tree to the client. (Quite useful, as I realized. :) )

To avoid the call of serialization() one should set skipSerialization.

event_default {
   my $self = shift;
   # avoid serialization call
   $self->skipSerialization( 1 ); # use 0 to unset

   # now you can directly print to the client, but don't forget the
   # headers.

   return 0;
}

If the serialization should be skipped, CGI::XMLApplication will not print any headers. In such case the application is on its own to pass all the output.

The algorithm used by serialization is simple:

  • request the appplication DOM through getDOM()

  • get the stylesheet the application preferes through selectStylesheet()

  • parse the stylesheet

  • transform the DOM with the stylesheet

  • set Content-Type and headers

  • return the content to the client

If errors occour on a certain stage of serialization, the application is stopped and the generated error messages are returned.

method panic SCALAR

This a simple error message handler

method setPanicMsg $SCALAR

This useful method, helps to pass more specific error messages to the user. Currently this method is not very sophisticated: if the method is called twice, only the last string will be displayed.

function getPanicMsg

This method returns the panic message set by setPanicMsg().

CGI Extras

The following functions are some neat features missing in CGI.pm

function checkFields LIST

This is an easy way to test wether all required fields are filled out correctly. Called in array context the function returns the list of missing parameter. (Different to param() which returns all parameter names). In scalar context the function returns a boolean value.

function getParamHash LIST

This function is a bit better for general data processing as the standard CGI::Vars function. While Vars sets a keys for each parameter found in the query string, getFieldsAsHash returns only the requested fields (as long they aren't NULL). This is useful in scripts where the script itself handles different kind of data within the same event.

Since the function relies on Vars the returned data has the same structure Vars returns.

some extra functions for stylesheet handling

CGI::XMLApplication had originally a rather strict design for XML/XSL integration, therefore there are some specific functions to manipulate data for such a system. The following functions left in the package, so older applications does not have to be rewritten. Now I recommend, to use the callback/ overriding system.

method setStylesheetDir DIRNAME

alias for setStylesheetPath

method setStylesheetPath DIRNAME

This method is for telling the application where the stylesheets can be found. If you keep your stylesheets in the same directory as your script -- generally a bad idea -- you might leave this untouched.

function getStylesheetPath

This function is only relevant if you write your own serialization() method. It returns the current path to the application stylesheets.

SEE ALSO

CGI, perlobj, perlmod, XML::LibXML, XML::LibXSLT

AUTHOR

Christian Glahn, christian.glahn@uibk.ac.at

VERSION

1.0.0