Commits

Shlomi Fish  committed cd93315

Add the RNG role.

  • Participants
  • Parent commits 5d3701e

Comments (0)

Files changed (5)

File XML-GrammarBase/MANIFEST

 Build.PL
 Changes
+extradata/fiction-xml.rng
 inc/Test/Run/Builder.pm
 lib/XML/GrammarBase.pm
 lib/XML/GrammarBase/Role/RelaxNG.pm
 README
 t/00-load.t
 t/data/screenplay-xml/xml/with-tags-inside-paragraphs.xml
+t/data/fiction-xml-test.xml
 t/pod-coverage.t
 t/pod.t
+t/rng.t
 META.json

File XML-GrammarBase/extradata/fiction-xml.rng

+<?xml version="1.0" encoding="UTF-8"?>
+<!-- $Id: testSuite.rng,v 1.12 2003/01/29 12:24:07 jjc Exp $ -->
+<grammar xmlns="http://relaxng.org/ns/structure/1.0"
+    xmlns:fic="http://web-cpan.berlios.de/modules/XML-Grammar-Fortune/fiction-xml-0.2/"
+    xmlns:xlink="http://www.w3.org/1999/xlink"
+    datatypeLibrary="http://www.w3.org/2001/XMLSchema-datatypes"
+    >
+    <start>
+        <element name="fic:document">
+            <ref name="common_attrs" />
+            <attribute name="version">
+                <text />
+            </attribute>
+            <element name="fic:head">
+                <empty/>
+            </element>
+            <element name="fic:body">
+                <ref name="common_attrs" />
+                <ref name="title" />
+                <oneOrMore>
+                    <ref name="section" />
+                </oneOrMore>
+            </element>
+        </element>
+    </start>
+    <define name="common_attrs">
+        <optional>
+            <attribute name="xml:id">
+                <data type="ID"/>
+            </attribute>
+        </optional>
+        <optional>
+            <attribute name="xml:lang">
+                <data type="NCName" />
+            </attribute>
+        </optional>
+        <optional>
+            <attribute name="xlink:href">
+                <data type="anyURI"/>
+            </attribute>
+        </optional>
+    </define>
+    <define name="section">
+        <element name="fic:section">
+            <ref name="common_attrs" />
+            <ref name="title" />
+            <ref name="several_paras" />
+            <zeroOrMore>
+                <ref name="section" />
+            </zeroOrMore>
+        </element>
+    </define>
+    <define name="several_paras">
+        <zeroOrMore>
+            <choice>
+                <ref name="blockquote"/>
+                <ref name="para"/>
+                <ref name="ol"/>
+                <ref name="ul"/>
+                <ref name="programlisting"/>
+            </choice>
+        </zeroOrMore>
+    </define>
+    <define name="inside_list">
+        <oneOrMore>
+            <ref name="li" />
+        </oneOrMore>
+    </define>
+    <define name="ol">
+        <element name="fic:ol">
+            <ref name="inside_list" />
+        </element>
+    </define>
+    <define name="ul">
+        <element name="fic:ul">
+            <ref name="inside_list" />
+        </element>
+    </define>
+    <define name="blockquote">
+        <element name="fic:blockquote">
+            <ref name="common_attrs" />
+            <ref name="several_paras"/>
+        </element>
+    </define>
+    <define name="li">
+        <element name="fic:li">
+            <oneOrMore>
+                <ref name="para" />
+            </oneOrMore>
+        </element>
+    </define>
+    <define name="para">
+        <element name="fic:p">
+            <zeroOrMore>
+                <choice>
+                    <ref name="text_with_style" />
+                    <ref name="span" />
+                </choice>
+            </zeroOrMore>
+        </element>
+    </define>
+    <define name="text_with_style">
+        <choice>
+            <text />
+            <ref name="b" />
+            <ref name="i" />
+        </choice>
+    </define>
+    <define name="span">
+        <element name="fic:span">
+            <ref name="common_attrs" />
+            <oneOrMore>
+                <ref name="text_with_style" />
+            </oneOrMore>
+        </element>
+    </define>
+    <define name="b">
+        <element name="fic:b">
+            <text />
+        </element>
+    </define>
+    <define name="i">
+        <element name="fic:i">
+            <text />
+        </element>
+    </define>
+    <define name="title">
+        <element name="fic:title">
+            <text />
+        </element>
+    </define>
+    <define name="programlisting">
+        <element name="fic:programlisting">
+            <text />
+        </element>
+    </define>
+</grammar>
+

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

 
 our $VERSION = '0.0.1';
 
