1package XML::Filter::Moose;
2{
3  $XML::Filter::Moose::VERSION = '0.15';
4}
5use Moose;
6use namespace::autoclean;
7
8use MooseX::NonMoose;
9
10extends qw(XML::SAX::Base);
11
12has stack => (
13    isa        => 'ArrayRef',
14    is         => 'ro',
15    lazy_build => 1,
16    clearer    => 'reset_stack',
17    traits     => ['Array'],
18    handles    => {
19        'add_element'     => ['push'],
20        'pop_element'     => ['pop'],
21        'root'            => [ 'get', 0 ],
22        'current_element' => [ 'get', -1 ],
23        'stack_length'    => ['count'],
24    }
25);
26
27sub _build_stack { [] }
28
29has text => (
30    isa       => 'Str',
31    is        => 'rw',
32    traits    => ['String'],
33    lazy      => 1,
34    clearer   => 'reset_text',
35    predicate => 'has_text',
36    default   => sub { '' },
37    handles   => { append_text => 'append', },
38);
39
40has cdata => ( isa => 'Bool', is => 'rw', );
41
42sub is_root { return shift->stack_length == 0 }
43
44sub parent_element {
45    my ($self) = @_;
46    if ( $self->stack_length > 1 ) {
47        return $self->stack->[-1];
48    }
49    $self->root;
50}
51
52sub start_document {
53    my ( $self, $doc ) = @_;
54    inner();
55    $self->reset_stack;
56}
57
58sub start_element {
59    my ( $self, $el ) = @_;
60    inner();
61    $self->add_element($el);
62}
63
64sub characters {
65    my ( $self, $el ) = @_;
66    inner();
67    $self->append_text( $el->{Data} ) if $el->{Data} =~ /\S/;
68}
69
70sub end_element {
71    my ( $self, $el ) = @_;
72    inner();
73    $self->pop_element;
74    $self->reset_text;
75}
76
77sub end_document {
78    my ( $self, $doc ) = @_;
79    inner();
80}
81
82__PACKAGE__->meta->make_immutable;
831;
84__END__
85
86=head1 NAME
87
88XML::Filter::Moose - A Moose-ified base class for XML::SAX
89
90=head1 VERSION
91
92version 0.15
93
94=head1 SYNOPSIS
95
96    package MyFilter;
97    use Moose
98    extends qw(XML::SAX::Base);
99
100    augment start_element => start {
101        my ($self, $el) = @_;
102        $el->{Data} = [do something];
103    };
104
105=head1 DESCRIPTION
106
107The XML::Filter::Moose class implements ...
108
109=head1 ATTRIBUTES
110
111=head2 stack - ArrayRef
112
113=head2 text - Str
114
115=head1 METHODS
116
117=head2 root()
118
119Returns the root element, or the first element in the stack
120
121=head2 current_element()
122
123Insert description of subroutine here...
124
125=head2 is_root()
126
127Return true if we are currently working on the root element.
128
129=head2 parent_element()
130
131Returns the parent of the current element.
132
133=head2 start_document($document)
134
135Fires at the start of the document.
136
137=head2 start_element($element)
138
139Fires at the start of an element.
140
141=head2 characters($element)
142
143Fires at the start of a text node.
144
145=head2 end_element($element)
146
147Fires at the end of an element.
148
149=head2 end_document($document)
150
151Fires at the end of a document.
152
153=head1 DEPENDENCIES
154
155L<Moose|Moose>
156
157L<XML::SAX::Base|XML::SAX::Base>
158
159=head1 BUGS AND LIMITATIONS
160
161Please report any bugs or feature requests to
162C<bug-xml-toolkit@rt.cpan.org>, or through the web interface at
163L<http://rt.cpan.org>.
164
165=head1 AUTHOR
166
167Chris Prather (chris@prather.org)
168
169Based upon L<XML::SAX::Base|XML::SAX::Base>
170
171Kip Hampton (khampton@totalcinema.com)
172
173Robin Berjon (robin@knowscape.com)
174
175Matt Sergeant (matt@sergeant.org)
176
177=head1 LICENCE
178
179Copyright 2009 by Chris Prather.
180
181This software is free.  It is licensed under the same terms as Perl itself.
182
183=cut
184