########################################################################## ## All portions of this code are copyright (c) 2003,2004 nethype GmbH ## ########################################################################## ## Using, reading, modifying or copying this code requires a LICENSE ## ## from nethype GmbH, Franz-Werfel-Str. 11, 74078 Heilbronn, ## ## Germany. If you happen to have questions, feel free to contact us at ## ## license@nethype.de. ## ########################################################################## =head1 NAME PApp::XML - pxml sections and more =head1 SYNOPSIS use PApp::XML; =head1 DESCRIPTION Apart from providing XML convinience functions, the PApp::XML module manages XML templates containing pappxml directives and perl code similar to phtml sections. Together with stylesheets (L) this can be used to almost totally seperate content from layout. Image a database containing XML documents with customized tags. A stylesheet can then be used to transform this XML document into html + special pappxml directives that can be used to create links etc... =cut package PApp::XML; use Convert::Scalar ':utf8'; use PApp::Util; use PApp::Exception qw(fancydie); use base 'Exporter'; $VERSION = 1.43; @EXPORT_OK = qw( xml_quote xml_attr xml_unquote xml_tag xml_cdata xml_check xml_encoding xml2utf8 pod2xml xml_include expand_pi xml_errorparser ); =head2 Functions for XML-Generation =over 4 =item xml_quote $string Quotes (and returns) the given string so that it's contents won't be interpreted by an XML parser (quotes ', ", <, & and > to avoid ]]>). Example: print xml_quote q( & <[[]]> ); => <xx> & <[[]]> =item xml_cdata $string Does the same thing as C, but using CDATA constructs, rather than quoting individual characters. Example: print xml_cdata q(hi ]]> there); => there ]]> =item xml_unquote $string Unquotes (and returns) an XML string (by resolving it's entities and CDATA sections). Currently, only the named predefined xml entities and numerical character entities are resolved. Everything else is silently ignored. Example: print xml_unquote q( & text2! ); => text1 & text2! =item xml_attr $attr => $value [, $attr2 => $value2, ...] Returns fully quoted $attr => $value pairs. Example: print xml_attr authors => q(Alan Cox & Linus "kubys" Torvalds); => authors="Alan Cox & Linus "kubys" Torvalds" =item xml_tag $element_name, [$attr => $value, ...] [, $content_or_undef] Generates a tag from the given element name, content and attribute name => value pairs. If content is undef, an empty tag will be generated. Example: print xml_tag "p", align => "center" =>