-has '_module_base' => (isa => 'Str', is => 'rw');
+has 'module_base' => (isa => 'Str', is => 'rw');
 has 'data_dir' => (isa => 'Str', is => 'rw');
 has 'rng_schema_basename' => (isa => 'Str', is => 'rw');
 has '_rng' => (isa => 'XML::LibXML::RelaxNG', is => 'rw');
 {
     my ($self) = @_;
 
-    my $data_dir = $self->_data_dir_from_input() ||
-        dist_dir( $self->_module_base() );
+    my $data_dir = $self->data_dir() ||
+        dist_dir( $self->module_base() );
 
     $self->data_dir($data_dir);
 
     $self->_rng($rngschema);
 }
 
+sub _undefize
+{
+    my $v = shift;
+
+    return defined($v) ? $v : "(undef)";
+}
+
 sub rng_validate_dom
 {
     my ($self, $source_dom) = @_;
 
 =head1 SLOTS
 
-=head2 _module_base
+=head2 module_base
 
 The basename of the module - used for dist dir.
 

File XML-GrammarBase/t/data/fiction-xml-test.xml

+<?xml version='1.0' encoding='utf-8'?>
+<document xmlns="http://web-cpan.berlios.de/modules/XML-Grammar-Fortune/fiction-xml-0.2/" xmlns:xlink="http://www.w3.org/1999/xlink" version="0.2">
+    <head>
+    </head>
+    <body xml:id="index">
+    <title>David vs. Goliath - Part I</title>
+    <section xml:id="top">
+    <title>The Top Section</title>
+    <!-- David has Green hair here -->
+    <p>
+    King <span xlink:href="http://en.wikipedia.org/wiki/David">David</span> and Goliath were standing by each other.
+    </p>
+    <p>
+    David said unto Goliath: "I will shoot you. I <b>swear</b> I will"
+    </p>
+    <section xml:id="goliath">
+    <title>Goliath's Response</title>
+    <!-- Goliath has to reply to that. -->
+    <p>
+    Goliath was not amused.
+    </p>
+    <p>
+    He said to David: "Oh, really. <i>David</i>, the red-headed!".
+    </p>
+    <p>
+    David started listing Goliath's disadvantages:
+    </p>
+    <blockquote xml:lang="fr-FR" xml:id="my_blockquote">
+    <ol>
+    <li>
+    <p>
+    You're slow.
+    </p>
+    </li>
+    <li>
+    <p>
+    You're big.
+    </p>
+    </li>
+    <li>
+    <p>
+    You're stupid.
+    </p>
+    </li>
+    </ol>
+    <p>
+    Goliath was insulted and told David he should use bullets for the
+    points, instead:
+    </p>
+    <ul>
+    <li>
+    <p>
+    David.
+    </p>
+    </li>
+    <li>
+    <p>
+    Saul.
+    </p>
+    </li>
+    <li>
+    <p>
+    Jonathan.
+    </p>
+    </li>
+    <li>
+    <p>
+    Michal.
+    </p>
+    </li>
+    <li>
+    <p>
+    Meyrav.
+    </p>
+    </li>
+    </ul>
+    </blockquote>
+    </section>
+    </section>
+</body>
+</document>

File XML-GrammarBase/t/rng.t

+#!/usr/bin/perl
+
+use strict;
+use warnings;
+
+use Test::More tests => 1;
+
+package MyGrammar::RNG;
+
+use Any::Moose;
+
+use File::Spec;
+
+with ('XML::GrammarBase::Role::RelaxNG');
+
+has '+module_base' => (default => 'XML::GrammarBase');
+has '+data_dir' => (default => File::Spec->catdir(File::Spec->curdir(), "extradata"));
+has '+rng_schema_basename' => (default => 'fiction-xml.rng');
+
+package main;
+
+{
+    my $rng = MyGrammar::RNG->new();
+
+    my $xml_parser = XML::LibXML->new();
+    $xml_parser->validation(0);
+
+    my $dom = $xml_parser->parse_file(
+        File::Spec->catfile(
+            File::Spec->curdir(), "t", "data", "fiction-xml-test.xml"
+        )
+    );
+    eval {
+        $rng->rng_validate_dom($dom);
+    };
+
+    # TEST
+    is ($@, '', 'No exception was thrown.');
+}
+