Esempio n. 1
0
    def namespacePrefix(self, namespace, enable_default_namespace=True):
        """Return the prefix to be used for the given namespace.

        This will L{declare <declareNamespace>} the namespace if it has not
        yet been observed.

        @param namespace: The namespace for which a prefix is needed.  If the
        provided namespace is C{None} or an absent namespace, the C{None}
        value will be returned as the corresponding prefix.

        @keyword enable_default_namespace: Normally if the namespace is the default
        namespace C{None} is returned to indicate this.  If this keyword is
        C{False} then we need a namespace prefix even if this is the default.
        """

        if (namespace is None) or namespace.isAbsentNamespace():
            return None
        if isinstance(namespace, basestring):
            namespace = pyxb.namespace.NamespaceForURI(namespace,
                                                       create_if_missing=True)
        if (self.__defaultNamespace == namespace) and enable_default_namespace:
            return None
        ns = self.__namespaces.get(namespace)
        if ns is None:
            ns = self.declareNamespace(namespace)
        return ns
Esempio n. 2
0
    def namespacePrefix (self, namespace, enable_default_namespace=True):
        """Return the prefix to be used for the given namespace.

        This will L{declare <declareNamespace>} the namespace if it has not
        yet been observed.  It will also ensure the mapping from the returned
        prefix to C{namespace} is recorded for addition as an xmlns directive
        in the final document.

        @param namespace: The namespace for which a prefix is needed.  If the
        provided namespace is C{None} or an absent namespace, the C{None}
        value will be returned as the corresponding prefix.

        @keyword enable_default_namespace: Normally if the namespace is the default
        namespace C{None} is returned to indicate this.  If this keyword is
        C{False} then we need a namespace prefix even if this is the default.
        """
        if (namespace is None) or namespace.isAbsentNamespace():
            return None
        if isinstance(namespace, six.string_types):
            namespace = pyxb.namespace.NamespaceForURI(namespace, create_if_missing=True)
        if (self.defaultNamespace() == namespace) and enable_default_namespace:
            return None
        pfx = self.__namespaceContext.prefixForNamespace(namespace)
        if pfx is None:
            pfx = self.__namespaceContext.declareNamespace(namespace)
        self.__referencedNamespacePrefixes.add((namespace, pfx))
        return pfx
Esempio n. 3
0
    def namespacePrefix (self, namespace, enable_default_namespace=True):
        """Return the prefix to be used for the given namespace.

        This will L{declare <declareNamespace>} the namespace if it has not
        yet been observed.  It will also ensure the mapping from the returned
        prefix to C{namespace} is recorded for addition as an xmlns directive
        in the final document.

        @param namespace: The namespace for which a prefix is needed.  If the
        provided namespace is C{None} or an absent namespace, the C{None}
        value will be returned as the corresponding prefix.

        @keyword enable_default_namespace: Normally if the namespace is the default
        namespace C{None} is returned to indicate this.  If this keyword is
        C{False} then we need a namespace prefix even if this is the default.
        """
        if (namespace is None) or namespace.isAbsentNamespace():
            return None
        if isinstance(namespace, six.string_types):
            namespace = pyxb.namespace.NamespaceForURI(namespace, create_if_missing=True)
        if (self.defaultNamespace() == namespace) and enable_default_namespace:
            return None
        pfx = self.__namespaceContext.prefixForNamespace(namespace)
        if pfx is None:
            pfx = self.__namespaceContext.declareNamespace(namespace)
        self.__referencedNamespacePrefixes.add((namespace, pfx))
        return pfx
