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

filter: Statementstmt 0.0% 0 / 138 Branchbran 0.0% 0 / 42 Conditioncond 0.0% 0 / 15 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 213 SCAR 62.3
CC
22
Cov
0%
_generate_content_for_aspect
182.0
_generate_xml_doc
42.0
xsl_template_files
12.0
CRAP 506.0
1 package UR::Object::View::Default::Xml;
2  
3 use strict;
4 use warnings;
5 require UR;
6 our $VERSION = "0.46"; # UR $VERSION;
7 use IO::File;
8 use XML::Dumper;
9 use XML::LibXML;
10  
11 class UR::Object::View::Default::Xml {
12 is => 'UR::Object::View::Default::Text',
13 has_constant => [
14 toolkit => { value => 'xml' },
15 ],
16 has_optional_transient => [
17 _xml_doc => { is => 'XML::LibXML::Document', doc => 'The LibXML document used to create the content for this view', },
18 ],
19 };
20  
21 sub xsl_template_files {
22 my $self = shift; #usually this is a view without a subject attached
23 my $output_format = shift;
24 my $root_path = shift;
25 my $perspective = shift || lc($self->perspective);
26  
27 my @xsl_names = map {
28 $_ =~ s/::/_/g;
29 my $pf = "/$output_format/$perspective/" . lc($_) . '.xsl';
30 my $df = "/$output_format/default/" . lc($_) . '.xsl';
31  
32 -e $root_path . $pf ? $pf : (-e $root_path . $df ? $df : undef)
33 } $self->all_subject_classes_ancestry;
34  
35 my @found_xsl_names = grep {
36 defined
37 } @xsl_names;
38  
39 return @found_xsl_names;
40 }
41  
42 sub _generate_xml_doc {
43 my $self = shift;
44  
45 my $subject = $self->subject();
46 return unless $subject;
47  
48 my $xml_doc = XML::LibXML->createDocument();
49 $self->_xml_doc($xml_doc);
50  
51 # the header line is the class followed by the id
52 my $object = $xml_doc->createElement('object');
53 $xml_doc->setDocumentElement($object);
54  
55 $object->addChild( $xml_doc->createAttribute('type', $self->subject_class_name) );
56  
57 $object->addChild( $xml_doc->createAttribute('id', $subject->id ) );
58  
59 my $display_name = $object->addChild( $xml_doc->createElement('display_name') );
60 $display_name->addChild( $xml_doc->createTextNode($subject->__display_name__) );
61  
62 my $label_name = $object->addChild( $xml_doc->createElement('label_name' ));
63 $label_name->addChild( $xml_doc->createTextNode($subject->__label_name__) );
64  
65 my $types = $object->addChild( $xml_doc->createElement('types') );
66 foreach my $c ($self->subject_class_name,$subject->__meta__->ancestry_class_names) {
67 my $isa = $types->addChild( $xml_doc->createElement('isa') );
68 $isa->addChild( $xml_doc->createAttribute('type', $c) );
69 }
70  
71 unless ($self->_subject_is_used_in_an_encompassing_view()) {
72 # the content for any given aspect is handled separately
73 my @aspects = $self->aspects;
74 if (@aspects) {
75 my @sorted_aspects = map { $_->[1] }
76 sort { $a->[0] <=> $b->[0] }
77 map { [ $_->number, $_ ] }
78 @aspects;
79 for my $aspect (@sorted_aspects) {
80 next if $aspect->name eq 'id';
81  
82 my $aspect_node = $self->_generate_content_for_aspect($aspect);
83  
84 $object->addChild( $aspect_node ) if $aspect_node; #If aspect has no values, it won't be included
85 }
86 }
87 }
88  
89 #From the XML::LibXML documentation:
90 #If $format is 1, libxml2 will add ignorable white spaces, so the nodes content is easier to read. Existing text nodes will not be altered
91 #If $format is 2 (or higher), libxml2 will act as $format == 1 but it add a leading and a trailing line break to each text node.
92  
93 return $xml_doc;
94 }
95  
96 sub _generate_content {
97 my $self = shift;
98  
99 my $xml_doc = $self->_generate_xml_doc;
100 return '' unless $xml_doc;
101  
102 my $doc_string = $xml_doc->toString(1);
103  
104 # remove invalid XML entities
105 $doc_string =~ s/[^\x09\x0A\x0D\x20-\x{D7FF}\x{E000}-\x{FFFD}\x{10000}-\x{10FFFF}]//go;
106  
107 return $doc_string;
108 }
109  
110 sub _add_perl_data_to_node {
111 my $self = shift;
112 my $perlref = shift;
113 my $node = shift;
114  
115 my $xml_doc = $self->_xml_doc;
116 $node ||= $xml_doc->documentElement;
117  
118 my $d = XML::Dumper->new;
119 my $perldata = $d->pl2xml($perlref);
120  
121 my $parser = XML::LibXML->new;
122 my $ref_xml_doc = $parser->parse_string($perldata);
123 my $ref_root = $ref_xml_doc->documentElement;
124 $xml_doc->adoptNode( $ref_root );
125 $node->addChild( $ref_root );
126  
127 return 1;
128 }
129  
130 sub _generate_content_for_aspect {
131 # This does two odd things:
132 # 1. It gets the value(s) for an aspect, then expects to just print them
133 # unless there is a delegate view. In which case, it replaces them
134 # with the delegate's content.
135 # 2. In cases where more than one value is returned, it recycles the same
136 # view and keeps the content.
137 #
138 # These shortcuts make it hard to abstract out logic from toolkit-specifics
139  
140 my $self = shift;
141 my $aspect = shift;
142  
143 my $subject = $self->subject;
144 my $xml_doc = $self->_xml_doc;
145 my $aspect_name = $aspect->name;
146  
147 my $aspect_node = $xml_doc->createElement('aspect');
148 $aspect_node->addChild( $xml_doc->createAttribute('name', $aspect_name) );
149  
150 my @value;
151 eval {
152 @value = $subject->$aspect_name;
153 };
154 if ($@) {
155 my ($file,$line) = ($@ =~ /at (.*?) line (\d+)$/m);
156  
157 my $exception = $aspect_node->addChild( $xml_doc->createElement('exception') );
158 $exception->addChild( $xml_doc->createAttribute('file', $file) );
159 $exception->addChild( $xml_doc->createAttribute('line', $line) );
160 $exception->addChild( $xml_doc->createCDATASection($@) );
161  
162 return $aspect_node;
163 }
164  
165 if (not Scalar::Util::blessed($value[0])) {
166 # shortcut to optimize for simple scalar values without delegate views
167 for my $value ( @value ) {
168 my $value_node = $aspect_node->addChild( $xml_doc->createElement('value') );
169 $value = '' if not defined $value;
170 $value_node->addChild( $xml_doc->createTextNode($value) );
171 }
172 return $aspect_node;
173 }
174  
175 unless ($aspect->delegate_view) {
176 $aspect->generate_delegate_view;
177 }
178  
179 # Delegate to a subordinate view if needed.
180 # This means we replace the value(s) with their
181 # subordinate widget content.
182 my $delegate_view = $aspect->delegate_view;
183 unless ($delegate_view) {
184 Carp::confess("No delegate view???");
185 }
186  
187 foreach my $value ( @value ) {
188 if (Scalar::Util::blessed($value)) {
189 $delegate_view->subject($value);
190 } else {
191 $delegate_view->subject_id($value);
192 }
193 $delegate_view->_update_view_from_subject();
194  
195 # merge the delegate view's XML into this one
196 if ($delegate_view->can('_xml_doc') and $delegate_view->_xml_doc) {
197 # the delegate has XML
198 my $delegate_xml_doc = $delegate_view->_xml_doc;
199 my $delegate_root = $delegate_xml_doc->documentElement;
200 #cloneNode($deep = 1)
201 $aspect_node->addChild( $delegate_root->cloneNode(1) );
202 }
203 elsif (ref($value) and not $value->isa("UR::Value")) {
204 # Note: Let UR::Values display content below
205 # Otherwise, the delegate view has no XML object, and the value is a reference
206 $self->_add_perl_data_to_node($value, $aspect_node);
207 }
208 elsif (ref($value) and $value->isa("UR::Value")) {
209 # For a UR::Value return both a formatted value and a raw value.
210 my $display_value_node = $aspect_node->addChild( $xml_doc->createElement('display_value') );
211 my $content = $delegate_view->content;
212 $content = '' if not defined $content;
213 $display_value_node->addChild( $xml_doc->createTextNode($content) );
214  
215 my $value_node = $aspect_node->addChild( $xml_doc->createElement('value') );
216 $content = $value->id;
217 $value_node->addChild( $xml_doc->createTextNode($content) );
218 }
219 else {
220 # no delegate view has no XML object, and the value is a non-reference
221 # (this is the old logic for non-delegate views when we didn't have delegate views for primitives)
222 my $value_node = $aspect_node->addChild( $xml_doc->createElement('value') );
223 unless(defined $value) {
224 $value = '';
225 }
226 my $content = $delegate_view->content;
227 $content = '' if not defined $content;
228 $value_node->addChild( $xml_doc->createTextNode($content) );
229
230 ## old logic for delegate views with no xml doc (unused now)
231 ## the delegate view may not be XML at all--wrap it in our aspect tag so that it parses
232 ## (assuming that whatever delegate was selected properly escapes anything that needs escaping)
233  
234 # my $delegate_text = $delegate_view->content() ? $delegate_view->content() : '';
235 # my $aspect_text = "<aspect name=\"$aspect_name\">\n$delegate_text\n</aspect>";
236 # my $parser = XML::LibXML->new;
237 # my $delegate_xml_doc = $parser->parse_string($aspect_text);
238 # $aspect_node = $delegate_xml_doc->documentElement;
239 # $xml_doc->adoptNode( $aspect_node );
240 }
241 }
242  
243 return $aspect_node;
244 }
245  
246 # Do not return any aspects by default if we're embedded in another view
247 # The creator of the view will have to specify them manually
248 sub _resolve_default_aspects {
249 my $self = shift;
250 unless ($self->parent_view) {
251 return $self->SUPER::_resolve_default_aspects;
252 }
253 return;
254 }
255  
256 1;
257  
258  
259 =pod
260  
261 =head1 NAME
262  
263 UR::Object::View::Default::Xml - represent object state in XML format
264  
265 =head1 SYNOPSIS
266  
267 $o = Acme::Product->get(1234);
268  
269 $v = $o->create_view(
270 toolkit => 'xml',
271 aspects => [
272 'id',
273 'name',
274 'qty_on_hand',
275 'outstanding_orders' => [
276 'id',
277 'status',
278 'customer' => [
279 'id',
280 'name',
281 ]
282 ],
283 ],
284 );
285  
286 $xml1 = $v->content;
287  
288 $o->qty_on_hand(200);
289  
290 $xml2 = $v->content;
291  
292 =head1 DESCRIPTION
293  
294 This class implements basic XML views of objects. It has standard behavior for all text views.
295  
296 =head1 SEE ALSO
297  
298 UR::Object::View::Default::Text, UR::Object::View, UR::Object::View::Toolkit::XML, UR::Object::View::Toolkit::Text, UR::Object
299  
300 =cut
301