|
NAMECGI::XMLApplication -- Object Oriented Interface for CGI Script ApplicationsSYNOPSISuse 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); # or a context object DESCRIPTIONCGI::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 splitting web applications into several simple parts. Through this most of the code stays simple and easy to maintain. Throughout the whole runtime 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 code abstraction of a click of a submit button or an image, CGI::XMLApplication implements a simple event system, that keeps event related code separated from other events. Therefore, a final application class is not meant 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. Also this forces the programmer to implement scripts more problem orientated, rather than class or content 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 by default but may be replaced easily by any other XML/XSLT capable modules. Each CGI Script should generate an XML-DOM, that can be processed with a given stylesheet. Pay attention: In this Document XML-DOM means the DOM of XML::LibXML and not XML::DOM! Programflow of a CGI::XMLApplicationThe following Flowchart illustratrates how CGI::XMLApplication behaves during runtime. Also chart shows where specialized application code gets control during script runtime.------- CGI Script ------->|<--------- CGI::XMLApplication -------- .---------------------. .--------------------. | app-class creation |--- | event registration | `---------------------' | registerEvents() | `--------------------' .------------------------. | | context initialization |------------' | ( optional ) | `------------------------' | .-----------------------. .------------------------. | run() function called |--| application initialize | `-----------------------' | event_init() | `------------------------' | .--------'`------------. / event parameter found? \_ \ testEvent() / \ `--------.,------------' | | | yes | no | | | .------------. .------------------. | call event | | call | | event_*() | | event_default() | `------------' `------------------' | | .---------------------. | | application cleanup |-----' | event_exit() | `---------------------' | .---------'`------------. _/ avoid XML serialization \ / \ skip_serialization() / | `---------.,------------' | | yes | no | | | | .--------------------------. | | XML generation, XSLT | | | serialization and output | | | serialization() | | `--------------------------' .---------------. | | | END |-------+-------------' `---------------' What are Events and how to catch themMost CGI Scripts 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. Because of this common events are thrown by arguments found in the CGI's query string.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: # Application Module package myApp; use CGI::XMLApplication; @ISA = qw(CGI::XMLApplication); sub registerEvents { qw( dummy ); } # list of event names # ... 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. One does not need to change the code to make the scripts fit to these changes; CGI::XMLApplication already did it. 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 tests if evname or evname.x exist in the querystring. So a perl artist can implement and test his code without caring if the design crew have done their job, too ;-) In many cases an web application is also confronted with events that can not be represented in with querystring arguments. For these cases CGI::XMLApplication offers the possibility to send special events from the event_init() function for example in case of application errors. 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!. Although a sendEvent function exists, CGI::XMLApplication doesn't implement an event queqe. For GUI programmers this seems like a unnessecary restriction. In 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 and stopped before the next event can be thrown. The only chance to stop the script from handling a certain event is to send a new event or delete this (or even all) events from inside the event_init() function. This function is always called at first from the run method. If another event uses the sendEvent function, the call will have no effect.
The Event SystemA CGI::XMLApplication is split into two main parts: 1) The executable script called by the webserver and 2) the application module which has to be loaded, initialized and called by the script.Commonly applications that make use of CGI::XMLApplication, will not bother about the run function too much. 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 an application panic, and will not try to generate a DOM or render it with a stylesheet. There are 4 defined panic levels:
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 a required perl module is not installed on the system. To make it clear: If CGI::XMLApplication throws a panic, the application is broken, not completely implemented or stylesheets are missing or broken. Application panics are meant for debugging purposes and to avoid Internal Server Errors. They are not meant as a replacement of a propper error handling! But how does CGI::XMLApplication know about the correct event handler? One needs to register the names of the events the application handles. This is done by implmenting a registerEvents() function that simply returns an array of event names. Through this function one prepares the CGI::XMLApplication to catch the listed names as events from the query string the client browser sends back to the script. CGI::XMLApplication tries to call a event handler if a name of a registred event is found. The coresponding function-name of an event has to have the following format: event_<eventname> E.g. event_init handles the init event described below. Each event has a single Parameter, the context. This can be an unblessed hash reference or an object, where the user can store whatever needed. This context is useful to pass scriptwide data between callbacks and event functions around. The callback is even available and useable if the script does not initialize the application context as earlier shown in the program flow chart. If such a function is not implemented in the application module, CGI::XMLApplication sets the Event not implemented panic state. All events have to return an integer that tells about their execution state as already described. By default CGI::XMLApplication does not test for other events if it already found one. The most significant event is the first name of an event found in the query string - all other names are simply ignored. One may change this behaviour by overriding the testEvent() function. But still it is a good idea to choose the event names carefully and do not mix them with ordinary datafield names.
Implicit EventsCGI::XMLApplication knows three implicit events which are more or less independent to client responses: They are 'init', 'exit', and 'default'. These events already exist for any CGI::XMLApplication. They need not to be implemented separately if they make no sense for the application.
the XML SerializationThe presentation is probably the main part of a CGI script. By using XML and XSLT this can be done in a standartised manner. From the application view all this can be isolated in a separate subsystem as well. In CGI::XMLApplication this subsystem is implemented inside the serialize() function.For XML phobic perl programmers it should be cleared, that CGI::XMLApplication makes real use of XML/XSLT functionalty only inside this function. For all code explained above it is not required to make use of XML at all. The XML serialization subsystem of CGI::XMLApplication tries to hide most of non application specific code from the application programmer. 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. 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:
If errors occour on a certain stage of serialization, the application is stopped and the generated error messages are returned. CGI::XMLApplication provides four pseudo-callbacks, that are used to get the application specific information during serialization. In order of being called by CGI::XMLApplication::serialization() they are:
In fact only getStylesheet has to be implemented. In most cases it will be a good idea to provide the getDOM function as well. The other functions provider a interface to make the CGI output more generic. For example one can set cookies or pass XSL parameters to XML::LibXSLT's xsl processor. 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.
Flow ControlBesides the sendEvent() function, CGI::XMLApplication provides two additional functions for controlling the flow of the application.These two functions are related to the XML serialization and have not affect to the event handling.
Helperfunctions for internal use
CGI ExtrasThe following functions are some neat features missing in CGI.pm
some extra functions for stylesheet handlingThe getStylesheet() function should return either a filename or a stringnyfied XSL-DOM. For the first case it can be a restriction to return the fully qualified path. The following functions help managing the stylesheetpath, system-wide.
SEE ALSOCGI, perlobj, perlmod, XML::LibXML, XML::LibXSLTAUTHORChristian Glahn, phish@cpan.orgVERSION1.1.5
Visit the GSP FreeBSD Man Page Interface. |