blib/lib/XML/Grammar/FictionBase/XSLT/Converter.pm

filter: Statementstmt 90.4% 19 / 21 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 7 / 7 Podpod n/a totaltotal 92.8% 26 / 28 CC 1 SCAR 0.4
CC
1
Cov
90%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« blib/lib/XML/Grammar/Screenplay/App/ToHTML.pm ↑ Summary bin/screenplay-text--list-images »
1 package XML::Grammar::FictionBase::XSLT::Converter;
2 $XML::Grammar::FictionBase::XSLT::Converter::VERSION = '0.22.0';
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 1 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use File::Spec ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 ▶use File::ShareDir ':ALL';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 ▶use XML::LibXML ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use XML::LibXSLT ();
Subroutine BEGIN: called CC=1 SCAR=2.6
13  
14 use MooX 'late';
15  
16 has '_data_dir' => (
17 isa => 'Str',
18 is => 'rw',
19 lazy => 1,
20 default => sub {
21 return shift->_calc_data_dir();
22 },
23 );
24  
25 has '_data_dir_from_input' => (
26 isa => 'Str',
27 is => 'rw',
28 init_arg => 'data_dir',
29 );
30  
31 has '_rng' => (
32 isa => 'XML::LibXML::RelaxNG',
33 is => 'rw',
34 lazy => 1,
35 default => sub {
36 return shift->_calc_rng();
37 },
38 );
39  
40 sub _calc_rng
41 {
42 my $self = shift;
43  
44 return XML::LibXML::RelaxNG->new(
45 location => File::Spec->catfile(
46 $self->_data_dir(), $self->rng_schema_basename(),
47 ),
48 );
49 }
50  
51 has '_xml_parser' => (
52 isa => 'XML::LibXML',
53 is => 'rw',
54 lazy => 1,
55 default => sub {
56 return shift->_calc_xml_parser();
57 },
58 );
59  
60 sub _calc_xml_parser
61 {
62 my $self = shift;
63  
64 return XML::LibXML->new();
65 }
66  
67 has '_xslt_processor' => (
68 isa => "XML::LibXSLT",
69 is => 'rw',
70 lazy => 1,
71 default => sub {
72 return shift->_calc_xslt_processor();
73 },
74 );
75  
76 sub _calc_xslt_processor
77 {
78 my ($self) = @_;
79  
80 return XML::LibXSLT->new();
81 }
82  
83 has '_stylesheet' => (
84 isa => "XML::LibXSLT::StylesheetWrapper",
85 is => 'rw',
86 lazy => 1,
87 default => sub {
88 return shift->_calc_stylesheet();
89 },
90 );
91  
92 sub _calc_stylesheet
93 {
94 my ($self) = @_;
95  
96 my $style_doc = $self->_xml_parser()->parse_file(
97 File::Spec->catfile(
98 $self->_data_dir(), $self->xslt_transform_basename(),
99 ),
100 );
101  
102 return $self->_xslt_processor->parse_stylesheet($style_doc);
103 }
104  
105 has 'rng_schema_basename' => ( is => 'ro', isa => 'Str', required => 1, );
106 has 'xslt_transform_basename' => ( is => 'ro', isa => 'Str', required => 1, );
107  
108  
109  
110 sub _calc_data_dir
111 {
112 my ($self) = @_;
113  
114 return $self->_data_dir_from_input() || dist_dir('XML-Grammar-Fiction');
115 }
116  
117  
118 sub _undefize
119 {
120 my $v = shift;
121  
122 return defined($v) ? $v : "(undef)";
123 }
124  
125 sub _calc_and_ret_dom_without_validate
126 {
127 my $self = shift;
128 my $args = shift;
129  
130 my $source = $args->{source};
131  
132 return
133 exists( $source->{'dom'} ) ? $source->{'dom'}
134 : exists( $source->{'string_ref'} )
135 ? $self->_xml_parser()->parse_string( ${ $source->{'string_ref'} } )
136 : $self->_xml_parser()->parse_file( $source->{'file'} );
137 }
138  
139 sub _get_dom_from_source
140 {
141 my $self = shift;
142 my $args = shift;
143  
144 my $source_dom = $self->_calc_and_ret_dom_without_validate($args);
145  
146 my $ret_code;
147  
148 eval { $ret_code = $self->_rng()->validate($source_dom); };
149  
150 if ( defined($ret_code) && ( $ret_code == 0 ) )
151 {
152 # It's OK.
153 }
154 else
155 {
156 confess "RelaxNG validation failed [\$ret_code == "
157 . _undefize($ret_code)
158 . " ; $@]";
159 }
160  
161 return $source_dom;
162 }
163  
164 sub perform_translation
165 {
166 my ( $self, $args ) = @_;
167  
168 my $source_dom = $self->_get_dom_from_source($args);
169  
170 my $stylesheet = $self->_stylesheet();
171  
172 my $results = $stylesheet->transform($source_dom);
173  
174 my $medium = $args->{output};
175  
176 if ( $medium eq "string" )
177 {
178 return $stylesheet->output_string($results);
179 }
180 elsif ( $medium eq "dom" )
181 {
182 return $results;
183 }
184 else
185 {
186 confess "Unknown medium";
187 }
188 }
189  
190 1;
191  
192 __END__
193  
194 =pod
195  
196 =encoding UTF-8
197  
198 =head1 NAME
199  
200 XML::Grammar::FictionBase::XSLT::Converter - base module that converts an XML
201 file to a different XML file using an XSLT transform.
202  
203 =head1 VERSION
204  
205 version 0.22.0
206  
207 =head1 METHODS
208  
209 =head2 new()
210  
211 Accepts no arguments so far. May take some time as the grammar is compiled
212 at that point.
213  
214 =head2 meta()
215  
216 Internal - (to settle pod-coverage.).
217  
218 =head2 rng_schema_basename()
219  
220 Inherited - (to settle pod-coverage.).
221  
222 =head2 xslt_transform_basename()
223  
224 Inherited - (to settle pod-coverage.).
225  
226 =head2 $converter->perform_translation
227  
228 =over 4
229  
230 =item * my $final_source = $converter->perform_translation({source => {file => $filename}, output => "string" })
231  
232 =item * my $final_source = $converter->perform_translation({source => {string_ref => \$buffer}, output => "string" })
233  
234 =item * my $final_dom = $converter->perform_translation({source => {file => $filename}, output => "dom" })
235  
236 =item * my $final_dom = $converter->perform_translation({source => {dom => $libxml_dom}, output => "dom" })
237  
238 =back
239  
240 Does the actual conversion. The C<'source'> argument points to a hash-ref with
241 keys and values for the source. If C<'file'> is specified there it points to the
242 filename to translate (currently the only available source). If
243 C<'string_ref'> is specified it points to a reference to a string, with the
244 contents of the source XML. If C<'dom'> is specified then it points to an XML
245 DOM as parsed or constructed by XML::LibXML.
246  
247 The C<'output'> key specifies the return value. A value of C<'string'> returns
248 the XML as a string, and a value of C<'dom'> returns the XML as an
249 L<XML::LibXML> DOM object.
250  
251 =for :stopwords cpan testmatrix url bugtracker rt cpants kwalitee diff irc mailto metadata placeholders metacpan
252  
253 =head1 SUPPORT
254  
255 =head2 Websites
256  
257 The following websites have more information about this module, and may be of help to you. As always,
258 in addition to those websites please use your favorite search engine to discover more resources.
259  
260 =over 4
261  
262 =item *
263  
264 MetaCPAN
265  
266 A modern, open-source CPAN search engine, useful to view POD in HTML format.
267  
268 L<https://metacpan.org/release/XML-Grammar-Fiction>
269  
270 =item *
271  
272 RT: CPAN's Bug Tracker
273  
274 The RT ( Request Tracker ) website is the default bug/issue tracking system for CPAN.
275  
276 L<https://rt.cpan.org/Public/Dist/Display.html?Name=XML-Grammar-Fiction>
277  
278 =item *
279  
280 CPANTS
281  
282 The CPANTS is a website that analyzes the Kwalitee ( code metrics ) of a distribution.
283  
284 L<http://cpants.cpanauthors.org/dist/XML-Grammar-Fiction>
285  
286 =item *
287  
288 CPAN Testers
289  
290 The CPAN Testers is a network of smoke testers who run automated tests on uploaded CPAN distributions.
291  
292 L<http://www.cpantesters.org/distro/X/XML-Grammar-Fiction>
293  
294 =item *
295  
296 CPAN Testers Matrix
297  
298 The CPAN Testers Matrix is a website that provides a visual overview of the test results for a distribution on various Perls/platforms.
299  
300 L<http://matrix.cpantesters.org/?dist=XML-Grammar-Fiction>
301  
302 =item *
303  
304 CPAN Testers Dependencies
305  
306 The CPAN Testers Dependencies is a website that shows a chart of the test results of all dependencies for a distribution.
307  
308 L<http://deps.cpantesters.org/?module=XML::Grammar::Fiction>
309  
310 =back
311  
312 =head2 Bugs / Feature Requests
313  
314 Please report any bugs or feature requests by email to C<bug-xml-grammar-fiction at rt.cpan.org>, or through
315 the web interface at L<https://rt.cpan.org/Public/Bug/Report.html?Queue=XML-Grammar-Fiction>. You will be automatically notified of any
316 progress on the request by the system.
317  
318 =head2 Source Code
319  
320 The code is open to the world, and available for you to hack on. Please feel free to browse it and play
321 with it, or whatever. If you want to contribute patches, please send me a diff or prod me to pull
322 from your repository :)
323  
324 L<https://github.com/shlomif/fiction-xml>
325  
326 git clone git://github.com/shlomif/fiction-xml.git
327  
328 =head1 AUTHOR
329  
330 Shlomi Fish <shlomif@cpan.org>
331  
332 =head1 BUGS
333  
334 Please report any bugs or feature requests on the bugtracker website
335 L<https://github.com/shlomif/fiction-xml/issues>
336  
337 When submitting a bug or request, please include a test-file or a
338 patch to an existing test-file that illustrates the bug or desired
339 feature.
340  
341 =head1 COPYRIGHT AND LICENSE
342  
343 This software is Copyright (c) 2007 by Shlomi Fish.
344  
345 This is free software, licensed under:
346  
347 The MIT (X11) License
348  
349 =cut
« blib/lib/XML/Grammar/Screenplay/App/ToHTML.pm ↑ Summary bin/screenplay-text--list-images »