Commits

Anonymous committed 52806a5

Committed some changes . Trunk is broken.

Comments (0)

Files changed (6)

Text-Qantor/MANIFEST

 Build.PL
 Changes
 extradata/qantor-xml.rng
+extradata/qantor-xml-to-xsl-fo.xslt
 inc/Test/Run/Builder.pm
 inc/Text/Qantor/Builder.pm
 inc/XML/Grammar/Builder.pm
 lib/Text/Qantor/Parser/Elem/Para.pm
 lib/Text/Qantor/Parser.pm
 lib/Text/Qantor.pm
+lib/Text/Qantor/XML/Base.pm
+lib/Text/Qantor/XML/ToXSL_FO.pm
 Makefile.PL
 MANIFEST
 META.yml

Text-Qantor/extradata/qantor-xml-to-xsl-fo.xslt

+<xsl:stylesheet version = '1.0'
+     xmlns:xsl='http://www.w3.org/1999/XSL/Transform'
+     xmlns:qant="http://web-cpan.berlios.de/Qantor/qantor-xml/"
+     xmlns:xlink="http://www.w3.org/1999/xlink"
+     xmlns:fo="http://www.w3.org/1999/XSL/Format"
+     >
+
+<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"
+ />
+
+<xsl:template match="/">
+        <xsl:apply-templates select="//body" />  
+</xsl:template>
+
+<xsl:template match="body">
+    <fo:root>
+        <fo:layout-master-set>
+            <fo:simple-page-master master-name="A4">
+                <fo:region-body />
+            </fo:simple-page-master>
+        </fo:layout-master-set>
+        <fo:page-sequence master-reference="A4">
+            <fo:flow flow-name="xsl-region-body">
+                <xsl:apply-templates select="p" />  
+            </fo:flow>
+        </fo:page-sequence>
+    </fo:root>
+</xsl:template>
+
+<xsl:template match="p">
+    <fo:block>
+        <xsl:apply-templates />
+    </fo:block>
+</xsl:template>
+
+<xsl:template match="b">
+    <fo:inline font-weight="bold">
+        <xsl:apply-templates />
+    </fo:inline>
+</xsl:template>
+
+</xsl:stylesheet>

Text-Qantor/lib/Text/Qantor/XML/Base.pm

+package Text::Qantor::XML::Base;
+
+use strict;
+use warnings;
+
+=head1 NAME
+
+Text::Qantor::XML::Base - base class for Text::Qantor::XML
+
+=head1 METHODS
+
+=head2 $package->new({%args});
+
+Constructs a new package
+
+=cut
+
+sub new
+{
+    my $class = shift;
+    my $self = {};
+
+    bless $self, $class;
+
+    $self->_init(@_);
+
+    return $self;
+}
+
+1;
+

Text-Qantor/lib/Text/Qantor/XML/ToXSL_FO.pm

+package Text::Qantor::XML::ToXSL_FO;
+
+use strict;
+use warnings;
+
+use Carp;
+use File::Spec;
+
+use XML::LibXSLT;
+
+use Text::Qantor::ConfigData;
+
+use XML::LibXML;
+use XML::LibXSLT;
+
+use Moose;
+
+extends("Text::Qantor::XML::Base");
+
+has '_data_dir' => (isa => 'Str', is => 'rw');
+has '_rng' => (isa => 'XML::LibXML::RelaxNG', is => 'rw');
+has '_xml_parser' => (isa => "XML::LibXML", is => 'rw');
+has '_stylesheet' => (isa => "XML::LibXSLT::StylesheetWrapper", is => 'rw');
+
+=head1 NAME
+
+Text::Qantor::XML::ToXSL_FO - module that converts the QantorXML to XSL-FO.
+
+=head1 VERSION
+
+Version 0.0.1
+
+=cut
+
+our $VERSION = '0.0.1';
+
+=head2 new()
+
+Accepts no arguments so far. May take some time as the grammar is compiled
+at that point.
+
+=head2 meta()
+
+Internal - (to settle pod-coverage.).
+
+=cut
+
+sub _init
+{
+    my ($self, $args) = @_;
+
+    my $data_dir = $args->{'data_dir'} ||
+        Text::Qantor::ConfigData->config('extradata_install_path')->[0];
+
+    $self->_data_dir($data_dir);
+
+    my $rngschema =
+        XML::LibXML::RelaxNG->new(
+            location =>
+            File::Spec->catfile(
+                $self->_data_dir(), 
+                "qantor-xml.rng"
+            ),
+        );
+
+    $self->_rng($rngschema);
+
+    $self->_xml_parser(XML::LibXML->new());
+
+    my $xslt = XML::LibXSLT->new();
+
+    my $style_doc = $self->_xml_parser()->parse_file(
+            File::Spec->catfile(
+                $self->_data_dir(), 
+                "qantor-xml-to-xsl-fo.xslt"
+            ),
+        );
+
+    $self->_stylesheet($xslt->parse_stylesheet($style_doc));
+
+    return 0;
+}
+
+=head2 $converter->translate_to_xsl_fo({source => {file => $filename}, output_type => "string" })
+
+Does the actual conversion. $filename is the filename to translate (currently
+the only available source). 
+
+The C<'output_type'> key specifies the return value. A value of C<'string'> returns 
+the XML as a string, and a value of C<'xml'> returns the XML as an 
+L<XML::LibXML> DOM object.
+
+=cut
+
+sub translate_to_xsl_fo
+{
+    my ($self, $args) = @_;
+
+    my $source_dom =
+        $self->_xml_parser()->parse_file($args->{source}->{file})
+        ;
+
+    my $ret_code;
+
+    eval
+    {
+        $ret_code = $self->_rng()->validate($source_dom);
+    };
+
+    if (defined($ret_code) && ($ret_code == 0))
+    {
+        # It's OK.
+    }
+    else
+    {
+        confess "RelaxNG validation failed [\$ret_code == $ret_code ; $@]";
+    }
+
+    my $stylesheet = $self->_stylesheet();
+
+    my $results = $stylesheet->transform($source_dom);
+
+    my $medium = $args->{output_type};
+
+    if ($medium eq "string")
+    {
+        return $stylesheet->output_string($results);
+    }
+    elsif ($medium eq "xml")
+    {
+        return $results;
+    }
+    else
+    {
+        confess "Unknown medium";
+    }
+}
+
+=head1 AUTHOR
+
+Shlomi Fish, L<http://www.shlomifish.org/>.
+
+=head1 BUGS
+
+Please report any bugs or feature requests to
+C<bug-xml-grammar-screenplay at rt.cpan.org>, or through the web interface at
+L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=XML-Grammar-Screenplay>.
+I will be notified, and then you'll automatically be notified of progress on
+your bug as I make changes.
+
+
+=head1 ACKNOWLEDGEMENTS
+
+=head1 COPYRIGHT & LICENSE
+
+Copyright 2007 Shlomi Fish, all rights reserved.
+
+This program is released under the following license: MIT X11.
+
+=cut
+
+1;
+

