示例#1
0
    def _finalize_schemalocs(self, schemaloc_dict=None):
        # If schemaloc_dict was passed in, make a copy so we don't mistakenly
        # modify the original.
        if schemaloc_dict:
            schemaloc_dict = dict(schemaloc_dict.iteritems())
        else:
            schemaloc_dict = {}

        # Get our id namespace
        id_ns = idgen.get_id_namespace()

        # Build our schemalocation dictionary!
        #
        # Initialize it from values found in the parsed, input schemalocations
        # (if there are any) and the schemaloc_dict parameter values (if there
        # are any).
        #
        # If there is a schemalocation found in both the parsed schemalocs and
        # the schema_loc dict, use the schemaloc_dict value.
        for ns, loc in self._input_schemalocs.iteritems():
            if ns in schemaloc_dict:
                continue
            schemaloc_dict[ns] = loc

        # Iterate over the finalized namespaces for a document and attempt
        # to map them to schemalocations. Warn if the namespace should have a
        # schemalocation and we can't find it anywhere.
        nsset = set(self.finalized_namespaces.itervalues())
        for ns in nsset:
            if ns in DEFAULT_STIX_SCHEMALOCATIONS:
                schemaloc_dict[ns] = DEFAULT_STIX_SCHEMALOCATIONS[ns]
            elif ns in schemaloc_dict:
                continue
            elif (ns == id_ns) or (ns in XML_NAMESPACES):
                continue
            else:
                error = "Unable to map namespace '{0}' to schemaLocation"
                warnings.warn(error.format(ns))

        return schemaloc_dict
    def _finalize_schemalocs(self, schemaloc_dict=None):
        # If schemaloc_dict was passed in, make a copy so we don't mistakenly
        # modify the original.
        if schemaloc_dict:
            schemaloc_dict = dict(schemaloc_dict.iteritems())
        else:
            schemaloc_dict = {}

        # Get our id namespace
        id_ns = idgen.get_id_namespace()

        # Build our schemalocation dictionary!
        #
        # Initialize it from values found in the parsed, input schemalocations
        # (if there are any) and the schemaloc_dict parameter values (if there
        # are any).
        #
        # If there is a schemalocation found in both the parsed schemalocs and
        # the schema_loc dict, use the schemaloc_dict value.
        for ns, loc in self._input_schemalocs.iteritems():
            if ns in schemaloc_dict:
                continue
            schemaloc_dict[ns] = loc

        # Iterate over the finalized namespaces for a document and attempt
        # to map them to schemalocations. Warn if the namespace should have a
        # schemalocation and we can't find it anywhere.
        nsset = set(self.finalized_namespaces.itervalues())
        for ns in nsset:
            if ns in DEFAULT_STIX_SCHEMALOCATIONS:
                schemaloc_dict[ns] = DEFAULT_STIX_SCHEMALOCATIONS[ns]
            elif ns in schemaloc_dict:
                continue
            elif (ns == id_ns) or (ns in XML_NAMESPACES):
                continue
            else:
                error = "Unable to map namespace '{0}' to schemaLocation"
                warnings.warn(error.format(ns))

        return schemaloc_dict