As a very special courtesy hack for you, if you omit the content argument entirely, only an opening tag will be generated. =cut sub xml_quote { local $_ = shift; s/&/&/g; s//>/g; #s/]]>/]]>/g; # avoids problems when ]] and > are quoted in seperate calls $_; } sub xml_cdata { local $_ = shift; s/]]>/]]]]>/g; ""; } sub xml_attr { my $attrs; for (my $i = 0; $i < $#_; $i += 2) { local $_ = $_[$i+1]; s/&/&/g; s/"/"/g; s/ 1; if (@_ & 1) { if (defined $_[-1]) { "$tag>$_[-1]"; } else { "$tag/>"; } } else { "$tag>"; } } sub xml_unquote($) { local $_ = shift; s{&([^;]+);|}{ if (defined $2) { $2; } elsif ("#" eq substr $1, 0, 1) { if ("x" eq substr $1, 1, 1) { chr hex substr $1, 2; } else { chr substr $1,1; } } else { { gt => '>', lt => '<', amp => '&', quot => '"', apos => "'" }->{$1} } }ge; $_; } =back =head2 Functions for Analyzing XML =over 4 =item ($msg, $line, $col, $byte) = xml_check $string [, $prolog, $epilog] Checks wether the given document is well-formed (as opposed to valid). This merely tries to parse the string as an xml-document. Nothing is returned if the document is well-formed. Otherwise it returns the error message, line (one-based), column (zero-based) and character-position (zero-based) of the point the error occured. The optional argument C<$prolog> is prepended to the string, while C<$epilog> is appended (i.e. the document is "$prolog$string$epilog"). The cool thing is that the epilog/prolog strings are not counted in the error position (and yes, they should be free of any errors!). (Hint: Remember to utf8_upgrade before calling this function or make sure that an encoding is given in the xml declaration). =cut sub xml_check { my ($string, $prolog, $epilog) = @_; require XML::Parser::Expat; my $parser = new XML::Parser::Expat; $prolog =~ s/\n//; $epilog =~ s/\n//; $string = "$prolog\n$string$epilog"; eval { local $SIG{__DIE__}; $parser->parsestring($string); }; my $err = $@; $parser->release; return () unless $err; $err =~ /^\n(.*?) at line (\d+), column (\d+), byte (\d+)/ or die "unparseable xml error message: $err"; ($1, $2 - 1, $3, ($4 <= length $string - length $epilog ? $4 - 1 - length $prolog : (length $string) - (length $prolog) - (length $epilog) - 1)); } =item xml_errorparser $xml, [$offset, $message] This function takes a slightly damaged XML document or fragment and tries to repair it. During this process it annotates many errors with error messages in -elements. It also offers the option of adding a custom error message around the specified offste in the file. This function currently works best with HTML or HTML-like input, and tries very hard not to place error messages at places where they won't be visible. The result should be parseable by XML parsers, but be warned that not every case will be fixed. =cut my %delay_error = ( script => 1, style => 1, head => 1, input => 1, select => 1, option => 1, applet => 1, frame => 1, h1 => 0, h2 => 0, table => 0, tr => 0, ); %delay_error = (); sub xml_errorparser { require HTML::Parser; # fix any invalid xml-"names" my $xmlname = sub { local $_ = $_[0]; s/([^:]*):([^:]*):/$1:$2_illegal-colon-in-name_/g; s/^([^\p{Letter}_:])/"illegal-xml-start-character_" . (ord $1)/e; s/([^\p{Letter}\p{Digit}\-_.:])/"_illegal-character-" . (ord $1) . "-in-name_"/ge; $_; }; my ($xml, $errofs, $errmsg) = @_; defined $errofs or $errofs = 1e99; my $output = ""; my $delayed; my @tag; # open elements my $err = sub { $delayed .= $_[0] if @_; return if exists $delay_error{$tag[-1]}; for (my $i = @tag; --$i >= 0; ) { return if $delay_error{$tag[$i]}; } $output .= $delayed; $delayed = ""; }; $xml =~ s% ([\x{0}-\x{8}\x{b}\x{c}\x{e}-\x{1f}\x{fffe}]) % "illegal-character-" . (ord $1) . "-skipped"; %gex; # HTML::Parser can't cope with unicode :(, unfortunately # this destroys position information quite severly utf8_upgrade $xml; $xml = (utf8_to PApp::Recode "iso-8859-1", \&PApp::_unicode_to_entity)->($xml); utf8_downgrade $xml; my $parser = new HTML::Parser api_version => 3, strict_names => 1, xml_mode => 1, unbroken_text => 1, case_sensitive => 1, ignore_elements=> [qw(script)], text_h => [sub { if ($_[1] >= $errofs) { $err->("$errmsg, source

\n"
                      . (xml_cdata substr $xml, $errofs >= 160 ? $errofs - 160 : 0, $errofs >= 160 ? 160 : $errofs)
                      . "÷"
                      . (xml_cdata substr $xml, $errofs, 160)
                      . "\n
"); $errofs = 1e99; } else { $delayed and $err->(); } $output .= PApp::XML::xml_quote $_[0]; }, "dtext, offset"], start_h => [sub { my $tag = $xmlname->($_[0]); push @tag, $tag; $output .= PApp::XML::xml_tag $tag, map +($xmlname->($_), $_[1]{$_}), keys %{$_[1]}; $delayed and $err->(); }, "tagname, attr"], end_h => [sub { my $tag = $xmlname->($_[0]); if ($tag[-1] eq $tag) { pop @tag; $output .= ""; $delayed and $err->(); } else { for (my $i = @tag; --$i >= 0; ) { if ($tag[$i] eq $tag) { my $errmsg = "ERROR: end-tag for element '$tag', which is not open, closing tag(s)"; while (@tag > $i) { my $tag = pop @tag; $output .= ""; $delayed and $err->(); $errmsg .= " $tag"; } $err->("$errmsg instead. "); return; } } $err->("ERROR: skipping end-tag for element '$tag', which is not open. "); } }, "tagname"], end_document_h => [sub { while (@tag) { my $tag = pop @tag; $output .= "" ; $delayed and $err->(); } }], declaration_h => [sub { }], comment_h => [sub { }], process_h => [sub { }], ; $parser->parse($xml); $parser->eof; utf8_upgrade $output; # just for your convinience } =item xml_encoding xml-string [DEPRECATED] Convinience function to detect the encoding used by the given xml string. It uses a variety of heuristics (mainly as given in appendix F of the XML specification). UCS4 and UTF-16 are ignored, mainly because I don't want to get into the byte-swapping business (maybe write an interface module for gconv?). The XML declaration itself is being ignored. =cut sub xml_encoding($) { use bytes; no utf8; # 00 00 00 3C: UCS-4, big-endian machine (1234 order) # 3C 00 00 00: UCS-4, little-endian machine (4321 order) # 00 00 3C 00: UCS-4, unusual octet order (2143) # 00 3C 00 00: UCS-4, unusual octet order (3412) # FE FF: UTF-16, big-endian # FF FE: UTF-16, little-endian # 00 3C 00 3F: UTF-16, big-endian, no Byte Order Mark (and thus, strictly speaking, in error) # 3C 00 3F 00: UTF-16, little-endian, no Byte Order Mark (and thus, strictly speaking, in error) # 3C 3F 78 6D: UTF-8, ISO 646, ASCII, some part of ISO 8859, Shift-JIS, EUC, or any other 7-bit, 8-bit, # 4C 6F A7 94: EBCDIC (in some flavor; the full encoding declaration must be read to tell which # this is rather borken substr($_[0], 0, 4) eq "\x00\x00\x00\x3c" and return "ucs-4"; # BE substr($_[0], 0, 4) eq "\x3c\x00\x00\x00" and return "ucs-4"; # LE substr($_[0], 0, 2) eq "\xfe\xff" and return "utf-16"; # BE substr($_[0], 0, 2) eq "\xff\xfe" and return "utf-16"; # LE substr($_[0], 0, 4) eq "\x00\x3c\x00\x3f" and return "utf-16"; # BE substr($_[0], 0, 4) eq "\x3c\x00\x3f\x00" and return "utf-16"; # LE return utf8_valid $_[0] ? "utf-8" : "iso-8859-1"; } =back =head2 Functions for Modifying XML =over 4 =item ($version, $encoding, $standalone) = xml_remove_decl $xml[, $encoding] Remove the xml header, if any, from the given string and return the info. If the declaration is missing, C<("1.0", $encoding || xml_encoding(), "yes")> is returned. =cut sub xml_remove_decl($;$) { use bytes; no utf8; if ($_[0] =~ s/^\s*<\? xml \s+ version \s*=\s* ["']([a-zA-Z0-9.:\-]+)["'] (?:\s+ encoding \s*=\s* ["']([A-Za-z][A-Za-z0-9._\-]*)["'] )? (?:\s+ standalone \s*=\s* ["'](yes|no)["'] )? \s* \?>//x) { return ($1, $2, $3); } else { return ("1.0", $_[1] || &xml_encoding, "yes"); } } =item ($version, $encoding, $standalone) = xml2utf8 xml-string[, encoding] Tries to convert the given string into utf8 (inplace). Currently only supports UTF-8 and ISO-8859-1, but could be extended easily to handle everything Expat can. Uses C to autodetect the encoding unless an explicit encoding argument is given. It returns the xml declaration parameters (where encoding is always utf-8). The xml declaration itself will be removed from the string. =cut sub xml2utf8($;$) { use bytes; no utf8; my ($version, $encoding, $standalone) = &xml_remove_decl; if ($encoding =~ /^utf-?8$/i) { utf8_on $_[0]; } elsif ($encoding =~ /^iso-?8859-?1$/i) { utf8_off $_[0]; # just to be sure ;) utf8_upgrade $_[0]; } else { # use expat! die "xml encoding '$encoding' not yet supported by PApp::XML::xml2utf8"; } ($version, "utf-8", $standalone); } =item expand_pi $xml, { pi => coderef, pi2 => coderef... } Takes an xml string and expands all processing instructions given in the second argument by calling the respective coderef. The resulting string is returned. The (single) argument to the coderef is the (unquoted) argument. This function uses a regex (without backtracking in the common case) and should be fast. For example, to execute sql commands using C processing instructions, use something like this: Test xml string: $expanded = expand_pi $xml, { sql => sub { xml_quote join "", sql_ufetch $_[0]; }, }; =cut sub expand_pi { local @pi; (my $xml = $_[0]) =~ m{ ^ (?: # first skip all "normal" text (not <) [^<]+ | # then skip CDATA sections <\[CDATA\[ (?: [^\]]+ | \][^\]] | \]\][^>] )* \]\]> | # now process processing instructions <\? (\w+) \s+ ( (?: [^?] | \?[^>] )+ )* \?\> (?{ push @pi, [$-[1] - 2, $+[2] + 2, $1, $2] if exists $_[1]{$1}; }) | # else must be a tag <[^?] )* $ }gx; for (reverse @pi) { my ($a, $b, $name, $content) = @$_; substr $xml, $a, $b - $a, $_[1]{$name}->(xml_unquote $content); } $xml; } =item xml_include $document, $base [, $uri_handler($uri, $base) ] Expand any xinclude:include elements in the given C<$document> by handing the href attribute and the current base URI to the C<$uri_handler> with this URI (-object). The C<$uri_handler> should fetch the document and return it (or C on error). Example (see http://www.w3.org/TR/xinclude/ for the definition of xinclude): The result of running xml_include on this document will have the first include element replaced by the document element (and it's contents) of C and the second include element replaced by a (correctly quoted) copy of your C file. Another common example is embedding stylesheet fragments into larger stylesheets. Using xinclude for these cases is faster than xsl's include/import machanism since xinclude expansion can be done after file loading while, while xsl's include mechanism is evaluated on every parse. At the moment this function always returns utf-8 documents, regardless of the input encoding used (included text is inserted as is, any converson must be done in the uri handler). This function does not conform to C. In addition to C and C, this function also supports C (insert text verbatim, i.e. like xslt's C) and C (parse xml file as pxml). The types C and C are also under consideration. =cut my $xmlns_xinclude1999 = "http://www.w3.org/1999/XML/xinclude"; my $xmlns_xinclude2001 = "http://www.w3.org/2001/XInclude"; sub xml_include { require XML::Parser::Expat; my $base = $_[1]; my $get = $_[2] || \&PApp::Util::load_file; my $nested = $_[3]; my $ignore = $nested; my ($self, $xinclude1999, $xinclude2001, $doc, $prefix, @context); my $qualify = sub { $prefix{$self->namespace($_[0])}.$_[0]; }; $self = new XML::Parser::Expat Namespaces => 1; $self->setHandlers( Start => sub { $ignore = 0; if ($self->eq_name ($_[1], $xinclude1999) || $self->eq_name ($_[1], $xinclude2001)) { my (undef, undef, %attr) = @_; my $parse = $attr{parse} || "xml"; my $href = $attr{href}; #$href->fragment eq "" or die "xml_include: fragment identifiers not supported"; my $file = $get->($href, $base); defined $file or die "$href: unable to fetch document\n"; if (defined $file) { if ($parse eq "pxml") { require PApp::PCode; $file = PApp::PCode::pxml2pcode ($file); $file = xml_include ($file, $href, $get, $nested + 1); $file = PApp::PCode::pcode2pxml ($file); } elsif ($parse eq "xml") { $file = xml_include ($file, $href, $get, $nested + 1); } elsif ($parse eq "text") { $file = $self->xml_escape ($file); } elsif ($parse eq "verbatim") { # } else { $self->xpcroak("parse method $parse not supported by this implementation"); } } defined $file or die "$href: unable to fetch document"; $doc .= $file; } elsif ($nested) { # must use the slow way... resolve entities &c. push @context, {}; my $xmlns; for ($self->new_ns_prefixes) { $context[-1]{$_} = delete $prefix{$_}; my $uri = $self->expand_ns_prefix ($_); # the values of $_ before and after this # comment do not need to be the same if ($_ eq "#default") { $prefix{$uri} = ""; $xmlns .= " xmlns='$uri'"; } else { $prefix{$uri} = $_ . ":"; $xmlns .= " xmlns:$_='$uri'"; } } my $tag = $qualify->($_[1]); $context[-1]{"\0"} = $tag; $doc .= "<".$qualify->($_[1]).$xmlns; for (my $i = 2; $i < @_; $i += 2) { $doc .= " ". $qualify->($_[$i]). "='". $self->xml_escape($_[$i+1], "'"). "'"; } $doc .= ">"; } else { $doc .= $self->recognized_string; } }, End => sub { unless ($self->eq_name($_[1], $xinclude)) { if ($nested) { my $ctx = pop @context; $doc .= "{"\0"}).">"; while (my($k, $v) = each %$doc) { $prefix{$k} = $v; } } else { $doc .= $self->recognized_string; } } $ignore = 1 if $nested && !$self->depth; }, XMLDecl => sub { unless ($ignore) { $doc .= ""; } }, Proc => sub { $doc .= ""; }, Comment => sub { $doc .= ""; }, Default => sub { $doc .= $_[1] unless $ignore; }, ); $xinclude1999 = $self->generate_ns_name("include", $xmlns_xinclude1999); $xinclude2001 = $self->generate_ns_name("include", $xmlns_xinclude2001); eval { local $SIG{__DIE__}; $self->parse($_[0]); }; $@ and fancydie "xml_include expansion failed", $@, info => [source => PApp::Util::format_source $_[0]]; { local $@; $self->release } $doc; } =item pod2xml $pod Converts a POD string (which can be either a fragment or a whole document) =cut { package PApp::XML::Pod2xml; sub stag { (PApp::XML::xml_tag @_) } sub title_tag { my ($name, $title, $cont, @a) = @_; stag $name, @a, (stag 'title' => $title) . (stag 'content' => $cont) } sub view_item { my $t = $_[1]->title->present ($_[0]); my $bullet; if ($t =~ s/^\s*\*\s+//) { $bullet = "*"; } elsif ($t =~ s/^\s*(\d+\.)\s+//) { $bullet = $1; } title_tag item => $t => $_[1]->content->present ($_[0]), $bullet ? (bullet => $bullet) : () } sub view_begin { $_[1]->format eq "xmlpod" ? $_[1]->content->present ($_[0]) : stag for => format => $_[1]->format, $_[1]->content->present ($_[0]) } sub view_for { $_[1]->format eq "xmlpod" ? $_[1]->text : stag for => $_[1]->text; } sub view_pod { stag pod => xmlns => "http://www.nethype.de/xmlns/xmlpod" => $_[1]->content->present ($_[0]) } sub view_head1 { title_tag head1 => $_[1]->title->present ($_[0]) => $_[1]->content->present ($_[0]) } sub view_head2 { title_tag head2 => $_[1]->title->present ($_[0]) => $_[1]->content->present ($_[0]) } sub view_head3 { title_tag head3 => $_[1]->title->present ($_[0]) => $_[1]->content->present ($_[0]) } sub view_head4 { title_tag head4 => $_[1]->title->present ($_[0]) => $_[1]->content->present ($_[0]) } sub view_over { stag over => indent => $_[1]->indent, $_[1]->content->present ($_[0]) } sub view_begin { stag begin => format => $_[1]->format, $_[1]->content->present ($_[0]) } sub view_verbatim { stag verbatim => PApp::XML::xml_cdata $_[1] } sub view_textblock { stag para => $_[1] } sub view_seq_code { stag code => $_[1] } sub view_seq_bold { stag bold => $_[1] } sub view_seq_italic { stag italic => $_[1] } sub view_seq_link { stag link => $_[1] } sub view_seq_index { stag index => $_[1] } sub view_seq_file { stag file => $_[1] } sub view_seq_zero { "" } sub view_seq_space { PApp::XML::xml_quote $_[1] } sub view_seq_text { PApp::XML::xml_quote $_[1] } sub view_seq_entity { PApp::XML::xml_quote $_[1] } } sub pod2xml($) { my ($pod) = @_; return "" if not $pod; require Pod::POM; my $parser = Pod::POM->new or die "Couldn't create POM object"; my $pom = $parser->parse_text ("=pod\n\n".$pod) or die $parser->error (); $pom->present (PApp::XML::Pod2xml::); } =back =head2 The PApp::XML Factory Class =over 4 =item new PApp::XML parameter => value... Creates a new PApp::XML template object with the specified behaviour. It can be used as an object factory to create new C objects. special a hashref containing special => coderef pairs. If a special is encountered, the given coderef will be compiled in instead (i.e. it will be called each time the fragment is print'ed). The coderef will be called with a reference to the attribute hash, the element's contents (as a string) and the PApp::XML::Template object used to print the string. If a reference to a coderef is given (e.g. C<\sub {}>), the coderef will be called during parsing and the resulting string will be added to the compiled subroutine. The arguments are the same, except that the contents are not given as string but as a magic token that must be inserted into the return value. The return value is expected to be in "phtml" (L) format, the magic "contents" token must not occur in code sections. html html output mode enable flag At the moment there is one predefined special named C, that maps almost directly into a call to slink (a leading underscore in an attribute name gets changed into a minus (C<->) to allow for one-shot arguments), e.g: Do it to Bill! might get changed to (note that C is treated specially): slink "Do it to Bill!", "kill", -doit => 1, name => "Bill"; In a XSLT stylesheet one could define: Which defines a C element that can be used like this: Kill Bill! =cut sub new($;%) { require PApp; my $class = shift, my %args = @_; my $self = bless {}, $class; $self->{attr} = delete $args{attr} || {}; $self->{html} = delete $args{html} || {}; $self->{special} = { slink => sub { my ($attr, $content) = @_; my %attr = %$attr; my $sublink = delete $attr{sublink}; my @args = delete $attr{module}; while (my ($k, $v) = each %attr) { $k =~ s/^_/-/; push @args, $k, $v; } PApp::echo ($sublink eq "yes") ? PApp::sublink ([PApp::current_locals ()], $content, @args) : PApp::slink ($content, @args); }, %{delete $args{special} || {}}, }; $self; } =item $pappxml->dom2template($dom, {special}, key => value...) Compile the given DOM into a C object and returns it. An additional set of specials only used to parse this dom can be passed as a hashref (this argument is optional). Additional key => value pairs will be added to the template's attribute hash. The template will be evaluated in the caller's package (e.g. to get access to __ and similar functions). On error, nothing is returned. Use the C method to get more information about the problem. In addition to the syntax accepted by C, this function evaluates certain XML Elements (please note that I consider the "papp" namespace to be reserved): papp:special _special="special-name" attributes... Evaluate the special with the name given by the attribute C<_special> after evaluating its content. The special will receive two arguments: a hashref with all additional attributes and a string representing an already evaluated code fragment. papp:unquote Expands ("unquotes") some (but not all) entities, namely lt, gt, amp, quot, apos. This can be easily used within a stylesheet to create verbatim html or perl sections, e.g. ]]> A XSLT stylesheet that converts sections just like in papp files might look like this: =begin comment attr a hashref with attribute => value pairs. These attributes can later be quieried and set using the C method. =end comment =cut sub dom2template($$;%) { my $self = shift; my $dom = shift; my $temp = bless { attr => {@_}, }, PApp::XML::Template::; my $package = (caller)[0]; $temp->{code} = $temp->_dom2sub($dom, $self, $package); delete $temp->{attr}{special}; if ($temp->{code}) { $temp; } else { # error (); } } =item $err = $pappxml->error Return information about an error as an C object (L). =cut sub error { my $self = shift; $self->{error}; } package PApp::XML::Template; use PApp::PCode (); our $_res; sub __dom2sub($) { my $node = $_[0]->getFirstChild; while ($node) { my $type = $node->getNodeType; if ($type == &XML::DOM::TEXT_NODE || $type == &XML::DOM::CDATA_SECTION_NODE) { $_res .= $node->toString; } elsif ($type == &XML::DOM::ELEMENT_NODE) { my $name = $node->getTagName; my %attr; { my $attrs = $node->getAttributes; for (my $n = $attrs->getLength; $n--; ) { my $attr = $attrs->item($n); $attr{$attr->getName} = $attr->getValue; } } if (substr($name, 0, 5) eq "papp:") { if ($name eq "papp:special") { my $name = delete $attr{_special}; my $sub = $_self->{attr}{special}{$name} || $_factory->{special}{$name}; if (defined $sub) { my $idx = @$_local; if (ref $sub eq "REF") { push @$_local, $_self->_dom2sub($node, $_factory, $_package); $_res .= $$sub->( \%attr, '<:$_dom2sub_local['.($idx).']():>', $_self, ); } else { push @$_local, $sub; push @$_local, \%attr; push @$_local, $_self->_dom2sub($node, $_factory, $_package); $_res .= '<: $_dom2sub_local['.($idx).']( $_dom2sub_local['.($idx+1).'], PApp::capture { $_dom2sub_local['.($idx+2).']() }, $_dom2sub_self, ) :>'; } } else { $_res .= "<<< undefined special '$name' containing '"; __dom2sub($node); $_res .= "' >>>"; } } elsif ($name eq "papp:unquote") { my $res = do { local $_res = ""; __dom2sub($node); $_res; }; $_res .= PApp::XML::xml_unquote $res; } else { $_res .= "<<< undefined papp element '$name' containing '"; __dom2sub($node); $_res .= "' >>>"; } } else { $_res .= "<$name"; while (my ($k, $v) = each %attr) { # we prefer single quotes, since __ and N_ do not $v =~ s/'/'/g; $_res .= " $k='$v'"; } my $content = do { local $_res = ""; __dom2sub($node); $_res; }; if ($content ne "") { $_res .= ">$content"; } elsif ($_factory->{html}) { if ($name =~ /^br|p|hr|img|meta|base|link$/i) { $_res .= ">"; } else { $_res .= ">"; } } else { $_res .= "/>"; } } } $node = $node->getNextSibling; } } sub _dom2sub($$$$) : locked { local $_self = shift; local $_dom = shift; local $_factory = shift; local $_package = shift; my @_dom2sub_local; local $_local = \@_dom2sub_local; local $_res = ""; __dom2sub($_dom); my $_dom2sub_self = $_self; my $_dom2sub_str = <{error} = new PApp::Exception error => $@, info => $_dom2sub_str; return; } else { delete $_factory->{error}; return $sub; } } =item $template->localvar([content]) [WIZARDRY] Create a local variable that can be used inside specials and return a string representation of it (i.e. a magic token that represents the lvalue of the variable when compiled). Can only be called during compilation. =cut sub localvar($$;$) { my ($self, $val) = @_; my $idx = @$_local; push @$_local, $val; '$_dom2sub_local['.($idx).']'; } =item $template->gen_surl() [WIZARDY] Returns a string representing a perl statement returning the surl. =cut sub gen_surl($;@) { my $self = shift; my $var = $self->localvar(\@_); "surl(\@{$var})"; } =item $template->gen_slink() [WIZARDY] Returns a string representing a perl statement returning the slink. =cut sub gen_slink($;@) { my $self = shift; my $content = $self->localvar(shift); my $surl = $self->gen_surl($content); "slink($content, $surl)"; } =item $template->attr(key, [newvalue]) Return the attribute value for the given key. If C is given, replaces the attribute and returns the previous value. =cut sub attr($$;$) { my $self = shift; my $key = shift; my $val = $self->{attr}{$key}; $self->{attr}{$key} = shift if @_; $val; } =item $template->print Print (and execute any required specials). You can capture the output using the C function. =cut sub print($) { shift->{code}(); } 1; =back =head1 Wizard Example In this section I'll try to sketch out a "wizard example" that shows how C could be used in the real world. Consider an application that fetches most or all content (even layout) from a database and uses a stylesheet to map xml content to html, which allows for almost total seperation of layout and content. It would have an init section loading a XSLT stylesheet and defining a content factory: use XML::XSLT; # ugly module, but it works great! use PApp::XML; # create the parser my $xsl = "$PApp::Config{LIBDIR}/stylesheet.xsl"; $xslt_parser = XML::XSLT->new($xsl, "FILE"); # create a content factory $tt_content_factory = new PApp::XML html => 1, # we want html output special => { include => sub { my ($attr, $content) = @_; get_content($attr->{name})->print; }, }; # create a cache (XSLT is quite slow) use Tie::Cache; tie %content_cache, Tie::Cache::, { MaxCount => 30, WriteSync => 0}; Here we define an C special that inserts another document inplace. How does C (see the definition of C) look like? [1] < time) { $cache->[0] = fetch_content $name, $special; $cache->[1] = time + 10; } $cache->[0]; :>]]> C is nothing more but a wrapper around C. It's sole purpose is to cache documents since parsing and transforming a xml file is quite slow (please note that I include the current language when caching documents since, of course, the documents get translated). In non-speed-critical applications you could just substitute C for C: $special, id => $id, name => $name, ctime => $ctime, lang => $lang, }); :>]]> C actually fetches the content string from the database. In this example, a content object has a name (which is used to reference it) a timestamp and a body, which is the actual document. After fetching the content object it uses C to transform the xml snippet into a perl sub that can be efficiently executed: transform_document( ''. "". $body. "", "STRING" ); my $dom = $xslt_parser->result_tree; $tt_content_factory->dom2template($dom, %$attr); }; if ($@) { my $line = $@ =~ /mismatched tag at line (\d+), column \d+, byte \d+/ ? $1 : -1; # create a fancy error message } $content || parse_content(""); :>]]> As you can see, it uses XSLT's C, which does the string -> DOM translation for us, and also transforms the XML code through the stylesheet. After that it uses C to compile the document into perl code and returns it. An example stylesheet would look like this: # add the earlier XSLT examples here. This stylesheet would transform the following XML snippet:

Look at our rubber-wobber-cake before it is sold out!

Which would be turned into something like this:

Look at our rubber-wobber-cake before it is sold out!

Now go back and try to understand the above code! But wait! Consider that you had a content editor installed as the module C, as I happen to have. Now lets introduce the C macro: #if access_p "admin"
<: sql_fetch \my($id), "select id from content where name = ?", $name; if ($id) { :> $id:><: } else { :> $name:><: } $content = get_content($name,\%special); $content->print; :>
#else <: $content = get_content($name,\%special); $content->print; :> #endif <: return $content; :>]]>
What does this do? Easy: If you are logged in as admin (i.e. have the "admin" access right), it displays a link that lets you edit the object directly. As normal user it just displays the content as-is. It could be used like this: $content->ctime; ]]> Disregarding C
and C