| 1 | package Apache::XBEL; | ||
| 2 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | |||
| 4 | # $Id: XBEL.pm,v 1.11 2004/03/01 21:25:11 asc Exp $ | ||
| 5 | |||
| 6 | =head1 NAME | ||
| 7 | |||
| 8 | Apache::XBEL - mod_perl handler to transform XBEL into exciting | ||
| 9 | and foofy HTML documents. | ||
| 10 | |||
| 11 | =head1 SYNOPSIS | ||
| 12 | |||
| 13 | <Location /> | ||
| 14 | SetHandler perl-script | ||
| 15 | PerlHandler Apache::XBEL | ||
| 16 | |||
| 17 | PerlSetVar XbelPath /path/to/yer-xbel-file.xbel | ||
| 18 | PerlSetVar XslPath /path/to/apache-xbel.xsl | ||
| 19 | PerlSetVar CacheDir /path/to/yer-cache-dir | ||
| 20 | |||
| 21 | # If set to "On", output-escaping will be disabled | ||
| 22 | # for title and description nodes in the XSL stylesheet | ||
| 23 | |||
| 24 | PerlSetVar DisableEscaping On | ||
| 25 | |||
| 26 | </Location> | ||
| 27 | |||
| 28 | =head1 DESCRIPTION | ||
| 29 | |||
| 30 | Apache::XBEL is an Apache mod_perl handler that uses XSLT to | ||
| 31 | transform XML Bookmarks Exchange Language (XBEL) files into | ||
| 32 | exciting and foofy dynamic HTML documents. | ||
| 33 | |||
| 34 | Documents are rendered as collapsible outlines and individual | ||
| 35 | nodes may be viewed and bookmarked as unique pages, so you don't | ||
| 36 | have to click through a gazillion nested leaves to find what | ||
| 37 | you're looking for. | ||
| 38 | |||
| 39 | Once individual nodes/pages have been rendered, they are cached | ||
| 40 | to reduce the load on the server. Cache files are updated | ||
| 41 | whenever any of the widgets involved in the transformation are | ||
| 42 | modified. | ||
| 43 | |||
| 44 | =head1 OPTIONS | ||
| 45 | |||
| 46 | =head2 XbelPath | ||
| 47 | |||
| 48 | I<required> | ||
| 49 | |||
| 50 | The path to the XBEL file you are transforming. | ||
| 51 | |||
| 52 | =head2 XslPath | ||
| 53 | |||
| 54 | I<required> | ||
| 55 | |||
| 56 | The path to the XSL file used to do the transforming. | ||
| 57 | |||
| 58 | =head2 CacheDir | ||
| 59 | |||
| 60 | I<required> | ||
| 61 | |||
| 62 | The path to a directory where the mod_perl interpreter can | ||
| 63 | write cache files. | ||
| 64 | |||
| 65 | =head2 DisableEscaping | ||
| 66 | |||
| 67 | If set to "On" (case-insenstive) output-escaping will be | ||
| 68 | disabled for title and description nodes in the XSL | ||
| 69 | stylesheet. | ||
| 70 | |||
| 71 | =head2 HtmlLang | ||
| 72 | |||
| 73 | Set this as the value of /html[@xml:lang] | ||
| 74 | |||
| 75 | =cut | ||
| 76 | |||
| 77 | $Apache::XBEL::VERSION = '1.3'; | ||
| 78 | |||
| 79 | 0 | ▶ | use Apache; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 80 | use Apache::Constants qw(:common :response); | ||
| 81 | use Apache::File; | ||
| 82 | use Apache::Log; | ||
| 83 | use Apache::URI; | ||
| 84 | |||
| 85 | use Digest::MD5 qw(md5_hex); | ||
| 86 | |||
| 87 | use File::Basename; | ||
| 88 | use File::Copy; | ||
| 89 | use File::Spec; | ||
| 90 | |||
| 91 | use Memoize; | ||
| 92 | |||
| 93 | use XML::LibXML; | ||
| 94 | use XML::LibXSLT; | ||
| 95 | |||
| 96 | my $xml_parser; | ||
| 97 | my $xsl_parser; | ||
| 98 | my $xsl_stylesheet; | ||
| 99 | my $xsl_transformer; | ||
| 100 | |||
| 101 | memoize ("cache_file"); | ||
| 102 | |||
| 103 | sub handler { | ||
| 104 | my $apache = shift; | ||
| 105 | $apache->register_cleanup(sub{&cleanup($apache)}); | ||
| 106 | |||
| 107 | # | ||
| 108 | |||
| 109 | my $root = $apache->location(); | ||
| 110 | my $root_uri = $apache->uri($root); | ||
| 111 | $root_uri =~ /($root)(.*)/; | ||
| 112 | my $path = $2; | ||
| 113 | |||
| 114 | $path =~ s/\/$//; | ||
| 115 | $path =~ s/^\///; | ||
| 116 | $root = &basename($root); | ||
| 117 | |||
| 118 | # | ||
| 119 | |||
| 120 | my $uri = Apache::URI->parse($apache); | ||
| 121 | my $uri_scheme = $uri->scheme(); | ||
| 122 | my $uri_host = $uri->hostname(); | ||
| 123 | |||
| 124 | if (! ($root_uri =~ /(.*)\/$/)) { | ||
| 125 | my $redirect = "$uri_scheme://$uri_host$root_uri/"; | ||
| 126 | $apache->headers_out->set("Location"=>$redirect); | ||
| 127 | return REDIRECT; | ||
| 128 | } | ||
| 129 | |||
| 130 | # | ||
| 131 | |||
| 132 | my $xsl_file = $apache->dir_config("XslPath"); | ||
| 133 | |||
| 134 | if (! -f $xsl_file) { | ||
| 135 | $apache->log->error("Unable to locate '$xsl_file'.\n"); | ||
| 136 | return NOT_FOUND; | ||
| 137 | } | ||
| 138 | |||
| 139 | # | ||
| 140 | |||
| 141 | if (! -d $apache->dir_config("CacheDir")) { | ||
| 142 | $apache->log->error("Unable to locate the cache directory.\n"); | ||
| 143 | return SERVER_ERROR; | ||
| 144 | } | ||
| 145 | |||
| 146 | # | ||
| 147 | |||
| 148 | $xml_parser ||= XML::LibXML->new(); | ||
| 149 | |||
| 150 | if (! $xml_parser) { | ||
| 151 | $apache->log->alert("Unable to create XML::LibXML object. $!"); | ||
| 152 | return SERVER_ERROR; | ||
| 153 | } | ||
| 154 | |||
| 155 | # | ||
| 156 | |||
| 157 | $xsl_parser ||= XML::LibXSLT->new(); | ||
| 158 | |||
| 159 | if (! $xsl_parser) { | ||
| 160 | $apache->log->alert("Unable to create XML::LibXSLT object. $!"); | ||
| 161 | return SERVER_ERROR; | ||
| 162 | } | ||
| 163 | |||
| 164 | # | ||
| 165 | |||
| 166 | $xsl_stylesheet ||= $xml_parser->parse_file($xsl_file); | ||
| 167 | |||
| 168 | if (! $xsl_stylesheet) { | ||
| 169 | $apache->log->alert("Failed to parse file $xsl_file"); | ||
| 170 | return SERVER_ERROR; | ||
| 171 | }; | ||
| 172 | |||
| 173 | # | ||
| 174 | |||
| 175 | $xsl_transformer ||= $xsl_parser->parse_stylesheet($xsl_stylesheet); | ||
| 176 | |||
| 177 | if (! $xsl_transformer) { | ||
| 178 | $apache->log->alert("Failed to parse stylesheet object."); | ||
| 179 | return SERVER_ERROR; | ||
| 180 | } | ||
| 181 | |||
| 182 | # | ||
| 183 | |||
| 184 | my $xbel_file = &load_file($apache->dir_config("XbelPath")); | ||
| 185 | |||
| 186 | # | ||
| 187 | |||
| 188 | my ($cache_file,$exists) = &fetch_cache($apache,$xbel_file,$path); | ||
| 189 | |||
| 190 | if ($exists) { | ||
| 191 | $apache->content_type("text/html"); | ||
| 192 | $apache->send_http_header(); | ||
| 193 | $apache->send_fd( Apache::File->new($cache_file) ); | ||
| 194 | return OK; | ||
| 195 | } | ||
| 196 | |||
| 197 | # | ||
| 198 | |||
| 199 | my $xbel_doc = $xml_parser->parse_file($xbel_file); | ||
| 200 | |||
| 201 | if (! $xbel_doc) { | ||
| 202 | $apache->log->alert("Unable to parse XBEL document : $!\n"); | ||
| 203 | return SERVER_ERROR; | ||
| 204 | } | ||
| 205 | |||
| 206 | # Note that we actually make use of @path | ||
| 207 | # below. We're not just cluttering up the | ||
| 208 | # symbol table for crazy reasons like, oh | ||
| 209 | # I don't know, increased readability... | ||
| 210 | |||
| 211 | my @path = File::Spec->splitdir($path); | ||
| 212 | my $lookup = &path2node(@path); | ||
| 213 | |||
| 214 | my $nodes = ($xbel_doc->findnodes($lookup))[0]; | ||
| 215 | |||
| 216 | if (! $nodes) { | ||
| 217 | $apache->log->error("Lookup for '$lookup' failed.\n"); | ||
| 218 | return NOT_FOUND; | ||
| 219 | } | ||
| 220 | |||
| 221 | # | ||
| 222 | |||
| 223 | if ($nodes->getName eq "xbel") { | ||
| 224 | |||
| 225 | if (! copy($xbel_file,$cache_file)) { | ||
| 226 | |||
| 227 | $apache->log->error("Failed to copy '$xbel_file' to '$cache_file', $!\n"); | ||
| 228 | return SERVER_ERROR; | ||
| 229 | } | ||
| 230 | } | ||
| 231 | |||
| 232 | elsif (! &render_slice($apache,$cache_file,$xbel_doc,$nodes)) { | ||
| 233 | |||
| 234 | # error is logged in function | ||
| 235 | return SERVER_ERROR; | ||
| 236 | } | ||
| 237 | |||
| 238 | else { } | ||
| 239 | |||
| 240 | # Set up some variables that | ||
| 241 | # we'll use later | ||
| 242 | |||
| 243 | my $loc = $apache->location(); | ||
| 244 | $loc =~ s/\/$//; | ||
| 245 | |||
| 246 | # XSLT parameters | ||
| 247 | |||
| 248 | my %params = (); | ||
| 249 | |||
| 250 | $params{ "base" } = | ||
| 251 | "'$uri_scheme://$uri_host". | ||
| 252 | join("/",$loc,@path[0..($#path - 1)]). | ||
| 253 | "/'"; | ||
| 254 | |||
| 255 | $params{ "escaping" } = | ||
| 256 | ($apache->dir_config("DisableEscaping") =~ /^(on)$/i) ? 1 : 0; | ||
| 257 | |||
| 258 | if (my $lang = $apache->dir_config("HtmlLang")) { | ||
| 259 | $params{ "lang" } = "'$lang'"; | ||
| 260 | } | ||
| 261 | |||
| 262 | # XSLT functions | ||
| 263 | |||
| 264 | my $i = 0; | ||
| 265 | |||
| 266 | my @breadcrumbs = ("root",@path); | ||
| 267 | |||
| 268 | my @hrefs = map { | ||
| 269 | join("/",@breadcrumbs[1..$i++]); | ||
| 270 | } @breadcrumbs; | ||
| 271 | |||
| 272 | $xsl_parser->register_function("urn:aaronstraupcope:apache:xbel", | ||
| 273 | "breadcrumbs", | ||
| 274 | sub { return shift @breadcrumbs; }); | ||
| 275 | |||
| 276 | $xsl_parser->register_function("urn:aaronstraupcope:apache:xbel", | ||
| 277 | "href_for_crumb", | ||
| 278 | sub { return join("/", | ||
| 279 | "$uri_scheme://$uri_host$loc", | ||
| 280 | shift @hrefs); }); | ||
| 281 | |||
| 282 | # Munge munge munge | ||
| 283 | |||
| 284 | my $xmldoc = $xml_parser->parse_file($cache_file); | ||
| 285 | my $html = $xsl_transformer->transform($xmldoc,%params); | ||
| 286 | |||
| 287 | $xsl_transformer->output_file($html,$cache_file); | ||
| 288 | |||
| 289 | # Send the stupid file, already | ||
| 290 | |||
| 291 | $apache->content_type("text/html"); | ||
| 292 | $apache->send_http_header(); | ||
| 293 | |||
| 294 | $apache->send_fd(Apache::File->new($cache_file)); | ||
| 295 | return OK; | ||
| 296 | } | ||
| 297 | |||
| 298 | sub cleanup { | ||
| 299 | my $apache = shift; | ||
| 300 | return 1; | ||
| 301 | } | ||
| 302 | |||
| 303 | sub cache_file { | ||
| 304 | my $apache = shift; | ||
| 305 | my $file = shift; | ||
| 306 | my $path = shift; | ||
| 307 | |||
| 308 | my $hex = &md5_hex(join("#",$file,$path)); | ||
| 309 | return File::Spec->catfile($apache->dir_config("CacheDir"),$hex); | ||
| 310 | } | ||
| 311 | |||
| 312 | sub fetch_cache { | ||
| 313 | my $apache = shift; | ||
| 314 | my $file = shift; | ||
| 315 | my $path = shift; | ||
| 316 | |||
| 317 | my $cache = &cache_file($apache,$file,$path); | ||
| 318 | |||
| 319 | # Check for existence | ||
| 320 | |||
| 321 | if (! -e $cache) { | ||
| 322 | $apache->log->debug("Cachefile '$cache' does not exist."); | ||
| 323 | return ($cache,0); | ||
| 324 | } | ||
| 325 | |||
| 326 | # | ||
| 327 | |||
| 328 | my $cache_mtime = (stat($cache))[9]; | ||
| 329 | |||
| 330 | if ((stat(__FILE__))[9] > $cache_mtime ) { | ||
| 331 | $apache->log->debug("Cache is out of sync with handler."); | ||
| 332 | return ($cache,0); | ||
| 333 | } | ||
| 334 | |||
| 335 | if ((stat($file))[9] > $cache_mtime ) { | ||
| 336 | $apache->log->debug("Cache is out of sync with XBEL file."); | ||
| 337 | return ($cache,0); | ||
| 338 | } | ||
| 339 | |||
| 340 | my $xsl_file = $apache->dir_config("XslPath"); | ||
| 341 | |||
| 342 | if ((stat($xsl_file))[9] > $cache_mtime) { | ||
| 343 | $apache->log->debug("Cache is out of sync with stylesheet."); | ||
| 344 | |||
| 345 | $xsl_stylesheet = $xml_parser->parse_file($xsl_file); | ||
| 346 | $xsl_transformer = $xsl_parser->parse_stylesheet($xsl_stylesheet); | ||
| 347 | |||
| 348 | return ($cache,0); | ||
| 349 | } | ||
| 350 | |||
| 351 | return ($cache,1); | ||
| 352 | } | ||
| 353 | |||
| 354 | sub path2node { | ||
| 355 | my @path = map { "folder[\@id=\"$_\"]"; } @_; | ||
| 356 | return join("/","","xbel",@path); | ||
| 357 | } | ||
| 358 | |||
| 359 | sub render_slice { | ||
| 360 | my $apache = shift; | ||
| 361 | my $file = shift; | ||
| 362 | my $doc = shift; | ||
| 363 | my $node = shift; | ||
| 364 | |||
| 365 | # | ||
| 366 | |||
| 367 | my $owner = $doc->find(qq(/xbel/info/metadata[\@owner])); | ||
| 368 | my $ver = $doc->version(); | ||
| 369 | my $enc = $doc->encoding(); | ||
| 370 | |||
| 371 | my $dom = XML::LibXML::Document->createDocument($ver,$enc); | ||
| 372 | my $xbel = XML::LibXML::Element->new(qq(xbel)); | ||
| 373 | |||
| 374 | my $info = XML::LibXML::Element->new(qq(info)); | ||
| 375 | my $meta = XML::LibXML::Element->new(qq(metadata)); | ||
| 376 | $meta->setAttribute("owner",$owner); | ||
| 377 | |||
| 378 | my $title = XML::LibXML::Element->new(qq(title)); | ||
| 379 | $title->appendText($doc->find(qq(/xbel/title))); | ||
| 380 | |||
| 381 | my $desc = XML::LibXML::Element->new(qq(desc)); | ||
| 382 | $desc->appendText($doc->find(qq(/xbel/desc))); | ||
| 383 | |||
| 384 | $info->appendChild($meta); | ||
| 385 | $xbel->appendChild($title); | ||
| 386 | $xbel->appendChild($info); | ||
| 387 | $xbel->appendChild($desc); | ||
| 388 | |||
| 389 | # Add support to expand <alias> | ||
| 390 | # elements here. This is slated | ||
| 391 | # for version 1.5 | ||
| 392 | |||
| 393 | $xbel->appendChild($node); | ||
| 394 | $dom->setDocumentElement($xbel); | ||
| 395 | |||
| 396 | # | ||
| 397 | |||
| 398 | if (my $fh = &get_fh($apache,$file)) { | ||
| 399 | |||
| 400 | # WTF doesn't Apache::File | ||
| 401 | # have a print method? | ||
| 402 | |||
| 403 | print $fh $dom->toString(); | ||
| 404 | undef $dom; | ||
| 405 | |||
| 406 | $fh->close(); | ||
| 407 | return 1; | ||
| 408 | } | ||
| 409 | |||
| 410 | return 0; | ||
| 411 | } | ||
| 412 | |||
| 413 | sub get_fh { | ||
| 414 | my $apache = shift; | ||
| 415 | my $file = shift; | ||
| 416 | |||
| 417 | my $fh = Apache::File->new(); | ||
| 418 | |||
| 419 | if (! $fh->open(">$file")) { | ||
| 420 | $apache->log->error("Failed to open $file for writing : $!\n"); | ||
| 421 | return SERVER_ERROR; | ||
| 422 | } | ||
| 423 | |||
| 424 | return &lock_cache($apache,$fh); | ||
| 425 | } | ||
| 426 | |||
| 427 | sub lock_cache { | ||
| 428 | my $apache = shift; | ||
| 429 | my $fh = shift; | ||
| 430 | |||
| 431 | my $success = 0; | ||
| 432 | my $tries = 0; | ||
| 433 | |||
| 434 | while ($tries++ < 10) { | ||
| 435 | return $fh if ($success = flock($fh,2)); | ||
| 436 | sleep(1); | ||
| 437 | } | ||
| 438 | |||
| 439 | $apache->log->error("Failed to lock file for writing."); | ||
| 440 | return undef; | ||
| 441 | } | ||
| 442 | |||
| 443 | # This is here so that, eventually, it can | ||
| 444 | # be sub-classed. | ||
| 445 | |||
| 446 | sub load_file { | ||
| 447 | return $_[0]; | ||
| 448 | } | ||
| 449 | |||
| 450 | =head1 VERSION | ||
| 451 | |||
| 452 | 1.3 | ||
| 453 | |||
| 454 | =head1 DATE | ||
| 455 | |||
| 456 | $Date: 2004/03/01 21:25:11 $ | ||
| 457 | |||
| 458 | =head1 AUTHOR | ||
| 459 | |||
| 460 | Aaron Straup Cope <ascope@cpan.org> | ||
| 461 | |||
| 462 | =head1 SEE ALSO | ||
| 463 | |||
| 464 | http://pyxml.sourceforge.net/topics/xbel/ | ||
| 465 | |||
| 466 | http://aaronland.info/perl/apache/xbel/example/1.3 | ||
| 467 | |||
| 468 | http://aaronland.info/xsl/xbel/apache-xbel | ||
| 469 | |||
| 470 | =head1 NOTES | ||
| 471 | |||
| 472 | =over 4 | ||
| 473 | |||
| 474 | =item * | ||
| 475 | |||
| 476 | If you are running this handler on a server that is also running | ||
| 477 | AxKit, pre version 1.5, Apache::XBEL may periodically fail and | ||
| 478 | return a server error. Some reports have suggested that reloading | ||
| 479 | the page may cause the widget to load properly. Or not. | ||
| 480 | |||
| 481 | =item * | ||
| 482 | |||
| 483 | Hooks for munging outliner documents with Text::Outline have been | ||
| 484 | removed as of release 1.3. They may come back, at a later date, in | ||
| 485 | a separate Apache::XBEL::Outline package. | ||
| 486 | |||
| 487 | =back | ||
| 488 | |||
| 489 | =head1 TO DO | ||
| 490 | |||
| 491 | =over 4 | ||
| 492 | |||
| 493 | =item * | ||
| 494 | |||
| 495 | Replace nested 'div' elements with some flavour of nested lists and | ||
| 496 | de-couple CSS from apache-xbel.xsl. De-couple JavaScript from | ||
| 497 | apache-xbel.xsl These changes are slated for version 1.4 | ||
| 498 | |||
| 499 | =item * | ||
| 500 | |||
| 501 | Support for expanding <alias> elements. This is slated for version | ||
| 502 | 1.5 | ||
| 503 | |||
| 504 | =item * | ||
| 505 | |||
| 506 | Support for mod_perl 2.0. This is slated for version 2.0 | ||
| 507 | |||
| 508 | =back | ||
| 509 | |||
| 510 | =head1 BUGS | ||
| 511 | |||
| 512 | Please report all bugs to : http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Apache::XBEL | ||
| 513 | |||
| 514 | =head1 LICENSE | ||
| 515 | |||
| 516 | Copyright (c) 2001-2004 Aaron Straup Cope. All Rights Reserved. | ||
| 517 | |||
| 518 | This is free software, you may use it and distribute it under | ||
| 519 | the same terms as Perl itself. | ||
| 520 | |||
| 521 | =cut | ||
| 522 | |||
| 523 | return 1; | ||