Commits

Anonymous committed b252e4e

[Qantor] Add the RelaxNG stylesheet. Not tested for validation.

Comments (0)

Files changed (7)

Text-Qantor/MANIFEST

 Build.PL
 Changes
+extradata/qantor-xml.rng
 inc/Test/Run/Builder.pm
 inc/Text/Qantor/Builder.pm
+inc/XML/Grammar/Builder.pm
 lib/Text/Qantor/Parser/Elem/Base.pm
 lib/Text/Qantor/Parser/Elem/MacroCall.pm
 lib/Text/Qantor/Parser/Elem/ParaList.pm

Text-Qantor/extradata/qantor-xml.rng

+<?xml version="1.0"?>
+<!-- $Id: testSuite.rng,v 1.12 2003/01/29 12:24:07 jjc Exp $ -->
+<grammar xmlns="http://relaxng.org/ns/structure/1.0"
+    datatypeLibrary="http://www.w3.org/2001/XMLSchema-datatypes"
+    >
+    <start>
+        <element name="doc">
+            <element name="body">
+                <oneOrMore>
+                    <ref name="p" />
+                </oneOrMore>
+            </element>
+        </element>
+    </start>
+    <define name="p">
+        <element name="p">
+            <oneOrMore>
+                <choice>
+                    <text />
+                    <ref name="b"/>
+                </choice>
+            </oneOrMore>
+        </element>
+    </define>
+    <define name="b">
+        <element name="b">
+            <text />
+        </element>
+    </define>
+</grammar>
+

Text-Qantor/inc/Text/Qantor/Builder.pm

 use strict;
 use warnings;
 
-use base 'Test::Run::Builder';
-
-use File::Spec;
-
-sub ACTION_yapp
-{
-    my $self = shift;
-
-    my $p = $self->{'properties'};
-
-    my $output = File::Spec->catdir($p->{base_dir}, "lib", qw(Text Qantor Parser.pm));
-    my $input = "qantor_grammar.yp";
-
-    if ($self->up_to_date($input, $output))
-    {
-        return;
-    }
-
-    my @cmd = (
-        "yapp",
-         "-o", $output,
-        "-m", "Text::Qantor::Parser",
-        $input,
-    );
-
-    print join(" ", @cmd), "\n";
-    if (system(@cmd))
-    {
-        die "Yapp Failed";
-    }
-}
-
-sub ACTION_code
-{
-    my $self = shift;
-
-    $self->depends_on('yapp');
-
-    return $self->SUPER::ACTION_code();
-}
+use base 'XML::Grammar::Builder';
 
 1;
 

Text-Qantor/inc/XML/Grammar/Builder.pm

+package XML::Grammar::Builder;
+
+use strict;
+use warnings;
+
+use base 'Test::Run::Builder';
+
+use File::Find;
+
+our $VERSION = '0.0101';
+
+sub new
+{
+    my $package = shift;
+    my %args = @_;
+    my @extradata_files;
+    
+    my $module_name = $args{'module_name'};
+    $module_name =~ s{::}{-}g;
+
+    my $filter_files_cb = sub {
+        my $filename = $File::Find::name;
+        if ((-f $filename) &&
+            ($filename =~ /\.(?:mod|xslt|dtd|ent|cat|jpg|rng|xcf\.bz2)$/)
+        )
+        {
+            push @extradata_files, $filename;
+        }
+    };
+ 
+    find({ wanted => $filter_files_cb, no_chdir => 1}, "extradata");
+
+    my $builder = $package->SUPER::new(
+        extradata_files =>
+        {
+            (map { $_ => $_ } @extradata_files)
+        },
+        @_
+    );
+
+    $builder->add_build_element('extradata');
+
+    $builder->install_path()->{'extradata'} = 
+        File::Spec->catdir(
+                $builder->install_destination("lib"),
+                qw(data modules),
+                $module_name,
+                qw(data)
+        );
+
+    $builder->config_data(
+        'extradata_install_path' =>
+        [$builder->install_path()->{'extradata'}]
+    );
+
+    return $builder;
+}
+
+
+=begin excluded
+
+    my $get_dest_extradata_cb = sub {
+        my $fn = shift;
+
+        # Trying if this makes it work.
+        # TODO : Either remove this line or the rest of the lines.
+        return $fn;
+
+        $fn =~ s{^extradata}{data};
+        return "lib/$module_name/$fn";
+    };
+
+=end excluded
+
+=cut
+
+1;

Text-Qantor/lib/Text/Qantor.pm

 use Moose;
 
 use Carp;
+use File::Spec;
 
 use XML::Writer;
 use Text::Qantor::Parser;
+use Text::Qantor::ConfigData;
 
 has '_xml_out' => (is => "rw", isa => "XML::Writer");
+has '_data_dir' => (isa => 'Str', is => 'rw');
+has '_rng' => (isa => 'XML::LibXML::RelaxNG', is => 'rw');
+has '_xml_parser' => (isa => "XML::LibXML", is => 'rw');
 
 my $fo_ns = "http://www.w3.org/1999/XSL/Format";
 my $xlink_ns = "http://www.w3.org/1999/xlink";
 {
     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());
+
     return;
 }
 
     return;
 }
 
+=head2 $qantor->validate_xml({%args})
+
+Validates the xml in %args for validity.
+
+    if (my $ret = self->validate_xml({in_fh => \*STDIN}))
+    {
+        # Error handling.
+    }
+
+=cut
+
+sub validate_xml
+{
+    my $self = shift;
+    my $args = shift;
+
+    my $source_dom =
+        $self->_xml_parser()->parse_fh($args->{in_fh})
+        ;
+
+    if (!$source_dom)
+    {
+        # TODO : Convert to Exception::Class objects.
+        return { error => "Unparsed", };
+    }
+    my $ret_code;
+
+    eval
+    {
+        $ret_code = $self->_rng()->validate($source_dom);
+    };
+
+    if (defined($ret_code) && ($ret_code == 0))
+    {
+        # It's OK.
+    }
+    else
+    {
+        return { 
+            error => "RelaxNG validation failed",
+            ret_code => $ret_code,
+            excpetion => $@,
+        };
+    }
+
+    # Return false upon success.
+    return;
+}
+
+
 sub _lexer
 {
     my $self = shift;

Text-Qantor/t/01-convert-to-xsl-fo.t

 
 use Test::More tests => 3;
 
+use File::Spec;
+
 use lib "./t/lib";
 
 use Test::XML::Ordered qw(is_xml_ordered);
 
     my $expected_file = "t/data/to-xsl-fo/xsl-fo/$base.fo";
 
-    my $qantor = Text::Qantor->new();
+    my $qantor = Text::Qantor->new(
+        {
+            data_dir => File::Spec->catdir(File::Spec->curdir(), "extradata"),
+        }
+    );
 
     open my $input_file_fh, "<", $input_file;
 

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

 
 use Test::More tests => 3;
 
+use File::Spec;
+
 use lib "./t/lib";
 
 use Test::XML::Ordered qw(is_xml_ordered);
 
     my $expected_file = "t/data/to-xsl-fo/qantor-xml/$base.xml";
 
-    my $qantor = Text::Qantor->new();
+    my $qantor = Text::Qantor->new(
+        {
+            data_dir => File::Spec->catdir(File::Spec->curdir(), "extradata"),
+        }
+    );
 
     open my $input_file_fh, "<", $input_file;