blib/lib/XML/DOM/Lite/XSLT.pm untested

filter: Statementstmt 0.0% 0 / 366 Branchbran 0.0% 0 / 166 Conditioncond 0.0% 0 / 80 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 26 Podpod 0.0% 0 / 21 totaltotal 0.0% 0 / 659 SCAR 88.7
CC
84
Cov
0%
xsltProcessContext
2070.0
xmlResolveEntities
72.0
xsltPassText
42.0
CRAP 7140.0
1 package XML::DOM::Lite::XSLT;
2  
3 use XML::DOM::Lite::XPath;
4 use XML::DOM::Lite::Constants qw(:all);
5 use Carp qw(confess);
6  
7 use warnings;
8 use strict;
9  
10 our $DEBUG = 0;
11  
12 sub new { bless { }, $_[0] }
13  
14 sub process {
15 my ($self, $xmlDoc, $stylesheet) = @_;
16 return xsltProcess($xmlDoc, $stylesheet);
17 }
18  
19 sub xsltProcess {
20 my ($xmlDoc, $stylesheet) = @_;
21  
22 $DEBUG && warn('XML STYLESHEET:');
23 $DEBUG && warn(xmlText($stylesheet));
24 $DEBUG && warn('XML INPUT:');
25 $DEBUG && warn(xmlText($xmlDoc));
26  
27 my $output = $xmlDoc->createDocumentFragment();
28 xsltProcessContext(XML::DOM::Lite::XPath::ExprContext->new($xmlDoc), $stylesheet, $output);
29  
30 my $ret = xmlText($output);
31  
32 $DEBUG && warn('HTML OUTPUT:');
33 $DEBUG && warn($ret);
34  
35 return $ret;
36 }
37  
38 sub xsltProcessContext {
39 my ($input, $template, $output) = @_;
40 my @nodename = split(/:/, $template->nodeName);
41 if (@nodename == 1 or $nodename[0] ne 'xsl') {
42 xsltPassThrough($input, $template, $output);
43  
44 } else {
45 if ($nodename[1] eq 'apply-imports') {
46 warn('not implemented: ' . $nodename[1]);
47 } elsif ($nodename[1] eq 'apply-templates') {
48 my $select = xmlGetAttribute($template, 'select');
49 my $nodes;
50 if ($select) {
51 $nodes = xpathEval($select, $input)->nodeSetValue();
52 } else {
53 $nodes = $input->{node}->childNodes;
54 }
55  
56 my $sortContext = $input->clone($nodes->[0], 0, $nodes);
57 xsltWithParam($sortContext, $template);
58 xsltSort($sortContext, $template);
59  
60 my $mode = xmlGetAttribute($template, 'mode');
61 my $top = $template->ownerDocument->documentElement;
62 for (my $i = 0; $i < $top->childNodes->length; ++$i) {
63 my $c = $top->childNodes->[$i];
64 if ($c->nodeType == ELEMENT_NODE and
65 $c->nodeName eq 'xsl:template' and
66 ($c->getAttribute('mode') || '') eq ($mode || '')) {
67 for (my $j = 0; $j < @{$sortContext->{nodelist}}; ++$j) {
68 my $nj = $sortContext->{nodelist}->[$j];
69 xsltProcessContext($sortContext->clone($nj, $j), $c, $output);
70 }
71 }
72 }
73  
74 } elsif ($nodename[1] eq 'attribute') {
75 my $nameexpr = xmlGetAttribute($template, 'name');
76 my $name = xsltAttributeValue($nameexpr, $input);
77 my $node = $output->ownerDocument->createDocumentFragment();
78 xsltChildNodes($input, $template, $node);
79 my $value = xmlValue($node);
80 $output->setAttribute($name, $value);
81  
82 } elsif ($nodename[1] eq 'attribute-set') {
83 warn('not implemented: ' . $nodename[1]);
84  
85 } elsif ($nodename[1] eq 'call-template') {
86 my $name = xmlGetAttribute($template, 'name');
87 my $top = $template->ownerDocument->documentElement;
88  
89 my $paramContext = $input->clone();
90 xsltWithParam($paramContext, $template);
91  
92 for (my $i = 0; $i < $top->childNodes->length; ++$i) {
93 my $c = $top->childNodes->[$i];
94 if ($c->nodeType == ELEMENT_NODE and
95 $c->nodeName eq 'xsl:template' and
96 $c->getAttribute('name') eq $name) {
97 xsltChildNodes($paramContext, $c, $output);
98 last;
99 }
100 }
101 } elsif ($nodename[1] eq 'choose') {
102 xsltChoose($input, $template, $output);
103  
104 } elsif ($nodename[1] eq 'comment') {
105 my $node = $output->ownerDocument->createDocumentFragment();
106 xsltChildNodes($input, $template, $node);
107 my $commentData = xmlValue($node);
108 my $commentNode = $output->ownerDocument->createComment($commentData);
109 $output->appendChild($commentNode);
110  
111 } elsif ($nodename[1] eq 'copy') {
112 if ($input->{node}->nodeType == ELEMENT_NODE) {
113 my $node = $output->ownerDocument->createElement($input->{node}->nodeName);
114 $output->appendChild($node);
115 xsltChildNodes($input, $template, $node);
116  
117 } elsif ($input->{node}->nodeType == ATTRIBUTE_NODE) {
118 my $node = $output->ownerDocument->createAttribute($input->{node}->nodeName);
119 $node->nodeValue = $input->{node}->nodeValue;
120 $output->setAttribute($node);
121 }
122  
123 } elsif ($nodename[1] eq 'copy-of') {
124 my $select = xmlGetAttribute($template, 'select');
125 my $value = xpathEval($select, $input);
126 if ($value->{type} eq 'node-set') {
127 my $nodes = $value->nodeSetValue();
128 for (my $i = 0; $i < @$nodes; ++$i) {
129 xsltCopyOf($output, $nodes->[$i]);
130 }
131  
132 } else {
133 my $node = $output->ownerDocument->createTextNode($value->stringValue());
134 $output->appendChild($node);
135 }
136  
137 } elsif ($nodename[1] eq 'decimal-format') {
138 warn('not implemented: ' . $nodename[1]);
139  
140 } elsif ($nodename[1] eq 'element') {
141 my $nameexpr = xmlGetAttribute($template, 'name');
142 my $name = xsltAttributeValue($nameexpr, $input);
143 my $node = $output->ownerDocument->createElement($name);
144 $output->appendChild($node);
145 xsltChildNodes($input, $template, $node);
146  
147 } elsif ($nodename[1] eq 'fallback') {
148 warn('not implemented: ' . $nodename[1]);
149  
150 } elsif ($nodename[1] eq 'for-each') {
151 my $sortContext = $input->clone();
152 xsltSort($sortContext, $template);
153 xsltForEach($sortContext, $template, $output);
154  
155 } elsif ($nodename[1] eq 'if') {
156 my $test = xmlGetAttribute($template, 'test');
157 if (xpathEval($test, $input)->booleanValue()) {
158 xsltChildNodes($input, $template, $output);
159 }
160  
161 } elsif ($nodename[1] eq 'import') {
162 warn('not implemented: ' . $nodename[1]);
163  
164 } elsif ($nodename[1] eq 'include') {
165 warn('not implemented: ' . $nodename[1]);
166  
167 } elsif ($nodename[1] eq 'key') {
168 warn('not implemented: ' . $nodename[1]);
169  
170 } elsif ($nodename[1] eq 'message') {
171 warn('not implemented: ' . $nodename[1]);
172  
173 } elsif ($nodename[1] eq 'namespace-alias') {
174 warn('not implemented: ' . $nodename[1]);
175  
176 } elsif ($nodename[1] eq 'number') {
177 warn('not implemented: ' . $nodename[1]);
178  
179 } elsif ($nodename[1] eq 'otherwise') {
180 warn('not implemented: ' . $nodename[1]);
181  
182 } elsif ($nodename[1] eq 'output') {
183  
184 } elsif ($nodename[1] eq 'preserve-space') {
185 warn('not implemented: ' . $nodename[1]);
186  
187 } elsif ($nodename[1] eq 'processing-instruction') {
188 warn('not implemented: ' . $nodename[1]);
189  
190 } elsif ($nodename[1] eq 'sort') {
191  
192 } elsif ($nodename[1] eq 'strip-space') {
193 warn('not implemented: ' . $nodename[1]);
194  
195 } elsif ($nodename[1] eq 'stylesheet' or $nodename[1] eq 'transform') {
196 xsltChildNodes($input, $template, $output);
197  
198 } elsif ($nodename[1] eq 'template') {
199 my $match = xmlGetAttribute($template, 'match');
200 if ($match and xpathMatch($match, $input)) {
201 xsltChildNodes($input, $template, $output);
202 }
203  
204 } elsif ($nodename[1] eq 'text') {
205 my $text = xmlValue($template);
206 my $node = $output->ownerDocument->createTextNode($text);
207 $output->appendChild($node);
208  
209 } elsif ($nodename[1] eq 'value-of') {
210 my $select = xmlGetAttribute($template, 'select');
211 my $value = xpathEval($select, $input)->stringValue();
212 unless ($output->ownerDocument) { die 'no ownerDocument for '.Dumper($output) }
213 my $node = $output->ownerDocument->createTextNode($value);
214 $output->appendChild($node);
215  
216 } elsif ($nodename[1] eq 'param') {
217 xsltVariable($input, $template, 0);
218  
219 } elsif ($nodename[1] eq 'variable') {
220 xsltVariable($input, $template, 1);
221  
222 } elsif ($nodename[1] eq 'when') {
223 warn('error if here: ' . $nodename[1]);
224  
225 } elsif ($nodename[1] eq 'with-param') {
226 warn('error if here: ' . $nodename[1]);
227  
228 } else {
229 warn('error if here: ' . $nodename[1]);
230 }
231 }
232 }
233  
234 sub xsltWithParam {
235 my ($input, $template) = @_;
236 for (my $i = 0; $i < $template->childNodes->length; ++$i) {
237 my $c = $template->childNodes->[$i];
238 if ($c->nodeType == ELEMENT_NODE and $c->nodeName eq 'xsl:with-param') {
239 xsltVariable($input, $c, 1);
240 }
241 }
242 }
243  
244 sub xsltSort {
245 my ($input, $template) = @_;
246 my $sort = [];
247 for (my $i = 0; $i < $template->childNodes->length; ++$i) {
248 my $c = $template->childNodes->[$i];
249 if ($c->nodeType == ELEMENT_NODE and $c->nodeName eq 'xsl:sort') {
250 my $select = xmlGetAttribute($c, 'select');
251 my $expr = xpathParse($select);
252 my $type = xmlGetAttribute($c, 'data-type') || 'text';
253 my $order = xmlGetAttribute($c, 'order') || 'ascending';
254 push(@$sort, { expr=> $expr, type=> $type, order=> $order });
255 }
256 }
257  
258 xpathSort($input, $sort);
259 }
260  
261 sub xsltVariable {
262 my ($input, $template, $override) = @_;
263
264 my $name = xmlGetAttribute($template, 'name');
265 my $select = xmlGetAttribute($template, 'select');
266  
267 my $value;
268  
269 if ($template->childNodes->length > 0) {
270 my $root = $input->{node}->ownerDocument->createDocumentFragment();
271 xsltChildNodes($input, $template, $root);
272 $value = new NodeSetValue([$root]);
273  
274 } elsif ($select) {
275 $value = xpathEval($select, $input);
276  
277 } else {
278 $value = new StringValue('');
279 }
280  
281 if ($override || !$input->getVariable($name)) {
282 $input->setVariable($name, $value);
283 }
284 }
285  
286  
287 sub xsltChoose {
288 my ($input, $template, $output) = @_;
289 for (my $i = 0; $i < $template->childNodes->length; ++$i) {
290 my $childNode = $template->childNodes->[$i];
291 if ($childNode->nodeType != ELEMENT_NODE) {
292 next;
293  
294 } elsif ($childNode->nodeName eq 'xsl:when') {
295 my $test = xmlGetAttribute($childNode, 'test');
296 if (xpathEval($test, $input)->booleanValue()) {
297 xsltChildNodes($input, $childNode, $output);
298 last;
299 }
300  
301 } elsif ($childNode->nodeName eq 'xsl:otherwise') {
302 xsltChildNodes($input, $childNode, $output);
303 last;
304 }
305 }
306 }
307  
308  
309 sub xsltForEach {
310 my ($input, $template, $output) = @_;
311 my $select = xmlGetAttribute($template, 'select');
312 my $nodes = xpathEval($select, $input)->nodeSetValue();
313 for (my $i = 0; $i < @$nodes; ++$i) {
314 my $context = $input->clone($nodes->[$i], $i, $nodes);
315 xsltChildNodes($context, $template, $output);
316 }
317 }
318  
319  
320 sub xsltChildNodes {
321 my ($input, $template, $output, $foo) = @_;
322 my $context = $input->clone();
323 foreach my $c (@{$template->childNodes}) {
324 xsltProcessContext($context, $c, $output);
325 }
326 }
327  
328  
329 sub xsltPassThrough {
330 my ($input, $template, $output) = @_;
331 if ($template->nodeType == TEXT_NODE) {
332 if (xsltPassText($template)) {
333 my $node = $output->ownerDocument->createTextNode($template->nodeValue);
334 $output->appendChild($node);
335 }
336  
337 } elsif ($template->nodeType == ELEMENT_NODE) {
338 my $node = $output->ownerDocument->createElement($template->nodeName);
339 for (my $i = 0; $i < $template->attributes->length; ++$i) {
340 my $a = $template->attributes->[$i];
341 if ($a) {
342 my $name = $a->nodeName;
343 my $value = xsltAttributeValue($a->nodeValue, $input);
344 $node->setAttribute($name, $value);
345 }
346 }
347 $output->appendChild($node);
348 xsltChildNodes($input, $template, $node);
349  
350 } else {
351 xsltChildNodes($input, $template, $output);
352 }
353 }
354  
355 sub xsltPassText {
356 my ($template) = @_;
357 unless ($template->nodeValue =~ /^\s*$/) {
358 return 1;
359 }
360  
361 my $element = $template->parentNode;
362 if ($element->nodeName eq 'xsl:text') {
363 return 1;
364 }
365  
366 while ($element and $element->nodeType == ELEMENT_NODE) {
367 my $xmlspace = $element->getAttribute('xml:space');
368 if ($xmlspace) {
369 if ($xmlspace eq 'default') {
370 return 0;
371 } elsif ($xmlspace eq 'preserve') {
372 return 1;
373 }
374 }
375  
376 $element = $element->parentNode;
377 }
378  
379 return 0;
380 }
381  
382 sub xsltAttributeValue {
383 my ($value, $context) = @_;
384 my $parts = [ split(/{/, $value) ];
385 if (@$parts == 1) {
386 return $value;
387 }
388  
389 my $ret = '';
390 for (my $i = 0; $i < @$parts; ++$i) {
391 my $rp = [ split(/}/, $parts->[$i]) ];
392 if (@$rp != 2) {
393 $ret .= $parts->[$i];
394 next;
395 }
396  
397 my $val = xpathEval($rp->[0], $context)->stringValue();
398 $ret .= ($val . $rp->[1]);
399 }
400  
401 return $ret;
402 }
403  
404  
405 sub xmlGetAttribute {
406 my ($node, $name) = @_;
407 my $value = $node->getAttribute($name);
408 if ($value) {
409 return xmlResolveEntities($value);
410 } else {
411 return $value;
412 }
413 }
414  
415  
416 sub xsltCopyOf {
417 my ($dst, $src) = @_;
418 if ($src->nodeType == TEXT_NODE) {
419 my $node = $dst->ownerDocument->createTextNode($src->nodeValue);
420 $dst->appendChild($node);
421  
422 } elsif ($src->nodeType == ATTRIBUTE_NODE) {
423 $dst->setAttribute($src->nodeName, $src->nodeValue);
424  
425 } elsif ($src->nodeType == ELEMENT_NODE) {
426 my $node = $dst->ownerDocument->createElement($src->nodeName);
427 $dst->appendChild($node);
428  
429 for (my $i = 0; $i < $src->attributes->length; ++$i) {
430 xsltCopyOf($node, $src->attributes->[$i]);
431 }
432  
433 for (my $i = 0; $i < $src->childNodes->length; ++$i) {
434 xsltCopyOf($node, $src->childNodes->[$i]);
435 }
436  
437 } elsif ($src->nodeType == DOCUMENT_FRAGMENT_NODE or
438 $src->nodeType == DOCUMENT_NODE) {
439 for (my $i = 0; $i < $src->childNodes->length; ++$i) {
440 xsltCopyOf($dst, $src->childNodes->[$i]);
441 }
442 }
443 }
444  
445 sub xpathParse {
446 my ($match) = @_;
447 return XML::DOM::Lite::XPath->parse($match);
448 }
449  
450 sub xpathMatch {
451 my ($match, $context) = @_;
452 my $expr = xpathParse($match);
453  
454 my $ret;
455 if ($expr->{steps} and (not $expr->{absolute})
456 and (@{$expr->{steps}} == 1)
457 and ($expr->{steps}->[0]->{axis} eq 'child')
458 and (@{$expr->{steps}->[0]->{predicate}} == 0)) {
459 $ret = $expr->{steps}->[0]->{nodetest}->evaluate($context)->booleanValue();
460 } else {
461  
462 $ret = 0;
463 my $node = $context->{node};
464  
465 while ((not $ret) and $node) {
466 my $result = $expr->evaluate($context->clone($node,0,[$node]))->nodeSetValue();
467 for (my $i = 0; $i < @$result; ++$i) {
468 if ($result->[$i] == $context->{node}) {
469 $ret = 1;
470 last;
471 }
472 }
473 $node = $node->parentNode;
474 }
475 }
476  
477 return $ret;
478 }
479  
480 sub xpathSort {
481 return XML::DOM::Lite::XPath::xpathSort(@_);
482 }
483  
484 sub xpathEval {
485 my ($select, $context) = @_;
486 my $expr = xpathParse($select);
487 my $ret = $expr->evaluate($context);
488 return $ret;
489 }
490  
491 sub xmlText {
492 my ($node) = @_;
493 my $ret = '';
494 if ($node->nodeType == TEXT_NODE) {
495 $ret .= $node->nodeValue;
496  
497 } elsif ($node->nodeType == ELEMENT_NODE) {
498 $ret .= '<' . $node->nodeName;
499 for (my $i = 0; $i < $node->attributes->length; ++$i) {
500 my $a = $node->attributes->[$i];
501 if ($a and $a->nodeName and $a->nodeValue) {
502 $ret .= ' ' . $a->nodeName;
503 $ret .= '="' . $a->nodeValue . '"';
504 }
505 }
506  
507 if ($node->childNodes->length == 0) {
508 $ret .= '/>';
509  
510 } else {
511 $ret .= '>';
512 for (my $i = 0; $i < $node->childNodes->length; ++$i) {
513 $ret .= xmlText($node->childNodes->[$i]);
514 }
515 $ret .= '</' . $node->nodeName . '>';
516 }
517  
518 } elsif ($node->nodeType == DOCUMENT_NODE or
519 $node->nodeType == DOCUMENT_FRAGMENT_NODE) {
520 for (my $i = 0; $i < $node->childNodes->length; ++$i) {
521 $ret .= xmlText($node->childNodes->[$i]);
522 }
523 }
524  
525 return $ret;
526 }
527  
528 sub xmlResolveEntities {
529 my ($s) = @_;
530  
531 my $parts = [ split(/&/, $s) ];
532  
533 my $ret = $parts->[0];
534 for (my $i = 1; $i < @$parts; ++$i) {
535 my $rp = [ split(/;/, $parts->[$i]) ];
536 if (@$rp == 1) {
537 $ret .= $parts->[$i];
538 next;
539 }
540
541 my $ch;
542 if ($rp->[0] eq 'lt') {
543 $ch = '<';
544 } elsif ($rp->[0] eq 'gt') {
545 $ch = '>';
546 } elsif ($rp->[0] eq 'amp') {
547 $ch = '&';
548 } elsif ($rp->[0] eq 'quot') {
549 $ch = '"';
550 } elsif ($rp->[0] eq 'apos') {
551 $ch = "'";
552 } elsif ($rp->[0] eq 'nbsp') {
553 $ch = ' '; # "\x160"
554 } else {
555 warn 'unknown entity '.$rp->[0];
556 #my span = window.document.createElement('span');
557 #span.innerHTML = '&' + rp[0] + '; ';
558 #ch = span.childNodes[0].nodeValue.charAt(0);
559 }
560 $ret .= ($ch . $rp->[1]);
561 }
562  
563 return $ret;
564 }
565  
566 1;
567  
568 __END__