in assets/lambda_helper_neptune/python/rdflib/plugins/parsers/pyRdfa/termorcurie.py [0:0]
def __init__(self, state, graph, inherited_state) :
"""Initialize the vocab bound to a specific state.
@param state: the state to which this vocab instance belongs to
@type state: L{state.ExecutionContext}
@param graph: the RDF graph being worked on
@type graph: rdflib.Graph
@param inherited_state: the state inherited by the current state. 'None' if this is the top level state.
@type inherited_state: L{state.ExecutionContext}
"""
def check_prefix(pr) :
from . import uri_schemes
if pr in uri_schemes :
# The prefix being defined is a registered URI scheme, better avoid it...
state.options.add_warning(err_redefining_URI_as_prefix % pr, node=state.node.nodeName)
self.state = state
self.graph = graph
# --------------------------------------------------------------------------------
# This is set to non-void only on the top level and in the case of 1.1
default_vocab = InitialContext(self.state, inherited_state == None)
# Set the default CURIE URI
if inherited_state == None :
# This is the top level...
self.default_curie_uri = Namespace(XHTML_URI)
# self.graph.bind(XHTML_PREFIX, self.default_curie_uri)
else :
self.default_curie_uri = inherited_state.term_or_curie.default_curie_uri
# --------------------------------------------------------------------------------
# Set the default term URI
# This is a 1.1 feature, ie, should be ignored if the version is < 1.0
if state.rdfa_version >= "1.1" :
# that is the absolute default setup...
if inherited_state == None :
self.default_term_uri = None
else :
self.default_term_uri = inherited_state.term_or_curie.default_term_uri
# see if the initial context has defined a default vocabulary:
if default_vocab.vocabulary :
self.default_term_uri = default_vocab.vocabulary
# see if there is local vocab that would override previous settings
# However, care should be taken with the vocab="" value that should not become a URI...
# Indeed, this value is used to 'vipe out', ie, get back to the default vocabulary...
if self.state.node.hasAttribute("vocab") and self.state.node.getAttribute("vocab") == "" :
self.default_term_uri = default_vocab.vocabulary
else :
def_term_uri = self.state.getURI("vocab")
if def_term_uri and def_term_uri != "" :
self.default_term_uri = def_term_uri
self.graph.add((URIRef(self.state.base),RDFA_VOCAB,URIRef(def_term_uri)))
else :
self.default_term_uri = None
# --------------------------------------------------------------------------------
# The simpler case: terms, adding those that have been defined by a possible initial context
if inherited_state is None :
# this is the vocabulary belonging to the top level of the tree!
self.terms = {}
if state.rdfa_version >= "1.1" :
# Simply get the terms defined by the default vocabularies. There is no need for merging
for key in default_vocab.terms :
self.terms[key] = default_vocab.terms[key]
else :
# The terms are hardwired...
for key in predefined_1_0_rel :
self.terms[key] = URIRef(XHTML_URI + key)
else :
# just refer to the inherited terms
self.terms = inherited_state.term_or_curie.terms
#-----------------------------------------------------------------
# the locally defined namespaces
dict = {}
# locally defined xmlns namespaces, necessary for correct XML Literal generation
xmlns_dict = {}
# Add the locally defined namespaces using the xmlns: syntax
for i in range(0, state.node.attributes.length) :
attr = state.node.attributes.item(i)
if attr.name.find('xmlns:') == 0 :
# yep, there is a namespace setting
prefix = attr.localName
if prefix != "" : # exclude the top level xmlns setting...
if state.rdfa_version >= "1.1" and state.options.host_language in warn_xmlns_usage :
state.options.add_warning(err_xmlns_deprecated % prefix, IncorrectPrefixDefinition, node=state.node.nodeName)
if prefix == "_" :
state.options.add_warning(err_bnode_local_prefix, IncorrectPrefixDefinition, node=state.node.nodeName)
elif prefix.find(':') != -1 :
state.options.add_warning(err_col_local_prefix % prefix, IncorrectPrefixDefinition, node=state.node.nodeName)
else :
# quote the URI, ie, convert special characters into %.. This is
# true, for example, for spaces
uri = quote_URI(attr.value, state.options)
# create a new RDFLib Namespace entry
ns = Namespace(uri)
# Add an entry to the dictionary if not already there (priority is left to right!)
if state.rdfa_version >= "1.1" :
pr = prefix.lower()
else :
pr = prefix
dict[pr] = ns
xmlns_dict[pr] = ns
self.graph.bind(pr,ns)
check_prefix(pr)
# Add the locally defined namespaces using the @prefix syntax
# this may override the definition @xmlns
if state.rdfa_version >= "1.1" and state.node.hasAttribute("prefix") :
pr = state.node.getAttribute("prefix")
if pr != None :
# separator character is whitespace
pr_list = pr.strip().split()
# range(0, len(pr_list), 2)
for i in range(len(pr_list) - 2, -1, -2) :
prefix = pr_list[i]
# see if there is a URI at all
if i == len(pr_list) - 1 :
state.options.add_warning(err_missing_URI_prefix % (prefix,pr), node=state.node.nodeName)
break
else :
value = pr_list[i+1]
# see if the value of prefix is o.k., ie, there is a ':' at the end
if prefix[-1] != ':' :
state.options.add_warning(err_invalid_prefix % (prefix,pr), IncorrectPrefixDefinition, node=state.node.nodeName)
continue
elif prefix == ":" :
state.options.add_warning(err_no_default_prefix % pr, IncorrectPrefixDefinition, node=state.node.nodeName)
continue
else :
prefix = prefix[:-1]
uri = Namespace(quote_URI(value, state.options))
if prefix == "" :
#something to be done here
self.default_curie_uri = uri
elif prefix == "_" :
state.options.add_warning(err_bnode_local_prefix, IncorrectPrefixDefinition, node=state.node.nodeName)
else :
# last check: is the prefix an NCNAME?
if ncname.match(prefix) :
real_prefix = prefix.lower()
dict[real_prefix] = uri
self.graph.bind(real_prefix,uri)
# Additional warning: is this prefix overriding an existing xmlns statement with a different URI? if
# so, that may lead to discrepancies between an RDFa 1.0 and RDFa 1.1 run...
if (prefix in xmlns_dict and xmlns_dict[prefix] != uri) or (real_prefix in xmlns_dict and xmlns_dict[real_prefix] != uri) :
state.options.add_warning(err_prefix_and_xmlns % (real_prefix,real_prefix), node=state.node.nodeName)
check_prefix(real_prefix)
else :
state.options.add_warning(err_non_ncname_prefix % (prefix,pr), IncorrectPrefixDefinition, node=state.node.nodeName)
# See if anything has been collected at all.
# If not, the namespaces of the incoming state is
# taken over by reference. Otherwise that is copied to the
# the local dictionary
if inherited_state == None :
self.default_prefixes = default_vocab.ns
inherited_prefixes = {}
else :
self.default_prefixes = inherited_state.term_or_curie.default_prefixes
inherited_prefixes = inherited_state.term_or_curie.ns
if len(dict) == 0 :
self.ns = inherited_prefixes
else :
self.ns = {}
for key in inherited_prefixes : self.ns[key] = inherited_prefixes[key]
for key in dict :
if (key in inherited_prefixes and dict[key] != inherited_prefixes[key]) or (key in self.default_prefixes and dict[key] != self.default_prefixes[key][0]) :
state.options.add_warning(err_prefix_redefinition % key, PrefixRedefinitionWarning, node=state.node.nodeName)
self.ns[key] = dict[key]
# the xmlns prefixes have to be stored separately, again for XML Literal generation
self.xmlns = {}
if len(xmlns_dict) == 0 and inherited_state :
self.xmlns = inherited_state.term_or_curie.xmlns
else :
if inherited_state :
for key in inherited_state.term_or_curie.xmlns : self.xmlns[key] = inherited_state.term_or_curie.xmlns[key]
for key in xmlns_dict : self.xmlns[key] = xmlns_dict[key]
else :
self.xmlns = xmlns_dict