Пример #1
0
	def generate_predicate_URI( self, name, context ) :
		"""
		Generate a full URI for a predicate, using the type, the vocabulary, etc.
		
		For details of this entry, see Section 4.4
		@param name: name of the property, ie, what appears in @itemprop
		@param context: an instance of an evaluation context
		@type context: L{Evaluation_Context}
		"""
		if debug: print "name: %s, %s" % (name,context)
		
		# Step 1: absolute URI-s are fine, take them as they are
		if is_absolute_URI(name) : return name
		
		# Step 2: if type is none, that this is just used as a fragment
		# if not context.current_type  :
		if context.current_type == None and context.current_vocabulary == None  :
			return generate_URI( self.base, name )

		#if context.current_type == None :
		#	return generate_URI( self.base, name )
		
		# Step 3: set the scheme
		try :
			if context.current_vocabulary in registry and "propertyURI" in registry[context.current_vocabulary] :
				scheme = registry[context.current_vocabulary]["propertyURI"]
			else :
				scheme = PropertySchemes.vocabulary
		except :
			# This is when the structure of the registry is broken
			scheme = PropertySchemes.vocabulary
			
		name = fragment_escape( name )
		if scheme == PropertySchemes.contextual :
			# Step 5.1
			s = context.current_name
			# s = context.current_type
			if s != None and s.startswith("http://www.w3.org/ns/md?type=") :
				# Step 5.2
				return s + '.' + name
			else :
				# Step 5.3
				return "http://www.w3.org/ns/md?type=" + fragment_escape(context.current_type) + "&prop=" + name
		else :
			# Step 4
			if context.current_vocabulary[-1] == '#' or context.current_vocabulary[-1] == '/' :
				return context.current_vocabulary + name
			else :
				return context.current_vocabulary + '#' + name
Пример #2
0
	def generate_predicate_URI( self, name, context ) :
		"""
		Generate a full URI for a predicate, using the type, the vocabulary, etc.
		
		For details of this entry, see Section 4.4
		@param name: name of the property, ie, what appears in @itemprop
		@param context: an instance of an evaluation context
		@type context: L{Evaluation_Context}
		"""
		if debug: print( "name: %s, %s" % (name,context) )
		
		# Step 1: absolute URI-s are fine, take them as they are
		if is_absolute_URI(name) : return name
		
		# Step 2: if type is none, that this is just used as a fragment
		# if not context.current_type  :
		if context.current_type == None and context.current_vocabulary == None  :
			if self.base[-1] == '#' :
				b = self.base[:-1]
			else :
				b = self.base
			return b + '#' + fragment_escape(name)

		#if context.current_type == None :
		#	return generate_URI( self.base, name )
		
		# Step 3: set the scheme
		try :
			if context.current_vocabulary in registry and "propertyURI" in registry[context.current_vocabulary] :
				scheme = registry[context.current_vocabulary]["propertyURI"]
			else :
				scheme = PropertySchemes.vocabulary
		except :
			# This is when the structure of the registry is broken
			scheme = PropertySchemes.vocabulary
			
		name = fragment_escape( name )
		if scheme == PropertySchemes.contextual :
			# Step 5.1
			s = context.current_name
			# s = context.current_type
			if s != None and s.startswith("http://www.w3.org/ns/md?type=") :
				# Step 5.2
				expandedURI = s + '.' + name
			else :
				# Step 5.3
				expandedURI =  "http://www.w3.org/ns/md?type=" + fragment_escape(context.current_type) + "&prop=" + name
		else :
			# Step 4
			if context.current_vocabulary[-1] == '#' or context.current_vocabulary[-1] == '/' :
				expandedURI =  context.current_vocabulary + name
			else :
				expandedURI =  context.current_vocabulary + '#' + name

		# see if there are subproperty/equivalentproperty relations
		try :
			vocab_mapping = registry[context.current_vocabulary]["properties"][name]
			# if we got that far, we may have some mappings

			expandedURIRef = URIRef(expandedURI)
			try :
				subpr = vocab_mapping["subPropertyOf"]
				if subpr != None :
					if isinstance(subpr,list) :
						for p in subpr :
							self.graph.add( (expandedURIRef, ns_rdfs["subPropertyOf"], URIRef(p)) )
					else :
						self.graph.add( (expandedURIRef, ns_rdfs["subPropertyOf"], URIRef(subpr)) )
			except :
				# Ok, no sub property
				pass
			try :
				subpr = vocab_mapping["equivalentProperty"]
				if subpr != None :
					if isinstance(subpr,list) :
						for p in subpr :
							self.graph.add( (expandedURIRef, ns_owl["equivalentProperty"], URIRef(p)) )
					else :
						self.graph.add( (expandedURIRef, ns_owl["equivalentProperty"], URIRef(subpr)) )
			except :
				# Ok, no sub property
				pass
		except :
			# no harm done, no extra vocabulary term
			pass


		return expandedURI
