File Coverage

blib/lib/XML/Toolkit/Builder/Filter.pm
Criterion Covered Total %
statement 42 46 91.3
branch 10 12 83.3
condition 2 2 100.0
subroutine 8 11 72.7
pod 6 6 100.0
total 68 77 88.3


line stmt bran cond sub pod time code
1             package XML::Toolkit::Builder::Filter;
2             {
3             $XML::Toolkit::Builder::Filter::VERSION = '0.15';
4             }
5 2     2   3836 use Moose;
  2         5  
  2         21  
6 2     2   15187 use Moose::Util::TypeConstraints;
  2         7  
  2         21  
7 2     2   4628 use aliased 'XML::Toolkit::MetaDescription::Trait' => 'XMLTrait';
  2         4  
  2         19  
8              
9 2     2   198 use namespace::autoclean;
  2         4  
  2         21  
10              
11             extends qw(XML::Filter::Moose);
12              
13             with qw(
14             XML::Toolkit::Builder::ClassRegistry
15             XML::Toolkit::Builder::ClassTemplate
16             );
17              
18             around create_class => sub {
19             my ( $next, $self, $name, $params ) = @_;
20             return $self->get_class($name) if $self->has_class($name);
21             class_type $name; # we only need to generate the class type when we build a new class
22             $self->$next( $name => %$params );
23             };
24              
25             sub add_attribute {
26 6     6 1 11315 my ( $self, $class, $type, $attr ) = @_;
27 6 100       33 my $name = $attr->{LocalName} . ( $type eq 'child' ? '_collection' : '' );
28 6 100       35 return if $class->has_attribute($name);
29              
30 5         71 my $param = { map { $_ => $attr->{$_} } qw(isa is auto_deref) };
  15         53  
31 5   100     37 $param->{isa} //= 'Str';
32 5         11 $param->{is} = 'bare';
33 5         22 $param->{traits} = [XMLTrait];
34 5         21 $param->{default} = $attr->{Value};
35 5         40 $param->{description} = {
36             node_type => $type,
37             Prefix => $attr->{Prefix},
38             NamespaceURI => $attr->{NamespaceURI},
39             LocalName => $attr->{LocalName},
40             Name => $attr->{Name},
41             };
42 5 50       23 $class->add_attribute( $name => $param )
43             unless $class->has_attribute($name);
44             }
45              
46             sub add_text_attribute {
47 4     4 1 10 my ( $self, $class ) = @_;
48 4         184 $class->add_attribute(
49             'text' => (
50             isa => 'Str',
51             is => 'rw',
52             traits => [XMLTrait],
53             description => {
54             node_type => 'character',
55             cdata => $self->cdata,
56             },
57             )
58             );
59             }
60              
61             sub start_cdata {
62 0     0 1 0 my ( $self, $el ) = @_;
63 0         0 $self->cdata(1);
64             }
65              
66             sub end_cdata {
67 0     0 1 0 my ( $self, $el ) = @_;
68             # $self->cdata(0);
69             }
70              
71             sub start_element {
72 5     5 1 4344 my ( $self, $el ) = @_;
73              
74 5         37 my $classname = $self->get_class_name($el);
75              
76 5         18 $el->{classname} = $classname;
77              
78 5         33 my $class = $self->create_class( $classname => $el );
79 5         7960 $self->add_class( $classname => $class );
80 5         31 $self->add_attribute( $class, 'attribute' => $_ )
81 5         10 for values %{ $el->{Attributes} };
82              
83 5 100       8003 unless ( $self->is_root ) {
84 2         14 my $parent = $self->get_class( $self->parent_element->{classname} );
85             $self->add_attribute(
86             $parent, 'child',
87             {
88             %$el,
89             isa => "ArrayRef[$classname]",
90             auto_deref => 1,
91             lazy => 1,
92 0     0   0 default => sub { [] },
93             }
94 2         32 );
95             }
96              
97             # cribbed from XML::Filter::Moose
98 5         7288 $self->add_element($el);
99             }
100              
101             sub end_element {
102 5     5 1 838 my ( $self, $el ) = @_;
103              
104 5         276 my $top = $self->current_element;
105              
106 5 100       222 $self->add_text_attribute( $self->get_class( $top->{classname} ) )
107             if $self->has_text;
108 5 50       36701 confess "INVALID PARSE: $el->{Name} ne $top->{Name}"
109             unless $el->{Name} eq $top->{Name};
110              
111             # cribbed from XML::Filter::Moose
112 5         263 $self->pop_element;
113 5         224 $self->reset_text;
114              
115             }
116              
117             __PACKAGE__->meta->make_immutable;
118             1; # Magic true value required at end of module
119             __END__
120              
121             =head1 NAME
122              
123              
124             =head1 VERSION
125              
126             version 0.15
127             XML::Toolkit::Builder::Filter - An XML::SAX Filter that generates Moose Classes
128             from SAX events.
129              
130             =head1 SYNOPSIS
131              
132             use XML::Toolkit::Builder::Filter;
133              
134             my $filter = XML::Toolkit::Builder::Filter->new(
135             template => $template,
136             namespace => $namespace
137             );
138              
139             my $parser = XML::SAX::ParserFactory->parser( Handler => $filter );
140              
141             $parser->parse_file($file);
142             print $filter->render;
143              
144             =head1 DESCRIPTION
145              
146             The XML::Toolkit::Builder::Filter class implements an XML::SAX Filter that
147             generates Moose Classes from SAX events.
148              
149             =head1 METHODS
150              
151             =head2 get_class_name()
152              
153             Insert description of method here...
154              
155             =head2 create_class($name, %params)
156              
157             Insert description of method here...
158              
159             =head2 add_attribute($class, $type, $attribute)
160              
161             Insert description of method here...
162              
163             =head2 add_text_attribute($class, $element)
164              
165             Insert description of method here...
166              
167             =head1 DEPENDENCIES
168              
169             Modules used, version dependencies, core yes/no
170              
171             L<Moose|Moose>
172              
173             L<MooseX::AttributeHelpers|MooseX::AttributeHelpers>
174              
175             L<Moose::Util::TypeConstraints|Moose::Util::TypeConstraints>
176              
177             L<XML::Filter::Moose|XML::Filter::Moose>
178              
179             =head1 BUGS AND LIMITATIONS
180              
181             Please report any bugs or feature requests to
182             C<bug-xml-toolkit@rt.cpan.org>, or through the web interface at
183             L<http://rt.cpan.org>.
184              
185             =head1 AUTHOR
186              
187             Chris Prather (chris@prather.org)
188              
189             =head1 LICENCE
190              
191             Copyright 2009 by Chris Prather.
192              
193             This software is free. It is licensed under the same terms as Perl itself.
194              
195             =cut