lib/XML/Schema/Factory.pm

filter: Statementstmt 89.2% 58 / 65 Branchbran 55.8% 19 / 34 Conditioncond 66.6% 4 / 6 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 90.0% 9 / 10 Podpod 16.6% 1 / 6 totaltotal 74.4% 93 / 125 CC 19 SCAR 15.0
CC
19
Cov
77%
adopt
6.8
create
6.5
isa
4.4
CRAP 4.5
« lib/XML/Schema/Handler.pm ↑ Summary lib/XML/Schema/Type/Builtin.pm »
1 #============================================================= -*-perl-*-
2 #
3 # XML::Schema::Factory
4 #
5 # DESCRIPTION
6 # Factory module for managing (e.g. loading and instantiating) other
7 # modules in the XML::Schema set.
8 #
9 # AUTHOR
10 # Andy Wardley <abw@kfs.org>
11 #
12 # COPYRIGHT
13 # Copyright (C) 2001 Canon Research Centre Europe Ltd.
14 # All Rights Reserved.
15 #
16 # This module is free software; you can redistribute it and/or
17 # modify it under the same terms as Perl itself.
18 #
19 # REVISION
20 # $Id: Factory.pm,v 1.2 2001/12/20 13:26:27 abw Exp $
21 #
22 #========================================================================
23  
24 package XML::Schema::Factory;
25  
26 28 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 28 ▶use vars qw( $VERSION $AUTOLOAD $DEBUG $ERROR $ETYPE $MODULES );
Subroutine BEGIN: called CC=1 SCAR=0.0
28 28 ▶use base qw( XML::Schema::Base );
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 28 $VERSION = sprintf("%d.%02d", q$Revision: 1.2 $ =~ /(\d+)\.(\d+)/);
31 28 ▶$DEBUG = 0 unless defined $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $XML::Schema::Factory::DEBUG 28 0 28
32 28 $ETYPE = 'Factory';
33 28 $ERROR = '';
34 28 $MODULES = {
35 # root schema object
36 schema => 'XML::Schema',
37  
38 # core XML::Schema::* objects
39 attribute => 'XML::Schema::Attribute',
40 attribute_group => 'XML::Schema::Attribute::Group',
41 complex => 'XML::Schema::Type::Complex',
42 content => 'XML::Schema::Content',
43 element => 'XML::Schema::Element',
44 exception => 'XML::Schema::Exception',
45 instance => 'XML::Schema::Instance',
46 model => 'XML::Schema::Model',
47 parser => 'XML::Schema::Parser',
48 particle => 'XML::Schema::Particle',
49 wildcard => 'XML::Schema::Wildcard',
50 simple => 'XML::Schema::Type::Simple',
51 # particle objects
52 element_particle => 'XML::Schema::Particle::Element',
53 sequence_particle => 'XML::Schema::Particle::Sequence',
54 choice_particle => 'XML::Schema::Particle::Choice',
55 model_particle => 'XML::Schema::Particle::Model',
56 # parser handlers
57 schema_handler => 'XML::Schema::Handler::Schema',
58 simple_handler => 'XML::Schema::Handler::Simple',
59 complex_handler => 'XML::Schema::Handler::Complex',
60 };
61  
62  
63 #------------------------------------------------------------------------
64 # init(\%config)
65 #
66 # Initialiser method called by base class new() constructor.
67 #------------------------------------------------------------------------
68  
69 0 ▶sub init {
Subroutine init: not called CC=1 SCAR=6.9
Pod documented
70 0 my ($self, $config) = @_;
71 0 my $class = ref $self;
72 0 bless { %$MODULES, %$config }, $class;
73 }
74  
75  
76 #------------------------------------------------------------------------
77 # create($module_type)
78 #
79 # Look up module name for a given type in $MODULES hash if called as a
80 # class method, or $self hash if called as an object method. Load the
81 # module via load() method and then instantiate an object via new()
82 #------------------------------------------------------------------------
83  
84 208 ▶sub create {
Subroutine create: called CC=6 SCAR=18.7
Pod undocumented
85 208 my $self = shift;
86 208 my $type = shift;
87  
88 208 ▶ $self->DEBUG($self->ID, "->create('$type')\n")
Branch: 1 branch
BranchTrueFalseTotal
if $XML::Schema::Factory::DEBUG 0 208 208
89 if $DEBUG;
90  
91 208 my $module;
92 208 ▶ if (ref $self) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self) { } 0 208 208
93 0 $module = $self->{ $type };
94 }
95 else {
96 208 $module = $MODULES->{ $type };
97 }
98 208 ▶ return $self->error("module not recognised: '$type'")
Branch: 1 branch
BranchTrueFalseTotal
unless $module 0 208 208
99 unless $module;
100  
101 208 ▶ return undef unless $self->load($module);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->load($module) 1 207 208
102 207 ▶ return $module->new(@_)
Condition66%2/3
$module->new(@_) || $self->error($module->error)
l!l&&r!l&&!r
192015
MC/DC50%1/2
$module->new(@_) || $self->error($module->error)
$module->new(@_) $self->error($module->error)
Truth table66%2/3
A || B
A= $module->new(@_)B= $self->error($module->error)
ABresult
1X1
011
000
103 || $self->error($module->error());
104 }
105  
106  
107 #------------------------------------------------------------------------
108 # adopt($module_type, $object, \@args)
109 #
110 # Look up module name for a given type in $MODULES hash if called as a
111 # class method, or $self hash if called as an object method. Load the
112 # module via load() method, rebless $object into the new module class
113 # then call its new init() method passing a reference to an optional
114 # hash reference of configuration options.
115 #------------------------------------------------------------------------
116  
117 22 ▶sub adopt {
Subroutine adopt: called CC=6 SCAR=19.2
Pod undocumented
118 22 my ($self, $type, $object, $config) = @_;
119  
120 22 ▶ $self->DEBUG($self->ID, "->adopt('$type', $object)\n")
Branch: 1 branch
BranchTrueFalseTotal
if $XML::Schema::Factory::DEBUG 0 22 22
121 if $DEBUG;
122  
123 22 my $module;
124 22 ▶ if (ref $self) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self) { } 0 22 22
125 0 $module = $self->{ $type };
126 }
127 else {
128 22 $module = $MODULES->{ $type };
129 }
130 22 ▶ return $self->error("module not recognised: '$type'")
Branch: 1 branch
BranchTrueFalseTotal
unless $module 0 22 22
131 unless $module;
132  
133 22 ▶ return undef unless $self->load($module);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->load($module) 0 22 22
134 22 bless $object, $module;
135  
136 22 ▶ return $object->init($config)
Condition66%2/3
$object->init($config) || $self->error($object->error)
l!l&&r!l&&!r
2002
MC/DC50%1/2
$object->init($config) || $self->error($object->error)
$object->init($config) $self->error($object->error)
Truth table66%2/3
A || B
A= $object->init($config)B= $self->error($object->error)
ABresult
1X1
011
000
137 || $self->error($object->error());
138 }
139  
140  
141 #------------------------------------------------------------------------
142 # load($module)
143 #
144 # Load a module via require(). Any occurences of '::' in the module name
145 # are be converted to '/' and '.pm' is appended. Returns 1 on success
146 # or undef on error. Use $class->error() to examine the error string.
147 #------------------------------------------------------------------------
148  
149 284 ▶sub load {
Subroutine load: called CC=3 SCAR=11.0
Pod undocumented
150 284 my ($self, $module) = @_;
151 284 $module =~ s[::][/]g;
152 284 $module .= '.pm';
153 284 ▶ $self->DEBUG($self->ID, "->require('$module')\n")
Branch: 1 branch
BranchTrueFalseTotal
if $XML::Schema::Factory::DEBUG 0 284 284
154 if $DEBUG;
155 284 eval {
156 284 require $module;
157 };
158 284 ▶ return $@ ? $self->error("failed to load $module: $@") : 1;
Branch: 1 branch
BranchTrueFalseTotal
$@ ? : 1 283 284
159 }
160  
161  
162 #------------------------------------------------------------------------
163 # isa($type, $object)
164 #
165 # Look up class name for a given type in $MODULES hash if called as a
166 # class method, or $self hash if called as an object method, and check
167 # that $object is of, or derived from that type.
168 #------------------------------------------------------------------------
169  
170 125 ▶sub isa {
Subroutine isa: called CC=4 SCAR=14.8
Pod undocumented
171 125 my ($self, $type, $object) = @_;
172  
173 125 ▶ $self->DEBUG($self->ID, "->isa($type => ", $object->ID, ")\n")
Branch: 1 branch
BranchTrueFalseTotal
if $XML::Schema::Factory::DEBUG 0 125 125
174 if $DEBUG;
175  
176 125 my $class;
177 125 ▶ if (ref $self) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self) { } 0 125 125
178 0 $class = $self->{ $type };
179 }
180 else {
181 125 $class = $MODULES->{ $type };
182 }
183 125 ▶ return $self->error("type not recognised: '$type'")
Branch: 1 branch
BranchTrueFalseTotal
unless $class 0 125 125
184 unless $class;
185  
186 125 return UNIVERSAL::isa($object, $class);
187 }
188  
189  
190 #------------------------------------------------------------------------
191 # module($name)
192 #
193 # Look up module name for a given type in $MODULES hash if called as a
194 # class method, or $self hash if called as an object method.
195 #------------------------------------------------------------------------
196  
197 151 ▶sub module {
Subroutine module: called CC=3 SCAR=11.6
Pod undocumented
198 151 my ($self, $name) = @_;
199 151 my $module;
200  
201 151 ▶ if (ref $self) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self) { } 0 151 151
202 0 $module = $self->{ $name };
203 }
204 else {
205 151 $module = $MODULES->{ $name };
206 }
207 151 ▶ return $self->error("module not recognised: '$name'")
Branch: 1 branch
BranchTrueFalseTotal
unless $module 0 151 151
208 unless $module;
209  
210 151 return $module;
211 }
212  
213  
214 #------------------------------------------------------------------------
215 # AUTOLOAD
216 #
217 # Map method calls of the form $modules->parser(....) to
218 # $modules->create('parser', ...)
219 #------------------------------------------------------------------------
220  
221 11 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=2 SCAR=7.0
222 11 my $self = shift;
223 11 my $item = $AUTOLOAD;
224 11 $item =~ s/.*:://;
225 11 ▶ return if $item eq 'DESTROY';
Branch: 1 branch
BranchTrueFalseTotal
if $item eq "DESTROY" 0 11 11
226 11 $self->create($item, @_);
227 }
228
229 28 1;
230  
« lib/XML/Schema/Handler.pm ↑ Summary lib/XML/Schema/Type/Builtin.pm »