File Coverage

blib/lib/XML/Idiom.pm
Criterion Covered Total %
statement 10 12 83.3
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 14 16 87.5


line stmt bran cond sub pod time code
1             # XML::Idiom.pm
2             #
3             # Copyright (c) 2004 Joe Minieri and OpenService (www.open.com).
4             # All rights reserved.
5             # This program is free software; you can redistribute it and/or modify it under the same
6             # terms as Perl itself.
7             #
8              
9             package XML::Idiom;
10              
11 1     1   40147 use 5.006001;
  1         4  
  1         49  
12 1     1   7 use strict;
  1         2  
  1         43  
13 1     1   5 use warnings;
  1         2  
  1         34  
14              
15 1     1   672 use XML::Simple;
  0            
  0            
16              
17             our $VERSION = '0.02';
18              
19             ##########################################################################################
20             #
21             # Methods to manage the errors
22             #
23             ##########################################################################################
24             sub isError { my $self = shift; return $self->{ _isError }; }
25             sub errorType { my $self = shift; return $self->{ _errorType }; }
26             sub errorContent { my $self = shift; return $self->{ _errorContent }; }
27             sub getError { my $self = shift; return $self->{ _errorHash }; }
28              
29             ##########################################################################################
30             #
31             # Methods to manage the events and event list
32             #
33             ##########################################################################################
34             sub getNextEvent { my $self = shift; return pop @{ $self->{ _eventList }}; }
35             sub getNumberOfEvents { my $self = shift; return (scalar @{ $self->{ _eventList }}); }
36             sub getEvents { my $self = shift; return @{ $self->{ _eventList }}; }
37              
38             ##########################################################################################
39             #
40             # Methods to manage the raw and unparsed data
41             #
42             ##########################################################################################
43             my $_IDIOM_document;
44             my $_IDIOM_xml;
45             sub Idiom {
46             my ($self, $doc) = @_;
47             if(defined($doc)) {
48             return $self->{ _IDIOM_document } = $doc;
49             } else {
50             return $self->{ _IDIOM_document }
51             }
52             }
53              
54             sub XML {
55             my ($self, $doc) = @_;
56             if(defined($doc)) {
57             if(defined($self->{ _IDIOM_document } = $doc)) {
58             return $self->{ _IDIOM_xml } = XMLin($self->{ _IDIOM_document });
59             } else {
60             return undef;
61             }
62             } else {
63             return $self->{ _IDIOM_xml };
64             }
65             }
66              
67             sub consume {
68             my ($self, $idiom_doc) = @_;
69              
70             $self->{ _isError} = undef;
71             $self->{ _errorType} = undef;
72             $self->{ _errorContent} = undef;
73             @{ $self->{ _eventList }} = ();
74             $self->{ _IDIOM_document} = undef;
75             $self->{ _IDIOM_xml} = undef;
76              
77             unless(defined($idiom_doc) and $idiom_doc ne '') { return undef }
78              
79             my $idiom_ref = $self->XML($idiom_doc);
80              
81             if(my $alerts_ref = $idiom_ref->{ 'evAlert' }) {
82             if(ref $alerts_ref eq 'ARRAY') {
83             # mutliple alerts
84             foreach my $alert_hash_ref ( @$alerts_ref ) {
85             push(@{ $self->{ _eventList }}, $alert_hash_ref);
86             }
87             } elsif( ref $alerts_ref eq 'HASH' ) {
88             # one alert
89             push(@{ $self->{ _eventList }}, $alerts_ref);
90             } # else no alerts!
91             return (scalar @{ $self->{ _eventList }});
92             } elsif(my $error_ref = $idiom_ref->{ 'errorMessage' }) {
93             $self->{ _isError } = 1;
94             $self->{ _errorType } = $error_ref->{ 'name' };
95             $self->{ _errorContent } = $error_ref->{ 'content' };
96             $self->{ _errorHash } = $error_ref;
97             return undef;
98             }
99             }
100              
101             sub new {
102             my ($class, $document) = @_;
103              
104             my $self = {};
105              
106             bless( $self, $class );
107             if(defined($document)) { $self->consume($document) }
108              
109             $XML::Simple::PREFERRED_PARSER='XML::Parser';
110              
111             return $self;
112             }
113              
114             1;
115             __END__