File Coverage

lib/Business/Shipping/UPS_XML/Parser.pm
Criterion Covered Total %
statement 6 86 6.9
branch 0 12 0.0
condition 0 3 0.0
subroutine 2 13 15.3
pod 0 11 0.0
total 8 125 6.4


line stmt bran cond sub pod time code
1             #!/usr/bin/perl
2             ################################################################################
3             #
4             # Script Name : UPS_XML.pm
5             # Version : 1
6             # Company : Down Home Web Design, Inc
7             # Author : Duane Hinkley ( duane@dhwd.com )
8             # Website : www.DownHomeWebDesign.com
9             #
10             # Description: A custom self contained module to calculate UPS rates using the
11             # newer XML method. This module properly calulates rates between
12             # and within other non-US countries including Canada.
13             #
14             # Copyright (c) 2003-2004 Down Home Web Design, Inc. All rights reserved.
15             #
16             # $Header: /home/cvs/interchange_upsxml/lib/Business/Shipping/UPS_XML/Parser.pm,v 1.1 2004/06/27 13:53:20 dlhinkley Exp $
17             #
18             # $Log: Parser.pm,v $
19             # Revision 1.1 2004/06/27 13:53:20 dlhinkley
20             # Rename module to UPS_XML
21             #
22             # Revision 1.3 2004/06/10 02:03:16 dlhinkley
23             # Fixed bugs from breaking up code and putting in CPAN format
24             #
25             # Revision 1.2 2004/06/01 02:48:25 dlhinkley
26             # Changes to make work
27             #
28             # Revision 1.5 2004/05/21 21:28:36 dlhinkley
29             # Add Currency
30             #
31             # Revision 1.4 2004/04/20 01:28:00 dlhinkley
32             # Added option for dimensions
33             #
34             # Revision 1.3 2004/03/14 18:50:31 dlhinkley
35             # Working version
36             #
37             #
38             ################################################################################
39              
40             package Business::Shipping::UPS_XML::Parser;
41 2     2   10 use strict;
  2         4  
  2         92  
42              
43 2     2   10 use vars qw($VERSION);
  2         3  
  2         2870  
44              
45             $VERSION = "0.07";
46              
47             sub new {
48 0     0 0   my $type = shift;
49 0           my ($xml,$level) = @_;
50 0           my $self = {};
51             # print "Start Level $level\n";
52 0           $level++;
53 0           $self->{'_name'} = undef;
54 0           $self->{'_contents'} = undef;
55 0           $self->{'_xml'} = $xml;
56 0           $self->{'_level'} = $level;
57 0           $self->{'_true'} = 1;
58 0           $self->{'_false'} = 0;
59              
60 0           bless $self, $type;
61             }
62              
63              
64             sub xml {
65              
66 0     0 0   my $self = shift;
67              
68 0           my ($xml)= @_;
69              
70 0 0         if ($xml ne "") {
71              
72 0           $self->{'_xml'} = $xml;
73             }
74 0           return $self->{'_xml'};
75             }
76             sub name {
77              
78 0     0 0   my $self = shift;
79              
80 0           my ($v)= @_;
81              
82 0 0         if ($v ne "") {
83              
84 0           $self->{'_name'} = $v;
85             }
86 0           return $self->{'_name'};
87             }
88             sub contents {
89              
90 0     0 0   my $self = shift;
91              
92 0           my ($v)= @_;
93              
94 0 0         if ($v ne "") {
95              
96 0           $self->{'_contents'} = $v;
97             }
98 0           return $self->{'_contents'};
99             }
100             sub have_children {
101              
102 0     0 0   my $self = shift;
103 0           my $children;
104 0           my $contents = $self->{'_contents'};
105              
106 0 0 0       if ($contents && $contents =~ /<.*>/sm) {
107              
108 0           $children = $self->{'_true'};
109             }
110             else {
111              
112 0           $children = $self->{'_false'};
113             }
114 0           return $children;
115             }
116             sub have_more_xml {
117              
118 0     0 0   my $self = shift;
119 0           my $children;
120 0           my $xml = $self->{'_xml'};
121 0           my $more;
122              
123 0 0         if ($xml =~ /<.*>/sm) {
124              
125 0           $more = $self->{'_true'};
126             }
127             else {
128              
129 0           $more = $self->{'_false'};
130             }
131 0           return $more;
132             }
133             sub remove_xml_type {
134              
135 0     0 0   my $self = shift;
136              
137 0           my $xml = $self->{'_xml'};
138              
139 0           $xml =~ s/<\?xml version="[0-9]\.[0-9]"\?>//;
140              
141 0           $self->{'_xml'} = $xml;
142             }
143             sub parse {
144              
145 0     0 0   my $self = shift;
146 0           my $contents;
147             my $name;
148 0           my $no_children;
149            
150 0           $self->remove_xml_type();
151              
152 0           while ( $self->have_more_xml() ) {
153              
154 0           $self->set_name();
155             # print "Set Name " . $self->{'_name'} . "\n";
156 0           $self->set_contents();
157             # print "Set Contents " . $self->{'_contents'} . "\n";
158             # print "xml " . $self->{'_xml'} . "\n";
159              
160 0           $no_children = 0;
161              
162 0           while ( $self->have_children() ) {
163              
164 0           $self->spawn_new_object();
165              
166 0           $no_children++;
167             }
168             # If there was no children assign the contents to a variable
169             #
170 0 0         if ( $no_children == 0 ) {
171             # print "child\n";
172              
173 0           $contents = $self->{'_contents'};
174 0           $name = $self->{'_name'};
175 0           $self->{$name} = $contents;
176             }
177 0           $self->{'_name'} = undef;
178 0           $self->{'_contents'} = undef;
179             }
180             # print "End Level " . $self->{'_level'} . "\n";
181             }
182             sub spawn_new_object() {
183              
184 0     0 0   my $self = shift;
185              
186 0           my $contents = $self->{'_contents'};
187 0           my $name = $self->{'_name'};
188 0           $self->{'_name'} = undef;
189 0           $self->{'_contents'} = undef;
190              
191 0           my $x = new Business::Shipping::UPS_XML::Parser($contents,$self->{'_level'});
192 0           $x->parse();
193 0           $self->{$name} = $x;
194             }
195             sub set_name {
196              
197 0     0 0   my $self = shift;
198              
199 0           my $xml = $self->{'_xml'};
200              
201 0           $xml =~ /<([A-Za-z]*)>/sm;
202              
203 0           $self->{'_name'} = $1;
204             }
205             sub set_contents {
206              
207 0     0 0   my $self = shift;
208 0           my $xml = $self->{'_xml'};
209              
210 0           my $name = $self->{'_name'};
211              
212             # Save the contents of the tag
213             #
214             #$xml =~ s/<$name>(.*)<\/$name>{1}//s;
215              
216             # Matches the contents of the named tag and removes the tag set from xml
217             #
218 0           $xml =~ s/<$name>( ( ?: (?!<\/$name>).)*)<\/$name>//sx;
219 0           $self->{'_contents'} = $1;
220 0           $self->{'_xml'} = $xml;
221             }
222              
223             #########################################################################################33
224             # End of class
225              
226             1;
227              
228             __END__