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.8';
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.8
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 SLOTS
124  
125 =head2 module_base
126  
127 The basename of the module - used for dist dir.
128  
129 =head2 data_dir
130  
131 The data directory where the XML assets can be found (the RELAX NG schema, etc.)
132  
133 =head2 rng_schema_basename
134  
135 The Relax NG Schema basename.
136  
137 =head1 METHODS
138  
139 =head2 $self->rng_validate_dom($source_dom)
140  
141 Validates the DOM ( $source_dom ) using the RELAX-NG schema.
142  
143 =head2 $self->rng_validate_file($file_path)
144  
145 Validates the file in $file_path using the RELAX-NG schema.
146  
147 =head2 $self->rng_validate_string($xml_string)
148  
149 Validates the XML in the $xml_string using the RELAX-NG schema.
150  
151 =head2 $self->dist_path($basename)
152  
153 Returns the $basename relative to data_dir().
154  
155 Utility method.
156  
157 =head2 $self->dist_path_slot($slot)
158  
159 Returns the basename of $self->$slot() relative to data_dir().
160  
161 Utility method.
162  
163 =head2 BUILD
164  
165 L<Any::Moose> constructor. For internal use.
166  
167 =head1 AUTHOR
168  
169 Shlomi Fish, C<< <shlomif at cpan.org> >>
170  
171 =head1 ACKNOWLEDGEMENTS
172  
173 =head1 COPYRIGHT & LICENSE
174  
175 Copyright 2009 Shlomi Fish.
176  
177 This program is distributed under the MIT (X11) License:
178 L<http://www.opensource.org/licenses/mit-license.php>
179  
180 Permission is hereby granted, free of charge, to any person
181 obtaining a copy of this software and associated documentation
182 files (the "Software"), to deal in the Software without
183 restriction, including without limitation the rights to use,
184 copy, modify, merge, publish, distribute, sublicense, and/or sell
185 copies of the Software, and to permit persons to whom the
186 Software is furnished to do so, subject to the following
187 conditions:
188  
189 The above copyright notice and this permission notice shall be
190 included in all copies or substantial portions of the Software.
191  
192 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
193 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
194 OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
195 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
196 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
197 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
198 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
199 OTHER DEALINGS IN THE SOFTWARE.
200  
201 =for :stopwords cpan testmatrix url bugtracker rt cpants kwalitee diff irc mailto metadata placeholders metacpan
202  
203 =head1 SUPPORT
204  
205 =head2 Websites
206  
207 The following websites have more information about this module, and may be of help to you. As always,
208 in addition to those websites please use your favorite search engine to discover more resources.
209  
210 =over 4
211  
212 =item *
213  
214 MetaCPAN
215  
216 A modern, open-source CPAN search engine, useful to view POD in HTML format.
217  
218 L<https://metacpan.org/release/XML-GrammarBase>
219  
220 =item *
221  
222 RT: CPAN's Bug Tracker
223  
224 The RT ( Request Tracker ) website is the default bug/issue tracking system for CPAN.
225  
226 L<https://rt.cpan.org/Public/Dist/Display.html?Name=XML-GrammarBase>
227  
228 =item *
229  
230 CPANTS
231  
232 The CPANTS is a website that analyzes the Kwalitee ( code metrics ) of a distribution.
233  
234 L<http://cpants.cpanauthors.org/dist/XML-GrammarBase>
235  
236 =item *
237  
238 CPAN Testers
239  
240 The CPAN Testers is a network of smoke testers who run automated tests on uploaded CPAN distributions.
241  
242 L<http://www.cpantesters.org/distro/X/XML-GrammarBase>
243  
244 =item *
245  
246 CPAN Testers Matrix
247  
248 The CPAN Testers Matrix is a website that provides a visual overview of the test results for a distribution on various Perls/platforms.
249  
250 L<http://matrix.cpantesters.org/?dist=XML-GrammarBase>
251  
252 =item *
253  
254 CPAN Testers Dependencies
255  
256 The CPAN Testers Dependencies is a website that shows a chart of the test results of all dependencies for a distribution.
257  
258 L<http://deps.cpantesters.org/?module=XML::GrammarBase>
259  
260 =back
261  
262 =head2 Bugs / Feature Requests
263  
264 Please report any bugs or feature requests by email to C<bug-xml-grammarbase at rt.cpan.org>, or through
265 the web interface at L<https://rt.cpan.org/Public/Bug/Report.html?Queue=XML-GrammarBase>. You will be automatically notified of any
266 progress on the request by the system.
267  
268 =head2 Source Code
269  
270 The code is open to the world, and available for you to hack on. Please feel free to browse it and play
271 with it, or whatever. If you want to contribute patches, please send me a diff or prod me to pull
272 from your repository :)
273  
274 L<https://github.com/shlomif/perl-XML-GrammarBase>
275  
276 git clone git://github.com/shlomif/perl-XML-GrammarBase.git
277  
278 =head1 AUTHOR
279  
280 Shlomi Fish <shlomif@cpan.org>
281  
282 =head1 BUGS
283  
284 Please report any bugs or feature requests on the bugtracker website
285 L<https://github.com/shlomif/perl-XML-GrammarBase/issues>
286  
287 When submitting a bug or request, please include a test-file or a
288 patch to an existing test-file that illustrates the bug or desired
289 feature.
290  
291 =head1 COPYRIGHT AND LICENSE
292  
293 This software is Copyright (c) 2018 by Shlomi Fish.
294  
295 This is free software, licensed under:
296  
297 The MIT (X11) License
298  
299 =cut
« blib/lib/XML/GrammarBase/Role/DataDir.pm ↑ Summary blib/lib/XML/GrammarBase/Role/XSLT.pm »