Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions lib/XML/Rabbit/Sugar.pm
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,15 @@ trait is automatically set to C<String>.
=cut

sub has_xpath_value {
my ($meta, $attr_name, $xpath_query, @moose_params) = @_;
my ($meta, $attr_name, $xpath_query, %moose_params) = @_;
$meta->add_attribute($attr_name,
is => 'ro',
isa => 'Str',
traits => [qw( XPathValue String )],
xpath_query => $xpath_query,
default => '',
@moose_params,
xml_default => $moose_params{default},
%moose_params,
);
return 1;
}
Expand Down
11 changes: 10 additions & 1 deletion lib/XML/Rabbit/Trait/XPathValue.pm
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,18 @@ use warnings;

package XML::Rabbit::Trait::XPathValue;
use Moose::Role;
use Scalar::Util qw(blessed);

with 'XML::Rabbit::Trait::XPath';

# ABSTRACT: Single value xpath extractor trait

has xml_default => (
is => 'ro',
isa => 'Str',
predicate => 'has_xml_default',
);

=method _build_default

Returns a coderef that is run to build the default value of the parent attribute. Read Only.
Expand All @@ -22,7 +29,9 @@ sub _build_default {
$parent,
$self->_resolve_xpath_query( $parent ),
);
return blessed($node) ? $node->to_literal . "" : "";
return blessed($node) ? $node->to_literal
: $self->has_xml_default ? $self->xml_default
: '';
};
}

Expand Down
58 changes: 58 additions & 0 deletions t/21_xpath_default.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#!/usr/bin/perl

use strict;
use warnings;

use Test::More;
use Path::Class;
use Scalar::Util qw(blessed);

my @xmls = grep {/[.]xml$/} dir('t/data/auto')->children;

for my $xml (@xmls) {
test_xml($xml);
}

done_testing;
exit;

sub test_xml {
my ($xml_file) = @_;
my $pm_file = $xml_file;
$pm_file =~ s/[.]xml$/.pm/xms;
my $tests_file = $xml_file;
$tests_file =~ s/[.]xml$/.has/xms;

my $module = require $pm_file;

my $object = $module->new( file => "$xml_file" );
isa_ok $object, $module;

my @tests = file($tests_file)->slurp;

for my $test (@tests) {
chomp $test;
local $TODO;
if ($test =~ s/^[#]//xms) {
$TODO = "Skipping test '$test' for $xml_file";
}

my ($search, $value) = split /\s+/, $test, 2;
is eval { search($object, $search) }, $value, "\$schema->$search == $value"
or note $@;
}
}

sub search {
my ($obj, $query) = @_;

my ($next, $rest) = split /->/, $query, 2;

my $next_obj
= blessed $obj && $obj->can($next) ? $obj->$next()
: ref $obj eq 'ARRAY' ? $obj->[$next]
: ref $obj eq 'HASH' ? $obj->{$next}
: die "Can't get $query from $obj\n";

return $rest ? search($next_obj, $rest) : $next_obj;
}
1 change: 1 addition & 0 deletions t/data/auto/complex_element.has
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
element_form_default unqualified
13 changes: 13 additions & 0 deletions t/data/auto/complex_element.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package MyComplexElement;

use XML::Rabbit::Root;

add_xpath_namespace 'xsd' => 'http://www.w3.org/2001/XMLSchema';

has_xpath_value 'element_form_default' => './@elementFormDefault' => (
default => 'unqualified',
);

finalize_class;

'MyComplexElement';
17 changes: 17 additions & 0 deletions t/data/auto/complex_element.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema
targetNamespace="http://element.example.com/"
xmlns:ec="http://element.example.com/"
xmlns="http://www.w3.org/2001/XMLSchema"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">

<xsd:element name="test" type="ec:testType"/>

<xsd:complexType name="testType">
<xsd:sequence>
<xsd:element name="id" type="xsd:int" minOccurs="0" maxOccurs="1"/>
<xsd:element name="name" type="xsd:string" minOccurs="1" maxOccurs="1"/>
</xsd:sequence>
</xsd:complexType>

</xsd:schema>