File Coverage

blib/lib/XML/Grammar/Fortune.pm
Criterion Covered Total %
statement 16 18 88.8
branch n/a
condition n/a
subroutine 6 6 100.0
pod n/a
total 22 24 91.6


line stmt bran cond sub pod time code
1             package XML::Grammar::Fortune;
2              
3 1     1   24826 use warnings;
  1         3  
  1         36  
4 1     1   5 use strict;
  1         2  
  1         37  
5              
6 1     1   1320 use Fatal (qw(open));
  1         25210  
  1         8  
7              
8 1     1   898 use File::Spec;
  1         2  
  1         34  
9              
10 1     1   1143 use MooX qw/late/;
  1         16797  
  1         8  
11              
12 1     1   48866 use XML::GrammarBase::Role::RelaxNG v0.2.2;
  0            
  0            
13             use XML::GrammarBase::Role::XSLT v0.2.2;
14              
15             with ('XML::GrammarBase::Role::RelaxNG');
16             with XSLT(output_format => 'html');
17              
18             has '+module_base' => (default => 'XML-Grammar-Fortune');
19             has '+rng_schema_basename' => (default => 'fortune-xml.rng');
20              
21              
22             has '+to_html_xslt_transform_basename' =>
23             (default => 'fortune-xml-to-html.xslt');
24              
25             has '_mode' => (is => 'rw', init_arg => 'mode');
26             has '_output_mode' => (is => 'rw', init_arg => 'output_mode',);
27              
28             =head1 NAME
29              
30             XML::Grammar::Fortune - convert the FortunesXML grammar to other formats and from plaintext.
31              
32             =head1 VERSION
33              
34             Version 0.0512
35              
36             =cut
37              
38             our $VERSION = '0.0512';
39              
40              
41             =head1 SYNOPSIS
42              
43             use XML::Grammar::Fortune;
44              
45             # Validate files.
46              
47             my $validator =
48             XML::Grammar::Fortune->new(
49             {
50             mode => "validate"
51             }
52             );
53              
54             # Returns 0 upon success - dies otherwise
55             exit($validator->run({input => "my-fortune-file.xml"}));
56              
57             # Convert files to XHTML.
58              
59             my $converter =
60             XML::Grammar::Fortune->new(
61             {
62             mode => "convert_to_html",
63             output_mode => "filename"
64             }
65             );
66              
67             $converter->run(
68             {
69             input => "my-fortune-file.xml",
70             output => "resultant-file.xhtml",
71             }
72             );
73              
74             =head1 FUNCTIONS
75              
76             =head2 my $processor = XML::Grammar::Fortune->new({mode => $mode, input => $in, output => $out, output_mode => "string",});
77              
78             Creates a new processor with mode $mode, output_mode "string", and input and output files.
79              
80             =head2 $self->run({ %args})
81              
82             Runs the processor. If $mode is "validate", validates the document.
83              
84             %args may contain:
85              
86             =over 4
87              
88             =item * xslt_params
89              
90             Parameters for the XSLT stylesheet.
91              
92             =item * input
93              
94             Input source - depends on input_mode.
95              
96             =item * output
97              
98             Output destination - depends on output mode.
99              
100             =back
101              
102             =cut
103              
104             sub run
105             {
106             my $self = shift;
107             my $args = shift;
108              
109             my $xslt_params = $args->{'xslt_params'} || {};
110              
111             my $output = $args->{'output'};
112             my $input = $args->{'input'};
113              
114             my $mode = $self->_mode();
115              
116             if ($mode eq "validate")
117             {
118             return $self->rng_validate_file($input);
119             }
120             elsif ($mode eq "convert_to_html")
121             {
122             my $translate = sub {
123             my ($medium, $encoding) = @_;
124              
125             return $self->perform_xslt_translation(
126             {
127             output_format => 'html',
128             source => {file => $input},
129             output => $medium,
130             encoding => $encoding,
131             xslt_params => $xslt_params,
132             }
133             );
134             };
135             if ($self->_output_mode() eq "string")
136             {
137             $$output .= $translate->('string', 'bytes');
138             }
139             else
140             {
141             $translate->({file => $output}, 'utf8');
142             }
143             }
144              
145             return;
146             }
147              
148             1; # End of XML::Grammar::Fortune
149              
150             __END__