| 1 | package XML::LibXML::Overlay::Document; | ||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use base qw(XML::LibXML::Document); | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use Carp qw( croak ); | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||
| 10 | 1 | ▶ | sub apply_to { | ||||||||||||||||||||||||||||
|
Subroutine
apply_to: called CC=4 SCAR=14.0
Pod
documented
| |||||||||||||||||||||||||||||||
| 11 | 1 | my $self = shift; | |||||||||||||||||||||||||||||
| 12 | 1 | my ($target_doc) = @_; | |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||
| 14 | 1 | my @overlay_targets = $self->findnodes('/overlay/target'); | |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||
| 16 | 1 | foreach my $overlay_target (@overlay_targets) { | |||||||||||||||||||||||||||||
| 17 | 6 | my $xpath = $overlay_target->getAttribute('xpath'); | |||||||||||||||||||||||||||||
| 18 | 6 | ▶ | if (not $xpath) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 19 | 0 | croak "missing xpath attribute for target node"; | |||||||||||||||||||||||||||||
| 20 | } | ||||||||||||||||||||||||||||||
| 21 | 6 | my @actions = $overlay_target->findnodes('action'); | |||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||
| 23 | # find all nodes target nodes in the target document | ||||||||||||||||||||||||||||||
| 24 | 6 | my @target_nodes = $target_doc->findnodes($xpath); | |||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||
| 26 | 6 | foreach my $target_node (@target_nodes) { | |||||||||||||||||||||||||||||
| 27 | 11 | $self->_apply_actions($target_node, \@actions); | |||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||
| 32 | 1 | return $target_doc; | |||||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||
| 35 | 11 | ▶ | sub _apply_actions { | ||||||||||||||||||||||||||||
|
Subroutine
_apply_actions: called CC=12 SCAR=24.9
| |||||||||||||||||||||||||||||||
| 36 | 11 | my $self = shift; | |||||||||||||||||||||||||||||
| 37 | 11 | my ($target_node, $actions) = @_; | |||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||
| 39 | # applay each $action to $target_node | ||||||||||||||||||||||||||||||
| 40 | 11 | foreach my $action (@$actions) { | |||||||||||||||||||||||||||||
| 41 | 11 | my $type = $action->getAttribute('type'); | |||||||||||||||||||||||||||||
| 42 | 11 | my $attribute = $action->getAttribute('attribute'); | |||||||||||||||||||||||||||||
| 43 | 11 | my @action_children = $action->childNodes(); | |||||||||||||||||||||||||||||
| 44 | 11 | ▶ | if (not $type) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 45 | 0 | croak "missing type attribute for action node"; | |||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||
| 48 | 11 | ▶ | if ( $type eq 'appendChild' ) { | ||||||||||||||||||||||||||||
|
Branch: 6 branches
| |||||||||||||||||||||||||||||||
| 49 | 1 | foreach my $action_child (@action_children) { | |||||||||||||||||||||||||||||
| 50 | 3 | $target_node->appendChild($action_child->cloneNode(1)); | |||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||||
| 53 | elsif ( $type eq 'delete' ) { | ||||||||||||||||||||||||||||||
| 54 | 1 | $target_node->unbindNode(); | |||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||||
| 56 | elsif ( $type eq 'insertBefore' ) { | ||||||||||||||||||||||||||||||
| 57 | 1 | foreach my $action_child (@action_children) { | |||||||||||||||||||||||||||||
| 58 | 3 | $target_node->parentNode->insertBefore($action_child->cloneNode(1), $target_node); | |||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||||
| 61 | elsif ( $type eq 'insertAfter' ) { | ||||||||||||||||||||||||||||||
| 62 | 1 | foreach my $action_child (reverse(@action_children)) { | |||||||||||||||||||||||||||||
| 63 | 3 | $target_node->parentNode->insertAfter($action_child->cloneNode(1), $target_node); | |||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||||
| 66 | elsif ( $type eq 'setAttribute' ) { | ||||||||||||||||||||||||||||||
| 67 | 1 | $target_node->setAttribute($attribute, $action->textContent()); | |||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||||
| 69 | elsif ( $type eq 'removeAttribute' ) { | ||||||||||||||||||||||||||||||
| 70 | 6 | $target_node->removeAttribute($attribute); | |||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||
| 75 | 1 | 1; | |||||||||||||||||||||||||||||
| 76 | __END__ | ||||||||||||||||||||||||||||||