Text-Qantor/t/02-convert-to-qantor-xml.t

 use strict;
 use warnings;
 
-use Test::More tests => 6;
+use Test::More tests => 13;
 
 use File::Spec;
 
 use IO::String;
 
 use Text::Qantor;
+use Text::Qantor::XML::ToXSL_FO;
 
 # TEST:$num_files=3
 my @files =
     open my $input_file_fh, "<", $input_file;
 
     my $got_file = "t/data/to-xsl-fo/output-qantor-xml/$base.xml";
-    open my $got_output_fh, ">", $got_file;
+    open my $got_output_fh, ">", $got_file
+        or die "Could not open got_out_fh - $!";
 
     $qantor->convert_input_to_xml(
         {
         diag(explain($error_code));
         ;
     }
+
+    # Now let's convert the XML to XSL-FO
+    {
+        my $fo_converter = Text::Qantor::XML::ToXSL_FO->new(
+            {
+                data_dir => File::Spec->catdir(File::Spec->curdir(), "extradata"),
+            }
+        );
+
+        # TEST
+        ok ($fo_converter, "Initialised converter");
+
+        my $string;
+        eval {
+            $string = $fo_converter->translate_to_xsl_fo(
+                {
+                    source => { file => $got_file },
+                    output_type => "string",
+                }
+            );
+        };
+
+        my $err = $@;
+
+        # TEST*$num_files
+        is ($err, "", "No exception was thrown");
+
+        my $got_fo_file = "t/data/to-xsl-fo/output-xsl-fo/$base.fo";
+        {
+            open my $got_fo_fh, ">:encoding(utf-8)", $got_fo_file
+                or die "could not open";
+            print {$got_fo_fh} $got_fo_file;
+            close($got_fo_fh);
+        }
+
+        my $expected_fo_file = "t/data/to-xsl-fo/xsl-fo/$base.fo";
+
+        # TEST*$num_files
+        is_xml_ordered(
+            [ location => $got_fo_file ],
+            [ location => $expected_fo_file ],
+            "'$input_file' generated good output"
+        );
+    }
 }

Text-Qantor/t/lib/Test/XML/Ordered.pm

     return;
 }
 
+sub _ns
+{
+    my $elem = shift;
+    my $ns = $elem->namespaceURI();
+
+    return defined($ns) ? $ns : "";
+}
+
 sub _compare_loop
 {
     my $self = shift;
             {
                 return $calc_prob->({param => "element_name"});
             }
+            if (_ns($self->_got) ne _ns($self->_expected))
+            {
+                return $calc_prob->({param => "mismatch_ns"});
+            }
         }
     }
     continue
             " ; " . 
             "Expected name: " . $self->_expected->name() . " at " .$self->_expected->lineNumber();
     }
+    elsif ($status_struct->{param} eq "mismatch_ns")
+    {
+        return
+            "Got Namespace: " . _ns($self->_got). " at " . $self->_got->lineNumber() . 
+            " ; " . 
+            "Expected Namespace: " . _ns($self->_expected) . " at " .$self->_expected->lineNumber();
+    }
+    
     else
     {
         die "Unknown param";