| 1 | package XML::GrammarBase::Role::XSLT::Global; | ||
| 2 | $XML::GrammarBase::Role::XSLT::Global::VERSION = '0.2.6'; | ||
| 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.6 | ||
| 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 VERSION | ||
| 170 | |||
| 171 | version 0.2.6 | ||
| 172 | |||
| 173 | =head1 METHODS | ||
| 174 | |||
| 175 | =head2 $self->perform_xslt_translation(...) | ||
| 176 | |||
| 177 | See L<XML::GrammarBase::Role::XSLT> . | ||
| 178 | |||
| 179 | =head1 AUTHOR | ||
| 180 | |||
| 181 | Shlomi Fish, C<< <shlomif at cpan.org> >> | ||
| 182 | |||
| 183 | =head1 BUGS | ||
| 184 | |||
| 185 | Please report any bugs or feature requests to C<bug-xml-grammarbase at rt.cpan.org>, or through | ||
| 186 | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=XML-GrammarBase>. I will be notified, and then you'll | ||
| 187 | automatically be notified of progress on your bug as I make changes. | ||
| 188 | |||
| 189 | =head1 SUPPORT | ||
| 190 | |||
| 191 | You can find documentation for this module with the perldoc command. | ||
| 192 | |||
| 193 | perldoc XML::GrammarBase | ||
| 194 | |||
| 195 | You can also look for information at: | ||
| 196 | |||
| 197 | =over 4 | ||
| 198 | |||
| 199 | =item * RT: CPAN's request tracker | ||
| 200 | |||
| 201 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=XML-GrammarBase> | ||
| 202 | |||
| 203 | =item * AnnoCPAN: Annotated CPAN documentation | ||
| 204 | |||
| 205 | L<http://annocpan.org/dist/XML-GrammarBase> | ||
| 206 | |||
| 207 | =item * CPAN Ratings | ||
| 208 | |||
| 209 | L<http://cpanratings.perl.org/d/XML-GrammarBase> | ||
| 210 | |||
| 211 | =item * Search CPAN | ||
| 212 | |||
| 213 | L<http://search.cpan.org/dist/XML-GrammarBase/> | ||
| 214 | |||
| 215 | =back | ||
| 216 | |||
| 217 | =head1 ACKNOWLEDGEMENTS | ||
| 218 | |||
| 219 | =head1 COPYRIGHT & LICENSE | ||
| 220 | |||
| 221 | Copyright 2009 Shlomi Fish. | ||
| 222 | |||
| 223 | This program is distributed under the MIT (X11) License: | ||
| 224 | L<http://www.opensource.org/licenses/mit-license.php> | ||
| 225 | |||
| 226 | Permission is hereby granted, free of charge, to any person | ||
| 227 | obtaining a copy of this software and associated documentation | ||
| 228 | files (the "Software"), to deal in the Software without | ||
| 229 | restriction, including without limitation the rights to use, | ||
| 230 | copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| 231 | copies of the Software, and to permit persons to whom the | ||
| 232 | Software is furnished to do so, subject to the following | ||
| 233 | conditions: | ||
| 234 | |||
| 235 | The above copyright notice and this permission notice shall be | ||
| 236 | included in all copies or substantial portions of the Software. | ||
| 237 | |||
| 238 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
| 239 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES | ||
| 240 | OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
| 241 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT | ||
| 242 | HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, | ||
| 243 | WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
| 244 | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR | ||
| 245 | OTHER DEALINGS IN THE SOFTWARE. | ||
| 246 | |||
| 247 | =head1 AUTHOR | ||
| 248 | |||
| 249 | Shlomi Fish <shlomif@cpan.org> | ||
| 250 | |||
| 251 | =head1 COPYRIGHT AND LICENSE | ||
| 252 | |||
| 253 | This software is Copyright (c) 2018 by Shlomi Fish. | ||
| 254 | |||
| 255 | This is free software, licensed under: | ||
| 256 | |||
| 257 | The MIT (X11) License | ||
| 258 | |||
| 259 | =head1 BUGS | ||
| 260 | |||
| 261 | Please report any bugs or feature requests on the bugtracker website | ||
| 262 | L<https://github.com/shlomif/perl-XML-GrammarBase/issues> | ||
| 263 | |||
| 264 | When submitting a bug or request, please include a test-file or a | ||
| 265 | patch to an existing test-file that illustrates the bug or desired | ||
| 266 | feature. | ||
| 267 | |||
| 268 | =for :stopwords cpan testmatrix url annocpan anno bugtracker rt cpants kwalitee diff irc mailto metadata placeholders metacpan | ||
| 269 | |||
| 270 | =head1 SUPPORT | ||
| 271 | |||
| 272 | =head2 Perldoc | ||
| 273 | |||
| 274 | You can find documentation for this module with the perldoc command. | ||
| 275 | |||
| 276 | perldoc XML::GrammarBase::Role::XSLT::Global | ||
| 277 | |||
| 278 | =head2 Websites | ||
| 279 | |||
| 280 | The following websites have more information about this module, and may be of help to you. As always, | ||
| 281 | in addition to those websites please use your favorite search engine to discover more resources. | ||
| 282 | |||
| 283 | =over 4 | ||
| 284 | |||
| 285 | =item * | ||
| 286 | |||
| 287 | MetaCPAN | ||
| 288 | |||
| 289 | A modern, open-source CPAN search engine, useful to view POD in HTML format. | ||
| 290 | |||
| 291 | L<https://metacpan.org/release/XML-GrammarBase> | ||
| 292 | |||
| 293 | =item * | ||
| 294 | |||
| 295 | Search CPAN | ||
| 296 | |||
| 297 | The default CPAN search engine, useful to view POD in HTML format. | ||
| 298 | |||
| 299 | L<http://search.cpan.org/dist/XML-GrammarBase> | ||
| 300 | |||
| 301 | =item * | ||
| 302 | |||
| 303 | RT: CPAN's Bug Tracker | ||
| 304 | |||
| 305 | The RT ( Request Tracker ) website is the default bug/issue tracking system for CPAN. | ||
| 306 | |||
| 307 | L<https://rt.cpan.org/Public/Dist/Display.html?Name=XML-GrammarBase> | ||
| 308 | |||
| 309 | =item * | ||
| 310 | |||
| 311 | AnnoCPAN | ||
| 312 | |||
| 313 | The AnnoCPAN is a website that allows community annotations of Perl module documentation. | ||
| 314 | |||
| 315 | L<http://annocpan.org/dist/XML-GrammarBase> | ||
| 316 | |||
| 317 | =item * | ||
| 318 | |||
| 319 | CPAN Ratings | ||
| 320 | |||
| 321 | The CPAN Ratings is a website that allows community ratings and reviews of Perl modules. | ||
| 322 | |||
| 323 | L<http://cpanratings.perl.org/d/XML-GrammarBase> | ||
| 324 | |||
| 325 | =item * | ||
| 326 | |||
| 327 | CPANTS | ||
| 328 | |||
| 329 | The CPANTS is a website that analyzes the Kwalitee ( code metrics ) of a distribution. | ||
| 330 | |||
| 331 | L<http://cpants.cpanauthors.org/dist/XML-GrammarBase> | ||
| 332 | |||
| 333 | =item * | ||
| 334 | |||
| 335 | CPAN Testers | ||
| 336 | |||
| 337 | The CPAN Testers is a network of smoke testers who run automated tests on uploaded CPAN distributions. | ||
| 338 | |||
| 339 | L<http://www.cpantesters.org/distro/X/XML-GrammarBase> | ||
| 340 | |||
| 341 | =item * | ||
| 342 | |||
| 343 | CPAN Testers Matrix | ||
| 344 | |||
| 345 | The CPAN Testers Matrix is a website that provides a visual overview of the test results for a distribution on various Perls/platforms. | ||
| 346 | |||
| 347 | L<http://matrix.cpantesters.org/?dist=XML-GrammarBase> | ||
| 348 | |||
| 349 | =item * | ||
| 350 | |||
| 351 | CPAN Testers Dependencies | ||
| 352 | |||
| 353 | The CPAN Testers Dependencies is a website that shows a chart of the test results of all dependencies for a distribution. | ||
| 354 | |||
| 355 | L<http://deps.cpantesters.org/?module=XML::GrammarBase> | ||
| 356 | |||
| 357 | =back | ||
| 358 | |||
| 359 | =head2 Bugs / Feature Requests | ||
| 360 | |||
| 361 | Please report any bugs or feature requests by email to C<bug-xml-grammarbase at rt.cpan.org>, or through | ||
| 362 | the web interface at L<https://rt.cpan.org/Public/Bug/Report.html?Queue=XML-GrammarBase>. You will be automatically notified of any | ||
| 363 | progress on the request by the system. | ||
| 364 | |||
| 365 | =head2 Source Code | ||
| 366 | |||
| 367 | The code is open to the world, and available for you to hack on. Please feel free to browse it and play | ||
| 368 | with it, or whatever. If you want to contribute patches, please send me a diff or prod me to pull | ||
| 369 | from your repository :) | ||
| 370 | |||
| 371 | L<https://github.com/shlomif/perl-XML-GrammarBase> | ||
| 372 | |||
| 373 | git clone git://github.com/shlomif/perl-XML-GrammarBase.git | ||
| 374 | |||
| 375 | =cut | ||