Schema/RootNode.pm

filter: Statementstmt 98.6% 74 / 75 Branchbran 75.0% 18 / 24 Conditioncond 100% 4 / 4 MC/DCmcdc 100% 2 / 2 Subroutinesub 100% 11 / 11 Podpod 16.6% 1 / 6 totaltotal 90.1% 110 / 122 CC 20 SCAR 17.1
CC
20
Cov
93%
complete_type
10.1
compile
5.0
complete_ref
5.0
CRAP 5.5
« Schema/SimpleTypeNode.pm ↑ Summary Schema/Library.pm »
1 package XML::Validator::Schema::RootNode;
2 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 5 ▶use base 'XML::Validator::Schema::ElementNode';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 5 ▶use XML::Validator::Schema::Util qw(_err);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 =head1 NAME
11  
12 XML::Validator::Schema::RootNode - the root node in a schema document
13  
14 =head1 DESCRIPTION
15  
16 This is an internal module used by XML::Validator::Schema to represent
17 the root node in an XML Schema document. Holds references to the
18 libraries for the schema document and is responsible for hooking up
19 named types to their uses in the node tree at the end of parsing.
20  
21 =cut
22  
23 51 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
24 51 my $pkg = shift;
25 51 my $self = $pkg->SUPER::new(@_);
26  
27 # start up with empty libraries
28 51 $self->{type_library} = XML::Validator::Schema::TypeLibrary->new;
29 51 $self->{element_library} = XML::Validator::Schema::ElementLibrary->new;
30 51 $self->{attribute_library} = XML::Validator::Schema::AttributeLibrary->new;
31  
32 51 return $self;
33 }
34  
35 # finish typing and references
36 39 ▶sub compile {
Subroutine compile: called CC=5 SCAR=16.1
Pod undocumented
37 39 my $self = shift;
38 39 my $element_library = $self->{element_library};
39  
40 # put global elements into the library (could move this to ::ElementNode)
41 39 foreach my $d ($self->daughters) {
42 80 ▶ if (ref($d) eq 'XML::Validator::Schema::ElementNode') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $d eq "XML::Validator::Schema::ElementNode") 80 0 80
43 $element_library->add(name => $d->{name},
44 80 obj => $d);
45 }
46 }
47  
48  
49 # complete all element refs first, forming a complete tree
50 39 foreach my $element ($self->descendants) {
51 214 $self->complete_ref($element);
52 }
53  
54 # completa all element types, including their attributes
55 39 foreach my $element ($self->descendants) {
56 256 $self->complete_type($element);
57 }
58  
59 }
60  
61 215 ▶sub complete_ref {
Subroutine complete_ref: called CC=5 SCAR=16.1
Pod undocumented
62 215 my ($self, $ref) = @_;
63  
64 # handle any unresolved attribute types
65 215 ▶ if ($ref->{attr}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref->{"attr"}) 16 199 215
66 $self->complete_attr_ref($_)
67 16 for (grep { $_->{unresolved_ref} } (@{$ref->{attr}}));
68 }
69  
70 # all done unless unresolved
71 215 ▶ return unless $ref->{unresolved_ref};
Branch: 1 branch
BranchTrueFalseTotal
unless $ref->{"unresolved_ref"} 172 43 215
72  
73 43 my $name = $ref->{name};
74 43 my ($element) = $self->{element_library}->find(name => $ref->{name});
75 43 ▶ _err("Found unresolved reference to element '$name'")
Branch: 1 branch
BranchTrueFalseTotal
unless $element 0 43 43
76 unless $element;
77  
78  
79  
80 # replace the current element
81 43 $ref->replace_with($element->copy_at_and_under);
82  
83 43 return;
84 }
85  
86 278 ▶sub complete_type {
Subroutine complete_type: called CC=10 SCAR=23.2
Pod undocumented
87 278 my ($self, $element) = @_;
88 278 my $library = $self->{type_library};
89  
90 # handle any unresolved attribute types
91 278 ▶ if ($element->{attr}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($element->{"attr"}) 28 250 278
92 $self->complete_attr_type($_)
93 28 for (grep { $_->{unresolved_type} } (@{$element->{attr}}));
94 }
95  
96 # all done unless unresolved
97 277 ▶ return unless $element->{unresolved_type};
Branch: 1 branch
BranchTrueFalseTotal
unless $element->{"unresolved_type"} 117 160 277
98  
99 # get type data
100 160 my $type_name = $element->{type_name};
101 160 my $type = $library->find(name => $type_name);
102  
103 # isn't there?
104 160 ▶ _err("Element '<$element->{name}>' has unrecognized type '$type_name'.")
Branch: 1 branch
BranchTrueFalseTotal
unless $type 0 160 160
105 unless $type;
106  
107  
108 160 ▶ if ($type->isa('XML::Validator::Schema::ComplexTypeNode')) {
Branch: 2 branches
BranchTrueFalseTotal
if ($type->isa('XML::Validator::Schema::ComplexTypeNode')) { } 18 142 160
elsif ($type->isa('XML::Validator::Schema::SimpleType')) { } 142 0 142
109 # can't have daughters for this to work
110 18 ▶ _err("Element '<$element->{name}>' is using a named complexType and has sub-elements of its own. That's not supported.")
Branch: 1 branch
BranchTrueFalseTotal
if $element->daughters 0 18 18
111 if $element->daughters;
112
113 # replace the current element with one based on the complex node
114 18 my $new_node = $type->copy_at_and_under;
115 18 $new_node->name($element->{name});
116 18 ▶ $new_node->{attr} = [ @{ $new_node->{attr} || [] },
Condition100%2/2
$new_node->{'attr'} || []
l!l
117
MC/DC100%1/1
$new_node->{'attr'} || []
$new_node->{'attr'}
Truth table100%2/2
A
A= $new_node->{'attr'}
Aresult
11
00
117 18 ▶ @{ $element->{attr} || [] } ];
Condition100%2/2
$element->{'attr'} || []
l!l
117
MC/DC100%1/1
$element->{'attr'} || []
$element->{'attr'}
Truth table100%2/2
A
A= $element->{'attr'}
Aresult
11
00
118 18 $element->replace_with($new_node);
119  
120  
121 } elsif ($type->isa('XML::Validator::Schema::SimpleType')) {
122 142 $element->{type} = $type;
123  
124 } else {
125 0 croak("Library returned '$type'!");
126 }
127  
128 # fixed it
129 160 delete $element->{unresolved_type};
130 }
131  
132 21 ▶sub complete_attr_type {
Subroutine complete_attr_type: called CC=2 SCAR=6.9
Pod undocumented
133 21 my ($self, $attr) = @_;
134  
135 21 my $type = $self->{type_library}->find(name => $attr->{type_name});
136 21 ▶ _err("Attribute '<$attr->{name}>' has unrecognized ".
Branch: 1 branch
BranchTrueFalseTotal
unless $type 1 20 21
137 "type '$attr->{type_name}'.")
138 unless $type;
139  
140 20 $attr->{type} = $type;
141 20 delete $attr->{unresolved_type};
142 }
143  
144 1 ▶sub complete_attr_ref {
Subroutine complete_attr_ref: called CC=2 SCAR=7.0
Pod undocumented
145 1 my ($self, $ref) = @_;
146  
147 1 my $attr = $self->{attribute_library}->find(name => $ref->{name});
148 1 ▶ _err("Attribute reference '$ref->{name}' not found.")
Branch: 1 branch
BranchTrueFalseTotal
unless $attr 0 1 1
149 unless $attr;
150
151 # clone, keep use
152 1 my $use = $ref->{required};
153 1 %$ref = %$attr;
154 1 $ref->{required} = $use;
155  
156 1 return;
157 }
158  
159  
160  
161 5 1;
« Schema/SimpleTypeNode.pm ↑ Summary Schema/Library.pm »