def _get_bad_ordinalities(self, nodes, tag, namespaces):
        """Returns a set of warnings for nodes in `nodes` that do not comply
        with @ordinality use of descriptive elements.

        Args:
            nodes: A set of nodes that have more than one instance of `tag`
                children.
            tag: The localname of the nodes to inspect for ordinalities.
            namespaces: A list of STIX namespaces.

        """
        def can_inspect(node):
            """Only check nodes that are in the STIX namespace and have a
            localname that matches the tag (e.g., 'Description').

            """
            qname = etree.QName(node)
            return (qname.localname == tag) and (qname.namespace in namespaces)


        filtered = []
        for node in nodes:
            # Filter out fields that belong to non-STIX namespaces
            filtered.extend(x for x in utils.iterchildren(node) if can_inspect(x))

        warns = []
        seen = set()
        
        for node in filtered:
            o = node.attrib.get('ordinality')

            if o is None:
                fmt = "@ordinality missing in '{0}' list."
                msg = fmt.format(tag)
                warns.append(BestPracticeWarning(node=node, message=msg))
                continue

            o = int(o)  # @ordinality is a xs:positiveInteger type.

            if o in seen:
                fmt = "@ordinality is duplicate in '{0}' list: '{1}'"
                msg = fmt.format(tag, o)
                warns.append(BestPracticeWarning(node=node, message=msg))
                continue

            seen.add(o)

        return warns
Пример #2
0
    def _get_bad_ordinalities(self, nodes, tag, namespaces):
        """Returns a set of warnings for nodes in `nodes` that do not comply
        with @ordinality use of descriptive elements.

        Args:
            nodes: A set of nodes that have more than one instance of `tag`
                children.
            tag: The localname of the nodes to inspect for ordinalities.
            namespaces: A list of STIX namespaces.

        """
        def can_inspect(node):
            """Only check nodes that are in the STIX namespace and have a
            localname that matches the tag (e.g., 'Description').

            """
            qname = etree.QName(node)
            return (qname.localname == tag) and (qname.namespace in namespaces)

        filtered = []
        for node in nodes:
            # Filter out fields that belong to non-STIX namespaces
            filtered.extend(x for x in utils.iterchildren(node)
                            if can_inspect(x))

        warns = []
        seen = set()

        for node in filtered:
            o = node.attrib.get('ordinality')

            if o is None:
                fmt = "@ordinality missing in '{0}' list."
                msg = fmt.format(tag)
                warns.append(BestPracticeWarning(node=node, message=msg))
                continue

            o = int(o)  # @ordinality is a xs:positiveInteger type.

            if o in seen:
                fmt = "@ordinality is duplicate in '{0}' list: '{1}'"
                msg = fmt.format(tag, o)
                warns.append(BestPracticeWarning(node=node, message=msg))
                continue

            seen.add(o)

        return warns
    def _check_titles(self, root, namespaces, selectors):
        """Checks that each node in `nodes` has a ``Title`` element unless
        there is an ``@idref`` attribute set.

        """
        results = BestPracticeWarningCollection("Missing Titles")
        xpath = " | ".join("//%s" % x for x in selectors)
        nodes = root.xpath(xpath, namespaces=namespaces)

        for node in nodes:
            if 'idref' in node.attrib:
                continue

            if not any(utils.localname(x) == 'Title' for x in utils.iterchildren(node)):
                warning = BestPracticeWarning(node=node)
                results.append(warning)

        return results
Пример #4
0
    def _check_titles(self, root, namespaces, selectors):
        """Checks that each node in `nodes` has a ``Title`` element unless
        there is an ``@idref`` attribute set.

        """
        results = BestPracticeWarningCollection("Missing Titles")
        xpath = " | ".join("//%s" % x for x in selectors)
        nodes = root.xpath(xpath, namespaces=namespaces)

        for node in nodes:
            if 'idref' in node.attrib:
                continue

            if not any(
                    utils.localname(x) == 'Title'
                    for x in utils.iterchildren(node)):
                warning = BestPracticeWarning(node=node)
                results.append(warning)

        return results