[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[cgiapp] CGI::Application::XML


Attached is a module I wrote today. Hopefully you guys can put it to good use. The module is pretty simple. Basically instead of your runmodes returning HTML, CGI::Application::XML expects runmodes to return XML. The module is simple to understand, but I made an example to explain its use.


Example of use:
###WebApp.pm###
package WebApp;

use XML::LibXML::SAX::Builder;
use XML::Generator::DBI;
use DBI;
use base 'CGI::Application::XML';

sub setup {
   my $self = shift;
$self->set_stylesheet_dir("/var/www/xslt/"); #set the location of the xslt stylesheets
   $self->start_mode('mysql');
   $self->mode_param('rm');
   $self->run_modes(
       'mysql' => 'do_mysql',
   );
}
sub cgiapp_postrun {
   my $self = shift;
   $self->serialize(shift);  #Most have this line in your cgiapp_postrun
} sub do_mysql {
   my $self = shift;
   my $sql = 'select * from user';

my $dbh = DBI->connect( 'dbi:mysql:database=mysql;host=localhost','username','password') || die "database connection couldn't be initialized: $DBI::errstr \n";

   my $builder = XML::LibXML::SAX::Builder->new();
   my $gen = XML::Generator::DBI->new(    Handler      => $builder,
                       dbh          => $dbh,
                       RootElement  => 'mysql',
                       QueryElement => 'userlist',
                       RowElement   => 'user');

   $gen->execute($sql) || die "Error Building DOM Tree\n";
   return $builder->result()->toString(1);
}
1;

###webapp.cgi###
use WebApp;
my $webapp = WebApp->new();
$webapp->run();

###mysql.xslt###
<xsl:stylesheet version="1.0"
               xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
               xmlns="http://www.w3.org/TR/xhtml1/strict";>
<xsl:output
  method="xml"
  indent="yes"
  encoding="iso-8859-1"
/>

<xsl:template match="mysql">
<html>
 <head>
   <title>mySQL User List</title>
  </head>
  <body>
   <table border="1">
    <tr>
     <td><b>Host</b></td>
     <td><b>User</b></td>
     <td><b>Password</b></td>
    </tr>
    <xsl:for-each select="userlist/user">
    <tr>
      <td>
   <xsl:value-of select="Host" />
      </td>
      <td>
       <xsl:value-of select="User" />
      </td>
      <td>
       <xsl:value-of select="Password" />
      </td>
     </tr>
     </xsl:for-each>
    </table>
  </body>
</html>
</xsl:template>
</xsl:stylesheet>
package CGI::Application::XML;

$CGI::Application::XML::VERSION = '0.1';

use strict;

use XML::LibXML;
use XML::LibXSLT;

use CGI::Application;

@CGI::Application::XML::ISA = qw(CGI::Application);


#Path to XSLT stylesheets
sub set_stylesheet_dir { $_[0]->{CGIAPPXML_STYLESHEETDIR} = $_[1] }
sub get_stylesheet_dir { $_[0]->{CGIAPPXML_STYLESHEETDIR} }

#Current stylesheet for runmode
sub get_stylesheet {
	my $self = shift;
	return $self->{CGIAPPXML_CURSTYLESHEET} || $self->{CGIAPPXML_STYLESHEETDIR} . $self->get_current_runmode . ".xslt";
}
sub set_stylesheet {
	my $self = shift;
	$self->{CGIAPPXML_CURSTYLESHEET} = $self->{CGIAPPXML_STYLESHEETDIR} ? $self->{CGIAPPXML_STYLESHEETDIR} . shift : shift;
}

#Serializes data in body
sub serialize {
	my $self = shift;
	my $bodyref = shift;

	my $style_doc = $self->get_stylesheet;

	my $parser = XML::LibXML->new();
	my $xslt = XML::LibXSLT->new();

	my $source = $parser->parse_string($$bodyref);
	my $stylesheet = $xslt->parse_stylesheet_file($style_doc);

	my $result = $stylesheet->transform($source);	

	$$bodyref = $stylesheet->output_string($result);
}

1;

---------------------------------------------------------------------
Web Archive:  http://www.mail-archive.com/suppressed/
              http://marc.theaimsgroup.com/?l=cgiapp&r=1&w=2
To unsubscribe, e-mail: suppressed
For additional commands, e-mail: suppressed

Mail converted by mhonarc 2.6.15
This archive provided courtesy of JSW4.NET, Internet Hosting Services for Small Business.