| 1 | #============================================================= -*-perl-*- | ||
| 2 | # | ||
| 3 | # XML::Schema::Type::Provider | ||
| 4 | # | ||
| 5 | # DESCRIPTION | ||
| 6 | # Module implementing a mixin object class for providing type | ||
| 7 | # management facilities within a particular scope. | ||
| 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: Provider.pm,v 1.1.1.1 2001/08/29 14:30:17 abw Exp $ | ||
| 21 | # | ||
| 22 | #======================================================================== | ||
| 23 | |||
| 24 | package XML::Schema::Type::Provider; | ||
| 25 | |||
| 26 | use strict; | ||
| 27 | use XML::Schema; | ||
| 28 | use base qw( XML::Schema::Base ); | ||
| 29 | use vars qw( $VERSION $DEBUG $ERROR @OPTIONAL ); | ||
| 30 | |||
| 31 | $VERSION = sprintf("%d.%02d", q$Revision: 1.1.1.1 $ =~ /(\d+)\.(\d+)/); | ||
| 32 | $DEBUG = 0 unless defined $DEBUG; | ||
| 33 | $ERROR = ''; | ||
| 34 | |||
| 35 | |||
| 36 | @OPTIONAL = qw( parent ); | ||
| 37 | |||
| 38 | |||
| 39 | #------------------------------------------------------------------------ | ||
| 40 | # init_types(\%config) | ||
| 41 | # | ||
| 42 | # Initialiser method called by base class new() constructor method. | ||
| 43 | #------------------------------------------------------------------------ | ||
| 44 | |||
| 45 | sub init_types { | ||
| 46 | my ($self, $config) = @_; | ||
| 47 | my $types; | ||
| 48 | |||
| 49 | $self->{ _FACTORY } = $config->{ FACTORY } | ||
| 50 | || $XML::Schema::FACTORY; | ||
| 51 | |||
| 52 | $self->{ _TYPES } = { }; | ||
| 53 | |||
| 54 | return $self; | ||
| 55 | } | ||
| 56 | |||
| 57 | |||
| 58 | #------------------------------------------------------------------------ | ||
| 59 | # types() | ||
| 60 | # | ||
| 61 | # Return reference to hash array of internal type definitions. | ||
| 62 | #------------------------------------------------------------------------ | ||
| 63 | |||
| 64 | sub types { | ||
| 65 | my $self = shift; | ||
| 66 | return $self->{ _TYPES }; | ||
| 67 | } | ||
| 68 | |||
| 69 | |||
| 70 | #------------------------------------------------------------------------ | ||
| 71 | # type($name) | ||
| 72 | # type($name, $type_obj) | ||
| 73 | # | ||
| 74 | # Direct way to fetch/store types against names. | ||
| 75 | #------------------------------------------------------------------------ | ||
| 76 | |||
| 77 | sub type { | ||
| 78 | my $self = shift; | ||
| 79 | my $name = shift; | ||
| 80 | my ($type, $scope, $factory, $simple, $class); | ||
| 81 | |||
| 82 | return ($self->{ _TYPES }->{ $name } = shift) | ||
| 83 | if @_; | ||
| 84 | |||
| 85 | return $type | ||
| 86 | if ($type = $self->{ _TYPES }->{ $name }); | ||
| 87 | |||
| 88 | # delegate to any defined 'scope' if type not found | ||
| 89 | if ($scope = $self->{ scope }) { | ||
| 90 | $self->TRACE("delegating $name to $scope") if $DEBUG; | ||
| 91 | return $scope->type($name) | ||
| 92 | || $self->error($scope->error()); | ||
| 93 | } | ||
| 94 | |||
| 95 | # otherwise look for it as a builtin simple type | ||
| 96 | $factory = $self->{ _FACTORY } | ||
| 97 | || return $self->error("no factory defined"); | ||
| 98 | |||
| 99 | $simple = $factory->module('simple') | ||
| 100 | || return $self->error($factory->error()); | ||
| 101 | |||
| 102 | if ($class = $simple->builtin($name)) { | ||
| 103 | return $class->new() | ||
| 104 | || $self->error($class->error()); | ||
| 105 | } | ||
| 106 | else { | ||
| 107 | return $self->error("no such type: $name"); | ||
| 108 | } | ||
| 109 | } | ||
| 110 | |||
| 111 | |||
| 112 | |||
| 113 | #------------------------------------------------------------------------ | ||
| 114 | # simpleType(\%type_options) | ||
| 115 | # | ||
| 116 | # Method for creating a simpleType object and adding it to the internal | ||
| 117 | # type definition facility. | ||
| 118 | #------------------------------------------------------------------------ | ||
| 119 | |||
| 120 | sub simpleType { | ||
| 121 | my $self = shift; | ||
| 122 | my $factory = $self->{ _FACTORY }; | ||
| 123 | my ($name, $args, $type); | ||
| 124 | |||
| 125 | if (ref $_[0]) { | ||
| 126 | # hash array or simple type object | ||
| 127 | $args = shift; | ||
| 128 | } | ||
| 129 | elsif (scalar @_ == 1) { | ||
| 130 | # name requesting specific type | ||
| 131 | $name = shift; | ||
| 132 | return $self->type($name); | ||
| 133 | } | ||
| 134 | else { | ||
| 135 | $args = { @_ }; | ||
| 136 | } | ||
| 137 | |||
| 138 | if ($factory->isa( simple => $args )) { | ||
| 139 | $type = $args; | ||
| 140 | } | ||
| 141 | else { | ||
| 142 | $type = $factory->create( simple => $args ) | ||
| 143 | || return $self->error( $factory->error() ); | ||
| 144 | } | ||
| 145 | defined ($name = $type->name()) | ||
| 146 | || return $self->error('no name specified for simpleType'); | ||
| 147 | |||
| 148 | $self->TRACE("name => ", $type->ID) if $DEBUG; | ||
| 149 | |||
| 150 | return $self->type($name => $type); | ||
| 151 | } | ||
| 152 | |||
| 153 | |||
| 154 | #------------------------------------------------------------------------ | ||
| 155 | # complexType(\%type_options) | ||
| 156 | # | ||
| 157 | # Method for creating a complexType object and adding it to the internal | ||
| 158 | # type definition facility. | ||
| 159 | #------------------------------------------------------------------------ | ||
| 160 | |||
| 161 | sub complexType { | ||
| 162 | my $self = shift; | ||
| 163 | my $factory = $self->{ _FACTORY }; | ||
| 164 | my ($name, $args, $type); | ||
| 165 | |||
| 166 | if (ref $_[0]) { | ||
| 167 | # hash array or complex type object | ||
| 168 | $args = shift; | ||
| 169 | } | ||
| 170 | elsif (scalar @_ == 1) { | ||
| 171 | # name requesting specific type | ||
| 172 | $name = shift; | ||
| 173 | return $self->type->{ $name }; | ||
| 174 | } | ||
| 175 | else { | ||
| 176 | $args = { @_ }; | ||
| 177 | } | ||
| 178 | |||
| 179 | if ($factory->isa( complex => $args )) { | ||
| 180 | $type = $args; | ||
| 181 | # define scope of complex type unless already set | ||
| 182 | $type->scope($self) | ||
| 183 | unless defined $type->scope(); | ||
| 184 | |||
| 185 | } | ||
| 186 | else { | ||
| 187 | # define scope of complex type unless already set | ||
| 188 | $args->{ scope } = $self | ||
| 189 | if UNIVERSAL::isa($args, 'HASH') | ||
| 190 | && ! exists $args->{ scope }; | ||
| 191 | |||
| 192 | $type = $factory->create( complex => $args ) | ||
| 193 | || return $self->error( $factory->error() ); | ||
| 194 | } | ||
| 195 | defined ($name = $type->name()) | ||
| 196 | || return $self->error('no name specified for complexType'); | ||
| 197 | |||
| 198 | $self->TRACE("name => ", $type->ID) if $DEBUG; | ||
| 199 | |||
| 200 | return $self->type($name => $type); | ||
| 201 | |||
| 202 | } | ||
| 203 | |||
| 204 | |||
| 205 | 1; | ||
| 206 |