1. Shlomi Fish
  2. perl-XML-GrammarBase

Commits

Shlomi Fish  committed c220eb7

Add a test for output => {file => ..}.

  • Participants
  • Parent commits 4e0bbfc
  • Branches default

Comments (0)

Files changed (2)

File XML-GrammarBase/lib/XML/GrammarBase/Role/XSLT.pm

View file
  • Ignore whitespace
 use XML::LibXML;
 use XML::LibXSLT;
 
+use autodie;
+
 our $VERSION = '0.0.1';
 
 with ('XML::GrammarBase::Role::RelaxNG');
 
     my $stylesheet = $self->_stylesheet();
 
-    my $results = $stylesheet->transform($source_dom);
 
     my $medium = $args->{output};
 
-    if ($medium eq "string")
+    my $is_string = ($medium eq 'string');
+    my $is_dom = ($medium eq 'dom');
+
+    if ($is_string or $is_dom)
     {
-        return $stylesheet->output_string($results);
+        my $results = $stylesheet->transform($source_dom);
+
+        return
+            $is_dom
+            ? $results
+            : $stylesheet->output_string($results)
+            ;
     }
-    elsif ($medium eq "dom")
+    elsif (ref($medium) eq 'HASH')
     {
-        return $results;
+        if (exists($medium->{'file'}))
+        {
+            open my $out, '>', $medium->{'file'};
+            $self->perform_xslt_translation(
+                {
+                    %$args,
+                    output => {fh => $out,},
+                }
+            );
+            close ($out);
+            return;
+        }
+        if (exists($medium->{'fh'}))
+        {
+            print {$medium->{'fh'}}
+            $self->perform_xslt_translation(
+                {
+                    %$args,
+                    output => "string",
+                }
+            );
+            return;
+        }
     }
-    else
-    {
-        confess "Unknown medium";
-    }
+
+    confess "Unknown medium";
 }
 
 =head1 SYNOPSIS
 
 =item * my $final_dom = $converter->perform_xslt_translation({source => {dom => $libxml_dom}, output => "dom" })
 
+=item * my $final_dom = $converter->perform_xslt_translation({source => {dom => $libxml_dom}, output => {file => $path_to_file,}, })
+
+=item * my $final_dom = $converter->perform_xslt_translation({source => {dom => $libxml_dom}, output => {fh => $filehandle,}, })
+
 =back
 
 Does the actual conversion. The C<'source'> argument points to a hash-ref with
 
 The C<'output'> key specifies the return value. A value of C<'string'> returns
 the XML as a string, and a value of C<'dom'> returns the XML as an
-L<XML::LibXML> DOM object.
+L<XML::LibXML> DOM object. If it is a hash ref then it specifies a
+C<'file'> or a C<'fh'> with a filepath or filehandle respectively.
 
 =cut
 

File XML-GrammarBase/t/xslt.t

View file
  • Ignore whitespace
 use strict;
 use warnings;
 
-use Test::More tests => 3;
+use Test::More tests => 4;
 
 package MyGrammar::XSLT;
 
 
 use Test::XML::Ordered qw(is_xml_ordered);
 
+use File::Temp qw(tempfile);
 
 sub _utf8_slurp
 {
             "'$input_fn' generated good output on source/string_ref - output - dom"
         );
     }
+
+    {
+        my ($fh, $filename) = tempfile();
+
+        $xslt->perform_xslt_translation(
+            {
+                source => {string_ref => \(_utf8_slurp($input_fn)) },
+                output => {file => $filename, },
+            }
+        );
+
+        my $xml_source = _utf8_slurp($output_fn);
+        my $final_source = _utf8_slurp($filename);
+
+        # TEST:$c++;
+        is_xml_ordered(
+            [ string => $final_source, ],
+            [ string => $xml_source, ],
+            "'$input_fn' generated good output on source/string_ref - output/file"
+        );
+    }
     return;
 }