| Home | Trees | Indices | Help |
|
|---|
|
|
1 # Copyright 2009-2012, Peter A. Bigot
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License"); you may
4 # not use this file except in compliance with the License. You may obtain a
5 # copy of the License at:
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 # License for the specific language governing permissions and limitations
13 # under the License.
14
15 """Classes corresponding to W3C XML Schema components.
16
17 Class names and behavior should conform to the schema components described in
18 U{XML Schema Part 1: Structures<http://www.w3.org/TR/xmlschema-1/>}.
19 References to sections in the documentation of this module generally refers to
20 that document.
21
22 Each class has a C{CreateFromDOM} class method that creates an instance and
23 initializes it from a DOM node. Only the L{Wildcard}, L{Particle}, and
24 L{ModelGroup} components are created from non-DOM sources. However, the
25 requirements on DOM interface are restricted to attributes, child nodes, and
26 basic fields, though all these must support namespaces.
27
28 @group Mixins: *_mixin
29 @group Ur Type Specializations: *UrType*
30 @group Utilities: _PluralityData, _ImportElementInformationItem
31
32 """
33
34 import traceback
35 import pyxb
36 import pyxb.xmlschema
37 from xml.dom import Node
38 import xml.dom
39 import types
40 import re
41
42 import pyxb.namespace.archive
43 import pyxb.namespace.resolution
44
45 from pyxb.binding import basis
46 from pyxb.binding import datatypes
47 from pyxb.binding import facets
48 from pyxb.utils.domutils import *
49 import pyxb.utils.utility
50 import copy
51 import urllib2
52 import urlparse
53 import os.path
54
55 # Flag indicating that the built in types have been registered
56 _PastAddBuiltInTypes = False
57
58 # Make it easier to check node names in the XMLSchema namespace
59 from pyxb.namespace import XMLSchema as xsd
60
61 -class _SchemaComponent_mixin (pyxb.namespace._ComponentDependency_mixin,
62 pyxb.namespace.archive._ArchivableObject_mixin,
63 pyxb.utils.utility.PrivateTransient_mixin,
64 pyxb.utils.utility.Locatable_mixin):
65 """A mix-in that marks the class as representing a schema component.
66
67 This exists so that we can determine the owning schema for any
68 component we encounter. This is normally done at construction
69 time by passing a C{schema=val} parameter to the constructor.
70 """
71
72 # This class suppports transient instance variables. These variables are
73 # added to the set of transients at the point of declaration in the class.
74 __PrivateTransient = set()
75
77 """The namespace context for this schema.
78
79 This defines where it looks things up, where it puts things it
80 createas, the in-scope namespace declarations, etc. Must be defined
81 for anything that does any sort of QName interpretation. The value is
82 generally a reference to a namespace context associated with the DOM
83 element node corresponding to this component."""
84 if self.__namespaceContext is None:
85 raise pyxb.LogicError('Attempt to access missing namespace context for %s' % (self,))
86 return self.__namespaceContext
90 __namespaceContext = None
91 __PrivateTransient.add('namespaceContext')
92
93 # The name by which this component is known within the binding module.
94 # This is in component rather than _NamedComponent_mixin because some
95 # unnamed components (like ModelGroup and Wildcard) have Python objects to
96 # represent them, so need a binding-level name.
97 __nameInBinding = None
98
99 # The schema component that owns this. If C{None}, the component is owned
100 # directly by the schema.
101 __owner = None
102 __PrivateTransient.add('owner')
103
104 # The schema components owned by this component.
105 __ownedComponents = None
106 __PrivateTransient.add('ownedComponent')
107
109 """The context into which declarations in or subordinate to this nodeare placed."""
110 return self.__scope
111 __scope = None
112
114 """Return True iff nobody has defined a scope for this node."""
115 return _ScopedDeclaration_mixin.ScopeIsIndeterminate(self._scope())
116
118 """Return True iff this component has global scope."""
119 return _ScopedDeclaration_mixin.ScopeIsGlobal(self._scope())
120
122 """Set the scope of this instance after construction.
123
124 This should only be invoked on cloned declarations being incorporated
125 into a complex type definition. Note that the source of the clone may
126 be any scope: indeterminate if from a model (attribute) group
127 definition; global if a reference to a global component; or ctd if
128 inherited from a complex base type."""
129 assert self.__cloneSource is not None
130 assert isinstance(self, _ScopedDeclaration_mixin)
131 assert isinstance(ctd, ComplexTypeDefinition)
132 self.__scope = ctd
133 return self
134
136 """Initialize portions of a component.
137
138 @keyword scope: The scope in which the component is defined
139
140 @keyword namespace_context: The NamespaceContext to use within this component
141
142 @keyword node: If no C{namespace_context} is provided, a DOM node must
143 be provided from which a namespace context can be identified.
144
145 @keyword owner: Reference to the component that owns this one (the
146 immediately enclosing component). Is C{None} in the case of top-level
147 components.
148
149 @keyword schema: Reference to the L{Schema} component to which the
150 component belongs. Required for every component except L{Schema},
151 L{Annotation}, and L{Wildcard}.
152 """
153
154 self.__ownedComponents = set()
155 self.__scope = kw.get('scope')
156 self.__namespaceContext = kw.get('namespace_context')
157 node = kw.get('node')
158 if self.__namespaceContext is None:
159 if node is None:
160 raise pyxb.LogicError('Schema component constructor must be given namespace_context or node')
161 self.__namespaceContext = pyxb.namespace.resolution.NamespaceContext.GetNodeContext(node)
162 if self.__namespaceContext is None:
163 raise pyxb.LogicError('No namespace_context for schema component')
164
165 super(_SchemaComponent_mixin, self).__init__(*args, **kw)
166
167 if isinstance(node, pyxb.utils.utility.Locatable_mixin):
168 self._setLocation(node._location())
169
170 self._namespaceContext().targetNamespace()._associateComponent(self)
171
172 self._setOwner(kw.get('owner'))
173
174 schema = kw.get('schema')
175 if schema is not None:
176 self._setObjectOrigin(schema.originRecord())
177 else:
178 assert isinstance(self, (Schema, Annotation, Wildcard)), 'No origin available for type %s' % (type(self),)
179
180 if isinstance(self, ComplexTypeDefinition):
181 assert 1 < len(self.__namespaceContext.inScopeNamespaces())
182
183
185 """Dissociate this component from its owning namespace.
186
187 This should only be done whwen there are no other references to the
188 component, and you want to ensure it does not appear in the model."""
189 self._namespaceContext().targetNamespace()._replaceComponent(self, None)
190 return self
191
193 """Set the owner of this component.
194
195 If C{owner} is C{None}, this has no effect. Otherwise, the
196 component's current owner must be either C{None} or the same as the
197 input C{owner}."""
198
199 if owner is not None:
200 assert (self.__owner is None) or (self.__owner == owner), 'Owner was %s set to %s' % (self.__owner, owner)
201 self.__owner = owner
202 owner.__ownedComponents.add(self)
203 return self
204
206 return self.__owner
207
208 # A reference to the instance from which this instance was cloned.
209 __cloneSource = None
210 __PrivateTransient.add('cloneSource')
211
213 """The source component from which this is a clone.
214
215 Returns C{None} if this is not a clone."""
216 return self.__cloneSource
217
218 # A set of references to all instances that are clones of this one.
219 __clones = None
220 __PrivateTransient.add('clones')
221
223 """The set of instances cloned from this component.
224
225 Returns None if no instances have been cloned from this."""
226 return self.__clones
227
229 """Virtual method to clear whatever attributes should be reset in a
230 cloned component.
231
232 This instance should be an instance created by copy.copy().
233
234 The implementation in this class clears the owner and dependency
235 relations.
236
237 Returns C{self}.
238 """
239 assert self.__cloneSource is not None
240 owner = kw['owner']
241 self.__nameInBinding = None
242 self.__owner = owner
243 assert not (isinstance(self, ComplexTypeDefinition) and isinstance(owner, Schema))
244 self.__ownedComponents = set()
245 self.__clones = None
246 owner._namespaceContext().targetNamespace()._associateComponent(self)
247 if self.__namespaceContext is None:
248 # When cloning imported components, loan them the owner's
249 # namespace context, only so that their cloned children can be
250 # associated with the same namespace.
251 self.__namespaceContext = owner._namespaceContext()
252 return getattr(super(_SchemaComponent_mixin, self), '_resetClone_csc', lambda *_args,**_kw: self)(**kw)
253
255 """Create a copy of this instance suitable for adoption by some other
256 component.
257
258 This is used for creating a locally-scoped declaration from a
259 declaration in a named model or attribute group."""
260
261 # We only care about cloning declarations, and they should
262 # have an unassigned scope. However, we do clone
263 # non-declarations that contain cloned declarations.
264 #assert (not isinstance(self, _ScopedDeclaration_mixin)) or self._scopeIsIndeterminate()
265 if isinstance(self, pyxb.namespace.resolution._Resolvable_mixin):
266 assert self.isResolved()
267
268 assert owner is not None
269 that = copy.copy(self)
270 that.__cloneSource = self
271 if self.__clones is None:
272 self.__clones = set()
273 self.__clones.add(that)
274 that._resetClone_csc(owner=owner, origin=origin)
275 if isinstance(that, pyxb.namespace.resolution._Resolvable_mixin):
276 assert that.isResolved()
277 return that
278
280 """Return True iff this component is a simple or complex type
281 definition."""
282 return isinstance(self, (SimpleTypeDefinition, ComplexTypeDefinition))
283
285 """Return True iff this component is a simple or complex type
286 definition."""
287 return isinstance(self, (_SimpleUrTypeDefinition, _UrTypeDefinition))
288
290 """Return the name of this component, as best it can be determined.
291
292 For example, ModelGroup instances will be named by their
293 ModelGroupDefinition, if available. Returns None if no name can be
294 inferred."""
295 if isinstance(self, _NamedComponent_mixin):
296 return self.name()
297 if isinstance(self, ModelGroup):
298 agd = self.modelGroupDefinition()
299 if agd is not None:
300 return agd.name()
301 return None
302
304 """Return the name by which this component is known in the generated
305 binding.
306
307 @note: To support builtin datatypes, type definitions with an
308 associated L{pythonSupport<SimpleTypeDefinition.pythonSupport>} class
309 initialize their binding name from the class name when the support
310 association is created. As long as no built-in datatype conflicts
311 with a language keyword, this should be fine."""
312 return self.__nameInBinding
313
315 """Return C{True} iff this is a component which has a user-visible
316 Python construct which serves as its binding.
317
318 Type definitions have classes as their bindings. Global element
319 declarations have instances of L{pyxb.binding.basis.element} as their
320 bindings."""
321 return self.isTypeDefinition() or (isinstance(self, ElementDeclaration) and self._scopeIsGlobal())
322
324 """Set the name by which this component shall be known in the XSD binding."""
325 self.__nameInBinding = name_in_binding
326 return self
327
329 """Override fields in this instance with those from the other.
330
331 Post-extended; description in leaf implementation in
332 ComplexTypeDefinition and SimpleTypeDefinition."""
333 assert self != other
334 getattr(super(_SchemaComponent_mixin, self), '_updateFromOther_csc', lambda *args, **kw: self)(other)
335 # The only thing we update is the binding name, and that only if it's new.
336 if self.__nameInBinding is None:
337 self.__nameInBinding = other.__nameInBinding
338 return self
339
341 """This class is a mix-in which guarantees that only one instance
342 of the class will be created. It is used to ensure that the
343 ur-type instances are pointer-equivalent even when unpickling.
344 See ComplexTypeDefinition.UrTypeDefinition()."""
346 singleton_property = '_%s__singleton' % (cls.__name__,)
347 if not (singleton_property in cls.__dict__):
348 setattr(cls, singleton_property, super(_Singleton_mixin, cls).__new__(cls, *args, **kw))
349 return cls.__dict__[singleton_property]
350
352 """Mix-in that supports an optional single annotation that describes the component.
353
354 Most schema components have annotations. The ones that don't are
355 L{AttributeUse}, L{Particle}, and L{Annotation}. L{ComplexTypeDefinition}
356 and L{Schema} support multiple annotations, so do not mix-in this
357 class."""
358
359 # Optional Annotation instance
360 __annotation = None
361
363 super(_Annotated_mixin, self).__init__(*args, **kw)
364 self.__annotation = kw.get('annotation')
365
367 cn = LocateUniqueChild(node, 'annotation')
368 if cn is not None:
369 kw = { }
370 if isinstance(self, _SchemaComponent_mixin):
371 kw['owner'] = self
372 self.__annotation = Annotation.CreateFromDOM(cn, **kw)
373
375 """Override fields in this instance with those from the other.
376
377 Post-extended; description in leaf implementation in
378 ComplexTypeDefinition and SimpleTypeDefinition."""
379 assert self != other
380 getattr(super(_Annotated_mixin, self), '_updateFromOther_csc', lambda *args, **kw: self)(other)
381 # @todo: make this a copy?
382 self.__annotation = other.__annotation
383 return self
384
386 return self.__annotation
387
389 """A helper that encapsulates a reference to an anonymous type in a different namespace.
390
391 Normally references to components in other namespaces can be made using
392 the component's name. This is not the case when a namespace derives from
393 a base type in another namespace and needs to reference the attribute or
394 element declarations held in that type. If these declarations are local
395 to the base complex type, they cannot be identified by name. This class
396 provides a pickleable representation for them that behaves rather like an
397 L{pyxb.namespace.ExpandedName} instance in that it can be used to
398 dereference various component types."""
399
400 __AnonymousCategory = pyxb.namespace.archive.NamespaceArchive._AnonymousCategory()
401
402 __namespace = None
403 __anonymousName = None
405 """Create a new anonymous reference.
406
407 @param namespace: The namespace in which the component is declared.
408 @type namespace: L{pyxb.namespace.Namespace}
409 @param anonymous_name: A generated name guaranteed to be unique within
410 the namespace. See L{_NamedComponent_mixin._anonymousName}.
411 @type anonymous_name: C{basestring}.
412 """
413 self.__namespace = namespace
414 self.__anonymousName = anonymous_name
415 assert self.__anonymousName is not None
416
417 @classmethod
419 """Return the component referred to by the provided reference,
420 regardless of whether it is a normal or anonymous reference."""
421 if not isinstance(object_reference, _PickledAnonymousReference):
422 assert isinstance(object_reference, tuple)
423 object_reference = pyxb.namespace.ExpandedName(object_reference)
424 return object_reference
425
427 return self.__namespace
428
430 return self.__anonymousName
431
435
437 #print 'Lookup %s' % (self,)
438 return self.__namespace.categoryMap(self.__AnonymousCategory).get(self.__anonymousName)
439
440 typeDefinition = __lookupObject
441 """Return the attribute group definition referenced by this instance, or
442 C{None} if the reference is not to an attribute group definition."""
443
444 attributeGroupDefinition = __lookupObject
445 modelGroupDefinition = __lookupObject
446 attributeDeclaration = __lookupObject
447 elementDeclaration = __lookupObject
448 identityConstraintDefinition = __lookupObject
449 notationDeclaration = __lookupObject
450
452 """Represent the anonymous reference in a form recognizable by a developer."""
453 return 'ANONYMOUS:%s' % (pyxb.namespace.ExpandedName(self.__namespace, self.__anonymousName),)
454
456 """Mix-in to hold the name and targetNamespace of a component.
457
458 The name may be None, indicating an anonymous component. The
459 targetNamespace is never None, though it could be an empty namespace. The
460 name and targetNamespace values are immutable after creation.
461
462 This class overrides the pickling behavior: when pickling a Namespace,
463 objects that do not belong to that namespace are pickled as references,
464 not as values. This ensures the uniqueness of objects when multiple
465 namespace definitions are pre-loaded.
466
467 This class must follow L{_SchemaComponent_mixin} in the MRO.
468 """
469
470 __PrivateTransient = set()
471
473 """Name of the component within its scope or namespace.
474
475 This is an NCName. The value isNone if the component is
476 anonymous. The attribute is immutable after the component is
477 created creation."""
478 return self.__name
479 __name = None
480
482 """Return true iff this instance is locally scoped (has no name)."""
483 return self.__name is None
484
486 # If this already has a name, keep using it.
487 if self.__anonymousName is not None:
488 return
489 assert self.__needAnonymousSupport()
490 assert namespace is not None
491 if self.bindingNamespace() is not None:
492 assert self.bindingNamespace() == namespace
493 if self.targetNamespace() is not None:
494 assert self.targetNamespace() == namespace
495 if anon_name is None:
496 anon_name = self.nameInBinding()
497 if anon_name is None:
498 anon_name = self.name()
499 if anon_name is None:
500 anon_name = 'ANON_IN_GROUP'
501 if unique_id is not None:
502 anon_name = '%s_%s' % (anon_name, unique_id)
503 anon_name = pyxb.utils.utility.MakeUnique(anon_name, set(namespace.categoryMap(self.__AnonymousCategory).keys()))
504 self.__anonymousName = anon_name
505 namespace.addCategoryObject(self.__AnonymousCategory, anon_name, self)
507 assert self.__anonymousName is not None, '%x %s %s in %s missing anonymous name' % (id(self), type(self), self.name(), self.targetNamespace())
508 return self.__anonymousName
509 __anonymousName = None
510
512 """The targetNamespace of a component.
513
514 This is None, or a reference to a Namespace in which the
515 component is declared (either as a global or local to one of
516 the namespace's complex type definitions). This is immutable
517 after creation.
518 """
519 return self.__targetNamespace
520 __targetNamespace = None
521
523 """The namespace in which this component's binding is placed."""
524 return self.__bindingNamespace
527 __bindingNamespace = None
528
530 """A map from template keys to component-specific values.
531
532 This is used in code generation to maintain unique names for accessor
533 methods, identifiers, keys, and other characteristics associated with
534 the code generated in support of the binding for this component."""
535 return self.__templateMap
536 __templateMap = None
537
538 __AnonymousCategory = pyxb.namespace.archive.NamespaceArchive._AnonymousCategory()
539
541 # If this component doesn't have a name, or if it's in some scope in
542 # which it cannot be located in a category map, we'll need a unique
543 # name for it.
544 return self.isAnonymous() or (self._scopeIsIndeterminate() and not isinstance(self, (AttributeGroupDefinition, ModelGroupDefinition)))
545
547 """Return the schema component from which this component was defined.
548
549 Needed so we can distinguish components that came from different
550 locations, since that imposes an external order dependency on them and
551 on cross-namespace inclusions.
552
553 @note: This characteristic is removed when the component is stored in
554 a namespace archive."""
555 return self.__schema
556 __schema = None
557 __PrivateTransient.add('schema')
558
560 if self.__needAnonymousSupport():
561 self._setAnonymousName(module_record.namespace(), unique_id=module_record.generationUID())
562 return getattr(super(_NamedComponent_mixin, self), '_prepareForArchive_csc', lambda *_args,**_kw: self)(module_record)
563
565 """Return C{True} if this component should be pickled by value in the
566 given namespace.
567
568 When pickling, a declaration component is considered to belong to the
569 namespace if it has a local scope which belongs to the namespace. In
570 that case, the declaration is a clone of something that does not
571 belong to the namespace; but the clone does.
572
573 @see: L{_bindsInNamespace}
574
575 @return: C{False} if the component should be pickled by reference.
576 """
577 if isinstance(self._scope(), ComplexTypeDefinition):
578 return self._scope()._picklesInArchive(archive)
579 assert not (self.targetNamespace() is None), '%s has no tns, scope %s, location %s, schema %s' % (self, self._scope(), self._location(), self._schema().targetNamespace())
580 assert not (self._objectOrigin() is None)
581 old_flag = (self.targetNamespace() in archive.namespaces())
582 new_flag = (self._objectOrigin().generationUID() == archive.generationUID())
583 #assert old_flag == new_flag, '%s %s %s %s' % (old_flag, new_flag, self, self._picklingReference())
584 return new_flag # old_flag # and new_flag
585
587 """Return C{True} if the binding for this component should be
588 generated in the given namespace.
589
590 This is the case when the component is in the given namespace. It's
591 also the case when the component has no associated namespace (but not
592 an absent namespace). Be aware that cross-namespace inheritance means
593 you will get references to elements in another namespace when
594 generating code for a subclass; that's fine, and those references
595 should not be generated locally.
596 """
597 return self.targetNamespace() in (ns, None)
598
600 """Return the L{pyxb.namespace.ExpandedName} of this object."""
601 if self.name() is None:
602 return None
603 return pyxb.namespace.ExpandedName(self.targetNamespace(), self.name())
604
606 """Pickling support.
607
608 Normally, we just create a new instance of this class.
609 However, if we're unpickling a reference in a loadable schema,
610 we need to return the existing component instance by looking
611 up the name in the component map of the desired namespace. We
612 can tell the difference because no normal constructors that
613 inherit from this have positional arguments; only invocations
614 by unpickling with a value returned in __getnewargs__ do.
615
616 This does require that the dependent namespace already have
617 been validated (or that it be validated here). That shouldn't
618 be a problem, except for the dependency loop resulting from
619 use of xml:lang in the XMLSchema namespace. For that issue,
620 see pyxb.namespace._XMLSchema.
621 """
622
623 if 0 == len(args):
624 rv = super(_NamedComponent_mixin, cls).__new__(cls)
625 return rv
626 ( object_reference, scope, icls ) = args
627
628 object_reference = _PickledAnonymousReference.FromPickled(object_reference)
629
630 # Explicitly validate here: the lookup operations won't do so,
631 # but will abort if the namespace hasn't been validated yet.
632 object_reference.validateComponentModel()
633 if isinstance(scope, (tuple, _PickledAnonymousReference)):
634 # Scope is the expanded name of the complex type in which the
635 # named value can be located.
636 scope_ref = _PickledAnonymousReference.FromPickled(scope)
637 if object_reference.namespace() != scope_ref.namespace():
638 scope_ref.validateComponentModel()
639 assert 'typeDefinition' in scope_ref.namespace().categories()
640 scope_ctd = scope_ref.typeDefinition()
641 if scope_ctd is None:
642 raise pyxb.SchemaValidationError('Unable to resolve local scope %s' % (scope_ref,))
643 if issubclass(icls, AttributeDeclaration):
644 rv = scope_ctd.lookupScopedAttributeDeclaration(object_reference)
645 elif issubclass(icls, ElementDeclaration):
646 rv = scope_ctd.lookupScopedElementDeclaration(object_reference)
647 else:
648 raise pyxb.IncompleteImplementationError('Scope %s reference lookup of %s not implemented for type %s' % (scope_ref, object_reference, icls))
649 if rv is None:
650 raise pyxb.SchemaValidationError('Unable to resolve %s as %s in scope %s' % (object_reference, icls, scope_ref))
651 elif _ScopedDeclaration_mixin.ScopeIsGlobal(scope) or _ScopedDeclaration_mixin.ScopeIsIndeterminate(scope):
652 if (issubclass(icls, SimpleTypeDefinition) or issubclass(icls, ComplexTypeDefinition)):
653 rv = object_reference.typeDefinition()
654 elif issubclass(icls, AttributeGroupDefinition):
655 rv = object_reference.attributeGroupDefinition()
656 elif issubclass(icls, ModelGroupDefinition):
657 rv = object_reference.modelGroupDefinition()
658 elif issubclass(icls, AttributeDeclaration):
659 rv = object_reference.attributeDeclaration()
660 elif issubclass(icls, ElementDeclaration):
661 rv = object_reference.elementDeclaration()
662 elif issubclass(icls, IdentityConstraintDefinition):
663 rv = object_reference.identityConstraintDefinition()
664 else:
665 raise pyxb.IncompleteImplementationError('Reference lookup of %s not implemented for type %s' % (object_reference, icls))
666 if rv is None:
667 raise pyxb.SchemaValidationError('Unable to resolve %s as %s' % (object_reference, icls))
668 else:
669 raise pyxb.IncompleteImplementationError('Unable to resolve reference %s, scope %s ns %s type %s, class %s' % (object_reference, scope, scope.targetNamespace(), type(scope), icls))
670 return rv
671
673 assert 0 == len(args)
674 name = kw.get('name')
675 # Must be None or a valid NCName
676 assert (name is None) or (0 > name.find(':')), 'name %s' % (name,)
677 self.__name = name
678
679 # Target namespace is taken from the context, unless somebody
680 # overrides it (as is done for local declarations if the form is
681 # unqualified).
682 self.__targetNamespace = kw.get('target_namespace', self._namespaceContext().targetNamespace())
683 self.__bindingNamespace = kw.get('binding_namespace')
684
685 self.__templateMap = {}
686
687 self.__schema = kw.get('schema')
688 assert self._schema() is not None
689
690 # Do parent invocations after we've set the name: they might need it.
691 super(_NamedComponent_mixin, self).__init__(*args, **kw)
692
694 """Return true iff this and the other component share the same name and target namespace.
695
696 Anonymous components are inherently name inequivalent, except to
697 themselves. This relies on equivalence as defined for
698 pyxb.namespace.ExpandedName, for which None is not equivalent to any
699 non-anonymous name."""
700 # Note that unpickled objects
701 return (self == other) or ((not self.isAnonymous()) and (self.expandedName() == other.expandedName()))
702
704 """Return True iff this and the other component have matching types.
705
706 It appears that name equivalence is used; two complex type definitions
707 with identical structures are not considered equivalent (at least, per
708 XMLSpy).
709 """
710 return (type(self) == type(other)) and self.isNameEquivalent(other)
711
713 """Return True iff this type can serve as a restriction of the other
714 type for the purposes of U{element consistency<http://www.w3.org/TR/xmlschema-1/#cos-element-consistent>}.
715
716 It appears that name equivalence is normally used; two complex type
717 definitions with identical structures are not considered equivalent
718 (at least, per XMLSpy). However, some OpenGIS standards demonstrate
719 that derivation by restriction from the other type is also acceptable.
720 That opens a whole can of worms; see
721 L{ElementDeclaration.isAdaptable}.
722 """
723 this = self
724 # can this succeed if component types are not equivalent?
725 while this is not None:
726 if this.isTypeEquivalent(other):
727 return True
728 print 'Checking %s against %s' % (this, other)
729 if not (this.isResolved() and other.isResolved()):
730 raise pyxb.IncompleteImplementationError('Oh fudge. Somebody violated the assumptions in ElementDeclaration.isAdaptable.')
731 if isinstance(self, ComplexTypeDefinition):
732 if self.DM_restriction != this.derivationMethod():
733 return False
734 elif isinstance(self, SimpleTypeDefinition):
735 if self._DA_restriction != this._derivationAlternative():
736 return False
737 else:
738 raise pyxb.IncompleteImplementationError('Need derivation consistency check for type %s' % (type(this),))
739 this = this.baseTypeDefinition()
740 if this.isUrTypeDefinition():
741 # Well, this certainly can't be a valid restriction of
742 # anything else.
743 break
744 return False
745
747 if self.__needAnonymousSupport():
748 #print 'Wrapping %s as anonymous %s in %s' % (self, self._anonymousName(), self.targetNamespace())
749 assert self._anonymousName() is not None
750 return _PickledAnonymousReference(self.targetNamespace(), self._anonymousName())
751 return self.expandedName().uriTuple()
752
754 if self.targetNamespace() is None:
755 return False
756 # Get the namespace we're pickling. If the namespace is None,
757 # we're not pickling; we're probably cloning, and in that case
758 # we don't want to use the reference state encoding.
759 pickling_archive = pyxb.namespace.archive.NamespaceArchive.PicklingArchive()
760 if pickling_archive is None:
761 return False
762 # If this thing is scoped in a complex type that belongs to the
763 # namespace being pickled, then it gets pickled as an object even if
764 # its target namespace isn't this one.
765 assert self._objectOrigin() is not None
766 if self._picklesInArchive(pickling_archive):
767 return False
768 # Note that anonymous objects must use their fallback
769 return True
770
772 if self.__pickleAsReference():
773 # NB: This instance may be a scoped declaration, but in
774 # this case (unlike getnewargs) we don't care about trying
775 # to look up a previous instance, so we don't need to
776 # encode the scope in the reference tuple.
777 return self._picklingReference()
778 if self.targetNamespace() is None:
779 # The only internal named objects that should exist are
780 # ones that have a non-global scope (including those with
781 # absent scope).
782 # @todo: this is wrong for schema that are not bound to a
783 # namespace, unless we use an unbound Namespace instance
784 #print type(self)
785 #assert isinstance(self, _ScopedDeclaration_mixin)
786 #assert self.SCOPE_global != self.scope()
787 # NOTE: The name of the scope may be None. This is not a
788 # problem unless somebody tries to extend or restrict the
789 # scope type, which at the moment I'm thinking is
790 # impossible for anonymous types. If it isn't, we're
791 # gonna need some other sort of ID, like a UUID associated
792 # with the anonymous class at the time it's written to the
793 # preprocessed schema file.
794 pass
795 return super(_NamedComponent_mixin, self).__getstate__()
796
798 """Pickling support.
799
800 If this instance is being pickled as a reference, provide the
801 arguments that are necessary so that the unpickler can locate
802 the appropriate component rather than create a duplicate
803 instance."""
804
805 if self.__pickleAsReference():
806 scope = self._scope()
807 if isinstance(self, _ScopedDeclaration_mixin):
808 # If scope is global, we can look it up in the namespace.
809 # If scope is indeterminate, this must be within a group in
810 # another namespace. Why are we serializing it?
811 # If scope is local, provide the namespace and name of
812 # the type that holds it
813 if self.SCOPE_global == self.scope():
814 pass
815 elif isinstance(self.scope(), ComplexTypeDefinition):
816 scope = self.scope()._picklingReference()
817 assert isinstance(scope, (tuple, _PickledAnonymousReference)), self
818 elif self._scopeIsIndeterminate():
819 # This is actually OK: we made sure both the scope and
820 # this instance can be looked up by a unique identifier.
821 pass
822 else:
823 raise pyxb.IncompleteImplementationError('pickling unrecognized scope %s type %s' % (self.scope(), type(self.scope())))
824 else:
825 assert isinstance(self, _NamedComponent_mixin), 'Pickling unnamed component %s in indeterminate scope by reference' % (self,)
826 assert not isinstance(scope, ComplexTypeDefinition), '%s %s %s %s' % (self, self.name(), scope, self._objectOrigin())
827
828 rv = ( self._picklingReference(), scope, self.__class__ )
829 return rv
830 return ()
831
833 if isinstance(state, tuple):
834 # We don't actually have to set any state here; we just
835 # make sure that we resolved to an already-configured
836 # instance.
837 assert self.targetNamespace() is not None
838 assert self.targetNamespace().uri() == state[0]
839 assert self.name() == state[1]
840 return
841 if isinstance(state, _PickledAnonymousReference):
842 assert self.targetNamespace() is not None
843 assert self.targetNamespace() == state.namespace()
844 assert self.__needAnonymousSupport()
845 assert self._anonymousName() == state.anonymousName()
846 return
847 self.__dict__.update(state)
848
850 self.__schema = None
851 rv = getattr(super(_NamedComponent_mixin, self), '_resetClone_csc', lambda *_args,**_kw: self)(**kw)
852 self.__templateMap = { }
853 origin = kw.get('origin')
854 self.__anonymousName = None
855 self._setObjectOrigin(origin, override=True)
856 return rv
857
859 """Mix-in indicating that the component contains a simple-type
860 value that may be constrained."""
861
862 VC_na = 0 #<<< No value constraint applies
863 VC_default = 1 #<<< Provided value constraint is default value
864 VC_fixed = 2 #<<< Provided value constraint is fixed value
865
866 # None, or a tuple containing a string followed by one of the VC_*
867 # values above.
868 __valueConstraint = None
870 """A constraint on the value of the attribute or element.
871
872 Either None, or a pair consisting of a string in the lexical
873 space of the typeDefinition and one of VC_default and
874 VC_fixed."""
875 return self.__valueConstraint
876
878 """If this instance constraints a default value, return that
879 value; otherwise return None."""
880 if not isinstance(self.__valueConstraint, tuple):
881 return None
882 if self.VC_default != self.__valueConstraint[1]:
883 return None
884 return self.__valueConstraint[0]
885
887 """If this instance constraints a fixed value, return that
888 value; otherwise return None."""
889 if not isinstance(self.__valueConstraint, tuple):
890 return None
891 if self.VC_fixed != self.__valueConstraint[1]:
892 return None
893 return self.__valueConstraint[0]
894
896 aval = NodeAttribute(node, 'default')
897 if aval is not None:
898 self.__valueConstraint = (aval, self.VC_default)
899 return self
900 aval = NodeAttribute(node, 'fixed')
901 if aval is not None:
902 self.__valueConstraint = (aval, self.VC_fixed)
903 return self
904 self.__valueConstraint = None
905 return self
906
908 """Mix-in class for named components that have a scope.
909
910 Scope is important when doing cross-namespace inheritance,
911 e.g. extending or restricting a complex type definition that is
912 from a different namespace. In this case, we will need to retain
913 a reference to the external component when the schema is
914 serialized.
915
916 This is done in the pickling process by including the scope when
917 pickling a component as a reference. The scope is the
918 SCOPE_global if global; otherwise, it is a tuple containing the
919 external namespace URI and the NCName of the complex type
920 definition in that namespace. We assume that the complex type
921 definition has global scope; otherwise, it should not have been
922 possible to extend or restrict it. (Should this be untrue, there
923 are comments in the code about a possible solution.)
924
925 @warning: This mix-in must follow L{_NamedComponent_mixin} in the C{mro}.
926 """
927
928 SCOPE_global = 'global' #<<< Marker to indicate global scope
929 XSCOPE_indeterminate = 'indeterminate' #<<< Marker to indicate scope has not been assigned
930
931 @classmethod
934
935 @classmethod
938
939 @classmethod
942
944 """Return True if this scope currently assigned to this instance is compatible with the given scope.
945
946 If either scope is indeterminate, presume they will ultimately be
947 compatible. Scopes that are equal are compatible, as is a local scope
948 if this already has a global scope."""
949 if self.ScopeIsIndeterminate(scope) or self.ScopeIsIndeterminate(self.scope()):
950 return True
951 if self.scope() == scope:
952 return True
953 return (self.SCOPE_global == self.scope()) and isinstance(scope, ComplexTypeDefinition)
954
955 # The scope for the element. Valid values are SCOPE_global or a
956 # complex type definition. None is an invalid value, but may
957 # appear if scope is determined by an ancestor component.
959 """The scope for the declaration.
960
961 Valid values are SCOPE_global, or a complex type definition.
962 A value of None means a non-global declaration that is not
963 owned by a complex type definition. These can only appear in
964 attribute group definitions or model group definitions.
965
966 @todo: For declarations in named model groups (viz., local
967 elements that aren't references), the scope needs to be set by
968 the owning complex type.
969 """
970 return self._scope()
971
972 # The base declaration is the original _ScopedDeclaration_mixin which
973 # introduced the element into its scope. This is used to retain a
974 # particular defining declaration when each extension type gets its own
975 # clone adapted for its scope.
976 __baseDeclaration = None
978 return self.__baseDeclaration or self
980 self.__baseDeclaration = referenced_declaration.baseDeclaration()
981 return self.__baseDeclaration
982
984 """This class represents an abstraction of the set of documents conformant
985 to a particle or particle term.
986
987 The abstraction of a given document is a map from element declarations
988 that can appear at the top level of the document to a boolean that is true
989 iff there could be multiple instances of that element declaration at the
990 top level of a valid document. The abstraction of the set is a list of
991 document abstractions.
992
993 This information is used in binding generation to determine whether a
994 field associated with a tag might need to hold multiple instances.
995 """
996
997 @classmethod
999 """Given two maps, return an updated map indicating the unified
1000 plurality."""
1001 umap = { }
1002 for k in set(map1.keys()).union(map2.keys()):
1003 if k in map1:
1004 umap[k] = (k in map2) or map1[k]
1005 else:
1006 umap[k] = map2[k]
1007 return umap
1008
1010 """Combine all the document abstractions into a single one that covers
1011 all possible documents.
1012
1013 The combined plurality is simply the elemental maximum over all
1014 document abstractions.
1015 """
1016
1017 combined_plurality = { }
1018 for pdm in self:
1019 for (ed, v) in pdm.items():
1020 if isinstance(ed, ElementDeclaration):
1021 assert ed.baseDeclaration() == ed
1022 combined_plurality[ed] = combined_plurality.get(ed, False) or v
1023 elif isinstance(ed, Wildcard):
1024 pass
1025 else:
1026 raise pyxb.LogicError('Unexpected plurality index %s' % (ed,))
1027 return combined_plurality
1028
1030 # Start by collecting the data for each of the particles.
1031 pdll = [ _PluralityData(_p) for _p in model_group.particles() ]
1032 #dumpmap = lambda _pdm: ', '.join( [ '%s: %s' % (_ed.expandedName(), _pl) for (_ed, _pl) in _pdm.items() ])
1033 #dumpmapset = lambda _pd: '(' + ') | ('.join([ dumpmap(_pdm) for _pdm in _pd ]) + ')'
1034 if (ModelGroup.C_CHOICE == model_group.compositor()):
1035 # Plurality for choice is simply any of the pluralities of the particles
1036 [ self.extend(_pd) for _pd in pdll ]
1037 elif ((ModelGroup.C_SEQUENCE == model_group.compositor()) or (ModelGroup.C_ALL == model_group.compositor())):
1038 # Sequence means all of them, in all their glory. All is treated
1039 # the same way. Essentially this is a pointwise OR of the
1040 # pluralities of the particles.
1041 if 0 < len(pdll):
1042 new_pd = pdll.pop()
1043 for pd in pdll:
1044 assert 0 < len(pd)
1045 assert 0 < len(new_pd)
1046 stage_pd = [ ]
1047 for pdm1 in new_pd:
1048 for pdm2 in pd:
1049 stage_pd.append(self._MapUnion(pdm1, pdm2))
1050 new_pd = stage_pd
1051 self.extend(new_pd)
1052 else:
1053 raise pyxb.LogicError('Unrecognized compositor value %s' % (model_group.compositor(),))
1054
1056 assert particle.isResolved()
1057 pd = particle.term().pluralityData()
1058
1059 # If the particle can't appear at all, there are no results.
1060 if 0 == particle.maxOccurs():
1061 return
1062
1063 # If the particle can only occur once, it has no effect on the
1064 # pluralities; use the term to identify them
1065 if 1 == particle.maxOccurs():
1066 self.__setFromComponent(particle.term())
1067 return
1068
1069 # If there are multiple alternatives, assume they are all
1070 # taken. Do this by creating a map that treats every possible
1071 # element as appearing multiple times.
1072 true_map = {}
1073 pd = _PluralityData(particle.term())
1074 while 0 < len(pd):
1075 pdm = pd.pop()
1076 [ true_map.setdefault(_k, True) for _k in pdm.keys() ]
1077 self.append(true_map)
1078
1080 del self[:]
1081 if isinstance(component, ElementDeclaration):
1082 assert component.isResolved()
1083 assert isinstance(component.baseDeclaration(), ElementDeclaration)
1084 self.append( { component.baseDeclaration(): False } )
1085 #self.append( { component: False } )
1086 elif isinstance(component, ModelGroup):
1087 self.__fromModelGroup(component)
1088 elif isinstance(component, Particle):
1089 self.__fromParticle(component)
1090 elif isinstance(component, Wildcard):
1091 pass
1092 elif component is not None:
1093 raise pyxb.IncompleteImplementationError("No support for plurality of component type %s" % (type(component),))
1094 # Elements get lost if there's a result set that doesn't have any
1095 # documents in it.
1096 if 0 == len(self):
1097 self.append({})
1098
1102
1104 """Support for components that accept attribute wildcards.
1105
1106 That is L{AttributeGroupDefinition} and L{ComplexTypeDefinition}. The
1107 calculations of the appropriate wildcard are sufficiently complex that
1108 they need to be abstracted out to a mix-in class."""
1109
1110 # Optional wildcard that constrains attributes
1111 __attributeWildcard = None
1112
1114 """Return the L{Wildcard} component associated with attributes of this
1115 instance, or C{None} if attribute wildcards are not present in the
1116 instance."""
1117 return self.__attributeWildcard
1118
1120 """Set the attribute wildcard property for this instance."""
1121 assert (attribute_wildcard is None) or isinstance(attribute_wildcard, Wildcard)
1122 self.__attributeWildcard = attribute_wildcard
1123 return self
1124
1126 """Return the nodes that are relevant for attribute processing.
1127
1128 @param node_list: A sequence of nodes found in a definition content
1129 information item.
1130
1131 @return: A tuple C{( attributes, attributeGroups, attributeWildcard)}
1132 where C{attributes} is the subsequence of C{node_list} that are
1133 XMLSchema C{attribute} nodes; C{attributeGroups} is analogous; and
1134 C{attributeWildcard} is a single DOM node with XMLSchema name
1135 C{anyAttribute} (or C{None}, if no such node is present in the list).
1136
1137 @raise pyxb.SchemaValidationError: An C{attributeGroup} node is
1138 present but does not have the required C{ref} attribute.
1139 @raise pyxb.SchemaValidationError: Multiple C{anyAttribute} nodes are
1140 identified.
1141 """
1142
1143 attributes = []
1144 attribute_groups = []
1145 any_attribute = None
1146 # Handle clauses 1 and 2 (common between simple and complex types)
1147 for node in node_list:
1148 if Node.ELEMENT_NODE != node.nodeType:
1149 continue
1150 if xsd.nodeIsNamed(node, 'attribute'):
1151 # Note: This attribute use instance may have use=prohibited
1152 attributes.append(node)
1153 elif xsd.nodeIsNamed(node, 'attributeGroup'):
1154 # This must be an attributeGroupRef
1155 agd_attr = NodeAttribute(node, 'ref')
1156 if agd_attr is None:
1157 raise pyxb.SchemaValidationError('Require ref attribute on internal attributeGroup elements')
1158 attribute_groups.append(agd_attr)
1159 elif xsd.nodeIsNamed(node, 'anyAttribute'):
1160 if any_attribute is not None:
1161 raise pyxb.SchemaValidationError('Multiple anyAttribute children are not allowed')
1162 any_attribute = node
1163
1164 return (attributes, attribute_groups, any_attribute)
1165
1166 @classmethod
1168 """Implement the algorithm as described the
1169 U{specification<http://www.w3.org/TR/xmlschema-1/#declare-type>}.
1170
1171 @param namespace_context: The L{pyxb.namespace.NamespaceContext} to be
1172 associated with any created L{Wildcard} instance
1173 @param attribute_groups: A list of L{AttributeGroupDefinition} instances
1174 @param local_wildcard: A L{Wildcard} instance computed from a relevant
1175 XMLSchema C{anyAttribute} element, or C{None} if no attribute wildcard
1176 is relevant
1177 """
1178
1179 # Non-absent wildcard properties of attribute groups
1180 agd_wildcards = []
1181 for agd in attribute_groups:
1182 assert isinstance(agd, AttributeGroupDefinition)
1183 if agd.attributeWildcard() is not None:
1184 agd_wildcards.append(agd.attributeWildcard())
1185 agd_constraints = [ _agd.namespaceConstraint() for _agd in agd_wildcards ]
1186
1187 # Clause 2.1
1188 if 0 == len(agd_wildcards):
1189 return local_wildcard
1190
1191 if local_wildcard is not None:
1192 # Clause 2.2.1
1193 return Wildcard(process_contents=local_wildcard.processContents(),
1194 namespace_constraint=Wildcard.IntensionalIntersection(agd_constraints + [local_wildcard.namespaecConstraint()]),
1195 annotation=local_wildcard.annotation(),
1196 namespace_context=namespace_context)
1197 # Clause 2.2.2
1198 return Wildcard(process_contents=agd_wildcards[0].processContents(),
1199 namespace_constraint=Wildcard.IntensionalIntersection(agd_constraints),
1200 namespace_context=namespace_context)
1201
1202 -class AttributeDeclaration (_SchemaComponent_mixin, _NamedComponent_mixin, pyxb.namespace.resolution._Resolvable_mixin, _Annotated_mixin, _ValueConstraint_mixin, _ScopedDeclaration_mixin):
1203 """An XMLSchema U{Attribute Declaration<http://www.w3.org/TR/xmlschema-1/#cAttribute_Declarations>} component.
1204 """
1205
1206 # The STD to which attribute values must conform
1207 __typeDefinition = None
1209 """The simple type definition to which an attribute value must
1210 conform."""
1211 return self.__typeDefinition
1212
1216
1218 if self.typeDefinition():
1219 return 'AD[%s:%s]' % (self.name(), self.typeDefinition().expandedName())
1220 return 'AD[%s:?]' % (self.expandedName(),)
1221
1222 @classmethod
1224 """Create an attribute declaration component for a specified namespace."""
1225 kw = { 'name' : name,
1226 'schema' : schema,
1227 'namespace_context' : schema.targetNamespace().initialNamespaceContext(),
1228 'scope' : _ScopedDeclaration_mixin.SCOPE_global }
1229 assert schema is not None
1230 bi = cls(**kw)
1231 if std is not None:
1232 bi.__typeDefinition = std
1233 bi.__typeAttribute = None
1234 return bi
1235
1236 # CFD:AD CFD:AttributeDeclaration
1237 @classmethod
1239 """Create an attribute declaration from the given DOM node.
1240
1241 wxs is a Schema instance within which the attribute is being
1242 declared.
1243
1244 node is a DOM element. The name must be one of ( 'all',
1245 'choice', 'sequence' ), and the node must be in the XMLSchema
1246 namespace.
1247
1248 scope is the _ScopeDeclaration_mxin context into which the
1249 attribute declaration is placed. It can be SCOPE_global, a
1250 complex type definition, or XSCOPE_indeterminate if this is an
1251 anonymous declaration within an attribute group. It is a
1252 required parameter for this function.
1253 """
1254
1255 scope = kw['scope']
1256 assert _ScopedDeclaration_mixin.ScopeIsIndeterminate(scope) or _ScopedDeclaration_mixin.IsValidScope(scope)
1257
1258 # Node should be an XMLSchema attribute node
1259 assert xsd.nodeIsNamed(node, 'attribute')
1260
1261 name = NodeAttribute(node, 'name')
1262
1263 # Implement per section 3.2.2
1264 if xsd.nodeIsNamed(node.parentNode, 'schema'):
1265 assert cls.SCOPE_global == scope
1266 elif NodeAttribute(node, 'ref') is None:
1267 # This is an anonymous declaration within an attribute use
1268 assert _ScopedDeclaration_mixin.ScopeIsIndeterminate(scope) or isinstance(scope, ComplexTypeDefinition)
1269 else:
1270 raise pyxb.SchemaValidationError('Internal attribute declaration by reference')
1271
1272 rv = cls(name=name, node=node, **kw)
1273 rv._annotationFromDOM(node)
1274 rv._valueConstraintFromDOM(node)
1275
1276 rv.__typeAttribute = NodeAttribute(node, 'type')
1277
1278 kw.pop('node', None)
1279 kw['owner'] = rv
1280
1281 st_node = LocateUniqueChild(node, 'simpleType')
1282 if st_node is not None:
1283 rv.__typeDefinition = SimpleTypeDefinition.CreateFromDOM(st_node, **kw)
1284 elif rv.__typeAttribute is None:
1285 rv.__typeDefinition = SimpleTypeDefinition.SimpleUrTypeDefinition()
1286
1287 if rv.__typeDefinition is None:
1288 rv._queueForResolution('creation')
1289 return rv
1290
1292 return self.__typeDefinition is not None
1293
1294 # res:AD res:AttributeDeclaration
1296 if self.isResolved():
1297 return self
1298
1299 # Although the type definition may not be resolved, *this* component
1300 # is resolved, since we don't look into the type definition for anything.
1301 assert self.__typeAttribute is not None, 'AD %s is unresolved but has no typeAttribute field' % (self.expandedName(),)
1302 type_en = self._namespaceContext().interpretQName(self.__typeAttribute)
1303 self.__typeDefinition = type_en.typeDefinition()
1304 if self.__typeDefinition is None:
1305 raise pyxb.SchemaValidationError('Type reference %s cannot be found' % (type_en,))
1306 if not isinstance(self.__typeDefinition, SimpleTypeDefinition):
1307 raise pyxb.SchemaValidationError('Need %s to be a simple type' % (type_ln,))
1308
1309 return self
1310
1312 """Override fields in this instance with those from the other.
1313
1314 This method is invoked only by Schema._addNamedComponent, and
1315 then only when a built-in type collides with a schema-defined
1316 type. Material like facets is not (currently) held in the
1317 built-in copy, so the DOM information is copied over to the
1318 built-in STD, which is subsequently re-resolved.
1319
1320 Returns self.
1321 """
1322 assert self != other
1323 assert self.name() is not None
1324 assert self.isNameEquivalent(other)
1325 super(AttributeDeclaration, self)._updateFromOther_csc(other)
1326
1327 # The other STD should be an unresolved schema-defined type.
1328 # Mark this instance as unresolved so it is re-examined
1329 if not other.isResolved():
1330 if pyxb.namespace.BuiltInObjectUID == self._objectOrigin().generationUID():
1331 #assert self.isResolved(), 'Built-in %s is not resolved' % (self.expandedName(),)
1332 print '**!!**!! Not destroying builtin %s: %s' % (self.expandedName(), self.__typeDefinition)
1333 else:
1334 self.__typeDefinition = None
1335 return self
1336
1337 # bR:AD
1339 """Attribute declarations require their type."""
1340 return frozenset([ self.__typeDefinition ])
1341
1342 -class AttributeUse (_SchemaComponent_mixin, pyxb.namespace.resolution._Resolvable_mixin, _ValueConstraint_mixin):
1343 """An XMLSchema U{Attribute Use<http://www.w3.org/TR/xmlschema-1/#cAttribute_Use>} component."""
1344
1345 # How this attribute can be used. The component property
1346 # "required" is true iff the value is USE_required.
1347 __use = None
1348
1349 USE_required = 0x01 #<<< The attribute is required
1350 USE_optional = 0x02 #<<< The attribute may or may not appear
1351 USE_prohibited = 0x04 #<<< The attribute must not appear
1354
1355 __restrictionOf = None
1357 return self.__restrictionOf
1359 assert isinstance(au, AttributeUse)
1360 # Might re-assign if had to suspend resolution
1361 assert (self.__restrictionOf is None) or (self.__restrictionOf == au)
1362 self.__restrictionOf = au
1363
1364 # A reference to an AttributeDeclaration
1366 """The attribute declaration for this use.
1367
1368 When the use scope is assigned, the declaration is cloned (if
1369 necessary) so that each declaration corresponds to only one use. We
1370 rely on this in code generation, because the template map for the use
1371 is stored in its declaration."""
1372 return self.__attributeDeclaration
1373 __attributeDeclaration = None
1374
1375 # Define so superclasses can take keywords
1378
1380 """Return the subset of au_set for which the use names match this use."""
1381
1382 if not self.isResolved():
1383 return None
1384 this_ad = self.attributeDeclaration()
1385 rv = set()
1386 for au in au_set:
1387 if not au.isResolved():
1388 return None
1389 that_ad = au.attributeDeclaration()
1390 if this_ad.isNameEquivalent(that_ad):
1391 rv.add(au)
1392 return rv
1393
1394 @classmethod
1396 kw = { 'schema' : schema,
1397 'namespace_context' : schema.targetNamespace().initialNamespaceContext() }
1398 bi = cls(**kw)
1399 assert isinstance(attribute_declaration, AttributeDeclaration)
1400 bi.__attributeDeclaration = attribute_declaration
1401 bi.__use = use
1402 return bi
1403
1404 # CFD:AU CFD:AttributeUse
1405 @classmethod
1407 """Create an Attribute Use from the given DOM node.
1408
1409 wxs is a Schema instance within which the attribute use is
1410 being defined.
1411
1412 node is a DOM element. The name must be 'attribute', and the
1413 node must be in the XMLSchema namespace.
1414
1415 scope is the _ScopeDeclaration_mixin context into which any
1416 required anonymous attribute declaration is put. This must be
1417 a complex type definition, or None if this use is in an
1418 attribute group.
1419 """
1420
1421 scope = kw['scope']
1422 assert _ScopedDeclaration_mixin.ScopeIsIndeterminate(scope) or isinstance(scope, ComplexTypeDefinition)
1423 assert xsd.nodeIsNamed(node, 'attribute')
1424 schema = kw['schema']
1425 rv = cls(node=node, **kw)
1426
1427 rv.__use = cls.USE_optional
1428 use = NodeAttribute(node, 'use')
1429 if use is not None:
1430 if 'required' == use:
1431 rv.__use = cls.USE_required
1432 elif 'optional' == use:
1433 rv.__use = cls.USE_optional
1434 elif 'prohibited' == use:
1435 rv.__use = cls.USE_prohibited
1436 else:
1437 raise pyxb.SchemaValidationError('Unexpected value %s for attribute use attribute' % (use,))
1438
1439 rv._valueConstraintFromDOM(node)
1440
1441 rv.__refAttribute = NodeAttribute(node, 'ref')
1442 if rv.__refAttribute is None:
1443 # Create an anonymous declaration
1444 kw.pop('node', None)
1445 kw['owner'] = rv
1446 kw['target_namespace'] = schema.targetNamespaceForNode(node, AttributeDeclaration)
1447 rv.__attributeDeclaration = AttributeDeclaration.CreateFromDOM(node, **kw)
1448
1449 if not rv.isResolved():
1450 rv._queueForResolution('creation')
1451
1452 return rv
1453
1455 return self.__attributeDeclaration is not None
1456
1458 if self.isResolved():
1459 return self
1460 ad_en = self._namespaceContext().interpretQName(self.__refAttribute)
1461 self.__attributeDeclaration = ad_en.attributeDeclaration()
1462 if self.__attributeDeclaration is None:
1463 raise pyxb.SchemaValidationError('Attribute declaration %s cannot be found' % (ad_en,))
1464
1465 assert isinstance(self.__attributeDeclaration, AttributeDeclaration)
1466
1467 return self
1468
1469 # bR:AU
1471 """Attribute uses require their declarations, but only if lax."""
1472 if not include_lax:
1473 return frozenset()
1474 return frozenset([ self.attributeDeclaration() ])
1475
1476 # aFS:AU
1478 """Adapt this instance for the given complex type.
1479
1480 If the attribute declaration for this use is not associated with a
1481 complex type definition, then associate a clone of it with this CTD,
1482 and clone a new attribute use that uses the associated declaration.
1483 This attribute use is then inherited by extensions and restrictions,
1484 while retaining its original scope."""
1485 rv = self
1486 assert self.isResolved()
1487 ad = self.__attributeDeclaration
1488 assert ad.scope() is not None
1489 assert isinstance(ctd, ComplexTypeDefinition)
1490 if not isinstance(ad.scope(), ComplexTypeDefinition):
1491 rv = self._clone(ctd, ctd._objectOrigin())
1492 rv.__attributeDeclaration = ad._clone(rv, ctd._objectOrigin())
1493 rv.__attributeDeclaration._setScope(ctd)
1494 ctd._recordLocalDeclaration(rv.__attributeDeclaration)
1495 return rv
1496
1498 return 'AU[%s]' % (self.attributeDeclaration(),)
1499
1500
1501 -class ElementDeclaration (_SchemaComponent_mixin, _NamedComponent_mixin, pyxb.namespace.resolution._Resolvable_mixin, _Annotated_mixin, _ValueConstraint_mixin, _ScopedDeclaration_mixin):
1502 """An XMLSchema U{Element Declaration<http://www.w3.org/TR/xmlschema-1/#cElement_Declarations>} component."""
1503
1504 # Simple or complex type definition
1505 __typeDefinition = None
1507 """The simple or complex type to which the element value conforms."""
1508 return self.__typeDefinition
1512
1513 __nillable = False
1515
1516 __identityConstraintDefinitions = None
1518 """A list of IdentityConstraintDefinition instances."""
1519 return self.__identityConstraintDefinitions
1520
1521 __substitutionGroupAffiliation = None
1523 """None, or a reference to an ElementDeclaration."""
1524 return self.__substitutionGroupAffiliation
1525
1526 SGE_none = 0 #<<< No substitution group exclusion specified
1527 SGE_extension = 0x01 #<<< Substitution by an extension of the base type
1528 SGE_restriction = 0x02 #<<< Substitution by a restriction of the base type
1529 SGE_substitution = 0x04 #<<< Substitution by replacement (?)
1530
1531 _SGE_Map = { 'extension' : SGE_extension
1532 , 'restriction' : SGE_restriction }
1533 _DS_Map = _SGE_Map.copy()
1534 _DS_Map.update( { 'substitution' : SGE_substitution } )
1535
1536 # Subset of SGE marks formed by bitmask. SGE_substitution is disallowed.
1537 __substitutionGroupExclusions = SGE_none
1538
1539 # Subset of SGE marks formed by bitmask
1540 __disallowedSubstitutions = SGE_none
1541
1542 __abstract = False
1544
1546 """Return the plurality information for this component.
1547
1548 An ElementDeclaration produces one instance of a single element."""
1549 return _PluralityData(self)
1550
1554
1555 # bR:ED
1557 """Element declarations depend on the type definition of their
1558 content."""
1559 return frozenset([self.__typeDefinition])
1560
1563
1564 # CFD:ED CFD:ElementDeclaration
1565 @classmethod
1567 """Create an element declaration from the given DOM node.
1568
1569 wxs is a Schema instance within which the element is being
1570 declared.
1571
1572 scope is the _ScopeDeclaration_mixin context into which the
1573 element declaration is recorded. It can be SCOPE_global, a
1574 complex type definition, or None in the case of elements
1575 declared in a named model group.
1576
1577 node is a DOM element. The name must be 'element', and the
1578 node must be in the XMLSchema namespace."""
1579
1580 scope = kw['scope']
1581 assert _ScopedDeclaration_mixin.ScopeIsIndeterminate(scope) or _ScopedDeclaration_mixin.IsValidScope(scope)
1582
1583 # Node should be an XMLSchema element node
1584 assert xsd.nodeIsNamed(node, 'element')
1585
1586 # Might be top-level, might be local
1587 name = NodeAttribute(node, 'name')
1588 if xsd.nodeIsNamed(node.parentNode, 'schema'):
1589 assert _ScopedDeclaration_mixin.SCOPE_global == scope
1590 elif NodeAttribute(node, 'ref') is None:
1591 # Scope may be None or a CTD.
1592 assert _ScopedDeclaration_mixin.ScopeIsIndeterminate(scope) or isinstance(scope, ComplexTypeDefinition)
1593 else:
1594 raise pyxb.SchemaValidationError('Created reference as element declaration')
1595
1596 rv = cls(name=name, node=node, **kw)
1597 rv._annotationFromDOM(node)
1598 rv._valueConstraintFromDOM(node)
1599
1600 rv.__substitutionGroupAttribute = NodeAttribute(node, 'substitutionGroup')
1601
1602 kw.pop('node', None)
1603 kw['owner'] = rv
1604
1605 identity_constraints = []
1606 for cn in node.childNodes:
1607 if (Node.ELEMENT_NODE == cn.nodeType) and xsd.nodeIsNamed(cn, 'key', 'unique', 'keyref'):
1608 identity_constraints.append(IdentityConstraintDefinition.CreateFromDOM(cn, **kw))
1609 rv.__identityConstraintDefinitions = identity_constraints
1610
1611 rv.__typeDefinition = None
1612 rv.__typeAttribute = NodeAttribute(node, 'type')
1613 simpleType_node = LocateUniqueChild(node, 'simpleType')
1614 complexType_node = LocateUniqueChild(node, 'complexType')
1615 if rv.__typeAttribute is not None:
1616 if (simpleType_node is not None) and (complexType_node is not None):
1617 raise pyxb.SchemaValidationError('Cannot combine type attribute with simpleType or complexType child')
1618 if (rv.__typeDefinition is None) and (simpleType_node is not None):
1619 rv.__typeDefinition = SimpleTypeDefinition.CreateFromDOM(simpleType_node, **kw)
1620 if (rv.__typeDefinition is None) and (complexType_node is not None):
1621 rv.__typeDefinition = ComplexTypeDefinition.CreateFromDOM(complexType_node, **kw)
1622 if rv.__typeDefinition is None:
1623 if rv.__typeAttribute is None:
1624 # Scan for particle types which were supposed to be enclosed in a complexType
1625 for cn in node.childNodes:
1626 if Particle.IsParticleNode(cn):
1627 raise pyxb.SchemaValidationError('Node %s in element must be wrapped by complexType.' % (cn.localName,))
1628 rv.__typeDefinition = ComplexTypeDefinition.UrTypeDefinition()
1629 rv.__isResolved = (rv.__typeDefinition is not None) and (rv.__substitutionGroupAttribute is None)
1630 if not rv.__isResolved:
1631 rv._queueForResolution('creation')
1632
1633 attr_val = NodeAttribute(node, 'nillable')
1634 if attr_val is not None:
1635 rv.__nillable = datatypes.boolean(attr_val)
1636
1637 attr_val = NodeAttribute(node, 'abstract')
1638 if attr_val is not None:
1639 rv.__abstract = datatypes.boolean(attr_val)
1640
1641 schema = kw['schema']
1642 rv.__disallowedSubstitutions = schema.blockForNode(node, cls._DS_Map)
1643 rv.__substitutionGroupExclusions = schema.finalForNode(node, cls._SGE_Map)
1644
1645 return rv
1646
1648 """Determine whether this element declaration is adaptable.
1649
1650 OK, this gets ugly. First, if this declaration isn't resolved, it's
1651 clearly not adaptable.
1652
1653 Now: For it to be adaptable, we must know enough about its type to
1654 verify that it is derivation-consistent with any other uses of the
1655 same name in the same complex type. If the element's type is
1656 resolved, that's good enough.
1657
1658 If the element's type isn't resolved, we're golden as long as
1659 type-equivalent types were used. But it's also allowed for the
1660 derived ctd to use the element name constraining it to a derivation of
1661 the element base type. (Go see namespace
1662 http://www.opengis.net/ows/1.1 types PositionType, PositionType2D,
1663 BoundingBox, and WGS84BoundingBox for an example). So, we really do
1664 have to have the element's type resolved.
1665
1666 Except that if a CTD's content incorporates an element with the same
1667 type as the CTD (i.e., nested), this will never happen, because the
1668 CTD can't get resolved until after it has been resolved.
1669 (Go see {http://www.opengis.net/ows/1.1}ContentsBaseType and
1670 {http://www.opengis.net/ows/1.1}DatasetDescriptionSummaryBaseType for
1671 an example).
1672
1673 So, we give the world a break and assume that if the type we're trying
1674 to resolve is the same as the type of an element in that type, then
1675 the element type will be resolved by the point it's needed. In point
1676 of fact, it won't, but we'll only notice that if a CTD contains an
1677 element whose type is a restriction of the CTD. In that case,
1678 isDerivationConsistent will blow chunks and somebody'll have to come
1679 back and finish up this mess.
1680 """
1681
1682 if not self.isResolved():
1683 return False
1684 if self.typeDefinition().isResolved():
1685 return True
1686 # Aw, dammit. See if we're gonna need the type resolved before we can
1687 # adapt this thing.
1688 existing_decl = ctd.lookupScopedElementDeclaration(self.expandedName())
1689 if existing_decl is None:
1690 # Nobody else has this name, so we don't have to check for
1691 # consistency.
1692 return True
1693 # OK, we've got a name clash. Are the two types trivially equivalent?
1694 if self.typeDefinition().isTypeEquivalent(existing_decl.typeDefinition()):
1695 # Yes! Go for it.
1696 return True
1697 # No. Can't proceed until the type definition is resolved. Hope it
1698 # can be....
1699 print 'WARNING: Require %s to be resolved; might be a loop.' % (self.typeDefinition(),)
1700 return False
1701
1702 # aFS:ED
1704 #print 'aFS:ED %s %s old scope %s' % (self.expandedName(), ctd.expandedName(), self.scope())
1705 rv = self
1706 assert isinstance(ctd, ComplexTypeDefinition), '%s is not a CTD' % (ctd,)
1707 if not isinstance(self.scope(), ComplexTypeDefinition):
1708 assert owner is not None
1709 rv = self._clone(owner, ctd._objectOrigin())
1710 rv._setScope(ctd)
1711 ctd._recordLocalDeclaration(rv)
1712 return rv
1713
1714 __isResolved = False
1716 return self.__isResolved
1717
1718 # res:ED res:ElementDeclaration
1720 if self.isResolved():
1721 return self
1722
1723 #if self._scopeIsIndeterminate():
1724 # print 'WARNING: Resolving ED %s with indeterminate scope (is this a problem?)' % (self.expandedName(),)
1725 if self.__substitutionGroupAttribute is not None:
1726 sg_en = self._namespaceContext().interpretQName(self.__substitutionGroupAttribute)
1727 sga = sg_en.elementDeclaration()
1728 if sga is None:
1729 raise pyxb.SchemaValidationError('Element declaration refers to unrecognized substitution group %s' % (sg_en,))
1730 self.__substitutionGroupAffiliation = sga
1731
1732 if self.__typeDefinition is None:
1733 assert self.__typeAttribute is not None
1734 type_en = self._namespaceContext().interpretQName(self.__typeAttribute)
1735 self.__typeDefinition = type_en.typeDefinition()
1736 if self.__typeDefinition is None:
1737 raise pyxb.SchemaValidationError('Type declaration %s cannot be found' % (type_en,))
1738
1739 self.__isResolved = True
1740 return self
1741
1743 if self.typeDefinition() is not None:
1744 return 'ED[%s:%s]' % (self.name(), self.typeDefinition().name())
1745 return 'ED[%s:?]' % (self.name(),)
1746
1747
1748 -class ComplexTypeDefinition (_SchemaComponent_mixin, _NamedComponent_mixin, pyxb.namespace.resolution._Resolvable_mixin, _Annotated_mixin, _AttributeWildcard_mixin):
1749 __PrivateTransient = set()
1750
1751 # The type resolved from the base attribute.
1752 __baseTypeDefinition = None
1756
1757 DM_empty = 0 #<<< No derivation method specified
1758 DM_extension = 0x01 #<<< Derivation by extension
1759 DM_restriction = 0x02 #<<< Derivation by restriction
1760
1761 _DM_Map = { 'extension' : DM_extension
1762 , 'restriction' : DM_restriction }
1763
1764 # How the type was derived (a DM_* value)
1765 # (This field is used to identify unresolved definitions.)
1766 __derivationMethod = None
1770
1771 # Derived from the final and finalDefault attributes
1772 __final = DM_empty
1773
1774 # Derived from the abstract attribute
1775 __abstract = False
1777
1778 # A frozenset() of AttributeUse instances.
1779 __attributeUses = None
1783
1784 # A map from NCNames to AttributeDeclaration instances that are
1785 # local to this type.
1786 __scopedAttributeDeclarations = None
1788 """Find an attribute declaration with the given name that is local to this type.
1789
1790 Returns None if there is no such local attribute declaration."""
1791 if self.__scopedAttributeDeclarations is None:
1792 return None
1793 return self.__scopedAttributeDeclarations.get(expanded_name)
1794
1795 # A map from NCNames to ElementDeclaration instances that are
1796 # local to this type.
1797 __scopedElementDeclarations = None
1799 """Find an element declaration with the given name that is local to this type.
1800
1801 Returns None if there is no such local element declaration."""
1802 if self.__scopedElementDeclarations is None:
1803 return None
1804 return self.__scopedElementDeclarations.get(expanded_name)
1805
1806 __localScopedDeclarations = None
1808 """Return a list of element and attribute declarations that were
1809 introduced in this definition (i.e., their scope is this CTD).
1810
1811 @note: This specifically returns a list, with element declarations
1812 first, because name binding should privilege the elements over the
1813 attributes. Within elements and attributes, the components are sorted
1814 by expanded name, to ensure consistency across a series of binding
1815 generations.
1816
1817 @keyword reset: If C{False} (default), a cached previous value (if it
1818 exists) will be returned.
1819 """
1820 if reset or (self.__localScopedDeclarations is None):
1821 rve = [ _ed for _ed in self.__scopedElementDeclarations.values() if (self == _ed.scope()) ]
1822 rve.sort(lambda _a, _b: cmp(_a.expandedName(), _b.expandedName()))
1823 rva = [ _ad for _ad in self.__scopedAttributeDeclarations.values() if (self == _ad.scope()) ]
1824 rva.sort(lambda _a, _b: cmp(_a.expandedName(), _b.expandedName()))
1825 self.__localScopedDeclarations = rve
1826 self.__localScopedDeclarations.extend(rva)
1827 return self.__localScopedDeclarations
1828
1830 """Record the given declaration as being locally scoped in
1831 this type."""
1832 assert isinstance(decl, _ScopedDeclaration_mixin)
1833 if isinstance(decl, ElementDeclaration):
1834 scope_map = self.__scopedElementDeclarations
1835 elif isinstance(decl, AttributeDeclaration):
1836 scope_map = self.__scopedAttributeDeclarations
1837 else:
1838 raise pyxb.LogicError('Unexpected instance of %s recording as local declaration' % (type(decl),))
1839 decl_en = decl.expandedName()
1840 existing_decl = scope_map.setdefault(decl_en, decl)
1841 if decl != existing_decl:
1842 if isinstance(decl, ElementDeclaration):
1843 # Test cos-element-consistent
1844 existing_type = existing_decl.typeDefinition()
1845 pending_type = decl.typeDefinition()
1846 if not pending_type.isDerivationConsistent(existing_type):
1847 raise pyxb.SchemaValidationError('Conflicting element declarations for %s: existing %s versus new %s' % (decl.expandedName(), existing_type, pending_type))
1848 elif isinstance(decl, AttributeDeclaration):
1849 raise pyxb.SchemaValidationError('Multiple attribute declarations for %s' % (decl.expandedName(),))
1850 else:
1851 assert False, 'Unrecognized type %s' % (type(decl),)
1852 decl._baseDeclaration(existing_decl)
1853 return self
1854
1856 """Return C{True} iff this is the root of a complex type definition hierarchy.
1857 """
1858 base = self.__baseTypeDefinition
1859 return isinstance(base, SimpleTypeDefinition) or base.isUrTypeDefinition()
1860
1861 CT_EMPTY = 'EMPTY' #<<< No content
1862 CT_SIMPLE = 'SIMPLE' #<<< Simple (character) content
1863 CT_MIXED = 'MIXED' #<<< Children may be elements or other (e.g., character) content
1864 CT_ELEMENT_ONLY = 'ELEMENT_ONLY' #<<< Expect only element content.
1865
1867 """Return the value of the content type identifier, i.e. one of the
1868 CT_ constants. Return value is None if no content type has been
1869 defined."""
1870 if isinstance(self.__contentType, tuple):
1871 return self.__contentType[0]
1872 return self.__contentType
1873
1878
1879 # Identify the sort of content in this type.
1880 __contentType = None
1882 """Identify the sort of content in this type.
1883
1884 Valid values are:
1885 - C{CT_EMPTY}
1886 - ( C{CT_SIMPLE}, a L{SimpleTypeDefinition} instance )
1887 - ( C{CT_MIXED}, a L{Particle} instance )
1888 - ( C{CT_ELEMENT_ONLY}, a L{Particle} instance )
1889 """
1890 return self.__contentType
1891
1893 if self.CT_EMPTY == self.contentType():
1894 return 'EMPTY'
1895 ( tag, particle ) = self.contentType()
1896 if self.CT_SIMPLE == tag:
1897 return 'Simple [%s]' % (particle,)
1898 if self.CT_MIXED == tag:
1899 return 'Mixed [%s]' % (particle,)
1900 if self.CT_ELEMENT_ONLY == tag:
1901 return 'Element [%s]' % (particle,)
1902 raise pyxb.LogicError('Unhandled content type')
1903
1904 # Derived from the block and blockDefault attributes
1905 __prohibitedSubstitutions = DM_empty
1906
1907 # @todo: Extracted from children of various types
1908 __annotations = None
1909
1911 super(ComplexTypeDefinition, self).__init__(*args, **kw)
1912 self.__derivationMethod = kw.get('derivation_method')
1913 self.__scopedElementDeclarations = { }
1914 self.__scopedAttributeDeclarations = { }
1915
1917 """Return True iff this type includes a wildcard element in
1918 its content model."""
1919 if self.CT_EMPTY == self.contentType():
1920 return False
1921 ( tag, particle ) = self.contentType()
1922 if self.CT_SIMPLE == tag:
1923 return False
1924 return particle.hasWildcardElement()
1925
1927 """Override fields in this instance with those from the other.
1928
1929 This method is invoked only by Schema._addNamedComponent, and
1930 then only when a built-in type collides with a schema-defined
1931 type. Material like facets is not (currently) held in the
1932 built-in copy, so the DOM information is copied over to the
1933 built-in STD, which is subsequently re-resolved.
1934
1935 Returns self.
1936 """
1937 assert self != other
1938 assert self.isNameEquivalent(other)
1939 super(ComplexTypeDefinition, self)._updateFromOther_csc(other)
1940
1941 if not other.isResolved():
1942 if pyxb.namespace.BuiltInObjectUID != self._objectOrigin().generationUID():
1943 self.__derivationMethod = None
1944
1945 return self
1946
1947 __UrTypeDefinition = None
1948 @classmethod
1950 """Create the ComplexTypeDefinition instance that approximates
1951 the ur-type.
1952
1953 See section 3.4.7.
1954 """
1955
1956 # The first time, and only the first time, this is called, a
1957 # namespace should be provided which is the XMLSchema
1958 # namespace for this run of the system. Please, do not try to
1959 # allow this by clearing the type definition.
1960 #if in_builtin_definition and (cls.__UrTypeDefinition is not None):
1961 # raise pyxb.LogicError('Multiple definitions of UrType')
1962 if cls.__UrTypeDefinition is None:
1963 # NOTE: We use a singleton subclass of this class
1964 assert schema is not None
1965
1966 ns_ctx = schema.targetNamespace().initialNamespaceContext()
1967
1968 kw = { 'name' : 'anyType',
1969 'schema' : schema,
1970 'namespace_context' : ns_ctx,
1971 'binding_namespace' : schema.targetNamespace(),
1972 'derivation_method' : cls.DM_restriction,
1973 'scope' : _ScopedDeclaration_mixin.SCOPE_global }
1974 bi = _UrTypeDefinition(**kw)
1975
1976 # The ur-type is its own baseTypeDefinition
1977 bi.__baseTypeDefinition = bi
1978
1979 # No constraints on attributes
1980 bi._setAttributeWildcard(Wildcard(namespace_constraint=Wildcard.NC_any, process_contents=Wildcard.PC_lax, **kw))
1981
1982 # There isn't anything to look up, but context is still global.
1983 # No declarations will be created, so use indeterminate scope to
1984 # be consistent with validity checks in Particle constructor.
1985 # Content is mixed, with elements completely unconstrained. @todo:
1986 # not associated with a schema (it should be)
1987 kw = { 'namespace_context' : ns_ctx
1988 , 'schema' : schema
1989 , 'scope': _ScopedDeclaration_mixin.XSCOPE_indeterminate }
1990 w = Wildcard(namespace_constraint=Wildcard.NC_any, process_contents=Wildcard.PC_lax, **kw)
1991 p = Particle(w, min_occurs=0, max_occurs=None, **kw)
1992 m = ModelGroup(compositor=ModelGroup.C_SEQUENCE, particles=[ p ], **kw)
1993 bi.__contentType = ( cls.CT_MIXED, Particle(m, **kw) )
1994
1995 # No attribute uses
1996 bi.__attributeUses = set()
1997
1998 # No constraints on extension or substitution
1999 bi.__final = cls.DM_empty
2000 bi.__prohibitedSubstitutions = cls.DM_empty
2001
2002 bi.__abstract = False
2003
2004 # Refer to it by name
2005 bi.setNameInBinding(bi.name())
2006
2007 # The ur-type is always resolved
2008 bi.__derivationMethod = cls.DM_restriction
2009
2010 cls.__UrTypeDefinition = bi
2011 return cls.__UrTypeDefinition
2012
2014 """Indicate whether this simple type is a built-in type."""
2015 return (self.UrTypeDefinition() == self)
2016
2017 # bR:CTD
2019 """Complex type definitions depend on their base type definition, the
2020 type definitions of any local attribute declarations, and if strict
2021 the type definitions of any local element declarations."""
2022 rv = set()
2023 assert self.__baseTypeDefinition is not None
2024 rv.add(self.__baseTypeDefinition)
2025 for decl in self.localScopedDeclarations():
2026 if include_lax or isinstance(decl, AttributeDeclaration):
2027 rv.add(decl.typeDefinition())
2028 if include_lax:
2029 ct = self._contentTypeComponent()
2030 if ct is not None:
2031 rv.add(ct)
2032 return frozenset(rv)
2033
2034 # CFD:CTD CFD:ComplexTypeDefinition
2035 @classmethod
2037 # Node should be an XMLSchema complexType node
2038 assert xsd.nodeIsNamed(node, 'complexType')
2039
2040 name = NodeAttribute(node, 'name')
2041
2042 rv = cls(name=name, node=node, derivation_method=None, **kw)
2043
2044 if name is None:
2045 assert not isinstance(rv.owner(), Schema)
2046
2047 # Most of the time, the scope will be global. It can be something
2048 # else only if this is an anonymous CTD (created within an element
2049 # declaration which itself may be global, in a containing CTD, or in a
2050 # model group).
2051 if not (rv._scopeIsGlobal() or rv.isAnonymous()):
2052 raise pyxb.LogicError('Attempt to create non-global complex type definition')
2053
2054 kw.pop('node', None)
2055 kw['owner'] = rv
2056 kw['scope'] = rv
2057
2058 return rv.__setContentFromDOM(node, **kw)
2059
2060 __ckw = None
2061 __anyAttribute = None
2062 __attributeGroupAttributes = None
2063 __usesC1 = None
2064 __usesC1C2 = None
2065 __attributeGroups = None
2066 __PrivateTransient.update(['ckw', 'anyAttribute', 'attributeGroupAttributes', 'usesC1', 'usesC1C2', 'attributeGroups' ])
2067
2068 # Handle attributeUses, attributeWildcard, contentType
2070
2071 if self.__usesC1C2 is None:
2072 # Handle clauses 1 and 2 (common between simple and complex types)
2073 uses_c1 = self.__usesC1 # attribute children
2074 uses_c2 = set() # attribute group children
2075 self.__attributeGroups = []
2076 for ag_attr in self.__attributeGroupAttributes:
2077 ag_en = self._namespaceContext().interpretQName(ag_attr)
2078 agd = ag_en.attributeGroupDefinition()
2079 if agd is None:
2080 raise pyxb.SchemaValidationError('Attribute group %s cannot be found' % (ag_en,))
2081 if not agd.isResolved():
2082 self._queueForResolution('unresolved attribute group', depends_on=agd)
2083 return self
2084 self.__attributeGroups.append(agd)
2085 uses_c2.update(agd.attributeUses())
2086
2087 uses_c1c2 = uses_c1.union(uses_c2)
2088 for au in uses_c1c2:
2089 if not au.isResolved():
2090 self._queueForResolution('attribute use not resolved')
2091 return self
2092 ad = au.attributeDeclaration()
2093 if not ad.isResolved():
2094 ad_en = ad.expandedName()
2095 self._queueForResolution('unresolved attribute declaration %s from base type' % (ad_en,), depends_on=ad)
2096 return self
2097
2098 self.__usesC1C2 = frozenset([ _u._adaptForScope(self) for _u in uses_c1c2 ])
2099
2100 # Handle clause 3. Note the slight difference in description between
2101 # simple and complex content is just that the complex content doesn't
2102 # bother to check that the base type definition is a complex type
2103 # definition. So the same code should work for both, and we don't
2104 # bother to check content_style.
2105 uses_c3 = set() # base attributes
2106 if isinstance(self.__baseTypeDefinition, ComplexTypeDefinition):
2107 # NB: The base type definition should be resolved, which means
2108 # that all its attribute uses have been adapted for scope already
2109 uses_c3 = set(self.__baseTypeDefinition.__attributeUses)
2110 assert self.__baseTypeDefinition.isResolved()
2111 for au in uses_c3:
2112 if not au.isResolved():
2113 self._queueForResolution('unresolved attribute use from base type', depends_on=au)
2114 return self
2115 ad = au.attributeDeclaration()
2116 if not ad.isResolved():
2117 ad_en = ad.expandedName()
2118 self._queueForResolution('unresolved attribute declaration %s from base type' % (ad_en,), depends_on=ad)
2119 return self
2120 assert not au.attributeDeclaration()._scopeIsIndeterminate()
2121
2122 if self.DM_restriction == method:
2123 # Exclude attributes per clause 3. Note that this process
2124 # handles both 3.1 and 3.2, since we have not yet filtered
2125 # uses_c1 for prohibited attributes.
2126 for au in self.__usesC1C2:
2127 matching_uses = au.matchingQNameMembers(uses_c3)
2128 assert matching_uses is not None
2129 assert 1 >= len(matching_uses), 'Multiple inherited attribute uses with name %s'
2130 for au2 in matching_uses:
2131 assert au2.isResolved()
2132 uses_c3.remove(au2)
2133 au._setRestrictionOf(au2)
2134 else:
2135 # In theory, the same attribute name can't appear in the base
2136 # and sub types because that would violate the local
2137 # declaration constraint.
2138 assert self.DM_extension == method
2139
2140 use_map = { }
2141 for au in self.__usesC1C2.union(uses_c3):
2142 assert au.isResolved()
2143 ad_en = au.attributeDeclaration().expandedName()
2144 if ad_en in use_map:
2145 raise pyxb.SchemaValidationError('Multiple definitions for %s in CTD %s' % (ad_en, self.expandedName()))
2146 use_map[ad_en] = au
2147
2148 # Past the last point where we might not resolve this instance. Store
2149 # the attribute uses, also recording local attribute declarations.
2150 self.__attributeUses = frozenset(use_map.values())
2151 if not self._scopeIsIndeterminate():
2152 for au in self.__attributeUses:
2153 assert not au.attributeDeclaration()._scopeIsIndeterminate(), 'indeterminate scope for %s' % (au,)
2154
2155 # @todo: Handle attributeWildcard
2156 # Clause 1
2157 local_wildcard = None
2158 if self.__anyAttribute is not None:
2159 local_wildcard = Wildcard.CreateFromDOM(self.__anyAttribute)
2160
2161 # Clause 2
2162 complete_wildcard = _AttributeWildcard_mixin.CompleteWildcard(self._namespaceContext(), self.__attributeGroups, local_wildcard)
2163
2164 # Clause 3
2165 if self.DM_restriction == method:
2166 # Clause 3.1
2167 self._setAttributeWildcard(complete_wildcard)
2168 else:
2169 assert (self.DM_extension == method)
2170 assert self.baseTypeDefinition().isResolved()
2171 # 3.2.1
2172 base_wildcard = None
2173 if isinstance(self.baseTypeDefinition(), ComplexTypeDefinition):
2174 base_wildcard = self.baseTypeDefinition().attributeWildcard()
2175 # 3.2.2
2176 if base_wildcard is not None:
2177 if complete_wildcard is None:
2178 # 3.2.2.1.1
2179 self._setAttributeWildcard(base_wildcard)
2180 else:
2181 # 3.2.2.1.2
2182 self._setAttributeWildcard(Wildcard (process_contents=complete_wildcard.processContents(),
2183 namespace_constraint = Wildcard.IntensionalUnion([complete_wildcard.namespaceConstraint(),
2184 base_wildcard.namespaceConstraint()]),
2185 annotation=complete_wildcard.annotation(),
2186 namespace_context=self._namespaceContext()))
2187 else:
2188 # 3.2.2.2
2189 self._setAttributeWildcard(complete_wildcard)
2190
2191 # @todo: Make sure we didn't miss any child nodes
2192
2193 # Remove local attributes we will never use again
2194 del self.__usesC1
2195 del self.__usesC1C2
2196 del self.__attributeGroups
2197 self.__ckw = None
2198
2199 # Only now that we've succeeded do we store the method, which
2200 # marks this component resolved.
2201
2202 self.__derivationMethod = method
2203 return self
2204
2206 # Do content type
2207 if isinstance(self.__baseTypeDefinition, ComplexTypeDefinition):
2208 # Clauses 1, 2, and 3 might apply
2209 parent_content_type = self.__baseTypeDefinition.__contentType
2210 #print '%s %s %s' % (self.expandedName(), self.__baseTypeDefinition.expandedName(), parent_content_type)
2211 if ((type(parent_content_type) == tuple) \
2212 and (self.CT_SIMPLE == parent_content_type[0]) \
2213 and (self.DM_restriction == method)):
2214 # Clause 1
2215 assert self.__ctscRestrictionNode is not None
2216 std = self.__ctscClause2STD
2217 if std is None:
2218 std = parent_content_type[1]
2219 assert isinstance(std, SimpleTypeDefinition)
2220 if not std.isResolved():
2221 return None
2222 restriction_node = self.__ctscRestrictionNode
2223 self.__ctscClause2STD = None
2224 self.__ctscRestrictionNode = None
2225 return ( self.CT_SIMPLE, std._createRestriction(self, restriction_node) )
2226 elif ((type(parent_content_type) == tuple) \
2227 and (self.CT_MIXED == parent_content_type[0]) \
2228 and parent_content_type[1].isEmptiable()):
2229 # Clause 2
2230 assert isinstance(self.__ctscClause2STD, SimpleTypeDefinition)
2231 return ( self.CT_SIMPLE, self.__ctscClause2STD )
2232 else:
2233 # Clause 3
2234 return parent_content_type
2235 else:
2236 # Clause 4
2237 return ( self.CT_SIMPLE, self.__baseTypeDefinition )
2238 assert False
2239
2240 __ctscClause2STD = None
2241 __ctscRestrictioNode = None
2242
2243 - def __setComplexContentFromDOM (self, type_node, content_node, definition_node_list, method, **kw):
2244 # Do content type. Cache the keywords that need to be used
2245 # for newly created schema components.
2246 ckw = kw.copy()
2247 ckw['namespace_context'] = pyxb.namespace.resolution.NamespaceContext.GetNodeContext(type_node)
2248
2249 # Definition 1: effective mixed
2250 mixed_attr = None
2251 if content_node is not None:
2252 mixed_attr = NodeAttribute(content_node, 'mixed')
2253 if mixed_attr is None:
2254 mixed_attr = NodeAttribute(type_node, 'mixed')
2255 if mixed_attr is not None:
2256 effective_mixed = datatypes.boolean(mixed_attr)
2257 else:
2258 effective_mixed = False
2259
2260 # Definition 2: effective content
2261 case_2_1_predicate_count = 0
2262 test_2_1_1 = True
2263 test_2_1_2 = False
2264 test_2_1_3 = False
2265 typedef_node = None
2266 for cn in definition_node_list:
2267 if Node.ELEMENT_NODE != cn.nodeType:
2268 continue
2269 if xsd.nodeIsNamed(cn, 'simpleContent', 'complexContent'):
2270 # Should have found the content node earlier.
2271 raise pyxb.LogicError('Missed explicit wrapper in complexType content')
2272 if Particle.IsTypedefNode(cn):
2273 typedef_node = cn
2274 test_2_1_1 = False
2275 if xsd.nodeIsNamed(cn, 'all', 'sequence') \
2276 and (not HasNonAnnotationChild(cn)):
2277 test_2_1_2 = True
2278 if xsd.nodeIsNamed(cn, 'choice') \
2279 and (not HasNonAnnotationChild(cn)):
2280 mo_attr = NodeAttribute(cn, 'minOccurs')
2281 if ((mo_attr is not None) \
2282 and (0 == datatypes.integer(mo_attr))):
2283 test_2_1_3 = True
2284 satisfied_predicates = 0
2285 if test_2_1_1:
2286 satisfied_predicates += 1
2287 if test_2_1_2:
2288 satisfied_predicates += 1
2289 if test_2_1_3:
2290 satisfied_predicates += 1
2291 if 1 == satisfied_predicates:
2292 if effective_mixed:
2293 # Clause 2.1.4
2294 assert (typedef_node is None) or test_2_1_2
2295 m = ModelGroup(compositor=ModelGroup.C_SEQUENCE, particles=[], **ckw)
2296 effective_content = Particle(m, **ckw)
2297 else:
2298 # Clause 2.1.5
2299 effective_content = self.CT_EMPTY
2300 else:
2301 # Clause 2.2
2302 assert typedef_node is not None
2303 effective_content = Particle.CreateFromDOM(typedef_node, **kw)
2304
2305 # For issues related to soapenc:Array and the fact that PyXB
2306 # determines the content of types derived from it is empty, see
2307 # http://tech.groups.yahoo.com/group/soapbuilders/message/5879 and
2308 # lament the fact that the WSDL spec is not compatible with XSD. It
2309 # is *not* an error in PyXB.
2310
2311 self.__effectiveMixed = effective_mixed
2312 self.__effectiveContent = effective_content
2313 self.__ckw = ckw
2314
2315 #if isinstance(effective_content, Particle):
2316 # print 'Effective total range: %s %s' % effective_content.effectiveTotalRange()
2317
2319 ckw = self.__ckw
2320
2321 # Shared from clause 3.1.2
2322 if self.__effectiveMixed:
2323 ct = self.CT_MIXED
2324 else:
2325 ct = self.CT_ELEMENT_ONLY
2326 # Clause 3
2327 if self.DM_restriction == method:
2328 # Clause 3.1
2329 if self.CT_EMPTY == self.__effectiveContent:
2330 # Clause 3.1.1
2331 content_type = self.CT_EMPTY # ASSIGN CT_EMPTY
2332 else:
2333 # Clause 3.1.2(.2)
2334 content_type = ( ct, self.__effectiveContent ) # ASSIGN RESTRICTION
2335 assert 0 == len(self.__scopedElementDeclarations)
2336 # Reference the parent element declarations; normally this
2337 # would happen naturally as a consequence of appending this
2338 # type's content model to the parent's, but with restriction
2339 # there is no such re-use unless we do this.
2340 self.__scopedElementDeclarations.update(self.__baseTypeDefinition.__scopedElementDeclarations)
2341 else:
2342 # Clause 3.2
2343 assert self.DM_extension == method
2344 assert self.__baseTypeDefinition.isResolved()
2345 parent_content_type = self.__baseTypeDefinition.contentType()
2346 if self.CT_EMPTY == self.__effectiveContent:
2347 content_type = parent_content_type # ASSIGN EXTENSION PARENT ONLY
2348 elif self.CT_EMPTY == parent_content_type:
2349 # Clause 3.2.2
2350 content_type = ( ct, self.__effectiveContent ) # ASSIGN EXTENSION LOCAL ONLY
2351 else:
2352 assert type(parent_content_type) == tuple
2353 m = ModelGroup(compositor=ModelGroup.C_SEQUENCE, particles=[ parent_content_type[1], self.__effectiveContent ], **ckw)
2354 content_type = ( ct, Particle(m, **ckw) ) # ASSIGN EXTENSION PARENT AND LOCAL
2355
2356 assert (self.CT_EMPTY == content_type) or ((type(content_type) == tuple) and (content_type[1] is not None))
2357 return content_type
2358
2360 """Indicate whether this complex type is fully defined.
2361
2362 All built-in type definitions are resolved upon creation.
2363 Schema-defined type definitionss are held unresolved until the
2364 schema has been completely read, so that references to later
2365 schema-defined types can be resolved. Resolution is performed
2366 after the entire schema has been scanned and type-definition
2367 instances created for all topLevel{Simple,Complex}Types.
2368
2369 If a built-in type definition is also defined in a schema
2370 (which it should be), the built-in definition is kept, with
2371 the schema-related information copied over from the matching
2372 schema-defined type definition. The former then replaces the
2373 latter in the list of type definitions to be resolved. See
2374 Schema._addNamedComponent.
2375 """
2376 # Only unresolved nodes have an unset derivationMethod
2377 return (self.__derivationMethod is not None)
2378
2379 # Back door to allow the ur-type to re-resolve itself. Only needed when
2380 # we're generating bindings for XMLSchema itself.
2384
2386 schema = kw.get('schema')
2387 assert schema is not None
2388 self.__prohibitedSubstitutions = schema.blockForNode(node, self._DM_Map)
2389 self.__final = schema.finalForNode(node, self._DM_Map)
2390
2391 attr_val = NodeAttribute(node, 'abstract')
2392 if attr_val is not None:
2393 self.__abstract = datatypes.boolean(attr_val)
2394
2395 # Assume we're in the short-hand case: the entire content is
2396 # implicitly wrapped in a complex restriction of the ur-type.
2397 definition_node_list = node.childNodes
2398 is_complex_content = True
2399 self.__baseTypeDefinition = ComplexTypeDefinition.UrTypeDefinition()
2400 method = self.DM_restriction
2401
2402 # Determine whether above assumption is correct by looking for
2403 # element content and seeing if it's one of the wrapper
2404 # elements.
2405 first_elt = LocateFirstChildElement(node)
2406 content_node = None
2407 clause2_std = None
2408 ctsc_restriction_node = None
2409 if first_elt:
2410 have_content = False
2411 if xsd.nodeIsNamed(first_elt, 'simpleContent'):
2412 have_content = True
2413 is_complex_content = False
2414 elif xsd.nodeIsNamed(first_elt, 'complexContent'):
2415 have_content = True
2416 else:
2417 # Not one of the wrappers; use implicit wrapper around
2418 # the children
2419 if not Particle.IsParticleNode(first_elt, 'attributeGroup', 'attribute', 'anyAttribute'):
2420 raise pyxb.SchemaValidationError('Unexpected element %s at root of complexType' % (first_elt.nodeName,))
2421 if have_content:
2422 # Repeat the search to verify that only the one child is present.
2423 content_node = LocateFirstChildElement(node, require_unique=True)
2424 assert content_node == first_elt
2425
2426 # Identify the contained restriction or extension
2427 # element, and extract the base type.
2428 ions = LocateFirstChildElement(content_node, absent_ok=False)
2429 if xsd.nodeIsNamed(ions, 'restriction'):
2430 method = self.DM_restriction
2431 if not is_complex_content:
2432 # Clause 2 of complex type with simple content
2433 ctsc_restriction_node = ions
2434 ions_st = LocateUniqueChild(ions,'simpleType')
2435 if ions_st is not None:
2436 clause2_std = SimpleTypeDefinition.CreateFromDOM(ions_st, **kw)
2437 elif xsd.nodeIsNamed(ions, 'extension'):
2438 method = self.DM_extension
2439 else:
2440 raise pyxb.SchemaValidationError('Expected restriction or extension as sole child of %s in %s' % (content_node.nodeName, self.name()))
2441 self.__baseAttribute = NodeAttribute(ions, 'base')
2442 if self.__baseAttribute is None:
2443 raise pyxb.SchemaValidationError('Element %s missing base attribute' % (ions.nodeName,))
2444 self.__baseTypeDefinition = None
2445 # The content is defined by the restriction/extension element
2446 definition_node_list = ions.childNodes
2447 # deriviationMethod is assigned after resolution completes
2448 self.__pendingDerivationMethod = method
2449 self.__isComplexContent = is_complex_content
2450 self.__ctscRestrictionNode = ctsc_restriction_node
2451 self.__ctscClause2STD = clause2_std
2452
2453 (attributes, attribute_group_attrs, any_attribute) = self._attributeRelevantChildren(definition_node_list)
2454 self.__usesC1 = set()
2455 for cn in attributes:
2456 au = AttributeUse.CreateFromDOM(cn, **kw)
2457 self.__usesC1.add(au)
2458 self.__attributeGroupAttributes = attribute_group_attrs
2459 self.__anyAttribute = any_attribute
2460
2461 if self.__isComplexContent:
2462 self.__setComplexContentFromDOM(node, content_node, definition_node_list, self.__pendingDerivationMethod, **kw)
2463
2464 # Creation does not attempt to do resolution. Queue up the newly created
2465 # whatsis so we can resolve it after everything's been read in.
2466 self._annotationFromDOM(node)
2467
2468 if not self.isResolved():
2469 self._queueForResolution('creation')
2470
2471 return self
2472
2473 # Resolution of a CTD can be delayed for the following reasons:
2474 #
2475 # * It extends or restricts a base type that has not been resolved
2476 # [_resolve]
2477 #
2478 # * It refers to an attribute or attribute group that has not been
2479 # resolved [__completeProcessing]
2480 #
2481 # * It includes an attribute that matches in NCName and namespace
2482 # an unresolved attribute from the base type
2483 # [__completeProcessing]
2484 #
2485 # * The content model includes a particle which cannot be resolved
2486 # (so has not contributed any local element declarations).
2487 # res:CTD
2489 if self.isResolved():
2490 return self
2491
2492 # @todo: implement prohibitedSubstitutions, final, annotations
2493
2494 # See whether we've resolved through to the base type
2495 if self.__baseTypeDefinition is None:
2496 base_en = self._namespaceContext().interpretQName(self.__baseAttribute)
2497 base_type = base_en.typeDefinition()
2498 if base_type is None:
2499 raise pyxb.SchemaValidationError('Cannot locate %s: need import?' % (base_en,))
2500 if not base_type.isResolved():
2501 # Have to delay resolution until the type this
2502 # depends on is available.
2503 self._queueForResolution('unresolved base type %s' % (base_en,), depends_on=base_type)
2504 return self
2505 self.__baseTypeDefinition = base_type
2506
2507 # Only build the content once. This will not complete if the content
2508 # is a restriction of an unresolved simple type; otherwise, it only
2509 # depends on the base type which we know is good.
2510 if self.__contentType is None:
2511 if self.__isComplexContent:
2512 content_type = self.__complexContent(self.__pendingDerivationMethod)
2513 self.__contentStyle = 'complex'
2514 else:
2515 # The definition node list is not relevant to simple content
2516 content_type = self.__simpleContent(self.__pendingDerivationMethod)
2517 if content_type is None:
2518 self._queueForResolution('restriction of unresolved simple type')
2519 return self
2520 self.__contentStyle = 'simple'
2521 assert content_type is not None
2522 self.__contentType = content_type
2523
2524 # Last chance for failure is if we haven't been able to
2525 # extract all the element declarations that might appear in
2526 # this complex type. That technically wouldn't stop this from
2527 # being resolved, but it does prevent us from using it as a
2528 # context.
2529 if isinstance(self.__contentType, tuple) and isinstance(self.__contentType[1], Particle):
2530 prt = self.__contentType[1]
2531 if not prt.isAdaptable(self):
2532 self._queueForResolution('content particle %s is not deep-resolved' % (prt,))
2533 return self
2534 self.__contentType = (self.__contentType[0], prt._adaptForScope(self, self))
2535 #print 'Done adapting %s content' % (self.expandedName(),)
2536
2537 return self.__completeProcessing(self.__pendingDerivationMethod, self.__contentStyle)
2538
2542
2544 if self.isAnonymous():
2545 return 'CTD{Anonymous}[%x]' % (id(self),)
2546 return 'CTD[%s]' % (self.expandedName(),)
2547
2549 """Subclass ensures there is only one ur-type."""
2553
2555 # The ur type is always resolved, except when it gets unresolved
2556 # through being updated from an instance read from the schema.
2557 return self._setDerivationMethod(self.DM_restriction)
2558
2559
2560 -class AttributeGroupDefinition (_SchemaComponent_mixin, _NamedComponent_mixin, pyxb.namespace.resolution._Resolvable_mixin, _Annotated_mixin, _AttributeWildcard_mixin):
2561 """An XMLSchema U{Attribute Group Definition<http://www.w3.org/TR/xmlschema-1/#cAttribute_Group_Definitions>} component."""
2562 __PrivateTransient = set()
2563
2564 # A frozenset of AttributeUse instances
2565 __attributeUses = None
2566
2569 #assert 'scope' in kw
2570 #assert self._scopeIsIndeterminate()
2571
2573 return 'AGD[%s]' % (self.expandedName(),)
2574
2575 @classmethod
2577 """Create an attribute declaration component for a specified namespace."""
2578 kw = { 'name' : name,
2579 'schema' : schema,
2580 'namespace_context' : schema.targetNamespace().initialNamespaceContext(),
2581 'scope' : _ScopedDeclaration_mixin.SCOPE_global }
2582 bi = cls(**kw)
2583 bi.__attributeUses = frozenset(attribute_uses)
2584 bi.__isResolved = True
2585 return bi
2586
2587 __anyAttribute = None
2588 __attributeGroupAttributes = None
2589 __PrivateTransient.update(['anyAttribute', 'attributeGroupAttributes'])
2590
2591 # CFD:AGD CFD:AttributeGroupDefinition
2592 @classmethod
2594 """Create an attribute group definition from the given DOM node.
2595
2596 """
2597
2598 assert xsd.nodeIsNamed(node, 'attributeGroup')
2599 name = NodeAttribute(node, 'name')
2600 schema = kw['schema']
2601
2602 # Attribute group definitions can only appear at the top level of the
2603 # schema, and any definitions in them are scope indeterminate until
2604 # they're referenced in a complex type.
2605 kw.update({ 'scope' : _ScopedDeclaration_mixin.XSCOPE_indeterminate })
2606 rv = cls(name=name, node=node, **kw)
2607
2608 rv._annotationFromDOM(node)
2609
2610 # Attribute group definitions must not be references
2611 rv.__refAttribute = NodeAttribute(node, 'ref')
2612 if rv.__refAttribute is not None:
2613 raise pyxb.SchemaValidationError('Attribute reference at top level')
2614
2615 kw.pop('node', None)
2616 kw['owner'] = rv
2617
2618 (attributes, attribute_group_attrs, any_attribute) = rv._attributeRelevantChildren(node.childNodes)
2619 rv.__attributeUses = set()
2620 for cn in attributes:
2621 rv.__attributeUses.add(AttributeUse.CreateFromDOM(cn, **kw))
2622 rv.__attributeGroupAttributes = attribute_group_attrs
2623 rv.__anyAttribute = any_attribute
2624
2625 # Unconditionally queue for resolution, to avoid repeating the
2626 # wildcard code.
2627 rv._queueForResolution('creation')
2628
2629 return rv
2630
2631 # Indicates whether we have resolved any references
2632 __isResolved = False
2634 return self.__isResolved
2635
2637 if self.__isResolved:
2638 return self
2639
2640 uses = self.__attributeUses
2641 attribute_groups = []
2642 for ag_attr in self.__attributeGroupAttributes:
2643 ag_en = self._namespaceContext().interpretQName(ag_attr)
2644 agd = ag_en.attributeGroupDefinition()
2645 if agd is None:
2646 raise pyxb.SchemaValidationError('Attribute group %s cannot be found' % (ag_en,))
2647 attribute_groups.append(agd)
2648 uses = uses.union(agd.attributeUses())
2649
2650 self.__attributeUses = frozenset(uses)
2651
2652 # "Complete wildcard" per CTD
2653 local_wildcard = None
2654 if self.__anyAttribute is not None:
2655 local_wildcard = Wildcard.CreateFromDOM(self.__anyAttribute)
2656 self._setAttributeWildcard(_AttributeWildcard_mixin.CompleteWildcard(self._namespaceContext(), attribute_groups, local_wildcard))
2657
2658 self.__isResolved = True
2659 return self
2660
2661 # bR:AGD
2663 """Attribute group declarations require their uses, but only if lax."""
2664 if not include_lax:
2665 return frozenset()
2666 return frozenset(self.attributeUses())
2667
2669 return self.__attributeUses
2670
2672 """An XMLSchema U{Model Group Definition<http://www.w3.org/TR/xmlschema-1/#cModel_Group_Definitions>} component."""
2673 # Reference to a _ModelGroup
2674 __modelGroup = None
2675
2679
2680 # CFD:MGD CFD:ModelGroupDefinition
2681 @classmethod
2683 """Create a Model Group Definition from a DOM element node.
2684
2685 wxs is a Schema instance within which the model group is being
2686 defined.
2687
2688 node is a DOM element. The name must be 'group', and the node
2689 must be in the XMLSchema namespace. The node must have a
2690 'name' attribute, and must not have a 'ref' attribute.
2691 """
2692 assert xsd.nodeIsNamed(node, 'group')
2693
2694 assert NodeAttribute(node, 'ref') is None
2695
2696 name = NodeAttribute(node, 'name')
2697 kw['scope'] = _ScopedDeclaration_mixin.XSCOPE_indeterminate
2698 rv = cls(name=name, node=node, **kw)
2699 rv._annotationFromDOM(node)
2700
2701 kw.pop('node', None)
2702 kw['owner'] = rv
2703
2704 for cn in node.childNodes:
2705 if Node.ELEMENT_NODE != cn.nodeType:
2706 continue
2707 if ModelGroup.IsGroupMemberNode(cn):
2708 assert not rv.__modelGroup
2709 # Model group definitions always occur at the top level of the
2710 # schema, so the elements declared in them are not bound to a
2711 # scope until they are referenced in a complex type.
2712 rv.__modelGroup = ModelGroup.CreateFromDOM(cn, model_group_definition=rv, **kw)
2713 assert rv.__modelGroup is not None
2714 return rv
2715
2716 # bR:MGD
2718 """Model group definitions depend on the contained model group."""
2719 if not include_lax:
2720 return frozenset()
2721 return frozenset([self.__modelGroup])
2722
2725
2728 """An XMLSchema U{Model Group<http://www.w3.org/TR/xmlschema-1/#cModel_Group>} component."""
2729 C_INVALID = 0
2730 C_ALL = 0x01
2731 C_CHOICE = 0x02
2732 C_SEQUENCE = 0x03
2733
2734 # One of the C_* values above. Set at construction time from the
2735 # keyword parameter "compositor".
2736 __compositor = C_INVALID
2738
2739 @classmethod
2741 """Map a compositor value to a string."""
2742 if cls.C_ALL == compositor:
2743 return 'all'
2744 if cls.C_CHOICE == compositor:
2745 return 'choice'
2746 if cls.C_SEQUENCE == compositor:
2747 return 'sequence'
2748 return 'invalid'
2749
2751 """Return a string representing the compositor value."""
2752 return self.CompositorToString(self.__compositor)
2753
2754 # A list of Particle instances. Set at construction time from
2755 # the keyword parameter "particles".
2756 __particles = None
2758
2760 """A model group has an unresolvable particle if any of its
2761 particles is unresolvable. Duh."""
2762 for p in self.particles():
2763 if not p.isAdaptable(ctd):
2764 return False
2765 return True
2766
2768 """Return the minimum and maximum of the number of elements that can
2769 appear in a sequence matched by this particle.
2770
2771 See http://www.w3.org/TR/xmlschema-1/#cos-seq-range
2772 """
2773 if self.__compositor in (self.C_ALL, self.C_SEQUENCE):
2774 sum_minoccurs = 0
2775 sum_maxoccurs = 0
2776 for prt in self.__particles:
2777 (prt_min, prt_max) = prt.effectiveTotalRange()
2778 sum_minoccurs += prt_min
2779 if sum_maxoccurs is not None:
2780 if prt_max is None:
2781 sum_maxoccurs = None
2782 else:
2783 sum_maxoccurs += prt_max
2784 prod_maxoccurs = particle.maxOccurs()
2785 if prod_maxoccurs is not None:
2786 if sum_maxoccurs is None:
2787 prod_maxoccurs = None
2788 else:
2789 prod_maxoccurs *= sum_maxoccurs
2790 return (sum_minoccurs * particle.minOccurs(), prod_maxoccurs)
2791 assert self.__compositor == self.C_CHOICE
2792 if 0 == len(self.__particles):
2793 min_minoccurs = 0
2794 max_maxoccurs = 0
2795 else:
2796 (min_minoccurs, max_maxoccurs) = self.__particles[0].effectiveTotalRange()
2797 for prt in self.__particles[1:]:
2798 (prt_min, prt_max) = prt.effectiveTotalRange()
2799 if prt_min < min_minoccurs:
2800 min_minoccurs = prt_min
2801 if prt_max is None:
2802 max_maxoccurs = None
2803 elif (max_maxoccurs is not None) and (prt_max > max_maxoccurs):
2804 max_maxoccurs = prt_max
2805 min_minoccurs *= particle.minOccurs()
2806 if (max_maxoccurs is not None) and (particle.maxOccurs() is not None):
2807 max_maxoccurs *= particle.maxOccurs()
2808 return (min_minoccurs, max_maxoccurs)
2809
2810 # The ModelGroupDefinition that names this ModelGroup, or None if
2811 # the ModelGroup is anonymous. This is set at construction time
2812 # from the keyword parameter "model_group_definition".
2813 __modelGroupDefinition = None
2815 """The ModelGroupDefinition that names this group, or None if it is unnamed."""
2816 return self.__modelGroupDefinition
2817
2819 """Create a new model group.
2820
2821 compositor must be a legal compositor value (one of C_ALL, C_CHOICE, C_SEQUENCE).
2822
2823 particles must be a list of zero or more Particle instances.
2824
2825 scope is the _ScopeDeclaration_mixin context into which new
2826 declarations are recorded. It can be SCOPE_global, a complex
2827 type definition, or None if this is (or is within) a named
2828 model group.
2829
2830 model_group_definition is an instance of ModelGroupDefinition
2831 if this is a named model group. It defaults to None
2832 indicating a local group.
2833 """
2834
2835 super(ModelGroup, self).__init__(*args, **kw)
2836 assert 'scope' in kw
2837 self.__compositor = compositor
2838 #print 'Incoming particles %s with scope %s' % (particles, self._scope())
2839 self.__particles = particles
2840 self.__modelGroupDefinition = kw.get('model_group_definition')
2841
2846
2848 """Return True if the model includes a wildcard amongst its particles."""
2849 for p in self.particles():
2850 if p.hasWildcardElement():
2851 return True
2852 return False
2853
2854 # bR:MG
2859
2860 # CFD:MG CFD:ModelGroup
2861 @classmethod
2863 """Create a model group from the given DOM node.
2864
2865 wxs is a Schema instance within which the model group is being
2866 defined.
2867
2868 node is a DOM element. The name must be one of ( 'all',
2869 'choice', 'sequence' ), and the node must be in the XMLSchema
2870 namespace.
2871
2872 scope is the _ScopeDeclaration_mxin context that is assigned
2873 to declarations that appear within the model group. It can be
2874 None, indicating no scope defined, or a complex type
2875 definition.
2876 """
2877
2878 scope = kw['scope']
2879 assert _ScopedDeclaration_mixin.ScopeIsIndeterminate(scope) or isinstance(scope, ComplexTypeDefinition)
2880
2881 if xsd.nodeIsNamed(node, 'all'):
2882 compositor = cls.C_ALL
2883 elif xsd.nodeIsNamed(node, 'choice'):
2884 compositor = cls.C_CHOICE
2885 elif xsd.nodeIsNamed(node, 'sequence'):
2886 compositor = cls.C_SEQUENCE
2887 else:
2888 raise pyxb.IncompleteImplementationError('ModelGroup: Got unexpected %s' % (node.nodeName,))
2889 particles = []
2890 # Remove the owner from particle constructor arguments: we need to set it later
2891 kw.pop('owner', None)
2892 for cn in node.childNodes:
2893 if Node.ELEMENT_NODE != cn.nodeType:
2894 continue
2895 if Particle.IsParticleNode(cn):
2896 # NB: Ancestor of particle is set in the ModelGroup constructor
2897 particles.append(Particle.CreateFromDOM(node=cn, **kw))
2898 elif not xsd.nodeIsNamed(cn, 'annotation'):
2899 raise pyxb.SchemaValidationError('Unexpected element %s in model group' % (cn.nodeName,))
2900 rv = cls(compositor, particles, node=node, **kw)
2901 for p in particles:
2902 p._setOwner(rv)
2903 rv._annotationFromDOM(node)
2904 return rv
2905
2906 @classmethod
2908 return xsd.nodeIsNamed(node, 'all', 'choice', 'sequence')
2909
2911 """Return a list of all ElementDeclarations that are at the
2912 top level of this model group, in the order in which they can
2913 occur."""
2914 element_decls = []
2915 model_groups = [ self ]
2916 #print 'Extracting element declarations from model group with %d particles: %s' % (len(self.particles()), self.particles())
2917 while model_groups:
2918 mg = model_groups.pop(0)
2919 for p in mg.particles():
2920 if isinstance(p.term(), ModelGroup):
2921 model_groups.append(p.term())
2922 elif isinstance(p.term(), ElementDeclaration):
2923 element_decls.extend(p.elementDeclarations())
2924 else:
2925 assert p.term() is not None
2926 pass
2927 #print 'Particle term: %s' % (object.__str__(p.term()),)
2928 #print 'Model group with %d particles produced %d element declarations' % (len(self.particles()), len(element_decls))
2929 return element_decls
2930
2931 # aFS:MG
2933 #print 'aFS:MG - %s' % (ctd.expandedName(),)
2934 rv = self
2935 assert isinstance(ctd, ComplexTypeDefinition)
2936 maybe_rv = self._clone(owner, ctd._objectOrigin())
2937 scoped_particles = [ _p._adaptForScope(maybe_rv, ctd) for _p in self.particles() ]
2938 do_clone = (self._scope() != ctd) or (self.particles() != scoped_particles)
2939 if do_clone:
2940 rv = maybe_rv
2941 rv.__particles = scoped_particles
2942 return rv
2943
2945 comp = None
2946 if self.C_ALL == self.compositor():
2947 comp = 'ALL'
2948 elif self.C_CHOICE == self.compositor():
2949 comp = 'CHOICE'
2950 elif self.C_SEQUENCE == self.compositor():
2951 comp = 'SEQUENCE'
2952 return '%s:(%s)' % (comp, ",".join( [ str(_p) for _p in self.particles() ] ) )
2953
2955 """An XMLSchema U{Particle<http://www.w3.org/TR/xmlschema-1/#cParticle>} component."""
2956
2957 # The minimum number of times the term may appear.
2958 __minOccurs = 1
2960 """The minimum number of times the term may appear.
2961
2962 Defaults to 1."""
2963 return self.__minOccurs
2964
2965 # Upper limit on number of times the term may appear.
2966 __maxOccurs = 1
2968 """Upper limit on number of times the term may appear.
2969
2970 If None, the term may appear any number of times; otherwise,
2971 this is an integral value indicating the maximum number of times
2972 the term may appear. The default value is 1; the value, unless
2973 None, must always be at least minOccurs().
2974 """
2975 return self.__maxOccurs
2976
2977 # A reference to a ModelGroup, WildCard, or ElementDeclaration
2978 __term = None
2982 __pendingTerm = None
2983
2985 assert self.__term is not None
2986 if isinstance(self.__term, ModelGroup):
2987 return self.__term.elementDeclarations()
2988 if isinstance(self.__term, ElementDeclaration):
2989 return [ self.__term ]
2990 if isinstance(self.__term, Wildcard):
2991 return [ ]
2992 raise pyxb.LogicError('Unexpected term type %s' % (self.__term,))
2993
2995 """Return the plurality data for this component.
2996
2997 The plurality data for a particle is the plurality data for
2998 its term, with the counts scaled by the effect of
2999 maxOccurs."""
3000 return _PluralityData(self)
3001
3003 """Extend the concept of effective total range to all particles.
3004
3005 See http://www.w3.org/TR/xmlschema-1/#cos-seq-range
3006 """
3007 if isinstance(self.__term, ModelGroup):
3008 return self.__term.effectiveTotalRange(self)
3009 return (self.minOccurs(), self.maxOccurs())
3010
3012 """Return C{True} iff this particle can legitimately match an empty
3013 sequence (no content).
3014
3015 See http://www.w3.org/TR/xmlschema-1/#cos-group-emptiable.
3016 """
3017 return 0 == self.effectiveTotalRange()[0]
3018
3020 """Return True iff this particle has a wildcard in its term.
3021
3022 Note that the wildcard may be in a nested model group."""
3023 return self.term().hasWildcardElement()
3024
3026 """Create a particle from the given DOM node.
3027
3028 term is a XML Schema Component: one of ModelGroup,
3029 ElementDeclaration, and Wildcard.
3030
3031 The following keyword arguments are processed:
3032
3033 min_occurs is a non-negative integer value with default 1,
3034 denoting the minimum number of terms required by the content
3035 model.
3036
3037 max_occurs is a positive integer value with default 1, or None
3038 indicating unbounded, denoting the maximum number of terms
3039 allowed by the content model.
3040
3041 scope is the _ScopeDeclaration_mxin context that is assigned
3042 to declarations that appear within the particle. It can be
3043 None, indicating no scope defined, or a complex type
3044 definition.
3045 """
3046
3047 super(Particle, self).__init__(*args, **kw)
3048
3049 min_occurs = kw.get('min_occurs', 1)
3050 max_occurs = kw.get('max_occurs', 1)
3051
3052 assert 'scope' in kw
3053 assert (self._scopeIsIndeterminate()) or isinstance(self._scope(), ComplexTypeDefinition)
3054
3055 if term is not None:
3056 self.__term = term
3057
3058 assert isinstance(min_occurs, (types.IntType, types.LongType))
3059 self.__minOccurs = min_occurs
3060 assert (max_occurs is None) or isinstance(max_occurs, (types.IntType, types.LongType))
3061 self.__maxOccurs = max_occurs
3062 if self.__maxOccurs is not None:
3063 if self.__minOccurs > self.__maxOccurs:
3064 raise pyxb.LogicError('Particle minOccurs %s is greater than maxOccurs %s on creation' % (min_occurs, max_occurs))
3065
3066 # res:Particle
3068 if self.isResolved():
3069 return self
3070 scope = self._scope()
3071
3072 # @RESOLUTION@
3073 if ModelGroup == self.__resolvableType:
3074 ref_en = self._namespaceContext().interpretQName(self.__refAttribute)
3075 group_decl = ref_en.modelGroupDefinition()
3076 if group_decl is None:
3077 raise pyxb.SchemaValidationError('Model group reference %s cannot be found' % (ref_en,))
3078
3079 self.__pendingTerm = group_decl.modelGroup()
3080 assert self.__pendingTerm is not None
3081 elif ElementDeclaration == self.__resolvableType:
3082 # 3.9.2 says use 3.3.2, which is Element. The element inside a
3083 # particle is a localElement, so we either get the one it refers
3084 # to (which is top-level), or create a local one here.
3085 if self.__refAttribute is not None:
3086 assert self.__pendingTerm is None
3087 ref_en = self._namespaceContext().interpretQName(self.__refAttribute)
3088 self.__pendingTerm = ref_en.elementDeclaration()
3089 if self.__pendingTerm is None:
3090 raise pyxb.SchemaValidationError('Unable to locate element referenced by %s' % (ref_en,))
3091 assert self.__pendingTerm is not None
3092
3093 # Whether this is a local declaration or one pulled in from the
3094 # global type definition symbol space, its name is now reserved in
3095 # this type.
3096 assert self.__pendingTerm is not None
3097 else:
3098 assert False
3099
3100 self.__term = self.__pendingTerm
3101 assert self.__term is not None
3102 return self
3103
3105 return self.__term is not None
3106
3107 # CFD:Particle
3108 @classmethod
3110 """Create a particle from the given DOM node.
3111
3112 wxs is a Schema instance within which the model group is being
3113 defined.
3114
3115 node is a DOM element. The name must be one of ( 'group',
3116 'element', 'any', 'all', 'choice', 'sequence' ), and the node
3117 must be in the XMLSchema namespace.
3118
3119 scope is the _ScopeDeclaration_mxin context that is assigned
3120 to declarations that appear within the model group. It can be
3121 None, indicating no scope defined, or a complex type
3122 definition.
3123 """
3124 scope = kw['scope']
3125 assert _ScopedDeclaration_mixin.ScopeIsIndeterminate(scope) or isinstance(scope, ComplexTypeDefinition)
3126
3127 kw.update({ 'min_occurs' : 1
3128 , 'max_occurs' : 1
3129 , 'node' : node })
3130
3131 if not Particle.IsParticleNode(node):
3132 raise pyxb.LogicError('Attempted to create particle from illegal element %s' % (node.nodeName,))
3133 attr_val = NodeAttribute(node, 'minOccurs')
3134 if attr_val is not None:
3135 kw['min_occurs'] = datatypes.nonNegativeInteger(attr_val)
3136 attr_val = NodeAttribute(node, 'maxOccurs')
3137 if attr_val is not None:
3138 if 'unbounded' == attr_val:
3139 kw['max_occurs'] = None
3140 else:
3141 kw['max_occurs'] = datatypes.nonNegativeInteger(attr_val)
3142
3143 rv = cls(None, **kw)
3144
3145 kw.pop('node', None)
3146 kw['owner'] = rv
3147
3148 rv.__refAttribute = NodeAttribute(node, 'ref')
3149 rv.__pendingTerm = None
3150 rv.__resolvableType = None
3151 if xsd.nodeIsNamed(node, 'group'):
3152 # 3.9.2 says use 3.8.2, which is ModelGroup. The group
3153 # inside a particle is a groupRef. If there is no group
3154 # with that name, this throws an exception as expected.
3155 if rv.__refAttribute is None:
3156 raise pyxb.SchemaValidationError('group particle without reference')
3157 rv.__resolvableType = ModelGroup
3158 elif xsd.nodeIsNamed(node, 'element'):
3159 if rv.__refAttribute is None:
3160 schema = kw.get('schema')
3161 assert schema is not None
3162 target_namespace = schema.targetNamespaceForNode(node, ElementDeclaration)
3163 incoming_tns = kw.get('target_namespace')
3164 if incoming_tns is not None:
3165 assert incoming_tns == target_namespace
3166 else:
3167 kw['target_namespace'] = target_namespace
3168 rv.__term = ElementDeclaration.CreateFromDOM(node=node, **kw)
3169 else:
3170 # NOTE: 3.3.3 clause 2.2 specifies that if ref is used, all
3171 # the other configuration attributes like nillable and default
3172 # must be absent. We don't even bother looking for them.
3173 rv.__resolvableType = ElementDeclaration
3174 assert not xsd.nodeIsNamed(node.parentNode, 'schema')
3175 elif xsd.nodeIsNamed(node, 'any'):
3176 # 3.9.2 says use 3.10.2, which is Wildcard.
3177 rv.__term = Wildcard.CreateFromDOM(node=node)
3178 elif ModelGroup.IsGroupMemberNode(node):
3179 # Choice, sequence, and all inside a particle are explicit
3180 # groups (or a restriction of explicit group, in the case
3181 # of all)
3182 rv.__term = ModelGroup.CreateFromDOM(node, **kw)
3183 else:
3184 raise pyxb.LogicError('Unhandled node in Particle.CreateFromDOM: %s' % (node.toxml("utf-8"),))
3185
3186 if not rv.isResolved():
3187 rv._queueForResolution('creation')
3188 return rv
3189
3190 # bR:PRT
3195
3196 # aFS:PRT
3198 #print 'aFS:PRT - %s' % (ctd.expandedName(),)
3199 rv = self
3200 assert isinstance(ctd, ComplexTypeDefinition)
3201 maybe_rv = self._clone(owner, ctd._objectOrigin())
3202 term = rv.__term._adaptForScope(maybe_rv, ctd)
3203 do_clone = (self._scope() != ctd) or (rv.__term != term)
3204 if do_clone:
3205 rv = maybe_rv
3206 rv.__term = term
3207 return rv
3208
3210 """A particle has an unresolvable particle if it cannot be
3211 resolved, or if it has resolved to a term which is a model
3212 group that has an unresolvable particle.
3213 """
3214 if not self.isResolved():
3215 return False
3216 return self.term().isAdaptable(ctd)
3217
3218 @classmethod
3220 return xsd.nodeIsNamed(node, 'group', 'all', 'choice', 'sequence')
3221
3222 @classmethod
3224 return xsd.nodeIsNamed(node, 'group', 'all', 'choice', 'sequence', 'element', 'any', *others)
3225
3229
3233 """An XMLSchema U{Wildcard<http://www.w3.org/TR/xmlschema-1/#cParticle>} component."""
3234
3235 NC_any = '##any' #<<< The namespace constraint "##any"
3236 NC_not = '##other' #<<< A flag indicating constraint "##other"
3237 NC_targetNamespace = '##targetNamespace'
3238 NC_local = '##local'
3239
3240 __namespaceConstraint = None
3242 """A constraint on the namespace for the wildcard.
3243
3244 Valid values are:
3245 - L{Wildcard.NC_any}
3246 - A tuple ( L{Wildcard.NC_not}, a_namespace )
3247 - set(of_namespaces)
3248
3249 Note that namespace are represented by
3250 L{Namespace<pyxb.namespace.Namespace>} instances, not the URIs that
3251 actually define a namespace. Absence of a namespace is represented by
3252 C{None}, both in the "not" pair and in the set.
3253 """
3254 return self.__namespaceConstraint
3255
3256 @classmethod
3258 """http://www.w3.org/TR/xmlschema-1/#cos-aw-union"""
3259 assert 0 < len(constraints)
3260 o1 = constraints.pop(0);
3261 while 0 < len(constraints):
3262 o2 = constraints.pop(0);
3263 # 1
3264 if (o1 == o2):
3265 continue
3266 # 2
3267 if (cls.NC_any == o1) or (cls.NC_any == o2):
3268 o1 = cls.NC_any
3269 continue
3270 # 3
3271 if isinstance(o1, set) and isinstance(o2, set):
3272 o1 = o1.union(o2)
3273 continue
3274 # 4
3275 if (isinstance(o1, tuple) and isinstance(o2, tuple)) and (o1[1] != o2[1]):
3276 o1 = ( cls.NC_not, None )
3277 continue
3278 # At this point, one must be a negated namespace and the
3279 # other a set. Identify them.
3280 c_tuple = None
3281 c_set = None
3282 if isinstance(o1, tuple):
3283 assert isinstance(o2, set)
3284 c_tuple = o1
3285 c_set = o2
3286 else:
3287 assert isinstance(o1, set)
3288 assert isinstance(o2, tuple)
3289 c_tuple = o2
3290 c_set = o1
3291 negated_ns = c_tuple[1]
3292 if negated_ns is not None:
3293 # 5.1
3294 if (negated_ns in c_set) and (None in c_set):
3295 o1 = cls.NC_any
3296 continue
3297 # 5.2
3298 if negated_ns in c_set:
3299 o1 = ( cls.NC_not, None )
3300 continue
3301 # 5.3
3302 if None in c_set:
3303 raise pyxb.SchemaValidationError('Union of wildcard namespace constraints not expressible')
3304 o1 = c_tuple
3305 continue
3306 # 6
3307 if None in c_set:
3308 o1 = cls.NC_any
3309 else:
3310 o1 = ( cls.NC_not, None )
3311 return o1
3312
3313 @classmethod
3315 """http://www.w3.org/TR/xmlschema-1/#cos-aw-intersect"""
3316 assert 0 < len(constraints)
3317 o1 = constraints.pop(0);
3318 while 0 < len(constraints):
3319 o2 = constraints.pop(0);
3320 # 1
3321 if (o1 == o2):
3322 continue
3323 # 2
3324 if (cls.NC_any == o1) or (cls.NC_any == o2):
3325 if cls.NC_any == o1:
3326 o1 = o2
3327 continue
3328 # 4
3329 if isinstance(o1, set) and isinstance(o2, set):
3330 o1 = o1.intersection(o2)
3331 continue
3332 if isinstance(o1, tuple) and isinstance(o2, tuple):
3333 ns1 = o1[1]
3334 ns2 = o2[1]
3335 # 5
3336 if (ns1 is not None) and (ns2 is not None) and (ns1 != ns2):
3337 raise pyxb.SchemaValidationError('Intersection of wildcard namespace constraints not expressible')
3338 # 6
3339 assert (ns1 is None) or (ns2 is None)
3340 if ns1 is None:
3341 assert ns2 is not None
3342 o1 = ( cls.NC_not, ns2 )
3343 else:
3344 assert ns1 is not None
3345 o1 = ( cls.NC_not, ns1 )
3346 continue
3347 # 3
3348 # At this point, one must be a negated namespace and the
3349 # other a set. Identify them.
3350 c_tuple = None
3351 c_set = None
3352 if isinstance(o1, tuple):
3353 assert isinstance(o2, set)
3354 c_tuple = o1
3355 c_set = o2
3356 else:
3357 assert isinstance(o1, set)
3358 assert isinstance(o2, tuple)
3359 c_tuple = o2
3360 c_set = o1
3361 negated_ns = c_tuple[1]
3362 if negated_ns in c_set:
3363 c_set.remove(negated_ns)
3364 if None in c_set:
3365 c_set.remove(None)
3366 o1 = c_set
3367 return o1
3368
3369 PC_skip = 'skip' #<<< No constraint is applied
3370 PC_lax = 'lax' #<<< Validate against available uniquely determined declaration
3371 PC_strict = 'strict' #<<< Validate against declaration or xsi:type which must be available
3372
3373 # One of PC_*
3374 __processContents = None
3376
3381
3385
3387 assert 0 == len(args)
3388 super(Wildcard, self).__init__(*args, **kw)
3389 self.__namespaceConstraint = kw['namespace_constraint']
3390 self.__processContents = kw['process_contents']
3391
3394
3395 # aFS:WC
3399
3400 # CFD:Wildcard
3401 @classmethod
3403 namespace_context = pyxb.namespace.resolution.NamespaceContext.GetNodeContext(node)
3404 assert xsd.nodeIsNamed(node, 'any', 'anyAttribute')
3405 nc = NodeAttribute(node, 'namespace')
3406 if nc is None:
3407 namespace_constraint = cls.NC_any
3408 else:
3409 if cls.NC_any == nc:
3410 namespace_constraint = cls.NC_any
3411 elif cls.NC_not == nc:
3412 namespace_constraint = ( cls.NC_not, namespace_context.targetNamespace() )
3413 else:
3414 ncs = set()
3415 for ns_uri in nc.split():
3416 if cls.NC_local == ns_uri:
3417 ncs.add(None)
3418 elif cls.NC_targetNamespace == ns_uri:
3419 ncs.add(namespace_context.targetNamespace())
3420 else:
3421 ncs.add(pyxb.namespace.NamespaceForURI(ns_uri, create_if_missing=True))
3422 namespace_constraint = frozenset(ncs)
3423
3424 pc = NodeAttribute(node, 'processContents')
3425 if pc is None:
3426 process_contents = cls.PC_strict
3427 else:
3428 if pc in [ cls.PC_skip, cls.PC_lax, cls.PC_strict ]:
3429 process_contents = pc
3430 else:
3431 raise pyxb.SchemaValidationError('illegal value "%s" for any processContents attribute' % (pc,))
3432
3433 rv = cls(node=node, namespace_constraint=namespace_constraint, process_contents=process_contents, **kw)
3434 rv._annotationFromDOM(node)
3435 return rv
3436
3437 # 3.11.1
3438 -class IdentityConstraintDefinition (_SchemaComponent_mixin, _NamedComponent_mixin, _Annotated_mixin, pyxb.namespace.resolution._Resolvable_mixin):
3439 """An XMLSchema U{Identity Constraint Definition<http://www.w3.org/TR/xmlschema-1/#cIdentity-constraint_Definitions>} component."""
3440
3441 ICC_KEY = 0x01
3442 ICC_KEYREF = 0x02
3443 ICC_UNIQUE = 0x04
3444
3445 __identityConstraintCategory = None
3447
3448 __selector = None
3450
3451 __fields = None
3453
3454 __referencedKey = None
3455
3456 __annotations = None
3458
3459 # CFD:ICD CFD:IdentityConstraintDefinition
3460 @classmethod
3462 name = NodeAttribute(node, 'name')
3463 scope = kw['scope']
3464 assert _ScopedDeclaration_mixin.ScopeIsIndeterminate(scope) or _ScopedDeclaration_mixin.IsValidScope(scope)
3465 rv = cls(name=name, node=node, **kw)
3466
3467 kw.pop('node', None)
3468 kw['owner'] = rv
3469
3470 #self._annotationFromDOM(node);
3471 rv.__isResolved = True
3472 icc = None
3473 if xsd.nodeIsNamed(node, 'key'):
3474 icc = rv.ICC_KEY
3475 elif xsd.nodeIsNamed(node, 'keyref'):
3476 icc = rv.ICC_KEYREF
3477 rv.__referAttribute = NodeAttribute(node, 'refer')
3478 if rv.__referAttribute is None:
3479 raise pyxb.SchemaValidationError('Require refer attribute on keyref elements')
3480 rv.__isResolved = False
3481 elif xsd.nodeIsNamed(node, 'unique'):
3482 icc = rv.ICC_UNIQUE
3483 else:
3484 raise pyxb.LogicError('Unexpected identity constraint node %s' % (node.toxml("utf-8"),))
3485 rv.__icc = icc
3486
3487 cn = LocateUniqueChild(node, 'selector')
3488 rv.__selector = NodeAttribute(cn, 'xpath')
3489 if rv.__selector is None:
3490 raise pyxb.SchemaValidationError('selector element missing xpath attribute')
3491
3492 rv.__fields = []
3493 for cn in LocateMatchingChildren(node, 'field'):
3494 xp_attr = NodeAttribute(cn, 'xpath')
3495 if xp_attr is None:
3496 raise pyxb.SchemaValidationError('field element missing xpath attribute')
3497 rv.__fields.append(xp_attr)
3498
3499 rv._annotationFromDOM(node)
3500 rv.__annotations = []
3501 if rv.annotation() is not None:
3502 rv.__annotations.append(rv)
3503
3504 for cn in node.childNodes:
3505 if (Node.ELEMENT_NODE != cn.nodeType):
3506 continue
3507 an = None
3508 if xsd.nodeIsNamed(cn, 'selector', 'field'):
3509 an = LocateUniqueChild(cn, 'annotation')
3510 elif xsd.nodeIsNamed(cn, 'annotation'):
3511 an = cn
3512 if an is not None:
3513 rv.__annotations.append(Annotation.CreateFromDOM(an, **kw))
3514
3515 rv.__identityConstraintCategory = icc
3516 if rv.ICC_KEYREF != rv.__identityConstraintCategory:
3517 rv._namespaceContext().targetNamespace().addCategoryObject('identityConstraintDefinition', rv.name(), rv)
3518
3519 if not rv.isResolved():
3520 rv._queueForResolution('creation')
3521 return rv
3522
3523 __isResolved = False
3525 return self.__isResolved
3526
3527 # res:ICD res:IdentityConstraintDefinition
3529 if self.isResolved():
3530 return self
3531
3532 icc = self.__icc
3533 if self.ICC_KEYREF == icc:
3534 refer_en = self._namespaceContext().interpretQName(self.__referAttribute)
3535 refer = refer_en.identityConstraintDefinition()
3536 if refer is None:
3537 self._queueForResolution('Identity constraint definition %s cannot be found' % (refer_en,), depends_on=refer)
3538 return self
3539 self.__referencedKey = refer
3540 self.__isResolved = True
3541 return self
3542
3543 # bR:ICD
3545 """Constraint definitions that are by reference require the referenced constraint."""
3546 rv = set()
3547 if include_lax and (self.__referencedKey is not None):
3548 rv.add(self.__referencedKey)
3549 return frozenset(rv)
3550
3551
3552
3553 # 3.12.1
3554 -class NotationDeclaration (_SchemaComponent_mixin, _NamedComponent_mixin, _Annotated_mixin):
3555 """An XMLSchema U{Notation Declaration<http://www.w3.org/TR/xmlschema-1/#cNotation_Declarations>} component."""
3556 __systemIdentifier = None
3558
3559 __publicIdentifier = None
3561
3562 # CFD:ND CFD:NotationDeclaration
3563 @classmethod
3565 name = NodeAttribute(node, 'name')
3566 rv = cls(name=name, node=node, **kw)
3567
3568 rv.__systemIdentifier = NodeAttribute(node, 'system')
3569 rv.__publicIdentifier = NodeAttribute(node, 'public')
3570
3571 rv._annotationFromDOM(node)
3572 return rv
3573
3576 """An XMLSchema U{Annotation<http://www.w3.org/TR/xmlschema-1/#cAnnotation>} component."""
3577
3578 __applicationInformation = None
3580 return self.__applicationInformation
3581
3582 __userInformation = None
3584 return self.__userInformation
3585
3586 # Define so superclasses can take keywords
3588 application_information = kw.pop('application_information', None)
3589 user_information = kw.pop('user_information', None)
3590 super(Annotation, self).__init__(**kw)
3591 if (user_information is not None) and (not isinstance(user_information, list)):
3592 user_information = [ unicode(user_information) ]
3593 if (application_information is not None) and (not isinstance(application_information, list)):
3594 application_information = [ unicode(application_information) ]
3595 self.__userInformation = user_information
3596 self.__applicationInformation = application_information
3597
3598 # @todo: what the hell is this? From 3.13.2, I think it's a place
3599 # to stuff attributes from the annotation element, which makes
3600 # sense, as well as from the annotation's parent element, which
3601 # doesn't. Apparently it's for attributes that don't belong to
3602 # the XMLSchema namespace; so maybe we're not supposed to add
3603 # those to the other components. Note that these are attribute
3604 # information items, not attribute uses.
3605 __attributes = None
3606
3607 # CFD:Annotation
3608 @classmethod
3610 rv = cls(node=node, **kw)
3611
3612 # @todo:: Scan for attributes in the node itself that do not
3613 # belong to the XMLSchema namespace.
3614
3615 # Node should be an XMLSchema annotation node
3616 assert xsd.nodeIsNamed(node, 'annotation')
3617 app_info = []
3618 user_info = []
3619 for cn in node.childNodes:
3620 if xsd.nodeIsNamed(cn, 'appinfo'):
3621 app_info.append(cn)
3622 elif xsd.nodeIsNamed(cn, 'documentation'):
3623 user_info.append(cn)
3624 else:
3625 pass
3626 if 0 < len(app_info):
3627 rv.__applicationInformation = app_info
3628 if 0 < len(user_info):
3629 rv.__userInformation = user_info
3630
3631 return rv
3632
3633 __RemoveMultiQuote_re = re.compile('""+')
3635 """Return the text in a form suitable for embedding in a
3636 triple-double-quoted docstring.
3637
3638 Any sequence of two or more double quotes is replaced by a sequence of
3639 single quotes that is the same length. Following this, spaces are
3640 added at the start and the end as necessary to ensure a double quote
3641 does not appear in those positions."""
3642 rv = self.text()
3643 rv = self.__RemoveMultiQuote_re.sub(lambda _mo: "'" * (_mo.end(0) - _mo.start(0)), rv)
3644 if rv.startswith('"'):
3645 rv = ' ' + rv
3646 if rv.endswith('"'):
3647 rv = rv + ' '
3648 return rv
3649
3651 if self.__userInformation is None:
3652 return ''
3653 text = []
3654 # Values in userInformation are DOM "documentation" elements.
3655 # We want their combined content.
3656 for dn in self.__userInformation:
3657 for cn in dn.childNodes:
3658 if Node.TEXT_NODE == cn.nodeType:
3659 text.append(cn.data)
3660 return ''.join(text)
3661
3663 """Return the catenation of all user information elements in the
3664 annotation as a single unicode string. Returns the empty string if
3665 there are no user information elements."""
3666 return self.text()
3667 text = []
3668 if not self.__userInformation:
3669 return ''
3670
3671 # Section 3.14.
3672 -class SimpleTypeDefinition (_SchemaComponent_mixin, _NamedComponent_mixin, pyxb.namespace.resolution._Resolvable_mixin, _Annotated_mixin):
3673 """An XMLSchema U{Simple Type Definition<http://www.w3.org/TR/xmlschema-1/#Simple_Type_Definitions>} component."""
3674
3675 # Reference to the SimpleTypeDefinition on which this is based.
3676 # The value must be non-None except for the simple ur-type
3677 # definition.
3678 __baseTypeDefinition = None
3680 return self.__baseTypeDefinition
3681
3682 # A map from a subclass of facets.Facet to an instance of that class.
3683 # Presence of a facet class as a key in this map is the indicator that the
3684 # type definition and its subtypes are permitted to use the corresponding
3685 # facet. All facets in force for this type are present in the map,
3686 # including those constraints inherited parent types.
3687 __facets = None
3689 assert (self.__facets is None) or (type(self.__facets) == types.DictType)
3690 return self.__facets
3691
3692 # The facets.FundamentalFacet instances that describe this type
3693 __fundamentalFacets = None
3695 """A frozenset of instances of facets.FundamentallFacet."""
3696 return self.__fundamentalFacets
3697
3698 STD_empty = 0 #<<< Marker indicating an empty set of STD forms
3699 STD_extension = 0x01 #<<< Representation for extension in a set of STD forms
3700 STD_list = 0x02 #<<< Representation for list in a set of STD forms
3701 STD_restriction = 0x04 #<<< Representation of restriction in a set of STD forms
3702 STD_union = 0x08 #<<< Representation of union in a set of STD forms
3703
3704 _STD_Map = { 'extension' : STD_extension
3705 , 'list' : STD_list
3706 , 'restriction' : STD_restriction
3707 , 'union' : STD_union }
3708
3709 # Bitmask defining the subset that comprises the final property
3710 __final = STD_empty
3711 @classmethod
3713 """Convert a final value to a string."""
3714 tags = []
3715 if final_value & cls.STD_extension:
3716 tags.append('extension')
3717 if final_value & cls.STD_list:
3718 tags.append('list')
3719 if final_value & cls.STD_restriction:
3720 tags.append('restriction')
3721 if final_value & cls.STD_union:
3722 tags.append('union')
3723 return ' '.join(tags)
3724
3725 VARIETY_absent = 0x01 #<<< Only used for the ur-type
3726 VARIETY_atomic = 0x02 #<<< Use for types based on a primitive type
3727 VARIETY_list = 0x03 #<<< Use for lists of atomic-variety types
3728 VARIETY_union = 0x04 #<<< Use for types that aggregate other types
3729
3730 # Derivation alternative
3731 _DA_empty = 'none specified'
3732 _DA_restriction = 'restriction'
3733 _DA_list = 'list'
3734 _DA_union = 'union'
3735
3737 return self.__derivationAlternative
3738 __derivationAlternative = None
3739
3740 # Identify the sort of value collection this holds. This field is
3741 # used to identify unresolved definitions.
3742 __variety = None
3744 return self.__variety
3745 @classmethod
3747 """Convert a variety value to a string."""
3748 if cls.VARIETY_absent == variety:
3749 return 'absent'
3750 if cls.VARIETY_atomic == variety:
3751 return 'atomic'
3752 if cls.VARIETY_list == variety:
3753 return 'list'
3754 if cls.VARIETY_union == variety:
3755 return 'union'
3756 return '?NoVariety?'
3757
3758 # For atomic variety only, the root (excepting ur-type) type.
3759 __primitiveTypeDefinition = None
3761 if throw_if_absent:
3762 if self.variety() != self.VARIETY_atomic:
3763 raise pyxb.BadPropertyError('[%s] primitiveTypeDefinition only defined for atomic types' % (self.name(), self.variety()))
3764 if self.__primitiveTypeDefinition is None:
3765 raise pyxb.LogicError('Expected primitive type for %s in %s', self, self.targetNamespace())
3766 return self.__primitiveTypeDefinition
3767
3768 # For list variety only, the type of items in the list
3769 __itemTypeDefinition = None
3771 if self.VARIETY_list != self.variety():
3772 raise pyxb.BadPropertyError('itemTypeDefinition only defined for list types')
3773 if self.__itemTypeDefinition is None:
3774 raise pyxb.LogicError('Expected item type')
3775 return self.__itemTypeDefinition
3776
3777 # For union variety only, the sequence of candidate members
3778 __memberTypeDefinitions = None
3780 if self.VARIETY_union != self.variety():
3781 raise pyxb.BadPropertyError('memberTypeDefinitions only defined for union types')
3782 if self.__memberTypeDefinitions is None:
3783 raise pyxb.LogicError('Expected member types')
3784 return self.__memberTypeDefinitions
3785
3786 # bR:STD
3788 """Implement base class method.
3789
3790 This STD depends on its baseTypeDefinition, unless its variety
3791 is absent. Other dependencies are on item, primitive, or
3792 member type definitions."""
3793 type_definitions = set()
3794 if self != self.baseTypeDefinition():
3795 type_definitions.add(self.baseTypeDefinition())
3796 if self.VARIETY_absent == self.variety():
3797 type_definitions = set()
3798 elif self.VARIETY_atomic == self.variety():
3799 if self != self.primitiveTypeDefinition():
3800 type_definitions.add(self.primitiveTypeDefinition())
3801 elif self.VARIETY_list == self.variety():
3802 assert self != self.itemTypeDefinition()
3803 type_definitions.add(self.itemTypeDefinition())
3804 elif self.VARIETY_union == self.variety():
3805 assert self not in self.memberTypeDefinitions()
3806 type_definitions.update(self.memberTypeDefinitions())
3807 else:
3808 raise pyxb.LogicError('Unable to identify dependent types: variety %s' % (self.variety(),))
3809 # NB: This type also depends on the value type definitions for
3810 # any facets that apply to it. This fact only matters when
3811 # generating the datatypes_facets source. That, and the fact
3812 # that there are dependency loops (e.g., integer requires a
3813 # nonNegativeInteger for its length facet) means we don't
3814 # bother adding in those.
3815 return frozenset(type_definitions)
3816
3817 # A non-property field that holds a reference to the DOM node from
3818 # which the type is defined. The value is held only between the
3819 # point where the simple type definition instance is created until
3820 # the point it is resolved.
3821 __domNode = None
3822
3823 # Indicate that this instance was defined as a built-in rather
3824 # than from a DOM instance.
3825 __isBuiltin = False
3826
3827 # Allocate one of these. Users should use one of the Create*
3828 # factory methods instead.
3829
3833
3835 """Extend base class unpickle support to retain link between
3836 this instance and the Python class that it describes.
3837
3838 This is because the pythonSupport value is a class reference,
3839 not an instance reference, so it wasn't deserialized, and its
3840 class member link was never set.
3841 """
3842 super_fn = getattr(super(SimpleTypeDefinition, self), '__setstate__', lambda _state: self.__dict__.update(_state))
3843 super_fn(state)
3844 if self.__pythonSupport is not None:
3845 self.__pythonSupport._SimpleTypeDefinition(self)
3846
3848 if self.name() is not None:
3849 elts = [ self.name(), ':' ]
3850 else:
3851 elts = [ '<anonymous>:' ]
3852 if self.VARIETY_absent == self.variety():
3853 elts.append('the ur-type')
3854 elif self.VARIETY_atomic == self.variety():
3855 elts.append('restriction of %s' % (self.baseTypeDefinition().name(),))
3856 elif self.VARIETY_list == self.variety():
3857 elts.append('list of %s' % (self.itemTypeDefinition().name(),))
3858 elif self.VARIETY_union == self.variety():
3859 elts.append('union of %s' % (" ".join([str(_mtd.name()) for _mtd in self.memberTypeDefinitions()],)))
3860 else:
3861 # Gets here if the type has not been resolved.
3862 elts.append('?')
3863 #raise pyxb.LogicError('Unexpected variety %s' % (self.variety(),))
3864 if self.__facets:
3865 felts = []
3866 for (k, v) in self.__facets.items():
3867 if v is not None:
3868 felts.append(str(v))
3869 elts.append("\n %s" % (','.join(felts),))
3870 if self.__fundamentalFacets:
3871 elts.append("\n ")
3872 elts.append(','.join( [str(_f) for _f in self.__fundamentalFacets ]))
3873 return 'STD[%s]' % (''.join(elts),)
3874
3876 """Override fields in this instance with those from the other.
3877
3878 This method is invoked only by Schema._addNamedComponent, and
3879 then only when a built-in type collides with a schema-defined
3880 type. Material like facets is not (currently) held in the
3881 built-in copy, so the DOM information is copied over to the
3882 built-in STD, which is subsequently re-resolved.
3883
3884 Returns self.
3885 """
3886 assert self != other
3887 assert self.isNameEquivalent(other)
3888 super(SimpleTypeDefinition, self)._updateFromOther_csc(other)
3889
3890 # The other STD should be an unresolved schema-defined type.
3891 assert other.__baseTypeDefinition is None, 'Update from resolved STD %s' % (other,)
3892 assert other.__domNode is not None
3893 self.__domNode = other.__domNode
3894
3895 # Preserve the python support
3896 if other.__pythonSupport is not None:
3897 # @todo: ERROR multiple references
3898 self.__pythonSupport = other.__pythonSupport
3899
3900 # Mark this instance as unresolved so it is re-examined
3901 self.__variety = None
3902 return self
3903
3907
3908 __SimpleUrTypeDefinition = None
3909 @classmethod
3911 """Create the SimpleTypeDefinition instance that approximates the simple ur-type.
3912
3913 See section 3.14.7."""
3914
3915 #if in_builtin_definition and (cls.__SimpleUrTypeDefinition is not None):
3916 # raise pyxb.LogicError('Multiple definitions of SimpleUrType')
3917 if cls.__SimpleUrTypeDefinition is None:
3918 # Note: We use a singleton subclass
3919 assert schema is not None
3920
3921 ns_ctx = schema.targetNamespace().initialNamespaceContext()
3922
3923 kw = { 'name' : 'anySimpleType',
3924 'schema' : schema,
3925 'namespace_context' : ns_ctx,
3926 'binding_namespace' : schema.targetNamespace(),
3927 'variety' : cls.VARIETY_absent,
3928 'scope' : _ScopedDeclaration_mixin.SCOPE_global }
3929 bi = _SimpleUrTypeDefinition(**kw)
3930 bi._setPythonSupport(datatypes.anySimpleType)
3931
3932 # The baseTypeDefinition is the ur-type.
3933 bi.__baseTypeDefinition = ComplexTypeDefinition.UrTypeDefinition()
3934 # The simple ur-type has an absent variety, not an atomic
3935 # variety, so does not have a primitiveTypeDefinition
3936
3937 # No facets on the ur type
3938 bi.__facets = {}
3939 bi.__fundamentalFacets = frozenset()
3940
3941 bi.__resolveBuiltin()
3942
3943 cls.__SimpleUrTypeDefinition = bi
3944 return cls.__SimpleUrTypeDefinition
3945
3946 @classmethod
3948 """Create STD instances for built-in types.
3949
3950 For example, xml:space is a restriction of NCName; xml:lang is a union.
3951
3952 """
3953 import pyxb.binding.xml_
3954 kw = { 'schema' : schema,
3955 'binding_namespace' : schema.targetNamespace(),
3956 'namespace_context' : schema.targetNamespace().initialNamespaceContext(),
3957 'scope' : _ScopedDeclaration_mixin.SCOPE_global,
3958 'variety' : cls.VARIETY_atomic }
3959 ns_ctx = pyxb.namespace.XML.initialNamespaceContext()
3960 if 'space' == name:
3961 bi = cls(**kw)
3962 bi.__derivationAlternative = cls._DA_restriction
3963 bi.__baseTypeDefinition = datatypes.NCName.SimpleTypeDefinition()
3964 bi.__primitiveTypeDefinition = bi.__baseTypeDefinition.__primitiveTypeDefinition
3965 bi._setPythonSupport(pyxb.binding.xml_.STD_ANON_space)
3966 bi.setNameInBinding('STD_ANON_space')
3967 elif 'lang' == name:
3968 bi = cls(**kw)
3969 bi.__baseTypeDefinition = cls.SimpleUrTypeDefinition()
3970 bi.__memberTypes = [ datatypes.language.SimpleTypeDefinition() ]
3971 bi.__derivationAlternative = cls._DA_union
3972 bi.__primitiveTypeDefinition = bi
3973 bi._setPythonSupport(pyxb.binding.xml_.STD_ANON_lang)
3974 bi.setNameInBinding('STD_ANON_lang')
3975 else:
3976 raise pyxb.IncompleteImplementationError('No implementation for %s' % (name,))
3977 bi.__facets = { }
3978 for v in bi.pythonSupport().__dict__.values():
3979 if isinstance(v, facets.ConstrainingFacet):
3980 bi.__facets[v.__class__] = v
3981 return bi
3982
3983 @classmethod
3985 """Create a primitive simple type in the target namespace.
3986
3987 This is mainly used to pre-load standard built-in primitive
3988 types, such as those defined by XMLSchema Datatypes. You can
3989 use it for your own schemas as well, if you have special types
3990 that require explicit support to for Pythonic conversion.
3991
3992 All parameters are required and must be non-None.
3993 """
3994
3995 kw = { 'name' : name,
3996 'schema' : schema,
3997 'binding_namespace' : schema.targetNamespace(),
3998 'namespace_context' : schema.targetNamespace().initialNamespaceContext(),
3999 'scope' : _ScopedDeclaration_mixin.SCOPE_global,
4000 'variety' : cls.VARIETY_atomic }
4001
4002 bi = cls(**kw)
4003 bi._setPythonSupport(python_support)
4004
4005 # Primitive types are based on the ur-type, and have
4006 # themselves as their primitive type definition.
4007 bi.__baseTypeDefinition = cls.SimpleUrTypeDefinition()
4008 bi.__primitiveTypeDefinition = bi
4009
4010 # Primitive types are built-in
4011 bi.__resolveBuiltin()
4012 assert bi.isResolved()
4013 return bi
4014
4015 @classmethod
4017 """Create a derived simple type in the target namespace.
4018
4019 This is used to pre-load standard built-in derived types. You
4020 can use it for your own schemas as well, if you have special
4021 types that require explicit support to for Pythonic
4022 conversion.
4023 """
4024 assert parent_std
4025 assert parent_std.__variety in (cls.VARIETY_absent, cls.VARIETY_atomic)
4026 kw = { 'name' : name,
4027 'schema' : schema,
4028 'binding_namespace' : schema.targetNamespace(),
4029 'namespace_context' : schema.targetNamespace().initialNamespaceContext(),
4030 'scope' : _ScopedDeclaration_mixin.SCOPE_global,
4031 'variety' : parent_std.__variety }
4032
4033 bi = cls(**kw)
4034 bi._setPythonSupport(python_support)
4035
4036 # We were told the base type. If this is atomic, we re-use
4037 # its primitive type. Note that these all may be in different
4038 # namespaces.
4039 bi.__baseTypeDefinition = parent_std
4040 if cls.VARIETY_atomic == bi.__variety:
4041 bi.__primitiveTypeDefinition = bi.__baseTypeDefinition.__primitiveTypeDefinition
4042
4043 # Derived types are built-in
4044 bi.__resolveBuiltin()
4045 return bi
4046
4047 @classmethod
4049 """Create a list simple type in the target namespace.
4050
4051 This is used to preload standard built-in list types. You can
4052 use it for your own schemas as well, if you have special types
4053 that require explicit support to for Pythonic conversion; but
4054 note that such support is identified by the item_std.
4055 """
4056
4057 kw = { 'name' : name,
4058 'schema' : schema,
4059 'binding_namespace' : schema.targetNamespace(),
4060 'namespace_context' : schema.targetNamespace().initialNamespaceContext(),
4061 'scope' : _ScopedDeclaration_mixin.SCOPE_global,
4062 'variety' : cls.VARIETY_list }
4063 bi = cls(**kw)
4064 bi._setPythonSupport(python_support)
4065
4066 # The base type is the ur-type. We were given the item type.
4067 bi.__baseTypeDefinition = cls.SimpleUrTypeDefinition()
4068 assert item_std
4069 bi.__itemTypeDefinition = item_std
4070
4071 # List types are built-in
4072 bi.__resolveBuiltin()
4073 return bi
4074
4075 @classmethod
4077 """(Placeholder) Create a union simple type in the target namespace.
4078
4079 This function has not been implemented."""
4080 raise pyxb.IncompleteImplementationError('No support for built-in union types')
4081
4083 simple_type_child = None
4084 for cn in body.childNodes:
4085 if (Node.ELEMENT_NODE == cn.nodeType):
4086 if not xsd.nodeIsNamed(cn, 'simpleType'):
4087 if other_elts_ok:
4088 continue
4089 raise pyxb.SchemaValidationError('Context requires element to be xs:simpleType')
4090 assert not simple_type_child
4091 simple_type_child = cn
4092 if simple_type_child is None:
4093 raise pyxb.SchemaValidationError('Content requires an xs:simpleType member (or a base attribute)')
4094 return simple_type_child
4095
4096 # The __initializeFrom* methods are responsible for identifying
4097 # the variety and the baseTypeDefinition. The remainder of the
4098 # resolution is performed by the __completeResolution method.
4099 # Note that in some cases resolution might yet be premature, so
4100 # variety is not saved until it is complete. All this stuff is
4101 # from section 3.14.2.
4102
4104 self.__baseTypeDefinition = self.SimpleUrTypeDefinition()
4105 self.__itemTypeAttribute = NodeAttribute(body, 'itemType')
4106 if self.__itemTypeAttribute is None:
4107 # NOTE: The newly created anonymous item type will
4108 # not be resolved; the caller needs to handle
4109 # that.
4110 self.__itemTypeDefinition = self.CreateFromDOM(self.__singleSimpleTypeChild(body), **kw)
4111 return self.__completeResolution(body, self.VARIETY_list, self._DA_list)
4112
4114 if self.__baseTypeDefinition is None:
4115 self.__baseAttribute = NodeAttribute(body, 'base')
4116 if self.__baseAttribute is None:
4117 self.__baseTypeDefinition = self.CreateFromDOM(self.__singleSimpleTypeChild(body, other_elts_ok=True), **kw)
4118 return self.__completeResolution(body, None, self._DA_restriction)
4119
4120 __localMemberTypes = None
4122 self.__baseTypeDefinition = self.SimpleUrTypeDefinition()
4123 self.__memberTypesAttribute = NodeAttribute(body, 'memberTypes')
4124 if self.__localMemberTypes is None:
4125 self.__localMemberTypes = []
4126 for cn in body.childNodes:
4127 if (Node.ELEMENT_NODE == cn.nodeType) and xsd.nodeIsNamed(cn, 'simpleType'):
4128 self.__localMemberTypes.append(self.CreateFromDOM(cn, **kw))
4129 return self.__completeResolution(body, self.VARIETY_union, self._DA_union)
4130
4132 if self.hasPythonSupport():
4133 self.__facets = { }
4134 for v in self.pythonSupport().__dict__.values():
4135 if isinstance(v, facets.ConstrainingFacet):
4136 #print 'Adding facet %s to %s' % (v, self.name())
4137 self.__facets[v.__class__] = v
4138 if v.ownerTypeDefinition() is None:
4139 v.setFromKeywords(_constructor=True, owner_type_definition=self)
4140 self.__isBuiltin = True
4141 return self
4142
4144 """Create facets for varieties that can take facets that are undeclared.
4145
4146 This means unions, which per section 4.1.2.3 of
4147 http://www.w3.org/TR/xmlschema-2/ can have enumeration or
4148 pattern restrictions."""
4149 if self.VARIETY_union != variety:
4150 return self
4151 self.__facets.setdefault(facets.CF_pattern)
4152 self.__facets.setdefault(facets.CF_enumeration)
4153 return self
4154
4156 """Identify the facets and properties for this stype.
4157
4158 This method simply identifies the facets that apply to this
4159 specific type, and records property values. Only
4160 explicitly-associated facets and properties are stored; others
4161 from base types will also affect this type. The information
4162 is taken from the applicationInformation children of the
4163 definition's annotation node, if any. If there is no support
4164 for the XMLSchema_hasFacetAndProperty namespace, this is a
4165 no-op.
4166
4167 Upon return, self.__facets is a map from the class for an
4168 associated fact to None, and self.__fundamentalFacets is a
4169 frozenset of instances of FundamentalFacet.
4170
4171 The return value is self.
4172 """
4173 self.__facets = { }
4174 self.__fundamentalFacets = frozenset()
4175 if self.annotation() is None:
4176 return self.__defineDefaultFacets(variety)
4177 app_info = self.annotation().applicationInformation()
4178 if app_info is None:
4179 return self.__defineDefaultFacets(variety)
4180 facet_map = { }
4181 fundamental_facets = set()
4182 seen_facets = set()
4183 for ai in app_info:
4184 for cn in ai.childNodes:
4185 if Node.ELEMENT_NODE != cn.nodeType:
4186 continue
4187 if pyxb.namespace.XMLSchema_hfp.nodeIsNamed(cn, 'hasFacet'):
4188 facet_name = NodeAttribute(cn, 'name')# , pyxb.namespace.XMLSchema_hfp)
4189 if facet_name is None:
4190 raise pyxb.SchemaValidationError('hasFacet missing name attribute in %s' % (cn,))
4191 if facet_name in seen_facets:
4192 raise pyxb.SchemaValidationError('Multiple hasFacet specifications for %s' % (facet_name,))
4193 seen_facets.add(facet_name)
4194 facet_class = facets.ConstrainingFacet.ClassForFacet(facet_name)
4195 #facet_map[facet_class] = facet_class(base_type_definition=self)
4196 facet_map[facet_class] = None
4197 if pyxb.namespace.XMLSchema_hfp.nodeIsNamed(cn, 'hasProperty'):
4198 fundamental_facets.add(facets.FundamentalFacet.CreateFromDOM(cn, self))
4199 if 0 < len(facet_map):
4200 assert self.__baseTypeDefinition == self.SimpleUrTypeDefinition()
4201 self.__facets = facet_map
4202 assert type(self.__facets) == types.DictType
4203 if 0 < len(fundamental_facets):
4204 self.__fundamentalFacets = frozenset(fundamental_facets)
4205 return self
4206
4207 # NB: Must be done after resolution of the base type
4209
4210 # Create local list consisting of facet classes matched in children
4211 # and the map of keywords used to initialize the local instance.
4212
4213 local_facets = {}
4214 for fc in facets.Facet.Facets:
4215 children = LocateMatchingChildren(body, fc.Name())
4216 if 0 < len(children):
4217 fi = fc(base_type_definition=self.__baseTypeDefinition,
4218 owner_type_definition=self)
4219 if isinstance(fi, facets._LateDatatype_mixin):
4220 fi.bindValueDatatype(self)
4221 for cn in children:
4222 kw = { 'annotation': LocateUniqueChild(cn, 'annotation') }
4223 for ai in range(0, cn.attributes.length):
4224 attr = cn.attributes.item(ai)
4225 # Convert name from unicode to string
4226 kw[str(attr.localName)] = attr.value
4227 try:
4228 fi.setFromKeywords(**kw)
4229 except pyxb.PyXBException, e:
4230 raise pyxb.SchemaValidationError('Error assigning facet %s in %s: %s' % (fc.Name(), self.expandedName(), e))
4231 local_facets[fc] = fi
4232 self.__localFacets = local_facets
4233
4234 # We want a map from the union of the facet classes from this STD up
4235 # through its baseTypeDefinition (if present). Map elements should be
4236 # to None if the facet has not been constrained, or to the nearest
4237 # ConstrainingFacet instance if it is. ConstrainingFacet instances
4238 # created for local constraints also need a pointer to the
4239 # corresponding facet from the ancestor type definition, because those
4240 # constraints also affect this type.
4241 base_facets = {}
4242
4243 # Built-ins didn't get their facets() setting configured, so use the
4244 # _FacetMap() instead.
4245 if self.__baseTypeDefinition.isBuiltin():
4246 pstd = self.__baseTypeDefinition.pythonSupport()
4247 if pstd != datatypes.anySimpleType:
4248 base_facets.update(pstd._FacetMap())
4249 elif self.__baseTypeDefinition.facets():
4250 assert type(self.__baseTypeDefinition.facets()) == types.DictType
4251 base_facets.update(self.__baseTypeDefinition.facets())
4252 base_facets.update(self.facets())
4253
4254 self.__facets = self.__localFacets
4255 for fc in base_facets.keys():
4256 self.__facets.setdefault(fc, base_facets[fc])
4257 assert type(self.__facets) == types.DictType
4258
4260 """Create a new simple type with this as its base.
4261
4262 The type is owned by the provided owner, and may have facet
4263 restrictions defined by the body.
4264 @param owner: the owner for the newly created type
4265 @type owner: L{ComplexTypeDefinition}
4266 @param body: the DOM node from which facet information will be extracted
4267 @type body: C{xml.dom.Node}
4268 @rtype: L{SimpleTypeDefinition}
4269 """
4270 std = SimpleTypeDefinition(owner=owner, namespace_context=owner._namespaceContext(), variety=None, scope=self._scope(), schema=owner._schema())
4271 std.__baseTypeDefinition = self
4272 return std.__completeResolution(body, None, self._DA_restriction)
4273
4274 # Complete the resolution of some variety of STD. Note that the
4275 # variety is compounded by an alternative, since there is no
4276 # 'restriction' variety.
4278 assert self.__variety is None
4279 if self.__baseTypeDefinition is None:
4280 assert self.__baseAttribute is not None
4281 base_en = self._namespaceContext().interpretQName(self.__baseAttribute)
4282 base_type = base_en.typeDefinition()
4283 if not isinstance(base_type, SimpleTypeDefinition):
4284 raise pyxb.SchemaValidationError('Unable to locate base type %s' % (base_en,))
4285 self.__baseTypeDefinition = base_type
4286 # If the base type exists but has not yet been resolved,
4287 # delay processing this type until the one it depends on
4288 # has been completed.
4289 assert self.__baseTypeDefinition != self
4290 if not self.__baseTypeDefinition.isResolved():
4291 self._queueForResolution('base type %s is not resolved' % (self.__baseTypeDefinition,), depends_on=self.__baseTypeDefinition)
4292 return self
4293 if variety is None:
4294 # 3.14.1 specifies that the variety is the variety of the base
4295 # type definition which, by the way, can't be the ur type.
4296 variety = self.__baseTypeDefinition.__variety
4297 assert variety is not None
4298
4299 if self.VARIETY_absent == variety:
4300 # The ur-type is always resolved. So are restrictions of it,
4301 # which is how we might get here.
4302 pass
4303 elif self.VARIETY_atomic == variety:
4304 # Atomic types (and their restrictions) use the primitive
4305 # type, which is the highest type that is below the
4306 # ur-type (which is not atomic).
4307 ptd = self
4308 while isinstance(ptd, SimpleTypeDefinition) and (self.VARIETY_atomic == ptd.__baseTypeDefinition.variety()):
4309 ptd = ptd.__baseTypeDefinition
4310
4311 self.__primitiveTypeDefinition = ptd
4312 elif self.VARIETY_list == variety:
4313 if self._DA_list == alternative:
4314 if self.__itemTypeAttribute is not None:
4315 it_en = self._namespaceContext().interpretQName(self.__itemTypeAttribute)
4316 self.__itemTypeDefinition = it_en.typeDefinition()
4317 if not isinstance(self.__itemTypeDefinition, SimpleTypeDefinition):
4318 raise pyxb.SchemaValidationError('Unable to locate STD %s for items' % (it_en,))
4319 elif self._DA_restriction == alternative:
4320 self.__itemTypeDefinition = self.__baseTypeDefinition.__itemTypeDefinition
4321 else:
4322 raise pyxb.LogicError('completeResolution list variety with alternative %s' % (alternative,))
4323 elif self.VARIETY_union == variety:
4324 if self._DA_union == alternative:
4325 # First time we try to resolve, create the member type
4326 # definitions. If something later prevents us from resolving
4327 # this type, we don't want to create them again, because we
4328 # might already have references to them.
4329 if self.__memberTypeDefinitions is None:
4330 mtd = []
4331 # If present, first extract names from memberTypes,
4332 # and add each one to the list
4333 if self.__memberTypesAttribute is not None:
4334 for mn in self.__memberTypesAttribute.split():
4335 # THROW if type has not been defined
4336 mn_en = self._namespaceContext().interpretQName(mn)
4337 std = mn_en.typeDefinition()
4338 if std is None:
4339 raise pyxb.SchemaValidationError('Unable to locate member type %s' % (mn_en,))
4340 # Note: We do not need these to be resolved (here)
4341 assert isinstance(std, SimpleTypeDefinition)
4342 mtd.append(std)
4343 # Now look for local type definitions
4344 mtd.extend(self.__localMemberTypes)
4345 self.__memberTypeDefinitions = mtd
4346 assert None not in self.__memberTypeDefinitions
4347
4348 # Replace any member types that are themselves unions with the
4349 # members of those unions, in order. Note that doing this
4350 # might indicate we can't resolve this type yet, which is why
4351 # we separated the member list creation and the substitution
4352 # phases
4353 mtd = []
4354 for mt in self.__memberTypeDefinitions:
4355 assert isinstance(mt, SimpleTypeDefinition)
4356 if not mt.isResolved():
4357 self._queueForResolution('member type not resolved', depends_on=mt)
4358 return self
4359 if self.VARIETY_union == mt.variety():
4360 mtd.extend(mt.memberTypeDefinitions())
4361 else:
4362 mtd.append(mt)
4363 elif self._DA_restriction == alternative:
4364 assert self.__baseTypeDefinition
4365 # Base type should have been resolved before we got here
4366 assert self.__baseTypeDefinition.isResolved()
4367 mtd = self.__baseTypeDefinition.__memberTypeDefinitions
4368 assert mtd is not None
4369 else:
4370 raise pyxb.LogicError('completeResolution union variety with alternative %s' % (alternative,))
4371 # Save a unique copy
4372 self.__memberTypeDefinitions = mtd[:]
4373 else:
4374 raise pyxb.LogicError('completeResolution with variety 0x%02x' % (variety,))
4375
4376 # Determine what facets, if any, apply to this type. This
4377 # should only do something if this is a primitive type.
4378 self.__processHasFacetAndProperty(variety)
4379 self.__updateFacets(body)
4380
4381 self.__derivationAlternative = alternative
4382 self.__variety = variety
4383 self.__domNode = None
4384 #print 'Completed STD %s' % (self,)
4385 return self
4386
4388 """Indicate whether this simple type is fully defined.
4389
4390 Type resolution for simple types means that the corresponding
4391 schema component fields have been set. Specifically, that
4392 means variety, baseTypeDefinition, and the appropriate
4393 additional fields depending on variety. See _resolve() for
4394 more information.
4395 """
4396 # Only unresolved nodes have an unset variety
4397 return (self.__variety is not None)
4398
4399 # STD:res
4401 """Attempt to resolve the type.
4402
4403 Type resolution for simple types means that the corresponding
4404 schema component fields have been set. Specifically, that
4405 means variety, baseTypeDefinition, and the appropriate
4406 additional fields depending on variety.
4407
4408 All built-in STDs are resolved upon creation. Schema-defined
4409 STDs are held unresolved until the schema has been completely
4410 read, so that references to later schema-defined STDs can be
4411 resolved. Resolution is performed after the entire schema has
4412 been scanned and STD instances created for all
4413 topLevelSimpleTypes.
4414
4415 If a built-in STD is also defined in a schema (which it should
4416 be for XMLSchema), the built-in STD is kept, with the
4417 schema-related information copied over from the matching
4418 schema-defined STD. The former then replaces the latter in
4419 the list of STDs to be resolved.
4420
4421 Types defined by restriction have the same variety as the type
4422 they restrict. If a simple type restriction depends on an
4423 unresolved type, this method simply queues it for resolution
4424 in a later pass and returns.
4425 """
4426 if self.__variety is not None:
4427 return self
4428 #print 'Resolving STD %s' % (self.name(),)
4429 assert self.__domNode
4430 node = self.__domNode
4431
4432 kw = { 'owner' : self
4433 , 'schema' : self._schema() }
4434
4435 bad_instance = False
4436 # The guts of the node should be exactly one instance of
4437 # exactly one of these three types.
4438 candidate = LocateUniqueChild(node, 'list')
4439 if candidate:
4440 self.__initializeFromList(candidate, **kw)
4441
4442 candidate = LocateUniqueChild(node, 'restriction')
4443 if candidate:
4444 if self.__variety is None:
4445 self.__initializeFromRestriction(candidate, **kw)
4446 else:
4447 bad_instance = True
4448
4449 candidate = LocateUniqueChild(node, 'union')
4450 if candidate:
4451 if self.__variety is None:
4452 self.__initializeFromUnion(candidate, **kw)
4453 else:
4454 bad_instance = True
4455
4456 if self.__baseTypeDefinition is None:
4457 raise pyxb.SchemaValidationError('xs:simpleType must have list, union, or restriction as child')
4458
4459 if self._schema() is not None:
4460 self.__final = self._schema().finalForNode(node, self._STD_Map)
4461
4462 # It is NOT an error to fail to resolve the type.
4463 if bad_instance:
4464 raise pyxb.SchemaValidationError('Expected exactly one of list, restriction, union as child of simpleType')
4465
4466 return self
4467
4468 # CFD:STD CFD:SimpleTypeDefinition
4469 @classmethod
4471 # Node should be an XMLSchema simpleType node
4472 assert xsd.nodeIsNamed(node, 'simpleType')
4473
4474 name = NodeAttribute(node, 'name')
4475
4476 rv = cls(name=name, node=node, variety=None, **kw)
4477 rv._annotationFromDOM(node)
4478
4479 # Creation does not attempt to do resolution. Queue up the newly created
4480 # whatsis so we can resolve it after everything's been read in.
4481 rv.__domNode = node
4482 rv._queueForResolution('creation')
4483
4484 return rv
4485
4486 # pythonSupport is None, or a subclass of datatypes.simpleTypeDefinition.
4487 # When set, this simple type definition instance must be uniquely
4488 # associated with the python support type.
4489 __pythonSupport = None
4490
4492 # Includes check that python_support is not None
4493 assert issubclass(python_support, basis.simpleTypeDefinition)
4494 # Can't share support instances
4495 self.__pythonSupport = python_support
4496 self.__pythonSupport._SimpleTypeDefinition(self)
4497 if self.nameInBinding() is None:
4498 self.setNameInBinding(self.__pythonSupport.__name__)
4499 return self.__pythonSupport
4500
4502 return self.__pythonSupport is not None
4503
4505 if self.__pythonSupport is None:
4506 raise pyxb.LogicError('%s: No support defined' % (self.name(),))
4507 return self.__pythonSupport
4508
4511
4514
4518
4520 """Data associated with an
4521 U{import<http://www.w3.org/TR/xmlschema-1/#composition-schemaImport>}
4522 statement within a schema."""
4523
4527 __id = None
4528
4530 """The L{pyxb.namespace.Namespace} instance corresponding to the value
4531 of the C{namespace} attribute from the import statement."""
4532 return self.__namespace
4533 __namespace = None
4534
4536 """The value of the C{schemaLocation} attribute from the import
4537 statement, normalized relative to the location of the importing
4538 schema."""
4539 return self.__schemaLocation
4540 __schemaLocation = None
4541
4543 """The prefix from a namespace declaration for L{namespace} that was
4544 active in the context of the import element, or C{None} if there was
4545 no relevant namespace declaration in scope at that point.
4546
4547 This is propagated to be used as the default prefix for the
4548 corresponding namespace if no prefix had been assigned.
4549 """
4550 return self.__prefix
4551 __prefix = None
4552
4554 """The L{Schema} instance corresponding to the imported schema, if
4555 available.
4556
4557 Normally C{import} statements will be fulfilled by loading components
4558 from a L{namespace archive<pyxb.namespace.NamespaceArchive>} in which
4559 the corresponding namespace is marked as public. Where there are
4560 cycles in the namespace dependency graph, or the schema for a
4561 namespace are associated with a restricted profile of another
4562 namespace, there may be no such archive and instead the components are
4563 obtained using this schema."""
4564 return self.__schema
4565 __schema = None
4566
4568 """Gather the information relative to an C{import} statement.
4569
4570 This examines the L{available
4571 namespaces<pyxb.namespace.archive.AvailableForLoad>} to see whether
4572 the imported namespace can be loaded. If so, the C{schemaLocation}
4573 attribute is ignored. Otherwise, it attempts to retrieve and parse
4574 the corresponding schema (if this has not already been done).
4575
4576 @param importing_schema: The L{Schema} instance in which the import
4577 was found.
4578 @param node: The C{xml.dom.DOM} node incorporating the schema
4579 information.
4580
4581 @raise Exception: Any exception raised when attempting to retrieve and
4582 parse data from the schema location.
4583 """
4584
4585 super(_ImportElementInformationItem, self).__init__(**kw)
4586 uri = NodeAttribute(node, 'namespace')
4587 if uri is None:
4588 raise pyxb.IncompleteImplementationError('import statements without namespace not supported')
4589 schema_location = pyxb.utils.utility.NormalizeLocation(NodeAttribute(node, 'schemaLocation'), importing_schema.location())
4590 self.__schemaLocation = schema_location
4591 ns = self.__namespace = pyxb.namespace.NamespaceForURI(uri, create_if_missing=True)
4592 need_schema = not (ns.isLoadable() or ns.isBuiltinNamespace())
4593 if not need_schema:
4594 # Discard location if we expect to be able to learn about this
4595 # namespace from an archive or a built-in description
4596 self.__schemaLocation = None
4597
4598 ns_ctx = pyxb.namespace.resolution.NamespaceContext.GetNodeContext(node)
4599 if self.schemaLocation() is not None:
4600 # @todo: NOTICE
4601 # print 'import %s + %s = %s' % (importing_schema.location(), self.__schemaLocation, schema_location)
4602 (has_schema, schema_instance) = self.__namespace.lookupSchemaByLocation(schema_location)
4603 if not has_schema:
4604 ckw = { 'absolute_schema_location' : schema_location,
4605 'generation_uid' : importing_schema.generationUID(),
4606 'uri_content_archive_directory' : importing_schema._uriContentArchiveDirectory(),
4607 }
4608 try:
4609 schema_instance = Schema.CreateFromLocation(**ckw)
4610 except Exception, e:
4611 print 'WARNING: Import %s cannot read schema location %s (%s): %s' % (ns, self.__schemaLocation, schema_location, e)
4612 raise
4613 self.__schema = schema_instance
4614 elif need_schema:
4615 print 'WARNING: No information available on imported namespace %s' % (uri,)
4616
4617 # If we think we found a schema, make sure it's in the right
4618 # namespace.
4619 if self.__schema is not None:
4620 if ns != self.__schema.targetNamespace():
4621 raise pyxb.SchemaValidationError('Import expected namespace %s but got %s' % (ns, self.__schema.targetNamespace()))
4622
4623 self.__prefix = ns_ctx.prefixForNamespace(self.namespace())
4624
4625 self._annotationFromDOM(node)
4626
4628 """An XMLSchema U{Schema<http://www.w3.org/TR/xmlschema-1/#Schemas>}."""
4629
4632
4633 # List of annotations
4634 __annotations = None
4635
4636 # True when we have started seeing elements, attributes, or
4637 # notations.
4638 __pastProlog = False
4639
4641 """URI or path to where the schema can be found.
4642
4643 For schema created by a user, the location should be provided to the
4644 constructor using the C{schema_location} keyword. In the case of
4645 imported or included schema, the including schema's location is used
4646 as the base URI for determining the absolute URI of the included
4647 schema from its (possibly relative) location value. For files,
4648 the scheme and authority portions are generally absent, as is often
4649 the abs_path part."""
4650 return self.__location
4651 __location = None
4652
4654 return self.__locationTag
4655 __locationTag = None
4656
4658 return self.__signature
4659 __signature = None
4660
4662 return self.__generationUID
4663 __generationUID = None
4664
4666 return self.__originRecord
4667 __originRecord = None
4668
4670 """The targetNamespace of a componen.
4671
4672 This is None, or a reference to a Namespace in which the
4673 component is declared (either as a global or local to one of
4674 the namespace's complex type definitions). This is immutable
4675 after creation.
4676 """
4677 return self.__targetNamespace
4678 __targetNamespace = None
4679
4681 """Default namespace of the schema.
4682
4683 Will be None unless the schema has an 'xmlns' attribute. The
4684 value must currently be provided as a keyword parameter to the
4685 constructor. """
4686 return self.__defaultNamespace
4687 __defaultNamespace = None
4688
4690 return self.__referencedNamespaces
4691 __referencedNamespaces = None
4692
4694 return self.__importEIIs
4695 __importEIIs = None
4696
4698 return self.__importedSchema
4699 __importedSchema = None
4701 return self.__includedSchema
4702 __includedSchema = None
4703
4704 _QUALIFIED = "qualified"
4705 _UNQUALIFIED = "unqualified"
4706
4707 # Default values for standard recognized schema attributes
4708 __attributeMap = { pyxb.namespace.ExpandedName(None, 'attributeFormDefault') : _UNQUALIFIED
4709 , pyxb.namespace.ExpandedName(None, 'elementFormDefault') : _UNQUALIFIED
4710 , pyxb.namespace.ExpandedName(None, 'blockDefault') : ''
4711 , pyxb.namespace.ExpandedName(None, 'finalDefault') : ''
4712 , pyxb.namespace.ExpandedName(None, 'id') : None
4713 , pyxb.namespace.ExpandedName(None, 'targetNamespace') : None
4714 , pyxb.namespace.ExpandedName(None, 'version') : None
4715 , pyxb.namespace.XML.createExpandedName('lang') : None
4716 }
4717
4719 """Override the schema attribute with the given DOM value."""
4720 self.__attributeMap[pyxb.namespace.ExpandedName(attr.name)] = attr.nodeValue
4721 return self
4722
4724 """Override the schema attributes with values from the given map."""
4725 self.__attributeMap.update(attr_map)
4726 return self
4727
4729 """Return True iff the schema has an attribute with the given (nc)name."""
4730 if isinstance(attr_name, basestring):
4731 attr_name = pyxb.namespace.ExpandedName(None, attr_name)
4732 return self.__attributeMap.has_key(attr_name)
4733
4735 """Return the schema attribute value associated with the given (nc)name.
4736
4737 @param attr_name: local name for the attribute in the schema element.
4738 @return: the value of the corresponding attribute, or C{None} if it
4739 has not been defined and has no default.
4740 @raise KeyError: C{attr_name} is not a valid attribute for a C{schema} element.
4741 """
4742 if isinstance(attr_name, basestring):
4743 attr_name = pyxb.namespace.ExpandedName(None, attr_name)
4744 return self.__attributeMap[attr_name]
4745
4746 __SchemaCategories = ( 'typeDefinition', 'attributeGroupDefinition', 'modelGroupDefinition',
4747 'attributeDeclaration', 'elementDeclaration', 'notationDeclaration',
4748 'identityConstraintDefinition' )
4749
4751 return self.__uriContentArchiveDirectory
4752 __uriContentArchiveDirectory = None
4753
4755 # Force resolution of available namespaces if not already done
4756 if not kw.get('_bypass_preload', False):
4757 pyxb.namespace.archive.NamespaceArchive.PreLoadArchives()
4758
4759 assert 'schema' not in kw
4760 self.__uriContentArchiveDirectory = kw.get('uri_content_archive_directory')
4761 self.__location = kw.get('schema_location')
4762 if self.__location is not None:
4763 schema_path = self.__location
4764 if 0 <= schema_path.find(':'):
4765 schema_path = urlparse.urlparse(schema_path)[2] # .path
4766 self.__locationTag = os.path.split(schema_path)[1].split('.')[0]
4767
4768 self.__generationUID = kw.get('generation_uid')
4769 if self.__generationUID is None:
4770 print 'WARNING: No generationUID provided'
4771 self.__generationUID = pyxb.utils.utility.UniqueIdentifier()
4772
4773 self.__signature = kw.get('schema_signature')
4774 #print 'Schema at %s signature %s uid %s' % (self.location(), self.signature(), self.generationUID())
4775
4776 super(Schema, self).__init__(*args, **kw)
4777 self.__importEIIs = set()
4778 self.__includedSchema = set()
4779 self.__importedSchema = set()
4780 self.__targetNamespace = self._namespaceContext().targetNamespace()
4781 if not isinstance(self.__targetNamespace, pyxb.namespace.Namespace):
4782 raise pyxb.LogicError('Schema constructor requires valid Namespace instance as target_namespace')
4783
4784 # NB: This will raise pyxb.SchemaUniquenessError if it appears this
4785 # schema has already been incorporated into the target namespace.
4786 self.__originRecord = self.__targetNamespace.addSchema(self)
4787
4788 self.__targetNamespace.configureCategories(self.__SchemaCategories)
4789 if self.__defaultNamespace is not None:
4790 self.__defaultNamespace.configureCategories(self.__SchemaCategories)
4791
4792 self.__attributeMap = self.__attributeMap.copy()
4793 self.__annotations = []
4794 # @todo: This isn't right if namespaces are introduced deeper in the document
4795 self.__referencedNamespaces = self._namespaceContext().inScopeNamespaces().values()
4796
4797 __TopLevelComponentMap = {
4798 'element' : ElementDeclaration,
4799 'attribute' : AttributeDeclaration,
4800 'notation' : NotationDeclaration,
4801 'simpleType' : SimpleTypeDefinition,
4802 'complexType' : ComplexTypeDefinition,
4803 'group' : ModelGroupDefinition,
4804 'attributeGroup' : AttributeGroupDefinition
4805 }
4806
4807 @classmethod
4809 if not ('schema_signature' in kw):
4810 kw['schema_signature'] = pyxb.utils.utility.HashForText(xmls)
4811 return cls.CreateFromDOM(StringToDOM(xmls, **kw), **kw)
4812
4813 @classmethod
4815 """Create a schema from a schema location.
4816
4817 Reads an XML document from the schema location and creates a schema
4818 using it. All keyword parameters are passed to L{CreateFromDOM}.
4819
4820 @keyword schema_location: A file path or a URI. If this is a relative
4821 URI and C{parent_uri} is present, the actual location will be
4822 L{normallzed<pyxb.utils.utility.NormalizeLocation>}.
4823 @keyword parent_uri: The context within which schema_location will be
4824 normalized, if necessary.
4825 @keyword absolute_schema_location: A file path or URI. This value is
4826 not normalized, and supersedes C{schema_location}.
4827 """
4828 schema_location = kw.pop('absolute_schema_location', pyxb.utils.utility.NormalizeLocation(kw.get('schema_location'), kw.get('parent_uri'), kw.get('prefix_map')))
4829 kw['location_base'] = kw['schema_location'] = schema_location
4830 assert isinstance(schema_location, basestring), 'Unexpected value %s type %s for schema_location' % (schema_location, type(schema_location))
4831 uri_content_archive_directory = kw.get('uri_content_archive_directory')
4832 return cls.CreateFromDocument(pyxb.utils.utility.TextFromURI(schema_location, archive_directory=uri_content_archive_directory), **kw)
4833
4834 @classmethod
4836 return cls.CreateFromDocument(stream.read(), **kw)
4837
4838 @classmethod
4839 - def CreateFromDOM (cls, node, namespace_context=None, schema_location=None, schema_signature=None, generation_uid=None, **kw):
4840 """Take the root element of the document, and scan its attributes under
4841 the assumption it is an XMLSchema schema element. That means
4842 recognize namespace declarations and process them. Also look for
4843 and set the default namespace. All other attributes are passed up
4844 to the parent class for storage."""
4845
4846 # Get the context of any schema that is including (not importing) this
4847 # one.
4848 including_context = kw.get('including_context')
4849
4850 root_node = node
4851 if Node.DOCUMENT_NODE == node.nodeType:
4852 root_node = root_node.documentElement
4853 if Node.ELEMENT_NODE != root_node.nodeType:
4854 raise pyxb.LogicError('Must be given a DOM node of type ELEMENT')
4855
4856 assert (namespace_context is None) or isinstance(namespace_context, pyxb.namespace.resolution.NamespaceContext)
4857 ns_ctx = pyxb.namespace.resolution.NamespaceContext.GetNodeContext(root_node,
4858 parent_context=namespace_context,
4859 including_context=including_context)
4860
4861 tns = ns_ctx.targetNamespace()
4862 if tns is None:
4863 raise pyxb.SchemaValidationError('No targetNamespace associated with content (not a schema?)')
4864 schema = cls(namespace_context=ns_ctx, schema_location=schema_location, schema_signature=schema_signature, generation_uid=generation_uid, **kw)
4865 schema.__namespaceData = ns_ctx
4866
4867 if schema.targetNamespace() != ns_ctx.targetNamespace():
4868 raise pyxb.SchemaValidationError('targetNamespace %s conflicts with %s' % (schema.targetNamespace(), ns_ctx.targetNamespace()))
4869
4870 # Update the attribute map
4871 for ai in range(root_node.attributes.length):
4872 schema._setAttributeFromDOM(root_node.attributes.item(ai))
4873
4874 # Verify that the root node is an XML schema element
4875 if not xsd.nodeIsNamed(root_node, 'schema'):
4876 raise pyxb.SchemaValidationError('Root node %s of document is not an XML schema element' % (root_node.nodeName,))
4877
4878 for cn in root_node.childNodes:
4879 if Node.ELEMENT_NODE == cn.nodeType:
4880 rv = schema.__processTopLevelNode(cn)
4881 if rv is None:
4882 print 'Unrecognized: %s %s' % (cn.nodeName, cn.toxml("utf-8"))
4883 elif Node.TEXT_NODE == cn.nodeType:
4884 # Non-element content really should just be whitespace.
4885 # If something else is seen, print it for inspection.
4886 text = cn.data.strip()
4887 if text:
4888 print 'Ignored text: %s' % (text,)
4889 elif Node.COMMENT_NODE == cn.nodeType:
4890 #print 'comment: %s' % (cn.data.strip(),)
4891 pass
4892 else:
4893 # ATTRIBUTE_NODE
4894 # CDATA_SECTION_NODE
4895 # ENTITY_NODE
4896 # PROCESSING_INSTRUCTION
4897 # DOCUMENT_NODE
4898 # DOCUMENT_TYPE_NODE
4899 # NOTATION_NODE
4900 print 'Ignoring non-element: %s' % (cn,)
4901
4902 # Do not perform resolution yet: we may be done with this schema, but
4903 # the namespace may incorporate additional ones, and we can't resolve
4904 # until everything's present.
4905 return schema
4906
4907 _SA_All = '#all'
4908
4910 ebv = NodeAttribute(dom_node, attr)
4911 if ebv is None:
4912 ebv = self.schemaAttribute('%sDefault' % (attr,))
4913 rv = 0
4914 if ebv == self._SA_All:
4915 for v in candidate_map.values():
4916 rv += v
4917 else:
4918 for candidate in ebv.split():
4919 rv += candidate_map.get(candidate, 0)
4920 return rv
4921
4923 """Return a bit mask indicating a set of options read from the node's "block" attribute or the schema's "blockDefault" attribute.
4924
4925 A value of '#all' means enable every options; otherwise, the attribute
4926 value should be a list of tokens, for which the corresponding value
4927 will be added to the return value.
4928
4929 @param dom_node: the node from which the "block" attribute will be retrieved
4930 @type dom_node: C{xml.dom.Node}
4931 @param candidate_map: map from strings to bitmask values
4932 """
4933 return self.__ebvForNode('block', dom_node, candidate_map)
4934
4936 """Return a bit mask indicating a set of options read from the node's
4937 "final" attribute or the schema's "finalDefault" attribute.
4938
4939 A value of '#all' means enable every options; otherwise, the attribute
4940 value should be a list of tokens, for which the corresponding value
4941 will be added to the return value.
4942
4943 @param dom_node: the node from which the "final" attribute will be retrieved
4944 @type dom_node: C{xml.dom.Node}
4945 @param candidate_map: map from strings to bitmask values
4946 """
4947 return self.__ebvForNode('final', dom_node, candidate_map)
4948
4950 """Determine the target namespace for a local attribute or element declaration.
4951
4952 Look at the node's C{form} attribute, or if none the schema's
4953 C{attributeFormDefault} or C{elementFormDefault} value. If the
4954 resulting value is C{"qualified"} and the parent schema has a
4955 non-absent target namespace, return it to use as the declaration
4956 target namespace. Otherwise, return None to indicate that the
4957 declaration has no namespace.
4958
4959 @param dom_node: The node defining an element or attribute declaration
4960 @param declaration_type: Either L{AttributeDeclaration} or L{ElementDeclaration}
4961 @return: L{pyxb.namespace.Namespace} or None
4962 """
4963
4964 form_type = NodeAttribute(dom_node, 'form')
4965 if form_type is None:
4966 if declaration_type == ElementDeclaration:
4967 form_type = self.schemaAttribute('elementFormDefault')
4968 elif declaration_type == AttributeDeclaration:
4969 form_type = self.schemaAttribute('attributeFormDefault')
4970 else:
4971 raise pyxb.LogicError('Expected ElementDeclaration or AttributeDeclaration: got %s' % (declaration_type,))
4972 tns = None
4973 if (self._QUALIFIED == form_type):
4974 tns = self.targetNamespace()
4975 if tns.isAbsentNamespace():
4976 tns = None
4977 else:
4978 if (self._UNQUALIFIED != form_type):
4979 raise pyxb.SchemaValidationError('Form type neither %s nor %s' % (self._QUALIFIED, self._UNQUALIFIED))
4980 return tns
4981
4983 """Throw a SchemaValidationException referencing the given
4984 node if we have passed the sequence point representing the end
4985 of prolog elements."""
4986
4987 if self.__pastProlog:
4988 print '%s past prolog' % (object.__str__(self),)
4989 raise pyxb.SchemaValidationError('Unexpected node %s after prolog' % (node_name,))
4990
4992 self.__requireInProlog(node.nodeName)
4993 # See section 4.2.1 of Structures.
4994 abs_uri = pyxb.utils.utility.NormalizeLocation(NodeAttribute(node, 'schemaLocation'), self.__location)
4995 #print 'include %s + %s = %s' % (self.__location, rel_uri, abs_uri)
4996 (has_schema, schema_instance) = self.targetNamespace().lookupSchemaByLocation(abs_uri)
4997 if not has_schema:
4998 kw = { 'absolute_schema_location': abs_uri,
4999 'including_context': self.__namespaceData,
5000 'generation_uid': self.generationUID(),
5001 'uri_content_archive_directory': self._uriContentArchiveDirectory(),
5002 }
5003 try:
5004 schema_instance = self.CreateFromLocation(**kw)
5005 except Exception, e:
5006 print 'INCLUDE %s caught: %s' % (abs_uri, e)
5007 #traceback.print_exception(*sys.exc_info())
5008 raise
5009 # @todo: NOTICE
5010 #print '%s completed including %s from %s' % (self.__location, included_schema.targetNamespace(), abs_uri)
5011 # @todo: NOTICE
5012 #print 'Included %s, back to %s' % (included_schema.location(), self.location())
5013 if schema_instance:
5014 if self.targetNamespace() != schema_instance.targetNamespace():
5015 raise pyxb.SchemaValidationError('Included namespace %s not consistent with including namespace %s' % (schema_instance.targetNamespace(), self.targetNamespace()))
5016 self.__includedSchema.add(schema_instance)
5017 return node
5018
5020 """Process an import directive.
5021
5022 This attempts to locate schema (named entity) information for
5023 a namespace that is referenced by this schema.
5024 """
5025
5026 self.__requireInProlog(node.nodeName)
5027 import_eii = _ImportElementInformationItem(self, node)
5028 # @todo: NOTICE
5029 #print 'Imported %s, prefix %s, back to %s' % (import_eii.namespace().uri(), import_eii.prefix(), self.__location)
5030 if import_eii.schema() is not None:
5031 self.__importedSchema.add(import_eii.schema())
5032 self.targetNamespace().importNamespace(import_eii.namespace())
5033 ins = import_eii.namespace()
5034 if ins.prefix() is None:
5035 ins.setPrefix(import_eii.prefix())
5036 self.__importEIIs.add(import_eii)
5037 return node
5038
5040 self.__requireInProlog(node.nodeName)
5041 raise pyxb.IncompleteImplementationError('redefine not implemented')
5042
5046
5048 """Process a DOM node from the top level of the schema.
5049
5050 This should return a non-None value if the node was
5051 successfully recognized."""
5052 if xsd.nodeIsNamed(node, 'include'):
5053 return self.__processInclude(node)
5054 if xsd.nodeIsNamed(node, 'import'):
5055 return self.__processImport(node)
5056 if xsd.nodeIsNamed(node, 'redefine'):
5057 return self.__processRedefine(node)
5058 if xsd.nodeIsNamed(node, 'annotation'):
5059 return self.__processAnnotation(node)
5060
5061 component = self.__TopLevelComponentMap.get(node.localName)
5062 if component is not None:
5063 self.__pastProlog = True
5064 kw = { 'scope' : _ScopedDeclaration_mixin.SCOPE_global,
5065 'schema' : self,
5066 'owner' : self }
5067 return self._addNamedComponent(component.CreateFromDOM(node, **kw))
5068
5069 raise pyxb.SchemaValidationError('Unexpected top-level element %s' % (node.nodeName,))
5070
5074
5076 tns = self.targetNamespace()
5077 assert tns is not None
5078 if not isinstance(nc, _NamedComponent_mixin):
5079 raise pyxb.LogicError('Attempt to add unnamed %s instance to dictionary' % (nc.__class__,))
5080 if nc.isAnonymous():
5081 raise pyxb.LogicError('Attempt to add anonymous component to dictionary: %s', (nc.__class__,))
5082 if isinstance(nc, _ScopedDeclaration_mixin):
5083 assert _ScopedDeclaration_mixin.SCOPE_global == nc.scope()
5084 #print 'Adding %s as %s' % (nc.__class__.__name__, nc.name())
5085 if isinstance(nc, (SimpleTypeDefinition, ComplexTypeDefinition)):
5086 return self.__addTypeDefinition(nc)
5087 if isinstance(nc, AttributeDeclaration):
5088 return self.__addAttributeDeclaration(nc)
5089 if isinstance(nc, AttributeGroupDefinition):
5090 return self.__addAttributeGroupDefinition(nc)
5091 if isinstance(nc, ModelGroupDefinition):
5092 return tns.addCategoryObject('modelGroupDefinition', nc.name(), nc)
5093 if isinstance(nc, ElementDeclaration):
5094 return tns.addCategoryObject('elementDeclaration', nc.name(), nc)
5095 if isinstance(nc, NotationDeclaration):
5096 return tns.addCategoryObject('notationDeclaration', nc.name(), nc)
5097 if isinstance(nc, IdentityConstraintDefinition):
5098 return tns.addCategoryObject('identityConstraintDefinition', nc.name(), nc)
5099 raise pyxb.IncompleteImplementationError('No support to record named component of type %s' % (nc.__class__,))
5100
5102 local_name = td.name()
5103 assert self.__targetNamespace
5104 tns = self.targetNamespace()
5105 old_td = tns.typeDefinitions().get(local_name)
5106 if (old_td is not None) and (old_td != td):
5107 if isinstance(td, ComplexTypeDefinition) != isinstance(old_td, ComplexTypeDefinition):
5108 raise pyxb.SchemaValidationError('Name %s used for both simple and complex types' % (td.name(),))
5109
5110 if not old_td._allowUpdateFromOther(td):
5111 raise pyxb.SchemaValidationError('Attempt to re-define non-builtin type definition %s' % (tns.createExpandedName(local_name),))
5112
5113 # Copy schema-related information from the new definition
5114 # into the old one, and continue to use the old one.
5115 td = tns._replaceComponent(td, old_td._updateFromOther(td))
5116 else:
5117 tns.addCategoryObject('typeDefinition', td.name(), td)
5118 assert td is not None
5119 return td
5120
5122 local_name = ad.name()
5123 assert self.__targetNamespace
5124 tns = self.targetNamespace()
5125 old_ad = tns.attributeDeclarations().get(local_name)
5126 if (old_ad is not None) and (old_ad != ad):
5127 if not old_ad._allowUpdateFromOther(ad):
5128 raise pyxb.SchemaValidationError('Attempt to re-define non-builtin attribute declaration %s' % (tns.createExpandedName(local_name),))
5129
5130 # Copy schema-related information from the new definition
5131 # into the old one, and continue to use the old one.
5132 ad = tns._replaceComponent(ad, old_ad._updateFromOther(ad))
5133 else:
5134 tns.addCategoryObject('attributeDeclaration', ad.name(), ad)
5135 assert ad is not None
5136 return ad
5137
5139 local_name = agd.name()
5140 assert self.__targetNamespace
5141 tns = self.targetNamespace()
5142 old_agd = tns.attributeGroupDefinitions().get(local_name)
5143 if (old_agd is not None) and (old_agd != agd):
5144 if not old_agd._allowUpdateFromOther(agd):
5145 raise pyxb.SchemaValidationError('Attempt to re-define non-builtin attribute group definition %s' % (tns.createExpandedName(local_name),))
5146
5147 # Copy schema-related information from the new definition
5148 # into the old one, and continue to use the old one.
5149 ad = tns._replaceComponent(agd, old_agd._updateFromOther(agd))
5150 else:
5151 tns.addCategoryObject('attributeGroupDefinition', agd.name(), agd)
5152 assert agd is not None
5153 return agd
5154
5156 return 'SCH[%s]' % (self.location(),)
5157
5160 """Add to the schema the definitions of the built-in types of XMLSchema.
5161 This should only be invoked by L{pyxb.namespace} when the built-in
5162 namespaces are initialized. """
5163 # Add the ur type
5164 #schema = namespace.schema()
5165 schema = Schema(namespace_context=pyxb.namespace.XMLSchema.initialNamespaceContext(), schema_location='URN:noLocation:PyXB:XMLSchema', generation_uid=pyxb.namespace.BuiltInObjectUID, _bypass_preload=True)
5166 td = schema._addNamedComponent(ComplexTypeDefinition.UrTypeDefinition(schema, in_builtin_definition=True))
5167 assert td.isResolved()
5168 # Add the simple ur type
5169 td = schema._addNamedComponent(SimpleTypeDefinition.SimpleUrTypeDefinition(schema, in_builtin_definition=True))
5170 assert td.isResolved()
5171 # Add definitions for all primitive and derived simple types
5172 pts_std_map = {}
5173 ns_ctx = namespace.initialNamespaceContext()
5174 for dtc in datatypes._PrimitiveDatatypes:
5175 name = dtc.__name__.rstrip('_')
5176 td = schema._addNamedComponent(SimpleTypeDefinition.CreatePrimitiveInstance(name, schema, dtc))
5177 assert td.isResolved()
5178 assert dtc.SimpleTypeDefinition() == td
5179 pts_std_map.setdefault(dtc, td)
5180 for dtc in datatypes._DerivedDatatypes:
5181 name = dtc.__name__.rstrip('_')
5182 parent_std = pts_std_map[dtc.XsdSuperType()]
5183 td = schema._addNamedComponent(SimpleTypeDefinition.CreateDerivedInstance(name, schema, parent_std, dtc))
5184 assert td.isResolved()
5185 assert dtc.SimpleTypeDefinition() == td
5186 pts_std_map.setdefault(dtc, td)
5187 for dtc in datatypes._ListDatatypes:
5188 list_name = dtc.__name__.rstrip('_')
5189 element_name = dtc._ItemType.__name__.rstrip('_')
5190 element_std = schema.targetNamespace().typeDefinitions().get(element_name)
5191 assert element_std is not None
5192 td = schema._addNamedComponent(SimpleTypeDefinition.CreateListInstance(list_name, schema, element_std, dtc))
5193 assert td.isResolved()
5194 global _PastAddBuiltInTypes
5195 _PastAddBuiltInTypes = True
5196
5197 return schema
5198
5199 import sys
5200 import pyxb.namespace.builtin
5201 pyxb.namespace.builtin._InitializeBuiltinNamespaces(sys.modules[__name__])
5202
5203 ## Local Variables:
5204 ## fill-column:78
5205 ## End:
5206
| Home | Trees | Indices | Help |
|
|---|
| Generated by Epydoc 3.0.1 on Fri Jun 15 14:42:48 2012 | http://epydoc.sourceforge.net |