| 1 | package WiX3::XML::Role::Tag; | ||
| 2 | |||
| 3 | use 5.008003; | ||
| 4 | use Moose::Role 2; | ||
| 5 | use Params::Util qw( _STRING _NONNEGINT ); | ||
| 6 | use WiX3::Exceptions; | ||
| 7 | |||
| 8 | our $VERSION = '0.011'; | ||
| 9 | |||
| 10 | ##################################################################### | ||
| 11 | # Methods | ||
| 12 | |||
| 13 | # Tags have to be able to be strings. | ||
| 14 | requires 'as_string'; | ||
| 15 | |||
| 16 | # Tags have to return the namespace they're in. | ||
| 17 | requires 'get_namespace'; | ||
| 18 | |||
| 19 | ######################################## | ||
| 20 | # indent($spaces, $string) | ||
| 21 | # Parameters: | ||
| 22 | # $spaces_num: Number of spaces to indent $string. | ||
| 23 | # $string: String to indent. | ||
| 24 | # Returns: | ||
| 25 | # Indented $string. | ||
| 26 | |||
| 27 | sub indent { | ||
| 28 | my ( $self, $spaces_num, $string ) = @_; | ||
| 29 | |||
| 30 | # Check parameters. | ||
| 31 | if ( not defined $string ) { | ||
| 32 | WiX3::Exception::Parameter::Missing->throw('string'); | ||
| 33 | } | ||
| 34 | |||
| 35 | if ( not defined $spaces_num ) { | ||
| 36 | WiX3::Exception::Parameter::Missing->throw('spaces_num'); | ||
| 37 | } | ||
| 38 | |||
| 39 | if ( not defined _STRING($string) ) { | ||
| 40 | WiX3::Exception::Parameter::Invalid->throw('string'); | ||
| 41 | } | ||
| 42 | |||
| 43 | if ( not defined _NONNEGINT($spaces_num) ) { | ||
| 44 | WiX3::Exception::Parameter::Invalid->throw('spaces_num'); | ||
| 45 | } | ||
| 46 | |||
| 47 | # Indent string. | ||
| 48 | my $spaces = q{ } x $spaces_num; | ||
| 49 | my $answer = $spaces . $string; | ||
| 50 | chomp $answer; | ||
| 51 | #<<< | ||
| 52 | $answer =~ s{\n} # match a newline | ||
| 53 | {\n$spaces}gxms; # and add spaces after it. | ||
| 54 | # (i.e. the beginning of the line.) | ||
| 55 | #>>> | ||
| 56 | return $answer; | ||
| 57 | } ## end sub indent | ||
| 58 | |||
| 59 | sub get_componentref_array { | ||
| 60 | my $self = shift; | ||
| 61 | |||
| 62 | my @components; | ||
| 63 | my $count = $self->count_child_tags(); | ||
| 64 | |||
| 65 | if ( $self->isa('WiX3::XML::Component') ) { | ||
| 66 | require WiX3::XML::ComponentRef; | ||
| 67 | return WiX3::XML::ComponentRef->new($self); | ||
| 68 | } | ||
| 69 | |||
| 70 | if ( $self->isa('WiX3::XML::Feature') ) { | ||
| 71 | require WiX3::XML::FeatureRef; | ||
| 72 | return WiX3::XML::FeatureRef->new($self); | ||
| 73 | } | ||
| 74 | |||
| 75 | if ( $self->isa('WiX3::XML::Merge') ) { | ||
| 76 | require WiX3::XML::MergeRef; | ||
| 77 | return WiX3::XML::MergeRef->new($self); | ||
| 78 | } | ||
| 79 | |||
| 80 | if ( 0 == $count ) { | ||
| 81 | return (); | ||
| 82 | } | ||
| 83 | |||
| 84 | foreach my $tag ( $self->get_child_tags() ) { | ||
| 85 | if ( $tag->does('WiX3::XML::Role::TagAllowsChildTags') ) { | ||
| 86 | push @components, $tag->get_componentref_array(); | ||
| 87 | } else { | ||
| 88 | return (); | ||
| 89 | } | ||
| 90 | } | ||
| 91 | |||
| 92 | return @components; | ||
| 93 | } ## end sub get_componentref_array | ||
| 94 | |||
| 95 | sub print_attribute { | ||
| 96 | my $self = shift; | ||
| 97 | my $attribute = shift || undef; | ||
| 98 | my $value = shift || undef; | ||
| 99 | |||
| 100 | if ( not defined $attribute ) { | ||
| 101 | WiX3::Exception::Parameter::Missing->throw('attribute'); | ||
| 102 | } | ||
| 103 | |||
| 104 | # TODO: $attribute needs to be an identifier. | ||
| 105 | |||
| 106 | if ( not defined $value ) { | ||
| 107 | return q{}; | ||
| 108 | } | ||
| 109 | |||
| 110 | if ( $value =~ m{[']}ms ) { | ||
| 111 | return qq{ $attribute="$value"}; | ||
| 112 | } else { | ||
| 113 | return qq{ $attribute='$value'}; | ||
| 114 | } | ||
| 115 | |||
| 116 | } ## end sub print_attribute | ||
| 117 | |||
| 118 | no Moose::Role; | ||
| 119 | |||
| 120 | 1; | ||
| 121 | |||
| 122 | __END__ |