| 1 |
|
| 2 |
""" |
| 3 |
Implementation of the C{@property} value handling. |
| 4 |
|
| 5 |
RDFa 1.0 and RDFa 1.1 are fairly different. RDFa 1.0 generates only literals, see |
| 6 |
U{RDFa Task Force's wiki page<http://www.w3.org/2006/07/SWD/wiki/RDFa/LiteralObject>} for the details. |
| 7 |
On the other hand, RDFa 1.1, beyond literals, can also generate URI references. Hence the duplicate method in the L{ProcessProperty} class, one for RDFa 1.0 and the other for RDFa 1.1. |
| 8 |
|
| 9 |
@summary: RDFa Literal generation |
| 10 |
@requires: U{RDFLib package<http://rdflib.net>} |
| 11 |
@organization: U{World Wide Web Consortium<http://www.w3.org>} |
| 12 |
@author: U{Ivan Herman<a href="http://www.w3.org/People/Ivan/">} |
| 13 |
@license: This software is available for use under the |
| 14 |
U{W3C® SOFTWARE NOTICE AND LICENSE<href="http://www.w3.org/Consortium/Legal/2002/copyright-software-20021231">} |
| 15 |
""" |
| 16 |
|
| 17 |
""" |
| 18 |
$Id: property.py,v 1.14 2013-07-26 16:10:16 ivan Exp $ |
| 19 |
$Date: 2013-07-26 16:10:16 $ |
| 20 |
""" |
| 21 |
|
| 22 |
from rdflib import BNode |
| 23 |
from rdflib import Literal, URIRef |
| 24 |
from rdflib import RDF as ns_rdf |
| 25 |
from rdflib.term import XSDToPython |
| 26 |
|
| 27 |
from . import IncorrectBlankNodeUsage, IncorrectLiteral, err_no_blank_node |
| 28 |
from .utils import has_one_of_attributes, return_XML |
| 29 |
|
| 30 |
import re |
| 31 |
|
| 32 |
XMLLiteral = ns_rdf["XMLLiteral"] |
| 33 |
HTMLLiteral = URIRef("http://www.w3.org/1999/02/22-rdf-syntax-ns#HTML") |
| 34 |
|
| 35 |
class ProcessProperty: |
| 36 |
"""Generate the value for C{@property} taking into account datatype, etc. |
| 37 |
Note: this class is created only if the C{@property} is indeed present, no need to check. |
| 38 |
|
| 39 |
@ivar node: DOM element node |
| 40 |
@ivar graph: the (RDF) graph to add the properies to |
| 41 |
@ivar subject: the RDFLib URIRef serving as a subject for the generated triples |
| 42 |
@ivar state: the current state to be used for the CURIE-s |
| 43 |
@type state: L{state.ExecutionContext} |
| 44 |
@ivar typed_resource: Typically the bnode generated by a @typeof |
| 45 |
""" |
| 46 |
def __init__(self, node, graph, subject, state, typed_resource=None): |
| 47 |
""" |
| 48 |
@param node: DOM element node |
| 49 |
@param graph: the (RDF) graph to add the properies to |
| 50 |
@param subject: the RDFLib URIRef serving as a subject for the generated triples |
| 51 |
@param state: the current state to be used for the CURIE-s |
| 52 |
@param state: L{state.ExecutionContext} |
| 53 |
@param typed_resource: Typically the bnode generated by a @typeof; in RDFa 1.1, that becomes the object for C{@property} |
| 54 |
""" |
| 55 |
self.node = node |
| 56 |
self.graph = graph |
| 57 |
self.subject = subject |
| 58 |
self.state = state |
| 59 |
self.typed_resource = typed_resource |
| 60 |
|
| 61 |
def generate(self): |
| 62 |
""" |
| 63 |
Common entry point for the RDFa 1.0 and RDFa 1.1 versions; bifurcates based on the RDFa version, as retrieved from the state object. |
| 64 |
""" |
| 65 |
if self.state.rdfa_version >= "1.1": |
| 66 |
self.generate_1_1() |
| 67 |
else: |
| 68 |
self.generate_1_0() |
| 69 |
|
| 70 |
def generate_1_1(self): |
| 71 |
"""Generate the property object, 1.1 version""" |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
irirefs = ("resource", "href", "src") |
| 76 |
noiri = ("content", "datatype", "rel", "rev") |
| 77 |
notypediri = ("content", "datatype", "rel", "rev", "about", "about_pruned") |
| 78 |
if has_one_of_attributes(self.node, irirefs) and not has_one_of_attributes(self.node, noiri): |
| 79 |
|
| 80 |
obj = self.state.getResource(irirefs) |
| 81 |
elif self.node.hasAttribute("typeof") and not has_one_of_attributes(self.node, notypediri) and self.typed_resource != None: |
| 82 |
|
| 83 |
obj = self.typed_resource |
| 84 |
else: |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
datatype = '' |
| 89 |
dtset = False |
| 90 |
if self.node.hasAttribute("datatype"): |
| 91 |
dtset = True |
| 92 |
dt = self.node.getAttribute("datatype") |
| 93 |
if dt != "": |
| 94 |
datatype = self.state.getURI("datatype") |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
if self.state.lang != None and self.state.supress_lang == False: |
| 101 |
lang = self.state.lang |
| 102 |
else: |
| 103 |
lang = '' |
| 104 |
|
| 105 |
|
| 106 |
if self.node.hasAttribute("content"): |
| 107 |
val = self.node.getAttribute("content") |
| 108 |
|
| 109 |
if dtset == False: |
| 110 |
obj = Literal(val, lang=lang) |
| 111 |
else: |
| 112 |
obj = self._create_Literal(val, datatype=datatype, lang=lang) |
| 113 |
|
| 114 |
else: |
| 115 |
|
| 116 |
if dtset: |
| 117 |
if datatype == XMLLiteral: |
| 118 |
litval = self._get_XML_literal(self.node) |
| 119 |
obj = Literal(litval,datatype=XMLLiteral) |
| 120 |
elif datatype == HTMLLiteral: |
| 121 |
|
| 122 |
|
| 123 |
obj = Literal(self._get_HTML_literal(self.node), datatype=HTMLLiteral) |
| 124 |
else: |
| 125 |
obj = self._create_Literal(self._get_literal(self.node), datatype=datatype, lang=lang) |
| 126 |
else: |
| 127 |
obj = self._create_Literal(self._get_literal(self.node), lang=lang) |
| 128 |
|
| 129 |
if obj != None: |
| 130 |
for prop in self.state.getURI("property"): |
| 131 |
if not isinstance(prop, BNode): |
| 132 |
if self.node.hasAttribute("inlist"): |
| 133 |
self.state.add_to_list_mapping(prop, obj) |
| 134 |
else : |
| 135 |
self.graph.add( (self.subject, prop, obj) ) |
| 136 |
else: |
| 137 |
self.state.options.add_warning(err_no_blank_node % "property", warning_type=IncorrectBlankNodeUsage, node=self.node.nodeName) |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
def generate_1_0(self): |
| 142 |
"""Generate the property object, 1.0 version""" |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
datatype = '' |
| 148 |
dtset = False |
| 149 |
if self.node.hasAttribute("datatype"): |
| 150 |
dtset = True |
| 151 |
dt = self.node.getAttribute("datatype") |
| 152 |
if dt != "": |
| 153 |
datatype = self.state.getURI("datatype") |
| 154 |
|
| 155 |
if self.state.lang != None: |
| 156 |
lang = self.state.lang |
| 157 |
else: |
| 158 |
lang = '' |
| 159 |
|
| 160 |
|
| 161 |
if self.node.hasAttribute("content"): |
| 162 |
val = self.node.getAttribute("content") |
| 163 |
|
| 164 |
if dtset == False: |
| 165 |
obj = Literal(val, lang=lang) |
| 166 |
else: |
| 167 |
obj = self._create_Literal(val, datatype=datatype, lang=lang) |
| 168 |
|
| 169 |
else: |
| 170 |
|
| 171 |
if dtset: |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
if datatype == XMLLiteral: |
| 176 |
litval = self._get_XML_literal(self.node) |
| 177 |
obj = Literal(litval,datatype=XMLLiteral) |
| 178 |
elif datatype == HTMLLiteral: |
| 179 |
|
| 180 |
|
| 181 |
obj = Literal(self._get_HTML_literal(self.node), datatype=HTMLLiteral) |
| 182 |
else: |
| 183 |
obj = self._create_Literal(self._get_literal(self.node), datatype=datatype, lang=lang) |
| 184 |
else: |
| 185 |
|
| 186 |
|
| 187 |
if True in [ n.nodeType == self.node.ELEMENT_NODE for n in self.node.childNodes ]: |
| 188 |
|
| 189 |
obj = self._create_Literal(self._get_XML_literal(self.node), datatype=XMLLiteral) |
| 190 |
else: |
| 191 |
|
| 192 |
|
| 193 |
obj = self._create_Literal(self._get_literal(self.node), lang=lang) |
| 194 |
|
| 195 |
for prop in self.state.getURI("property"): |
| 196 |
if not isinstance(prop,BNode): |
| 197 |
self.graph.add( (self.subject,prop,obj) ) |
| 198 |
else: |
| 199 |
self.state.options.add_warning(err_no_blank_node % "property", warning_type=IncorrectBlankNodeUsage, node=self.node.nodeName) |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
def _putBackEntities(self, data): |
| 207 |
"""Put 'back' entities for the '&','<', and '>' characters, to produce a proper XML string. |
| 208 |
Used by the XML Literal extraction. |
| 209 |
@param data: string to be converted |
| 210 |
@return: string with entities |
| 211 |
@rtype: string |
| 212 |
""" |
| 213 |
return data.replace('&','&').replace('<','<').replace('>','>') |
| 214 |
|
| 215 |
def _get_literal(self, Pnode): |
| 216 |
""" |
| 217 |
Get (recursively) the full text from a DOM Node. |
| 218 |
|
| 219 |
@param Pnode: DOM Node |
| 220 |
@return: string |
| 221 |
""" |
| 222 |
rc = "" |
| 223 |
for node in Pnode.childNodes: |
| 224 |
if node.nodeType == node.TEXT_NODE: |
| 225 |
rc = rc + node.data |
| 226 |
elif node.nodeType == node.ELEMENT_NODE: |
| 227 |
rc = rc + self._get_literal(node) |
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
if self.state.options.space_preserve: |
| 233 |
return rc |
| 234 |
else: |
| 235 |
return re.sub(r'(\r| |\n|\t)+'," ",rc).strip() |
| 236 |
|
| 237 |
|
| 238 |
def _get_XML_literal(self, Pnode): |
| 239 |
""" |
| 240 |
Get (recursively) the XML Literal content of a DOM Node. |
| 241 |
|
| 242 |
@param Pnode: DOM Node |
| 243 |
@return: string |
| 244 |
""" |
| 245 |
rc = "" |
| 246 |
for node in Pnode.childNodes: |
| 247 |
if node.nodeType == node.TEXT_NODE: |
| 248 |
rc = rc + self._putBackEntities(node.data) |
| 249 |
elif node.nodeType == node.ELEMENT_NODE: |
| 250 |
rc = rc + return_XML(self.state, node, base = False) |
| 251 |
return rc |
| 252 |
|
| 253 |
|
| 254 |
def _get_HTML_literal(self, Pnode): |
| 255 |
""" |
| 256 |
Get (recursively) the XML Literal content of a DOM Node. |
| 257 |
|
| 258 |
@param Pnode: DOM Node |
| 259 |
@return: string |
| 260 |
""" |
| 261 |
rc = "" |
| 262 |
for node in Pnode.childNodes: |
| 263 |
if node.nodeType == node.TEXT_NODE: |
| 264 |
rc = rc + self._putBackEntities(node.data) |
| 265 |
elif node.nodeType == node.ELEMENT_NODE: |
| 266 |
rc = rc + return_XML(self.state, node, base = False, xmlns = False ) |
| 267 |
return rc |
| 268 |
|
| 269 |
|
| 270 |
def _create_Literal(self, val, datatype = '', lang = ''): |
| 271 |
""" |
| 272 |
Create a literal, taking into account the datatype and language. |
| 273 |
@return: Literal |
| 274 |
""" |
| 275 |
if datatype == None or datatype == '': |
| 276 |
return Literal(val, lang=lang) |
| 277 |
|
| 278 |
|
| 279 |
else: |
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
convFunc = XSDToPython.get(datatype, None) |
| 286 |
if convFunc: |
| 287 |
try: |
| 288 |
_pv = convFunc(val) |
| 289 |
|
| 290 |
except: |
| 291 |
self.state.options.add_warning("Incompatible value (%s) and datatype (%s) in Literal definition." % (val, datatype), warning_type=IncorrectLiteral, node=self.node.nodeName) |
| 292 |
return Literal(val, datatype=datatype) |