Skip to content

Commit

Permalink
Start throwing exceptions.
Browse files Browse the repository at this point in the history
  • Loading branch information
dom committed Oct 21, 2005
1 parent 367e0c3 commit d95d8de
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
20 changes: 20 additions & 0 deletions lib/XML/Filter/Normalize.pm
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,18 @@ use warnings;
use strict;

use XML::NamespaceSupport;
use XML::SAX::Exception;

our $VERSION = '0.01';

use base qw( XML::SAX::Base );

#---------------------------------------------------------------------
# Create a new exception class.
#---------------------------------------------------------------------

@XML::Filter::Normalize::Exception::ISA = qw( XML::SAX::Exception );

#---------------------------------------------------------------------
# SAX Handlers
#---------------------------------------------------------------------
Expand Down Expand Up @@ -75,6 +82,10 @@ sub correct_element_data {
my ( $uri, $prefix, $lname, $name ) =
$self->extract_name_tuple( $nsup, $data );

if ( !$uri && !$lname ) {
$self->whinge('No NamespaceURI or LocalName found');
}

$data->{ NamespaceURI } = $uri;
$data->{ Prefix } = $prefix;
$data->{ LocalName } = $lname;
Expand Down Expand Up @@ -140,6 +151,13 @@ sub extract_name_tuple {
return $uri, $prefix, $lname, $name;
}

sub whinge {
my $self = shift;
my ( $msg ) = @_;

XML::Filter::Normalize::Exception->throw( Message => $msg );
}

1;
__END__
Expand Down Expand Up @@ -190,6 +208,8 @@ These are standard SAX event handlers.
=item nsup()
=item whinge()
These are private methods and should not be called directly.
=back
Expand Down
19 changes: 19 additions & 0 deletions t/xml-filter-normalize.t
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,15 @@ my @test_data = (
);
test_correct_element_data( $_ ) foreach @test_data;

my @exceptions_data = (
{
desc => 'no input data at all',
in => {},
expected => 'No NamespaceURI or LocalName found',
},
);
test_bad_element_data( $_ ) foreach @exceptions_data;

# Now that all looks ok, try some real SAX work.
test_sax_handler();

Expand All @@ -338,6 +347,16 @@ sub test_correct_element_data {
is_deeply( $out, $t->{ expected }, "correct_element() $t->{ desc }" );
}

sub test_bad_element_data {
my ( $t ) = @_;
my $norm = XML::Filter::Normalize->new();
my $nsup = XML::NamespaceSupport->new();

eval { $norm->correct_element_data( $nsup, $t->{ in } ) };
isa_ok( $@, 'XML::SAX::Exception' );
is( "$@", "$t->{expected}\n", "test_bad_element_data: $t->{desc}" );
}

sub test_sax_handler {

my $record = Recorder->new();
Expand Down

0 comments on commit d95d8de

Please sign in to comment.