blib/lib/XML/GrammarBase/Role/XSLT/Global.pm

filter: Statementstmt 86.6% 13 / 15 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 5 / 5 Podpod n/a totaltotal 90.0% 18 / 20 CC 1 SCAR 0.6
CC
1
Cov
87%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« blib/lib/XML/GrammarBase/Role/XSLT.pm ↑ Summary blib/lib/XML/GrammarBase.pm »
1 package XML::GrammarBase::Role::XSLT::Global;
2 $XML::GrammarBase::Role::XSLT::Global::VERSION = '0.2.8';
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6  
7 1 ▶use MooX::Role 'late';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 ▶use XML::LibXML '2.0017';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use XML::LibXSLT '1.80';
Subroutine BEGIN: called CC=1 SCAR=2.6
11  
12 use autodie;
13  
14 with('XML::GrammarBase::Role::RelaxNG');
15  
16 has '_xml_parser' => (
17 isa => "XML::LibXML",
18 is => 'rw',
19 default => sub { return XML::LibXML->new; },
20 lazy => 1,
21 );
22  
23 has '_xslt_parser' => (
24 isa => "XML::LibXSLT",
25 is => 'rw',
26 default => sub { return XML::LibXSLT->new; },
27 lazy => 1,
28 );
29  
30 sub _calc_stylesheet
31 {
32 my ( $self, $output_format ) = @_;
33  
34 my $style_doc =
35 $self->_xml_parser()
36 ->parse_file(
37 $self->dist_path_slot("to_${output_format}_xslt_transform_basename"),
38 );
39  
40 return $self->_xslt_parser->parse_stylesheet($style_doc);
41 }
42  
43 sub _calc_and_ret_dom_without_validate
44 {
45 my $self = shift;
46 my $args = shift;
47  
48 my $source = $args->{source};
49  
50 return
51 exists( $source->{'dom'} ) ? $source->{'dom'}
52 : exists( $source->{'string_ref'} )
53 ? $self->_xml_parser()->parse_string( ${ $source->{'string_ref'} } )
54 : $self->_xml_parser()->parse_file( $source->{'file'} );
55 }
56  
57 sub _get_dom_from_source
58 {
59 my $self = shift;
60 my $args = shift;
61  
62 my $source_dom = $self->_calc_and_ret_dom_without_validate($args);
63  
64 my $ret_code;
65  
66 eval { $ret_code = $self->_rng()->validate($source_dom); };
67  
68 if ( defined($ret_code) && ( $ret_code == 0 ) )
69 {
70 # It's OK.
71 }
72 else
73 {
74 confess "RelaxNG validation failed [\$ret_code == "
75 . $self->_undefize($ret_code)
76 . " ; $@]";
77 }
78  
79 return $source_dom;
80 }
81  
82 sub perform_xslt_translation
83 {
84 my ( $self, $args ) = @_;
85  
86 my $output_format = $args->{output_format};
87 my $encoding = ( $args->{encoding} || 'utf8' );
88  
89 my $source_dom = $self->_get_dom_from_source($args);
90  
91 my $stylesheet_method = "_to_${output_format}_stylesheet";
92 my $stylesheet = $self->$stylesheet_method();
93  
94 my $medium = $args->{output};
95  
96 my $is_string = ( $medium eq 'string' );
97 my $is_dom = ( $medium eq 'dom' );
98  
99 my $xslt_params = $args->{xslt_params} || {};
100  
101 if ( $is_string or $is_dom )
102 {
103 my $results = $stylesheet->transform( $source_dom, %$xslt_params );
104  
105 return
106 $is_dom ? $results
107 : ( $encoding eq 'bytes' ) ? $stylesheet->output_as_bytes($results)
108 : $stylesheet->output_as_chars($results);
109 }
110 elsif ( ref($medium) eq 'HASH' )
111 {
112 if ( exists( $medium->{'file'} ) )
113 {
114 open my $out, '>', $medium->{'file'};
115 $self->perform_xslt_translation(
116 {
117 %$args,
118 output => { fh => $out, },
119 encoding => 'bytes',
120 }
121 );
122 close($out);
123 return;
124 }
125 if ( exists( $medium->{'fh'} ) )
126 {
127 print { $medium->{'fh'} } $self->perform_xslt_translation(
128 {
129 %$args, output => "string",
130 }
131 );
132 return;
133 }
134 }
135  
136 confess "Unknown medium";
137 }
138  
139  
140 1; # End of XML::GrammarBase::RelaxNG::Validate
141  
142 __END__
143  
144 =pod
145  
146 =encoding UTF-8
147  
148 =head1 NAME
149  
150 XML::GrammarBase::Role::XSLT::Global - a base, non-parameterised, role for an XSLT converter.
151  
152 =head1 VERSION
153  
154 version 0.2.8
155  
156 =head1 SYNOPSIS
157  
158 package XML::Grammar::MyGrammar::RelaxNG::Validate;
159  
160 use Moo;
161  
162 with ('XML::GrammarBase::Role::XSLT::Global');
163  
164 =head1 DESCRIPTION
165  
166 This is a utility global (non-variant) role used by
167 L<XML::GrammarBase::Role::XSLT> . For internal use.
168  
169 =head1 METHODS
170  
171 =head2 $self->perform_xslt_translation(...)
172  
173 See L<XML::GrammarBase::Role::XSLT> .
174  
175 =head1 AUTHOR
176  
177 Shlomi Fish, C<< <shlomif at cpan.org> >>
178  
179 =head1 ACKNOWLEDGEMENTS
180  
181 =head1 COPYRIGHT & LICENSE
182  
183 Copyright 2009 Shlomi Fish.
184  
185 This program is distributed under the MIT (X11) License:
186 L<http://www.opensource.org/licenses/mit-license.php>
187  
188 Permission is hereby granted, free of charge, to any person
189 obtaining a copy of this software and associated documentation
190 files (the "Software"), to deal in the Software without
191 restriction, including without limitation the rights to use,
192 copy, modify, merge, publish, distribute, sublicense, and/or sell
193 copies of the Software, and to permit persons to whom the
194 Software is furnished to do so, subject to the following
195 conditions:
196  
197 The above copyright notice and this permission notice shall be
198 included in all copies or substantial portions of the Software.
199  
200 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
201 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
202 OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
203 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
204 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
205 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
206 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
207 OTHER DEALINGS IN THE SOFTWARE.
208  
209 =for :stopwords cpan testmatrix url bugtracker rt cpants kwalitee diff irc mailto metadata placeholders metacpan
210  
211 =head1 SUPPORT
212  
213 =head2 Websites
214  
215 The following websites have more information about this module, and may be of help to you. As always,
216 in addition to those websites please use your favorite search engine to discover more resources.
217  
218 =over 4
219  
220 =item *
221  
222 MetaCPAN
223  
224 A modern, open-source CPAN search engine, useful to view POD in HTML format.
225  
226 L<https://metacpan.org/release/XML-GrammarBase>
227  
228 =item *
229  
230 RT: CPAN's Bug Tracker
231  
232 The RT ( Request Tracker ) website is the default bug/issue tracking system for CPAN.
233  
234 L<https://rt.cpan.org/Public/Dist/Display.html?Name=XML-GrammarBase>
235  
236 =item *
237  
238 CPANTS
239  
240 The CPANTS is a website that analyzes the Kwalitee ( code metrics ) of a distribution.
241  
242 L<http://cpants.cpanauthors.org/dist/XML-GrammarBase>
243  
244 =item *
245  
246 CPAN Testers
247  
248 The CPAN Testers is a network of smoke testers who run automated tests on uploaded CPAN distributions.
249  
250 L<http://www.cpantesters.org/distro/X/XML-GrammarBase>
251  
252 =item *
253  
254 CPAN Testers Matrix
255  
256 The CPAN Testers Matrix is a website that provides a visual overview of the test results for a distribution on various Perls/platforms.
257  
258 L<http://matrix.cpantesters.org/?dist=XML-GrammarBase>
259  
260 =item *
261  
262 CPAN Testers Dependencies
263  
264 The CPAN Testers Dependencies is a website that shows a chart of the test results of all dependencies for a distribution.
265  
266 L<http://deps.cpantesters.org/?module=XML::GrammarBase>
267  
268 =back
269  
270 =head2 Bugs / Feature Requests
271  
272 Please report any bugs or feature requests by email to C<bug-xml-grammarbase at rt.cpan.org>, or through
273 the web interface at L<https://rt.cpan.org/Public/Bug/Report.html?Queue=XML-GrammarBase>. You will be automatically notified of any
274 progress on the request by the system.
275  
276 =head2 Source Code
277  
278 The code is open to the world, and available for you to hack on. Please feel free to browse it and play
279 with it, or whatever. If you want to contribute patches, please send me a diff or prod me to pull
280 from your repository :)
281  
282 L<https://github.com/shlomif/perl-XML-GrammarBase>
283  
284 git clone git://github.com/shlomif/perl-XML-GrammarBase.git
285  
286 =head1 AUTHOR
287  
288 Shlomi Fish <shlomif@cpan.org>
289  
290 =head1 BUGS
291  
292 Please report any bugs or feature requests on the bugtracker website
293 L<https://github.com/shlomif/perl-XML-GrammarBase/issues>
294  
295 When submitting a bug or request, please include a test-file or a
296 patch to an existing test-file that illustrates the bug or desired
297 feature.
298  
299 =head1 COPYRIGHT AND LICENSE
300  
301 This software is Copyright (c) 2018 by Shlomi Fish.
302  
303 This is free software, licensed under:
304  
305 The MIT (X11) License
306  
307 =cut
« blib/lib/XML/GrammarBase/Role/XSLT.pm ↑ Summary blib/lib/XML/GrammarBase.pm »