Esempio n. 4
0
    def addXMLNSDeclaration(self, element, namespace, prefix=None):
        """Manually add an XMLNS declaration to the document element.

        @param namespace: a L{pyxb.namespace.Namespace} instance

        @param prefix: the prefix by which the namespace is known.  If
        C{None}, the default prefix as previously declared will be used; if
        C{''} (empty string) a declaration for C{namespace} as the default
        namespace will be generated.

        @return: C{prefix} as used in the added declaration.
        """
        if not isinstance(namespace, pyxb.namespace.Namespace):
            raise pyxb.UsageError(
                'addXMLNSdeclaration: must be given a namespace instance')
        if namespace.isAbsentNamespace():
            raise pyxb.UsageError(
                'addXMLNSdeclaration: namespace must not be an absent namespace'
            )
        if prefix is None:
            prefix = self.namespacePrefix(namespace)
        if not prefix:  # None or empty string
            an = 'xmlns'
        else:
            an = 'xmlns:' + prefix
        element.setAttributeNS(pyxb.namespace.XMLNamespaces.uri(), an,
                               namespace.uri())
        return prefix
Esempio n. 5
0
    def namespacePrefix (self, namespace, enable_default_namespace=True):
        """Return the prefix to be used for the given namespace.

        This will L{declare <declareNamespace>} the namespace if it has not
        yet been observed.

        @param namespace: The namespace for which a prefix is needed.  If the
        provided namespace is C{None} or an absent namespace, the C{None}
        value will be returned as the corresponding prefix.

        @keyword enable_default_namespace: Normally if the namespace is the default
        namespace C{None} is returned to indicate this.  If this keyword is
        C{False} then we need a namespace prefix even if this is the default.
        """

        if (namespace is None) or namespace.isAbsentNamespace():
            return None
        if isinstance(namespace, basestring):
            namespace = pyxb.namespace.NamespaceForURI(namespace, create_if_missing=True)
        if (self.__defaultNamespace == namespace) and enable_default_namespace:
            return None
        ns = self.__namespaces.get(namespace)
        if ns is None:
            ns = self.declareNamespace(namespace)
        return ns
Esempio n. 6
0
    def namespacePrefix(self, namespace):
        """Return the prefix to be used for the given namespace.
        
        This will L{declare <declareNamespace>} the namespace if it has not
        yet been observed.

        @param namespace: The namespace for which a prefix is needed.  If the
        provided namespace is C{None} or an absent namespace, the C{None}
        value will be returned as the corresponding prefix.
        """

        if (namespace is None) or namespace.isAbsentNamespace():
            return None
        if isinstance(namespace, basestring):
            namespace = pyxb.namespace.NamespaceForURI(namespace, create_if_missing=True)
        if not (namespace in self.__namespaces):
            return self.declareNamespace(namespace)
        return self.__namespaces[namespace]
Esempio n. 7
0
    def namespacePrefix (self, namespace):
        """Return the prefix to be used for the given namespace.
        
        This will L{declare <declareNamespace>} the namespace if it has not
        yet been observed.

        @param namespace: The namespace for which a prefix is needed.  If the
        provided namespace is C{None} or an absent namespace, the C{None}
        value will be returned as the corresponding prefix.
        """
        
        if (namespace is None) or namespace.isAbsentNamespace():
            return None
        if isinstance(namespace, basestring):
            namespace = pyxb.namespace.NamespaceForURI(namespace, create_if_missing=True)
        if not (namespace in self.__namespaces):
            return self.declareNamespace(namespace)
        return self.__namespaces[namespace]
