File Coverage

lib/XML/XMetaL.pm
Criterion Covered Total %
statement 15 62 24.1
branch 0 18 0.0
condition 0 25 0.0
subroutine 5 12 41.6
pod 2 3 66.6
total 22 120 18.3


line stmt bran cond sub pod time code
1             package XML::XMetaL;
2            
3 1     1   38384 use strict;
  1         3  
  1         36  
4 1     1   7 use warnings;
  1         1  
  1         32  
5            
6 1     1   5 use Carp;
  1         2  
  1         101  
7 1     1   954 use Hash::Util qw(lock_keys);
  1         3230  
  1         9  
8            
9            
10             our $VERSION = '0.52';
11            
12             our $singleton;
13             our $application;
14             our %custom_handlers;
15            
16             sub new {
17 0     0 1   my ($class, %args) = @_;
18 0           my $self;
19 0           eval {
20 0           lock_keys(%args,qw(-application));
21 0 0         if ($singleton) {
22 0           $self = $singleton;
23             } else {
24 0   0       $application = $args{-application} ||
25             croak "-application argument missing or undefined";
26 0   0       $self = bless {}, ref($class) || $class;
27 0           lock_keys(%$self, keys %$self);
28 0           $singleton = $self;
29             }
30             };
31 0 0         croak $@ if $@;
32 0           return $self;
33             }
34            
35             sub _get_application {
36 0   0 0     return $application ||
37             croak "Application object does not exist";
38             }
39            
40             sub get_handler {
41 0     0 0   my ($self, $key) = @_;
42 0   0       return $custom_handlers{$key} || croak "Handler ".($key || "UNDEF")." does not exist";
43             }
44            
45            
46             sub add_handler {
47 0     0 1   my ($self, %args) = @_;
48 0           my $handler_exists_exception = "Handler is registered already";
49 0           eval {
50 0           lock_keys(%args, qw(-system_identifier -handler));
51 0           my $dtd_name = $self->_base_path($args{-system_identifier});
52 0 0         die $handler_exists_exception if exists $custom_handlers{$dtd_name};
53 0           $custom_handlers{$args{-system_identifier}} = $args{-handler};
54             };
55 0 0         if ($@ eq $handler_exists_exception) {
    0          
56 0           return;
57             } elsif ($@) {
58 0           croak $@;
59             }
60             }
61            
62             sub _base_path {
63 0     0     my ($self, $system_identifier) = @_;
64 0           my ($base_name) = $system_identifier =~ /([^\\\/]+)$/;
65 0           return $base_name;
66             }
67            
68             # ======================================================================
69             # Dispatcher
70             #
71             # The AUTOLOAD method generates dispatch methods automatically,
72             # and installs them in the symbol table
73             # ======================================================================
74            
75             our $AUTOLOAD;
76             sub AUTOLOAD {
77 0     0     my ($self, @args) = @_;
78 0           my ($class, $method) = $AUTOLOAD =~ /^(.*)::(.*)$/;
79 0 0         return if $method eq 'DESTROY';
80 1     1   821 no strict 'refs';
  1         3  
  1         330  
81 0           *{$AUTOLOAD} = sub {
82 0     0     my ($self, @args) = @_;
83 0           my $application;
84             my @return_values;
85 0           eval {
86 0   0       $application = $self->_get_application() ||
87             die "Dispatch method $method called, but there is no application object";
88 0   0       my $active_document = $application->{ActiveDocument} ||
89             die "Dispatch method $method called, but there is no active document";
90 0   0       my $doctype = $active_document->{doctype} ||
91             die "The active document had no doctype";
92 0   0       my $system_identifier = $doctype->{systemId} ||
93             die "The doctype declaration of the active document had no system identifier";
94 0   0       my $handler = $self->get_handler($system_identifier) ||
95             die "Handler for system identifier $system_identifier could not be found";
96 0           @return_values = $handler->$method(@args);
97             };
98 0 0 0       if ($@ && $application) {
    0          
99 0           $application->Alert("$@");
100             } elsif ($@) {
101 0           croak $@;
102             }
103 0 0         return wantarray ? @return_values : $return_values[0];
104 0           };
105 0           $self->$method(@args);
106             }
107            
108            
109            
110            
111             1;
112             __END__