You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

2937 lines
100 KiB

  1. # Copyright (c) 2003, The Regents of the University of California,
  2. # through Lawrence Berkeley National Laboratory (subject to receipt of
  3. # any required approvals from the U.S. Dept. of Energy). All rights
  4. # reserved.
  5. #
  6. # Copyright (c) 2001 Zope Corporation and Contributors. All Rights Reserved.
  7. #
  8. # This software is subject to the provisions of the Zope Public License,
  9. # Version 2.0 (ZPL). A copy of the ZPL should accompany this distribution.
  10. # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
  11. # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  12. # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
  13. # FOR A PARTICULAR PURPOSE.
  14. ident = "$Id$"
  15. import types, weakref, urllib, sys
  16. from threading import RLock
  17. try:
  18. from xml.ns import XMLNS
  19. except ImportError:
  20. # ref:
  21. # http://cvs.sourceforge.net/viewcvs.py/pyxml/xml/xml/ns.py?view=markup
  22. class XMLNS:
  23. """XMLNS, Namespaces in XML
  24. XMLNS (14-Jan-1999) is a W3C Recommendation. It is specified in
  25. http://www.w3.org/TR/REC-xml-names
  26. BASE -- the basic namespace defined by the specification
  27. XML -- the namespace for XML 1.0
  28. HTML -- the namespace for HTML4.0
  29. """
  30. BASE = "http://www.w3.org/2000/xmlns/"
  31. XML = "http://www.w3.org/XML/1998/namespace"
  32. HTML = "http://www.w3.org/TR/REC-html40"
  33. from Utility import DOM, Collection
  34. from StringIO import StringIO
  35. try:
  36. from xml.dom.ext import SplitQName
  37. except ImportError, ex:
  38. def SplitQName(qname):
  39. l = qname.split(':')
  40. if len(l) == 1:
  41. l.insert(0, None)
  42. elif len(l) == 2:
  43. if l[0] == 'xmlns':
  44. l.reverse()
  45. else:
  46. return
  47. return tuple(l)
  48. def GetSchema(component):
  49. """convience function for finding the parent XMLSchema instance.
  50. """
  51. parent = component
  52. while not isinstance(parent, XMLSchema):
  53. parent = parent._parent()
  54. return parent
  55. class SchemaReader:
  56. """A SchemaReader creates XMLSchema objects from urls and xml data.
  57. """
  58. def __init__(self, domReader=None, base_url=None):
  59. """domReader -- class must implement DOMAdapterInterface
  60. base_url -- base url string
  61. """
  62. self.__base_url = base_url
  63. self.__readerClass = domReader
  64. if not self.__readerClass:
  65. self.__readerClass = DOMAdapter
  66. self._includes = {}
  67. self._imports = {}
  68. def __setImports(self, schema):
  69. """Add dictionary of imports to schema instance.
  70. schema -- XMLSchema instance
  71. """
  72. for ns,val in schema.imports.items():
  73. if self._imports.has_key(ns):
  74. schema.addImportSchema(self._imports[ns])
  75. def __setIncludes(self, schema):
  76. """Add dictionary of includes to schema instance.
  77. schema -- XMLSchema instance
  78. """
  79. for schemaLocation, val in schema.includes.items():
  80. if self._includes.has_key(schemaLocation):
  81. schema.addIncludeSchema(self._imports[schemaLocation])
  82. def addSchemaByLocation(self, location, schema):
  83. """provide reader with schema document for a location.
  84. """
  85. self._includes[location] = schema
  86. def addSchemaByNamespace(self, schema):
  87. """provide reader with schema document for a targetNamespace.
  88. """
  89. self._imports[schema.targetNamespace] = schema
  90. def loadFromNode(self, parent, element):
  91. """element -- DOM node or document
  92. parent -- WSDLAdapter instance
  93. """
  94. reader = self.__readerClass(element)
  95. schema = XMLSchema(parent)
  96. #HACK to keep a reference
  97. schema.wsdl = parent
  98. schema.setBaseUrl(self.__base_url)
  99. schema.load(reader)
  100. return schema
  101. def loadFromStream(self, file):
  102. """Return an XMLSchema instance loaded from a file object.
  103. file -- file object
  104. """
  105. reader = self.__readerClass()
  106. reader.loadDocument(file)
  107. schema = XMLSchema()
  108. schema.setBaseUrl(self.__base_url)
  109. schema.load(reader)
  110. self.__setIncludes(schema)
  111. self.__setImports(schema)
  112. return schema
  113. def loadFromString(self, data):
  114. """Return an XMLSchema instance loaded from an XML string.
  115. data -- XML string
  116. """
  117. return self.loadFromStream(StringIO(data))
  118. def loadFromURL(self, url):
  119. """Return an XMLSchema instance loaded from the given url.
  120. url -- URL to dereference
  121. """
  122. if not url.endswith('xsd'):
  123. raise SchemaError, 'unknown file type %s' %url
  124. reader = self.__readerClass()
  125. if self.__base_url:
  126. url = urllib.basejoin(self.__base_url,url)
  127. reader.loadFromURL(url)
  128. schema = XMLSchema()
  129. schema.setBaseUrl(self.__base_url)
  130. schema.load(reader)
  131. self.__setIncludes(schema)
  132. self.__setImports(schema)
  133. return schema
  134. def loadFromFile(self, filename):
  135. """Return an XMLSchema instance loaded from the given file.
  136. filename -- name of file to open
  137. """
  138. file = open(filename, 'rb')
  139. try: schema = self.loadFromStream(file)
  140. finally: file.close()
  141. return schema
  142. class SchemaError(Exception):
  143. pass
  144. ###########################
  145. # DOM Utility Adapters
  146. ##########################
  147. class DOMAdapterInterface:
  148. def hasattr(self, attr, ns=None):
  149. """return true if node has attribute
  150. attr -- attribute to check for
  151. ns -- namespace of attribute, by default None
  152. """
  153. raise NotImplementedError, 'adapter method not implemented'
  154. def getContentList(self, *contents):
  155. """returns an ordered list of child nodes
  156. *contents -- list of node names to return
  157. """
  158. raise NotImplementedError, 'adapter method not implemented'
  159. def setAttributeDictionary(self, attributes):
  160. """set attribute dictionary
  161. """
  162. raise NotImplementedError, 'adapter method not implemented'
  163. def getAttributeDictionary(self):
  164. """returns a dict of node's attributes
  165. """
  166. raise NotImplementedError, 'adapter method not implemented'
  167. def getNamespace(self, prefix):
  168. """returns namespace referenced by prefix.
  169. """
  170. raise NotImplementedError, 'adapter method not implemented'
  171. def getTagName(self):
  172. """returns tagName of node
  173. """
  174. raise NotImplementedError, 'adapter method not implemented'
  175. def getParentNode(self):
  176. """returns parent element in DOMAdapter or None
  177. """
  178. raise NotImplementedError, 'adapter method not implemented'
  179. def loadDocument(self, file):
  180. """load a Document from a file object
  181. file --
  182. """
  183. raise NotImplementedError, 'adapter method not implemented'
  184. def loadFromURL(self, url):
  185. """load a Document from an url
  186. url -- URL to dereference
  187. """
  188. raise NotImplementedError, 'adapter method not implemented'
  189. class DOMAdapter(DOMAdapterInterface):
  190. """Adapter for ZSI.Utility.DOM
  191. """
  192. def __init__(self, node=None):
  193. """Reset all instance variables.
  194. element -- DOM document, node, or None
  195. """
  196. if hasattr(node, 'documentElement'):
  197. self.__node = node.documentElement
  198. else:
  199. self.__node = node
  200. self.__attributes = None
  201. def hasattr(self, attr, ns=None):
  202. """attr -- attribute
  203. ns -- optional namespace, None means unprefixed attribute.
  204. """
  205. if not self.__attributes:
  206. self.setAttributeDictionary()
  207. if ns:
  208. return self.__attributes.get(ns,{}).has_key(attr)
  209. return self.__attributes.has_key(attr)
  210. def getContentList(self, *contents):
  211. nodes = []
  212. ELEMENT_NODE = self.__node.ELEMENT_NODE
  213. for child in DOM.getElements(self.__node, None):
  214. if child.nodeType == ELEMENT_NODE and\
  215. SplitQName(child.tagName)[1] in contents:
  216. nodes.append(child)
  217. return map(self.__class__, nodes)
  218. def setAttributeDictionary(self):
  219. self.__attributes = {}
  220. for v in self.__node._attrs.values():
  221. self.__attributes[v.nodeName] = v.nodeValue
  222. def getAttributeDictionary(self):
  223. if not self.__attributes:
  224. self.setAttributeDictionary()
  225. return self.__attributes
  226. def getTagName(self):
  227. return self.__node.tagName
  228. def getParentNode(self):
  229. if self.__node.parentNode.nodeType == self.__node.ELEMENT_NODE:
  230. return DOMAdapter(self.__node.parentNode)
  231. return None
  232. def getNamespace(self, prefix):
  233. """prefix -- deference namespace prefix in node's context.
  234. Ascends parent nodes until found.
  235. """
  236. namespace = None
  237. if prefix == 'xmlns':
  238. namespace = DOM.findDefaultNS(prefix, self.__node)
  239. else:
  240. try:
  241. namespace = DOM.findNamespaceURI(prefix, self.__node)
  242. except DOMException, ex:
  243. if prefix != 'xml':
  244. raise SchemaError, '%s namespace not declared for %s'\
  245. %(prefix, self.__node._get_tagName())
  246. namespace = XMLNS
  247. return namespace
  248. def loadDocument(self, file):
  249. self.__node = DOM.loadDocument(file)
  250. if hasattr(self.__node, 'documentElement'):
  251. self.__node = self.__node.documentElement
  252. def loadFromURL(self, url):
  253. self.__node = DOM.loadFromURL(url)
  254. if hasattr(self.__node, 'documentElement'):
  255. self.__node = self.__node.documentElement
  256. class XMLBase:
  257. """ These class variables are for string indentation.
  258. """
  259. __indent = 0
  260. __rlock = RLock()
  261. def __str__(self):
  262. XMLBase.__rlock.acquire()
  263. XMLBase.__indent += 1
  264. tmp = "<" + str(self.__class__) + '>\n'
  265. for k,v in self.__dict__.items():
  266. tmp += "%s* %s = %s\n" %(XMLBase.__indent*' ', k, v)
  267. XMLBase.__indent -= 1
  268. XMLBase.__rlock.release()
  269. return tmp
  270. ##########################################################
  271. # Schema Components
  272. #########################################################
  273. class XMLSchemaComponent(XMLBase):
  274. """
  275. class variables:
  276. required -- list of required attributes
  277. attributes -- dict of default attribute values, including None.
  278. Value can be a function for runtime dependencies.
  279. contents -- dict of namespace keyed content lists.
  280. 'xsd' content of xsd namespace.
  281. xmlns_key -- key for declared xmlns namespace.
  282. xmlns -- xmlns is special prefix for namespace dictionary
  283. xml -- special xml prefix for xml namespace.
  284. """
  285. required = []
  286. attributes = {}
  287. contents = {}
  288. xmlns_key = ''
  289. xmlns = 'xmlns'
  290. xml = 'xml'
  291. def __init__(self, parent=None):
  292. """parent -- parent instance
  293. instance variables:
  294. attributes -- dictionary of node's attributes
  295. """
  296. self.attributes = None
  297. self._parent = parent
  298. if self._parent:
  299. self._parent = weakref.ref(parent)
  300. if not self.__class__ == XMLSchemaComponent\
  301. and not (type(self.__class__.required) == type(XMLSchemaComponent.required)\
  302. and type(self.__class__.attributes) == type(XMLSchemaComponent.attributes)\
  303. and type(self.__class__.contents) == type(XMLSchemaComponent.contents)):
  304. raise RuntimeError, 'Bad type for a class variable in %s' %self.__class__
  305. def getTargetNamespace(self):
  306. """return targetNamespace
  307. """
  308. parent = self
  309. targetNamespace = 'targetNamespace'
  310. tns = self.attributes.get(targetNamespace)
  311. while not tns:
  312. parent = parent._parent()
  313. tns = parent.attributes.get(targetNamespace)
  314. return tns
  315. def getTypeDefinition(self, attribute):
  316. """attribute -- attribute with a QName value (eg. type).
  317. collection -- check types collection in parent Schema instance
  318. """
  319. return self.getQNameAttribute('types', attribute)
  320. def getElementDeclaration(self, attribute):
  321. """attribute -- attribute with a QName value (eg. element).
  322. collection -- check elements collection in parent Schema instance.
  323. """
  324. return self.getQNameAttribute('elements', attribute)
  325. def getQNameAttribute(self, collection, attribute):
  326. """returns object instance representing QName --> (namespace,name),
  327. or if does not exist return None.
  328. attribute -- an information item attribute, with a QName value.
  329. collection -- collection in parent Schema instance to search.
  330. """
  331. obj = None
  332. tdc = self.attributes.get(attribute)
  333. if tdc:
  334. parent = GetSchema(self)
  335. if parent.targetNamespace == tdc.getTargetNamespace():
  336. obj = getattr(parent, collection)[tdc.getName()]
  337. elif parent.imports.has_key(tdc.getTargetNamespace()):
  338. schema = parent.imports[tdc.getTargetNamespace()].getSchema()
  339. obj = getattr(schema, collection)[tdc.getName()]
  340. return obj
  341. def getXMLNS(self, prefix=None):
  342. """deference prefix or by default xmlns, returns namespace.
  343. """
  344. parent = self
  345. ns = self.attributes[XMLSchemaComponent.xmlns].get(prefix or\
  346. XMLSchemaComponent.xmlns_key)
  347. while not ns:
  348. parent = parent._parent()
  349. ns = parent.attributes[XMLSchemaComponent.xmlns].get(prefix or\
  350. XMLSchemaComponent.xmlns_key)
  351. if not ns and isinstance(parent, WSDLToolsAdapter):
  352. raise SchemaError, 'unknown prefix %s' %prefix
  353. return ns
  354. def getAttribute(self, attribute):
  355. """return requested attribute or None
  356. """
  357. return self.attributes.get(attribute)
  358. def setAttributes(self, node):
  359. """Sets up attribute dictionary, checks for required attributes and
  360. sets default attribute values. attr is for default attribute values
  361. determined at runtime.
  362. structure of attributes dictionary
  363. ['xmlns'][xmlns_key] -- xmlns namespace
  364. ['xmlns'][prefix] -- declared namespace prefix
  365. [namespace][prefix] -- attributes declared in a namespace
  366. [attribute] -- attributes w/o prefix, default namespaces do
  367. not directly apply to attributes, ie Name can't collide
  368. with QName.
  369. """
  370. self.attributes = {XMLSchemaComponent.xmlns:{}}
  371. for k,v in node.getAttributeDictionary().items():
  372. prefix,value = SplitQName(k)
  373. if value == XMLSchemaComponent.xmlns:
  374. self.attributes[value][prefix or XMLSchemaComponent.xmlns_key] = v
  375. elif prefix:
  376. ns = node.getNamespace(prefix)
  377. if not ns:
  378. raise SchemaError, 'no namespace for attribute prefix %s'\
  379. %prefix
  380. if not self.attributes.has_key(ns):
  381. self.attributes[ns] = {}
  382. elif self.attributes[ns].has_key(value):
  383. raise SchemaError, 'attribute %s declared multiple times in %s'\
  384. %(value, ns)
  385. self.attributes[ns][value] = v
  386. elif not self.attributes.has_key(value):
  387. self.attributes[value] = v
  388. else:
  389. raise SchemaError, 'attribute %s declared multiple times' %value
  390. self.__checkAttributes()
  391. self.__setAttributeDefaults()
  392. #set QNames
  393. for k in ['type', 'element', 'base', 'ref', 'substitutionGroup', 'itemType']:
  394. if self.attributes.has_key(k):
  395. prefix, value = SplitQName(self.attributes.get(k))
  396. self.attributes[k] = \
  397. TypeDescriptionComponent((self.getXMLNS(prefix), value))
  398. #Union, memberTypes is a whitespace separated list of QNames
  399. for k in ['memberTypes']:
  400. if self.attributes.has_key(k):
  401. qnames = self.attributes[k]
  402. self.attributes[k] = []
  403. for qname in qnames.split():
  404. prefix, value = SplitQName(qname)
  405. self.attributes['memberTypes'].append(\
  406. TypeDescriptionComponent(\
  407. (self.getXMLNS(prefix), value)))
  408. def getContents(self, node):
  409. """retrieve xsd contents
  410. """
  411. return node.getContentList(*self.__class__.contents['xsd'])
  412. def __setAttributeDefaults(self):
  413. """Looks for default values for unset attributes. If
  414. class variable representing attribute is None, then
  415. it must be defined as an instance variable.
  416. """
  417. for k,v in self.__class__.attributes.items():
  418. if v and not self.attributes.has_key(k):
  419. if isinstance(v, types.FunctionType):
  420. self.attributes[k] = v(self)
  421. else:
  422. self.attributes[k] = v
  423. def __checkAttributes(self):
  424. """Checks that required attributes have been defined,
  425. attributes w/default cannot be required. Checks
  426. all defined attributes are legal, attribute
  427. references are not subject to this test.
  428. """
  429. for a in self.__class__.required:
  430. if not self.attributes.has_key(a):
  431. raise SchemaError,\
  432. 'class instance %s, missing required attribute %s'\
  433. %(self.__class__, a)
  434. for a in self.attributes.keys():
  435. if (a != XMLSchemaComponent.xmlns) and\
  436. (a not in self.__class__.attributes.keys()) and not\
  437. (self.isAttribute() and self.isReference()):
  438. raise SchemaError, '%s, unknown attribute' %a
  439. class WSDLToolsAdapter(XMLSchemaComponent):
  440. """WSDL Adapter to grab the attributes from the wsdl document node.
  441. """
  442. attributes = {'name':None, 'targetNamespace':None}
  443. def __init__(self, wsdl):
  444. #XMLSchemaComponent.__init__(self, None)
  445. XMLSchemaComponent.__init__(self, parent=wsdl)
  446. self.setAttributes(DOMAdapter(wsdl.document))
  447. def getImportSchemas(self):
  448. """returns WSDLTools.WSDL types Collection
  449. """
  450. return self._parent().types
  451. """Marker Interface: can determine something about an instances properties by using
  452. the provided convenience functions.
  453. """
  454. class DefinitionMarker:
  455. """marker for definitions
  456. """
  457. pass
  458. class DeclarationMarker:
  459. """marker for declarations
  460. """
  461. pass
  462. class AttributeMarker:
  463. """marker for attributes
  464. """
  465. pass
  466. class AttributeGroupMarker:
  467. """marker for attribute groups
  468. """
  469. pass
  470. class WildCardMarker:
  471. """marker for wildcards
  472. """
  473. pass
  474. class ElementMarker:
  475. """marker for wildcards
  476. """
  477. pass
  478. class ReferenceMarker:
  479. """marker for references
  480. """
  481. pass
  482. class ModelGroupMarker:
  483. """marker for model groups
  484. """
  485. pass
  486. class ExtensionMarker:
  487. """marker for extensions
  488. """
  489. pass
  490. class RestrictionMarker:
  491. """marker for restrictions
  492. """
  493. facets = ['enumeration', 'length', 'maxExclusive', 'maxInclusive',\
  494. 'maxLength', 'minExclusive', 'minInclusive', 'minLength',\
  495. 'pattern', 'fractionDigits', 'totalDigits', 'whiteSpace']
  496. class SimpleMarker:
  497. """marker for simple type information
  498. """
  499. pass
  500. class ComplexMarker:
  501. """marker for complex type information
  502. """
  503. pass
  504. class MarkerInterface:
  505. def isDefinition(self):
  506. return isinstance(self, DefinitionMarker)
  507. def isDeclaration(self):
  508. return isinstance(self, DeclarationMarker)
  509. def isAttribute(self):
  510. return isinstance(self, AttributeMarker)
  511. def isAttributeGroup(self):
  512. return isinstance(self, AttributeGroupMarker)
  513. def isElement(self):
  514. return isinstance(self, ElementMarker)
  515. def isReference(self):
  516. return isinstance(self, ReferenceMarker)
  517. def isWildCard(self):
  518. return isinstance(self, WildCardMarker)
  519. def isModelGroup(self):
  520. return isinstance(self, ModelGroupMarker)
  521. def isExtension(self):
  522. return isinstance(self, ExtensionMarker)
  523. def isRestriction(self):
  524. return isinstance(self, RestrictionMarker)
  525. def isSimple(self):
  526. return isinstance(self, SimpleMarker)
  527. def isComplex(self):
  528. return isinstance(self, ComplexMarker)
  529. class Notation(XMLSchemaComponent):
  530. """<notation>
  531. parent:
  532. schema
  533. attributes:
  534. id -- ID
  535. name -- NCName, Required
  536. public -- token, Required
  537. system -- anyURI
  538. contents:
  539. annotation?
  540. """
  541. required = ['name', 'public']
  542. attributes = {'id':None, 'name':None, 'public':None, 'system':None}
  543. contents = {'xsd':('annotation')}
  544. def __init__(self, parent):
  545. XMLSchemaComponent.__init__(self, parent)
  546. self.annotation = None
  547. def fromDom(self, node):
  548. self.setAttributes(node)
  549. contents = self.getContents(node)
  550. for i in contents:
  551. component = SplitQName(i.getTagName())[1]
  552. if component == 'annotation' and not self.annotation:
  553. self.annotation = Annotation(self)
  554. self.annotation.fromDom(i)
  555. else:
  556. raise SchemaError, 'Unknown component (%s)' %(i.getTagName())
  557. class Annotation(XMLSchemaComponent):
  558. """<annotation>
  559. parent:
  560. all,any,anyAttribute,attribute,attributeGroup,choice,complexContent,
  561. complexType,element,extension,field,group,import,include,key,keyref,
  562. list,notation,redefine,restriction,schema,selector,simpleContent,
  563. simpleType,union,unique
  564. attributes:
  565. id -- ID
  566. contents:
  567. (documentation | appinfo)*
  568. """
  569. attributes = {'id':None}
  570. contents = {'xsd':('documentation', 'appinfo')}
  571. def __init__(self, parent):
  572. XMLSchemaComponent.__init__(self, parent)
  573. self.content = None
  574. def fromDom(self, node):
  575. self.setAttributes(node)
  576. contents = self.getContents(node)
  577. content = []
  578. for i in contents:
  579. component = SplitQName(i.getTagName())[1]
  580. if component == 'documentation':
  581. #print_debug('class %s, documentation skipped' %self.__class__, 5)
  582. continue
  583. elif component == 'appinfo':
  584. #print_debug('class %s, appinfo skipped' %self.__class__, 5)
  585. continue
  586. else:
  587. raise SchemaError, 'Unknown component (%s)' %(i.getTagName())
  588. self.content = tuple(content)
  589. class Documentation(XMLSchemaComponent):
  590. """<documentation>
  591. parent:
  592. annotation
  593. attributes:
  594. source, anyURI
  595. xml:lang, language
  596. contents:
  597. mixed, any
  598. """
  599. attributes = {'source':None, 'xml:lang':None}
  600. contents = {'xsd':('mixed', 'any')}
  601. def __init__(self, parent):
  602. XMLSchemaComponent.__init__(self, parent)
  603. self.content = None
  604. def fromDom(self, node):
  605. self.setAttributes(node)
  606. contents = self.getContents(node)
  607. content = []
  608. for i in contents:
  609. component = SplitQName(i.getTagName())[1]
  610. if component == 'mixed':
  611. #print_debug('class %s, mixed skipped' %self.__class__, 5)
  612. continue
  613. elif component == 'any':
  614. #print_debug('class %s, any skipped' %self.__class__, 5)
  615. continue
  616. else:
  617. raise SchemaError, 'Unknown component (%s)' %(i.getTagName())
  618. self.content = tuple(content)
  619. class Appinfo(XMLSchemaComponent):
  620. """<appinfo>
  621. parent:
  622. annotation
  623. attributes:
  624. source, anyURI
  625. contents:
  626. mixed, any
  627. """
  628. attributes = {'source':None, 'anyURI':None}
  629. contents = {'xsd':('mixed', 'any')}
  630. def __init__(self, parent):
  631. XMLSchemaComponent.__init__(self, parent)
  632. self.content = None
  633. def fromDom(self, node):
  634. self.setAttributes(node)
  635. contents = self.getContents(node)
  636. content = []
  637. for i in contents:
  638. component = SplitQName(i.getTagName())[1]
  639. if component == 'mixed':
  640. #print_debug('class %s, mixed skipped' %self.__class__, 5)
  641. continue
  642. elif component == 'any':
  643. #print_debug('class %s, any skipped' %self.__class__, 5)
  644. continue
  645. else:
  646. raise SchemaError, 'Unknown component (%s)' %(i.getTagName())
  647. self.content = tuple(content)
  648. class XMLSchemaFake:
  649. # This is temporary, for the benefit of WSDL until the real thing works.
  650. def __init__(self, element):
  651. self.targetNamespace = DOM.getAttr(element, 'targetNamespace')
  652. self.element = element
  653. class XMLSchema(XMLSchemaComponent):
  654. """A schema is a collection of schema components derived from one
  655. or more schema documents, that is, one or more <schema> element
  656. information items. It represents the abstract notion of a schema
  657. rather than a single schema document (or other representation).
  658. <schema>
  659. parent:
  660. ROOT
  661. attributes:
  662. id -- ID
  663. version -- token
  664. xml:lang -- language
  665. targetNamespace -- anyURI
  666. attributeFormDefault -- 'qualified' | 'unqualified', 'unqualified'
  667. elementFormDefault -- 'qualified' | 'unqualified', 'unqualified'
  668. blockDefault -- '#all' | list of
  669. ('substitution | 'extension' | 'restriction')
  670. finalDefault -- '#all' | list of
  671. ('extension' | 'restriction' | 'list' | 'union')
  672. contents:
  673. ((include | import | redefine | annotation)*,
  674. (attribute, attributeGroup, complexType, element, group,
  675. notation, simpleType)*, annotation*)*
  676. attributes -- schema attributes
  677. imports -- import statements
  678. includes -- include statements
  679. redefines --
  680. types -- global simpleType, complexType definitions
  681. elements -- global element declarations
  682. attr_decl -- global attribute declarations
  683. attr_groups -- attribute Groups
  684. model_groups -- model Groups
  685. notations -- global notations
  686. """
  687. attributes = {'id':None,
  688. 'version':None,
  689. 'xml:lang':None,
  690. 'targetNamespace':None,
  691. 'attributeFormDefault':'unqualified',
  692. 'elementFormDefault':'unqualified',
  693. 'blockDefault':None,
  694. 'finalDefault':None}
  695. contents = {'xsd':('include', 'import', 'redefine', 'annotation', 'attribute',\
  696. 'attributeGroup', 'complexType', 'element', 'group',\
  697. 'notation', 'simpleType', 'annotation')}
  698. empty_namespace = ''
  699. def __init__(self, parent=None):
  700. """parent --
  701. instance variables:
  702. targetNamespace -- schema's declared targetNamespace, or empty string.
  703. _imported_schemas -- namespace keyed dict of schema dependencies, if
  704. a schema is provided instance will not resolve import statement.
  705. _included_schemas -- schemaLocation keyed dict of component schemas,
  706. if schema is provided instance will not resolve include statement.
  707. _base_url -- needed for relative URLs support, only works with URLs
  708. relative to initial document.
  709. includes -- collection of include statements
  710. imports -- collection of import statements
  711. elements -- collection of global element declarations
  712. types -- collection of global type definitions
  713. attr_decl -- collection of global attribute declarations
  714. attr_groups -- collection of global attribute group definitions
  715. model_groups -- collection of model group definitions
  716. notations -- collection of notations
  717. """
  718. self.targetNamespace = None
  719. XMLSchemaComponent.__init__(self, parent)
  720. f = lambda k: k.attributes['name']
  721. ns = lambda k: k.attributes['namespace']
  722. sl = lambda k: k.attributes['schemaLocation']
  723. self.includes = Collection(self, key=sl)
  724. self.imports = Collection(self, key=ns)
  725. self.elements = Collection(self, key=f)
  726. self.types = Collection(self, key=f)
  727. self.attr_decl = Collection(self, key=f)
  728. self.attr_groups = Collection(self, key=f)
  729. self.model_groups = Collection(self, key=f)
  730. self.notations = Collection(self, key=f)
  731. self._imported_schemas = {}
  732. self._included_schemas = {}
  733. self._base_url = None
  734. def addImportSchema(self, schema):
  735. """for resolving import statements in Schema instance
  736. schema -- schema instance
  737. _imported_schemas
  738. """
  739. if not isinstance(schema, XMLSchema):
  740. raise TypeError, 'expecting a Schema instance'
  741. if schema.targetNamespace != self.targetNamespace:
  742. self._imported_schemas[schema.targetNamespace] = schema
  743. else:
  744. raise SchemaError, 'import schema bad targetNamespace'
  745. def addIncludeSchema(self, schemaLocation, schema):
  746. """for resolving include statements in Schema instance
  747. schemaLocation -- schema location
  748. schema -- schema instance
  749. _included_schemas
  750. """
  751. if not isinstance(schema, XMLSchema):
  752. raise TypeError, 'expecting a Schema instance'
  753. if not schema.targetNamespace or\
  754. schema.targetNamespace == self.targetNamespace:
  755. self._included_schemas[schemaLocation] = schema
  756. else:
  757. raise SchemaError, 'include schema bad targetNamespace'
  758. def setImportSchemas(self, schema_dict):
  759. """set the import schema dictionary, which is used to
  760. reference depedent schemas.
  761. """
  762. self._imported_schemas = schema_dict
  763. def getImportSchemas(self):
  764. """get the import schema dictionary, which is used to
  765. reference depedent schemas.
  766. """
  767. return self._imported_schemas
  768. def getSchemaNamespacesToImport(self):
  769. """returns tuple of namespaces the schema instance has declared
  770. itself to be depedent upon.
  771. """
  772. return tuple(self.includes.keys())
  773. def setIncludeSchemas(self, schema_dict):
  774. """set the include schema dictionary, which is keyed with
  775. schemaLocation (uri).
  776. This is a means of providing
  777. schemas to the current schema for content inclusion.
  778. """
  779. self._included_schemas = schema_dict
  780. def getIncludeSchemas(self):
  781. """get the include schema dictionary, which is keyed with
  782. schemaLocation (uri).
  783. """
  784. return self._included_schemas
  785. def getBaseUrl(self):
  786. """get base url, used for normalizing all relative uri's
  787. """
  788. return self._base_url
  789. def setBaseUrl(self, url):
  790. """set base url, used for normalizing all relative uri's
  791. """
  792. self._base_url = url
  793. def getElementFormDefault(self):
  794. """return elementFormDefault attribute
  795. """
  796. return self.attributes.get('elementFormDefault')
  797. def getAttributeFormDefault(self):
  798. """return attributeFormDefault attribute
  799. """
  800. return self.attributes.get('attributeFormDefault')
  801. def getBlockDefault(self):
  802. """return blockDefault attribute
  803. """
  804. return self.attributes.get('blockDefault')
  805. def getFinalDefault(self):
  806. """return finalDefault attribute
  807. """
  808. return self.attributes.get('finalDefault')
  809. def load(self, node):
  810. pnode = node.getParentNode()
  811. if pnode:
  812. pname = SplitQName(pnode.getTagName())[1]
  813. if pname == 'types':
  814. attributes = {}
  815. self.setAttributes(pnode)
  816. attributes.update(self.attributes)
  817. self.setAttributes(node)
  818. for k,v in attributes['xmlns'].items():
  819. if not self.attributes['xmlns'].has_key(k):
  820. self.attributes['xmlns'][k] = v
  821. else:
  822. self.setAttributes(node)
  823. else:
  824. self.setAttributes(node)
  825. self.targetNamespace = self.getTargetNamespace()
  826. contents = self.getContents(node)
  827. indx = 0
  828. num = len(contents)
  829. while indx < num:
  830. while indx < num:
  831. node = contents[indx]
  832. component = SplitQName(node.getTagName())[1]
  833. if component == 'include':
  834. tp = self.__class__.Include(self)
  835. tp.fromDom(node)
  836. self.includes[tp.attributes['schemaLocation']] = tp
  837. schema = tp.getSchema()
  838. if schema.targetNamespace and \
  839. schema.targetNamespace != self.targetNamespace:
  840. raise SchemaError, 'included schema bad targetNamespace'
  841. for collection in ['imports','elements','types',\
  842. 'attr_decl','attr_groups','model_groups','notations']:
  843. for k,v in getattr(schema,collection).items():
  844. if not getattr(self,collection).has_key(k):
  845. v._parent = weakref.ref(self)
  846. getattr(self,collection)[k] = v
  847. elif component == 'import':
  848. tp = self.__class__.Import(self)
  849. tp.fromDom(node)
  850. import_ns = tp.getAttribute('namespace')
  851. if import_ns:
  852. if import_ns == self.targetNamespace:
  853. raise SchemaError,\
  854. 'import and schema have same targetNamespace'
  855. self.imports[import_ns] = tp
  856. else:
  857. self.imports[self.__class__.empty_namespace] = tp
  858. if not self.getImportSchemas().has_key(import_ns) and\
  859. tp.getAttribute('schemaLocation'):
  860. self.addImportSchema(tp.getSchema())
  861. elif component == 'redefine':
  862. #print_debug('class %s, redefine skipped' %self.__class__, 5)
  863. pass
  864. elif component == 'annotation':
  865. #print_debug('class %s, annotation skipped' %self.__class__, 5)
  866. pass
  867. else:
  868. break
  869. indx += 1
  870. # (attribute, attributeGroup, complexType, element, group,
  871. # notation, simpleType)*, annotation*)*
  872. while indx < num:
  873. node = contents[indx]
  874. component = SplitQName(node.getTagName())[1]
  875. if component == 'attribute':
  876. tp = AttributeDeclaration(self)
  877. tp.fromDom(node)
  878. self.attr_decl[tp.getAttribute('name')] = tp
  879. elif component == 'attributeGroup':
  880. tp = AttributeGroupDefinition(self)
  881. tp.fromDom(node)
  882. self.attr_groups[tp.getAttribute('name')] = tp
  883. elif component == 'complexType':
  884. tp = ComplexType(self)
  885. tp.fromDom(node)
  886. self.types[tp.getAttribute('name')] = tp
  887. elif component == 'element':
  888. tp = ElementDeclaration(self)
  889. tp.fromDom(node)
  890. self.elements[tp.getAttribute('name')] = tp
  891. elif component == 'group':
  892. tp = ModelGroupDefinition(self)
  893. tp.fromDom(node)
  894. self.model_groups[tp.getAttribute('name')] = tp
  895. elif component == 'notation':
  896. tp = Notation(self)
  897. tp.fromDom(node)
  898. self.notations[tp.getAttribute('name')] = tp
  899. elif component == 'simpleType':
  900. tp = SimpleType(self)
  901. tp.fromDom(node)
  902. self.types[tp.getAttribute('name')] = tp
  903. else:
  904. break
  905. indx += 1
  906. while indx < num:
  907. node = contents[indx]
  908. component = SplitQName(node.getTagName())[1]
  909. if component == 'annotation':
  910. #print_debug('class %s, annotation 2 skipped' %self.__class__, 5)
  911. pass
  912. else:
  913. break
  914. indx += 1
  915. class Import(XMLSchemaComponent, MarkerInterface):
  916. """<import>
  917. parent:
  918. schema
  919. attributes:
  920. id -- ID
  921. namespace -- anyURI
  922. schemaLocation -- anyURI
  923. contents:
  924. annotation?
  925. """
  926. attributes = {'id':None,
  927. 'namespace':None,
  928. 'schemaLocation':None}
  929. contents = {'xsd':['annotation']}
  930. def __init__(self, parent):
  931. XMLSchemaComponent.__init__(self, parent)
  932. self.annotation = None
  933. self._schema = None
  934. def fromDom(self, node):
  935. self.setAttributes(node)
  936. contents = self.getContents(node)
  937. if self.attributes['namespace'] == self._parent().attributes['targetNamespace']:
  938. raise SchemaError, 'namespace of schema and import match'
  939. for i in contents:
  940. component = SplitQName(i.getTagName())[1]
  941. if component == 'annotation' and not self.annotation:
  942. self.annotation = Annotation(self)
  943. self.annotation.fromDom(i)
  944. else:
  945. raise SchemaError, 'Unknown component (%s)' %(i.getTagName())
  946. def getSchema(self):
  947. """if schema is not defined, first look for a Schema class instance
  948. in parent Schema. Else if not defined resolve schemaLocation
  949. and create a new Schema class instance, and keep a hard reference.
  950. """
  951. if not self._schema:
  952. ns = self.attributes['namespace']
  953. schema = self._parent().getImportSchemas().get(ns)
  954. if not schema and self._parent()._parent:
  955. schema = self._parent()._parent().getImportSchemas().get(ns)
  956. if not schema:
  957. url = self.attributes.get('schemaLocation')
  958. if not url:
  959. raise SchemaError, 'namespace(%s) is unknown' %ns
  960. base_url = self._parent().getBaseUrl()
  961. reader = SchemaReader(base_url=base_url)
  962. reader._imports = self._parent().getImportSchemas()
  963. reader._includes = self._parent().getIncludeSchemas()
  964. self._schema = reader.loadFromURL(url)
  965. return self._schema or schema
  966. class Include(XMLSchemaComponent, MarkerInterface):
  967. """<include schemaLocation>
  968. parent:
  969. schema
  970. attributes:
  971. id -- ID
  972. schemaLocation -- anyURI, required
  973. contents:
  974. annotation?
  975. """
  976. required = ['schemaLocation']
  977. attributes = {'id':None,
  978. 'schemaLocation':None}
  979. contents = {'xsd':['annotation']}
  980. def __init__(self, parent):
  981. XMLSchemaComponent.__init__(self, parent)
  982. self.annotation = None
  983. self._schema = None
  984. def fromDom(self, node):
  985. self.setAttributes(node)
  986. contents = self.getContents(node)
  987. for i in contents:
  988. component = SplitQName(i.getTagName())[1]
  989. if component == 'annotation' and not self.annotation:
  990. self.annotation = Annotation(self)
  991. self.annotation.fromDom(i)
  992. else:
  993. raise SchemaError, 'Unknown component (%s)' %(i.getTagName())
  994. def getSchema(self):
  995. """if schema is not defined, first look for a Schema class instance
  996. in parent Schema. Else if not defined resolve schemaLocation
  997. and create a new Schema class instance.
  998. """
  999. if not self._schema:
  1000. #schema = self._parent()._parent()
  1001. schema = self._parent()
  1002. #self._schema = schema.getIncludeSchemas(\
  1003. # self.attributes['schemaLocation'])
  1004. self._schema = schema.getIncludeSchemas().get(\
  1005. self.attributes['schemaLocation']
  1006. )
  1007. if not self._schema:
  1008. url = self.attributes['schemaLocation']
  1009. reader = SchemaReader(base_url=schema.getBaseUrl())
  1010. reader._imports = schema.getImportSchemas()
  1011. reader._includes = schema.getIncludeSchemas()
  1012. self._schema = reader.loadFromURL(url)
  1013. return self._schema
  1014. class AttributeDeclaration(XMLSchemaComponent,\
  1015. MarkerInterface,\
  1016. AttributeMarker,\
  1017. DeclarationMarker):
  1018. """<attribute name>
  1019. parent:
  1020. schema
  1021. attributes:
  1022. id -- ID
  1023. name -- NCName, required
  1024. type -- QName
  1025. default -- string
  1026. fixed -- string
  1027. contents:
  1028. annotation?, simpleType?
  1029. """
  1030. required = ['name']
  1031. attributes = {'id':None,
  1032. 'name':None,
  1033. 'type':None,
  1034. 'default':None,
  1035. 'fixed':None}
  1036. contents = {'xsd':['annotation','simpleType']}
  1037. def __init__(self, parent):
  1038. XMLSchemaComponent.__init__(self, parent)
  1039. self.annotation = None
  1040. self.content = None
  1041. def fromDom(self, node):
  1042. """ No list or union support
  1043. """
  1044. self.setAttributes(node)
  1045. contents = self.getContents(node)
  1046. for i in contents:
  1047. component = SplitQName(i.getTagName())[1]
  1048. if component == 'annotation' and not self.annotation:
  1049. self.annotation = Annotation(self)
  1050. self.annotation.fromDom(i)
  1051. elif component == 'simpleType':
  1052. self.content = AnonymousSimpleType(self)
  1053. self.content.fromDom(i)
  1054. else:
  1055. raise SchemaError, 'Unknown component (%s)' %(i.getTagName())
  1056. class LocalAttributeDeclaration(AttributeDeclaration,\
  1057. MarkerInterface,\
  1058. AttributeMarker,\
  1059. DeclarationMarker):
  1060. """<attribute name>
  1061. parent:
  1062. complexType, restriction, extension, attributeGroup
  1063. attributes:
  1064. id -- ID
  1065. name -- NCName, required
  1066. type -- QName
  1067. form -- ('qualified' | 'unqualified'), schema.attributeFormDefault
  1068. use -- ('optional' | 'prohibited' | 'required'), optional
  1069. default -- string
  1070. fixed -- string
  1071. contents:
  1072. annotation?, simpleType?
  1073. """
  1074. required = ['name']
  1075. attributes = {'id':None,
  1076. 'name':None,
  1077. 'type':None,
  1078. 'form':lambda self: GetSchema(self).getAttributeFormDefault(),
  1079. 'use':'optional',
  1080. 'default':None,
  1081. 'fixed':None}
  1082. contents = {'xsd':['annotation','simpleType']}
  1083. def __init__(self, parent):
  1084. AttributeDeclaration.__init__(self, parent)
  1085. self.annotation = None
  1086. self.content = None
  1087. def fromDom(self, node):
  1088. self.setAttributes(node)
  1089. contents = self.getContents(node)
  1090. for i in contents:
  1091. component = SplitQName(i.getTagName())[1]
  1092. if component == 'annotation' and not self.annotation:
  1093. self.annotation = Annotation(self)
  1094. self.annotation.fromDom(i)
  1095. elif component == 'simpleType':
  1096. self.content = AnonymousSimpleType(self)
  1097. self.content.fromDom(i)
  1098. else:
  1099. raise SchemaError, 'Unknown component (%s)' %(i.getTagName())
  1100. class AttributeWildCard(XMLSchemaComponent,\
  1101. MarkerInterface,\
  1102. AttributeMarker,\
  1103. DeclarationMarker,\
  1104. WildCardMarker):
  1105. """<anyAttribute>
  1106. parents:
  1107. complexType, restriction, extension, attributeGroup
  1108. attributes:
  1109. id -- ID
  1110. namespace -- '##any' | '##other' |
  1111. (anyURI* | '##targetNamespace' | '##local'), ##any
  1112. processContents -- 'lax' | 'skip' | 'strict', strict
  1113. contents:
  1114. annotation?
  1115. """
  1116. attributes = {'id':None,
  1117. 'namespace':'##any',
  1118. 'processContents':'strict'}
  1119. contents = {'xsd':['annotation']}
  1120. def __init__(self, parent):
  1121. XMLSchemaComponent.__init__(self, parent)
  1122. self.annotation = None
  1123. def fromDom(self, node):
  1124. self.setAttributes(node)
  1125. contents = self.getContents(node)
  1126. for i in contents:
  1127. component = SplitQName(i.getTagName())[1]
  1128. if component == 'annotation' and not self.annotation:
  1129. self.annotation = Annotation(self)
  1130. self.annotation.fromDom(i)
  1131. else:
  1132. raise SchemaError, 'Unknown component (%s)' %(i.getTagName())
  1133. class AttributeReference(XMLSchemaComponent,\
  1134. MarkerInterface,\
  1135. AttributeMarker,\
  1136. ReferenceMarker):
  1137. """<attribute ref>
  1138. parents:
  1139. complexType, restriction, extension, attributeGroup
  1140. attributes:
  1141. id -- ID
  1142. ref -- QName, required
  1143. use -- ('optional' | 'prohibited' | 'required'), optional
  1144. default -- string
  1145. fixed -- string
  1146. contents:
  1147. annotation?
  1148. """
  1149. required = ['ref']
  1150. attributes = {'id':None,
  1151. 'ref':None,
  1152. 'use':'optional',
  1153. 'default':None,
  1154. 'fixed':None}
  1155. contents = {'xsd':['annotation']}
  1156. def __init__(self, parent):
  1157. XMLSchemaComponent.__init__(self, parent)
  1158. self.annotation = None
  1159. def fromDom(self, node):
  1160. self.setAttributes(node)
  1161. contents = self.getContents(node)
  1162. for i in contents:
  1163. component = SplitQName(i.getTagName())[1]
  1164. if component == 'annotation' and not self.annotation:
  1165. self.annotation = Annotation(self)
  1166. self.annotation.fromDom(i)
  1167. else:
  1168. raise SchemaError, 'Unknown component (%s)' %(i.getTagName())
  1169. class AttributeGroupDefinition(XMLSchemaComponent,\
  1170. MarkerInterface,\
  1171. AttributeGroupMarker,\
  1172. DefinitionMarker):
  1173. """<attributeGroup name>
  1174. parents:
  1175. schema, redefine
  1176. attributes:
  1177. id -- ID
  1178. name -- NCName, required
  1179. contents:
  1180. annotation?, (attribute | attributeGroup)*, anyAttribute?
  1181. """
  1182. required = ['name']
  1183. attributes = {'id':None,
  1184. 'name':None}
  1185. contents = {'xsd':['annotation']}
  1186. def __init__(self, parent):
  1187. XMLSchemaComponent.__init__(self, parent)
  1188. self.annotation = None
  1189. self.attr_content = None
  1190. def fromDom(self, node):
  1191. self.setAttributes(node)
  1192. contents = self.getContents(node)
  1193. content = []
  1194. for indx in range(len(contents)):
  1195. component = SplitQName(i.getTagName())[1]
  1196. if (component == 'annotation') and (not indx):
  1197. self.annotation = Annotation(self)
  1198. self.annotation.fromDom(contents[indx])
  1199. elif (component == 'attribute'):
  1200. if contents[indx].hasattr('name'):
  1201. content.append(AttributeDeclaration())
  1202. elif contents[indx].hasattr('ref'):
  1203. content.append(AttributeReference())
  1204. else:
  1205. raise SchemaError, 'Unknown attribute type'
  1206. content[-1].fromDom(contents[indx])
  1207. elif (component == 'attributeGroup'):
  1208. content.append(AttributeGroupReference())
  1209. content[-1].fromDom(contents[indx])
  1210. elif (component == 'anyAttribute') and (len(contents) == x+1):
  1211. content.append(AttributeWildCard())
  1212. content[-1].fromDom(contents[indx])
  1213. else:
  1214. raise SchemaError, 'Unknown component (%s)' %(i.getTagName())
  1215. self.attr_content = tuple(content)
  1216. class AttributeGroupReference(XMLSchemaComponent,\
  1217. MarkerInterface,\
  1218. AttributeGroupMarker,\
  1219. ReferenceMarker):
  1220. """<attributeGroup ref>
  1221. parents:
  1222. complexType, restriction, extension, attributeGroup
  1223. attributes:
  1224. id -- ID
  1225. ref -- QName, required
  1226. contents:
  1227. annotation?
  1228. """
  1229. required = ['ref']
  1230. attributes = {'id':None,
  1231. 'ref':None}
  1232. contents = {'xsd':['annotation']}
  1233. def __init__(self, parent):
  1234. XMLSchemaComponent.__init__(self, parent)
  1235. self.annotation = None
  1236. def fromDom(self, node):
  1237. self.setAttributes(node)
  1238. contents = self.getContents(node)
  1239. for i in contents:
  1240. component = SplitQName(i.getTagName())[1]
  1241. if component == 'annotation' and not self.annotation:
  1242. self.annotation = Annotation(self)
  1243. self.annotation.fromDom(i)
  1244. else:
  1245. raise SchemaError, 'Unknown component (%s)' %(i.getTagName())
  1246. ######################################################
  1247. # Elements
  1248. #####################################################
  1249. class IdentityConstrants(XMLSchemaComponent):
  1250. """Allow one to uniquely identify nodes in a document and ensure the
  1251. integrity of references between them.
  1252. attributes -- dictionary of attributes
  1253. selector -- XPath to selected nodes
  1254. fields -- list of XPath to key field
  1255. """
  1256. def __init__(self, parent):
  1257. XMLSchemaComponent.__init__(self, parent)
  1258. self.selector = None
  1259. self.fields = None
  1260. self.annotation = None
  1261. def fromDom(self, node):
  1262. self.setAttributes(node)
  1263. contents = self.getContents(node)
  1264. fields = []
  1265. for i in contents:
  1266. component = SplitQName(i.getTagName())[1]
  1267. if component in self.__class__.contents['xsd']:
  1268. if component == 'annotation' and not self.annotation:
  1269. self.annotation = Annotation(self)
  1270. self.annotation.fromDom(i)
  1271. elif component == 'selector':
  1272. self.selector = self.Selector(self)
  1273. self.selector.fromDom(i)
  1274. continue
  1275. elif component == 'field':
  1276. fields.append(self.Field(self))
  1277. fields[-1].fromDom(i)
  1278. continue
  1279. else:
  1280. raise SchemaError, 'Unknown component (%s)' %(i.getTagName())
  1281. else:
  1282. raise SchemaError, 'Unknown component (%s)' %(i.getTagName())
  1283. self.fields = tuple(fields)
  1284. class Constraint(XMLSchemaComponent):
  1285. def __init__(self, parent):
  1286. XMLSchemaComponent.__init__(self, parent)
  1287. self.annotation = None
  1288. def fromDom(self, node):
  1289. self.setAttributes(node)
  1290. contents = self.getContents(node)
  1291. for i in contents:
  1292. component = SplitQName(i.getTagName())[1]
  1293. if component in self.__class__.contents['xsd']:
  1294. if component == 'annotation' and not self.annotation:
  1295. self.annotation = Annotation(self)
  1296. self.annotation.fromDom(i)
  1297. else:
  1298. raise SchemaError, 'Unknown component (%s)' %(i.getTagName())
  1299. else:
  1300. raise SchemaError, 'Unknown component (%s)' %(i.getTagName())
  1301. class Selector(Constraint):
  1302. """<selector xpath>
  1303. parent:
  1304. unique, key, keyref
  1305. attributes:
  1306. id -- ID
  1307. xpath -- XPath subset, required
  1308. contents:
  1309. annotation?
  1310. """
  1311. required = ['xpath']
  1312. attributes = {'id':None,
  1313. 'xpath':None}
  1314. contents = {'xsd':['annotation']}
  1315. class Field(Constraint):
  1316. """<field xpath>
  1317. parent:
  1318. unique, key, keyref
  1319. attributes:
  1320. id -- ID
  1321. xpath -- XPath subset, required
  1322. contents:
  1323. annotation?
  1324. """
  1325. required = ['xpath']
  1326. attributes = {'id':None,
  1327. 'xpath':None}
  1328. contents = {'xsd':['annotation']}
  1329. class Unique(IdentityConstrants):
  1330. """<unique name> Enforce fields are unique w/i a specified scope.
  1331. parent:
  1332. element
  1333. attributes:
  1334. id -- ID
  1335. name -- NCName, required
  1336. contents:
  1337. annotation?, selector, field+
  1338. """
  1339. required = ['name']
  1340. attributes = {'id':None,
  1341. 'name':None}
  1342. contents = {'xsd':['annotation', 'selector', 'field']}
  1343. class Key(IdentityConstrants):
  1344. """<key name> Enforce fields are unique w/i a specified scope, and all
  1345. field values are present w/i document. Fields cannot
  1346. be nillable.
  1347. parent:
  1348. element
  1349. attributes:
  1350. id -- ID
  1351. name -- NCName, required
  1352. contents:
  1353. annotation?, selector, field+
  1354. """
  1355. required = ['name']
  1356. attributes = {'id':None,
  1357. 'name':None}
  1358. contents = {'xsd':['annotation', 'selector', 'field']}
  1359. class KeyRef(IdentityConstrants):
  1360. """<keyref name refer> Ensure a match between two sets of values in an
  1361. instance.
  1362. parent:
  1363. element
  1364. attributes:
  1365. id -- ID
  1366. name -- NCName, required
  1367. refer -- QName, required
  1368. contents:
  1369. annotation?, selector, field+
  1370. """
  1371. required = ['name', 'refer']
  1372. attributes = {'id':None,
  1373. 'name':None,
  1374. 'refer':None}
  1375. contents = {'xsd':['annotation', 'selector', 'field']}
  1376. class ElementDeclaration(XMLSchemaComponent,\
  1377. MarkerInterface,\
  1378. ElementMarker,\
  1379. DeclarationMarker):
  1380. """<element name>
  1381. parents:
  1382. schema
  1383. attributes:
  1384. id -- ID
  1385. name -- NCName, required
  1386. type -- QName
  1387. default -- string
  1388. fixed -- string
  1389. nillable -- boolean, false
  1390. abstract -- boolean, false
  1391. substitutionGroup -- QName
  1392. block -- ('#all' | ('substition' | 'extension' | 'restriction')*),
  1393. schema.blockDefault
  1394. final -- ('#all' | ('extension' | 'restriction')*),
  1395. schema.finalDefault
  1396. contents:
  1397. annotation?, (simpleType,complexType)?, (key | keyref | unique)*
  1398. """
  1399. required = ['name']
  1400. attributes = {'id':None,
  1401. 'name':None,
  1402. 'type':None,
  1403. 'default':None,
  1404. 'fixed':None,
  1405. 'nillable':0,
  1406. 'abstract':0,
  1407. 'block':lambda self: self._parent().getBlockDefault(),
  1408. 'final':lambda self: self._parent().getFinalDefault()}
  1409. contents = {'xsd':['annotation', 'simpleType', 'complexType', 'key',\
  1410. 'keyref', 'unique']}
  1411. def __init__(self, parent):
  1412. XMLSchemaComponent.__init__(self, parent)
  1413. self.annotation = None
  1414. self.content = None
  1415. self.constraints = None
  1416. def fromDom(self, node):
  1417. self.setAttributes(node)
  1418. contents = self.getContents(node)
  1419. constraints = []
  1420. for i in contents:
  1421. component = SplitQName(i.getTagName())[1]
  1422. if component in self.__class__.contents['xsd']:
  1423. if component == 'annotation' and not self.annotation:
  1424. self.annotation = Annotation(self)
  1425. self.annotation.fromDom(i)
  1426. elif component == 'simpleType' and not self.content:
  1427. self.content = AnonymousSimpleType(self)
  1428. self.content.fromDom(i)
  1429. elif component == 'complexType' and not self.content:
  1430. self.content = LocalComplexType(self)
  1431. self.content.fromDom(i)
  1432. elif component == 'key':
  1433. constraints.append(Key(self))
  1434. constraints[-1].fromDom(i)
  1435. elif component == 'keyref':
  1436. constraints.append(KeyRef(self))
  1437. constraints[-1].fromDom(i)
  1438. elif component == 'unique':
  1439. constraints.append(Unique(self))
  1440. constraints[-1].fromDom(i)
  1441. else:
  1442. raise SchemaError, 'Unknown component (%s)' %(i.getTagName())
  1443. else:
  1444. raise SchemaError, 'Unknown component (%s)' %(i.getTagName())
  1445. self.constraints = tuple(constraints)
  1446. class LocalElementDeclaration(ElementDeclaration):
  1447. """<element>
  1448. parents:
  1449. all, choice, sequence
  1450. attributes:
  1451. id -- ID
  1452. name -- NCName, required
  1453. form -- ('qualified' | 'unqualified'), schema.elementFormDefault
  1454. type -- QName
  1455. minOccurs -- Whole Number, 1
  1456. maxOccurs -- (Whole Number | 'unbounded'), 1
  1457. default -- string
  1458. fixed -- string
  1459. nillable -- boolean, false
  1460. block -- ('#all' | ('extension' | 'restriction')*), schema.blockDefault
  1461. contents:
  1462. annotation?, (simpleType,complexType)?, (key | keyref | unique)*
  1463. """
  1464. required = ['name']
  1465. attributes = {'id':None,
  1466. 'name':None,
  1467. 'form':lambda self: GetSchema(self).getElementFormDefault(),
  1468. 'type':None,
  1469. 'minOccurs':'1',
  1470. 'maxOccurs':'1',
  1471. 'default':None,
  1472. 'fixed':None,
  1473. 'nillable':0,
  1474. 'abstract':0,
  1475. 'block':lambda self: GetSchema(self).getBlockDefault()}
  1476. contents = {'xsd':['annotation', 'simpleType', 'complexType', 'key',\
  1477. 'keyref', 'unique']}
  1478. class ElementReference(XMLSchemaComponent,\
  1479. MarkerInterface,\
  1480. ElementMarker,\
  1481. ReferenceMarker):
  1482. """<element ref>
  1483. parents:
  1484. all, choice, sequence
  1485. attributes:
  1486. id -- ID
  1487. ref -- QName, required
  1488. minOccurs -- Whole Number, 1
  1489. maxOccurs -- (Whole Number | 'unbounded'), 1
  1490. contents:
  1491. annotation?
  1492. """
  1493. required = ['ref']
  1494. attributes = {'id':None,
  1495. 'ref':None,
  1496. 'minOccurs':'1',
  1497. 'maxOccurs':'1'}
  1498. contents = {'xsd':['annotation']}
  1499. def __init__(self, parent):
  1500. XMLSchemaComponent.__init__(self, parent)
  1501. self.annotation = None
  1502. def fromDom(self, node):
  1503. self.annotation = None
  1504. self.setAttributes(node)
  1505. for i in self.getContents(node):
  1506. component = SplitQName(i.getTagName())[1]
  1507. if component in self.__class__.contents['xsd']:
  1508. if component == 'annotation' and not self.annotation:
  1509. self.annotation = Annotation(self)
  1510. self.annotation.fromDom(i)
  1511. else:
  1512. raise SchemaError, 'Unknown component (%s)' %(i.getTagName())
  1513. class ElementWildCard(LocalElementDeclaration,\
  1514. WildCardMarker):
  1515. """<any>
  1516. parents:
  1517. choice, sequence
  1518. attributes:
  1519. id -- ID
  1520. minOccurs -- Whole Number, 1
  1521. maxOccurs -- (Whole Number | 'unbounded'), 1
  1522. namespace -- '##any' | '##other' |
  1523. (anyURI* | '##targetNamespace' | '##local'), ##any
  1524. processContents -- 'lax' | 'skip' | 'strict', strict
  1525. contents:
  1526. annotation?
  1527. """
  1528. required = []
  1529. attributes = {'id':None,
  1530. 'minOccurs':'1',
  1531. 'maxOccurs':'1',
  1532. 'namespace':'##any',
  1533. 'processContents':'strict'}
  1534. contents = {'xsd':['annotation']}
  1535. def __init__(self, parent):
  1536. XMLSchemaComponent.__init__(self, parent)
  1537. self.annotation = None
  1538. def fromDom(self, node):
  1539. self.annotation = None
  1540. self.setAttributes(node)
  1541. for i in self.getContents(node):
  1542. component = SplitQName(i.getTagName())[1]
  1543. if component in self.__class__.contents['xsd']:
  1544. if component == 'annotation' and not self.annotation:
  1545. self.annotation = Annotation(self)
  1546. self.annotation.fromDom(i)
  1547. else:
  1548. raise SchemaError, 'Unknown component (%s)' %(i.getTagName())
  1549. ######################################################
  1550. # Model Groups
  1551. #####################################################
  1552. class Sequence(XMLSchemaComponent,\
  1553. MarkerInterface,\
  1554. ModelGroupMarker):
  1555. """<sequence>
  1556. parents:
  1557. complexType, extension, restriction, group, choice, sequence
  1558. attributes:
  1559. id -- ID
  1560. minOccurs -- Whole Number, 1
  1561. maxOccurs -- (Whole Number | 'unbounded'), 1
  1562. contents:
  1563. annotation?, (element | group | choice | sequence | any)*
  1564. """
  1565. attributes = {'id':None,
  1566. 'minOccurs':'1',
  1567. 'maxOccurs':'1'}
  1568. contents = {'xsd':['annotation', 'element', 'group', 'choice', 'sequence',\
  1569. 'any']}
  1570. def __init__(self, parent):
  1571. XMLSchemaComponent.__init__(self, parent)
  1572. self.annotation = None
  1573. self.content = None
  1574. def fromDom(self, node):
  1575. self.setAttributes(node)
  1576. contents = self.getContents(node)
  1577. content = []
  1578. for i in contents:
  1579. component = SplitQName(i.getTagName())[1]
  1580. if component in self.__class__.contents['xsd']:
  1581. if component == 'annotation' and not self.annotation:
  1582. self.annotation = Annotation(self)
  1583. self.annotation.fromDom(i)
  1584. continue
  1585. elif component == 'element':
  1586. if i.hasattr('ref'):
  1587. content.append(ElementReference(self))
  1588. else:
  1589. content.append(LocalElementDeclaration(self))
  1590. elif component == 'group':
  1591. content.append(ModelGroupReference(self))
  1592. elif component == 'choice':
  1593. content.append(Choice(self))
  1594. elif component == 'sequence':
  1595. content.append(Sequence(self))
  1596. elif component == 'any':
  1597. content.append(ElementWildCard(self))
  1598. else:
  1599. raise SchemaError, 'Unknown component (%s)' %(i.getTagName())
  1600. content[-1].fromDom(i)
  1601. else:
  1602. raise SchemaError, 'Unknown component (%s)' %(i.getTagName())
  1603. self.content = tuple(content)
  1604. class All(XMLSchemaComponent,\
  1605. MarkerInterface,\
  1606. ModelGroupMarker):
  1607. """<all>
  1608. parents:
  1609. complexType, extension, restriction, group
  1610. attributes:
  1611. id -- ID
  1612. minOccurs -- '0' | '1', 1
  1613. maxOccurs -- '1', 1
  1614. contents:
  1615. annotation?, element*
  1616. """
  1617. attributes = {'id':None,
  1618. 'minOccurs':'1',
  1619. 'maxOccurs':'1'}
  1620. contents = {'xsd':['annotation', 'element']}
  1621. def __init__(self, parent):
  1622. XMLSchemaComponent.__init__(self, parent)
  1623. self.annotation = None
  1624. self.content = None
  1625. def fromDom(self, node):
  1626. self.setAttributes(node)
  1627. contents = self.getContents(node)
  1628. content = []
  1629. for i in contents:
  1630. component = SplitQName(i.getTagName())[1]
  1631. if component in self.__class__.contents['xsd']:
  1632. if component == 'annotation' and not self.annotation:
  1633. self.annotation = Annotation(self)
  1634. self.annotation.fromDom(i)
  1635. continue
  1636. elif component == 'element':
  1637. if i.hasattr('ref'):
  1638. content.append(ElementReference(self))
  1639. else:
  1640. content.append(LocalElementDeclaration(self))
  1641. else:
  1642. raise SchemaError, 'Unknown component (%s)' %(i.getTagName())
  1643. content[-1].fromDom(i)
  1644. else:
  1645. raise SchemaError, 'Unknown component (%s)' %(i.getTagName())
  1646. self.content = tuple(content)
  1647. class Choice(XMLSchemaComponent,\
  1648. MarkerInterface,\
  1649. ModelGroupMarker):
  1650. """<choice>
  1651. parents:
  1652. complexType, extension, restriction, group, choice, sequence
  1653. attributes:
  1654. id -- ID
  1655. minOccurs -- Whole Number, 1
  1656. maxOccurs -- (Whole Number | 'unbounded'), 1
  1657. contents:
  1658. annotation?, (element | group | choice | sequence | any)*
  1659. """
  1660. attributes = {'id':None,
  1661. 'minOccurs':'1',
  1662. 'maxOccurs':'1'}
  1663. contents = {'xsd':['annotation', 'element', 'group', 'choice', 'sequence',\
  1664. 'any']}
  1665. def __init__(self, parent):
  1666. XMLSchemaComponent.__init__(self, parent)
  1667. self.annotation = None
  1668. self.content = None
  1669. def fromDom(self, node):
  1670. self.setAttributes(node)
  1671. contents = self.getContents(node)
  1672. content = []
  1673. for i in contents:
  1674. component = SplitQName(i.getTagName())[1]
  1675. if component in self.__class__.contents['xsd']:
  1676. if component == 'annotation' and not self.annotation:
  1677. self.annotation = Annotation(self)
  1678. self.annotation.fromDom(i)
  1679. continue
  1680. elif component == 'element':
  1681. if i.hasattr('ref'):
  1682. content.append(ElementReference(self))
  1683. else:
  1684. content.append(LocalElementDeclaration(self))
  1685. elif component == 'group':
  1686. content.append(ModelGroupReference(self))
  1687. elif component == 'choice':
  1688. content.append(Choice(self))
  1689. elif component == 'sequence':
  1690. content.append(Sequence(self))
  1691. elif component == 'any':
  1692. content.append(ElementWildCard(self))
  1693. else:
  1694. raise SchemaError, 'Unknown component (%s)' %(i.getTagName())
  1695. content[-1].fromDom(i)
  1696. else:
  1697. raise SchemaError, 'Unknown component (%s)' %(i.getTagName())
  1698. self.content = tuple(content)
  1699. class ModelGroupDefinition(XMLSchemaComponent,\
  1700. MarkerInterface,\
  1701. ModelGroupMarker,\
  1702. DefinitionMarker):
  1703. """<group name>
  1704. parents:
  1705. redefine, schema
  1706. attributes:
  1707. id -- ID
  1708. name -- NCName, required
  1709. contents:
  1710. annotation?, (all | choice | sequence)?
  1711. """
  1712. required = ['name']
  1713. attributes = {'id':None,
  1714. 'name':None}
  1715. contents = {'xsd':['annotation', 'all', 'choice', 'sequence']}
  1716. def __init__(self, parent):
  1717. XMLSchemaComponent.__init__(self, parent)
  1718. self.annotation = None
  1719. self.content = None
  1720. def fromDom(self, node):
  1721. self.setAttributes(node)
  1722. contents = self.getContents(node)
  1723. for i in contents:
  1724. component = SplitQName(i.getTagName())[1]
  1725. if component in self.__class__.contents['xsd']:
  1726. if component == 'annotation' and not self.annotation:
  1727. self.annotation = Annotation()
  1728. self.annotation.fromDom(i)
  1729. continue
  1730. elif component == 'all' and not self.content:
  1731. self.content = All(self)
  1732. elif component == 'choice' and not self.content:
  1733. self.content = Choice(self)
  1734. elif component == 'sequence' and not self.content:
  1735. self.content = Sequence(self)
  1736. else:
  1737. raise SchemaError, 'Unknown component (%s)' %(i.getTagName())
  1738. self.content.fromDom(i)
  1739. else:
  1740. raise SchemaError, 'Unknown component (%s)' %(i.getTagName())
  1741. class ModelGroupReference(XMLSchemaComponent,\
  1742. MarkerInterface,\
  1743. ModelGroupMarker,\
  1744. ReferenceMarker):
  1745. """<group ref>
  1746. parents:
  1747. choice, complexType, extension, restriction, sequence
  1748. attributes:
  1749. id -- ID
  1750. ref -- NCName, required
  1751. contents:
  1752. annotation?
  1753. """
  1754. required = ['ref']
  1755. attributes = {'id':None,
  1756. 'ref':None}
  1757. contents = {'xsd':['annotation']}
  1758. def __init__(self, parent):
  1759. XMLSchemaComponent.__init__(self, parent)
  1760. self.annotation = None
  1761. def fromDom(self, node):
  1762. self.setAttributes(node)
  1763. contents = self.getContents(node)
  1764. for i in contents:
  1765. component = SplitQName(i.getTagName())[1]
  1766. if component in self.__class__.contents['xsd']:
  1767. if component == 'annotation' and not self.annotation:
  1768. self.annotation = Annotation(self)
  1769. self.annotation.fromDom(i)
  1770. else:
  1771. raise SchemaError, 'Unknown component (%s)' %(i.getTagName())
  1772. else:
  1773. raise SchemaError, 'Unknown component (%s)' %(i.getTagName())
  1774. class ComplexType(XMLSchemaComponent,\
  1775. MarkerInterface,\
  1776. DefinitionMarker,\
  1777. ComplexMarker):
  1778. """<complexType name>
  1779. parents:
  1780. redefine, schema
  1781. attributes:
  1782. id -- ID
  1783. name -- NCName, required
  1784. mixed -- boolean, false
  1785. abstract -- boolean, false
  1786. block -- ('#all' | ('extension' | 'restriction')*), schema.blockDefault
  1787. final -- ('#all' | ('extension' | 'restriction')*), schema.finalDefault
  1788. contents:
  1789. annotation?, (simpleContent | complexContent |
  1790. ((group | all | choice | sequence)?, (attribute | attributeGroup)*, anyAttribute?))
  1791. """
  1792. required = ['name']
  1793. attributes = {'id':None,
  1794. 'name':None,
  1795. 'mixed':0,
  1796. 'abstract':0,
  1797. 'block':lambda self: self._parent().getBlockDefault(),
  1798. 'final':lambda self: self._parent().getFinalDefault()}
  1799. contents = {'xsd':['annotation', 'simpleContent', 'complexContent',\
  1800. 'group', 'all', 'choice', 'sequence', 'attribute', 'attributeGroup',\
  1801. 'anyAttribute', 'any']}
  1802. def __init__(self, parent):
  1803. XMLSchemaComponent.__init__(self, parent)
  1804. self.annotation = None
  1805. self.content = None
  1806. self.attr_content = None
  1807. def fromDom(self, node):
  1808. self.setAttributes(node)
  1809. contents = self.getContents(node)
  1810. indx = 0
  1811. num = len(contents)
  1812. #XXX ugly
  1813. if not num:
  1814. return
  1815. component = SplitQName(contents[indx].getTagName())[1]
  1816. if component == 'annotation':
  1817. self.annotation = Annotation(self)
  1818. self.annotation.fromDom(contents[indx])
  1819. indx += 1
  1820. component = SplitQName(contents[indx].getTagName())[1]
  1821. self.content = None
  1822. if component == 'simpleContent':
  1823. self.content = self.__class__.SimpleContent(self)
  1824. self.content.fromDom(contents[indx])
  1825. elif component == 'complexContent':
  1826. self.content = self.__class__.ComplexContent(self)
  1827. self.content.fromDom(contents[indx])
  1828. else:
  1829. if component == 'all':
  1830. self.content = All(self)
  1831. elif component == 'choice':
  1832. self.content = Choice(self)
  1833. elif component == 'sequence':
  1834. self.content = Sequence(self)
  1835. elif component == 'group':
  1836. self.content = ModelGroupReference(self)
  1837. if self.content:
  1838. self.content.fromDom(contents[indx])
  1839. indx += 1
  1840. self.attr_content = []
  1841. while indx < num:
  1842. component = SplitQName(contents[indx].getTagName())[1]
  1843. if component == 'attribute':
  1844. if contents[indx].hasattr('ref'):
  1845. self.attr_content.append(AttributeReference(self))
  1846. else:
  1847. self.attr_content.append(LocalAttributeDeclaration(self))
  1848. elif component == 'attributeGroup':
  1849. self.attr_content.append(AttributeGroupReference(self))
  1850. elif component == 'anyAttribute':
  1851. self.attr_content.append(AttributeWildCard(self))
  1852. else:
  1853. raise SchemaError, 'Unknown component (%s)' %(contents[indx].getTagName())
  1854. self.attr_content[-1].fromDom(contents[indx])
  1855. indx += 1
  1856. class _DerivedType(XMLSchemaComponent):
  1857. def __init__(self, parent):
  1858. XMLSchemaComponent.__init__(self, parent)
  1859. self.annotation = None
  1860. self.derivation = None
  1861. def fromDom(self, node):
  1862. self.setAttributes(node)
  1863. contents = self.getContents(node)
  1864. for i in contents:
  1865. component = SplitQName(i.getTagName())[1]
  1866. if component in self.__class__.contents['xsd']:
  1867. if component == 'annotation' and not self.annotation:
  1868. self.annotation = Annotation(self)
  1869. self.annotation.fromDom(i)
  1870. continue
  1871. elif component == 'restriction' and not self.derivation:
  1872. self.derivation = self.__class__.Restriction(self)
  1873. elif component == 'extension' and not self.derivation:
  1874. self.derivation = self.__class__.Extension(self)
  1875. else:
  1876. raise SchemaError, 'Unknown component (%s)' %(i.getTagName())
  1877. else:
  1878. raise SchemaError, 'Unknown component (%s)' %(i.getTagName())
  1879. self.derivation.fromDom(i)
  1880. class ComplexContent(_DerivedType,\
  1881. MarkerInterface,\
  1882. ComplexMarker):
  1883. """<complexContent>
  1884. parents:
  1885. complexType
  1886. attributes:
  1887. id -- ID
  1888. mixed -- boolean, false
  1889. contents:
  1890. annotation?, (restriction | extension)
  1891. """
  1892. attributes = {'id':None,
  1893. 'mixed':0 }
  1894. contents = {'xsd':['annotation', 'restriction', 'extension']}
  1895. class _DerivationBase(XMLSchemaComponent):
  1896. """<extension>,<restriction>
  1897. parents:
  1898. complexContent
  1899. attributes:
  1900. id -- ID
  1901. base -- QName, required
  1902. contents:
  1903. annotation?, (group | all | choice | sequence)?,
  1904. (attribute | attributeGroup)*, anyAttribute?
  1905. """
  1906. required = ['base']
  1907. attributes = {'id':None,
  1908. 'base':None }
  1909. contents = {'xsd':['annotation', 'group', 'all', 'choice',\
  1910. 'sequence', 'attribute', 'attributeGroup', 'anyAttribute']}
  1911. def fromDom(self, node):
  1912. self.setAttributes(node)
  1913. contents = self.getContents(node)
  1914. indx = 0
  1915. num = len(contents)
  1916. #XXX ugly
  1917. if not num:
  1918. return
  1919. component = SplitQName(contents[indx].getTagName())[1]
  1920. if component == 'annotation':
  1921. self.annotation = Annotation(self)
  1922. self.annotation.fromDom(contents[indx])
  1923. indx += 1
  1924. component = SplitQName(contents[indx].getTagName())[1]
  1925. if component == 'all':
  1926. self.content = All(self)
  1927. self.content.fromDom(contents[indx])
  1928. indx += 1
  1929. elif component == 'choice':
  1930. self.content = Choice(self)
  1931. self.content.fromDom(contents[indx])
  1932. indx += 1
  1933. elif component == 'sequence':
  1934. self.content = Sequence(self)
  1935. self.content.fromDom(contents[indx])
  1936. indx += 1
  1937. elif component == 'group':
  1938. self.content = ModelGroupReference(self)
  1939. self.content.fromDom(contents[indx])
  1940. indx += 1
  1941. else:
  1942. self.content = None
  1943. self.attr_content = []
  1944. while indx < num:
  1945. component = SplitQName(contents[indx].getTagName())[1]
  1946. if component == 'attribute':
  1947. if contents[indx].hasattr('ref'):
  1948. self.attr_content.append(AttributeReference(self))
  1949. else:
  1950. self.attr_content.append(LocalAttributeDeclaration(self))
  1951. elif component == 'attributeGroup':
  1952. self.attr_content.append(AttributeGroupDefinition(self))
  1953. elif component == 'anyAttribute':
  1954. self.attr_content.append(AttributeWildCard(self))
  1955. else:
  1956. raise SchemaError, 'Unknown component (%s)' %(contents[indx].getTagName())
  1957. self.attr_content[-1].fromDom(contents[indx])
  1958. indx += 1
  1959. class Extension(_DerivationBase, MarkerInterface, ExtensionMarker):
  1960. """<extension base>
  1961. parents:
  1962. complexContent
  1963. attributes:
  1964. id -- ID
  1965. base -- QName, required
  1966. contents:
  1967. annotation?, (group | all | choice | sequence)?,
  1968. (attribute | attributeGroup)*, anyAttribute?
  1969. """
  1970. pass
  1971. class Restriction(_DerivationBase,\
  1972. MarkerInterface,\
  1973. RestrictionMarker):
  1974. """<restriction base>
  1975. parents:
  1976. complexContent
  1977. attributes:
  1978. id -- ID
  1979. base -- QName, required
  1980. contents:
  1981. annotation?, (group | all | choice | sequence)?,
  1982. (attribute | attributeGroup)*, anyAttribute?
  1983. """
  1984. pass
  1985. class SimpleContent(_DerivedType,\
  1986. MarkerInterface,\
  1987. SimpleMarker):
  1988. """<simpleContent>
  1989. parents:
  1990. complexType
  1991. attributes:
  1992. id -- ID
  1993. contents:
  1994. annotation?, (restriction | extension)
  1995. """
  1996. attributes = {'id':None}
  1997. contents = {'xsd':['annotation', 'restriction', 'extension']}
  1998. class Extension(XMLSchemaComponent,\
  1999. MarkerInterface,\
  2000. ExtensionMarker):
  2001. """<extension base>
  2002. parents:
  2003. simpleContent
  2004. attributes:
  2005. id -- ID
  2006. base -- QName, required
  2007. contents:
  2008. annotation?, (attribute | attributeGroup)*, anyAttribute?
  2009. """
  2010. required = ['base']
  2011. attributes = {'id':None,
  2012. 'base':None }
  2013. contents = {'xsd':['annotation', 'attribute', 'attributeGroup',
  2014. 'anyAttribute']}
  2015. def __init__(self, parent):
  2016. XMLSchemaComponent.__init__(self, parent)
  2017. self.annotation = None
  2018. self.attr_content = None
  2019. def fromDom(self, node):
  2020. self.setAttributes(node)
  2021. contents = self.getContents(node)
  2022. indx = 0
  2023. num = len(contents)
  2024. component = SplitQName(contents[indx].getTagName())[1]
  2025. if component == 'annotation':
  2026. self.annotation = Annotation(self)
  2027. self.annotation.fromDom(contents[indx])
  2028. indx += 1
  2029. component = SplitQName(contents[indx].getTagName())[1]
  2030. content = []
  2031. while indx < num:
  2032. component = SplitQName(contents[indx].getTagName())[1]
  2033. if component == 'attribute':
  2034. if contents[indx].hasattr('ref'):
  2035. content.append(AttributeReference(self))
  2036. else:
  2037. content.append(LocalAttributeDeclaration(self))
  2038. elif component == 'attributeGroup':
  2039. content.append(AttributeGroupReference(self))
  2040. elif component == 'anyAttribute':
  2041. content.append(AttributeWildCard(self))
  2042. else:
  2043. raise SchemaError, 'Unknown component (%s)'\
  2044. %(contents[indx].getTagName())
  2045. content[-1].fromDom(contents[indx])
  2046. indx += 1
  2047. self.attr_content = tuple(content)
  2048. class Restriction(XMLSchemaComponent,\
  2049. MarkerInterface,\
  2050. RestrictionMarker):
  2051. """<restriction base>
  2052. parents:
  2053. simpleContent
  2054. attributes:
  2055. id -- ID
  2056. base -- QName, required
  2057. contents:
  2058. annotation?, simpleType?, (enumeration | length |
  2059. maxExclusive | maxInclusive | maxLength | minExclusive |
  2060. minInclusive | minLength | pattern | fractionDigits |
  2061. totalDigits | whiteSpace)*, (attribute | attributeGroup)*,
  2062. anyAttribute?
  2063. """
  2064. required = ['base']
  2065. attributes = {'id':None,
  2066. 'base':None }
  2067. contents = {'xsd':['annotation', 'simpleType', 'attribute',\
  2068. 'attributeGroup', 'anyAttribute'] + RestrictionMarker.facets}
  2069. class LocalComplexType(ComplexType):
  2070. """<complexType>
  2071. parents:
  2072. element
  2073. attributes:
  2074. id -- ID
  2075. mixed -- boolean, false
  2076. contents:
  2077. annotation?, (simpleContent | complexContent |
  2078. ((group | all | choice | sequence)?, (attribute | attributeGroup)*, anyAttribute?))
  2079. """
  2080. required = []
  2081. attributes = {'id':None,
  2082. 'mixed':0}
  2083. class SimpleType(XMLSchemaComponent,\
  2084. MarkerInterface,\
  2085. DefinitionMarker,\
  2086. SimpleMarker):
  2087. """<simpleType name>
  2088. parents:
  2089. redefine, schema
  2090. attributes:
  2091. id -- ID
  2092. name -- NCName, required
  2093. final -- ('#all' | ('extension' | 'restriction' | 'list' | 'union')*),
  2094. schema.finalDefault
  2095. contents:
  2096. annotation?, (restriction | list | union)
  2097. """
  2098. required = ['name']
  2099. attributes = {'id':None,
  2100. 'name':None,
  2101. 'final':lambda self: self._parent().getFinalDefault()}
  2102. contents = {'xsd':['annotation', 'restriction', 'list', 'union']}
  2103. def __init__(self, parent):
  2104. XMLSchemaComponent.__init__(self, parent)
  2105. self.annotation = None
  2106. self.content = None
  2107. self.attr_content = None
  2108. def fromDom(self, node):
  2109. self.setAttributes(node)
  2110. contents = self.getContents(node)
  2111. for child in contents:
  2112. component = SplitQName(child.getTagName())[1]
  2113. if component == 'annotation':
  2114. self.annotation = Annotation(self)
  2115. self.annotation.fromDom(child)
  2116. break
  2117. else:
  2118. return
  2119. if component == 'restriction':
  2120. self.content = self.__class__.Restriction(self)
  2121. elif component == 'list':
  2122. self.content = self.__class__.List(self)
  2123. elif component == 'union':
  2124. self.content = self.__class__.Union(self)
  2125. else:
  2126. raise SchemaError, 'Unknown component (%s)' %(contents[indx].getTagName())
  2127. self.content.fromDom(child)
  2128. class Restriction(XMLSchemaComponent,\
  2129. MarkerInterface,\
  2130. RestrictionMarker):
  2131. """<restriction base>
  2132. parents:
  2133. simpleType
  2134. attributes:
  2135. id -- ID
  2136. base -- QName, required or simpleType child
  2137. contents:
  2138. annotation?, simpleType?, (enumeration | length |
  2139. maxExclusive | maxInclusive | maxLength | minExclusive |
  2140. minInclusive | minLength | pattern | fractionDigits |
  2141. totalDigits | whiteSpace)*
  2142. """
  2143. attributes = {'id':None,
  2144. 'base':None }
  2145. contents = {'xsd':['annotation', 'simpleType']+RestrictionMarker.facets}
  2146. def __init__(self, parent):
  2147. XMLSchemaComponent.__init__(self, parent)
  2148. self.annotation = None
  2149. self.content = None
  2150. self.attr_content = None
  2151. def fromDom(self, node):
  2152. self.setAttributes(node)
  2153. contents = self.getContents(node)
  2154. content = []
  2155. self.attr_content = []
  2156. for indx in range(len(contents)):
  2157. component = SplitQName(contents[indx].getTagName())[1]
  2158. if (component == 'annotation') and (not indx):
  2159. self.annotation = Annotation(self)
  2160. self.annotation.fromDom(contents[indx])
  2161. continue
  2162. elif (component == 'simpleType') and (not indx or indx == 1):
  2163. content.append(AnonymousSimpleType(self))
  2164. content[-1].fromDom(contents[indx])
  2165. elif component in RestrictionMarker.facets:
  2166. #print_debug('%s class instance, skipping %s' %(self.__class__, component))
  2167. pass
  2168. else:
  2169. raise SchemaError, 'Unknown component (%s)' %(i.getTagName())
  2170. self.content = tuple(content)
  2171. class Union(XMLSchemaComponent):
  2172. """<union>
  2173. parents:
  2174. simpleType
  2175. attributes:
  2176. id -- ID
  2177. memberTypes -- list of QNames, required or simpleType child.
  2178. contents:
  2179. annotation?, simpleType*
  2180. """
  2181. attributes = {'id':None,
  2182. 'memberTypes':None }
  2183. contents = {'xsd':['annotation', 'simpleType']}
  2184. def __init__(self, parent):
  2185. XMLSchemaComponent.__init__(self, parent)
  2186. self.annotation = None
  2187. self.content = None
  2188. self.attr_content = None
  2189. def fromDom(self, node):
  2190. self.setAttributes(node)
  2191. contents = self.getContents(node)
  2192. content = []
  2193. self.attr_content = []
  2194. for indx in range(len(contents)):
  2195. component = SplitQName(contents[indx].getTagName())[1]
  2196. if (component == 'annotation') and (not indx):
  2197. self.annotation = Annotation(self)
  2198. self.annotation.fromDom(contents[indx])
  2199. elif (component == 'simpleType'):
  2200. content.append(AnonymousSimpleType(self))
  2201. content[-1].fromDom(contents[indx])
  2202. else:
  2203. raise SchemaError, 'Unknown component (%s)' %(i.getTagName())
  2204. self.content = tuple(content)
  2205. class List(XMLSchemaComponent):
  2206. """<list>
  2207. parents:
  2208. simpleType
  2209. attributes:
  2210. id -- ID
  2211. itemType -- QName, required or simpleType child.
  2212. contents:
  2213. annotation?, simpleType?
  2214. """
  2215. attributes = {'id':None,
  2216. 'itemType':None }
  2217. contents = {'xsd':['annotation', 'simpleType']}
  2218. def __init__(self, parent):
  2219. XMLSchemaComponent.__init__(self, parent)
  2220. self.annotation = None
  2221. self.content = None
  2222. self.attr_content = None
  2223. def fromDom(self, node):
  2224. self.setAttributes(node)
  2225. contents = self.getContents(node)
  2226. self.content = []
  2227. self.attr_content = []
  2228. for indx in range(len(contents)):
  2229. component = SplitQName(contents[indx].getTagName())[1]
  2230. if (component == 'annotation') and (not indx):
  2231. self.annotation = Annotation(self)
  2232. self.annotation.fromDom(contents[indx])
  2233. elif (component == 'simpleType'):
  2234. self.content = AnonymousSimpleType(self)
  2235. self.content.fromDom(contents[indx])
  2236. break
  2237. else:
  2238. raise SchemaError, 'Unknown component (%s)' %(i.getTagName())
  2239. class AnonymousSimpleType(SimpleType,\
  2240. MarkerInterface,\
  2241. SimpleMarker):
  2242. """<simpleType>
  2243. parents:
  2244. attribute, element, list, restriction, union
  2245. attributes:
  2246. id -- ID
  2247. contents:
  2248. annotation?, (restriction | list | union)
  2249. """
  2250. required = []
  2251. attributes = {'id':None}
  2252. class Redefine:
  2253. """<redefine>
  2254. parents:
  2255. attributes:
  2256. contents:
  2257. """
  2258. pass
  2259. ###########################
  2260. ###########################
  2261. if sys.version_info[:2] >= (2, 2):
  2262. tupleClass = tuple
  2263. else:
  2264. import UserTuple
  2265. tupleClass = UserTuple.UserTuple
  2266. class TypeDescriptionComponent(tupleClass):
  2267. """Tuple of length 2, consisting of
  2268. a namespace and unprefixed name.
  2269. """
  2270. def __init__(self, args):
  2271. """args -- (namespace, name)
  2272. Remove the name's prefix, irrelevant.
  2273. """
  2274. if len(args) != 2:
  2275. raise TypeError, 'expecting tuple (namespace, name), got %s' %args
  2276. elif args[1].find(':') >= 0:
  2277. args = (args[0], SplitQName(args[1])[1])
  2278. tuple.__init__(self, args)
  2279. return
  2280. def getTargetNamespace(self):
  2281. return self[0]
  2282. def getName(self):
  2283. return self[1]
  2284. '''
  2285. import string, types, base64, re
  2286. from Utility import DOM, Collection
  2287. from StringIO import StringIO
  2288. class SchemaReader:
  2289. """A SchemaReader creates XMLSchema objects from urls and xml data."""
  2290. def loadFromStream(self, file):
  2291. """Return an XMLSchema instance loaded from a file object."""
  2292. document = DOM.loadDocument(file)
  2293. schema = XMLSchema()
  2294. schema.load(document)
  2295. return schema
  2296. def loadFromString(self, data):
  2297. """Return an XMLSchema instance loaded from an xml string."""
  2298. return self.loadFromStream(StringIO(data))
  2299. def loadFromURL(self, url):
  2300. """Return an XMLSchema instance loaded from the given url."""
  2301. document = DOM.loadFromURL(url)
  2302. schema = XMLSchema()
  2303. schema.location = url
  2304. schema.load(document)
  2305. return schema
  2306. def loadFromFile(self, filename):
  2307. """Return an XMLSchema instance loaded from the given file."""
  2308. file = open(filename, 'rb')
  2309. try: schema = self.loadFromStream(file)
  2310. finally: file.close()
  2311. return schema
  2312. class SchemaError(Exception):
  2313. pass
  2314. class XMLSchema:
  2315. # This is temporary, for the benefit of WSDL until the real thing works.
  2316. def __init__(self, element):
  2317. self.targetNamespace = DOM.getAttr(element, 'targetNamespace')
  2318. self.element = element
  2319. class realXMLSchema:
  2320. """A schema is a collection of schema components derived from one
  2321. or more schema documents, that is, one or more <schema> element
  2322. information items. It represents the abstract notion of a schema
  2323. rather than a single schema document (or other representation)."""
  2324. def __init__(self):
  2325. self.simpleTypes = Collection(self)
  2326. self.complexTypes = Collection(self)
  2327. self.attributes = Collection(self)
  2328. self.elements = Collection(self)
  2329. self.attrGroups = Collection(self)
  2330. self.idConstraints=None
  2331. self.modelGroups = None
  2332. self.notations = None
  2333. self.extensions = []
  2334. targetNamespace = None
  2335. attributeFormDefault = 'unqualified'
  2336. elementFormDefault = 'unqualified'
  2337. blockDefault = None
  2338. finalDefault = None
  2339. location = None
  2340. version = None
  2341. id = None
  2342. def load(self, document):
  2343. if document.nodeType == document.DOCUMENT_NODE:
  2344. schema = DOM.getElement(document, 'schema', None, None)
  2345. else:
  2346. schema = document
  2347. if schema is None:
  2348. raise SchemaError('Missing <schema> element.')
  2349. self.namespace = namespace = schema.namespaceURI
  2350. if not namespace in DOM.NS_XSD_ALL:
  2351. raise SchemaError(
  2352. 'Unknown XML schema namespace: %s.' % self.namespace
  2353. )
  2354. for attrname in (
  2355. 'targetNamespace', 'attributeFormDefault', 'elementFormDefault',
  2356. 'blockDefault', 'finalDefault', 'version', 'id'
  2357. ):
  2358. value = DOM.getAttr(schema, attrname, None, None)
  2359. if value is not None:
  2360. setattr(self, attrname, value)
  2361. # Resolve imports and includes here?
  2362. ## imported = {}
  2363. ## while 1:
  2364. ## imports = []
  2365. ## for element in DOM.getElements(definitions, 'import', NS_WSDL):
  2366. ## location = DOM.getAttr(element, 'location')
  2367. ## if not imported.has_key(location):
  2368. ## imports.append(element)
  2369. ## if not imports:
  2370. ## break
  2371. ## for element in imports:
  2372. ## self._import(document, element)
  2373. ## imported[location] = 1
  2374. for element in DOM.getElements(schema, None, None):
  2375. localName = element.localName
  2376. if not DOM.nsUriMatch(element.namespaceURI, namespace):
  2377. self.extensions.append(element)
  2378. continue
  2379. elif localName == 'message':
  2380. name = DOM.getAttr(element, 'name')
  2381. docs = GetDocumentation(element)
  2382. message = self.addMessage(name, docs)
  2383. parts = DOM.getElements(element, 'part', NS_WSDL)
  2384. message.load(parts)
  2385. continue
  2386. def _import(self, document, element):
  2387. namespace = DOM.getAttr(element, 'namespace', default=None)
  2388. location = DOM.getAttr(element, 'location', default=None)
  2389. if namespace is None or location is None:
  2390. raise WSDLError(
  2391. 'Invalid import element (missing namespace or location).'
  2392. )
  2393. # Sort-of support relative locations to simplify unit testing. The
  2394. # WSDL specification actually doesn't allow relative URLs, so its
  2395. # ok that this only works with urls relative to the initial document.
  2396. location = urllib.basejoin(self.location, location)
  2397. obimport = self.addImport(namespace, location)
  2398. obimport._loaded = 1
  2399. importdoc = DOM.loadFromURL(location)
  2400. try:
  2401. if location.find('#') > -1:
  2402. idref = location.split('#')[-1]
  2403. imported = DOM.getElementById(importdoc, idref)
  2404. else:
  2405. imported = importdoc.documentElement
  2406. if imported is None:
  2407. raise WSDLError(
  2408. 'Import target element not found for: %s' % location
  2409. )
  2410. imported_tns = DOM.getAttr(imported, 'targetNamespace')
  2411. importer_tns = namespace
  2412. if imported_tns != importer_tns:
  2413. return
  2414. if imported.localName == 'definitions':
  2415. imported_nodes = imported.childNodes
  2416. else:
  2417. imported_nodes = [imported]
  2418. parent = element.parentNode
  2419. for node in imported_nodes:
  2420. if node.nodeType != node.ELEMENT_NODE:
  2421. continue
  2422. child = DOM.importNode(document, node, 1)
  2423. parent.appendChild(child)
  2424. child.setAttribute('targetNamespace', importer_tns)
  2425. attrsNS = imported._attrsNS
  2426. for attrkey in attrsNS.keys():
  2427. if attrkey[0] == DOM.NS_XMLNS:
  2428. attr = attrsNS[attrkey].cloneNode(1)
  2429. child.setAttributeNode(attr)
  2430. finally:
  2431. importdoc.unlink()
  2432. class Element:
  2433. """Common base class for element representation classes."""
  2434. def __init__(self, name=None, documentation=''):
  2435. self.name = name
  2436. self.documentation = documentation
  2437. self.extensions = []
  2438. def addExtension(self, item):
  2439. self.extensions.append(item)
  2440. class SimpleTypeDefinition:
  2441. """Represents an xml schema simple type definition."""
  2442. class ComplexTypeDefinition:
  2443. """Represents an xml schema complex type definition."""
  2444. class AttributeDeclaration:
  2445. """Represents an xml schema attribute declaration."""
  2446. class ElementDeclaration:
  2447. """Represents an xml schema element declaration."""
  2448. def __init__(self, name, type=None, targetNamespace=None):
  2449. self.name = name
  2450. targetNamespace = None
  2451. annotation = None
  2452. nillable = 0
  2453. abstract = 0
  2454. default = None
  2455. fixed = None
  2456. scope = 'global'
  2457. type = None
  2458. form = 0
  2459. # Things we will not worry about for now.
  2460. id_constraint_defs = None
  2461. sub_group_exclude = None
  2462. sub_group_affils = None
  2463. disallowed_subs = None
  2464. class AttributeGroupDefinition:
  2465. """Represents an xml schema attribute group definition."""
  2466. class IdentityConstraintDefinition:
  2467. """Represents an xml schema identity constraint definition."""
  2468. class ModelGroupDefinition:
  2469. """Represents an xml schema model group definition."""
  2470. class NotationDeclaration:
  2471. """Represents an xml schema notation declaration."""
  2472. class Annotation:
  2473. """Represents an xml schema annotation."""
  2474. class ModelGroup:
  2475. """Represents an xml schema model group."""
  2476. class Particle:
  2477. """Represents an xml schema particle."""
  2478. class WildCard:
  2479. """Represents an xml schema wildcard."""
  2480. class AttributeUse:
  2481. """Represents an xml schema attribute use."""
  2482. class ElementComponent:
  2483. namespace = ''
  2484. name = ''
  2485. type = None
  2486. form = 'qualified | unqualified'
  2487. scope = 'global or complex def'
  2488. constraint = ('value', 'default | fixed')
  2489. nillable = 0
  2490. id_constraint_defs = None
  2491. sub_group_affil = None
  2492. sub_group_exclusions = None
  2493. disallowed_subs = 'substitution, extension, restriction'
  2494. abstract = 0
  2495. minOccurs = 1
  2496. maxOccurs = 1
  2497. ref = ''
  2498. class AttributeThing:
  2499. name = ''
  2500. namespace = ''
  2501. typeName = ''
  2502. typeUri = ''
  2503. scope = 'global | local to complex def'
  2504. constraint = ('value:default', 'value:fixed')
  2505. use = 'optional | prohibited | required'
  2506. class ElementDataType:
  2507. namespace = ''
  2508. name = ''
  2509. element_form = 'qualified | unqualified'
  2510. attr_form = None
  2511. type_name = ''
  2512. type_uri = ''
  2513. def __init__(self, name, namespace, type_name, type_uri):
  2514. self.namespace = namespace
  2515. self.name = name
  2516. # type may be anonymous...
  2517. self.type_name = type_name
  2518. self.type_uri = type_uri
  2519. def checkValue(self, value, context):
  2520. # Delegate value checking to the type of the element.
  2521. typeref = (self.type_uri, self.type_name)
  2522. handler = context.serializer.getType(typeref)
  2523. return handler.checkValue(value, context)
  2524. def serialize(self, name, namespace, value, context, **kwargs):
  2525. if context.check_values:
  2526. self.checkValue(value, context)
  2527. # Delegate serialization to the type of the element.
  2528. typeref = (self.type_uri, self.type_name)
  2529. handler = context.serializer.getType(typeref)
  2530. return handler.serialize(self.name, self.namespace, value, context)
  2531. def deserialize(self, element, context):
  2532. if element_is_null(element, context):
  2533. return None
  2534. # Delegate deserialization to the type of the element.
  2535. typeref = (self.type_uri, self.type_name)
  2536. handler = context.serializer.getType(typeref)
  2537. return handler.deserialize(element, context)
  2538. def parse_schema(data):
  2539. targetNS = ''
  2540. attributeFormDefault = 0
  2541. elementFormDefault = 0
  2542. blockDefault = ''
  2543. finalDefault = ''
  2544. language = None
  2545. version = None
  2546. id = ''
  2547. '''