xml-axkit-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jw...@apache.org
Subject cvs commit: xml-axkit/lib/Apache/AxKit/Language HtmlDoc.pm LibXSLT.pm PassiveTeX.pm Query.pm Sablot.pm XPathScript.pm
Date Mon, 25 Mar 2002 14:06:09 GMT
jwalt       02/03/25 06:06:09

  Modified:    demo/xsp index.xml
               lib/Apache/AxKit/Language LibXSLT.pm PassiveTeX.pm Query.pm
                        Sablot.pm XPathScript.pm
  Added:       demo/AxKit/XSP/Demo Object.pm
               demo/xsp simpletaglib-object.xsp
               lib/Apache/AxKit/Language HtmlDoc.pm
  Log:
  - added demo of OO taglib usage
  - cleaned up ::Language::* handler return values (explicitly return OK)
  - added HtmlDoc processor
  
  Revision  Changes    Path
  1.1                  xml-axkit/demo/AxKit/XSP/Demo/Object.pm
  
  Index: Object.pm
  ===================================================================
  # $Id: Object.pm,v 1.1 2002/03/25 14:06:09 jwalt Exp $
  package AxKit::XSP::Demo::Object;
  
  # Always use strict. Do it. I mean it. And turn on warnings as well.
  use strict;
  
  # this single line makes the taglib a Simpletaglib powered one
  use Apache::AxKit::Language::XSP::SimpleTaglib;
  
  # providing a version is good style, but not strictly neccessary
  $AxKit::XSP::Demo::Simple::VERSION = 0.90;
  
  # The namespace associated with this taglib. If it doesn't work, double check
  # that you are using the exact same NS uri here and in the XSP.
  $AxKit::XSP::Demo::Simple::NS = 'http://www.creITve.de/2002/XSP/Demo/Object';
  
  # something to go at the top of the XSP script - this is NOT executed on every
  # request, but only once for each httpd process
  
  sub start_document {
      # We are using a Time::Piece object as example. Look at it's man page, it is
      # very straighforward, and a useful module as well.
      return "use Time::Piece;\n".
  }
  
  package AxKit::XSP::Demo::Object::Handlers;
  
  # You need this dummy because the existence of a sub alone decides if a tag is
  # valid or not, even when the work is done in the opening tag. We tell
  # SimpleTaglib that we
  sub new : attrib(name,timestamp) {
      return '';
  }
  
  # here the work is done
  sub new__open {
      # here we are using the passed arguments, specifically, the 'name'
      # attribute
      my ($e, $tag, %attr) = @_;
  
      # if you do not nest this object, you can just ignore the name,
      # we provide a default
      my $name = $attr{'name'} || 'object';
  
      # make the name 'safe'
      $name =~ s/[^a-zA-Z0-9]/_/g;
  
      # provide a default timestamp (use current time)
      my $time = defined $attr{'timestamp'}? $attr{'timestamp'} : '';
  
      # We could allow any expression, thus making it more flexible, but
      # this would be unusual usage of XSP so I decided to enforce
      # numeric literals. The XSPish way for dynamic arguments are child
      # tags, but we cannot use them here.
      # Note that this is an important limitation: We cannot use any
      # content of the tag in the opening tag handler, just attributes.
      $time =~ s/[^0-9]//g;
  
      # note the naming convention: all xsp internal variable names start with
      # _xsp_, followed by the package name minus AxKit::XSP::, replacing '::'
      # with '_'. Oh, and if you are wondering about that 'localtime', read
      # the man page of Time::Piece.
      return 'my $_xsp_demo_object_'.$name.' = localtime('.$time.');'."\n";
  }
  
  sub day : expr attrib(name) {
      # this is the same prologue as above, we use it in every tag
      my ($e, $tag, %attr) = @_;
      my $name = $attr{'name'} || 'object';
      $name =~ s/[^a-zA-Z0-9]/_/g;
  
      # and a one-liner for the actual method call
      return '$_xsp_demo_object_'.$name.'->day';
  }
  
  sub month : expr attrib(name) {
      # this is the same prologue as above, we use it in every tag
      my ($e, $tag, %attr) = @_;
      my $name = $attr{'name'} || 'object';
      $name =~ s/[^a-zA-Z0-9]/_/g;
  
      return '$_xsp_demo_object_'.$name.'->month';
  }
  
  sub year : expr attrib(name) {
      # this is the same prologue as above, we use it in every tag
      my ($e, $tag, %attr) = @_;
      my $name = $attr{'name'} || 'object';
      $name =~ s/[^a-zA-Z0-9]/_/g;
  
      return '$_xsp_demo_object_'.$name.'->year';
  }
  
  # ... and so on. For ways of specifying input and output, see the other
  # SimpleTaglib demos.
  
  1;
  
  __END__
  
  =head1 NAME
  
  AxKit::XSP::Demo::Object - how to work object oriented with SimpleTaglib
  
  =head1 SYNOPSIS
  
  Add the demo: namespace to your XSP C<<xsp:page>> tag:
  
      <xsp:page
           language="Perl"
           xmlns:xsp="http://apache.org/xsp/core/v1"
           xmlns:object="http://www.creITve.de/2002/XSP/Demo/Object"
      >
  
  Add this taglib to AxKit (via httpd.conf or .htaccess):
  
      AxAddXSPTaglib AxKit::XSP::Demo::Object
  
  =head1 DESCRIPTION
  
  This is a demo how to do some OO stuff with SimpleTaglib. It is far from
  complete, but gives you an Idea how this would work. See the SimpleTaglib
  docs for details.
  
  =head1 AUTHOR
  
  Jörg Walter <jwalt@cpan.org>
  
  =head1 COPYRIGHT
  
  Copyright (c) 2002 Jörg Walter.
  All rights reserved. This program is free software; you can redistribute it and/or
  modify it under the same terms as AxKit itself.
  
  =head1 SEE ALSO
  
  AxKit, Apache::AxKit::Language::XSP, Apache::AxKit::Language::XSP::SimpleTaglib
  
  =cut
  
  
  
  1.5       +3 -1      xml-axkit/demo/xsp/index.xml
  
  Index: index.xml
  ===================================================================
  RCS file: /home/cvs/xml-axkit/demo/xsp/index.xml,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- index.xml	17 Mar 2002 17:29:04 -0000	1.4
  +++ index.xml	25 Mar 2002 14:06:09 -0000	1.5
  @@ -176,9 +176,11 @@
   
               eval {
                   require Apache::AxKit::Language::XSP::SimpleTaglib;
  +                require Time::Piece;
               };
               if (!$@) {
  -                <li><a href="simpletaglib-simple.xsp">SimpleTaglib Taglib Example</a></li>
  +                <li><a href="simpletaglib-simple.xsp">SimpleTaglib Simple Taglib
Example</a></li>
  +                <li><a href="simpletaglib-object.xsp">SimpleTaglib OO Taglib
Example</a></li>
               }
               else {
                   AxKit::Debug(1, "SimpleTaglib failed to load: $@");
  
  
  
  1.1                  xml-axkit/demo/xsp/simpletaglib-object.xsp
  
  Index: simpletaglib-object.xsp
  ===================================================================
  <?xml version="1.0"?>
  <?xml-stylesheet href="NULL" type="application/x-xsp"?>
  <xsp:page
      language="Perl"
      xmlns:xsp="http://apache.org/xsp/core/v1"
      xmlns:object="http://www.creITve.de/2002/XSP/Demo/Object"
  >
  <html>
  <head>
  <title>SimpleTaglib OO Demo</title>
  </head>
  <body>
  <h1>SimpleTaglib OO Demo</h1>
  <h2>One object</h2>
  <object:new>
      <ul>
          <li>day: <object:day/></li>
          <li>month: <object:month/></li>
          <li>year: <object:year/></li>
      </ul>
  </object:new>
  <h2>Several nested objects</h2>
  <object:new>
      <object:new name="epoch" timestamp="0">
          <object:new name="Birthday of 2 AxKit-Developers" timestamp="227228400">
              <ul>
                  <li>days: <object:day/> - <object:day name="epoch"/> -
<object:day name="Birthday of 2 AxKit-Developers"/></li>
                  <li>months: <object:month/> - <object:month name="epoch"/>
- <object:month name="Birthday of 2 AxKit-Developers"/></li>
                  <li>years: <object:year/> - <object:year name="epoch"/>
- <object:year name="Birthday of 2 AxKit-Developers"/></li>
              </ul>
          </object:new>
      </object:new>
  </object:new>
  </body>
  </html>
  </xsp:page>
  
  
  
  1.4       +5 -4      xml-axkit/lib/Apache/AxKit/Language/LibXSLT.pm
  
  Index: LibXSLT.pm
  ===================================================================
  RCS file: /home/cvs/xml-axkit/lib/Apache/AxKit/Language/LibXSLT.pm,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- LibXSLT.pm	18 Feb 2002 18:36:20 -0000	1.3
  +++ LibXSLT.pm	25 Mar 2002 14:06:09 -0000	1.4
  @@ -1,4 +1,4 @@
  -# $Id: LibXSLT.pm,v 1.3 2002/02/18 18:36:20 darobin Exp $
  +# $Id: LibXSLT.pm,v 1.4 2002/03/25 14:06:09 jwalt Exp $
   
   package Apache::AxKit::Language::LibXSLT;
   
  @@ -122,12 +122,13 @@
       }
   
       $stylesheet->output_fh($results, $r) if $last_in_chain;
  -    
  +
       $r->pnotes('dom_tree', $results);
  -     
  +
   #         warn "LibXSLT returned $output \n";
   #         print $stylesheet->output_string($results);
  -    
  +
  +    return Apache::Constants::OK;
   }
   
   sub fixup_params {
  
  
  
  1.2       +12 -10    xml-axkit/lib/Apache/AxKit/Language/PassiveTeX.pm
  
  Index: PassiveTeX.pm
  ===================================================================
  RCS file: /home/cvs/xml-axkit/lib/Apache/AxKit/Language/PassiveTeX.pm,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- PassiveTeX.pm	13 Jan 2002 20:45:11 -0000	1.1
  +++ PassiveTeX.pm	25 Mar 2002 14:06:09 -0000	1.2
  @@ -1,4 +1,4 @@
  -# $Id: PassiveTeX.pm,v 1.1 2002/01/13 20:45:11 matts Exp $
  +# $Id: PassiveTeX.pm,v 1.2 2002/03/25 14:06:09 jwalt Exp $
   
   package Apache::AxKit::Language::PassiveTeX;
   
  @@ -66,28 +66,30 @@
               close($fh) || fail("Cannot close: $!");
           }
       }
  -    
  +
       chdir($tempdir) || fail("Cannot cd: $!");
  -    
  -    local $ENV{TEXINPUTS} = dirname($r->filename()); 
  +
  +    local $ENV{TEXINPUTS} = dirname($r->filename());
       AxKit::Debug(8, "About to shell out to pdfxmltex - hope you have passivetex installed...");
       my $retval = system("pdfxmltex temp.fo");
       $retval >>= 8;
  -    
  +
       if ($retval) {
           fail("pdfxmltex exited with $retval");
       }
  -    
  +
       my $pdfh = Apache->gensym();
       open($pdfh, "temp.pdf") || fail("Cannot open PDF: $!");
  -    
  +
       $AxKit::Cfg->AllowOutputCharset(0);
  -    
  +
       $r->content_type("application/pdf");
  -    
  +
       local $/;
  -    
  +
       $r->print(<$pdfh>);
  +
  +    return Apache::Constants::OK;
   }
   
   sub cleanup {
  
  
  
  1.2       +5 -5      xml-axkit/lib/Apache/AxKit/Language/Query.pm
  
  Index: Query.pm
  ===================================================================
  RCS file: /home/cvs/xml-axkit/lib/Apache/AxKit/Language/Query.pm,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- Query.pm	13 Jan 2002 20:45:11 -0000	1.1
  +++ Query.pm	25 Mar 2002 14:06:09 -0000	1.2
  @@ -8,7 +8,7 @@
   sub handler {
   	my $r = shift;
   	my ($xmlfile, $stylesheet) = @_;
  -	
  +
   	my $vals;
   	open(STYLE, $stylesheet) || die "Can't open stylesheet '$stylesheet': $!";
   	flock(STYLE, 1);
  @@ -20,7 +20,7 @@
   		next unless $key;
   		$vals->{$key} = $val;
   	}
  -	
  +
   	my $dsn;
   	if ($vals->{CONNECT_STRING}) {
   		$dsn = $vals->{CONNECT_STRING};
  @@ -28,7 +28,7 @@
   	else {
   		$dsn = "dbi:$vals->{DRIVER}:$vals->{CONNECT_EXTRA}";
   	}
  -	
  +
   	my %attr;
   	foreach my $key (keys %$vals) {
   		next unless $key =~ /^ATTR_(.*)$/;
  @@ -36,8 +36,8 @@
   	}
   
   	my $dbh = DBI->connect($dsn, $vals->{USER}, $vals->{PASSWORD}, \%attr);
  -	
  -	
  +
  +    return Apache::Constants::OK;
   }
   
   1;
  
  
  
  1.3       +4 -2      xml-axkit/lib/Apache/AxKit/Language/Sablot.pm
  
  Index: Sablot.pm
  ===================================================================
  RCS file: /home/cvs/xml-axkit/lib/Apache/AxKit/Language/Sablot.pm,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- Sablot.pm	30 Jan 2002 18:17:21 -0000	1.2
  +++ Sablot.pm	25 Mar 2002 14:06:09 -0000	1.3
  @@ -1,4 +1,4 @@
  -# $Id: Sablot.pm,v 1.2 2002/01/30 18:17:21 darobin Exp $
  +# $Id: Sablot.pm,v 1.3 2002/03/25 14:06:09 jwalt Exp $
   
   package Apache::AxKit::Language::Sablot;
   
  @@ -61,8 +61,10 @@
       }
   
       print $xslt_processor->GetResultArg("result");
  -    
  +
       $xslt_processor->FreeResultArgs();
  +
  +    return Apache::Constants::OK;
   }
   
   END {
  
  
  
  1.2       +9 -10     xml-axkit/lib/Apache/AxKit/Language/XPathScript.pm
  
  Index: XPathScript.pm
  ===================================================================
  RCS file: /home/cvs/xml-axkit/lib/Apache/AxKit/Language/XPathScript.pm,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- XPathScript.pm	13 Jan 2002 20:45:11 -0000	1.1
  +++ XPathScript.pm	25 Mar 2002 14:06:09 -0000	1.2
  @@ -1,4 +1,4 @@
  -# $Id: XPathScript.pm,v 1.1 2002/01/13 20:45:11 matts Exp $
  +# $Id: XPathScript.pm,v 1.2 2002/03/25 14:06:09 jwalt Exp $
   
   package Apache::AxKit::Language::XPathScript;
   
  @@ -89,9 +89,7 @@
           compile($package, $style_provider);
           $stash->{$style_key}{mtime} = get_mtime($class, $style_provider);
       }
  -    
  -    my $old_status = $r->status;
  -    
  +
       no strict 'refs';
       my $cv = \&{"$package\::handler"};
   
  @@ -99,29 +97,30 @@
       my $t = {};
       local $Apache::AxKit::Language::XPathScript::trans = $t;
       local $Apache::AxKit::Language::XPathScript::style_provider = $style_provider;
  -    
  +
       AxKit::Debug(7, "Running XPathScript script\n");
       local $^W;
  +    my $rc = Apache::Constants::OK;
       eval {
  -        $cv->($r, $xpath, $t);
  +        $rc = $cv->($r, $xpath, $t);
       };
       if ($@) {
           AxKit::Debug(1, "XPathScript error: $@");
           throw $@;
       }
  -    
  -    if (!$r->pnotes('xml_string') && 
  +
  +    if (!$r->pnotes('xml_string') &&
           !$r->dir_config('XPSNoApplyTemplatesOnEmptyOutput')) { # no output? Try apply_templates
           print Apache::AxKit::Language::XPathScript::Toys::apply_templates();
       }
  -    
  +
   #    warn "Run\n";
   
       $Apache::AxKit::Language::XPathScript::xp = undef;
       $Apache::AxKit::Language::XPathScript::trans = undef;
       $Apache::AxKit::Language::XPathScript::style_provider = undef;
   #    warn "Returning $old_status\n";
  -    return $r->status($old_status);
  +    return $rc;
   }
   
   sub get_source_tree {
  
  
  
  1.1                  xml-axkit/lib/Apache/AxKit/Language/HtmlDoc.pm
  
  Index: HtmlDoc.pm
  ===================================================================
  # $Id: HtmlDoc.pm,v 1.1 2002/03/25 14:06:09 jwalt Exp $
  # Apache::AxKit::Language::HtmlDoc - xhtml->pdf renderer
  package Apache::AxKit::Language::HtmlDoc;
  
  @ISA = ('Apache::AxKit::Language');
  
  use strict;
  
  use Apache;
  use Apache::Constants qw(:common);
  use Apache::Request;
  use Apache::AxKit::Language;
  use Apache::AxKit::Language::LibXSLT;
  use Apache::AxKit::Provider;
  use IPC::Run qw(run);
  use Cwd;
  
  my $olddir;
  my $tempdir;
  
  sub stylesheet_exists () { 0; }
  
  sub handler {
      my $class = shift;
      my ($r, $xml_provider, undef, $last_in_chain) = @_;
  
      my $parser = XML::LibXML->new();
      local $XML::LibXML::match_cb = \&Apache::AxKit::Language::LibXSLT::match_uri;
      local $XML::LibXML::open_cb = \&Apache::AxKit::Language::LibXSLT::open_uri;
      local $XML::LibXML::read_cb = \&Apache::AxKit::Language::LibXSLT::read_uri;
      local $XML::LibXML::close_cb = \&Apache::AxKit::Language::LibXSLT::close_uri;
  
      my $dom;
      my $source_text;
      if ($dom = $r->pnotes('dom_tree')) {
          ;
      } elsif ($source_text = $r->pnotes('xml_string')) {
          $dom = $parser->parse_string($source_text, $r->uri());
      }
      else {
          $source_text = eval { ${$xml_provider->get_strref()} };
          if ($@) {
              my $fh = $xml_provider->get_fh();
              $source_text = join("",<$fh>);
          }
          $dom = $parser->parse_string($source_text, $r->uri());
      }
      $dom->process_xinclude();
      my $style_dom = $parser->parse_string(<< 'EOX','.');
  <?xml version="1.0"?>
  <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1999/xhtml">
  <xsl:output method="html" encoding="ISO-8859-15"/>
  <xsl:template match="*"><xsl:copy select="."><xsl:copy-of select="@*"/><xsl:apply-templates/></xsl:copy></xsl:template>
  <xsl:template match="text()"><xsl:value-of select="."/></xsl:template>
  </xsl:stylesheet>
  EOX
      my $stylesheet = XML::LibXSLT->parse_stylesheet($style_dom);
      my $results = $stylesheet->transform($dom);
  
      my $result;
      my $input = $stylesheet->output_string($results);
      my $host = $r->hostname;
      $input =~ s{ href="/}{ href="http://$host/}g;
      my $path = $r->document_root;
      $input =~ s{ src="/}{ src="$path/}g;
      $path = $r->uri;
      $path =~ s{/+[^/]*$}{/};
      $input =~ s{ href="(?!/|.{0,5}:)}{ href="http://$host$path}g;
      AxKit::Debug(8, "About to shell out to htmldoc - hope you have it installed...");
      AxKit::Debug(10, $input);
      my $rc = run(['htmldoc','--quiet','--format','pdf13','--truetype','--size','a4','--color','--charset','8859-15','--webpage',$r->dir_config->get('AxHtmlDocOptions'),'-'],\$input,\$result);
  
      if (!$rc) {
          throw Apache::AxKit::Exception::Error(-text => 'htmldoc returned error: '.$result);
      }
  
      $AxKit::Cfg->AllowOutputCharset(0);
  
      $r->content_type('application/pdf');
      $r->pnotes('xml_string',$result);
      return OK;
  }
  
  1;
  
  =head1 NAME
  
  Apache::AxKit::Language::HtmlDoc - deliver XHTML as PDF
  
  =head1 SYNOPSIS
  
    AxAddStyleMap text/xhtml Apache::AxKit::Language::HtmlDoc
  
    # as last step in your processor chain, add:
    AxAddProcessor text/xhtml NULL
    
    # want custom HTMLDOC args? here we go:
    PerlAddVar AxHtmlDocOptions --linkcolor '#ff0000' --linkstyle plain
  
  =head1 DESCRIPTION
  
  Go and get HTMLDOC (L<http://www.easysw.com/htmldoc/>) first, then you
  can convert any XHTML page into a quite nice looking PDF document. Be
  prepared to do some tweaking of your xhtml input, though, because
  HTMLDOC is HTML 3.2 only, it does not yet understand CSS and only some
  HTML 4.0 (as of version 1.8.18). Using an extra XSLT stylesheet, it
  isn't all that hard to create HTMLDOC friendly input and you get nice
  results.
  
  You should not use this for mostly hand-crafted PDFs, for that see the
  PassiveTeX module, which converts XSL:FO to PDF. HTMLDOC has its quirks,
  sometimes it is a bit frustrating getting the output right. It pays
  off if you have lots of existing (or generated) HTML and want all of
  them to be PDF, but for a custom PDF like a bill, you have much better
  control with PassiveTeX.
  
  =head1 AUTHOR
  
  Jörg Walter, jwalt@cpan.org
  
  =cut
  
  
  

Mime
View raw message