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
« GrammarBase/Role/XSLT.pm ↑ Summary GrammarBase.pm »
1 package XML::GrammarBase::Role::XSLT::Global;
2 $XML::GrammarBase::Role::XSLT::Global::VERSION = '0.2.10';
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 0 ▶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 return $self->_xslt_parser->parse_stylesheet($style_doc);
40 }
41  
42 sub _calc_and_ret_dom_without_validate
43 {
44 my $self = shift;
45 my $args = shift;
46  
47 my $source = $args->{source};
48  
49 return
50 exists( $source->{'dom'} ) ? $source->{'dom'}
51 : exists( $source->{'string_ref'} )
52 ? $self->_xml_parser()->parse_string( ${ $source->{'string_ref'} } )
53 : $self->_xml_parser()->parse_file( $source->{'file'} );
54 }
55  
56 sub _get_dom_from_source
57 {
58 my $self = shift;
59 my $args = shift;
60  
61 my $source_dom = $self->_calc_and_ret_dom_without_validate($args);
62  
63 my $ret_code;
64  
65 eval { $ret_code = $self->_rng()->validate($source_dom); };
66  
67 if ( defined($ret_code) && ( $ret_code == 0 ) )
68 {
69 # It's OK.
70 }
71 else
72 {
73 confess "RelaxNG validation failed [\$ret_code == "
74 . $self->_undefize($ret_code)
75 . " ; $@]";
76 }
77  
78 return $source_dom;
79 }
80  
81 sub perform_xslt_translation
82 {
83 my ( $self, $args ) = @_;
84  
85 my $output_format = $args->{output_format};
86 my $encoding = ( $args->{encoding} || 'utf8' );
87  
88 my $source_dom = $self->_get_dom_from_source($args);
89  
90 my $stylesheet_method = "_to_${output_format}_stylesheet";
91 my $stylesheet = $self->$stylesheet_method();
92  
93 my $medium = $args->{output};
94  
95 my $is_string = ( $medium eq 'string' );
96 my $is_dom = ( $medium eq 'dom' );
97  
98 my $xslt_params = $args->{xslt_params} || {};
99  
100 if ( $is_string or $is_dom )
101 {
102 my $results = $stylesheet->transform( $source_dom, %$xslt_params );
103  
104 return
105 $is_dom ? $results
106 : ( $encoding eq 'bytes' ) ? $stylesheet->output_as_bytes($results)
107 : $stylesheet->output_as_chars($results);
108 }
109 elsif ( ref($medium) eq 'HASH' )
110 {
111 if ( exists( $medium->{'file'} ) )
112 {
113 open my $out, '>', $medium->{'file'};
114 $self->perform_xslt_translation(
115 {
116 %$args,
117 output => { fh => $out, },
118 encoding => 'bytes',
119 }
120 );
121 close($out);
122 return;
123 }
124 if ( exists( $medium->{'fh'} ) )
125 {
126 print { $medium->{'fh'} } $self->perform_xslt_translation(
127 {
128 %$args, output => "string",
129 }
130 );
131 return;
132 }
133 }
134  
135 confess "Unknown medium";
136 }
137  
138 1;
139  
140 __END__
« GrammarBase/Role/XSLT.pm ↑ Summary GrammarBase.pm »