File Coverage

lib/XML/Schema/Particle/Element.pm
Criterion Covered Total %
statement 26 33 78.7
branch 13 22 59.0
condition 2 4 50.0
subroutine 5 7 71.4
pod 1 4 25.0
total 47 70 67.1


line stmt bran cond sub pod time code
1             #============================================================= -*-perl-*-
2             #
3             # XML::Schema::Particle::Element.pm
4             #
5             # DESCRIPTION
6             # Subclassed particle to contain a reference to a element instead
7             # of a simple particle.
8             #
9             # AUTHOR
10             # Andy Wardley
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: Element.pm,v 1.1.1.1 2001/08/29 14:30:17 abw Exp $
21             #
22             #========================================================================
23              
24             package XML::Schema::Particle::Element;
25              
26 5     5   810 use strict;
  5         10  
  5         227  
27 5     5   42 use base qw( XML::Schema::Particle );
  5         10  
  5         473  
28 5     5   28 use vars qw( $VERSION $DEBUG $ERROR $ETYPE );
  5         8  
  5         3262  
29              
30             $VERSION = sprintf("%d.%02d", q$Revision: 1.1.1.1 $ =~ /(\d+)\.(\d+)/);
31             #$DEBUG = 0 unless defined $DEBUG;
32             #$ERROR = '';
33             $ETYPE = 'ElementParticle';
34              
35             *DEBUG = \$XML::Schema::Particle::DEBUG;
36             *ERROR = \$XML::Schema::Particle::ERROR;
37             #*DECLINED = \&XML::Schema::Particle::DECLINED;
38              
39              
40             #------------------------------------------------------------------------
41             # init()
42             #
43             # Called automatically by base class new() method.
44             #------------------------------------------------------------------------
45              
46             sub init {
47 20     20 1 35 my ($self, $config) = @_;
48              
49 20 50       51 $self->TRACE("config => ", $config) if $DEBUG;
50              
51 20         86 $self->{ type } = 'element';
52             $self->{ element } = $config->{ element }
53 20   50     67 || return $self->error(ref $self, ': element not specified');
54             $self->{ name } = $self->{ element }->name()
55 20   50     79 || return $self->error("unable to determine name for element '$self->{ element }'");
56              
57 20 100       88 $self->constrain($config)
58             || return;
59              
60 19         203 return $self;
61             }
62              
63              
64             sub element {
65 16     16 0 39 my ($self, $name) = @_;
66 16         39 my ($min, $max, $occurs, $ename)
67             = @$self{ qw( min max occurs name ) };
68 16         28 $self->{ _ERROR } = '';
69              
70             # return element reference for reflective purposes when called
71             # without a name argument
72             return $self->{ element }
73 16 100       50 unless $name;
74              
75 10 50       20 $self->TRACE("name => $name") if $DEBUG;
76              
77             # if the element names don't match then the candidate element must
78             # belong to the next particle in the content model; we must therefore
79             # validate the current particle to ensure it has been satisfied
80 10 100       28 unless ($name eq $ename) {
81            
82 2 50       28 return $self->error("unexpected <$name> found (min. $min <$ename> element",
    50          
83             $min > 1 ? 's' : '', " required)")
84             if $occurs < $min;
85              
86 0         0 return $self->decline("unexpected <$name> element found");
87             }
88              
89             # at this point, we know the element names match, but we may have
90             # exceeded our maxOccurs limit, in which case we decline hoping
91             # that a subsequent particle can collect it
92 8 50       57 return $self->decline("maximum of $max <$ename> element",
    100          
93             $max > 1 ? 's' : '', " exceeded")
94             unless $occurs < $max;
95              
96             # OK, it looks like the particle can accept the element
97 5         8 $self->{ occurs }++;
98              
99 5         15 return $self->{ element };
100             }
101              
102              
103             sub match {
104 0     0 0   my ($self, $name) = @_;
105              
106             # true if names match
107 0 0         return 1 if $self->{ name } eq $name;
108              
109             # false if names don't match but particle has minOccurs == 0
110 0 0         return 0 if $self->{ min } == 0;
111              
112             # undef otherwise
113 0           return undef;
114             }
115              
116              
117             sub ID {
118 0     0 0   my $self = shift;
119 0           return "$ETYPE\[$self->{ name }]";
120             }
121            
122             1;
123              
124