blib/lib/XML/GrammarBase/Role/RelaxNG.pm

filter: Statementstmt 100% 43 / 43 Branchbran 100% 2 / 2 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 100% 11 / 11 Podpod 100% 3 / 3 totaltotal 96.8% 62 / 64 CC 3 SCAR 3.8
CC
3
Cov
98%
rng_validate_dom
3.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.5
« blib/lib/XML/GrammarBase/Role/DataDir.pm ↑ Summary blib/lib/XML/GrammarBase/Role/XSLT.pm »
1 package XML::GrammarBase::Role::RelaxNG;
2 4 $XML::GrammarBase::Role::RelaxNG::VERSION = '0.2.6';
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6  
7 4 ▶use MooX::Role 'late';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 4 ▶use File::ShareDir qw(dist_dir);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use XML::LibXML '2.0017';
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 4 with('XML::GrammarBase::Role::DataDir');
13  
14 4 has 'rng_schema_basename' => ( isa => 'Str', is => 'rw' );
15 has '_rng' => (
16 isa => 'XML::LibXML::RelaxNG',
17 is => 'rw',
18 6 ▶ default => sub { return shift->_calc_default_rng_schema; },
Subroutine __ANON__: called CC=1 SCAR=0.0
19 4 lazy => 1,
20 );
21  
22 sub _calc_default_rng_schema
23 {
24 6 ▶ my ($self) = @_;
Subroutine _calc_default_rng_schema: called CC=1 SCAR=0.0
25  
26 6 my $rngschema =
27 XML::LibXML::RelaxNG->new(
28 location => $self->dist_path_slot('rng_schema_basename'), );
29  
30 6 return $rngschema;
31 }
32  
33 sub rng_validate_dom
34 {
35 6 ▶ my ( $self, $source_dom ) = @_;
Subroutine rng_validate_dom: called CC=3 SCAR=11.0
Pod documented
36  
37 6 my $ret_code;
38  
39 6 eval { $ret_code = $self->_rng()->validate($source_dom); };
40  
41 6 ▶ if ( defined($ret_code) && ( $ret_code == 0 ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ret_code and $ret_code == 0) { } 3 3 6
Condition66%2/3
defined $ret_code and $ret_code == 0
!ll&&!rl&&r
303
MC/DC50%1/2
defined $ret_code and $ret_code == 0
defined $ret_code $ret_code == 0
Truth table66%2/3
A and B
A= defined $ret_codeB= $ret_code == 0
ABresult
0X0
100
111
42 {
43 # It's OK.
44 }
45 else
46 {
47 3 confess "RelaxNG validation failed [\$ret_code == "
48 . $self->_undefize($ret_code)
49 . " ; $@]";
50 }
51  
52 3 return;
53 }
54  
55 sub _calc_parser
56 {
57 4 ▶ my ($self) = @_;
Subroutine _calc_parser: called CC=1 SCAR=0.0
58  
59 4 my $xml_parser = XML::LibXML->new();
60  
61 4 $xml_parser->validation(0);
62 4 $xml_parser->load_ext_dtd(0);
63 4 $xml_parser->no_network(1);
64  
65 4 return $xml_parser;
66 }
67  
68 sub rng_validate_file
69 {
70 2 ▶ my ( $self, $filename ) = @_;
Subroutine rng_validate_file: called CC=1 SCAR=0.0
Pod documented
71  
72 2 my $dom = $self->_calc_parser()->parse_file($filename);
73  
74 2 return $self->rng_validate_dom($dom);
75 }
76  
77 sub rng_validate_string
78 {
79 2 ▶ my ( $self, $xml_string ) = @_;
Subroutine rng_validate_string: called CC=1 SCAR=0.0
Pod documented
80  
81 2 my $dom = $self->_calc_parser()->parse_string($xml_string);
82  
83 2 return $self->rng_validate_dom($dom);
84 }
85  
86  
87 4 1; # End of XML::GrammarBase::RelaxNG::Validate
88  
89 __END__
90  
91 =pod
92  
93 =encoding UTF-8
94  
95 =head1 NAME
96  
97 XML::GrammarBase::Role::RelaxNG - base class for a RelaxNG validator
98  
99 =head1 VERSION
100  
101 version 0.2.6
102  
103 =head1 SYNOPSIS
104  
105 package XML::Grammar::MyGrammar::RelaxNG::Validate;
106  
107 use MooX 'late';
108  
109 with ('XML::GrammarBase::Role::RelaxNG');
110  
111 has '+module_base' => (default => 'XML::Grammar::MyGrammar');
112 has '+rng_schema_basename' => (default => 'my-grammar.rng');
113  
114 package main;
115  
116 my $rnger = XML::Grammar::MyGrammar::RelaxNG::Validate->new(
117 data_dir => "/path/to/data-dir",
118 );
119  
120 # Throws an exception on failure.
121 $rnger->rng_validate_file("/different-path-to-xml-file.xml");
122  
123 =head1 VERSION
124  
125 version 0.2.6
126  
127 =head1 SLOTS
128  
129 =head2 module_base
130  
131 The basename of the module - used for dist dir.
132  
133 =head2 data_dir
134  
135 The data directory where the XML assets can be found (the RELAX NG schema, etc.)
136  
137 =head2 rng_schema_basename
138  
139 The Relax NG Schema basename.
140  
141 =head1 METHODS
142  
143 =head2 $self->rng_validate_dom($source_dom)
144  
145 Validates the DOM ( $source_dom ) using the RELAX-NG schema.
146  
147 =head2 $self->rng_validate_file($file_path)
148  
149 Validates the file in $file_path using the RELAX-NG schema.
150  
151 =head2 $self->rng_validate_string($xml_string)
152  
153 Validates the XML in the $xml_string using the RELAX-NG schema.
154  
155 =head2 $self->dist_path($basename)
156  
157 Returns the $basename relative to data_dir().
158  
159 Utility method.
160  
161 =head2 $self->dist_path_slot($slot)
162  
163 Returns the basename of $self->$slot() relative to data_dir().
164  
165 Utility method.
166  
167 =head2 BUILD
168  
169 L<Any::Moose> constructor. For internal use.
170  
171 =head1 AUTHOR
172  
173 Shlomi Fish, C<< <shlomif at cpan.org> >>
174  
175 =head1 BUGS
176  
177 Please report any bugs or feature requests to C<bug-xml-grammarbase at rt.cpan.org>, or through
178 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=XML-GrammarBase>. I will be notified, and then you'll
179 automatically be notified of progress on your bug as I make changes.
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<http://rt.cpan.org/NoAuth/Bugs.html?Dist=XML-GrammarBase>
194  
195 =item * AnnoCPAN: Annotated CPAN documentation
196  
197 L<http://annocpan.org/dist/XML-GrammarBase>
198  
199 =item * CPAN Ratings
200  
201 L<http://cpanratings.perl.org/d/XML-GrammarBase>
202  
203 =item * Search CPAN
204  
205 L<http://search.cpan.org/dist/XML-GrammarBase/>
206  
207 =back
208  
209 =head1 ACKNOWLEDGEMENTS
210  
211 =head1 COPYRIGHT & LICENSE
212  
213 Copyright 2009 Shlomi Fish.
214  
215 This program is distributed under the MIT (X11) License:
216 L<http://www.opensource.org/licenses/mit-license.php>
217  
218 Permission is hereby granted, free of charge, to any person
219 obtaining a copy of this software and associated documentation
220 files (the "Software"), to deal in the Software without
221 restriction, including without limitation the rights to use,
222 copy, modify, merge, publish, distribute, sublicense, and/or sell
223 copies of the Software, and to permit persons to whom the
224 Software is furnished to do so, subject to the following
225 conditions:
226  
227 The above copyright notice and this permission notice shall be
228 included in all copies or substantial portions of the Software.
229  
230 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
231 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
232 OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
233 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
234 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
235 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
236 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
237 OTHER DEALINGS IN THE SOFTWARE.
238  
239 =head1 AUTHOR
240  
241 Shlomi Fish <shlomif@cpan.org>
242  
243 =head1 COPYRIGHT AND LICENSE
244  
245 This software is Copyright (c) 2018 by Shlomi Fish.
246  
247 This is free software, licensed under:
248  
249 The MIT (X11) License
250  
251 =head1 BUGS
252  
253 Please report any bugs or feature requests on the bugtracker website
254 L<https://github.com/shlomif/perl-XML-GrammarBase/issues>
255  
256 When submitting a bug or request, please include a test-file or a
257 patch to an existing test-file that illustrates the bug or desired
258 feature.
259  
260 =for :stopwords cpan testmatrix url annocpan anno bugtracker rt cpants kwalitee diff irc mailto metadata placeholders metacpan
261  
262 =head1 SUPPORT
263  
264 =head2 Perldoc
265  
266 You can find documentation for this module with the perldoc command.
267  
268 perldoc XML::GrammarBase::Role::RelaxNG
269  
270 =head2 Websites
271  
272 The following websites have more information about this module, and may be of help to you. As always,
273 in addition to those websites please use your favorite search engine to discover more resources.
274  
275 =over 4
276  
277 =item *
278  
279 MetaCPAN
280  
281 A modern, open-source CPAN search engine, useful to view POD in HTML format.
282  
283 L<https://metacpan.org/release/XML-GrammarBase>
284  
285 =item *
286  
287 Search CPAN
288  
289 The default CPAN search engine, useful to view POD in HTML format.
290  
291 L<http://search.cpan.org/dist/XML-GrammarBase>
292  
293 =item *
294  
295 RT: CPAN's Bug Tracker
296  
297 The RT ( Request Tracker ) website is the default bug/issue tracking system for CPAN.
298  
299 L<https://rt.cpan.org/Public/Dist/Display.html?Name=XML-GrammarBase>
300  
301 =item *
302  
303 AnnoCPAN
304  
305 The AnnoCPAN is a website that allows community annotations of Perl module documentation.
306  
307 L<http://annocpan.org/dist/XML-GrammarBase>
308  
309 =item *
310  
311 CPAN Ratings
312  
313 The CPAN Ratings is a website that allows community ratings and reviews of Perl modules.
314  
315 L<http://cpanratings.perl.org/d/XML-GrammarBase>
316  
317 =item *
318  
319 CPANTS
320  
321 The CPANTS is a website that analyzes the Kwalitee ( code metrics ) of a distribution.
322  
323 L<http://cpants.cpanauthors.org/dist/XML-GrammarBase>
324  
325 =item *
326  
327 CPAN Testers
328  
329 The CPAN Testers is a network of smoke testers who run automated tests on uploaded CPAN distributions.
330  
331 L<http://www.cpantesters.org/distro/X/XML-GrammarBase>
332  
333 =item *
334  
335 CPAN Testers Matrix
336  
337 The CPAN Testers Matrix is a website that provides a visual overview of the test results for a distribution on various Perls/platforms.
338  
339 L<http://matrix.cpantesters.org/?dist=XML-GrammarBase>
340  
341 =item *
342  
343 CPAN Testers Dependencies
344  
345 The CPAN Testers Dependencies is a website that shows a chart of the test results of all dependencies for a distribution.
346  
347 L<http://deps.cpantesters.org/?module=XML::GrammarBase>
348  
349 =back
350  
351 =head2 Bugs / Feature Requests
352  
353 Please report any bugs or feature requests by email to C<bug-xml-grammarbase at rt.cpan.org>, or through
354 the web interface at L<https://rt.cpan.org/Public/Bug/Report.html?Queue=XML-GrammarBase>. You will be automatically notified of any
355 progress on the request by the system.
356  
357 =head2 Source Code
358  
359 The code is open to the world, and available for you to hack on. Please feel free to browse it and play
360 with it, or whatever. If you want to contribute patches, please send me a diff or prod me to pull
361 from your repository :)
362  
363 L<https://github.com/shlomif/perl-XML-GrammarBase>
364  
365 git clone git://github.com/shlomif/perl-XML-GrammarBase.git
366  
367 =cut
« blib/lib/XML/GrammarBase/Role/DataDir.pm ↑ Summary blib/lib/XML/GrammarBase/Role/XSLT.pm »