blib/lib/UR/Object/View/Default/Xsl.pm untested

filter: Statementstmt 0.0% 0 / 126 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 6 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 176 SCAR 50.5
CC
12
Cov
0%
_generate_xsl_doc
56.0
_generate_content
20.0
_get_xml_view
6.0
CRAP 156.0
1 package UR::Object::View::Default::Xsl;
2  
3 use strict;
4 use warnings;
5 require UR;
6 our $VERSION = "0.46"; # UR $VERSION;
7 use IO::File;
8  
9 use XML::LibXML;
10 use XML::LibXSLT;
11  
12 class UR::Object::View::Default::Xsl {
13 is => 'UR::Object::View::Default::Text',
14 has => [
15 output_format => { value => 'html' },
16 transform => { is => 'Boolean', value => 0 },
17 xsl_variables => { is => 'Hash', is_optional => 1 },
18 rest_variable => { value => '/rest', is_deprecated => 1 },
19 desired_perspective => { },
20 xsl_path => {
21 is_optional => 1,
22 doc => 'web relative path starting with / where the xsl ' .
23 'is located when serving from a web service'
24 },
25 xsl_root => {
26 doc => 'absolute path where xsl files will be found, expected ' .
27 'format is $xsl_path/$output_format/$perspective/' .
28 '$normalized_class_name.xsl'
29 },
30 ]
31 };
32  
33 use Exporter 'import';
34 our @EXPORT_OK = qw(type_to_url url_to_type);
35  
36 sub _generate_content {
37 my ($self, %params) = @_;
38  
39 if (!$self->desired_perspective) {
40 $self->desired_perspective($self->perspective);
41 }
42 # my $subject = $self->subject;
43 # return unless $subject;
44  
45 unless ($self->xsl_root && -e $self->xsl_root) {
46 die 'xsl_root does not exist:' . $self->xsl_root;
47 }
48  
49 my $xml_view = $self->_get_xml_view(%params);
50  
51 # my $xml_content = $xml_view->_generate_content();
52  
53 my $doc = $self->_generate_xsl_doc($xml_view);
54  
55 if ($self->transform) {
56 return $self->transform_xml($xml_view,$doc); #$xsl_template);
57 } else {
58 return $doc->toString(1); # $xsl_template;
59 }
60 }
61  
62 sub _get_xml_view {
63 my $self = shift;
64 my %params = @_;
65  
66 # get the xml for the equivalent perspective
67 my $xml_view;
68 eval {
69 $xml_view = UR::Object::View->create(
70 subject_class_name => $self->subject_class_name,
71 perspective => $self->desired_perspective,
72 toolkit => 'xml',
73 %params
74 );
75 };
76 if ($@) {
77 # try again, for debugging, don't hate me for this $DB::single you're about to crash..
78 $DB::single = 1;
79 $xml_view = UR::Object::View->create(
80 subject_class_name => $self->subject_class_name,
81 perspective => $self->perspective,
82 toolkit => 'xml',
83 %params
84 );
85 }
86  
87 return $xml_view;
88 }
89  
90 sub _generate_xsl_doc {
91 my $self = shift;
92 my $xml_view = shift;
93  
94 # subclasses typically have this as a constant value
95 # it turns out we don't need it, since the file will be HTML.pm.xsl for xml->html conversion
96 # my $toolkit = $self->toolkit;
97  
98 my $output_format = $self->output_format;
99 my $xsl_path = $self->xsl_root;
100  
101 unless ($self->transform) {
102 # when not transforming we'll return a relative path
103 # suitable for urls
104 $xsl_path = $self->xsl_path;
105 }
106  
107 my $perspective = $self->desired_perspective;
108  
109 my @include_files = $self->_resolve_xsl_template_files(
110 $xml_view,
111 $output_format,
112 $xsl_path,
113 $perspective
114 );
115  
116 my $rootxsl = "/$output_format/$perspective/root.xsl";
117 if (!-e $xsl_path . $rootxsl) {
118 $rootxsl = "/$output_format/default/root.xsl";
119 }
120  
121 my $commonxsl = "/$output_format/common.xsl";
122 if (-e $xsl_path . $commonxsl) {
123 push(@include_files, $commonxsl);
124 }
125  
126 no warnings;
127  
128 my $xslns = 'http://www.w3.org/1999/XSL/Transform';
129  
130 my $doc = XML::LibXML::Document->new("1.0", "ISO-8859-1");
131 my $ss = $doc->createElementNS($xslns, 'stylesheet');
132 $ss->setAttribute('version', '1.0');
133 $doc->setDocumentElement($ss);
134 $ss->setNamespace($xslns, 'xsl', 1);
135  
136 my $time = time . "000";
137  
138 ## this is the wrong place for this information
139 # since it is already part of the XML document
140 # it shouldn't be hard coded into the transform
141 my $display_name = $self->subject->__display_name__;
142 my $label_name = $self->subject->__label_name__;
143  
144 my $set_var = sub {
145 my $e = $doc->createElementNS($xslns, 'param');
146 $e->setAttribute('name', $_[0]);
147 $e->appendChild( $doc->createTextNode( $_[1] ) );
148 $ss->appendChild($e)
149 };
150  
151 $set_var->('currentPerspective',$perspective);
152 $set_var->('currentToolkit',$output_format);
153 $set_var->('displayName',$display_name);
154 $set_var->('labelName',$label_name);
155 $set_var->('currentTime',$time);
156 $set_var->('username',$ENV{'REMOTE_USER'});
157  
158 if (my $id = $self->subject->id) {
159 $set_var->('objectId', $id);
160 }
161  
162 if (my $class_name = $self->subject->class) {
163 $set_var->('objectClassName', $class_name);
164 }
165  
166 if (my $vars = $self->xsl_variables) {
167 while (my ($key,$val) = each %$vars) {
168 $set_var->($key, $val);
169 }
170 } else {
171 $set_var->('rest',$self->rest_variable);
172 }
173  
174 my $rootn = $doc->createElementNS($xslns, 'include');
175 $rootn->setAttribute('href',"$xsl_path$rootxsl");
176 $ss->appendChild($rootn);
177  
178 for (@include_files) {
179 my $e = $doc->createElementNS($xslns, 'include');
180 $e->setAttribute('href',"$xsl_path$_");
181 $ss->appendChild($e)
182 }
183  
184 return $doc;
185 }
186  
187 sub _resolve_xsl_template_files {
188 my ($self, $xml_view, $output_format, $xsl_path, $perspective) = @_;
189  
190 return $xml_view->xsl_template_files(
191 $output_format,
192 $xsl_path,
193 $perspective,
194 );
195 }
196  
197 sub transform_xml {
198 my ($self,$xml_view,$style_doc) = @_;
199  
200 $xml_view->subject($self->subject);
201 my $xml_content = $xml_view->_generate_content();
202  
203 # remove invalid XML entities
204 $xml_content =~ s/[^\x09\x0A\x0D\x20-\x{D7FF}\x{E000}-\x{FFFD}\x{10000}-\x{10FFFF}]//go;
205  
206 my $parser = XML::LibXML->new;
207 my $xslt = XML::LibXSLT->new;
208  
209 my $source;
210 if($xml_view->can('_xml_doc') and $xml_view->_xml_doc) {
211 $source = $xml_view->_xml_doc;
212 } else {
213 $source = $parser->parse_string($xml_content);
214 }
215  
216 # convert the xml
217 my $stylesheet = $xslt->parse_stylesheet($style_doc);
218 my $results = $stylesheet->transform($source);
219 my $content = $stylesheet->output_string($results);
220  
221 return $content;
222 }
223  
224 sub type_to_url {
225 join(
226 '/',
227 map {
228 s/(?<!^)([[:upper:]]{1})/-$1/g;
229 lc;
230 } split( '::', $_[0] )
231 );
232 }
233  
234 sub url_to_type {
235 join(
236 '::',
237 map {
238 $_ = ucfirst;
239 s/-(\w{1})/\u$1/g;
240 $_;
241 } split( '/', $_[0] )
242 );
243 }
244  
245 ## register a helper function for xslt
246 XML::LibXSLT->register_function( 'urn:rest', 'typetourl', \&type_to_url );
247 XML::LibXSLT->register_function( 'urn:rest', 'urltotype', \&url_to_type );
248  
249  
250 1;
251  
252 =pod
253  
254 =head1 NAME
255  
256 UR::Object::View::Default::Xsl - base class for views which use XSL on an XML view to generate content
257  
258 =head1 SYNOPSIS
259  
260 #####
261  
262 class Acme::Product::View::OrderStatus::Html {
263 is => 'UR::Object::View::Default::Xsl',
264 }
265  
266 #####
267  
268 Acme/Product/View/OrderStatus/Html.pm.xsl
269  
270 #####
271  
272 $o = Acme::Product->get(1234);
273  
274 $v = $o->create_view(
275 perspective => 'order status',
276 toolkit => 'html',
277 aspects => [
278 'id',
279 'name',
280 'qty_on_hand',
281 'outstanding_orders' => [
282 'id',
283 'status',
284 'customer' => [
285 'id',
286 'name',
287 ]
288 ],
289 ],
290 );
291  
292 $xml1 = $v->content;
293  
294 $o->qty_on_hand(200);
295  
296 $xml2 = $v->content;
297  
298 =head1 DESCRIPTION
299  
300 This class implements basic HTML views of objects. It has standard behavior for all text views.
301  
302 =head1 SEE ALSO
303  
304 UR::Object::View::Default::Text, UR::Object::View, UR::Object::View::Toolkit::XML, UR::Object::View::Toolkit::Text, UR::Object
305  
306 =cut
307