File Coverage

blib/lib/XML/Toolkit/Generator/Default.pm
Criterion Covered Total %
statement 9 67 13.4
branch 0 38 0.0
condition n/a
subroutine 3 12 25.0
pod n/a
total 12 117 10.2


line stmt bran cond sub pod time code
1             package XML::Toolkit::Generator::Default;
2             {
3             $XML::Toolkit::Generator::Default::VERSION = '0.15';
4             }
5 2     2   3795 use Moose;
  2         6  
  2         21  
6 2     2   18060 use Encode;
  2         7  
  2         211  
7 2     2   13 use namespace::autoclean;
  2         5  
  2         26  
8              
9             extends qw(XML::Generator::Moose);
10             with qw(
11             XML::Toolkit::Generator::Interface
12             XML::Toolkit::Builder::NamespaceRegistry
13             );
14              
15             after 'xml_decl' => sub {
16             my $self = shift;
17             for my $pair ( $self->xmlns_pairs ) {
18             my ( $prefix, $uri ) = @$pair;
19             $self->start_prefix_mapping( $prefix => $uri, );
20             }
21             $self->newline;
22             };
23              
24             sub get_element_name {
25 0     0     my ( $self, $meta ) = @_;
26 0 0         if ( $meta->can('description') ) {
27 0           return $meta->description->{Name};
28             }
29             else {
30 0           my $name = $meta->name;
31 0           $name =~ /::(\w+)$/oi;
32 0           return lcfirst $1;
33             }
34             }
35              
36             sub is_node {
37 0     0     my ( $self, $attr ) = @_;
38 0 0         confess "no attribute" unless $attr;
39 0 0         return 0 unless $attr->can('description');
40 0 0         return 0 unless $attr->description->{node_type};
41 0           return 1;
42             }
43              
44             sub is_child_node {
45 0     0     my ( $self, $attr ) = @_;
46 0 0         return 0 unless $self->is_node($attr);
47 0 0         return 1 if $attr->description->{node_type} eq 'child';
48 0           return 0;
49             }
50              
51             sub is_text_node {
52 0     0     my ( $self, $attr ) = @_;
53 0 0         return 0 unless $self->is_node($attr);
54 0 0         return 1 if $attr->description->{node_type} eq 'character';
55 0           return 0;
56             }
57              
58             sub is_cdata_node {
59 0     0     my ( $self, $attr ) = @_;
60 0 0         return 0 unless $self->is_node($attr);
61 0 0         return 1 if $attr->description->{cdata};
62 0           return 0;
63             }
64              
65             sub is_attribute_node {
66 0     0     my ( $self, $attr ) = @_;
67 0 0         return 0 unless $self->is_node($attr);
68 0 0         return 1 if $attr->description->{node_type} eq 'attribute';
69 0           return 0;
70             }
71              
72             sub get_attribute_nodes {
73 0     0     my ( $self, $meta, $obj ) = @_;
74 0           my @attrs = grep { $self->is_attribute_node($_) }
  0            
75 0           grep { defined $_->get_value($obj) }
76 0           map { $meta->get_attribute($_) } $meta->get_attribute_list;
77 0           return map {
78 0           $_->description->{LocalName} =>
79 0           { %{ $_->description }, Value => $_->get_value($obj) }
80             } @attrs;
81             }
82              
83             sub parse_object {
84 0     0     my ( $self, $meta, $obj, $descr ) = @_;
85 0           my %attrs = $self->get_attribute_nodes( $meta, $obj );
86 0           my $name = $descr->{Name};
87              
88 0           $self->start_element(
89             $name => \%attrs,
90             $descr,
91             );
92              
93 0           for my $attr ( $self->_get_sorted_filtered_attributes($meta) ) {
94 0 0         if ( $self->is_text_node($attr) ) {
    0          
95 0           my $data = $attr->get_value($obj);
96 0 0         $self->is_cdata_node($attr)
97             ? $self->cdata($data)
98             : $self->characters($data);
99             }
100             elsif ( $self->is_child_node($attr) ) {
101 0 0         next unless my $value = $attr->get_value($obj);
102 0           for my $child ( grep { defined } @$value ) {
  0            
103 0 0         next unless blessed $child;
104 0           $self->parse_object( $child->meta, $child, $attr->description );
105             }
106             }
107 0           else { warn "${\$attr->dump} is funky" }
  0            
108             }
109 0           $self->end_element($name);
110             }
111              
112             augment 'parse' => sub {
113             my ( $self, $obj ) = @_;
114             $self->parse_object( $obj->meta, $obj, { Name => $self->get_element_name( $obj->meta ) } );
115              
116             };
117              
118             sub _get_sorted_filtered_attributes {
119 0     0     my ( $self, $meta ) = @_;
120 0 0         sort {
121 0           return -1 unless exists $a->description->{sort_order};
122 0 0         return 1 unless exists $b->description->{sort_order};
123 0           return $a->description->{sort_order} <=> $b->description->{sort_order}
124             }
125 0           grep { !$self->is_attribute_node($_) }
126 0           grep { !$_->does('XML::Toolkit::Trait::NoXML') }
127             $meta->get_all_attributes;
128             }
129              
130             __PACKAGE__->meta->make_immutable;
131             1;
132             __END__
133              
134             =head1 NAME
135              
136             XML::Toolkit::Generator::Default - A Default Moose Object to XML Generator
137              
138             =head1 VERSION
139              
140             version 0.15
141              
142             =head1 SYNOPSIS
143              
144             use XML::Toolkit::Generator::Default;
145             XML::Toolkit::Generator::Default->new( Handler => XML::SAX::Writer->new );
146              
147             =head1 DESCRIPTION
148              
149             A subclass of XML::Generator::Moose, this class generates SAX events from
150             Moose objects.
151            
152             =head1 ATTRIBUTES
153              
154             See XML::Generator::Moose.
155              
156             =head1 METHODS
157              
158             =over
159              
160             =item get_element_name
161              
162             =item is_node
163              
164             =item is_child_node
165              
166             =item is_text_node
167              
168             =item is_attribute_node
169              
170             =item get_attribute_nodes
171              
172             =item parse_object
173              
174             =back
175              
176             See Also XML::Generator::Moose
177              
178             =head1 INCOMPATIBILITIES
179              
180             None reported.
181              
182             =head1 BUGS AND LIMITATIONS
183              
184             No bugs have been reported.
185              
186             Please report any bugs or feature requests to
187             C<bug-xml-toolkit@rt.cpan.org>, or through the web interface at
188             L<http://rt.cpan.org>.
189              
190             =head1 AUTHOR
191              
192             Chris Prather C<< <chris@prather.org> >>
193              
194             =head1 LICENCE AND COPYRIGHT
195              
196             Copyright (c) 2008, Chris Prather C<< <chris@prather.org> >>. Some rights reserved.
197              
198             This module is free software; you can redistribute it and/or
199             modify it under the same terms as Perl itself. See L<perlartistic>.