Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Start throwing exceptions.

  • Loading branch information...
commit d95d8ded1f485715277a32163e77c7d86474fa93 1 parent 367e0c3
dom authored
Showing with 39 additions and 0 deletions.
  1. +20 −0 lib/XML/Filter/Normalize.pm
  2. +19 −0 t/xml-filter-normalize.t
20 lib/XML/Filter/Normalize.pm
View
@@ -6,12 +6,19 @@ 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
#---------------------------------------------------------------------
@@ -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;
@@ -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__
@@ -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
19 t/xml-filter-normalize.t
View
@@ -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();
@@ -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();
Please sign in to comment.
Something went wrong with that request. Please try again.