示例#3
0
    def _finalize_namespaces(self, ns_dict=None):
        """Returns a dictionary of namespaces to be exported with an XML
        document.

        This loops over all the namespaces that were discovered and built
        during the execution of ``collect()`` and
        ``_parse_collected_classes()`` and attempts to merge them all.

        Returns:
            An ``alias: namespace`` dictionary containing all namespaces
            required to be present on an exported document.

        Raises:
            .DuplicatePrefixError: If namespace prefix was mapped to more than
                one namespace.

        """
        if not ns_dict:
            ns_dict = {}

        # Copy and flip the input dictionary from ns=>alias to alias=>ns
        user_namespaces = {}
        for ns, alias in ns_dict.iteritems():
            user_namespaces[alias] = ns

        # Our return value
        ns_dict = collections.defaultdict(set)

        # Add the ID namespaces
        id_alias = idgen.get_id_namespace_alias()
        id_ns = idgen.get_id_namespace()
        ns_dict[id_alias].add(id_ns)

        # Build namespace dictionaries from the collected Entity objects.
        collected_prefixed = dict(self._collected_namespaces.iteritems())

        # Pop the unprefixed entries.
        no_prefix = collected_prefixed.pop(None, set())

        # Resolve namespace aliases for the unprefixed namespaces.
        collected_unprefixed = self._resolve_unprefixed(no_prefix)

        # Remap the example namespace to the one expected by the APIs if the
        # sample example namespace is found.
        self._fix_example_namespace()

        # All the namespaces dictionaries we need to merge and export.
        namespace_dicts = itertools.chain(
            self._BASELINE_NAMESPACES.iteritems(),
            self._input_namespaces.iteritems(), collected_prefixed.iteritems(),
            collected_unprefixed.iteritems(), user_namespaces.iteritems())

        # Build our merged namespace dictionary. It will be inspected for
        # duplicate ns prefix mappings.
        for alias, ns in namespace_dicts:
            ns_dict[alias].add(ns)

        # Check that all the prefixes are mapped to only one namespace
        self._check_namespaces(ns_dict)

        # Flatten the dictionary by popping the namespace from the namespace
        # set values in ns_dict.
        flattened = {}
        for alias, ns_set in ns_dict.iteritems():
            flattened[alias] = ns_set.pop()

        # Return the flattened dictionary
        return flattened
示例#4
0
 def test_get_id_namespace(self):
     self.assertEqual(idgen.get_id_namespace(), TEST_NS.name)
     self.assertEqual(idgen.get_id_namespace_prefix(), TEST_NS.prefix)
     self.assertEqual(idgen.get_id_namespace_alias(), TEST_NS.prefix)
示例#5
0
 def test_get_id_namespace(self):
     self.assertEqual(idgen.get_id_namespace(), TEST_NS.name)
     self.assertEqual(idgen.get_id_namespace_prefix(), TEST_NS.prefix)
     self.assertEqual(idgen.get_id_namespace_alias(), TEST_NS.prefix)
    def _finalize_namespaces(self, ns_dict=None):
        """Returns a dictionary of namespaces to be exported with an XML
        document.

        This loops over all the namespaces that were discovered and built
        during the execution of ``collect()`` and
        ``_parse_collected_classes()`` and attempts to merge them all.

        Returns:
            An ``alias: namespace`` dictionary containing all namespaces
            required to be present on an exported document.

        Raises:
            .DuplicatePrefixError: If namespace prefix was mapped to more than
                one namespace.

        """
        if not ns_dict:
            ns_dict = {}

        # Copy and flip the input dictionary from ns=>alias to alias=>ns
        user_namespaces = {}
        for ns, alias in ns_dict.iteritems():
            user_namespaces[alias] = ns

        # Our return value
        ns_dict = collections.defaultdict(set)

        # Add the ID namespaces
        id_alias = idgen.get_id_namespace_alias()
        id_ns = idgen.get_id_namespace()
        ns_dict[id_alias].add(id_ns)

        # Build namespace dictionaries from the collected Entity objects.
        collected_prefixed = dict(self._collected_namespaces.iteritems())

        # Pop the unprefixed entries.
        no_prefix = collected_prefixed.pop(None, set())

        # Resolve namespace aliases for the unprefixed namespaces.
        collected_unprefixed = self._resolve_unprefixed(no_prefix)

        # Remap the example namespace to the one expected by the APIs if the
        # sample example namespace is found.
        self._fix_example_namespace()

        # All the namespaces dictionaries we need to merge and export.
        namespace_dicts = itertools.chain(
            self._BASELINE_NAMESPACES.iteritems(),
            self._input_namespaces.iteritems(),
            collected_prefixed.iteritems(),
            collected_unprefixed.iteritems(),
            user_namespaces.iteritems()
        )

        # Build our merged namespace dictionary. It will be inspected for
        # duplicate ns prefix mappings.
        for alias, ns in namespace_dicts:
            ns_dict[alias].add(ns)

        # Check that all the prefixes are mapped to only one namespace
        self._check_namespaces(ns_dict)

        # Flatten the dictionary by popping the namespace from the namespace
        # set values in ns_dict.
        flattened = {}
        for alias, ns_set in ns_dict.iteritems():
            flattened[alias] = ns_set.pop()

        # Return the flattened dictionary
        return flattened