Пример #3
0
	def generate_triples( self, item, context ) :
		"""
		Generate the triples for a specific item. See the W3C Note for the details.
		
		@param item: the DOM Node for the specific item
		@type item: DOM Node
		@param context: an instance of an evaluation context
		@type context: L{Evaluation_Context}
		@return: a URIRef or a BNode for the (RDF) subject
		"""
		# Step 1,2: if the subject has to be set, store it in memory
		subject = context.get_memory( item )
		if subject == None :
			# nop, there is no subject set. If there is a valid @itemid, that carries it
			if item.hasAttribute("itemid") and is_absolute_URI( item.getAttribute("itemid") ):
				subject = URIRef( item.getAttribute("itemid").strip() )
			else :
				subject = BNode()
			context.set_memory( item, subject )
			
		# Step 3: set the type triples if any
		types = []
		if item.hasAttribute("itemtype") :
			types = item.getAttribute("itemtype").strip().split()
			for t in types :
				if is_absolute_URI( t ) :
					self.graph.add( (subject, ns_rdf["type"], URIRef(t)) )
		
		# Step 4, 5 and 6 to set the typing variable
		if len(types) == 0 :
			itype = None
		else :
			if is_absolute_URI(types[0]) :
				itype = types[0]
				context.current_name = None
			elif context.current_type != None :
				itype = context.current_type
			else :
				itype = None

		# Step 7, 8, 9: Check the registry for possible keys and set the vocab
		vocab = None
		if itype != None :
			for key in list(registry.keys()) :
				if itype.startswith(key) :
					# There is a predefined vocabulary for this type...
					vocab = key
					# Step 7: Issue an rdfa usesVocabulary triple
					self.graph.add( (URIRef(self.base), RDFA_VOCAB, URIRef(vocab)))
					self.vocabularies_used = True
					break
			# The registry has not set the vocabulary; has to be extracted from the type
			if vocab == None :
				parsed = urlsplit(itype)
				if parsed.fragment != "" :
					vocab = urlunsplit( (parsed.scheme,parsed.netloc,parsed.path,parsed.query,"") ) + '#'					
				elif parsed.path == "" and parsed.query == "" :
					vocab = itype
					if vocab[-1] != '/' : vocab += '/'
				else :
					vocab = itype.rsplit('/',1)[0] + '/'
		
		# Step 9: update vocab in the context
		if vocab != None :
			context.current_vocabulary = vocab
		elif item.hasAttribute("itemtype") :
			context.current_vocabulary = None

		# Step 10: set up a property list; this will be used to generate triples later.
		# each entry in the dictionary is an array of RDF objects
		property_list = {}
		
		# Step 11: Get the item properties and run a cycle on those
		for prop in self.get_item_properties(item) :
			for name in prop.getAttribute("itemprop").strip().split() :
				# 11.1.1. set a new context
				new_context = context.new_copy(itype)
				# 11.1.2, generate the URI for the property name, that will be the predicate
				# Also update the context
				new_context.current_name = predicate = self.generate_predicate_URI( name,new_context )
				# 11.1.3, generate the property value. The extra flag signals that the value is a new item
				# Note that 10.1.4 step is done in the method itself, ie, a recursion may occur there
				# if a new item is hit (in which case the return value is a RDF resource chaining to a subject)
				value  = self.get_property_value( prop, new_context )
				# 11.1.5, store all the values
				if predicate in property_list :
					property_list[predicate].append(value)
				else :
					property_list[predicate] = [ value ]
						
		# step 12: generate the triples
		for property in list(property_list.keys()) :
			self.generate_property_values( subject, URIRef(property), property_list[property], context )
			
		# Step 13: return the subject to the caller
		return subject