Esempio n. 8
0
    def declareNamespace(self, namespace, prefix=None, add_to_map=False):
        """Add the given namespace as one to be used in this document.

        @param namespace: The namespace to be associated with the document.
        @type namespace: L{pyxb.namespace.Namespace}

        @keyword prefix: Optional prefix to be used with this namespace.  If
        not provided, a unique prefix is generated or a standard prefix is
        used, depending on the namespace.

        @keyword add_to_map: If C{False} (default), the prefix is not added to
        the namespace prefix map.  If C{True} it is added.  (Often, things
        added to the prefix map are preserved across resets, which is often
        not desired for specific prefix/namespace pairs).

        @todo: ensure multiple namespaces do not share the same prefix
        @todo: provide default prefix in L{pyxb.namespace.Namespace}
        @todo: support multiple prefixes for each namespace
        """
        if not isinstance(namespace, pyxb.namespace.Namespace):
            raise pyxb.UsageError(
                'declareNamespace: must be given a namespace instance')
        if namespace.isAbsentNamespace():
            raise pyxb.UsageError(
                'declareNamespace: namespace must not be an absent namespace')
        if prefix is None:
            prefix = self.__namespaces.get(namespace)
        if prefix is None:
            prefix = self.__namespacePrefixMap.get(namespace)
        if prefix is None:
            prefix = namespace.prefix()
        if prefix is None:
            self.__namespacePrefixCounter += 1
            prefix = 'ns%d' % (self.__namespacePrefixCounter, )
        if prefix == self.__namespaces.get(namespace):
            return prefix
        if prefix in self.__prefixes:
            raise pyxb.LogicError('Prefix %s is already in use' % (prefix, ))
        self.__namespaces[namespace] = prefix
        self.__prefixes.add(prefix)
        if add_to_map:
            self.__namespacePrefixMap[namespace] = prefix
        return prefix
Esempio n. 9
0
    def declareNamespace (self, namespace, prefix=None, add_to_map=False):
        """Add the given namespace as one to be used in this document.

        @param namespace: The namespace to be associated with the document.
        @type namespace: L{pyxb.namespace.Namespace}

        @keyword prefix: Optional prefix to be used with this namespace.  If
        not provided, a unique prefix is generated or a standard prefix is
        used, depending on the namespace.

        @keyword add_to_map: If C{False} (default), the prefix is not added to
        the namespace prefix map.  If C{True} it is added.  (Often, things
        added to the prefix map are preserved across resets, which is often
        not desired for specific prefix/namespace pairs).

        @todo: ensure multiple namespaces do not share the same prefix
        @todo: provide default prefix in L{pyxb.namespace.Namespace}
        @todo: support multiple prefixes for each namespace
        """
        if not isinstance(namespace, pyxb.namespace.Namespace):
            raise pyxb.UsageError('declareNamespace: must be given a namespace instance')
        if namespace.isAbsentNamespace():
            raise pyxb.UsageError('declareNamespace: namespace must not be an absent namespace')
        if prefix is None:
            prefix = self.__namespaces.get(namespace)
        if prefix is None:
            prefix = self.__namespacePrefixMap.get(namespace)
        if prefix is None:
            prefix = namespace.prefix()
        if prefix is None:
            self.__namespacePrefixCounter += 1
            prefix = 'ns%d' % (self.__namespacePrefixCounter,)
        if prefix == self.__namespaces.get(namespace):
            return prefix
        if prefix in self.__prefixes:
            raise pyxb.LogicError('Prefix %s is already in use' % (prefix,))
        self.__namespaces[namespace] = prefix
        self.__prefixes.add(prefix)
        #_log.debug('%x declared namespace %s as %s', id(self), namespace, prefix)
        if add_to_map:
            self.__namespacePrefixMap[namespace] = prefix
        return prefix
Esempio n. 10
0
    def addXMLNSDeclaration (self, element, namespace, prefix=None):
        """Manually add an XMLNS declaration to the document element.

        @param namespace: a L{pyxb.namespace.Namespace} instance

        @param prefix: the prefix by which the namespace is known.  If
        C{None}, the default prefix as previously declared will be used; if
        C{''} (empty string) a declaration for C{namespace} as the default
        namespace will be generated.

        @return: C{prefix} as used in the added declaration.
        """
        if not isinstance(namespace, pyxb.namespace.Namespace):
            raise pyxb.UsageError('addXMLNSdeclaration: must be given a namespace instance')
        if namespace.isAbsentNamespace():
            raise pyxb.UsageError('addXMLNSdeclaration: namespace must not be an absent namespace')
        if prefix is None:
            prefix = self.namespacePrefix(namespace)
        if not prefix: # None or empty string
            an = 'xmlns'
        else:
            an = 'xmlns:' + prefix
        element.setAttributeNS(pyxb.namespace.XMLNamespaces.uri(), an, namespace.uri())
        return prefix