3 :organization: Logilab |
3 :organization: Logilab |
4 :copyright: 2009 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. |
4 :copyright: 2009 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. |
5 :contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
5 :contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
6 :license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
6 :license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
7 """ |
7 """ |
|
8 __docformat__ = "restructuredtext en" |
|
9 |
8 import rql |
10 import rql |
9 from yams import xy |
11 from yams import xy |
10 |
12 |
11 from cubicweb.view import StartupView |
13 from lxml import etree |
|
14 from lxml.builder import E |
|
15 |
|
16 from cubicweb.view import StartupView, AnyRsetView |
12 from cubicweb.web import form, formfields, formwidgets as fwdgs |
17 from cubicweb.web import form, formfields, formwidgets as fwdgs |
13 from cubicweb.web.views import forms, urlrewrite |
18 from cubicweb.web.views import forms, urlrewrite |
14 from cubicweb.spa2rql import Sparql2rqlTranslator |
19 from cubicweb.spa2rql import Sparql2rqlTranslator |
15 |
20 |
16 |
21 |
39 except UnsupportedQuery: |
44 except UnsupportedQuery: |
40 self.w(self.req._('we are not yet ready to handle this query')) |
45 self.w(self.req._('we are not yet ready to handle this query')) |
41 except xy.UnsupportedVocabulary, ex: |
46 except xy.UnsupportedVocabulary, ex: |
42 self.w(self.req._('unknown vocabulary:') + u' ' + unicode('ex')) |
47 self.w(self.req._('unknown vocabulary:') + u' ' + unicode('ex')) |
43 self.wview('table', rset, 'null') |
48 self.wview('table', rset, 'null') |
|
49 |
|
50 ## sparql resultset views ##################################################### |
|
51 |
|
52 YAMS_XMLSCHEMA_MAPPING = { |
|
53 'String': 'string', |
|
54 'Int': 'integer', |
|
55 'Float': 'float', |
|
56 'Boolean': 'boolean', |
|
57 'Datetime': 'dateTime', |
|
58 'Date': 'date', |
|
59 'Time': 'time', |
|
60 # XXX the following types don't have direct mapping |
|
61 'Decimal': 'string', |
|
62 'Interval': 'duration', |
|
63 'Password': 'string', |
|
64 'Bytes': 'base64Binary', |
|
65 } |
|
66 |
|
67 def xmlschema(yamstype): |
|
68 return 'http://www.w3.org/2001/XMLSchema#%s' % YAMS_XMLSCHEMA_MAPPING[yamstype] |
|
69 |
|
70 class SparqlResultXmlView(AnyRsetView): |
|
71 """The spec can be found here: http://www.w3.org/TR/rdf-sparql-XMLres/ |
|
72 """ |
|
73 id = 'sparql' |
|
74 content_type = 'application/sparql-results+xml' |
|
75 templatable = False |
|
76 # XXX use accept-headers-selectors to choose among the sparql result views |
|
77 |
|
78 def call(self): |
|
79 # XXX handle UNION |
|
80 rqlst = self.rset.syntax_tree().children[0] |
|
81 varnames = [var.name for var in rqlst.selection] |
|
82 results = E.results() |
|
83 for rowidx in xrange(len(self.rset)): |
|
84 result = E.result() |
|
85 for colidx, varname in enumerate(varnames): |
|
86 result.append(self.cell_binding(rowidx, colidx, varname)) |
|
87 results.append(result) |
|
88 sparql = E.sparql(E.head(*(E.variable(name=name) for name in varnames)), |
|
89 results) |
|
90 self.w(u'<?xml version="1.0"?>\n') |
|
91 self.w(etree.tostring(sparql, encoding=unicode)) |
|
92 |
|
93 def cell_binding(self, row, col, varname): |
|
94 celltype = self.rset.description[row][col] |
|
95 if self.schema.eschema(celltype).is_final(): |
|
96 cellcontent = self.view('cell', self.rset, row=row, col=col) |
|
97 return E.binding(E.literal(cellcontent, |
|
98 datatype=xmlschema(celltype)), |
|
99 name=varname) |
|
100 else: |
|
101 entity = self.entity(row, col) |
|
102 return E.binding(E.uri(entity.absolute_url()), name=varname) |