| 1 | package XML::Schematron::SchemaReader; | ||
| 2 | use Moose; | ||
| 3 | use namespace::autoclean; | ||
| 4 | use MooseX::NonMoose; | ||
| 5 | extends 'XML::SAX::Base'; | ||
| 6 | |||
| 7 | use XML::Schematron::Test; | ||
| 8 | use Data::Dumper; | ||
| 9 | |||
| 10 | has test_stack => ( | ||
| 11 | traits => ['Array'], | ||
| 12 | is => 'rw', | ||
| 13 | isa => 'ArrayRef[XML::Schematron::Test]', | ||
| 14 | default => sub { [] }, | ||
| 15 | handles => { | ||
| 16 | add_test => 'push', | ||
| 17 | } | ||
| 18 | ); | ||
| 19 | |||
| 20 | has [qw|context test_type expression|] => ( | ||
| 21 | traits => ['String'], | ||
| 22 | is => 'rw', | ||
| 23 | isa => 'Str', | ||
| 24 | default => sub { return ''; } | ||
| 25 | ); | ||
| 26 | |||
| 27 | has pattern => ( | ||
| 28 | traits => ['String'], | ||
| 29 | is => 'rw', | ||
| 30 | isa => 'Str', | ||
| 31 | required => 1, | ||
| 32 | default => sub { '[none]' }, | ||
| 33 | ); | ||
| 34 | |||
| 35 | has message => ( | ||
| 36 | traits => ['String'], | ||
| 37 | is => 'rw', | ||
| 38 | isa => 'Str', | ||
| 39 | default => sub { return '' }, | ||
| 40 | handles => { | ||
| 41 | add_to_message => 'append', | ||
| 42 | reset_message => 'clear', | ||
| 43 | }, | ||
| 44 | |||
| 45 | ); | ||
| 46 | |||
| 47 | sub start_element { | ||
| 48 | my ($self, $el) = @_; | ||
| 49 | #warn "processing element " . $el->{LocalName} . "\n"; | ||
| 50 | |||
| 51 | # simplify | ||
| 52 | my $attrs = {}; | ||
| 53 | foreach my $attr ( keys ( %{$el->{Attributes}} ) ) { | ||
| 54 | $attrs->{$el->{Attributes}->{$attr}->{LocalName}} = $el->{Attributes}->{$attr}->{Value}; | ||
| 55 | } | ||
| 56 | |||
| 57 | #warn "EL " . Dumper( $el ); | ||
| 58 | |||
| 59 | if ( defined( $attrs->{context} )) { | ||
| 60 | $self->context( $attrs->{context} ); | ||
| 61 | } | ||
| 62 | |||
| 63 | |||
| 64 | if (( $el->{LocalName} =~ /(assert|report)$/)) { | ||
| 65 | if ( defined( $attrs->{test} )) { | ||
| 66 | $self->expression( $attrs->{test} ); | ||
| 67 | } | ||
| 68 | else { | ||
| 69 | warn "Schema Warning: Assert/Report element found with no associated 'test' attribute."; | ||
| 70 | $self->expression(''); | ||
| 71 | } | ||
| 72 | } | ||
| 73 | elsif ($el->{LocalName} eq 'pattern' && defined( $attrs->{name} )) { | ||
| 74 | $self->pattern( $attrs->{name} ); | ||
| 75 | } | ||
| 76 | } | ||
| 77 | |||
| 78 | sub end_element { | ||
| 79 | my ($self, $el) = @_; | ||
| 80 | |||
| 81 | if (( $el->{LocalName} =~ /(assert|report)$/)) { | ||
| 82 | $self->test_type( $el->{LocalName} ); | ||
| 83 | |||
| 84 | my $test = XML::Schematron::Test->new( | ||
| 85 | test_type => $self->test_type, | ||
| 86 | expression => $self->expression, | ||
| 87 | context => $self->context, | ||
| 88 | message => $self->message, | ||
| 89 | pattern => $self->pattern, | ||
| 90 | ); | ||
| 91 | |||
| 92 | $self->add_test( $test ); | ||
| 93 | $self->reset_message; | ||
| 94 | } | ||
| 95 | } | ||
| 96 | |||
| 97 | sub characters { | ||
| 98 | my ($self, $characters) = @_; | ||
| 99 | $self->add_to_message( $characters->{Data} ); | ||
| 100 | } | ||
| 101 | |||
| 102 | no Moose; | ||
| 103 | __PACKAGE__->meta->make_immutable; | ||
| 104 | |||
| 105 | =cut | ||
| 106 | |||
| 107 | 1; | ||
| 108 |