Package pyxb :: Package namespace :: Module builtin
[hide private]
[frames] | no frames]

Source Code for Module pyxb.namespace.builtin

  1  # Copyright 2009, Peter A. Bigot 
  2  # 
  3  # Licensed under the Apache License, Version 2.0 (the "License"); you may 
  4  # not use this file except in compliance with the License. You may obtain a 
  5  # copy of the License at: 
  6  # 
  7  #            http://www.apache.org/licenses/LICENSE-2.0 
  8  # 
  9  # Unless required by applicable law or agreed to in writing, software 
 10  # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 
 11  # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 
 12  # License for the specific language governing permissions and limitations 
 13  # under the License. 
 14   
 15  """Classes and global objects related to built-in U{XML Namespaces<http://www.w3.org/TR/2006/REC-xml-names-20060816/index.html>}.""" 
 16   
 17  import pyxb 
 18   
 19  # A unique identifier for components that are built-in to the PyXB system 
 20  BuiltInObjectUID = pyxb.utils.utility.UniqueIdentifier('PyXB-' + pyxb.__version__ + '-Builtin') 
 21   
 22  from pyxb.namespace import Namespace 
 23   
24 -class _XMLSchema_instance (Namespace):
25 """Extension of L{Namespace} that pre-defines components available in the 26 XMLSchema Instance namespace.""" 27
28 - def _defineBuiltins_ox (self, structures_module):
29 """Ensure this namespace is ready for use. 30 31 Overrides base class implementation, since there is no schema 32 for this namespace. """ 33 34 assert structures_module is not None 35 schema = structures_module.Schema(namespace_context=self.initialNamespaceContext(), schema_location="URN:noLocation:PyXB:xsi", generation_uid=BuiltInObjectUID, _bypass_preload=True) 36 type = schema._addNamedComponent(structures_module.AttributeDeclaration.CreateBaseInstance('type', schema)) 37 nil = schema._addNamedComponent(structures_module.AttributeDeclaration.CreateBaseInstance('nil', schema)) 38 schema_location = schema._addNamedComponent(structures_module.AttributeDeclaration.CreateBaseInstance('schemaLocation', schema)) 39 no_namespace_schema_location = schema._addNamedComponent(structures_module.AttributeDeclaration.CreateBaseInstance('noNamespaceSchemaLocation', schema)) 40 return self
41
42 -class _XML (Namespace):
43 """Extension of L{Namespace} that pre-defines components available in the 44 XML (xml) namespace. Specifically those are the attribute declarations: 45 46 - C{xml:space} 47 - C{xml:lang} 48 - C{xml:base} 49 - C{xml:id} 50 51 the encompassing attribute group declaration: 52 53 - C{xml:specialAttrs} 54 55 and the anonymous types that support these.""" 56
57 - def _defineBuiltins_ox (self, structures_module):
58 """Ensure this namespace is ready for use. 59 60 Overrides base class implementation, since there is no schema 61 for this namespace. """ 62 63 assert structures_module is not None 64 import pyxb.binding.datatypes as xsd 65 import pyxb.binding.facets as xsdf 66 import archive 67 68 self.configureCategories([archive.NamespaceArchive._AnonymousCategory()]) 69 70 schema = structures_module.Schema(namespace_context=self.initialNamespaceContext(), schema_location="URN:noLocation:PyXB:XML", generation_uid=BuiltInObjectUID, _bypass_preload=True) 71 72 std_space = structures_module.SimpleTypeDefinition._CreateXMLInstance('space', schema) 73 std_space._setAnonymousName(self, anon_name='STD_ANON_space') 74 std_space._setBindingNamespace(self) 75 std_lang = structures_module.SimpleTypeDefinition._CreateXMLInstance('lang', schema) 76 std_lang._setAnonymousName(self, anon_name='STD_ANON_lang') 77 std_lang._setBindingNamespace(self) 78 79 base = schema._addNamedComponent(structures_module.AttributeDeclaration.CreateBaseInstance('base', schema, std=xsd.anyURI.SimpleTypeDefinition())) 80 id = schema._addNamedComponent(structures_module.AttributeDeclaration.CreateBaseInstance('id', schema, std=xsd.ID.SimpleTypeDefinition())) 81 space = schema._addNamedComponent(structures_module.AttributeDeclaration.CreateBaseInstance('space', schema, std=std_space)) 82 lang = schema._addNamedComponent(structures_module.AttributeDeclaration.CreateBaseInstance('lang', schema, std=std_lang)) 83 84 specialAttrs = schema._addNamedComponent(structures_module.AttributeGroupDefinition.CreateBaseInstance('specialAttrs', schema, [ 85 structures_module.AttributeUse.CreateBaseInstance(schema, space), 86 structures_module.AttributeUse.CreateBaseInstance(schema, base), 87 structures_module.AttributeUse.CreateBaseInstance(schema, lang), 88 structures_module.AttributeUse.CreateBaseInstance(schema, id), 89 ])) 90 91 return self
92
93 -class _XMLSchema (Namespace):
94 """Extension of L{Namespace} that pre-defines components available in the 95 XMLSchema namespace. 96 97 The types are defined when L{pyxb.xmlschema.structures} is imported. 98 """ 99
100 - def _defineBuiltins_ox (self, structures_module):
101 """Register the built-in types into the XMLSchema namespace.""" 102 103 # Defer the definitions to the structures module 104 assert structures_module is not None 105 structures_module._AddSimpleTypes(self) 106 107 # A little validation here 108 assert structures_module.ComplexTypeDefinition.UrTypeDefinition() == self.typeDefinitions()['anyType'] 109 assert structures_module.SimpleTypeDefinition.SimpleUrTypeDefinition() == self.typeDefinitions()['anySimpleType'] 110 111 # Provide access to the binding classes 112 self.configureCategories(['typeBinding', 'elementBinding']) 113 for ( en, td ) in self.typeDefinitions().items(): 114 if td.pythonSupport() is not None: 115 self.addCategoryObject('typeBinding', en, td.pythonSupport())
116 117 XMLSchema_instance = _XMLSchema_instance('http://www.w3.org/2001/XMLSchema-instance', 118 description='XML Schema Instance', 119 builtin_namespace='XMLSchema_instance') 120 """Namespace and URI for the XMLSchema Instance namespace. This is always 121 built-in, and does not (cannot) have an associated schema.""" 122 123 XMLNamespaces = Namespace('http://www.w3.org/2000/xmlns/', 124 description='Namespaces in XML', 125 builtin_namespace='XMLNamespaces', 126 bound_prefix='xmlns') 127 """Namespaces in XML. Not really a namespace, but is always available as C{xmlns}.""" 128 129 # http://www.w3.org/2001/XMLSchema.xsd 130 XMLSchema = _XMLSchema('http://www.w3.org/2001/XMLSchema', 131 description='XML Schema', 132 builtin_namespace='XMLSchema', 133 builtin_module_path='pyxb.binding.datatypes', 134 in_scope_namespaces = { 'xs' : None }) 135 """Namespace and URI for the XMLSchema namespace (often C{xs}, or C{xsd})""" 136 137 # http://www.w3.org/1999/xhtml.xsd 138 XHTML = Namespace('http://www.w3.org/1999/xhtml', 139 description='Family of document types that extend HTML', 140 builtin_namespace='XHTML', 141 default_namespace=XMLSchema) 142 """There really isn't a schema for this, but it's used as the default 143 namespace in the XML schema, so define it.""" 144 145 # http://www.w3.org/2001/xml.xsd 146 XML = _XML('http://www.w3.org/XML/1998/namespace', 147 description='XML namespace', 148 builtin_namespace='XML', 149 builtin_module_path='pyxb.binding.xml_', 150 is_undeclared_namespace=True, 151 bound_prefix='xml', 152 default_namespace=XHTML, 153 in_scope_namespaces = { 'xs' : XMLSchema }) 154 """Namespace and URI for XML itself (always available as C{xml})""" 155 156 # http://www.w3.org/2001/XMLSchema-hasFacetAndProperty 157 XMLSchema_hfp = Namespace('http://www.w3.org/2001/XMLSchema-hasFacetAndProperty', 158 description='Facets appearing in appinfo section', 159 builtin_namespace='XMLSchema_hfp', 160 default_namespace=XMLSchema, 161 in_scope_namespaces = { 'hfp' : None 162 , 'xhtml' : XHTML }) 163 """Elements appearing in appinfo elements to support data types.""" 164 165 # List of built-in namespaces. 166 BuiltInNamespaces = [ 167 XMLSchema_instance, 168 XMLSchema_hfp, 169 XMLSchema, 170 XMLNamespaces, 171 XML, 172 XHTML 173 ] 174 175 __InitializedBuiltinNamespaces = False 176
177 -def _InitializeBuiltinNamespaces (structures_module):
178 """Invoked at the end of the L{pyxb.xmlschema.structures} module to 179 initialize the component models of the built-in namespaces. 180 181 @param structures_module: The L{pyxb.xmlschema.structures} module may not 182 be importable by that name at the time this is invoked (because it is 183 still being processed), so it gets passed in as a parameter.""" 184 global __InitializedBuiltinNamespaces 185 if not __InitializedBuiltinNamespaces: 186 __InitializedBuiltinNamespaces = True 187 [ _ns._defineBuiltins(structures_module) for _ns in BuiltInNamespaces ]
188 189 # Set up the prefixes for xml, xmlns, etc. 190 _UndeclaredNamespaceMap = { } 191 [ _UndeclaredNamespaceMap.setdefault(_ns.boundPrefix(), _ns) for _ns in BuiltInNamespaces if _ns.isUndeclaredNamespace() ] 192