XSLTParser.pm untested

filter: Statementstmt 0.0% 0 / 309 Branchbran 0.0% 0 / 170 Conditioncond 0.0% 0 / 28 MC/DCmcdc 0.0% 0 / 18 Subroutinesub 0.0% 0 / 18 Podpod 0.0% 0 / 12 totaltotal 0.0% 0 / 555 CC 86 SCAR 70.8
CC
86
Cov
0%
_examine_child
2352.0
parse_xml_selection
420.0
parse_xsl_select
56.0
CRAP 1185.5
1 ################################################################################
2 #
3 # Perl module: XSLTParser
4 #
5 # By Geert Josten, gjosten@sci.kun.nl
6 # and Egon Willighagen, egonw@sci.kun.nl
7 #
8 ################################################################################
9  
10 ######################################################################
11 package XSLTParser;
12 ######################################################################
13 use strict;
14 use XML::DOM;
15  
16 BEGIN {
17 require XML::DOM;
18  
19 my $needVersion = '1.25';
20 die "need at least XML::DOM version $needVersion (current=" . $XML::DOM::VERSION . ")"
21 unless $XML::DOM::VERSION >= $needVersion;
22  
23 use Exporter ();
24 use vars qw( $VERSION @ISA @EXPORT);
25  
26 do "version.h";
27  
28 @ISA = qw( Exporter );
29 @EXPORT = qw( &new &openproject &evaluate_xsl &print_xsl );
30  
31 use vars qw ( $_indent );
32 $_indent = "";
33 $_indent = 0;
34 }
35  
36 sub new {
37 my ($class) = @_;
38  
39 return bless {}, $class;
40 }
41  
42 sub openproject {
43 my ($self, $xmlfile, $xslfile) = @_;
44  
45 $XSLT::DOMparser = new XML::DOM::Parser;
46 $XSLT::xsl = $XSLT::DOMparser->parsefile ("$xslfile");
47 $XSLT::xml = $XSLT::DOMparser->parsefile ("$xmlfile");
48 $XSLT::result = new XML::DOM::Document;
49 }
50  
51 sub evaluate_xsl {
52 my ($self) = @_;
53  
54 $self->_evaluate_contents (
55 $XSLT::xsl, # current XSL node, the root
56 $XSLT::xml, # current XML node, the root
57 '' # current XML selection path, the root
58 );
59 }
60  
61 sub print_xsl {
62 my ($self, $fh) = @_;
63  
64 $XSLT::outputstring = $XSLT::xsl->toString;
65  
66 $XSLT::outputstring =~ s/\<xsl.*?\>//g; # Strip xsl opening tags
67 $XSLT::outputstring =~ s/\<\/xsl.*?\>//g; # Strip xsl closing tags
68 $XSLT::outputstring =~ s/\n\s*\n(\s*)\n/\n$1\n/g; # Substitute multiple empty lines by one
69 $XSLT::outputstring =~ s/\/\>/ \/\>/g; # Insert a space before all />
70  
71 if ($fh) {
72 print $fh $XSLT::outputstring;
73 } else {
74 print $XSLT::outputstring;
75 }
76 }
77  
78 sub _evaluate_contents {
79 my $self = shift;
80 my $current_xsl_node = shift;
81 my $current_xml_node = shift;
82 my $current_xml_selection_path = shift;
83  
84 print " "x$_indent, "path: \"$current_xml_selection_path\"$/" if $XSLT::debug;
85 $_indent += 2;
86 if ($current_xsl_node->hasChildNodes) {
87 my $xsl_children = $current_xsl_node->getElementsByTagName('*','');
88  
89  
90 for (my $i=0;$i< $xsl_children->getLength();$i++) {
91 my $xsl_child = $xsl_children->item($i);
92 $self->_examine_child ($xsl_child, $current_xml_node, $current_xml_selection_path);
93 $xsl_children = $current_xsl_node->getElementsByTagName('*','');
94 }
95  
96 # foreach my $xsl_child (@$xsl_children) {
97 # $self->_examine_child ($xsl_child, $current_xml_node, $current_xml_selection_path);
98 # }
99 }
100 $_indent -= 2;
101 print " "x$_indent, "path: \"$current_xml_selection_path\"$/" if $XSLT::debug;
102 }
103  
104 sub _examine_child {
105 my $self = shift;
106 my $current_xsl_child = shift;
107 my $current_xml_node = shift;
108 my $current_xml_selection_path = shift;
109
110 $current_xsl_child->normalize();
111
112 print " "x$_indent, "<", $current_xsl_child->getTagName, ">", $/ if $XSLT::debug;
113  
114 if ($current_xsl_child->getTagName =~ /^xsl:/i) {
115  
116 if ($current_xsl_child->getTagName =~ /^xsl:stylesheet/i) {
117 $self->_evaluate_contents ($current_xsl_child, $current_xml_node,
118 "$current_xml_selection_path");
119 } elsif ($current_xsl_child->getTagName =~ /^xsl:template/i
120 || $current_xsl_child->getTagName =~ /^xsl:sub-template/i) {
121
122 # extend the base reference of the xml tree and recurse
123 my $xml_path_extension = $current_xsl_child->getAttribute('match');
124  
125 # expand multiple options
126 if (detect_or($xml_path_extension)) {
127
128 print "or detected$/" if $XSLT::debug;
129 my ($sel1, $sel2) = parse_or($xml_path_extension);
130  
131 print "selections: $sel1 $sel2$/";
132 $current_xsl_child->setAttribute("match", $sel1);
133 my $next_node = $current_xsl_child->getNextSibling();
134
135 while (detect_or($sel2)) {
136
137 ($sel1,$sel2) = parse_or($sel2);
138 print "selections: $sel1 $sel2$/";
139 my $new_node = $current_xsl_child->cloneNode('deep');
140 $new_node->setAttribute("match", $sel1);
141  
142 if (defined($next_node)) {
143 $current_xsl_child->getParentNode()->insertBefore($new_node, $next_node);
144 } else {
145 $current_xsl_child->getParentNode()->appendChild($new_node);
146 }
147 }
148
149 my $new_node2 = $current_xsl_child->cloneNode('deep');
150 $new_node2->setAttribute("match", $sel2);
151 if (defined($next_node)) {
152 $current_xsl_child->getParentNode()->insertBefore($new_node2, $next_node);
153 } else {
154 $current_xsl_child->getParentNode()->appendChild($new_node2);
155 }
156  
157 }
158 $xml_path_extension = $current_xsl_child->getAttribute('match');
159 my $curr_xml_node = $self->parse_xml_selection ($current_xml_node,
160 $current_xml_selection_path, $xml_path_extension);
161 if ($curr_xml_node) {
162 if (!$xml_path_extension || $xml_path_extension eq "/") {
163 $self->_evaluate_contents ($current_xsl_child, $curr_xml_node,
164 "$current_xml_selection_path");
165 } else {
166 $self->_evaluate_contents ($current_xsl_child, $curr_xml_node,
167 "$current_xml_selection_path/$xml_path_extension");
168 }
169 }
170  
171 } elsif ($current_xsl_child->getTagName =~ /^xsl:include/i) {
172  
173 # get include file name and look if exists
174 my $include_file = $current_xsl_child->getAttribute('select');
175 if (! (-f $include_file)) {
176 die "Error: include $include_file can not be read$/";
177 }
178  
179 # parse file and insert tree into xsl tree
180 my $include_xsl = $XSLT::DOMparser->parsefile ($include_file);
181 my $xsl_append_tree = $include_xsl->getFirstChild();
182
183 if ($xsl_append_tree) {
184 $xsl_append_tree->setOwnerDocument ($XSLT::xsl);
185 $current_xsl_child->appendChild ($xsl_append_tree);
186 }
187  
188 # recurse into inserted tree
189 $self->_evaluate_contents ($current_xsl_child, $current_xml_node,
190 $current_xml_selection_path);
191  
192 } elsif ($current_xsl_child->getTagName =~ /^xsl:value-of/i) {
193  
194 # get element from xml tree
195 my $xml_selection = $current_xsl_child->getAttribute('select');
196 my $xml_item = $self->parse_xml_selection ($current_xml_node, $current_xml_selection_path,
197 $xml_selection);
198  
199 print "haalt ie dit? xml-sel: $xml_selection\n curr_nod: $xml_item" if $XSLT::debug;
200 if ($xml_item) {
201 if ($xml_item->isTextNode) {
202 $current_xsl_child->appendChild($xml_item);
203 } elsif ($xml_item->hasChildNodes) {
204 $current_xsl_child->appendChild ($xml_item->getFirstChild);
205 }
206 }
207  
208 } elsif ($current_xsl_child->getTagName =~ /^xsl:for-each/i) {
209  
210 # copy xsl sub tree
211 my $xsl_sub_tree_orig = $current_xsl_child->cloneNode ('deep');
212 my $xsl_sub_tree;
213  
214 # delete from xsl
215 foreach my $child ($current_xsl_child->getChildNodes) {
216 $current_xsl_child->removeChild ($child);
217 }
218  
219 # select appropriate xml part
220 my $xml_selection = $current_xsl_child->getAttribute('select');
221 my $xml_parent = $self->parse_xml_selection ($current_xml_node, $current_xml_selection_path, "");
222  
223 if ($xml_parent && $xml_parent->hasChildNodes) {
224 # search xml for matches
225 my $child_nodes = $xml_parent->getElementsByTagName($xml_selection,'');
226  
227 # look at each child
228 my $count = 0;
229 foreach my $node (@$child_nodes) {
230  
231 # copy sub tree to evaluate it for this match
232 $xsl_sub_tree = $xsl_sub_tree_orig->cloneNode ('deep');
233  
234 # recurse for this match
235 my $curr_xml_node = $self->parse_xml_selection ($current_xml_node,
236 $current_xml_selection_path, join ("", $xml_selection,'[',$count,']'));
237 $self->_evaluate_contents ($xsl_sub_tree, $curr_xml_node,
238 join ("", $current_xml_selection_path,'/',$xml_selection,'[',$count,']'));
239  
240 # and append to xsl tree
241 $current_xsl_child->appendChild ($xsl_sub_tree);
242 $count++;
243 }
244 }
245  
246 } elsif ($current_xsl_child->getTagName =~ /^xsl:choose/i) {
247  
248 my $not_done = "True";
249 my $when_nodes = $current_xsl_child->getElementsByTagName ('xsl:when', '');
250  
251 # test all when cases
252 for (my $i = 0; $i < $when_nodes->getLength(); $i++) {
253 my $node = $when_nodes->item($i);
254 if ($not_done) {
255 my $xml_selection = $node->getAttribute('test');
256 my ($sec_item, $sec_attr, $sec_value) = parse_xsl_select($current_xml_node, $xml_selection);
257 my $xml_item = $self->parse_xml_selection ($current_xml_node, $current_xml_selection_path,
258 $xml_selection);
259  
260 # insert if when tests okay
261 if (($xml_item) && ($sec_attr)) {
262 # attr must be tested
263 print " "x$_indent, " trying to get attribute\n" if $XSLT::debug;
264 my $attr_value = $xml_item->getAttribute($sec_attr);
265 if ($attr_value ne $sec_value) {
266 $current_xsl_child->removeChild($node);
267 } else {
268 $not_done = ""; # False
269 }
270 # else remove
271 } else {
272 $current_xsl_child->removeChild($node);
273 }
274 # else remove
275 } else {
276 $current_xsl_child->removeChild($node);
277 }
278 }
279  
280 # or get the default case if available
281 my $otherwise_nodes = $current_xsl_child->getElementsByTagName ("xsl:otherwise", "notdeep");
282 my $n = $otherwise_nodes->getLength();
283 for (my $i = 0; $i < $n; $i++) {
284 my $node = $otherwise_nodes->item($i);
285 if ($not_done) {
286 my $xml_selection = $node->getAttribute('test');
287 my ($sec_item, $sec_attr, $sec_value) = parse_xsl_select($current_xml_node, $xml_selection);
288 my $xml_item = $self->parse_xml_selection ($current_xml_node, $current_xml_selection_path,
289 $xml_selection);
290  
291 # insert if when tests okay
292 if (($xml_item) && ($sec_attr)) {
293 # attr must be tested
294 print " "x$_indent, " trying to get attribute\n" if $XSLT::debug;
295 my $attr_value = $xml_item->getAttribute($sec_attr);
296 if ($attr_value ne $sec_value) {
297 $current_xsl_child->removeChild($node);
298 } else {
299 $not_done = ""; # False
300 }
301 # else remove
302 } else {
303 $current_xsl_child->removeChild($node);
304 }
305 # else remove
306 } else {
307 $current_xsl_child->removeChild($node);
308 }
309 }
310  
311 # recurse into sub tree
312 $self->_evaluate_contents ($current_xsl_child, $current_xml_node, $current_xml_selection_path);
313  
314 } elsif ($current_xsl_child->getTagName =~ /^xsl:when/i) {
315  
316 my $xml_selection = $current_xsl_child->getAttribute('test');
317 my ($sec_item, $sec_attr, $sec_value) = parse_xsl_select($current_xml_node, $xml_selection);
318 my $xml_item = $self->parse_xml_selection ($current_xml_node, $current_xml_selection_path, $sec_item);
319  
320 # remove if not tests okay
321 if (!$xml_item) {
322 #$node->getParentNode()->removeChild($node);
323 while ($current_xsl_child->hasChildNodes) {
324 $current_xsl_child->removeChild ($current_xsl_child->getLastChild);
325 }
326 } else {
327 #node found
328 if ($sec_attr) {
329 # attr must be tested
330 print " "x$_indent, " trying to get attribute\n" if $XSLT::debug;
331 my $attr_value = $xml_item->getAttribute($sec_attr);
332 if ($attr_value ne $sec_value) {
333 while ($current_xsl_child->hasChildNodes) {
334 $current_xsl_child->removeChild ($current_xsl_child->getLastChild);
335 }
336 }
337 }
338 }
339  
340 # recurse into sub tree
341 $self->_evaluate_contents ($current_xsl_child, $current_xml_node, $current_xml_selection_path);
342  
343 } elsif ($current_xsl_child->getTagName =~ /^xsl:processing-instruction/i) {
344
345 my $new_PI = $XSLT::xsl->createProcessingInstruction("xsl", $current_xsl_child->getFirstChild->getNodeValue);
346
347 if ($new_PI) {
348 $current_xsl_child->getParentNode->replaceChild($new_PI, $current_xsl_child);
349 }
350  
351 } elsif ($current_xsl_child->getTagName =~ /^xsl:output/i) {
352
353 # not supported yet, implementation comming up?
354  
355 } else {
356  
357 # just recurse into sub tree
358 $self->_evaluate_contents ($current_xsl_child, $current_xml_node, $current_xml_selection_path);
359  
360 }
361  
362 } else {
363  
364 my $new_value = "";
365 print " "x$_indent, "Child: $current_xsl_child\n" if $XSLT::debug;
366 my $attr_tmp = $current_xsl_child->getAttributes();
367 print " "x$_indent, "Attibs: $attr_tmp\n" if $XSLT::debug;
368 for (my $i = 0; $i < $attr_tmp->getLength; $i++) {
369 my $attr_name = $attr_tmp->item($i)->getName;
370 my $attr_value = $attr_tmp->item($i)->getValue;
371 $_ = $attr_value;
372 if ($attr_value ne "") {
373 #test is attribute should be tested
374 if ($attr_value =~ /\{.*\}/) {
375 print " "x$_indent, "Attrib: $attr_name - $attr_value\n" if $XSLT::debug;
376 my ($sec_item, $sec_attr) = $self->parse_select($attr_value);
377 my $xml_item = $self->parse_xml_selection ($current_xml_node, $current_xml_selection_path, $sec_item);
378 print " "x$_indent, "Selected item: $xml_item\n" if $XSLT::debug;
379 if ($sec_attr) {
380 $new_value = $xml_item->getAttribute($sec_attr);
381 } else {
382 #sort of value-of
383 if ($xml_item->isTextNode) {
384 $new_value = $xml_item->getNodeValue;
385 } elsif ($xml_item->hasChildNodes) {
386 $new_value = $xml_item->getFirstChild->getNodeValue;
387 }
388 }
389 } else {
390 $new_value = $attr_value;
391 }
392 }
393 print " "x$_indent, "Nieuw: ".$new_value."\n" if $XSLT::debug;
394 $current_xsl_child->setAttribute($attr_name, $new_value);
395 }
396  
397 # No xsl command, so just recurse into sub tree
398 $self->_evaluate_contents ($current_xsl_child, $current_xml_node, $current_xml_selection_path)
399  
400 }
401  
402 }
403  
404 sub parse_xml_selection {
405 my $self = shift;
406 my $current_node = shift;
407 my $selection_path = shift;
408 my $selection = (shift || "");
409 my $content;
410 my $child;
411  
412 $selection =~ s/\|/\\\|/g;
413 print " "x$_indent, " selection: \"$selection\"$/" if $XSLT::debug;
414  
415 # voorkom de Adam-bug
416 if (($selection =~ /\/\//) || ($selection =~ /\.\./)) {
417 print " "x$_indent, " ** eerste mogelijkheid\n" if $XSLT::debug;
418 if ($selection =~ /\{.*\}/i) {
419 $content = "$selection_path$selection";
420 } else {
421 $content = "$selection_path/$selection";
422 }
423 $content =~ s#\/.*\/\/#\/#; # // -> begin from root
424 $content =~ s#\/*$##; # chop ending /
425 $content =~ s#\/.*?\/\.\.##g; # /abc/.. -> "" get parent of abc
426  
427 $child = $XSLT::xml;
428 } else {
429 print " "x$_indent, " ** tweede mogelijkheid\n" if $XSLT::debug;
430 if ($selection =~ /\{.*\}/i) {
431 $content = $selection;
432 } elsif ($selection =~ /\./i) {
433 $content = "";
434 } else {
435 $content = "/$selection";
436 }
437 $content =~ s#\/*$##; # chop ending /
438  
439 $child = $current_node;
440 }
441  
442 print " "x$_indent, " work select: \"$content\"$/" if $XSLT::debug;
443  
444 while ($content && $child) {
445 if ($content =~ /^\/(\w+)\[(.*?)\]/) {
446 $content =~ s/^\/(\w+)\[(.*?)\]//;
447  
448 my $child_nodes = $child->getElementsByTagName($1,'');
449 $child = "";
450 if ($child_nodes->getLength > 0) {
451 $child = $child_nodes->item($2)->cloneNode('deep');
452 }
453 } elsif ($content =~ /^\/(\w+)/) {
454 $content =~ s/^\/(\w+)//;
455  
456 my $child_nodes = $child->getElementsByTagName($1,'');
457 $child = "";
458 if ($child_nodes->getLength > 0) {
459 $child = $child_nodes->item(0)->cloneNode('deep');
460 }
461 } elsif ($content =~ /^\@(.+)/) {
462 $content =~ s/^\@(.+)//;
463 $child = $child->getAttributeNode($1);
464 } elsif ($content =~ /^\{(.+)\}/) {
465 $content =~ s/^\{(.+)\}//;
466 $child = $child->getAttributeNode($1);
467 }
468 }
469  
470 if ($child) {
471 if ($child->getNodeType == ATTRIBUTE_NODE) {
472 print " "x$_indent, " attribute: \"", $child->getValue, "\"$/" if $XSLT::debug;
473 return ($XSLT::xsl->createTextNode ($child->getValue));
474 } else {
475 $child = $child->cloneNode('deep');
476 $child->setOwnerDocument ($XSLT::xsl);
477 $child->normalize();
478 }
479 if (($XSLT::debug) && ($child->getNodeType != DOCUMENT_NODE)) {
480 print " "x$_indent, " tag: \"", $child->getTagName, "\"$/";
481 }
482 } else {
483 print " "x$_indent, " no selection", $/ if $XSLT::debug;
484 }
485 return ($child);
486 }
487  
488 sub parse_select {
489 my $self = shift;
490 my $selection = shift;
491
492 print " "x$_indent, "About to parse selection: $selection\n" if $XSLT::debug;
493  
494 my $item = "";
495 my $attr = "";
496
497 $_ = $selection;
498 if (m/^\{(.*)\}/) {
499 $selection = $1;
500 $_ = $selection;
501 if (m/(.*)@(.*)/) {
502 $item = $1;
503 $attr = $2;
504 } else {
505 $item = $selection;
506 }
507 }
508
509 print " "x$_indent, " item: $item\n" if $XSLT::debug;
510 print " "x$_indent, " attr: $attr\n" if $XSLT::debug;
511  
512 return ($item, $attr);
513 }
514  
515 sub parse_xsl_select {
516 my $self = shift;
517 my $selection = (shift || "");
518
519 print " "x$_indent, "About to parse xsl:selection: $selection\n" if $XSLT::debug;
520  
521 my $item = "";
522 my $attr = "";
523 my $value = "";
524
525 if ($selection =~ /^(.*)\[\@(.*)=(\"|\')(.*)(\"|\')\]/) {
526 print " "x$_indent, " ** Attribute test found\n" if $XSLT::debug;
527 $item = $1;
528 $attr = $2;
529 $value = $4;
530 } else {
531 $item = $selection;
532 }
533
534 print " "x$_indent, " item: $item\n" if $XSLT::debug;
535 print " "x$_indent, " attr: $attr\n" if $XSLT::debug;
536 print " "x$_indent, " valu: $value\n" if $XSLT::debug;
537  
538 return ($item, $attr, $value);
539 }
540  
541 sub detect_or {
542 my $select = shift;
543
544 my $result_value = 0;
545 if ($select =~ /\|/) {
546 $result_value = 1;
547 }
548  
549 return $result_value;
550 }
551  
552 sub parse_or {
553 my $select = shift;
554  
555 my $select1 = "";
556 my $select2 = "";
557 if ($select =~ /(.*?)\|(.*)/) {
558 $select1 = $1;
559 $select2 = $2;
560 } else {
561 print "error parsing or-select occured$/" if $XSLT::debug;
562 }
563
564 return ($select1, $select2);
565 }
566  
567 1;