Shlomi Fish avatar Shlomi Fish committed 05a3e28

Add what we have now. Need to consult someone why it fails.

Comments (0)

Files changed (2)

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

 
 =head1 NAME
 
-XML::GrammarBase::Role::XSLT - role for an XSLT converter.
+XML::GrammarBase::Role::XSLT - a parameterized role for an XSLT converter.
 
 =head1 VERSION
 
 
 =cut
 
-use Moo::Role;
+use Package::Variant
+    importing => ['Moo::Role'],
+    subs => [ qw(has) ];
 
 use MooX 'late';
 
 
 with ('XML::GrammarBase::Role::RelaxNG');
 
-has 'xslt_transform_basename' => (isa => 'Str', is => 'rw');
-has '_stylesheet' => (isa => "XML::LibXSLT::StylesheetWrapper", is => 'rw');
+sub make_variant
+{
+    my ($class, $target_package, %args) = @_;
+
+    my $output_format = $args{output_format};
+
+    has "to_${output_format}_xslt_transform_basename"
+        => (isa => 'Str', is => 'rw');
+
+    has "_to_${output_format}_stylesheet" =>
+    (
+        isa => "XML::LibXSLT::StylesheetWrapper",
+        is => 'rw',
+        default => sub { return shift->_calc_stylesheet($output_format), },
+        lazy => 1,
+    );
+
+    return;
+}
+
 has '_xml_parser' => (
     isa => "XML::LibXML",
     is => 'rw',
     default => sub { return XML::LibXML->new; },
     lazy => 1,
 );
+
 has '_xslt_parser' => (
     isa => "XML::LibXSLT",
     is => 'rw',
     default => sub { return XML::LibXSLT->new; },
     lazy => 1,
 );
-has '_stylesheet' => (
-    isa => "XML::LibXSLT::StylesheetWrapper",
-    is => 'rw',
-    default => sub { return shift->_calc_stylesheet(), },
-    lazy => 1,
-);
 
 sub _calc_stylesheet {
-    my $self = shift;
+    my ($self, $output_format) = @_;
 
     my $style_doc = $self->_xml_parser()->parse_file(
-        $self->dist_path_slot('xslt_transform_basename'),
+        $self->dist_path_slot("to_{$output_format}_xslt_transform_basename"),
     );
 
     return $self->_xslt_parser->parse_stylesheet($style_doc);
 {
     my ($self, $args) = @_;
 
+    my $output_format = $args->{output_format};
     my $source_dom = $self->_get_dom_from_source($args);
 
-    my $stylesheet = $self->_stylesheet();
+    my $stylesheet_method = "_to_${output_format}_stylesheet";
+    my $stylesheet = $self->$stylesheet_method();
 
 
     my $medium = $args->{output};
 
 =head2 BUILD
 
-L<Any::Moose> constructor. For internal use.
+L<Moo> constructor. For internal use.
+
+=head2 make_variant
+
+L<Package::Variant> constructor. For internal use.
 
 =head1 AUTHOR
 

XML-GrammarBase/t/xslt.t

 
 use File::Spec;
 
-with ('XML::GrammarBase::Role::XSLT');
+use XML::GrammarBase::Role::RelaxNG;
+use XML::GrammarBase::Role::XSLT;
+
+with ('XML::GrammarBase::Role::RelaxNG');
+with XSLT(output_format => 'html');
 
 has '+module_base' => (default => 'XML-GrammarBase');
 has '+data_dir' => (default => File::Spec->catdir(File::Spec->curdir(), "t", "data"));
-has '+xslt_transform_basename' => (default => 'fiction-xml-to-html.xslt');
 has '+rng_schema_basename' => (default => 'fiction-xml.rng');
 
+has '+to_html_xslt_transform_basename' => (default => 'fiction-xml-to-html.xslt');
+
 package main;
 
 use Test::XML::Ordered qw(is_xml_ordered);
     {
         my $final_source = $xslt->perform_xslt_translation(
             {
+                output_format => 'html',
                 source => {file => $input_fn, },
                 output => "string",
             }
     {
         my $final_source = $xslt->perform_xslt_translation(
             {
+                output_format => 'html',
                 source => {string_ref => \(_utf8_slurp($input_fn)) },
                 output => "string",
             }
     {
         my $final_dom = $xslt->perform_xslt_translation(
             {
+                output_format => 'html',
                 source => {string_ref => \(_utf8_slurp($input_fn)) },
                 output => "dom",
             }
 
         $xslt->perform_xslt_translation(
             {
+                output_format => 'html',
                 source => {string_ref => \(_utf8_slurp($input_fn)) },
                 output => {file => $filename, },
             }
 
         $xslt->perform_xslt_translation(
             {
+                output_format => 'html',
                 source => {string_ref => \(_utf8_slurp($input_fn)) },
                 output => {fh => $fh, },
             }
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.