lib/XML/Toolset.pm

filter: Statementstmt 11.0% 18 / 163 Branchbran 0.0% 0 / 106 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 20.0% 5 / 25 Podpod 15.0% 3 / 20 totaltotal 7.7% 26 / 334 CC 58 SCAR 37.3
CC
58
Cov
6%
validate_document
182.0
doc2dom
30.0
doc2xml
30.0
CRAP 41.6
« blib/lib/XML/Toolset/MSXML.pm ↑ Summary blib/lib/XML/Toolset/LibXML.pm »
1  
2 package XML::Toolset;
3  
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use App;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use App::Service;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use vars qw($VERSION @ISA);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 $VERSION = sprintf"%d.%03d", q$Revision: 1.25 $ =~ /: (\d+)\.(\d+)/;
10 1 @ISA = ("App::Service");
11  
12 1 ▶use XML::Toolset::Document;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 =head1 NAME
15  
16 XML::Toolset - perform XML construction, parsing, validation, and XPath operations using whatever underlying XML library is available (ALPHA!)
17  
18 =head1 SYNOPSIS
19  
20 use App;
21 use XML::Toolset;
22  
23 my $context = App->context();
24 my $toolset = $context->xml_toolset(class => "XML::Toolset::BestAvailable");
25  
26 ...
27  
28 =head1 DESCRIPTION
29  
30 The XML-Toolset distribution is a wrapper which provides a simplified XML
31 processing API which uses XPath to construct and dissect XML messages.
32  
33 The architecture of the XML-Toolset distribution allows for the user of
34 the API to access XML capabilities independent of the underlying XML
35 toolset technology: i.e. Xerces, LibXML, XML::XMLDOM, MSXML.
36  
37 =cut
38  
39 ###########################################################################
40 # abstract methods
41 ###########################################################################
42  
43 0 ▶sub get_value {
Subroutine get_value: not called CC=3 SCAR=24.8
Pod undocumented
44 0 ▶ &App::sub_entry if ($App::trace);
Branch: 1 branch
BranchTrueFalseTotal
if $App::trace 0 0 0
45 0 my ($self, $xpath) = @_;
46 0 my ($value);
47 0 die "get_value() must be implemented in a subclass";
48 0 ▶ &App::sub_exit($value) if ($App::trace);
Branch: 1 branch
BranchTrueFalseTotal
if $App::trace 0 0 0
49 0 return($value);
50 }
51  
52 0 ▶sub set_value {
Subroutine set_value: not called CC=3 SCAR=24.8
Pod undocumented
53 0 ▶ &App::sub_entry if ($App::trace);
Branch: 1 branch
BranchTrueFalseTotal
if $App::trace 0 0 0
54 0 my ($self, $xpath, $value) = @_;
55 0 die "set_value() must be implemented in a subclass";
56 0 ▶ &App::sub_exit() if ($App::trace);
Branch: 1 branch
BranchTrueFalseTotal
if $App::trace 0 0 0
57 }
58  
59 0 ▶sub get_nodes {
Subroutine get_nodes: not called CC=3 SCAR=24.8
Pod undocumented
60 0 ▶ &App::sub_entry if ($App::trace);
Branch: 1 branch
BranchTrueFalseTotal
if $App::trace 0 0 0
61 0 my ($self, $xpath) = @_;
62 0 my (@nodes);
63 0 die "set_nodes() must be implemented in a subclass";
64 0 ▶ &App::sub_exit(@nodes) if ($App::trace);
Branch: 1 branch
BranchTrueFalseTotal
if $App::trace 0 0 0
65 0 return(@nodes);
66 }
67  
68 0 ▶sub set_nodes {
Subroutine set_nodes: not called CC=3 SCAR=24.8
Pod undocumented
69 0 ▶ &App::sub_entry if ($App::trace);
Branch: 1 branch
BranchTrueFalseTotal
if $App::trace 0 0 0
70 0 my ($self, @nodes) = @_;
71 0 die "set_nodes() must be implemented in a subclass";
72 0 ▶ &App::sub_exit() if ($App::trace);
Branch: 1 branch
BranchTrueFalseTotal
if $App::trace 0 0 0
73 }
74  
75 0 ▶sub to_string {
Subroutine to_string: not called CC=3 SCAR=24.8
Pod undocumented
76 0 ▶ &App::sub_entry if ($App::trace);
Branch: 1 branch
BranchTrueFalseTotal
if $App::trace 0 0 0
77 0 my ($self) = @_;
78 0 my ($xml);
79 0 die "to_string() must be implemented in a subclass";
80 0 ▶ &App::sub_exit($xml) if ($App::trace);
Branch: 1 branch
BranchTrueFalseTotal
if $App::trace 0 0 0
81 0 return($xml);
82 }
83  
84 0 ▶sub transform {
Subroutine transform: not called CC=3 SCAR=24.8
Pod undocumented
85 0 ▶ &App::sub_entry if ($App::trace);
Branch: 1 branch
BranchTrueFalseTotal
if $App::trace 0 0 0
86 0 my ($self) = @_;
87 0 die "transform() must be implemented in a subclass";
88 0 ▶ &App::sub_exit() if ($App::trace);
Branch: 1 branch
BranchTrueFalseTotal
if $App::trace 0 0 0
89 }
90  
91 0 ▶sub version {
Subroutine version: not called CC=3 SCAR=24.8
Pod documented
92 0 ▶ &App::sub_entry if ($App::trace);
Branch: 1 branch
BranchTrueFalseTotal
if $App::trace 0 0 0
93 0 my ($self) = @_;
94 0 my ($version);
95 0 die "version() must be implemented in a subclass";
96 0 ▶ &App::sub_exit($version) if ($App::trace);
Branch: 1 branch
BranchTrueFalseTotal
if $App::trace 0 0 0
97 0 return($version);
98 }
99  
100 0 ▶sub new_parser {
Subroutine new_parser: not called CC=3 SCAR=24.8
Pod undocumented
101 0 ▶ &App::sub_entry if ($App::trace);
Branch: 1 branch
BranchTrueFalseTotal
if $App::trace 0 0 0
102 0 my ($self, $options) = @_;
103 0 my $parser = undef;
104 0 die "new_parser() must be implemented in a subclass";
105 0 ▶ &App::sub_exit($parser) if ($App::trace);
Branch: 1 branch
BranchTrueFalseTotal
if $App::trace 0 0 0
106 0 return($parser);
107 }
108  
109 0 ▶sub new_dom {
Subroutine new_dom: not called CC=3 SCAR=24.8
Pod undocumented
110 0 ▶ &App::sub_entry if ($App::trace);
Branch: 1 branch
BranchTrueFalseTotal
if $App::trace 0 0 0
111 0 my ($self, $root_tag, $xmlns) = @_;
112 0 my ($dom);
113 0 die "new_dom() must be implemented in a subclass";
114 0 ▶ &App::sub_exit($dom) if ($App::trace);
Branch: 1 branch
BranchTrueFalseTotal
if $App::trace 0 0 0
115 0 return($dom);
116 }
117  
118 0 ▶sub parse {
Subroutine parse: not called CC=3 SCAR=24.8
Pod undocumented
119 0 ▶ &App::sub_entry if ($App::trace);
Branch: 1 branch
BranchTrueFalseTotal
if $App::trace 0 0 0
120 0 my ($self, $xml, $options) = @_;
121
122 0 my $valid = 1;
123 0 die "parse() must be implemented in a subclass";
124  
125 0 ▶ &App::sub_exit($valid) if ($App::trace);
Branch: 1 branch
BranchTrueFalseTotal
if $App::trace 0 0 0
126 0 return($valid);
127 }
128  
129 ###########################################################################
130 # base class methods
131 ###########################################################################
132  
133 #<?xml version="1.0" encoding="UTF-8"?>
134 #<OTA_PingRS ... />
135  
136 0 ▶sub get_root_tag {
Subroutine get_root_tag: not called CC=4 SCAR=30.0
Pod undocumented
137 0 ▶ &App::sub_entry if ($App::trace);
Branch: 1 branch
BranchTrueFalseTotal
if $App::trace 0 0 0
138 0 my ($self, $doc) = @_;
139 0 my ($tag);
140 0 my $xml = $self->doc2xml($doc);
141 0 ▶ if ($xml =~ /^(<\?xml[^<>]*\?>)?\s*<\s*(\S+)/s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($xml =~ /^(<\?xml[^<>]*\?>)?\s*<\s*(\S+)/s) 0 0 0
142 0 $tag = $2;
143 }
144 0 ▶ &App::sub_exit($tag) if ($App::trace);
Branch: 1 branch
BranchTrueFalseTotal
if $App::trace 0 0 0
145 0 return($tag);
146 }
147  
148 0 ▶sub get_root_element_attribute {
Subroutine get_root_element_attribute: not called CC=4 SCAR=30.0
Pod undocumented
149 0 ▶ &App::sub_entry if ($App::trace);
Branch: 1 branch
BranchTrueFalseTotal
if $App::trace 0 0 0
150 0 my ($self, $xml, $attrib) = @_;
151 0 my ($root_tag, $value);
152 0 ▶ if ($xml =~ /^(<\?xml[^<>]*\?>)?\s*<\s*(\S+)[^<>]*\s+$attrib\s*=\s*['"]([^<>'"]*)['"]/s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($xml =~ /^(<\?xml[^<>]*\?>)?\s*<\s*(\S+)[^<>]*\s+$attrib\s*=\s*['"]([^<>'"]*)['"]/s) 0 0 0
153 0 $root_tag = $2;
154 0 $value = $3;
155 }
156 0 ▶ &App::sub_exit($value) if ($App::trace);
Branch: 1 branch
BranchTrueFalseTotal
if $App::trace 0 0 0
157 0 return($value);
158 }
159  
160 0 ▶sub set_root_element_attribute {
Subroutine set_root_element_attribute: not called CC=3 SCAR=24.8
Pod undocumented
161 0 ▶ &App::sub_entry if ($App::trace);
Branch: 1 branch
BranchTrueFalseTotal
if $App::trace 0 0 0
162 0 my ($self, $xmlref, $attrib, $value) = @_;
163 0 my ($root_tag);
164 0 $$xmlref =~ s/^(<\?xml[^<>]*\?>)?(\s*<\s*\S+[^<>]*\s+$attrib\s*=\s*['"])([^<>'"]*)(['"])/$1$2$value$4/s;
165 0 ▶ &App::sub_exit() if ($App::trace);
Branch: 1 branch
BranchTrueFalseTotal
if $App::trace 0 0 0
166 }
167  
168 0 ▶sub new_document {
Subroutine new_document: not called CC=3 SCAR=24.8
Pod undocumented
169 0 ▶ &App::sub_entry if ($App::trace);
Branch: 1 branch
BranchTrueFalseTotal
if $App::trace 0 0 0
170 0 my ($self, @args) = @_;
171 0 my $doc = XML::Toolset::Document->new(@args, xml_toolset => $self);
172 0 ▶ &App::sub_exit($doc) if ($App::trace);
Branch: 1 branch
BranchTrueFalseTotal
if $App::trace 0 0 0
173 0 return($doc);
174 }
175  
176 0 ▶sub type {
Subroutine type: not called CC=3 SCAR=24.8
Pod documented
177 0 ▶ &App::sub_entry if ($App::trace);
Branch: 1 branch
BranchTrueFalseTotal
if $App::trace 0 0 0
178 0 my ($self) = @_;
179 0 my $class = ref($self);
180 0 $class =~ m/XML::Toolset::(.*)/;
181 0 my $type = $1;
182 0 ▶ &App::sub_exit($type) if ($App::trace);
Branch: 1 branch
BranchTrueFalseTotal
if $App::trace 0 0 0
183 0 return($type);
184 }
185  
186 0 ▶sub validate_document {
Subroutine validate_document: not called CC=13 SCAR=52.0
Pod documented
187 0 ▶ &App::sub_entry if ($App::trace);
Branch: 1 branch
BranchTrueFalseTotal
if $App::trace 0 0 0
188 0 my ($self, $doc, $options) = @_;
189 0 my $xml = $self->doc2xml($doc);
190  
191 0 ▶ if ($self->{xmlns} && $self->{schema_location}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"xmlns"} and $self->{"schema_location"}) 0 0 0
Condition0%0/3
$self->{"xmlns"} and $self->{"schema_location"}
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"xmlns"} and $self->{"schema_location"}
$self->{"xmlns"} $self->{"schema_location"}
Truth table0%0/3
A and B
A= $self->{"xmlns"}B= $self->{"schema_location"}
ABresult
0X0
100
111
192 0 my $schema_location = $self->get_root_element_attribute($xml,"xsi:schemaLocation");
193 #if ($schema_location && $schema_location =~ /^$self->{xmlns} /) { }
194 0 ▶ if ($schema_location) {
Branch: 1 branch
BranchTrueFalseTotal
if ($schema_location) 0 0 0
195 0 my $schema_file = $schema_location;
196 0 $schema_file =~ s/.* //;
197 0 ▶ if ($schema_file =~ m/^[a-z]:/i || $schema_file =~ m/^[\\\/]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($schema_file =~ /^[a-z]:/i or $schema_file =~ m[^[\\/]]) 0 0 0
Condition0%0/3
$schema_file =~ /^[a-z]:/i or $schema_file =~ m[^[\\/]]
l!l&&r!l&&!r
000
MC/DC0%0/2
$schema_file =~ /^[a-z]:/i or $schema_file =~ m[^[\\/]]
$schema_file =~ /^[a-z]:/i $schema_file =~ m[^[\\/]]
Truth table0%0/3
A or B
A= $schema_file =~ /^[a-z]:/iB= $schema_file =~ m[^[\\/]]
ABresult
1X1
011
000
198 0 $schema_file =~ s/.*[\\\/]//;
199 0 my $good_schema_location = "$self->{xmlns} $self->{schema_location}/$schema_file";
200 0 $self->set_root_element_attribute(\$xml,"xsi:schemaLocation",$good_schema_location);
201 }
202 }
203 }
204  
205 0 ▶ $options = {} if (!$options);
Branch: 1 branch
BranchTrueFalseTotal
unless $options 0 0 0
206 0 my $valid = 1;
207 0 delete $options->{dom};
208 0 delete $options->{error};
209 0 delete $options->{error_line};
210 0 delete $options->{error_column};
211  
212 0 ▶ die "validate called with no data to validate\n" unless defined $xml and length $xml > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $xml and length $xml > 0 0 0 0
Condition0%0/3
defined $xml and length $xml > 0
!ll&&!rl&&r
000
MC/DC0%0/2
defined $xml and length $xml > 0
defined $xml length $xml > 0
Truth table0%0/3
A and B
A= defined $xmlB= length $xml > 0
ABresult
0X0
100
111
213  
214 0 my $dom = $self->parse($xml, $options);
215 0 ▶ if ($dom && !$options->{error}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dom and not $options->{'error'}) { } 0 0 0
Condition0%0/3
$dom and not $options->{"error"}
!ll&&!rl&&r
000
MC/DC0%0/2
$dom and not $options->{"error"}
$dom not $options->{"error"}
Truth table0%0/3
A and not(B)
A= $domB= not $options->{"error"}
ABresult
0X0
100
111
216 0 $options->{dom} = $dom;
217 }
218 else {
219 0 $valid = 0;
220 }
221  
222 0 ▶ &App::sub_exit($valid) if ($App::trace);
Branch: 1 branch
BranchTrueFalseTotal
if $App::trace 0 0 0
223 0 return($valid);
224 }
225  
226 0 ▶sub doc2dom {
Subroutine doc2dom: not called CC=5 SCAR=34.0
Pod undocumented
227 0 ▶ &App::sub_entry if ($App::trace);
Branch: 1 branch
BranchTrueFalseTotal
if $App::trace 0 0 0
228 0 my ($self, $doc) = @_;
229 0 my $ref = ref($doc);
230 0 my ($dom);
231 0 ▶ if (!$ref) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $ref) { } 0 0 0
elsif ($ref eq 'XML::Toolset::Document') { } 0 0 0
232 0 $dom = $self->parse($doc);
233 }
234 elsif ($ref eq "XML::Toolset::Document") {
235 0 $dom = $doc->dom();
236 }
237 else {
238 0 $dom = $doc;
239 }
240 0 ▶ &App::sub_exit($dom) if ($App::trace);
Branch: 1 branch
BranchTrueFalseTotal
if $App::trace 0 0 0
241 0 return($dom);
242 }
243  
244 0 ▶sub doc2xml {
Subroutine doc2xml: not called CC=5 SCAR=34.0
Pod undocumented
245 0 ▶ &App::sub_entry if ($App::trace);
Branch: 1 branch
BranchTrueFalseTotal
if $App::trace 0 0 0
246 0 my ($self, $doc) = @_;
247 0 my $ref = ref($doc);
248 0 my ($xml);
249 0 ▶ if (!$ref) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $ref) { } 0 0 0
elsif ($ref eq 'XML::Toolset::Document') { } 0 0 0
250 0 $xml = $doc;
251 }
252 elsif ($ref eq "XML::Toolset::Document") {
253 0 $xml = $doc->xml();
254 }
255 else {
256 0 $xml = $doc->to_string();
257 }
258 0 ▶ &App::sub_exit($xml) if ($App::trace);
Branch: 1 branch
BranchTrueFalseTotal
if $App::trace 0 0 0
259 0 return($xml);
260 }
261  
262 0 ▶sub set_validation {
Subroutine set_validation: not called CC=3 SCAR=24.8
Pod undocumented
263 0 ▶ &App::sub_entry if ($App::trace);
Branch: 1 branch
BranchTrueFalseTotal
if $App::trace 0 0 0
264 0 my ($self, $validation) = @_;
265 0 $self->{validation} = $validation;
266 0 delete $self->{parser}; # the next parser will have the new validation setting
267 0 ▶ &App::sub_exit() if ($App::trace);
Branch: 1 branch
BranchTrueFalseTotal
if $App::trace 0 0 0
268 }
269  
270 0 ▶sub parser {
Subroutine parser: not called CC=4 SCAR=30.0
Pod undocumented
271 0 ▶ &App::sub_entry if ($App::trace);
Branch: 1 branch
BranchTrueFalseTotal
if $App::trace 0 0 0
272 0 my ($self, $options) = @_;
273 0 my $parser = $self->{parser};
274 0 ▶ if (!$parser) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($parser) 0 0 0
275 0 $parser = $self->new_parser($options);
276 0 $self->{parser} = $parser;
277 }
278 0 ▶ &App::sub_exit($parser) if ($App::trace);
Branch: 1 branch
BranchTrueFalseTotal
if $App::trace 0 0 0
279 0 return($parser);
280 }
281  
282 1 1;
283  
284 __END__
« blib/lib/XML/Toolset/MSXML.pm ↑ Summary blib/lib/XML/Toolset/LibXML.pm »