File Coverage

lib/XML/Schema/Content.pm
Criterion Covered Total %
statement 37 38 97.3
branch 19 20 95.0
condition 8 15 53.3
subroutine 12 13 92.3
pod 1 9 11.1
total 77 95 81.0


line stmt bran cond sub pod time code
1             #============================================================= -*-perl-*-
2             #
3             # XML::Schema::Content.pm
4             #
5             # DESCRIPTION
6             # Module implementing a class to represent a content model being either
7             # 'empty', having a 'simple' type, or a pair of particle and model type,
8             # which can be one of 'mixed' or 'element-only'.
9             #
10             # AUTHOR
11             # Andy Wardley
12             #
13             # COPYRIGHT
14             # Copyright (C) 2001 Canon Research Centre Europe Ltd.
15             # All Rights Reserved.
16             #
17             # This module is free software; you can redistribute it and/or
18             # modify it under the same terms as Perl itself.
19             #
20             # REVISION
21             # $Id: Content.pm,v 1.1.1.1 2001/08/29 14:30:17 abw Exp $
22             #
23             #========================================================================
24              
25             package XML::Schema::Content;
26              
27 9     9   2440 use strict;
  9         17  
  9         371  
28 9     9   56 use XML::Schema;
  9         20  
  9         224  
29 9     9   50 use base qw( XML::Schema::Base );
  9         20  
  9         951  
30 9     9   47 use vars qw( $VERSION $DEBUG $ERROR $ETYPE @ARGS );
  9         17  
  9         11056  
31              
32             $VERSION = sprintf("%d.%02d", q$Revision: 1.1.1.1 $ =~ /(\d+)\.(\d+)/);
33             $DEBUG = 0 unless defined $DEBUG;
34             $ERROR = '';
35             $ETYPE = 'content';
36             @ARGS = qw( type content particle mixed empty );
37              
38             *FACTORY = \$XML::Schema::FACTORY;
39              
40              
41             # alias min() to minOccurs() and max() to maxOccurs()
42             *minOccurs = \&min;
43             *maxOccurs = \&max;
44              
45              
46             #------------------------------------------------------------------------
47             # init()
48             #
49             # Called automatically by base class new() method.
50             #------------------------------------------------------------------------
51              
52             sub init {
53 30     30 1 62 my ($self, $config) = @_;
54 30         51 my ($type, $content, $particle, $mixed);
55 30   33     287 my $factory = $self->{ FACTORY } ||= $config->{ FACTORY } || $XML::Schema::FACTORY;
      33        
56              
57 30 50       79 $self->TRACE("config => ", $config) if $DEBUG;
58              
59 30         113 $self->{ type } = undef;
60             # if ($type = $config->{ type }) {
61             # # simple type content
62             # $self->{ type } = $type;
63             # $self->TRACE("set type to $type") if $DEBUG;
64             # }
65             # elsif ($particle = $config->{ particle }) {
66 30 100       141 if ($particle = $config->{ particle }) {
    100          
67             # particle specified directly, mixed flag also allowed
68 4         22 $self->{ particle } = $particle;
69 4 100       14 $self->{ mixed } = $config->{ mixed } ? 1 : 0;
70             }
71             elsif (! $config->{ empty }) {
72 14 100       57 if ($particle = $factory->create( particle => $config )) {
73             # have a bash at creating a particle anyway
74 9         26 $self->{ particle } = $particle;
75             }
76             else {
77 5         21 my $error = $factory->error();
78             # HACK: this might be an empty/text only content model so
79             # we ignore particle errors that report a missing particle
80 5 100       33 return $self->error($error)
81             unless $error =~ /^particle expects one of:/;
82             }
83             }
84              
85 28 100       112 $self->{ mixed } = $config->{ mixed } ? 1 : 0;
86              
87 28         284 return $self;
88             }
89              
90             sub model {
91 15     15 0 26 my $self = shift;
92             return $self->{ type }
93             || $self->{ particle }
94 15   66     178 || $self->error("no particle defined in content model");
95             }
96              
97             sub type {
98 6     6 0 25 return $_[0]->{ type };
99             }
100              
101             sub particle {
102 15     15 0 24 my $self = shift;
103             return $self->{ particle }
104 15   66     87 || $self->error("no particle defined in content model");
105             }
106              
107             sub args {
108 27     27 0 182 return @ARGS;
109             }
110              
111             #------------------------------------------------------------------------
112             # mixed($flag)
113             #
114             # Used to set (if called with an argument) or get the current value
115             # for the 'mixed' flag indicating if the complexType accepts mixed
116             # content.
117             #------------------------------------------------------------------------
118              
119             sub mixed {
120 25     25 0 43 my $self = shift;
121 25 100       159 return @_ ? ($self->{ mixed } = shift) : $self->{ mixed };
122             }
123              
124              
125             #------------------------------------------------------------------------
126             # element_only($flag)
127             #
128             # The inverse of mixed(). Returns true if mixed is false and vice
129             # verse. Can also be used to update the mixed flag wih the correct
130             # truth inversion performed.
131             #------------------------------------------------------------------------
132              
133             sub element_only {
134 5     5 0 6 my $self = shift;
135 5 100       31 return @_ ? ! ($self->{ mixed } = ! shift) : ! $self->{ mixed };
136             }
137              
138              
139             #------------------------------------------------------------------------
140             # empty()
141             #
142             # Returns true if the content model is empty.
143             #------------------------------------------------------------------------
144              
145             sub empty {
146 8 100 66 8 0 75 return ($_[0]->{ type } || $_[0]->{ particle }) ? 0 : 1;
147             }
148              
149              
150             sub ID {
151 0     0 0   return 'Content';
152             }
153              
154             1;
155              
156