| 1 | # -*- perl -*- | ||
| 2 | |||
| 3 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use utf8; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 1 | ▶ | use Fcntl (); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 1 | ▶ | use URI (); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 1 | ▶ | use LWP::Simple (); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 0 | ▶ | use XML::DOM (); |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 9 | use XML::EP::Processor::XSLTParser (); | ||
| 10 | |||
| 11 | package XML::EP::Processor::XSLT; | ||
| 12 | |||
| 13 | sub new { | ||
| 14 | my $proto = shift; | ||
| 15 | my $self = (@_ == 1) ? \%{ shift() } : { @_ }; | ||
| 16 | bless($self, (ref($proto) || $proto)); | ||
| 17 | |||
| 18 | } | ||
| 19 | |||
| 20 | sub Process { | ||
| 21 | my($self, $req, $xml) = @_; | ||
| 22 | |||
| 23 | die "Missing href attribute in stylesheet declaration" | ||
| 24 | unless $self->{'pidata'} =~ /\bhref=\"(.*?)\"/; | ||
| 25 | my $url = $1; | ||
| 26 | my $base = $req->Request()->Uri(); | ||
| 27 | $url = URI::URL->new($url, $base)->abs(); | ||
| 28 | my $content = LWP::Simple::get($url) | ||
| 29 | || die "Failed to access stylesheet $url.\n"; | ||
| 30 | my $parser = XML::DOM::Parser->new(); | ||
| 31 | my $stylesheet = $parser->parse($content); | ||
| 32 | |||
| 33 | my $xslt = XML::EP::Processor::XSLTParser->new('xmlDocument' => $xml, | ||
| 34 | 'xslDocument' => $stylesheet); | ||
| 35 | |||
| 36 | my $result = $xslt->process_project(); | ||
| 37 | |||
| 38 | # The XSLT parser returns a document fragment. We have to replace | ||
| 39 | # the old document contents with the document fragments. | ||
| 40 | while (my $child = $xml->getFirstChild()) { | ||
| 41 | $xml->removeChild($child); | ||
| 42 | } | ||
| 43 | my $child = $result->getFirstChild(); | ||
| 44 | while ($child) { | ||
| 45 | my $c = $child; | ||
| 46 | $child = $c->getNextSibling(); | ||
| 47 | next if $c->getNodeType() == XML::DOM::TEXT_NODE; # Skip blanks | ||
| 48 | $result->removeChild($c); | ||
| 49 | $xml->appendChild($c); | ||
| 50 | } | ||
| 51 | $xml; | ||
| 52 | } | ||
| 53 | |||
| 54 | 1; | ||