File Coverage

blib/lib/XML/GrammarBase/Role/RelaxNG.pm
Criterion Covered Total %
statement 13 15 86.6
branch n/a
condition n/a
subroutine 5 5 100.0
pod n/a
total 18 20 90.0


line stmt bran cond sub pod time code
1             package XML::GrammarBase::Role::RelaxNG;
2              
3 3     3   220799 use strict;
  3         8  
  3         114  
4 3     3   16 use warnings;
  3         6  
  3         92  
5              
6              
7             =head1 NAME
8              
9             XML::GrammarBase::Role::RelaxNG - base class for a RelaxNG validator
10              
11             =head1 VERSION
12              
13             Version 0.2.3
14              
15             =cut
16              
17 3     3   3138 use MooX::Role 'late';
  3         581  
  3         17  
18              
19 3     3   39494 use File::ShareDir qw(dist_dir);
  3         37061  
  3         265  
20 3     3   3763 use XML::LibXML '2.0017';
  0            
  0            
21              
22             with ('XML::GrammarBase::Role::DataDir');
23              
24             our $VERSION = '0.2.3';
25              
26             has 'rng_schema_basename' => (isa => 'Str', is => 'rw');
27             has '_rng' =>
28             (
29             isa => 'XML::LibXML::RelaxNG',
30             is => 'rw',
31             default => sub { return shift->_calc_default_rng_schema; },
32             lazy => 1,
33             );
34              
35             sub _calc_default_rng_schema
36             {
37             my ($self) = @_;
38              
39             my $rngschema =
40             XML::LibXML::RelaxNG->new(
41             location =>
42             $self->dist_path_slot('rng_schema_basename'),
43             );
44              
45             return $rngschema;
46             }
47              
48             sub rng_validate_dom
49             {
50             my ($self, $source_dom) = @_;
51              
52             my $ret_code;
53              
54             eval
55             {
56             $ret_code = $self->_rng()->validate($source_dom);
57             };
58              
59             if (defined($ret_code) && ($ret_code == 0))
60             {
61             # It's OK.
62             }
63             else
64             {
65             confess "RelaxNG validation failed [\$ret_code == "
66             . $self->_undefize($ret_code) . " ; $@]"
67             ;
68             }
69              
70             return;
71             }
72              
73             sub _calc_parser
74             {
75             my ($self) = @_;
76              
77             my $xml_parser = XML::LibXML->new();
78              
79             $xml_parser->validation(0);
80             $xml_parser->load_ext_dtd(0);
81             $xml_parser->no_network(1);
82              
83             return $xml_parser;
84             }
85              
86             sub rng_validate_file
87             {
88             my ($self, $filename) = @_;
89              
90             my $dom = $self->_calc_parser()->parse_file($filename);
91              
92             return $self->rng_validate_dom($dom);
93             }
94              
95             sub rng_validate_string
96             {
97             my ($self, $xml_string) = @_;
98              
99             my $dom = $self->_calc_parser()->parse_string($xml_string);
100              
101             return $self->rng_validate_dom($dom);
102             }
103              
104             =head1 SYNOPSIS
105              
106             package XML::Grammar::MyGrammar::RelaxNG::Validate;
107              
108             use MooX 'late';
109              
110             with ('XML::GrammarBase::Role::RelaxNG');
111              
112             has '+module_base' => (default => 'XML::Grammar::MyGrammar');
113             has '+rng_schema_basename' => (default => 'my-grammar.rng');
114              
115             package main;
116              
117             my $rnger = XML::Grammar::MyGrammar::RelaxNG::Validate->new(
118             data_dir => "/path/to/data-dir",
119             );
120              
121             # Throws an exception on failure.
122             $rnger->rng_validate_file("/different-path-to-xml-file.xml");
123              
124             =head1 SLOTS
125              
126             =head2 module_base
127              
128             The basename of the module - used for dist dir.
129              
130             =head2 data_dir
131              
132             The data directory where the XML assets can be found (the RELAX NG schema, etc.)
133              
134             =head2 rng_schema_basename
135              
136             The Relax NG Schema basename.
137              
138             =head1 METHODS
139              
140             =head2 $self->rng_validate_dom($source_dom)
141              
142             Validates the DOM ( $source_dom ) using the RELAX-NG schema.
143              
144             =head2 $self->rng_validate_file($file_path)
145              
146             Validates the file in $file_path using the RELAX-NG schema.
147              
148             =head2 $self->rng_validate_string($xml_string)
149              
150             Validates the XML in the $xml_string using the RELAX-NG schema.
151              
152             =head2 $self->dist_path($basename)
153              
154             Returns the $basename relative to data_dir().
155              
156             Utility method.
157              
158             =head2 $self->dist_path_slot($slot)
159              
160             Returns the basename of $self->$slot() relative to data_dir().
161              
162             Utility method.
163              
164             =head2 BUILD
165              
166             L constructor. For internal use.
167              
168             =head1 AUTHOR
169              
170             Shlomi Fish, C<< >>
171              
172             =head1 BUGS
173              
174             Please report any bugs or feature requests to C, or through
175             the web interface at L. I will be notified, and then you'll
176             automatically be notified of progress on your bug as I make changes.
177              
178              
179              
180              
181             =head1 SUPPORT
182              
183             You can find documentation for this module with the perldoc command.
184              
185             perldoc XML::GrammarBase
186              
187             You can also look for information at:
188              
189             =over 4
190              
191             =item * RT: CPAN's request tracker
192              
193             L
194              
195             =item * AnnoCPAN: Annotated CPAN documentation
196              
197             L
198              
199             =item * CPAN Ratings
200              
201             L
202              
203             =item * Search CPAN
204              
205             L
206              
207             =back
208              
209              
210             =head1 ACKNOWLEDGEMENTS
211              
212              
213             =head1 COPYRIGHT & LICENSE
214              
215             Copyright 2009 Shlomi Fish.
216              
217             This program is distributed under the MIT (X11) License:
218             L
219              
220             Permission is hereby granted, free of charge, to any person
221             obtaining a copy of this software and associated documentation
222             files (the "Software"), to deal in the Software without
223             restriction, including without limitation the rights to use,
224             copy, modify, merge, publish, distribute, sublicense, and/or sell
225             copies of the Software, and to permit persons to whom the
226             Software is furnished to do so, subject to the following
227             conditions:
228              
229             The above copyright notice and this permission notice shall be
230             included in all copies or substantial portions of the Software.
231              
232             THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
233             EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
234             OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
235             NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
236             HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
237             WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
238             FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
239             OTHER DEALINGS IN THE SOFTWARE.
240              
241             =cut
242              
243             1; # End of XML::GrammarBase::RelaxNG::Validate
244