Example #1
0
    def __init__(self, nsmap: PrefixMap, shape: Shape, shape_interpreter):
        self._shape = shape
        self.containing_schema = shape_interpreter
        w_shape = PyxbWrapper(shape)
        self.tripleconstraints = self.depth_first_triples(w_shape)
        self.predmap = {}
        mp_num = 0
        while mp_num < len(self.tripleconstraints):
            k = nsmap.uri_for(self.tripleconstraints[mp_num].predicate)
            self.predmap.setdefault(k, [])
            self.predmap[k].append(str(mp_num) + ' ')
            mp_num += 1
        self._triple_number = 0
        self._pattern = r''.join(self.depth_first_pattern(w_shape))

        # Generate an additional match for every extra predicate
        #  Extra patterns are recognized by the leading '0
        for e in self._shape.extra:
            mp_num += 1
            ptn = '0' + str(mp_num) + ' '
            k = nsmap.uri_for(e.ref)
            self.predmap.setdefault(k, [])
            self.predmap[k].append(ptn)
            self._pattern += "(" + ptn + ")*"

        self.matchpattern = re.compile(self._pattern)
Example #2
0
 def shex_shape_constraint(self, sc: ShapeConstraint) -> dict:
     """ <code>xs:complexType name="ShapeConstraint"</code>
     :param sc: A complete shape constraint
     :return: S-JSON expression
     """
     rval = dict()
     sc_wrapper = PyxbWrapper(sc)
     for e in sc_wrapper.elements:
         entry = self.shex_expression_choice({}, e)
         if "expression" in entry:
             rval.setdefault("expressions", []).append(entry["expression"])
     self.shex_annotations_and_actions(rval, sc_wrapper)
     self.shex_cardinality(rval, sc_wrapper)
     return rval
Example #3
0
    def shex_triple_constraint(self, tc: TripleConstraint) -> dict:
        """ <code>xs:complexType name="TripleConstraint"</code>
        :param tc: TripleConstraint to process
        :return: SJson equivalent
        """
        assert not ((tc.objectConstraint or tc.object or tc.objectShape or tc.objectType) and
               (tc.subjectConstraint or tc.subject or tc.subjectShape or tc.subjectType)), \
            "Cannot mix subject and object constraints"

        tc_dict = dict(type="tripleConstraint",
                       predicate=self.shex_iri(tc.predicate))
        if tc.valueClass:
            tc_dict["valueClassRef"] = self.shex_value_class_label(
                tc.valueClass)
        else:
            vc_dict = dict(type="valueClass")
            if tc.objectConstraint:
                self.shex_triple_constraint_value_class(
                    vc_dict, tc.objectConstraint)
            if tc.object:
                vc_dict["values"] = [self.shex_iri(tc.object)]
            if tc.objectShape:
                vc_dict["reference"] = self.shex_shape_label(tc.objectShape)
            if tc.objectType:
                vc_dict["nodeKind"] = self.shex_node_type(tc.objectType)

            if tc.subjectConstraint or tc.subject or tc.subjectShape or tc.subjectType or tc.inverse:
                tc_dict["inverse"] = True
                if tc.subjectConstraint:
                    self.shex_triple_constraint_value_class(
                        vc_dict, tc.subjectConstraint)
            if tc.subject:
                vc_dict["values"] = [self.shex_iri(tc.subject)]
            if tc.subjectShape:
                vc_dict["reference"] = self.shex_shape_label(tc.subjectShape)
            if tc.subjectType:
                vc_dict["nodeKind"] = self.shex_node_type(tc.subjectType)

            if tc.datatype:
                vc_dict["datatype"] = self._uri(tc.datatype)
            if tc.negated:
                tc_dict["negated"] = tc.negated
            tc_wrapper = PyxbWrapper(tc)
            self.shex_annotations_and_actions(tc_dict, tc_wrapper)
            self.shex_cardinality(tc_dict, tc_wrapper)
            tc_dict["value"] = vc_dict
        return tc_dict
Example #4
0
    def shex_shape(self, shape: Shape) -> dict:
        """ <code>xs:complexType name="shape"</code>
        :param shape: XML Shape
        :return: S-JSON Shape Entry
        """
        rval = dict(type="shape")
        w_shape = PyxbWrapper(shape)
        self.shex_annotations_and_actions(rval, w_shape)
        [self.shex_expression_choice(rval, e) for e in w_shape.elements]
        for e in w_shape.elements:
            if e.type == "import_":
                rval.setdefault("inherit",
                                []).append(self.shex_shape_ref(e.value.node))
            elif e.type == "extra":
                rval.setdefault(e.type, []).append(self._uri(e.value.node.ref))

        # shape.label is the dictionary key in the Schema container
        if shape.virtual:
            rval["virtual"] = shape.virtual
        if shape.closed:
            rval["closed"] = shape.closed
        return rval
Example #5
0
 def shex_inline_value_class_definition(
         self, vc: dict, ivcd: InlineValueClassDefinition) -> list:
     """ <code>xs:complexType name="InlineValueClassDefinition"</code>
     :param vc: dictionary to record the actual elements
     :param ivcd:
     :return:
     """
     # valueClassLabel becomes the identity
     vcd_wrapper = PyxbWrapper(ivcd)
     for e in vcd_wrapper.elements:
         if e.type == "nodetype":
             vc["nodeKind"] = self.shex_node_type(e.value.node)
         elif e.type == "datatype":
             vc[e.type] = self._uri(e.value.node)
         elif e.type == "facet":
             self.shex_xs_facet(vc, e.value.node)
         elif e.type == "or_":
             vc["reference"] = self.shex_group_shape_constr(e.value.node)
         elif e.type == "valueSet":
             vc["values"] = self.shex_value_set(e.value.node)
         else:
             assert False, "Unknown ValueClassExpression choice entry: %s" % e.type
Example #6
0
 def _uri(self, element):
     """ Map element into a complete URI
     :param element: URI or QNAME
     :return: URI
     """
     return self._prefixmap.uri_for(PyxbWrapper.proc_unicode(element))
Example #7
0
 def shex_code_decl(cd: CodeDecl):
     """ <code>xs:complexType name="CodeDecl" mixed="true"</code>
     :param cd:
     :return:
     """
     return PyxbWrapper.mixed_content(cd)
Example #8
0
 def _uri(self, element):
     """ Map element into a complete URI
     :param element: URI or QNAME
     :return: URI
     """
     return self._prefixmap.uri_for(PyxbWrapper.proc_unicode(element))
Example #9
0
 def shex_code_decl(cd: CodeDecl):
     """ <code>xs:complexType name="CodeDecl" mixed="true"</code>
     :param cd:
     :return:
     """
     return PyxbWrapper.mixed_content(cd)