def setPrefix (self, prefix): if self.__boundPrefix is not None: if self.__boundPrefix == prefix: return self raise pyxb.NamespaceError(self, 'Cannot change the prefix of a bound namespace') self.__prefix = prefix return self
def processXMLNS(self, prefix, uri): if not self.__mutableInScopeNamespaces: self.__inScopeNamespaces = self.__inScopeNamespaces.copy() self.__mutableInScopeNamespaces = True if uri: if prefix is None: ns = self.__defaultNamespace = utility.NamespaceForURI( uri, create_if_missing=True) self.__inScopeNamespaces[None] = self.__defaultNamespace else: ns = utility.NamespaceForURI(uri, create_if_missing=True) self.__inScopeNamespaces[prefix] = ns #if ns.prefix() is None: # ns.setPrefix(prefix) # @todo should we record prefix in namespace so we can use it # during generation? I'd rather make the user specify what to # use. if self.__targetNamespace: self.__targetNamespace._referenceNamespace(ns) else: self.__pendingReferencedNamespaces.add(ns) else: # NB: XMLNS 6.2 says that you can undefine a default # namespace, but does not say anything explicitly about # undefining a prefixed namespace. XML-Infoset 2.2 # paragraph 6 implies you can do this, but expat blows up # if you try it. I don't think it's legal. if prefix is not None: raise pyxb.NamespaceError( self, 'Attempt to undefine non-default namespace %s' % (attr.localName, )) self.__inScopeNamespaces.pop(prefix, None) self.__defaultNamespace = None
def categoryMap(self, category): """Map from local names to NamedObjectMap instances for the given category.""" try: return self.__categoryMap[category] except KeyError: raise pyxb.NamespaceError( self, '%s has no category %s' % (self, category))
def markNotLoadable (self): """Prevent loading this namespace from an archive. This marks all archives in which the namespace appears, whether publically or privately, as not loadable.""" if self._loadedFromArchive(): raise pyxb.NamespaceError(self, 'cannot mark not loadable when already loaded') for mr in self.moduleRecords(): mr._setIsLoadable(False)
def setPrefix(self, prefix): if self.__boundPrefix is not None: if self.__boundPrefix == prefix: return self raise pyxb.NamespaceError( self, 'Cannot change the prefix of a bound namespace') if (None is not prefix) and (0 == len(prefix)): raise pyxb.UsageError('prefix must be non-empty string') self.__prefix = prefix return self
def _loadNamedObjects (self, category_map): """Add the named objects from the given map into the set held by this namespace. It is an error to name something which is already present.""" self.configureCategories(six.iterkeys(category_map)) for category in six.iterkeys(category_map): current_map = self.categoryMap(category) new_map = category_map[category] for (local_name, component) in six.iteritems(new_map): existing_component = current_map.get(local_name) if existing_component is None: current_map[local_name] = component elif existing_component._allowUpdateFromOther(component): existing_component._updateFromOther(component) else: raise pyxb.NamespaceError(self, 'Load attempted to override %s %s in %s' % (category, local_name, self.uri())) self.__defineCategoryAccessors()
def _loadCategoryObjects (self, category_objects): assert self.__categoryObjects is None assert not self.__constructedLocally ns = self.namespace() ns.configureCategories(category_objects.iterkeys()) for (cat, obj_map) in category_objects.iteritems(): current_map = ns.categoryMap(cat) for (local_name, component) in obj_map.iteritems(): existing_component = current_map.get(local_name) if existing_component is None: current_map[local_name] = component elif existing_component._allowUpdateFromOther(component): existing_component._updateFromOther(component) else: raise pyxb.NamespaceError(self, 'Load attempted to override %s %s in %s' % (cat, local_name, self.namespace())) self.markIncorporated()
def processXMLNS(self, prefix, uri): from pyxb.namespace import builtin if not self.__mutableInScopeNamespaces: self.__clonePrefixMap() self.__mutableInScopeNamespaces = True if builtin.XML.boundPrefix() == prefix: # Bound prefix xml is permitted if it's bound to the right URI, or # if the scope is being left. In neither case is the mapping # adjusted. if (uri is None) or builtin.XML.uri() == uri: return raise pyxb.LogicError('Cannot manipulate bound prefix xml') if uri: if prefix is None: ns = self.__defaultNamespace = utility.NamespaceForURI( uri, create_if_missing=True) self.__inScopeNamespaces[None] = self.__defaultNamespace else: ns = utility.NamespaceForURI(uri, create_if_missing=True) self.__removePrefixMap(prefix) self.__addPrefixMap(prefix, ns) if self.__targetNamespace: self.__targetNamespace._referenceNamespace(ns) else: self.__pendingReferencedNamespaces.add(ns) else: # NB: XMLNS 6.2 says that you can undefine a default # namespace, but does not say anything explicitly about # undefining a prefixed namespace. XML-Infoset 2.2 # paragraph 6 implies you can do this, but expat blows up # if you try it. I don't think it's legal. if prefix is not None: raise pyxb.NamespaceError( self, 'Attempt to undefine non-default namespace %s' % (prefix, )) self.__removePrefixMap(prefix) self.__defaultNamespace = None