| 1 | package WiX3::XML::Role::TagAllowsChildTags; | ||
| 2 | |||
| 3 | use 5.008003; | ||
| 4 | use Moose::Role 2; | ||
| 5 | use WiX3::Exceptions; | ||
| 6 | use WiX3::Types qw(IsTag); | ||
| 7 | use MooseX::Types::Moose qw(ArrayRef); | ||
| 8 | use List::MoreUtils qw( uniq ); | ||
| 9 | |||
| 10 | our $VERSION = '0.011'; | ||
| 11 | |||
| 12 | with 'WiX3::XML::Role::Tag'; | ||
| 13 | |||
| 14 | ##################################################################### | ||
| 15 | # Attributes | ||
| 16 | |||
| 17 | # A tag can contain other tags. | ||
| 18 | has 'child_tags' => ( | ||
| 19 | traits => ['Array'], | ||
| 20 | is => 'rw', | ||
| 21 | isa => ArrayRef [IsTag], | ||
| 22 | init_arg => undef, | ||
| 23 | default => sub { return []; }, | ||
| 24 | handles => { | ||
| 25 | 'get_child_tags' => 'elements', | ||
| 26 | 'add_child_tag' => 'push', | ||
| 27 | 'get_child_tag' => 'get', | ||
| 28 | 'has_child_tags' => 'count', | ||
| 29 | 'count_child_tags' => 'count', | ||
| 30 | 'delete_child_tag' => 'delete', | ||
| 31 | 'clear_child_tags' => 'clear', | ||
| 32 | }, | ||
| 33 | ); | ||
| 34 | |||
| 35 | # I think you could do method aliasing... with 'Role' => { alias => { 'add_child_tag' => '_add_child_tag' } } | ||
| 36 | # then implement your own child tag to do validation | ||
| 37 | |||
| 38 | ##################################################################### | ||
| 39 | # Methods | ||
| 40 | |||
| 41 | sub as_string_children { | ||
| 42 | my $self = shift; | ||
| 43 | |||
| 44 | my $string; | ||
| 45 | my $count = $self->count_child_tags(); | ||
| 46 | |||
| 47 | if ( 0 == $count ) { | ||
| 48 | return q{}; | ||
| 49 | } | ||
| 50 | |||
| 51 | foreach my $tag ( $self->get_child_tags() ) { | ||
| 52 | $string .= $tag->as_string(); | ||
| 53 | } | ||
| 54 | |||
| 55 | return $self->indent( 2, $string ); | ||
| 56 | } ## end sub as_string_children | ||
| 57 | |||
| 58 | sub get_namespaces { | ||
| 59 | my $self = shift; | ||
| 60 | |||
| 61 | my @namespaces = ( $self->get_namespace() ); | ||
| 62 | my $count = $self->count_child_tags(); | ||
| 63 | |||
| 64 | if ( 0 == $count ) { | ||
| 65 | return @namespaces; | ||
| 66 | } | ||
| 67 | |||
| 68 | foreach my $tag ( $self->get_child_tags() ) { | ||
| 69 | if ( $tag->does('get_namespaces') ) { | ||
| 70 | push @namespaces, $tag->get_namespaces(); | ||
| 71 | } else { | ||
| 72 | push @namespaces, $tag->get_namespace(); | ||
| 73 | } | ||
| 74 | } | ||
| 75 | |||
| 76 | return uniq @namespaces; | ||
| 77 | } ## end sub get_namespaces | ||
| 78 | |||
| 79 | sub count_all_child_tags { | ||
| 80 | my $self = shift; | ||
| 81 | |||
| 82 | my $answer = 1; | ||
| 83 | |||
| 84 | foreach my $tag ( $self->get_child_tags() ) { | ||
| 85 | if ( $tag->does('count_all_child_tags') ) { | ||
| 86 | $answer += $tag->count_all_child_tags(); | ||
| 87 | } else { | ||
| 88 | $answer++; | ||
| 89 | } | ||
| 90 | } | ||
| 91 | |||
| 92 | return $answer; | ||
| 93 | } ## end sub count_all_child_tags | ||
| 94 | |||
| 95 | no Moose::Role; | ||
| 96 | |||
| 97 | 1; | ||
| 98 | |||
| 99 | __END__ |