def set_about(node) : if has_one_of_attributes(node, "rel", "rev") : if not has_one_of_attributes(top, "about", "src") : node.setAttribute("about","") else : if not has_one_of_attributes(node, "href", "resource", "about", "src") : node.setAttribute("about","")
def top_about(root, options) : """ @param root: a DOM node for the top level element @param options: invocation options @type options: L{Options<pyRdfa.Options>} """ def set_about(node) : if has_one_of_attributes(node, "rel", "rev") : if not has_one_of_attributes(top, "about", "src") : node.setAttribute("about","") else : if not has_one_of_attributes(node, "href", "resource", "about", "src") : node.setAttribute("about","") from pyRdfa.host import HostLanguage from pyRdfa.utils import has_one_of_attributes if not has_one_of_attributes(root, "about") : root.setAttribute("about","") if options.host_language in [ HostLanguage.xhtml, HostLanguage.html ] : for top in root.getElementsByTagName("head") : if not has_one_of_attributes(top, "href", "resource", "about", "src") : set_about(top) for top in root.getElementsByTagName("body") : if not has_one_of_attributes(top, "href", "resource", "about", "src") : set_about(top)
def top_about(root, options): """ @param root: a DOM node for the top level element @param options: invocation options @type options: L{Options<pyRdfa.Options>} """ def set_about(node): if has_one_of_attributes(node, "rel", "rev"): if not has_one_of_attributes(top, "about", "src"): node.setAttribute("about", "") else: if not has_one_of_attributes(node, "href", "resource", "about", "src"): node.setAttribute("about", "") from pyRdfa.host import HostLanguage from pyRdfa.utils import has_one_of_attributes if not has_one_of_attributes(root, "about"): root.setAttribute("about", "") if options.host_language in [HostLanguage.xhtml, HostLanguage.html]: for top in root.getElementsByTagName("head"): if not has_one_of_attributes(top, "href", "resource", "about", "src"): set_about(top) for top in root.getElementsByTagName("body"): if not has_one_of_attributes(top, "href", "resource", "about", "src"): set_about(top)
def set_about(node): if has_one_of_attributes(node, "rel", "rev"): if not has_one_of_attributes(top, "about", "src"): node.setAttribute("about", "") else: if not has_one_of_attributes(node, "href", "resource", "about", "src"): node.setAttribute("about", "")
def generate_1_1(self) : """Generate the property object, 1.1 version""" ######################################################################### # See if the target is _not_ a literal irirefs = ("resource", "href", "src") noiri = ("content", "datatype", "rel", "rev") notypediri = ("content", "datatype", "rel", "rev", "about") if has_one_of_attributes(self.node, irirefs) and not has_one_of_attributes(self.node, noiri) : object = self.state.getResource(irirefs) elif has_one_of_attributes(self.node, "typeof") and not has_one_of_attributes(self.node, notypediri) and self.typed_resource != None : object = self.typed_resource else : # We have to generate a literal indeed. # Get, if exists, the value of @datatype datatype = '' dtset = False if self.node.hasAttribute("datatype") : dtset = True dt = self.node.getAttribute("datatype") if dt != "" : datatype = self.state.getURI("datatype") if self.state.lang != None : lang = self.state.lang else : lang = '' # The simple case: separate @content attribute if self.node.hasAttribute("content") : val = self.node.getAttribute("content") # Handling the automatic uri conversion case if dtset == False : object = Literal(val, lang=lang) else : object = self._create_Literal(val, datatype=datatype, lang=lang) # The value of datatype has been set, and the keyword paramaters take care of the rest else : # see if there *is* a datatype (even if it is empty!) if dtset : if datatype == XMLLiteral : object = Literal(self._get_XML_literal(self.node),datatype=XMLLiteral) else : object = self._create_Literal(self._get_literal(self.node), datatype=datatype, lang=lang) else : object = self._create_Literal(self._get_literal(self.node), lang=lang) if object != None : for prop in self.state.getURI("property") : if not isinstance(prop, BNode) : if self.state.rdfa_version >= "1.1" and self.node.hasAttribute("inlist") : self.state.add_to_list_mapping(prop, object) else : self.graph.add( (self.subject, prop, object) ) else : self.state.options.add_warning(no_blank_node % "property", warning_type=IncorrectBlankNodeUsage, node=self.node.nodeName)
def header_check(p_obj) : """Special disposition for the HTML <head> and <body> elements...""" if state.options.host_language in [ HostLanguage.xhtml, HostLanguage.html5, HostLanguage.xhtml5 ] : if node.nodeName == "head" or node.nodeName == "body" : if not has_one_of_attributes(node, "about", "resource", "src", "href") : return p_obj else : return None
def top_about(root, options, state): """ @param root: a DOM node for the top level element @param options: invocation options @type options: L{Options<pyRdfa.options>} @param state: top level execution state @type state: L{State<pyRdfa.state>} """ def set_about(node): if has_one_of_attributes(node, "rel", "rev"): if not has_one_of_attributes(top, "about", "src"): node.setAttribute("about", "") else: if not has_one_of_attributes(node, "href", "resource", "about", "src"): node.setAttribute("about", "") from pyRdfa.host import HostLanguage from pyRdfa.utils import has_one_of_attributes if not has_one_of_attributes(root, "about"): # The situation is a bit complicated: if a @resource is present without anything else, then it sets # the subject, ie, should be accepted... if has_one_of_attributes(root, "resource", "href", "src"): if has_one_of_attributes(root, "rel", "rev", "property"): root.setAttribute("about", "") else: root.setAttribute("about", "") if options.host_language in [ HostLanguage.xhtml, HostLanguage.html5, HostLanguage.xhtml5 ]: if state.rdfa_version >= "1.1": pass else: for top in root.getElementsByTagName("head"): if not has_one_of_attributes(top, "href", "resource", "about", "src"): set_about(top) for top in root.getElementsByTagName("body"): if not has_one_of_attributes(top, "href", "resource", "about", "src"): set_about(top)
def top_about(root, options, state) : """ @param root: a DOM node for the top level element @param options: invocation options @type options: L{Options<pyRdfa.options>} @param state: top level execution state @type state: L{State<pyRdfa.state>} """ def set_about(node) : if has_one_of_attributes(node, "rel", "rev") : if not has_one_of_attributes(top, "about", "src") : node.setAttribute("about","") else : if not has_one_of_attributes(node, "href", "resource", "about", "src") : node.setAttribute("about","") from pyRdfa.host import HostLanguage from pyRdfa.utils import has_one_of_attributes if not has_one_of_attributes(root, "about") : # The situation is a bit complicated: if a @resource is present without anything else, then it sets # the subject, ie, should be accepted... if has_one_of_attributes(root, "resource", "href", "src") : if has_one_of_attributes(root, "rel", "rev","property") : root.setAttribute("about","") else : root.setAttribute("about","") if options.host_language in [ HostLanguage.xhtml, HostLanguage.html5, HostLanguage.xhtml5 ] : if state.rdfa_version >= "1.1" : pass else : for top in root.getElementsByTagName("head") : if not has_one_of_attributes(top, "href", "resource", "about", "src") : set_about(top) for top in root.getElementsByTagName("body") : if not has_one_of_attributes(top, "href", "resource", "about", "src") : set_about(top)
def generate_1_1(self): """Generate the property object, 1.1 version""" ######################################################################### # See if the target is _not_ a literal irirefs = ("resource", "href", "src") noiri = ("content", "datatype", "rel", "rev") notypediri = ("content", "datatype", "rel", "rev", "about") if has_one_of_attributes( self.node, irirefs) and not has_one_of_attributes(self.node, noiri): object = self.state.getResource(irirefs) elif has_one_of_attributes( self.node, "typeof") and not has_one_of_attributes( self.node, notypediri) and self.typed_resource != None: object = self.typed_resource else: # We have to generate a literal indeed. # Get, if exists, the value of @datatype datatype = '' dtset = False if self.node.hasAttribute("datatype"): dtset = True dt = self.node.getAttribute("datatype") if dt != "": datatype = self.state.getURI("datatype") if self.state.lang != None: lang = self.state.lang else: lang = '' # The simple case: separate @content attribute if self.node.hasAttribute("content"): val = self.node.getAttribute("content") # Handling the automatic uri conversion case if dtset == False: object = Literal(val, lang=lang) else: object = self._create_Literal(val, datatype=datatype, lang=lang) # The value of datatype has been set, and the keyword paramaters take care of the rest else: # see if there *is* a datatype (even if it is empty!) if dtset: if datatype == XMLLiteral: object = Literal(self._get_XML_literal(self.node), datatype=XMLLiteral) else: object = self._create_Literal(self._get_literal( self.node), datatype=datatype, lang=lang) else: object = self._create_Literal(self._get_literal(self.node), lang=lang) if object != None: for prop in self.state.getURI("property"): if not isinstance(prop, BNode): if self.state.rdfa_version >= "1.1" and self.node.hasAttribute( "inlist"): self.state.add_to_list_mapping(prop, object) else: self.graph.add((self.subject, prop, object)) else: self.state.options.add_warning( no_blank_node % "property", warning_type=IncorrectBlankNodeUsage, node=self.node.nodeName)
def _parse_1_1(node, graph, parent_object, incoming_state, parent_incomplete_triples) : """The (recursive) step of handling a single node. See the U{RDFa syntax document<http://www.w3.org/TR/rdfa-syntax>} for further details. This is the RDFa 1.1 (and higher) version. @param node: the DOM node to handle @param graph: the RDF graph @type graph: RDFLib's Graph object instance @param parent_object: the parent's object, as an RDFLib URIRef @param incoming_state: the inherited state (namespaces, lang, etc) @type incoming_state: L{state.ExecutionContext} @param parent_incomplete_triples: list of hanging triples (the missing resource set to None) to be handled (or not) by the current node. @return: whether the caller has to complete it's parent's incomplete triples @rtype: Boolean """ # Update the state. This means, for example, the possible local settings of # namespaces and lang state = None state = ExecutionContext(node, graph, inherited_state=incoming_state) #--------------------------------------------------------------------------------- # Handle the special case for embedded RDF, eg, in SVG1.2. # This may add some triples to the target graph that does not originate from RDFa parsing # If the function return TRUE, that means that an rdf:RDF has been found. No # RDFa parsing should be done on that subtree, so we simply return... if state.options.host_language in accept_embedded_rdf and node.nodeType == node.ELEMENT_NODE and handle_embeddedRDF(node, graph, state) : return #--------------------------------------------------------------------------------- # calling the host language specific massaging of the DOM if state.options.host_language in host_dom_transforms and node.nodeType == node.ELEMENT_NODE : for func in host_dom_transforms[state.options.host_language] : func(node, state) #--------------------------------------------------------------------------------- # First, let us check whether there is anything to do at all. Ie, # whether there is any relevant RDFa specific attribute on the element # if not has_one_of_attributes(node, "href", "resource", "about", "property", "rel", "rev", "typeof", "src", "vocab", "prefix") : # nop, there is nothing to do here, just go down the tree and return... for n in node.childNodes : if n.nodeType == node.ELEMENT_NODE : parse_one_node(n, graph, parent_object, state, parent_incomplete_triples) return #----------------------------------------------------------------- # The goal is to establish the subject and object for local processing # The behaviour is slightly different depending on the presense or not # of the @rel/@rev attributes current_subject = None current_object = None typed_resource = None if has_one_of_attributes(node, "rel", "rev") : # in this case there is the notion of 'left' and 'right' of @rel/@rev # in establishing the new Subject and the objectResource # set first the subject if node.hasAttribute("about") : current_subject = state.getURI("about") if node.hasAttribute("typeof") : typed_resource = current_subject # get_URI may return None in case of an illegal CURIE, so # we have to be careful here, not use only an 'else' if current_subject == None : current_subject = parent_object else : state.reset_list_mapping(origin = current_subject) # set the object resource current_object = state.getResource("resource", "href", "src") if node.hasAttribute("typeof") and not node.hasAttribute("about") : if current_object == None : current_object = BNode() typed_resource = current_object if not node.hasAttribute("inlist") and current_object != None : # In this case the newly defined object is, in fact, the head of the list # just reset the whole thing. state.reset_list_mapping(origin = current_object) elif node.hasAttribute("property") and not has_one_of_attributes(node, "content", "datatype") : # this is the case when the property may take hold of @src and friends... if node.hasAttribute("about") : current_subject = state.getURI("about") if node.hasAttribute("typeof") : typed_resource = current_subject # get_URI_ref may return None in case of an illegal CURIE, so # we have to be careful here, not use only an 'else' if current_subject == None : current_subject = parent_object else : state.reset_list_mapping(origin = current_subject) if typed_resource == None and node.hasAttribute("typeof") : typed_resource = state.getResource("resource", "href", "src") if typed_resource == None : typed_resource = BNode() current_object = typed_resource else : current_object = current_subject else : # in this case all the various 'resource' setting attributes # behave identically, though they also have their own priority current_subject = state.getResource("about", "resource", "href", "src") # get_URI_ref may return None in case of an illegal CURIE, so # we have to be careful here, not use only an 'else' if current_subject == None : if node.hasAttribute("typeof") : current_subject = BNode() else : current_subject = parent_object else : state.reset_list_mapping(origin = current_subject) # in this case no non-literal triples will be generated, so the # only role of the current_object Resource is to be transferred to # the children node current_object = current_subject if node.hasAttribute("typeof") : typed_resource = current_subject # --------------------------------------------------------------------- ## The possible typeof indicates a number of type statements on the typed resource for defined_type in state.getURI("typeof") : if typed_resource : graph.add((typed_resource, ns_rdf["type"], defined_type)) # --------------------------------------------------------------------- # In case of @rel/@rev, either triples or incomplete triples are generated # the (possible) incomplete triples are collected, to be forwarded to the children incomplete_triples = [] for prop in state.getURI("rel") : if not isinstance(prop,BNode) : if node.hasAttribute("inlist") : if current_object != None : state.add_to_list_mapping(prop, current_object) else : incomplete_triples.append((None, prop, None)) else : theTriple = (current_subject, prop, current_object) if current_object != None : graph.add(theTriple) else : incomplete_triples.append(theTriple) else : state.options.add_warning(err_no_blank_node % "rel", warning_type=IncorrectBlankNodeUsage, node=node.nodeName) for prop in state.getURI("rev") : if not isinstance(prop,BNode) : theTriple = (current_object,prop,current_subject) if current_object != None : graph.add(theTriple) else : incomplete_triples.append(theTriple) else : state.options.add_warning(err_no_blank_node % "rev", warning_type=IncorrectBlankNodeUsage, node=node.nodeName) # ---------------------------------------------------------------------- # Generation of the literal values. The newSubject is the subject # A particularity of property is that it stops the parsing down the DOM tree if an XML Literal is generated, # because everything down there is part of the generated literal. if node.hasAttribute("property") : ProcessProperty(node, graph, current_subject, state, typed_resource).generate_1_1() # ---------------------------------------------------------------------- # Setting the current object to a bnode is setting up a possible resource # for the incomplete triples downwards if current_object == None : object_to_children = BNode() else : object_to_children = current_object #----------------------------------------------------------------------- # Here is the recursion step for all the children for n in node.childNodes : if n.nodeType == node.ELEMENT_NODE : _parse_1_1(n, graph, object_to_children, state, incomplete_triples) # --------------------------------------------------------------------- # At this point, the parent's incomplete triples may be completed for (s,p,o) in parent_incomplete_triples : if s == None and o == None : # This is an encoded version of a hanging rel for a collection: incoming_state.add_to_list_mapping( p, current_subject ) else : if s == None : s = current_subject if o == None : o = current_subject graph.add((s,p,o)) # Generate the lists, if any and if this is the level where a new list was originally created if state.new_list and not state.list_empty() : for prop in state.get_list_props() : vals = state.get_list_value(prop) heads = [ BNode() for r in vals ] + [ ns_rdf["nil"] ] for i in xrange(0, len(vals)) : graph.add( (heads[i], ns_rdf["first"], vals[i]) ) graph.add( (heads[i], ns_rdf["rest"], heads[i+1]) ) # Anchor the list graph.add( (state.get_list_origin(), prop, heads[0]) ) # ------------------------------------------------------------------- # This should be it... # ------------------------------------------------------------------- return
def generate_1_1(self) : """Generate the property object, 1.1 version""" ######################################################################### # See if the target is _not_ a literal irirefs = ("resource", "href", "src") noiri = ("content", "datatype", "rel", "rev") notypediri = ("content", "datatype", "rel", "rev", "about", "about_pruned") if has_one_of_attributes(self.node, irirefs) and not has_one_of_attributes(self.node, noiri) : # @href/@resource/@src takes the lead here... object = self.state.getResource(irirefs) elif self.node.hasAttribute("typeof") and not has_one_of_attributes(self.node, notypediri) and self.typed_resource != None : # a @typeof creates a special branch in case the typed resource was set during parsing object = self.typed_resource else : # We have to generate a literal # Get, if exists, the value of @datatype datatype = '' dtset = False if self.node.hasAttribute("datatype") : dtset = True dt = self.node.getAttribute("datatype") if dt != "" : datatype = self.state.getURI("datatype") # Supress lange is set in case some elements explicitly want to supress the effect of language # There were discussions, for example, that the <time> element should do so. Although, # after all, this was reversed, the functionality is kept in the code in case another # element might need it... if self.state.lang != None and self.state.supress_lang == False : lang = self.state.lang else : lang = '' # The simple case: separate @content attribute if self.node.hasAttribute("content") : val = self.node.getAttribute("content") # Handling the automatic uri conversion case if dtset == False : object = Literal(val, lang=lang) else : object = self._create_Literal(val, datatype=datatype, lang=lang) # The value of datatype has been set, and the keyword paramaters take care of the rest else : # see if there *is* a datatype (even if it is empty!) if dtset : if datatype == XMLLiteral : object = Literal(self._get_XML_literal(self.node), datatype=XMLLiteral) elif datatype == HTMLLiteral : object = Literal(self._get_HTML_literal(self.node), datatype=HTMLLiteral) else : object = self._create_Literal(self._get_literal(self.node), datatype=datatype, lang=lang) else : object = self._create_Literal(self._get_literal(self.node), lang=lang) if object != None : for prop in self.state.getURI("property") : if not isinstance(prop, BNode) : if self.node.hasAttribute("inlist") : self.state.add_to_list_mapping(prop, object) else : self.graph.add( (self.subject, prop, object) ) else : self.state.options.add_warning(err_no_blank_node % "property", warning_type=IncorrectBlankNodeUsage, node=self.node.nodeName)
def generate_1_1(self): """Generate the property object, 1.1 version""" ######################################################################### # See if the target is _not_ a literal irirefs = ("resource", "href", "src") noiri = ("content", "datatype", "rel", "rev") notypediri = ("content", "datatype", "rel", "rev", "about", "about_pruned") if has_one_of_attributes( self.node, irirefs) and not has_one_of_attributes(self.node, noiri): # @href/@resource/@src takes the lead here... object = self.state.getResource(irirefs) elif self.node.hasAttribute("typeof") and not has_one_of_attributes( self.node, notypediri) and self.typed_resource != None: # a @typeof creates a special branch in case the typed resource was set during parsing object = self.typed_resource else: # We have to generate a literal # Get, if exists, the value of @datatype datatype = '' dtset = False if self.node.hasAttribute("datatype"): dtset = True dt = self.node.getAttribute("datatype") if dt != "": datatype = self.state.getURI("datatype") # Supress lange is set in case some elements explicitly want to supress the effect of language # There were discussions, for example, that the <time> element should do so. Although, # after all, this was reversed, the functionality is kept in the code in case another # element might need it... if self.state.lang != None and self.state.supress_lang == False: lang = self.state.lang else: lang = '' # The simple case: separate @content attribute if self.node.hasAttribute("content"): val = self.node.getAttribute("content") # Handling the automatic uri conversion case if dtset == False: object = Literal(val, lang=lang) else: object = self._create_Literal(val, datatype=datatype, lang=lang) # The value of datatype has been set, and the keyword paramaters take care of the rest else: # see if there *is* a datatype (even if it is empty!) if dtset: if datatype == XMLLiteral: object = Literal(self._get_XML_literal(self.node), datatype=XMLLiteral) elif datatype == HTMLLiteral: object = Literal(self._get_HTML_literal(self.node), datatype=HTMLLiteral) else: object = self._create_Literal(self._get_literal( self.node), datatype=datatype, lang=lang) else: object = self._create_Literal(self._get_literal(self.node), lang=lang) if object != None: for prop in self.state.getURI("property"): if not isinstance(prop, BNode): if self.node.hasAttribute("inlist"): self.state.add_to_list_mapping(prop, object) else: self.graph.add((self.subject, prop, object)) else: self.state.options.add_warning( err_no_blank_node % "property", warning_type=IncorrectBlankNodeUsage, node=self.node.nodeName)
def _parse_1_1(node, graph, parent_object, incoming_state, parent_incomplete_triples): """The (recursive) step of handling a single node. See the U{RDFa syntax document<http://www.w3.org/TR/rdfa-syntax>} for further details. This is the RDFa 1.1 (and higher) version. @param node: the DOM node to handle @param graph: the RDF graph @type graph: RDFLib's Graph object instance @param parent_object: the parent's object, as an RDFLib URIRef @param incoming_state: the inherited state (namespaces, lang, etc) @type incoming_state: L{state.ExecutionContext} @param parent_incomplete_triples: list of hanging triples (the missing resource set to None) to be handled (or not) by the current node. @return: whether the caller has to complete it's parent's incomplete triples @rtype: Boolean """ # Update the state. This means, for example, the possible local settings of # namespaces and lang state = None state = ExecutionContext(node, graph, inherited_state=incoming_state) #--------------------------------------------------------------------------------- # Handle the special case for embedded RDF, eg, in SVG1.2. # This may add some triples to the target graph that does not originate from RDFa parsing # If the function return TRUE, that means that an rdf:RDF has been found. No # RDFa parsing should be done on that subtree, so we simply return... if state.options.host_language in accept_embedded_rdf and node.nodeType == node.ELEMENT_NODE and handle_embeddedRDF( node, graph, state): return #--------------------------------------------------------------------------------- # calling the host language specific massaging of the DOM if state.options.host_language in host_dom_transforms and node.nodeType == node.ELEMENT_NODE: for func in host_dom_transforms[state.options.host_language]: func(node, state) #--------------------------------------------------------------------------------- # First, let us check whether there is anything to do at all. Ie, # whether there is any relevant RDFa specific attribute on the element # if not has_one_of_attributes(node, "href", "resource", "about", "property", "rel", "rev", "typeof", "src", "vocab", "prefix"): # nop, there is nothing to do here, just go down the tree and return... for n in node.childNodes: if n.nodeType == node.ELEMENT_NODE: parse_one_node(n, graph, parent_object, state, parent_incomplete_triples) return #----------------------------------------------------------------- # The goal is to establish the subject and object for local processing # The behaviour is slightly different depending on the presense or not # of the @rel/@rev attributes current_subject = None current_object = None typed_resource = None if has_one_of_attributes(node, "rel", "rev"): # in this case there is the notion of 'left' and 'right' of @rel/@rev # in establishing the new Subject and the objectResource # set first the subject if node.hasAttribute("about"): current_subject = state.getURI("about") if node.hasAttribute("typeof"): typed_resource = current_subject # get_URI may return None in case of an illegal CURIE, so # we have to be careful here, not use only an 'else' if current_subject == None: current_subject = parent_object else: state.reset_list_mapping(origin=current_subject) # set the object resource current_object = state.getResource("resource", "href", "src") if node.hasAttribute("typeof") and not node.hasAttribute("about"): if current_object == None: current_object = BNode() typed_resource = current_object if not node.hasAttribute("inlist") and current_object != None: # In this case the newly defined object is, in fact, the head of the list # just reset the whole thing. state.reset_list_mapping(origin=current_object) elif node.hasAttribute("property") and not has_one_of_attributes( node, "content", "datatype"): # this is the case when the property may take hold of @src and friends... if node.hasAttribute("about"): current_subject = state.getURI("about") if node.hasAttribute("typeof"): typed_resource = current_subject # get_URI_ref may return None in case of an illegal CURIE, so # we have to be careful here, not use only an 'else' if current_subject == None: current_subject = parent_object else: state.reset_list_mapping(origin=current_subject) if typed_resource == None and node.hasAttribute("typeof"): typed_resource = state.getResource("resource", "href", "src") if typed_resource == None: typed_resource = BNode() current_object = typed_resource else: current_object = current_subject else: # in this case all the various 'resource' setting attributes # behave identically, though they also have their own priority current_subject = state.getResource("about", "resource", "href", "src") # get_URI_ref may return None in case of an illegal CURIE, so # we have to be careful here, not use only an 'else' if current_subject == None: if node.hasAttribute("typeof"): current_subject = BNode() else: current_subject = parent_object else: state.reset_list_mapping(origin=current_subject) # in this case no non-literal triples will be generated, so the # only role of the current_object Resource is to be transferred to # the children node current_object = current_subject if node.hasAttribute("typeof"): typed_resource = current_subject # --------------------------------------------------------------------- ## The possible typeof indicates a number of type statements on the typed resource for defined_type in state.getURI("typeof"): if typed_resource: graph.add((typed_resource, ns_rdf["type"], defined_type)) # --------------------------------------------------------------------- # In case of @rel/@rev, either triples or incomplete triples are generated # the (possible) incomplete triples are collected, to be forwarded to the children incomplete_triples = [] for prop in state.getURI("rel"): if not isinstance(prop, BNode): if node.hasAttribute("inlist"): if current_object != None: state.add_to_list_mapping(prop, current_object) else: incomplete_triples.append((None, prop, None)) else: theTriple = (current_subject, prop, current_object) if current_object != None: graph.add(theTriple) else: incomplete_triples.append(theTriple) else: state.options.add_warning(err_no_blank_node % "rel", warning_type=IncorrectBlankNodeUsage, node=node.nodeName) for prop in state.getURI("rev"): if not isinstance(prop, BNode): theTriple = (current_object, prop, current_subject) if current_object != None: graph.add(theTriple) else: incomplete_triples.append(theTriple) else: state.options.add_warning(err_no_blank_node % "rev", warning_type=IncorrectBlankNodeUsage, node=node.nodeName) # ---------------------------------------------------------------------- # Generation of the literal values. The newSubject is the subject # A particularity of property is that it stops the parsing down the DOM tree if an XML Literal is generated, # because everything down there is part of the generated literal. if node.hasAttribute("property"): ProcessProperty(node, graph, current_subject, state, typed_resource).generate_1_1() # ---------------------------------------------------------------------- # Setting the current object to a bnode is setting up a possible resource # for the incomplete triples downwards if current_object == None: object_to_children = BNode() else: object_to_children = current_object #----------------------------------------------------------------------- # Here is the recursion step for all the children for n in node.childNodes: if n.nodeType == node.ELEMENT_NODE: _parse_1_1(n, graph, object_to_children, state, incomplete_triples) # --------------------------------------------------------------------- # At this point, the parent's incomplete triples may be completed for (s, p, o) in parent_incomplete_triples: if s == None and o == None: # This is an encoded version of a hanging rel for a collection: incoming_state.add_to_list_mapping(p, current_subject) else: if s == None: s = current_subject if o == None: o = current_subject graph.add((s, p, o)) # Generate the lists, if any and if this is the level where a new list was originally created if state.new_list and not state.list_empty(): for prop in state.get_list_props(): vals = state.get_list_value(prop) heads = [BNode() for r in vals] + [ns_rdf["nil"]] for i in xrange(0, len(vals)): graph.add((heads[i], ns_rdf["first"], vals[i])) graph.add((heads[i], ns_rdf["rest"], heads[i + 1])) # Anchor the list graph.add((state.get_list_origin(), prop, heads[0])) # ------------------------------------------------------------------- # This should be it... # ------------------------------------------------------------------- return