| 1 | package XML::Schematron::Schema; | ||
| 2 | |||
| 3 | use Moose::Role; | ||
| 4 | use namespace::autoclean; | ||
| 5 | |||
| 6 | use MooseX::Types::Path::Class; | ||
| 7 | use XML::SAX::ParserFactory; | ||
| 8 | use XML::Schematron::SchemaReader; | ||
| 9 | use XML::Filter::BufferText; | ||
| 10 | |||
| 11 | requires qw( tests ); | ||
| 12 | |||
| 13 | has sax_filter => ( | ||
| 14 | is => 'ro', | ||
| 15 | isa => 'XML::Filter::BufferText', | ||
| 16 | lazy_build => 1, | ||
| 17 | ); | ||
| 18 | |||
| 19 | sub _build_sax_filter { | ||
| 20 | my $self = shift; | ||
| 21 | return XML::Filter::BufferText->new( Handler => $self->sax_handler ); | ||
| 22 | } | ||
| 23 | |||
| 24 | has sax_handler => ( | ||
| 25 | is => 'ro', | ||
| 26 | isa => 'XML::Schematron::SchemaReader', | ||
| 27 | default => sub { return XML::Schematron::SchemaReader->new(); }, | ||
| 28 | ); | ||
| 29 | |||
| 30 | has sax_parser => ( | ||
| 31 | is => 'ro', | ||
| 32 | isa => 'Object', | ||
| 33 | lazy_build => 1, | ||
| 34 | ); | ||
| 35 | |||
| 36 | sub _build_sax_parser { | ||
| 37 | my $self = shift; | ||
| 38 | return XML::SAX::ParserFactory->parser(Handler => $self->sax_filter); | ||
| 39 | } | ||
| 40 | |||
| 41 | sub parse_schema { | ||
| 42 | my $self = shift; | ||
| 43 | my $parser = $self->sax_parser; | ||
| 44 | $parser->parse_file( $self->schema->stringify ); | ||
| 45 | |||
| 46 | my $tests = $self->sax_handler->test_stack || []; | ||
| 47 | $self->tests( $tests ); | ||
| 48 | |||
| 49 | return 1; | ||
| 50 | } | ||
| 51 | |||
| 52 | has schema => ( | ||
| 53 | is => 'rw', | ||
| 54 | isa => 'Path::Class::File', | ||
| 55 | coerce => 1, | ||
| 56 | predicate => 'has_schema', | ||
| 57 | ); | ||
| 58 | |||
| 59 | |||
| 60 | 1; |