Exemplo n.º 1
0
 def create_profile_node(self, profile_element):
     """Creates the single root node of the tree of validators."""
     profile_node = conformance_validators.Node()
     for element in profile_element:
         if element.tag in PROFILE_NODE_TYPES:
             profile_node.add_child(self.create_node(element))
     return profile_node
Exemplo n.º 2
0
    def create_node(self, node_element):
        """Parses the node element to a conformance_validators.Node object.

    Args:
      node_element: ElementTree.Element object of a profile node type.

    Returns:
      A conformance_validators.Node object of the node element.
    """
        min_occurs = self.get_occurs_number(
            'minOccurs', node_element.attrib.get('minOccurs', '1'))
        max_occurs = self.get_max_occurs(node_element)
        is_sequence = node_element.tag == constants.XSD_TAG_PREFIX + 'sequence'
        is_choice = node_element.tag == constants.XSD_TAG_PREFIX + 'choice'
        node = conformance_validators.Node(min_occurs, max_occurs, is_sequence,
                                           is_choice)
        node_row_type = node_element.attrib.get('type', '')
        # Strip the DSRF prefix.
        if (node_row_type
                and not node_row_type.startswith(constants.DSRF_TYPE_PREFIX)):
            raise error.XsdParsingFailure(
                self.dsrf_xsd_file_name,
                'The element "%s" with type "%s" does not have the "%s" prefix. This '
                'is likely caused by the type of the parent element not being '
                'recognized as a valid row type. Please ensure that all row types in '
                'in the XSD start with the prefix "%s".' %
                (node_element.attrib.get('name'), node_row_type,
                 constants.DSRF_TYPE_PREFIX, constants.VALID_ROW_TYPE_PREFIX))
        node_row_type = node_row_type.split(':')[-1]
        if node_row_type:
            if constants.is_row_type(node_row_type):
                node.set_row_type(
                    node_row_type[len(constants.VALID_ROW_TYPE_PREFIX):])
                return node
            elif node_row_type in self.complex_elements:
                # All complex elements are sequences.
                node.is_sequence = True
                for element in self.complex_elements[node_row_type]:
                    if element.tag in PROFILE_NODE_TYPES:
                        node.add_child(self.create_node(element))
                return node
            else:
                raise error.XsdParsingFailure(
                    self.dsrf_xsd_file_name,
                    'The element "%s" with type "%s" does not exist in the dsrf xsd '
                    'file "%s".' % (node_element.attrib.get('name'),
                                    node_row_type, self.dsrf_xsd_file_name))
        if node_element.getchildren():
            for child in node_element:
                if child.tag in PROFILE_NODE_TYPES:
                    node.add_child(self.create_node(child))
        return node
Exemplo n.º 3
0
    def setUp(self):
        """Setting the nodes tree.

    Nodes structure:

     ugc profile
          |
        choice
        /    \
    sequence  C
      /\
     A  B
    """
        a = conformance_validators.Node(min_occurs=1,
                                        max_occurs=1,
                                        is_sequence=False,
                                        is_choice=False)
        a.set_row_type('a')
        b = conformance_validators.Node(min_occurs=1,
                                        max_occurs=1,
                                        is_sequence=False,
                                        is_choice=False)
        b.set_row_type('b')
        sequence = conformance_validators.Node(min_occurs=1,
                                               max_occurs=1,
                                               is_sequence=True,
                                               is_choice=False)
        sequence.add_child(a)
        sequence.add_child(b)
        c = conformance_validators.Node(min_occurs=1,
                                        max_occurs=1,
                                        is_sequence=False,
                                        is_choice=False)
        c.set_row_type('c')
        choice = conformance_validators.Node(min_occurs=1,
                                             max_occurs=float('inf'),
                                             is_sequence=False,
                                             is_choice=True)
        choice.add_child(sequence)
        choice.add_child(c)
        self.root = conformance_validators.Node(min_occurs=1,
                                                max_occurs=1,
                                                is_sequence=False,
                                                is_choice=False)
        self.root.add_child(choice)
    def test_process_block(self):
        r"""Constructs a node tree.

    In addition, reads an ASCII block, serializes and writes it to "stdout" so
    the conformance processor can read it.

    Nodes structure:

       profile root
            |
          choice
          /    \
      sequence  MW01
        / \
    RE01  AS02
    """
        re01 = conformance_validators.Node()
        re01.set_row_type('RE01')
        as02 = conformance_validators.Node()
        as02.set_row_type('AS02')
        sequence = conformance_validators.Node(is_sequence=True)
        sequence.add_child(re01)
        sequence.add_child(as02)
        mw01 = conformance_validators.Node()
        mw01.set_row_type('MW01')
        choice = conformance_validators.Node(max_occurs=float('inf'),
                                             is_choice=True)
        choice.add_child(sequence)
        choice.add_child(mw01)
        root = conformance_validators.Node()
        root.add_child(choice)
        conformance_block_processor = (
            conformance_processor.ConformanceBlockProcessor())
        conformance_block_processor.node = root
        # Verify that two rows were validated successfully.
        nr_rows_validated = conformance_block_processor.process_block(
            self.block_from_ascii(BODY_BLOCK))
        self.assertEquals(nr_rows_validated, 2)