1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 """This module contains support classes from which schema-specific bindings
16 inherit, and that describe the content models of those schema."""
17
18 import pyxb
19 import xml.dom
20 import pyxb.utils.domutils as domutils
21 import pyxb.utils.utility as utility
22 import types
23 import pyxb.namespace
24 from pyxb.namespace.builtin import XMLSchema_instance as XSI
25
26 BINDING_STYLE_ACCESSOR = 'accessor'
27 BINDING_STYLE_PROPERTY = 'property'
28
29 BINDING_STYLES = (BINDING_STYLE_ACCESSOR, BINDING_STYLE_PROPERTY)
30 DEFAULT_BINDING_STYLE = BINDING_STYLE_PROPERTY
31 CURRENT_BINDING_STYLE = None
38
40
41 @classmethod
52
53 _ExpandedName = None
54 """The expanded name of the component."""
55
56 _ReservedSymbols = set([ 'validateBinding', 'toDOM', 'toxml', 'Factory', 'property' ])
57
58 if pyxb._CorruptionDetectionEnabled:
63
64
65
66 _PyXBFactoryKeywords = ( '_dom_node', '_fallback_namespace', '_apply_whitespace_facet', '_validate_constraints', '_require_value', '_nil', '_element', '_convert_string_values' )
67 """Keywords that are interpreted by __new__ or __init__ in one or more
68 classes in the PyXB type hierarchy. All these keywords must be removed
69 before invoking base Python __init__ or __new__."""
70
71
72
73
74 _Abstract = False
75
77 """Return a L{namespace context <pyxb.binding.NamespaceContext>}
78 associated with the binding instance.
79
80 This will return C{None} unless something has provided a context to
81 the instance. Context is provided when instances are generated by the
82 DOM and SAX-based translators."""
83 return self.__namespaceContext
84 - def _setNamespaceContext (self, namespace_context):
85 """Associate a L{namespace context <pyxb.binding.NamespaceContext>}
86 with the binding instance."""
87 self.__namespaceContext = namespace_context
88 return self
89 __namespaceContext = None
90
92 """Associate a L{pyxb.binding.basis.element} with the instance."""
93 self.__element = element
94 return self
96 """Return a L{pyxb.binding.basis.element} associated with the binding
97 instance.
98
99 This will return C{None} unless an element has been associated.
100 Constructing a binding instance using the element instance will add
101 this association.
102 """
103 return self.__element
104 __element = None
105
106 __xsiNil = None
108 """Indicate whether this instance is U{nil
109 <http://www.w3.org/TR/xmlschema-1/#xsi_nil>}.
110
111 The value is set by the DOM and SAX parsers when building an instance
112 from a DOM element with U{xsi:nil
113 <http://www.w3.org/TR/xmlschema-1/#xsi_nil>} set to C{true}.
114
115 @return: C{None} if the element used to create the instance is not
116 U{nillable<http://www.w3.org/TR/xmlschema-1/#nillable>}.
117 If it is nillable, returns C{True} or C{False} depending on
118 whether the instance itself is U{nil<http://www.w3.org/TR/xmlschema-1/#xsi_nil>}.
119 """
120 return self.__xsiNil
122 """Set the xsi:nil property of the instance.
123
124 @raise pyxb.NoNillableSupportError: the instance is not associated
125 with an element that is L{nillable
126 <pyxb.binding.basis.element.nillable>}.
127 """
128 if self.__xsiNil is None:
129 raise pyxb.NoNillableSupportError(type(self))
130 self.__xsiNil = True
131 self._resetContent()
132
133 - def _resetContent (self):
135
136 __constructedWithValue = False
150
151
152
153
154 __WarnedUnassociatedElement = False
155
166
167 @classmethod
169 """Method invoked upon entry to the Factory method.
170
171 This method is entitled to modify the keywords array. It can also
172 return a state value which is passed to _postFactory_vx."""
173 return None
174
175 - def _postFactory_vx (cls, state):
176 """Method invoked prior to leaving the Factory method.
177
178 This is an instance method, and is given the state that was returned
179 by _PreFactory_vx."""
180 return None
181
182 @classmethod
184 """Provide a common mechanism to create new instances of this type.
185
186 The class constructor won't do, because you can't create
187 instances of union types.
188
189 This method may be overridden in subclasses (like STD_union). Pre-
190 and post-creation actions can be customized on a per-class instance by
191 overriding the L{_PreFactory_vx} and L{_postFactory_vx} methods.
192
193 @keyword _dom_node: If provided, the value must be a DOM node, the
194 content of which will be used to set the value of the instance.
195
196 @keyword _apply_whitespace_facet: If C{True} and this is a
197 simpleTypeDefinition with a whiteSpace facet, the first argument will
198 be normalized in accordance with that facet prior to invoking the
199 parent constructor.
200
201 @keyword _validate_constraints: If C{True}, any constructed value is
202 checked against constraints applied to the union as well as the member
203 type.
204
205 @keyword _require_value: If C{False} (default), it is permitted to
206 create a value without an initial value. If C{True} and no initial
207 value was provided, causes L{pyxb.MissingContentError} to be raised.
208 Only applies to simpleTypeDefinition instances; this is used when
209 creating values from DOM nodes.
210 """
211
212
213 dom_node = kw.get('_dom_node')
214 used_cls = cls._SupersedingClass()
215 state = used_cls._PreFactory_vx(args, kw)
216 rv = cls._DynamicCreate(*args, **kw)
217 rv._postFactory_vx(state)
218 if isinstance(dom_node, utility.Locatable_mixin):
219 rv._setLocation(dom_node.location)
220 return rv
221
226
227 @classmethod
230
231 @classmethod
233 """Return a variant of the value that is compatible with this type.
234
235 Compatibility is defined relative to the type definition associated
236 with the element. The value C{None} is always compatible. If
237 C{value} has a Python type (e.g., C{int}) that is a superclass of the
238 required L{_TypeBinding_mixin} class (e.g., C{xs:byte}), C{value} is
239 used as a constructor parameter to return a new instance of the
240 required type. Note that constraining facets are applied here if
241 necessary (e.g., although a Python C{int} with value C{500} is
242 type-compatible with C{xs:byte}, it is outside the value space, and
243 compatibility will fail.
244
245 @keyword _convert_string_values: If C{True} (default) and the incoming value is
246 a string, an attempt will be made to form a compatible value by using
247 the string as a constructor argument to the this class. This flag is
248 set to C{False} when testing automaton transitions.
249
250 @raise pyxb.BadTypeValueError: if the value is not both
251 type-consistent and value-consistent with the element's type.
252 """
253 convert_string_values = kw.get('_convert_string_values', True)
254
255 if value is None:
256 return None
257
258 if isinstance(value, cls):
259
260
261 return value
262 value_type = type(value)
263
264 if str == value_type:
265 value_type = unicode
266
267
268
269 if issubclass(cls, value_type):
270 return cls(value)
271
272
273
274 if isinstance(value, int) and issubclass(cls, long):
275 return cls(value)
276
277
278 if isinstance(value, bool) and issubclass(cls, pyxb.binding.datatypes.boolean):
279 return cls(value)
280
281
282
283 if convert_string_values and (unicode == value_type):
284 return cls(value)
285
286
287 if issubclass(cls, STD_union):
288 for mt in cls._MemberTypes:
289 try:
290 return mt._CompatibleValue(value, **kw)
291 except:
292 pass
293
294
295 if (pyxb.binding.datatypes.anySimpleType == cls) and issubclass(value_type, simpleTypeDefinition):
296 return value
297 if (pyxb.binding.datatypes.anyType == cls) and issubclass(value_type, complexTypeDefinition):
298 return value
299
300
301
302 if isinstance(value, pyxb.BIND):
303 return value.createInstance(cls.Factory, **kw)
304
305
306
307
308
309
310 raise pyxb.BadTypeValueError('No conversion from %s to %s' % (value_type, cls))
311
312 @classmethod
314 """Return True iff the content of this binding object is a simple type.
315
316 This is true only for descendents of simpleTypeDefinition and instances
317 of complexTypeDefinition that have simple type content."""
318 raise pyxb.LogicError('Failed to override _TypeBinding_mixin._IsSimpleTypeContent')
319
320 - def toDOM (self, bds=None, parent=None, element_name=None):
321 """Convert this instance to a DOM node.
322
323 The name of the top-level element is either the name of the L{element}
324 instance associated with this instance, or the XML name of the type of
325 this instance.
326
327 @param bds: Support for customizing the generated document
328 @type bds: L{pyxb.utils.domutils.BindingDOMSupport}
329 @param parent: If C{None}, a standalone document is created;
330 otherwise, the created element is a child of the given element.
331 @type parent: C{xml.dom.Element} or C{None}
332 @rtype: C{xml.dom.Document}
333 """
334
335 if bds is None:
336 bds = domutils.BindingDOMSupport()
337 need_xsi_type = bds.requireXSIType()
338 if isinstance(element_name, (str, unicode)):
339 element_name = pyxb.namespace.ExpandedName(bds.defaultNamespace(), element_name)
340 if (element_name is None) and (self._element() is not None):
341 element_binding = self._element()
342 element_name = element_binding.name()
343 need_xsi_type = need_xsi_type or element_binding.typeDefinition()._RequireXSIType(type(self))
344 if element_name is None:
345 element_name = self._ExpandedName
346 element = bds.createChildElement(element_name, parent)
347 if need_xsi_type:
348 val_type_qname = self._ExpandedName.localName()
349 tns_prefix = bds.namespacePrefix(self._ExpandedName.namespace())
350 if tns_prefix is not None:
351 val_type_qname = '%s:%s' % (tns_prefix, val_type_qname)
352 bds.addAttribute(element, XSI.type, val_type_qname)
353 self._toDOM_csc(bds, element)
354 bds.finalize()
355 return bds.document()
356
357 - def toxml (self, bds=None, root_only=False):
358 """Shorthand to get the object as an XML document.
359
360 If you want to set the default namespace, pass in a pre-configured
361 C{bds}.
362
363 @param bds: Optional L{pyxb.utils.domutils.BindingDOMSupport} instance
364 to use for creation. If not provided (default), a new generic one is
365 created.
366 """
367 dom = self.toDOM(bds)
368 if root_only:
369 dom = dom.documentElement
370 return dom.toxml()
371
377
379 """Override in subclasses for type-specific validation of instance
380 content.
381
382 @return: C{True} if the instance validates
383 @raise pyxb.BindingValidationError: complex content does not match model
384 @raise pyxb.BadTypeValueError: simple content fails to satisfy constraints
385 """
386 raise pyxb.IncompleteImplementationError('%s did not override _validateBinding_vx' % (type(self),))
387
389 """Check whether the binding content matches its content model.
390
391 @return: C{True} if validation succeeds.
392 @raise pyxb.BindingValidationError: complex content does not match model
393 @raise pyxb.BadTypeValueError: simple content fails to satisfy constraints
394 """
395 if self._PerformValidation():
396 self._validateBinding_vx()
397 return True
398
399 @classmethod
406
408 """Helper to allow overriding the implementation class.
409
410 Generally we'll want to augment the generated bindings by subclassing
411 them, and adding functionality to the subclass. This mix-in provides a
412 way to communicate the existence of the superseding subclass back to the
413 binding infrastructure, so that when it creates an instance it uses the
414 subclass rather than the unaugmented binding class.
415
416 When a raw generated binding is subclassed, L{_SetSupersedingClass} should be
417 invoked on the raw class passing in the superseding subclass. E.g.::
418
419 class mywsdl (raw.wsdl):
420 pass
421 raw.wsdl._SetSupersedingClass(mywsdl)
422
423 """
424
425 @classmethod
427 return '_%s__SupersedingClass' % (cls.__name__,)
428
429 @classmethod
431 return '_%s__AlternativeConstructor' % (cls.__name__,)
432
433 @classmethod
437
438 @classmethod
440 """Return the class stored in the class reference attribute."""
441 rv = getattr(cls, cls.__AlternativeConstructorAttribute(), None)
442 if isinstance(rv, tuple):
443 rv = rv[0]
444 return rv
445
446 @classmethod
448 """Set the class reference attribute.
449
450 @param superseding: A Python class that is a subclass of this class.
451 """
452 assert (superseding is None) or issubclass(superseding, cls)
453 if superseding is None:
454 cls.__dict__.pop(cls.__SupersedingClassAttribute(), None)
455 else:
456 setattr(cls, cls.__SupersedingClassAttribute(), superseding)
457 return superseding
458
459 @classmethod
461 attr = cls.__AlternativeConstructorAttribute()
462 if alternative_constructor is None:
463 cls.__dict__.pop(attr, None)
464 else:
465
466
467
468 setattr(cls, attr, (alternative_constructor,))
469 assert cls._AlternativeConstructor() == alternative_constructor
470 return alternative_constructor
471
472 @classmethod
482
483 -class simpleTypeDefinition (_TypeBinding_mixin, utility._DeconflictSymbols_mixin, _DynamicCreate_mixin):
484 """L{simpleTypeDefinition} is a base class that is part of the
485 hierarchy of any class that represents the Python datatype for a
486 L{SimpleTypeDefinition<pyxb.xmlschema.structures.SimpleTypeDefinition>}.
487
488 @note: This class, or a descendent of it, must be the first class
489 in the method resolution order when a subclass has multiple
490 parents. Otherwise, constructor keyword arguments may not be
491 removed before passing them on to Python classes that do not
492 accept them.
493 """
494
495
496
497
498 __FacetMap = {}
499
500 _ReservedSymbols = _TypeBinding_mixin._ReservedSymbols.union(set([ 'XsdLiteral', 'xsdLiteral',
501 'XsdSuperType', 'XsdPythonType', 'XsdConstraintsOK',
502 'xsdConstraintsOK', 'XsdValueLength', 'xsdValueLength',
503 'PythonLiteral', 'pythonLiteral',
504 'SimpleTypeDefinition' ]))
505 """Symbols that remain the responsibility of this class. Any
506 public symbols in generated binding subclasses are deconflicted
507 by providing an alternative name in the subclass. (There
508 currently are no public symbols in generated SimpleTypeDefinion
509 bindings."""
510
511
512 @classmethod
520
521
522
523
524 __FacetMapAttributeNameMap = { }
525 @classmethod
527 """ """
528 '''
529 if cls == simpleTypeDefinition:
530 return '_%s__FacetMap' % (cls.__name__.strip('_'),)
531
532 # It is not uncommon for a class in one namespace to extend a class of
533 # the same name in a different namespace, so encode the namespace URI
534 # in the attribute name (if it is part of a namespace).
535 ns_uri = ''
536 try:
537 ns_uri = cls._ExpandedName.namespaceURI()
538 except Exception, e:
539 pass
540 nm = '_' + utility.MakeIdentifier('%s_%s_FacetMap' % (ns_uri, cls.__name__.strip('_')))
541 '''
542 nm = cls.__FacetMapAttributeNameMap.get(cls)
543 if nm is None:
544 if cls == simpleTypeDefinition:
545 nm = '_%s__FacetMap' % (cls.__name__.strip('_'),)
546 else:
547
548
549
550 ns_uri = ''
551 try:
552 ns_uri = cls._ExpandedName.namespaceURI()
553 except Exception, e:
554 pass
555 nm = '_' + utility.MakeIdentifier('%s_%s_FacetMap' % (ns_uri, cls.__name__.strip('_')))
556 cls.__FacetMapAttributeNameMap[cls] = nm
557 return nm
558
559 @classmethod
561 """Return a reference to the facet map for this datatype.
562
563 The facet map is a map from leaf facet classes to instances of those
564 classes that constrain or otherwise apply to the lexical or value
565 space of the datatype. Classes may inherit their facet map from their
566 superclass, or may create a new class instance if the class adds a new
567 constraint type.
568
569 :raise AttributeError: if the facet map has not been defined"""
570 return getattr(cls, cls.__FacetMapAttributeName())
571
572 @classmethod
574 """Initialize the facet map for this datatype.
575
576 This must be called exactly once, after all facets belonging to the
577 datatype have been created.
578
579 :raise pyxb.LogicError: if called multiple times (on the same class)
580 :raise pyxb.LogicError: if called when a parent class facet map has not been initialized
581 :return: the facet map"""
582 fm = None
583 try:
584 fm = cls._FacetMap()
585 except AttributeError:
586 pass
587 if fm is not None:
588 raise pyxb.LogicError('%s facet map initialized multiple times: %s' % (cls.__name__,cls.__FacetMapAttributeName()))
589
590
591
592
593
594 source_class = cls
595 while fm is None:
596
597
598 for super_class in source_class.mro():
599
600 assert super_class is not None
601 if (super_class == simpleTypeDefinition):
602 break
603 if issubclass(super_class, simpleTypeDefinition):
604 try:
605 fm = super_class._FacetMap()
606
607 break
608 except AttributeError:
609 pass
610 if fm is None:
611 try:
612 source_class = source_class.XsdSuperType()
613 except AttributeError:
614 source_class = None
615
616 if source_class is None:
617 fm = { }
618
619 if fm is None:
620 raise pyxb.LogicError('%s is not a child of simpleTypeDefinition' % (cls.__name__,))
621 fm = fm.copy()
622
623 for facet in args:
624 fm[type(facet)] = facet
625
626
627 setattr(cls, cls.__FacetMapAttributeName(), fm)
628 return fm
629
630 @classmethod
633
634 @classmethod
636 """Pre-process the arguments.
637
638 This is used before invoking the parent constructor. One application
639 is to apply the whitespace facet processing; if such a request is in
640 the keywords, it is removed so it does not propagate to the
641 superclass. Another application is to convert the arguments from a
642 string to a list. Binding-specific applications are performed in the
643 overloaded L{_ConvertArguments_vx} method."""
644 dom_node = kw.pop('_dom_node', None)
645 if dom_node is not None:
646 text_content = domutils.ExtractTextContent(dom_node)
647 if text_content is not None:
648 args = (domutils.ExtractTextContent(dom_node),) + args
649 kw['_apply_whitespace_facet'] = True
650 apply_whitespace_facet = kw.pop('_apply_whitespace_facet', True)
651 if (0 < len(args)) and isinstance(args[0], types.StringTypes):
652 cf_whitespace = getattr(cls, '_CF_whiteSpace', None)
653 if cf_whitespace is not None:
654
655 norm_str = unicode(cf_whitespace.normalizeString(args[0]))
656 args = (norm_str,) + args[1:]
657 return cls._ConvertArguments_vx(args, kw)
658
659
660
661
662
663
664
665
666
667
668
670
671 kw.pop('_validate_constraints', None)
672 kw.pop('_require_value', None)
673 kw.pop('_element', None)
674 kw.pop('_fallback_namespace', None)
675 kw.pop('_nil', None)
676
677 args = cls._ConvertArguments(args, kw)
678 assert issubclass(cls, _TypeBinding_mixin)
679 try:
680 rv = super(simpleTypeDefinition, cls).__new__(cls, *args, **kw)
681 return rv
682 except ValueError, e:
683 raise pyxb.BadTypeValueError(e)
684 except OverflowError, e:
685 raise pyxb.BadTypeValueError(e)
686
687
688
690 """Initialize a newly created STD instance.
691
692 Usually there is one positional argument, which is a value that can be
693 converted to the underlying Python type.
694
695 @keyword _validate_constraints: If True (default), the newly
696 constructed value is checked against its constraining facets.
697 @type _validate_constraints: C{bool}
698 """
699
700 validate_constraints = kw.pop('_validate_constraints', self._PerformValidation())
701 require_value = kw.pop('_require_value', False)
702
703
704 args = self._ConvertArguments(args, kw)
705 try:
706 super(simpleTypeDefinition, self).__init__(*args, **kw)
707 except OverflowError, e:
708 raise pyxb.BadTypeValueError(e)
709 if require_value and (not self._constructedWithValue()):
710 raise pyxb.MissingContentError('missing value')
711
712 if validate_constraints:
713 self.xsdConstraintsOK()
714
715
716
717
718
719
720
721 @classmethod
723 return '_%s__SimpleTypeDefinition' % (cls.__name__,)
724
725 @classmethod
727 """Set the L{pyxb.xmlschema.structures.SimpleTypeDefinition} instance
728 associated with this binding."""
729 attr_name = cls.__STDAttrName()
730 if hasattr(cls, attr_name):
731 old_value = getattr(cls, attr_name)
732 if old_value != std:
733 raise pyxb.LogicError('%s: Attempt to override existing STD %s with %s' % (cls, old_value.name(), std.name()))
734 setattr(cls, attr_name, std)
735
736 @classmethod
738 """Return the SimpleTypeDefinition instance for the given
739 class.
740
741 This should only be invoked when generating bindings.
742
743 @raise pyxb.IncompleteImplementationError: no STD instance has been
744 associated with the class.
745
746 """
747 attr_name = cls.__STDAttrName()
748 if hasattr(cls, attr_name):
749 return getattr(cls, attr_name)
750 raise pyxb.IncompleteImplementationError('%s: No STD available' % (cls,))
751
752 @classmethod
754 """Convert from a python value to a string usable in an XML
755 document.
756
757 This should be implemented in the subclass."""
758 raise pyxb.LogicError('%s does not implement XsdLiteral' % (cls,))
759
761 """Return text suitable for representing the value of this
762 instance in an XML document.
763
764 The base class implementation delegates to the object class's
765 XsdLiteral method."""
766 if self._isNil():
767 return ''
768 return self.XsdLiteral(self)
769
770 @classmethod
772 """Find the nearest parent class in the PST hierarchy.
773
774 The value for anySimpleType is None; for all others, it's a
775 primitive or derived PST descendent (including anySimpleType)."""
776 for sc in cls.mro():
777 if sc == cls:
778 continue
779 if simpleTypeDefinition == sc:
780
781
782
783 return cls._XsdBaseType
784 if issubclass(sc, simpleTypeDefinition):
785 return sc
786 raise pyxb.LogicError('No supertype found for %s' % (cls,))
787
788 @classmethod
790 """Pre-extended class method to verify other things before
791 checking constraints.
792
793 This is used for list types, to verify that the values in the
794 list are acceptable, and for token descendents, to check the
795 lexical/value space conformance of the input.
796 """
797 super_fn = getattr(super(simpleTypeDefinition, cls), '_XsdConstraintsPreCheck_vb', lambda *a,**kw: value)
798 return super_fn(value)
799
800
801
802 __ClassFacetSequence = { }
803
804 @classmethod
806 """Validate the given value against the constraints on this class.
807
808 @raise pyxb.BadTypeValueError: if any constraint is violated.
809 """
810
811 value = cls._XsdConstraintsPreCheck_vb(value)
812
813 facet_values = cls.__ClassFacetSequence.get(cls)
814 if facet_values is None:
815
816
817 classes = [ _x for _x in cls.mro() if issubclass(_x, simpleTypeDefinition) ]
818 classes.reverse()
819 cache_result = True
820 facet_values = []
821 for clazz in classes:
822
823
824
825
826
827
828 try:
829 clazz_facets = clazz._FacetMap().values()
830 except AttributeError, e:
831 cache_result = False
832 clazz_facets = []
833 for v in clazz_facets:
834 if not (v in facet_values):
835 facet_values.append(v)
836 if cache_result:
837 cls.__ClassFacetSequence[cls] = facet_values
838 for f in facet_values:
839 if not f.validateConstraint(value):
840 raise pyxb.BadTypeValueError('%s violation for %s in %s' % (f.Name(), value, cls.__name__))
841
842 return value
843
845 """Validate the value of this instance against its constraints."""
846 return self.XsdConstraintsOK(self)
847
852
853 @classmethod
855 """Return the length of the given value.
856
857 The length is calculated by a subclass implementation of
858 _XsdValueLength_vx in accordance with
859 http://www.w3.org/TR/xmlschema-2/#rf-length.
860
861 The return value is a non-negative integer, or C{None} if length
862 constraints should be considered trivially satisfied (as with
863 QName and NOTATION).
864
865 :raise pyxb.LogicError: the provided value is not an instance of cls.
866 :raise pyxb.LogicError: an attempt is made to calculate a length for
867 an instance of a type that does not support length calculations.
868 """
869 assert isinstance(value, cls)
870 if not hasattr(cls, '_XsdValueLength_vx'):
871 raise pyxb.LogicError('Class %s does not support length validation' % (cls.__name__,))
872 return cls._XsdValueLength_vx(value)
873
875 """Return the length of this instance within its value space.
876
877 See XsdValueLength."""
878 return self.XsdValueLength(self)
879
880 @classmethod
882 """Return a string which can be embedded into Python source to
883 represent the given value as an instance of this class."""
884 class_name = cls.__name__
885 return '%s(%s)' % (class_name, repr(value))
886
888 """Return a string which can be embedded into Python source to
889 represent the value of this instance."""
890 return self.PythonLiteral(self)
891
896
897 @classmethod
899 """STDs have simple type content."""
900 return True
901
902 @classmethod
910
911 @classmethod
913
914 """NB: Invoking this on a value that is a list will, if necessary,
915 replace the members of the list with new values that are of the
916 correct item type. This is permitted because only with lists is it
917 possible to bypass the normal content validation (by invoking
918 append/extend on the list instance)."""
919 if value is None:
920 raise pyxb.BadTypeValueError('None is not a valid instance of %s' % (cls,))
921
922 value_class = cls
923 if issubclass(cls, STD_list):
924
925 try:
926 iter(value)
927 except TypeError, e:
928 raise pyxb.BadTypeValueError('%s cannot have non-iterable value type %s' % (cls, type(value)))
929 for v in value:
930 if not cls._ItemType._IsValidValue(v):
931 raise pyxb.BadTypeValueError('%s cannot have member of type %s (want %s)' % (cls, type(v), cls._ItemType))
932 else:
933 if issubclass(cls, STD_union):
934
935 value_class = None
936 for mt in cls._MemberTypes:
937 if mt._IsValidValue(value):
938 value_class = mt
939 break
940 if value_class is None:
941 raise pyxb.BadTypeValueError('%s cannot have value type %s' % (cls, type(value)))
942
943 if not isinstance(value, value_class):
944 raise pyxb.BadTypeValueError('Value type %s is not valid for %s' % (type(value), cls))
945 value_class.XsdConstraintsOK(value)
946
949
952
953 @classmethod
954 - def _description (cls, name_only=False, user_documentation=True):
955 name = cls._Name()
956 if name_only:
957 return name
958 desc = [ name, ' restriction of ', cls.XsdSuperType()._description(name_only=True) ]
959 if user_documentation and (cls._Documentation is not None):
960 desc.extend(["\n", cls._Documentation])
961 return ''.join(desc)
962
964 """Base class for union datatypes.
965
966 This class descends only from simpleTypeDefinition. A pyxb.LogicError is
967 raised if an attempt is made to construct an instance of a subclass of
968 STD_union. Values consistent with the member types are constructed using
969 the Factory class method. Values are validated using the _ValidatedMember
970 class method.
971
972 Subclasses must provide a class variable _MemberTypes which is a
973 tuple of legal members of the union."""
974
975 _MemberTypes = None
976 """A list of classes which are permitted as values of the union."""
977
978
979
980
981 __FacetMap = {}
982
983 @classmethod
985 """Given a value, attempt to create an instance of some member of this
986 union. The first instance which can be legally created is returned.
987
988 @keyword _validate_constraints: If True (default), any constructed
989 value is checked against constraints applied to the union as well as
990 the member type.
991
992 @raise pyxb.BadTypeValueError: no member type will permit creation of
993 an instance from the parameters in C{args} and C{kw}.
994 """
995
996 used_cls = cls._SupersedingClass()
997 state = used_cls._PreFactory_vx(args, kw)
998
999 rv = None
1000
1001 validate_constraints = kw.get('_validate_constraints', cls._PerformValidation())
1002 assert isinstance(validate_constraints, bool)
1003 if 0 < len(args):
1004 arg = args[0]
1005 try:
1006 rv = cls._ValidatedMember(arg)
1007 except pyxb.BadTypeValueError, e:
1008 pass
1009 if rv is None:
1010 kw['_validate_constraints'] = True
1011 for mt in cls._MemberTypes:
1012 try:
1013 rv = mt.Factory(*args, **kw)
1014 break
1015 except pyxb.BadTypeValueError:
1016 pass
1017 except ValueError:
1018 pass
1019 except:
1020 pass
1021 if rv is not None:
1022 if validate_constraints:
1023 cls.XsdConstraintsOK(rv)
1024 rv._postFactory_vx(state)
1025 return rv
1026 raise pyxb.BadTypeValueError('%s cannot construct union member from args %s' % (cls.__name__, args))
1027
1028 @classmethod
1030 """Validate the given value as a potential union member.
1031
1032 @raise pyxb.BadTypeValueError: the value is not an instance of a
1033 member type."""
1034 if not isinstance(value, cls._MemberTypes):
1035 for mt in cls._MemberTypes:
1036 try:
1037
1038
1039 value = mt.Factory(value, _validate_constraints=True)
1040 return value
1041 except (TypeError, pyxb.BadTypeValueError):
1042 pass
1043 raise pyxb.BadTypeValueError('%s cannot hold a member of type %s' % (cls.__name__, value.__class__.__name__))
1044 return value
1045
1047 raise pyxb.LogicError('%s: cannot construct instances of union' % (self.__class__.__name__,))
1048
1049 @classmethod
1050 - def _description (cls, name_only=False, user_documentation=True):
1057
1058 @classmethod
1062
1063
1064 -class STD_list (simpleTypeDefinition, types.ListType):
1065 """Base class for collection datatypes.
1066
1067 This class descends from the Python list type, and incorporates
1068 simpleTypeDefinition. Subclasses must define a class variable _ItemType
1069 which is a reference to the class of which members must be instances."""
1070
1071 _ItemType = None
1072 """A reference to the binding class for items within this list."""
1073
1074
1075
1076 __FacetMap = {}
1077
1078 @classmethod
1096
1097 @classmethod
1099
1100
1101 if 0 < len(args):
1102 arg1 = args[0]
1103 if isinstance(arg1, types.StringTypes):
1104 args = (arg1.split(),) + args[1:]
1105 arg1 = args[0]
1106 is_iterable = False
1107 try:
1108 iter(arg1)
1109 is_iterable = True
1110 except TypeError:
1111 pass
1112 if is_iterable:
1113 new_arg1 = []
1114 for i in range(len(arg1)):
1115 new_arg1.append(cls._ValidatedItem(arg1[i]))
1116 args = (new_arg1,) + args[1:]
1117 super_fn = getattr(super(STD_list, cls), '_ConvertArguments_vx', lambda *a,**kw: args)
1118 return super_fn(args, kw)
1119
1120 @classmethod
1123
1124 @classmethod
1126 """Convert from a binding value to a string usable in an XML document."""
1127 return ' '.join([ cls._ItemType.XsdLiteral(_v) for _v in value ])
1128
1129 @classmethod
1130 - def _description (cls, name_only=False, user_documentation=True):
1136
1137
1138 @classmethod
1141
1142
1145
1151
1154
1157
1158
1159
1162
1165
1168
1169 - def index (self, x, *args):
1171
1174
1177
1178 -class element (utility._DeconflictSymbols_mixin, _DynamicCreate_mixin):
1179 """Class that represents a schema element.
1180
1181 Global and local elements are represented by instances of this class.
1182 """
1183
1185 """The expanded name of the element within its scope."""
1186 return self.__name
1187 __name = None
1188
1192 __typeDefinition = None
1193
1195 """The scope of the element. This is either C{None}, representing a
1196 top-level element, or an instance of C{complexTypeDefinition} for
1197 local elements."""
1198 return self.__scope
1199 __scope = None
1200
1202 """Indicate whether values matching this element can have U{nil
1203 <http://www.w3.org/TR/xmlschema-1/#xsi_nil>} set."""
1204 return self.__nillable
1205 __nillable = False
1206
1208 """Indicate whether this element is abstract (must use substitution
1209 group members for matches)."""
1210 return self.__abstract
1211 __abstract = False
1212
1214 """Contents of any documentation annotation in the definition."""
1215 return self.__documentation
1216 __documentation = None
1217
1220 __defaultValue = None
1221
1223 """The L{element} instance to whose substitution group this element
1224 belongs. C{None} if this element is not part of a substitution
1225 group."""
1226 return self.__substitutionGroup
1232 __substitutionGroup = None
1233
1241
1243 """Determine whether an instance of this element can substitute for the other element.
1244
1245 See U{Substitution Group OK<http://www.w3.org/TR/xmlschema-1/#cos-equiv-derived-ok-rec>)}.
1246
1247 @todo: Do something about blocking constraints. This ignores them, as
1248 does everything leading to this point.
1249 """
1250 if self.substitutionGroup() is None:
1251 return False
1252 if other is None:
1253 return False
1254 assert isinstance(other, element)
1255
1256
1257 if other.scope() is not None:
1258 other = other.name().elementBinding()
1259 if other is None:
1260 return False
1261 assert other.scope() is None
1262
1263 if self.name().elementBinding() == other:
1264 return True
1265 return (self.substitutionGroup() == other) or self.substitutionGroup().substitutesFor(other)
1266
1268 """Stub replaced by _real_substitutesFor when element supports substitution groups."""
1269 return False
1270
1272 """Return a reference to the element instance used for the given name
1273 within this element.
1274
1275 The type for this element must be a complex type definition."""
1276 return self.typeDefinition()._UseForTag(name).elementBinding()
1277
1278 - def __init__ (self, name, type_definition, scope=None, nillable=False, abstract=False, default_value=None, substitution_group=None, documentation=None):
1290
1292 """Invoke the Factory method on the type associated with this element.
1293
1294 @keyword _dom_node: If set, specifies a DOM node that should be used
1295 for initialization. In that case, the L{createFromDOM} method is
1296 invoked instead of the type definition Factory method.
1297
1298 @raise pyxb.AbstractElementError: This element is abstract and no DOM
1299 node was provided.
1300 """
1301 dom_node = kw.pop('_dom_node', None)
1302 assert dom_node is None, 'Cannot pass DOM node directly to element constructor; use createFromDOM'
1303 if '_element' in kw:
1304 raise pyxb.LogicError('Cannot set _element in element-based instance creation')
1305 kw['_element'] = self
1306
1307 if self.abstract():
1308 raise pyxb.AbstractElementError(self)
1309 return self.typeDefinition().Factory(*args, **kw)
1310
1333
1334
1335 @classmethod
1347
1349 """Return the element that should be used if this element binding is
1350 permitted and an element with the given name is encountered.
1351
1352 Normally, the incoming name matches the name of this binding, and
1353 C{self} is returned. If the incoming name is different, it is
1354 expected to be the name of a global element which is within this
1355 element's substitution group. In that case, the binding corresponding
1356 to the named element is return.
1357
1358 @return: An instance of L{element}, or C{None} if no element with the
1359 given name can be found.
1360 """
1361
1362
1363 if self.name() == name:
1364 return self
1365
1366
1367 top_elt = self.name().elementBinding()
1368 if top_elt is None:
1369 return None
1370
1371
1372
1373
1374 elt_en = top_elt.name().adoptName(name)
1375 assert 'elementBinding' in elt_en.namespace()._categoryMap(), 'No element bindings in %s' % (elt_en.namespace(),)
1376 named_elt = elt_en.elementBinding()
1377 if (named_elt is None) or (named_elt == top_elt):
1378 return None
1379 if named_elt.substitutesFor(top_elt):
1380 return named_elt
1381 return None
1382
1383 - def createFromDOM (self, node, expanded_name=None, fallback_namespace=None, **kw):
1384 """Create a binding instance from the given DOM node.
1385
1386 @keyword expanded_name: Optional name for the DOM node. If not
1387 present, is inferred from C{node}.
1388
1389 @keyword fallback_namespace: Optional namespace to use when resolving
1390 unqualified names.
1391 """
1392 if xml.dom.Node.DOCUMENT_NODE == node.nodeType:
1393 node = node.documentElement
1394 if expanded_name is None:
1395 expanded_name = pyxb.namespace.ExpandedName(node, fallback_namespace=fallback_namespace)
1396 return self._createFromDOM(node, expanded_name, **kw)
1397
1399 """Create a binding instance from the given DOM node, using the
1400 provided name to identify the correct binding.
1401
1402 The context and information associated with this element is used to
1403 identify the actual element binding to use. By default, C{self} is
1404 used. If this element represents a term in a content model, the name
1405 and namespace of the incoming node may identify a different element.
1406 If that element is a member of this element's substitution group, the
1407 binding associated with the node's name will be used instead.
1408
1409 The type of object returned is determined by the type definition
1410 associated with the element binding and the value of any U{xsi:type
1411 <http://www.w3.org/TR/xmlschema-1/#xsi_type>} attribute found in the
1412 node, modulated by the configuration of L{XSI.ProcessTypeAttribute<pyxb.namespace.builtin._XMLSchema_instance.ProcessTypeAttribute>}.
1413
1414 Keyword parameters are passed to the factory method of the type
1415 associated with the selected element binding. See
1416 L{_TypeBinding_mixin} and any specializations of it.
1417
1418 @param expanded_name: The expanded name of the node. If the value is
1419 C{None}, defaults to the name of this element. (In the case of
1420 substitution groups, the default is wrong, but correct inference
1421 depends on context not available here.)
1422
1423 @keyword _fallback_namespace: Optional namespace to use when resolving
1424 unqualified type names.
1425
1426 @param node: The DOM node specifying the element content. If this is
1427 a (top-level) Document node, its element node is used.
1428 @type node: C{xml.dom.Node}
1429 @return: An instance of L{_TypeBinding_mixin}
1430 @raise pyxb.StructuralBadDocumentError: The node's name does identify an element binding.
1431 @raise pyxb.AbstractElementError: The element binding associated with the node is abstract.
1432 @raise pyxb.BadDocumentError: An U{xsi:type <http://www.w3.org/TR/xmlschema-1/#xsi_type>} attribute in the node fails to resolve to a recognized type
1433 @raise pyxb.BadDocumentError: An U{xsi:type <http://www.w3.org/TR/xmlschema-1/#xsi_type>} attribute in the node resolves to a type that is not a subclass of the type of the element binding.
1434 """
1435
1436
1437
1438 if xml.dom.Node.DOCUMENT_NODE == node.nodeType:
1439 node = node.documentElement
1440
1441
1442
1443
1444
1445 fallback_namespace = kw.pop('_fallback_namespace', None)
1446 element_binding = self.elementForName(expanded_name)
1447 if element_binding is None:
1448 raise pyxb.StructuralBadDocumentError('Element %s cannot create from node %s' % (self.name(), expanded_name))
1449
1450
1451
1452
1453 if element_binding.abstract():
1454 raise pyxb.AbstractElementError(element_binding)
1455
1456
1457
1458 if '_element' in kw:
1459 raise pyxb.LogicError('Cannot set _element in element-based instance creation')
1460 kw['_element'] = element_binding
1461
1462
1463
1464 type_class = element_binding.typeDefinition()
1465 elt_ns = element_binding.name().namespace()
1466
1467
1468
1469
1470
1471 ns_ctx = pyxb.namespace.resolution.NamespaceContext.GetNodeContext(node)
1472 (did_replace, type_class) = XSI._InterpretTypeAttribute(XSI.type.getAttribute(node), ns_ctx, fallback_namespace, type_class)
1473
1474
1475
1476
1477 is_nil = XSI.nil.getAttribute(node)
1478 if is_nil is not None:
1479 kw['_nil'] = pyxb.binding.datatypes.boolean(is_nil)
1480
1481 rv = type_class.Factory(_dom_node=node, _fallback_namespace=fallback_namespace, **kw)
1482 assert rv._element() == element_binding
1483 rv._setNamespaceContext(ns_ctx)
1484 if pyxb._ParsingRequiresValid:
1485 rv.validateBinding()
1486 return rv
1487
1489 return 'Element %s' % (self.name(),)
1490
1491 - def _description (self, name_only=False, user_documentation=True):
1505
1507 """Marker in case we need to know that a PST has an enumeration constraint facet."""
1508 pass
1509
1510 -class complexTypeDefinition (_TypeBinding_mixin, utility._DeconflictSymbols_mixin, _DynamicCreate_mixin):
1511 """Base for any Python class that serves as the binding for an
1512 XMLSchema complexType.
1513
1514 Subclasses should define a class-level _AttributeMap variable which maps
1515 from the unicode tag of an attribute to the AttributeUse instance that
1516 defines it. Similarly, subclasses should define an _ElementMap variable.
1517 """
1518
1519 _CT_EMPTY = 'EMPTY'
1520 _CT_SIMPLE = 'SIMPLE'
1521 _CT_MIXED = 'MIXED'
1522 _CT_ELEMENT_ONLY = 'ELEMENT_ONLY'
1523
1524 _ContentTypeTag = None
1525
1526 _TypeDefinition = None
1527 """Subclass of simpleTypeDefinition that corresponds to the type content.
1528 Only valid if _ContentTypeTag is _CT_SIMPLE"""
1529
1530
1531
1532
1533 _AttributeWildcard = None
1534
1535 _AttributeMap = { }
1536 """Map from expanded names to AttributeUse instances."""
1537
1538
1539
1540 _HasWildcardElement = False
1541
1542
1543 _ElementMap = { }
1544 """Map from expanded names to ElementUse instances."""
1545
1546
1547
1548 __wildcardAttributeMap = None
1549
1551 """Obtain access to wildcard attributes.
1552
1553 The return value is C{None} if this type does not support wildcard
1554 attributes. If wildcard attributes are allowed, the return value is a
1555 map from QNames to the unicode string value of the corresponding
1556 attribute.
1557
1558 @todo: The map keys should be namespace extended names rather than
1559 QNames, as the in-scope namespace may not be readily available to the
1560 user.
1561 """
1562 return self.__wildcardAttributeMap
1563
1564
1565
1566 __wildcardElements = None
1567
1569 """Obtain access to wildcard elements.
1570
1571 The return value is C{None} if the content model for this type does not
1572 support wildcard elements. If wildcard elements are allowed, the
1573 return value is a list of values corresponding to conformant
1574 unrecognized elements, in the order in which they were encountered.
1575 If the containing binding was created from an XML document and enough
1576 information was present to determine the binding of the member
1577 element, the value is a binding instance. Otherwise, the value is the
1578 original DOM Element node.
1579 """
1580 return self.__wildcardElements
1581
1582 @classmethod
1590
1592 """Create a new instance of this binding.
1593
1594 Arguments are used as transition values along the content model.
1595 Keywords are passed to the constructor of any simple content, or used
1596 to initialize attribute and element values whose L{id
1597 <content.ElementUse.id>} (not L{name <content.ElementUse.name>})
1598 matches the keyword.
1599
1600 @keyword _dom_node: The node to use as the source of binding content.
1601 @type _dom_node: C{xml.dom.Element}
1602
1603 """
1604
1605 fallback_namespace = kw.pop('_fallback_namespace', None)
1606 is_nil = False
1607 dom_node = kw.pop('_dom_node', None)
1608 if dom_node is not None:
1609 if isinstance(dom_node, pyxb.utils.utility.Locatable_mixin):
1610 self._setLocation(dom_node.location)
1611 if xml.dom.Node.DOCUMENT_NODE == dom_node.nodeType:
1612 dom_node = dom_node.documentElement
1613
1614 is_nil = XSI.nil.getAttribute(dom_node)
1615 if is_nil is not None:
1616 is_nil = kw['_nil'] = pyxb.binding.datatypes.boolean(is_nil)
1617 if self._AttributeWildcard is not None:
1618 self.__wildcardAttributeMap = { }
1619 if self._HasWildcardElement:
1620 self.__wildcardElements = []
1621 if self._Abstract:
1622 raise pyxb.AbstractInstantiationError(type(self))
1623 super(complexTypeDefinition, self).__init__(**kw)
1624 self.reset()
1625
1626 attribute_settings = { }
1627 if dom_node is not None:
1628 attribute_settings.update(self.__AttributesFromDOM(dom_node))
1629 for fu in self._AttributeMap.values():
1630 iv = kw.pop(fu.id(), None)
1631 if iv is not None:
1632 attribute_settings[fu.name()] = iv
1633 for (attr_en, value) in attribute_settings.items():
1634 au = self._setAttribute(attr_en, value)
1635 for fu in self._ElementMap.values():
1636 iv = kw.pop(fu.id(), None)
1637 if iv is not None:
1638 fu.set(self, iv)
1639 if kw and kw.pop('_strict_keywords', True):
1640 [ kw.pop(_fkw, None) for _fkw in self._PyXBFactoryKeywords ]
1641 if kw:
1642 raise pyxb.ExtraContentError(kw)
1643 if dom_node is not None:
1644 if self._CT_SIMPLE == self._ContentTypeTag:
1645 self.__initializeSimpleContent(args, dom_node)
1646 else:
1647 self._setContentFromDOM(dom_node, fallback_namespace)
1648 elif 0 < len(args):
1649 self.extend(args)
1650 else:
1651 if self._CT_SIMPLE == self._ContentTypeTag:
1652 self.__initializeSimpleContent(args, dom_node)
1653
1654 - def __initializeSimpleContent (self, args, dom_node=None):
1655
1656
1657
1658
1659 value = self._TypeDefinition.Factory(_require_value=not self._isNil(), _dom_node=dom_node, _nil=self._isNil(), *args)
1660 if value._constructedWithValue():
1661 if self._isNil():
1662 raise pyxb.ContentInNilElementError(value)
1663 else:
1664 self.append(value)
1665
1666
1667 _ReservedSymbols = _TypeBinding_mixin._ReservedSymbols.union(set([ 'wildcardElements', 'wildcardAttributeMap',
1668 'xsdConstraintsOK', 'content', 'append', 'extend', 'value', 'reset' ]))
1669
1670
1671
1672 _ContentModel = None
1673
1674 @classmethod
1676 """Method used by generated code to associate the element binding with a use in this type.
1677
1678 This is necessary because all complex type classes appear in the
1679 module prior to any of the element instances (which reference type
1680 classes), so the association must be formed after the element
1681 instances are available."""
1682 return cls._UseForTag(element.name())._setElementBinding(element)
1683
1684 @classmethod
1686 """Return the ElementUse object corresponding to the element name.
1687
1688 @param tag: The L{ExpandedName} of an element in the class."""
1689 rv = cls._ElementMap.get(tag)
1690 if (rv is None) and raise_if_fail:
1691 raise pyxb.NotAnElementError(tag, cls)
1692 return rv
1693
1695 """Generate a list of children in the order in which they should be
1696 added to the parent when creating a DOM representation of this
1697 object.
1698
1699 @note: This is not currently used; it is retained as an example of one
1700 way to override L{_validatedChildren} in cases where content model
1701 validation is not required.
1702 """
1703 order = []
1704 for eu in self._ElementMap.values():
1705 value = eu.value(self)
1706 if value is None:
1707 continue
1708 if isinstance(value, list):
1709 order.extend([ (eu, _v) for _v in value ])
1710 continue
1711 order.append( (eu, value) )
1712 return order
1713
1715 """Provide the child elements and non-element content in an order
1716 consistent with the content model.
1717
1718 Returns a sequence of tuples representing a valid path through the
1719 content model where each transition corresponds to one of the member
1720 element instances within this instance. The tuple is a pair
1721 comprising the L{content.ElementUse} instance and the value for the
1722 transition.
1723
1724 If the content of the instance does not validate against the content
1725 model, C{None} is returned.
1726
1727 The base class implementation uses the
1728 L{content.ContentModel.validate} method. Subclasses may desire to
1729 override this in cases where the desired order is not maintained by
1730 model interpretation (for example, when an "all" model is used and the
1731 original element order is desired). See L{__childrenForDOM} as an
1732 example of an alternative approach.
1733
1734 @return: C{None} or a list as described above.
1735 """
1736 if self._ContentTypeTag in (self._CT_EMPTY, self._CT_SIMPLE):
1737 return []
1738
1739 if self._ContentModel is None:
1740 raise pyxb.NoContentModel(self)
1741 path = self._ContentModel.validate(self._symbolSet())
1742 if path is not None:
1743 ( symbols, sequence ) = path
1744 if 0 == len(symbols):
1745 return sequence
1746 raise pyxb.BindingValidationError('Ungenerated symbols: %s' % (symbols,) )
1747 return None
1748
1750 """Return a map from L{content.ElementUse} instances to a list of
1751 values associated with that use.
1752
1753 This is used as the set of symbols available for transitions when
1754 validating content against a model. Note that the order of values
1755 within a use is likely to be significant, although the model cannot
1756 detect this.
1757
1758 The value C{None} should be used to provide a list of wildcard
1759 members.
1760
1761 If an element use has no associated values, it must not appear in the
1762 returned map.
1763 """
1764 rv = { }
1765 for eu in self._ElementMap.values():
1766 value = eu.value(self)
1767 if value is None:
1768 continue
1769 res = None
1770 converter = eu.elementBinding().compatibleValue
1771 if eu.isPlural():
1772 if 0 < len(value):
1773 rv[eu] = [ converter(_v) for _v in value ]
1774 else:
1775 rv[eu] = [ converter(value)]
1776 wce = self.wildcardElements()
1777 if (wce is not None) and (0 < len(wce)):
1778 rv[None] = wce
1779 return rv
1780
1784
1785
1806
1807 @classmethod
1827
1837
1839 """Initialize the attributes of this element from those of the DOM node.
1840
1841 @raise pyxb.UnrecognizedAttributeError: if the DOM node has attributes
1842 that are not allowed in this type
1843 @raise pyxb.ProhibitedAttributeError: a prohibited attribute was encountered
1844 @raise pyxb.MissingAttributeError: a required attribute could not be found
1845 """
1846
1847
1848 attrs_available = set(self._AttributeMap.values())
1849 for (attr_en, value) in attribute_settings.items():
1850 au = self._setAttribute(attr_en, value)
1851 if au is not None:
1852 attrs_available.remove(au)
1853
1854 return self
1855
1857 """Validate the content against the simple type.
1858
1859 @return: C{True} if the content validates against its type.
1860 @raise pyxb.NotSimpleContentError: this type does not have simple content.
1861 @raise pyxb.MissingContentError: the content of this type has not been set
1862 """
1863
1864 if self._CT_SIMPLE != self._ContentTypeTag:
1865 raise pyxb.NotSimpleContentError(self)
1866 if self._isNil():
1867 return True
1868 if self.value() is None:
1869 raise pyxb.MissingContentError(self)
1870 return self.value().xsdConstraintsOK()
1871
1872 __content = None
1873 - def content (self):
1874 """Return the content of the element.
1875
1876 This must be a complex type with complex content. The return value is
1877 a list of the element and non-element content in the order in which it
1878 was added.
1879 @raise pyxb.NotComplexContentError: this is not a complex type with mixed or element-only content
1880 """
1881 if self._ContentTypeTag in (self._CT_EMPTY, self._CT_SIMPLE):
1882 raise pyxb.NotComplexContentError(str(self._ExpandedName))
1883 return self.__content
1884
1886 """Return the value of the element.
1887
1888 This must be a complex type with simple content. The returned value
1889 is expected to be an instance of some L{simpleTypeDefinition} class.
1890
1891 @raise pyxb.NotSimpleContentError: this is not a complex type with simple content
1892 """
1893 if self._CT_SIMPLE != self._ContentTypeTag:
1894 raise pyxb.NotSimpleContentError('%s (%s)' % (str(self._ExpandedName), type(self)))
1895 return self.__content
1896
1897 - def _resetContent (self):
1898 if self._ContentTypeTag in (self._CT_MIXED, self._CT_ELEMENT_ONLY):
1899 self.__setContent([])
1900 else:
1901 self.__setContent(None)
1902
1903 __dfaStack = None
1920
1921 @classmethod
1923 """Determine what the given name means as an element in this type.
1924
1925 Normally, C{element_name} identifies an element definition within this
1926 type. If so, the returned C{element_use} identifies that definition,
1927 and the C{element_binding} is extracted from that use.
1928
1929 It may also be that the C{element_name} does not appear as an element
1930 definition, but that it identifies a global element. In that case,
1931 the returned C{element_binding} identifies the global element. If,
1932 further, that element is a member of a substitution group which does
1933 have an element definition in this class, then the returned
1934 C{element_use} identifies that definition.
1935
1936 If a non-C{None} C{element_use} is returned, there will be an
1937 associated C{element_binding}. However, it is possible to return a
1938 non-C{None} C{element_binding}, but C{None} as the C{element_use}. In
1939 that case, the C{element_binding} can be used to create a binding
1940 instance, but the content model will have to treat it as a wildcard.
1941
1942 @param element_name: The name of the element in this type, either an
1943 expanded name or a local name if the element has an absent namespace.
1944
1945 @return: C{( element_binding, element_use )}
1946 """
1947 element_use = cls._ElementMap.get(element_name)
1948 element_binding = None
1949 if element_use is None:
1950 try:
1951 element_binding = element_name.elementBinding()
1952 except pyxb.NamespaceError:
1953 pass
1954 if element_binding is not None:
1955 element_use = element_binding.findSubstituendUse(cls)
1956 else:
1957 element_binding = element_use.elementBinding()
1958 return (element_binding, element_use)
1959
1960 - def append (self, value, element_use=None, maybe_element=True, _fallback_namespace=None, require_validation=True):
1961 """Add the value to the instance.
1962
1963 The value should be a DOM node or other value that is or can be
1964 converted to a binding instance. If the instance has a DFA state, the
1965 value must be permitted by the content model.
1966
1967 @raise pyxb.ExtraContentError: the value is not permitted at the
1968 current state of the content model.
1969 """
1970
1971
1972
1973 element_binding = None
1974 if element_use is not None:
1975 import content
1976 assert isinstance(element_use, content.ElementUse)
1977 element_binding = element_use.elementBinding()
1978 assert element_binding is not None
1979
1980 if isinstance(value, xml.dom.Node):
1981 assert maybe_element
1982 assert element_binding is None
1983 node = value
1984 require_validation = pyxb._ParsingRequiresValid
1985 if xml.dom.Node.COMMENT_NODE == node.nodeType:
1986
1987
1988 return self
1989 if node.nodeType in (xml.dom.Node.TEXT_NODE, xml.dom.Node.CDATA_SECTION_NODE):
1990 value = node.data
1991 maybe_element = False
1992 else:
1993
1994 assert xml.dom.Node.ELEMENT_NODE == node.nodeType
1995 expanded_name = pyxb.namespace.ExpandedName(node, fallback_namespace=_fallback_namespace)
1996 (element_binding, element_use) = self._ElementBindingUseForName(expanded_name)
1997 if element_binding is not None:
1998 value = element_binding._createFromDOM(node, expanded_name, _fallback_namespace=_fallback_namespace)
1999 else:
2000
2001
2002
2003 try:
2004 ns = expanded_name.namespace()
2005 if ns is not None:
2006 for mr in ns.moduleRecords():
2007 try:
2008 if (mr.module() is None) and (mr.modulePath() is not None):
2009 print 'Importing %s to get binding for wildcard %s' % (mr.modulePath(), expanded_name)
2010 mod = __import__(mr.modulePath())
2011 for c in mr.modulePath().split('.')[1:]:
2012 mod = getattr(mod, c)
2013 mr._setModule(mod)
2014 value = mr.module().CreateFromDOM(node)
2015 break
2016 except pyxb.PyXBException, e:
2017 print 'Ignoring error when creating binding for wildcard %s: %s' % (expanded_name, e)
2018 except AttributeError, e:
2019
2020
2021
2022
2023 if mr.namespace() != pyxb.namespace.XMLSchema:
2024 raise
2025 except Exception, e:
2026 print 'WARNING: Unable to convert DOM node %s to Python instance: %s' % (expanded_name, e)
2027 if (not maybe_element) and isinstance(value, basestring) and (self._ContentTypeTag in (self._CT_EMPTY, self._CT_ELEMENT_ONLY)):
2028 if (0 == len(value.strip())) and not self._isNil():
2029 return self
2030 if self._isNil() and not self._IsSimpleTypeContent():
2031 raise pyxb.ExtraContentError('%s: Content %s present in element with xsi:nil' % (type(self), value))
2032 if maybe_element and (self.__dfaStack is not None):
2033
2034 if not require_validation:
2035 if element_use is not None:
2036 element_use.setOrAppend(self, value)
2037 return self
2038 raise pyxb.StructuralBadDocumentError('Validation is required when no element_use can be found')
2039 if self.wildcardElements() is not None:
2040 self._appendWildcardElement(value)
2041 return self
2042 else:
2043 if self.__dfaStack.step(self, value, element_use):
2044 return self
2045
2046
2047
2048 if (element_binding is not None) or isinstance(value, xml.dom.Node):
2049 raise pyxb.ExtraContentError('%s: Extra content starting with %s' % (self._ExpandedName, value,))
2050
2051
2052
2053 if self._IsSimpleTypeContent():
2054 if self.__content is not None:
2055 raise pyxb.ExtraContentError('Extra content starting with %s (already have %s)' % (value, self.__content))
2056 if not self._isNil():
2057 if not isinstance(value, self._TypeDefinition):
2058 value = self._TypeDefinition.Factory(value)
2059 self.__setContent(value)
2060 if require_validation:
2061
2062
2063
2064 self.xsdConstraintsOK()
2065 return self
2066
2067
2068 if not self._IsMixed():
2069 raise pyxb.UnexpectedNonElementContentError(value)
2070 if isinstance(value, _TypeBinding_mixin):
2071 raise pyxb.ExtraContentError('Extra content starting with %s' % (value,))
2072
2073 self._addContent(value, element_binding)
2074 return self
2075
2081
2082 - def extend (self, value_list, _fallback_namespace=None):
2083 """Invoke L{append} for each value in the list, in turn."""
2084 [ self.append(_v, _fallback_namespace=_fallback_namespace) for _v in value_list ]
2085 return self
2086
2087 - def __setContent (self, value):
2089
2090 - def _addContent (self, child, element_binding):
2091
2092
2093
2094 assert not (self._ContentTypeTag in (self._CT_EMPTY, self._CT_SIMPLE))
2095 if isinstance(child, _TypeBinding_mixin) and (child._element() is None):
2096 child._setElement(element_binding)
2097 self.__content.append(child)
2098
2099 @classmethod
2102
2103 - def _setContentFromDOM (self, node, _fallback_namespace):
2104 """Initialize the content of this element from the content of the DOM node."""
2105
2106 self.extend(node.childNodes[:], _fallback_namespace)
2107 if self._PerformValidation() and (not self._isNil()) and (self.__dfaStack is not None) and (not self.__dfaStack.isTerminal()):
2108 raise pyxb.MissingContentError()
2109 return self
2110
2116
2118 """Create a DOM element with the given tag holding the content of this instance."""
2119 element = parent
2120 self._setDOMFromAttributes(dom_support, element)
2121 if self._isNil():
2122 pass
2123 elif self._CT_EMPTY == self._ContentTypeTag:
2124 pass
2125 elif self._CT_SIMPLE == self._ContentTypeTag:
2126 assert self.value() is not None, '%s has no value' % (self,)
2127 element.appendChild(dom_support.document().createTextNode(self.value().xsdLiteral()))
2128 else:
2129 if pyxb._GenerationRequiresValid:
2130 order = self._validatedChildren()
2131 else:
2132 order = self.__childrenForDOM()
2133 if order is None:
2134 raise pyxb.DOMGenerationError('Binding value inconsistent with content model')
2135 for (eu, v) in order:
2136 assert v != self
2137 if eu is None:
2138 if isinstance(v, xml.dom.Node):
2139 element.appendChild(v)
2140 else:
2141 v.toDOM(dom_support, parent)
2142 else:
2143 eu.toDOM(dom_support, parent, v)
2144 mixed_content = self.content()
2145 for mc in mixed_content:
2146 pass
2147 return getattr(super(complexTypeDefinition, self), '_toDOM_csc', lambda *_args,**_kw: dom_support)(dom_support, parent)
2148
2149 @classmethod
2151 """CTDs with simple content are simple; other CTDs are not."""
2152 return cls._CT_SIMPLE == cls._ContentTypeTag
2153
2154 @classmethod
2155 - def _description (cls, name_only=False, user_documentation=True):
2181
2182 ConfigureBindingStyle(DEFAULT_BINDING_STYLE)
2183
2184
2185
2186
2187