| 1 | package Web::App::Presenter::XSLT; | ||
| 2 | # $Id: XSLT.pm,v 1.18 2009/06/09 08:14:43 apla Exp $ | ||
| 3 | |||
| 4 | use Class::Easy; | ||
| 5 | |||
| 6 | use Web::App::Presenter; | ||
| 7 | use base qw(Web::App::Presenter); | ||
| 8 | |||
| 9 | use XML::LibXML; | ||
| 10 | use XML::LibXSLT; | ||
| 11 | |||
| 12 | use Data::Dump::XML; | ||
| 13 | use Data::Dump::XML::Parser; | ||
| 14 | |||
| 15 | use IO::Easy; | ||
| 16 | use File::Spec; | ||
| 17 | |||
| 18 | use Web::App; | ||
| 19 | |||
| 20 | has 'template_dir'; | ||
| 21 | |||
| 22 | our $PARSED = {}; | ||
| 23 | |||
| 24 | sub headers { | ||
| 25 | my $app = Web::App->app; | ||
| 26 | my $headers = $app->response->headers; | ||
| 27 | $headers->header ('Content-Type' => 'text/html; charset=utf-8'); | ||
| 28 | $headers->header ('Cache-Control' => 'no-store'); | ||
| 29 | } | ||
| 30 | |||
| 31 | |||
| 32 | # -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- | ||
| 33 | sub web_app_expand { | ||
| 34 | my $object = shift; | ||
| 35 | } | ||
| 36 | |||
| 37 | sub _init { | ||
| 38 | my $self = shift; | ||
| 39 | |||
| 40 | my $app = Web::App->app; | ||
| 41 | |||
| 42 | my $template_set = $self->{'template-set'}; | ||
| 43 | my $local_path = $self->{'local-path'}; | ||
| 44 | $local_path = 'share/presentation' | ||
| 45 | unless defined $local_path; | ||
| 46 | |||
| 47 | debug "local path is: '$local_path'"; | ||
| 48 | |||
| 49 | my $dir = IO::Easy->new ($app->root); | ||
| 50 | $dir->append_in_place ($local_path, $template_set); | ||
| 51 | |||
| 52 | $self->{template_dir} = $dir; | ||
| 53 | |||
| 54 | $dir->as_dir->scan_tree (sub { | ||
| 55 | my $file = shift; | ||
| 56 | |||
| 57 | return 1 if $file->type eq 'dir'; | ||
| 58 | |||
| 59 | return if ! $file->extension or $file->extension !~ /xslt?/; | ||
| 60 | |||
| 61 | $self->parse_stylesheet ($file); | ||
| 62 | }); | ||
| 63 | } | ||
| 64 | |||
| 65 | sub locate_stylesheet { | ||
| 66 | my $self = shift; | ||
| 67 | my $app = shift; | ||
| 68 | |||
| 69 | my $presentation = shift; | ||
| 70 | |||
| 71 | my $file = $presentation->{'file'}; | ||
| 72 | |||
| 73 | critical "Web::App::Request not defined: we can't detect stylesheet file name" | ||
| 74 | unless $app->request; | ||
| 75 | |||
| 76 | if (File::Spec->file_name_is_absolute ($presentation->{'file'})) { | ||
| 77 | $file = IO::Easy->new ($presentation->{'file'}); | ||
| 78 | } else { | ||
| 79 | $file = $self->template_dir->append ($presentation->{'file'}); | ||
| 80 | } | ||
| 81 | |||
| 82 | # avoid usage of disk i/o | ||
| 83 | return $file | ||
| 84 | if exists $PARSED->{$file}; | ||
| 85 | |||
| 86 | my $index_path = $self->template_dir->append ($app->request->screen->id, 'index.xsl'); | ||
| 87 | |||
| 88 | $index_path = $self->template_dir->append ($app->request->screen->id . '.xsl') | ||
| 89 | unless -f $index_path; | ||
| 90 | |||
| 91 | return $index_path | ||
| 92 | if exists $PARSED->{$index_path}; | ||
| 93 | |||
| 94 | unless (-f $file) { | ||
| 95 | $file = $index_path; | ||
| 96 | # warn "$file"; | ||
| 97 | } | ||
| 98 | |||
| 99 | critical "we can't find stylesheet file '$file'" | ||
| 100 | unless -f $file; | ||
| 101 | |||
| 102 | return $file; | ||
| 103 | |||
| 104 | } | ||
| 105 | |||
| 106 | sub parse_stylesheet { | ||
| 107 | my $self = shift; | ||
| 108 | my $file = shift; | ||
| 109 | |||
| 110 | my $production = shift || 0; | ||
| 111 | |||
| 112 | # always return parsed stylesheet when in production | ||
| 113 | if (exists $PARSED->{$file} and $production) { | ||
| 114 | return $PARSED->{$file}->{s}; | ||
| 115 | } | ||
| 116 | |||
| 117 | my $mtime = (stat $file)[9]; | ||
| 118 | |||
| 119 | return $PARSED->{$file}->{s} | ||
| 120 | if exists $PARSED->{$file} and $PARSED->{$file}->{m} == $mtime; # and !$Class::Easy::DEBUG; | ||
| 121 | |||
| 122 | my $xslt = XML::LibXSLT->new; | ||
| 123 | |||
| 124 | my $stylesheet; | ||
| 125 | |||
| 126 | my $t = timer ("parsing $file"); | ||
| 127 | |||
| 128 | eval { | ||
| 129 | $stylesheet = $xslt->parse_stylesheet_file ($file); | ||
| 130 | critical "can't parse stylesheet" | ||
| 131 | unless $stylesheet; | ||
| 132 | }; | ||
| 133 | |||
| 134 | $t->end; | ||
| 135 | |||
| 136 | if ($@) { | ||
| 137 | critical "Can't parse stylesheet: $file. Please report to administrator: $@"; | ||
| 138 | } | ||
| 139 | |||
| 140 | debug $mtime; | ||
| 141 | |||
| 142 | $PARSED->{$file} = {s => $stylesheet, m => $mtime}; | ||
| 143 | |||
| 144 | return $stylesheet; | ||
| 145 | } | ||
| 146 | |||
| 147 | sub process { | ||
| 148 | my $self = shift; | ||
| 149 | my $app = shift; | ||
| 150 | my $data = shift; | ||
| 151 | my %params = @_; | ||
| 152 | |||
| 153 | my $t = timer ('dumping xml'); | ||
| 154 | |||
| 155 | my $xml = Data::Dump::XML->new; | ||
| 156 | my $source = $xml->dump_xml ($data); | ||
| 157 | |||
| 158 | #$t->lap ('xml to string'); | ||
| 159 | #my $xml_string = $source->toString (1); | ||
| 160 | #debug $xml_string; | ||
| 161 | #$app->root->append ('xml.xml')->as_file->store ($xml_string); | ||
| 162 | |||
| 163 | # $t->lap ('xml from string'); | ||
| 164 | # my $parser = Data::Dump::XML::Parser->new; | ||
| 165 | # $parser->parse_string ($xml_string); | ||
| 166 | |||
| 167 | # $t->lap ('dom from string'); | ||
| 168 | # $parser = XML::LibXML->new; | ||
| 169 | # $parser->parse_string ($xml_string); | ||
| 170 | |||
| 171 | $t->lap ('locating stylesheet'); | ||
| 172 | |||
| 173 | my $file = $self->locate_stylesheet ($app, \%params); | ||
| 174 | |||
| 175 | debug "using stylesheet $file to generate some content"; | ||
| 176 | |||
| 177 | my $production = $app->project->config->{production}; | ||
| 178 | |||
| 179 | my $stylesheet = $self->parse_stylesheet ($file, $production); | ||
| 180 | |||
| 181 | $t->lap ('processing data transformation'); | ||
| 182 | |||
| 183 | my $result_object; | ||
| 184 | my $result; | ||
| 185 | |||
| 186 | eval { | ||
| 187 | $result_object = $stylesheet->transform ($source, XML::LibXSLT::xpath_to_string (@_)); | ||
| 188 | $result = $stylesheet->output_as_chars ($result_object); | ||
| 189 | }; | ||
| 190 | |||
| 191 | debug "result length = ", length $result; | ||
| 192 | |||
| 193 | if ($@ or not $result_object or not $result ) { | ||
| 194 | debug $source->toString (1); | ||
| 195 | critical "Can't transform data:\n<strong>$@</strong>"; | ||
| 196 | } | ||
| 197 | |||
| 198 | # $result = Encode::decode_utf8 ($result); | ||
| 199 | |||
| 200 | unless (defined $result or $result ne '' or $result !~ m!body></body!) { | ||
| 201 | debug "presenter's transformation result is empty"; | ||
| 202 | } | ||
| 203 | |||
| 204 | $t->end; | ||
| 205 | |||
| 206 | return $result; | ||
| 207 | |||
| 208 | } | ||
| 209 | # -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- | ||
| 210 | # -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- | ||
| 211 | # -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- | ||
| 212 | |||
| 213 | sub wrap_log { | ||
| 214 | my $self = shift; | ||
| 215 | my $content = shift; | ||
| 216 | |||
| 217 | return join '', "\n<pre>\n", $content, "\n</pre>\n"; | ||
| 218 | } | ||
| 219 | |||
| 220 | 1; |