Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*
3 : : * This file is part of the LibreOffice project.
4 : : *
5 : : * This Source Code Form is subject to the terms of the Mozilla Public
6 : : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : : *
9 : : * This file incorporates work covered by the following license notice:
10 : : *
11 : : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : : * contributor license agreements. See the NOTICE file distributed
13 : : * with this work for additional information regarding copyright
14 : : * ownership. The ASF licenses this file to you under the Apache
15 : : * License, Version 2.0 (the "License"); you may not use this file
16 : : * except in compliance with the License. You may obtain a copy of
17 : : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : : */
19 : :
20 : : #include "librdf_repository.hxx"
21 : :
22 : : #include <string.h>
23 : :
24 : : #include <set>
25 : : #include <map>
26 : : #include <functional>
27 : : #include <algorithm>
28 : :
29 : : #include <boost/utility.hpp>
30 : : #include <boost/shared_ptr.hpp>
31 : : #include <boost/shared_array.hpp>
32 : : #include <boost/bind.hpp>
33 : :
34 : : #include <libxslt/security.h>
35 : : #include <libxml/parser.h>
36 : :
37 : : #include <redland.h>
38 : :
39 : : #include <com/sun/star/lang/XServiceInfo.hpp>
40 : : #include <com/sun/star/lang/XInitialization.hpp>
41 : : #include <com/sun/star/lang/WrappedTargetRuntimeException.hpp>
42 : : #include <com/sun/star/lang/IllegalArgumentException.hpp>
43 : : #include <com/sun/star/io/XSeekableInputStream.hpp>
44 : : #include <com/sun/star/text/XTextRange.hpp>
45 : : #include <com/sun/star/rdf/XDocumentRepository.hpp>
46 : : #include <com/sun/star/rdf/XLiteral.hpp>
47 : : #include <com/sun/star/rdf/FileFormat.hpp>
48 : : #include <com/sun/star/rdf/URIs.hpp>
49 : : #include <com/sun/star/rdf/BlankNode.hpp>
50 : : #include <com/sun/star/rdf/URI.hpp>
51 : : #include <com/sun/star/rdf/Literal.hpp>
52 : :
53 : : #include <rtl/ref.hxx>
54 : : #include <rtl/ustring.hxx>
55 : : #include <cppuhelper/implbase1.hxx>
56 : : #include <cppuhelper/implbase3.hxx>
57 : : #include <cppuhelper/basemutex.hxx>
58 : :
59 : : #include <comphelper/stlunosequence.hxx>
60 : : #include <comphelper/sequenceasvector.hxx>
61 : : #include <comphelper/makesequence.hxx>
62 : : #include <comphelper/xmltools.hxx>
63 : :
64 : : #include <com/sun/star/embed/XEncryptionProtectedSource2.hpp>
65 : :
66 : : /**
67 : : Implementation of the service com.sun.star.rdf.Repository.
68 : :
69 : : This implementation uses the Redland RDF library (librdf).
70 : :
71 : : There are several classes involved:
72 : : librdf_TypeConverter: helper class to convert data types redland <-> uno
73 : : librdf_Repository: the main repository, does almost all the work
74 : : librdf_NamedGraph: the XNamedGraph, forwards everything to repository
75 : : librdf_GraphResult: an XEnumeration<Statement>
76 : : librdf_QuerySelectResult: an XEnumeration<sequence<XNode>>
77 : :
78 : : @author mst
79 : : */
80 : :
81 : : /// anonymous implementation namespace
82 : : namespace {
83 : :
84 : : class librdf_NamedGraph;
85 : : class librdf_Repository;
86 : :
87 : : using namespace ::com::sun::star;
88 : :
89 : : typedef std::map< ::rtl::OUString, ::rtl::Reference<librdf_NamedGraph> >
90 : : NamedGraphMap_t;
91 : :
92 : : const char s_sparql [] = "sparql";
93 : : const char s_nsRDFs [] = "http://www.w3.org/2000/01/rdf-schema#";
94 : : const char s_label [] = "label";
95 : : const char s_nsOOo [] = "http://openoffice.org/2004/office/rdfa/";
96 : :
97 : : ////////////////////////////////////////////////////////////////////////////
98 : :
99 : : //FIXME: this approach is not ideal. can we use blind nodes instead?
100 : 524 : bool isInternalContext(librdf_node *i_pNode) throw ()
101 : : {
102 : : OSL_ENSURE(i_pNode, "isInternalContext: context null");
103 : : OSL_ENSURE(librdf_node_is_resource(i_pNode),
104 : : "isInternalContext: context not resource");
105 [ + - ]: 524 : if (i_pNode) {
106 : 524 : librdf_uri *pURI(librdf_node_get_uri(i_pNode));
107 : : OSL_ENSURE(pURI, "isInternalContext: URI null");
108 [ + - ]: 524 : if (pURI) {
109 : 524 : unsigned char *pContextURI(librdf_uri_as_string(pURI));
110 : : OSL_ENSURE(pContextURI,
111 : : "isInternalContext: URI string null");
112 : : // if prefix matches reserved uri, it is RDFa context
113 [ + + ]: 524 : if (!strncmp(reinterpret_cast<char *>(pContextURI),
114 : 524 : s_nsOOo, sizeof(s_nsOOo)-1)) {
115 : 122 : return true;
116 : : }
117 : : }
118 : 402 : return false;
119 : : }
120 : 524 : return true;
121 : : }
122 : :
123 : :
124 : : ////////////////////////////////////////////////////////////////////////////
125 : :
126 : : // n.b.: librdf destructor functions dereference null pointers!
127 : : // so they need to be wrapped to be usable with boost::shared_ptr.
128 : 20 : static void safe_librdf_free_world(librdf_world *const world)
129 : : {
130 [ + - ]: 20 : if (world) { librdf_free_world(world); }
131 : 20 : }
132 : 120 : static void safe_librdf_free_model(librdf_model *const model)
133 : : {
134 [ + + ]: 120 : if (model) { librdf_free_model(model); }
135 : 120 : }
136 : 840 : static void safe_librdf_free_node(librdf_node* node)
137 : : {
138 [ + - ]: 840 : if (node) { librdf_free_node(node); }
139 : 840 : }
140 : 30 : static void safe_librdf_free_parser(librdf_parser *const parser)
141 : : {
142 [ + - ]: 30 : if (parser) { librdf_free_parser(parser); }
143 : 30 : }
144 : 16 : static void safe_librdf_free_query(librdf_query *const query)
145 : : {
146 [ + - ]: 16 : if (query) { librdf_free_query(query); }
147 : 16 : }
148 : : static void
149 : 16 : safe_librdf_free_query_results(librdf_query_results *const query_results)
150 : : {
151 [ + - ]: 16 : if (query_results) { librdf_free_query_results(query_results); }
152 : 16 : }
153 : 24 : static void safe_librdf_free_serializer(librdf_serializer *const serializer)
154 : : {
155 [ + - ]: 24 : if (serializer) { librdf_free_serializer(serializer); }
156 : 24 : }
157 : 838 : static void safe_librdf_free_statement(librdf_statement *const statement)
158 : : {
159 [ + - ]: 838 : if (statement) { librdf_free_statement(statement); }
160 : 838 : }
161 : 120 : static void safe_librdf_free_storage(librdf_storage *const storage)
162 : : {
163 [ + + ]: 120 : if (storage) { librdf_free_storage(storage); }
164 : 120 : }
165 : 894 : static void safe_librdf_free_stream(librdf_stream *const stream)
166 : : {
167 [ + - ]: 894 : if (stream) { librdf_free_stream(stream); }
168 : 894 : }
169 : 126 : static void safe_librdf_free_uri(librdf_uri *const uri)
170 : : {
171 [ + - ]: 126 : if (uri) { librdf_free_uri(uri); }
172 : 126 : }
173 : :
174 : :
175 : : ////////////////////////////////////////////////////////////////////////////
176 : :
177 : : /** converts between librdf types and UNO API types.
178 : : */
179 : 60 : class librdf_TypeConverter
180 : : {
181 : : public:
182 : 60 : librdf_TypeConverter(
183 : : uno::Reference< uno::XComponentContext > const & i_xContext,
184 : : librdf_Repository &i_rRep)
185 : : : m_xContext(i_xContext)
186 : 60 : , m_rRep(i_rRep)
187 : 60 : { };
188 : :
189 : : librdf_world *createWorld() const;
190 : : librdf_storage *createStorage(librdf_world *i_pWorld) const;
191 : : librdf_model *createModel(librdf_world *i_pWorld,
192 : : librdf_storage * i_pStorage) const;
193 : : librdf_uri* mkURI( librdf_world* i_pWorld,
194 : : const uno::Reference< rdf::XURI > & i_xURI) const;
195 : : librdf_node* mkResource( librdf_world* i_pWorld,
196 : : const uno::Reference< rdf::XResource > & i_xResource) const;
197 : : librdf_node* mkNode( librdf_world* i_pWorld,
198 : : const uno::Reference< rdf::XNode > & i_xNode) const;
199 : : librdf_statement* mkStatement( librdf_world* i_pWorld,
200 : : const uno::Reference< rdf::XResource > & i_xSubject,
201 : : const uno::Reference< rdf::XURI > & i_xPredicate,
202 : : const uno::Reference< rdf::XNode > & i_xObject) const;
203 : : uno::Reference<rdf::XURI> convertToXURI(librdf_uri* i_pURI) const;
204 : : uno::Reference<rdf::XURI> convertToXURI(librdf_node* i_pURI) const;
205 : : uno::Reference<rdf::XResource>
206 : : convertToXResource(librdf_node* i_pNode) const;
207 : : uno::Reference<rdf::XNode> convertToXNode(librdf_node* i_pNode) const;
208 : : rdf::Statement
209 : : convertToStatement(librdf_statement* i_pStmt, librdf_node* i_pContext)
210 : : const;
211 : :
212 : : private:
213 : : uno::Reference< uno::XComponentContext > m_xContext;
214 : : librdf_Repository & m_rRep;
215 : : };
216 : :
217 : :
218 : : ////////////////////////////////////////////////////////////////////////////
219 : :
220 : : /** implements the repository service.
221 : : */
222 : : class librdf_Repository:
223 : : private boost::noncopyable,
224 : : // private ::cppu::BaseMutex,
225 : : public ::cppu::WeakImplHelper3<
226 : : lang::XServiceInfo,
227 : : rdf::XDocumentRepository,
228 : : lang::XInitialization>
229 : : {
230 : : public:
231 : :
232 : : explicit librdf_Repository(
233 : : uno::Reference< uno::XComponentContext > const & i_xContext);
234 : : virtual ~librdf_Repository();
235 : :
236 : : // ::com::sun::star::lang::XServiceInfo:
237 : : virtual ::rtl::OUString SAL_CALL getImplementationName()
238 : : throw (uno::RuntimeException);
239 : : virtual ::sal_Bool SAL_CALL supportsService(
240 : : const ::rtl::OUString & ServiceName) throw (uno::RuntimeException);
241 : : virtual uno::Sequence< ::rtl::OUString > SAL_CALL
242 : : getSupportedServiceNames() throw (uno::RuntimeException);
243 : :
244 : : // ::com::sun::star::rdf::XRepository:
245 : : virtual uno::Reference< rdf::XBlankNode > SAL_CALL createBlankNode()
246 : : throw (uno::RuntimeException);
247 : : virtual uno::Reference<rdf::XNamedGraph> SAL_CALL importGraph(
248 : : ::sal_Int16 i_Format,
249 : : const uno::Reference< io::XInputStream > & i_xInStream,
250 : : const uno::Reference< rdf::XURI > & i_xGraphName,
251 : : const uno::Reference< rdf::XURI > & i_xBaseURI)
252 : : throw (uno::RuntimeException, lang::IllegalArgumentException,
253 : : datatransfer::UnsupportedFlavorException,
254 : : container::ElementExistException, rdf::ParseException,
255 : : rdf::RepositoryException, io::IOException);
256 : : virtual void SAL_CALL exportGraph(::sal_Int16 i_Format,
257 : : const uno::Reference< io::XOutputStream > & i_xOutStream,
258 : : const uno::Reference< rdf::XURI > & i_xGraphName,
259 : : const uno::Reference< rdf::XURI > & i_xBaseURI)
260 : : throw (uno::RuntimeException, lang::IllegalArgumentException,
261 : : datatransfer::UnsupportedFlavorException,
262 : : container::NoSuchElementException, rdf::RepositoryException,
263 : : io::IOException);
264 : : virtual uno::Sequence< uno::Reference< rdf::XURI > > SAL_CALL
265 : : getGraphNames() throw (uno::RuntimeException, rdf::RepositoryException);
266 : : virtual uno::Reference< rdf::XNamedGraph > SAL_CALL getGraph(
267 : : const uno::Reference< rdf::XURI > & i_xGraphName)
268 : : throw (uno::RuntimeException, lang::IllegalArgumentException,
269 : : rdf::RepositoryException);
270 : : virtual uno::Reference< rdf::XNamedGraph > SAL_CALL createGraph(
271 : : const uno::Reference< rdf::XURI > & i_xGraphName)
272 : : throw (uno::RuntimeException, lang::IllegalArgumentException,
273 : : container::ElementExistException, rdf::RepositoryException);
274 : : virtual void SAL_CALL destroyGraph(
275 : : const uno::Reference< rdf::XURI > & i_xGraphName)
276 : : throw (uno::RuntimeException, lang::IllegalArgumentException,
277 : : container::NoSuchElementException, rdf::RepositoryException);
278 : : virtual uno::Reference< container::XEnumeration > SAL_CALL getStatements(
279 : : const uno::Reference< rdf::XResource > & i_xSubject,
280 : : const uno::Reference< rdf::XURI > & i_xPredicate,
281 : : const uno::Reference< rdf::XNode > & i_xObject)
282 : : throw (uno::RuntimeException,
283 : : rdf::RepositoryException);
284 : : virtual uno::Reference< rdf::XQuerySelectResult > SAL_CALL
285 : : querySelect(const ::rtl::OUString & i_rQuery)
286 : : throw (uno::RuntimeException, rdf::QueryException,
287 : : rdf::RepositoryException);
288 : : virtual uno::Reference< container::XEnumeration > SAL_CALL
289 : : queryConstruct(const ::rtl::OUString & i_rQuery)
290 : : throw (uno::RuntimeException, rdf::QueryException,
291 : : rdf::RepositoryException);
292 : : virtual ::sal_Bool SAL_CALL queryAsk(const ::rtl::OUString & i_rQuery)
293 : : throw (uno::RuntimeException, rdf::QueryException,
294 : : rdf::RepositoryException);
295 : :
296 : : // ::com::sun::star::rdf::XDocumentRepository:
297 : : virtual void SAL_CALL setStatementRDFa(
298 : : const uno::Reference< rdf::XResource > & i_xSubject,
299 : : const uno::Sequence< uno::Reference< rdf::XURI > > & i_rPredicates,
300 : : const uno::Reference< rdf::XMetadatable > & i_xObject,
301 : : const ::rtl::OUString & i_rRDFaContent,
302 : : const uno::Reference< rdf::XURI > & i_xRDFaDatatype)
303 : : throw (uno::RuntimeException, lang::IllegalArgumentException,
304 : : rdf::RepositoryException);
305 : : virtual void SAL_CALL removeStatementRDFa(
306 : : const uno::Reference< rdf::XMetadatable > & i_xElement)
307 : : throw (uno::RuntimeException, lang::IllegalArgumentException,
308 : : rdf::RepositoryException);
309 : : virtual beans::Pair< uno::Sequence<rdf::Statement>, sal_Bool > SAL_CALL
310 : : getStatementRDFa(uno::Reference< rdf::XMetadatable > const& i_xElement)
311 : : throw (uno::RuntimeException, lang::IllegalArgumentException,
312 : : rdf::RepositoryException);
313 : : virtual uno::Reference< container::XEnumeration > SAL_CALL
314 : : getStatementsRDFa(
315 : : const uno::Reference< rdf::XResource > & i_xSubject,
316 : : const uno::Reference< rdf::XURI > & i_xPredicate,
317 : : const uno::Reference< rdf::XNode > & i_xObject)
318 : : throw (uno::RuntimeException,
319 : : rdf::RepositoryException);
320 : :
321 : : // ::com::sun::star::lang::XInitialization:
322 : : virtual void SAL_CALL initialize(
323 : : const uno::Sequence< ::com::sun::star::uno::Any > & i_rArguments)
324 : : throw (uno::RuntimeException, uno::Exception);
325 : :
326 : : // XNamedGraph forwards ---------------------------------------------
327 : : const NamedGraphMap_t::iterator SAL_CALL clearGraph(
328 : : const uno::Reference< rdf::XURI > & i_xName,
329 : : bool i_Internal = false );
330 : : void addStatementGraph(
331 : : const uno::Reference< rdf::XResource > & i_xSubject,
332 : : const uno::Reference< rdf::XURI > & i_xPredicate,
333 : : const uno::Reference< rdf::XNode > & i_xObject,
334 : : const uno::Reference< rdf::XURI > & i_xName,
335 : : bool i_Internal = false );
336 : : // throw (uno::RuntimeException, lang::IllegalArgumentException,
337 : : // container::NoSuchElementException, rdf::RepositoryException);
338 : : void removeStatementsGraph(
339 : : const uno::Reference< rdf::XResource > & i_xSubject,
340 : : const uno::Reference< rdf::XURI > & i_xPredicate,
341 : : const uno::Reference< rdf::XNode > & i_xObject,
342 : : const uno::Reference< rdf::XURI > & i_xName );
343 : : // throw (uno::RuntimeException, lang::IllegalArgumentException,
344 : : // container::NoSuchElementException, rdf::RepositoryException);
345 : : uno::Reference< container::XEnumeration > getStatementsGraph(
346 : : const uno::Reference< rdf::XResource > & i_xSubject,
347 : : const uno::Reference< rdf::XURI > & i_xPredicate,
348 : : const uno::Reference< rdf::XNode > & i_xObject,
349 : : const uno::Reference< rdf::XURI > & i_xName,
350 : : bool i_Internal = false );
351 : : // throw (uno::RuntimeException, lang::IllegalArgumentException,
352 : : // container::NoSuchElementException, rdf::RepositoryException);
353 : :
354 : 544 : const librdf_TypeConverter& getTypeConverter() { return m_TypeConverter; };
355 : :
356 : : private:
357 : :
358 : : uno::Reference< uno::XComponentContext > m_xContext;
359 : :
360 : : /// librdf global data
361 : : /** N.B.: The redland documentation gives the impression that you can have
362 : : as many librdf_worlds as you like. This is true in the same sense
363 : : that you can physically be in as many places as you like.
364 : : Well, you can, just not at the same time.
365 : : The ugly truth is that destroying a librdf_world kills a bunch
366 : : of static variables; other librdf_worlds become very unhappy
367 : : when they access these.
368 : : And of course this is not documented anywhere that I could find.
369 : : So we allocate a single world, and refcount that.
370 : : */
371 : : static boost::shared_ptr<librdf_world> m_pWorld;
372 : : /// refcount
373 : : static sal_uInt32 m_NumInstances;
374 : : /// mutex for m_pWorld - redland is not as threadsafe as is often claimed
375 : : static osl::Mutex m_aMutex;
376 : :
377 : : // NB: sequence of the shared pointers is important!
378 : : /// librdf repository storage
379 : : boost::shared_ptr<librdf_storage> m_pStorage;
380 : : /// librdf repository model
381 : : boost::shared_ptr<librdf_model> m_pModel;
382 : :
383 : : /// all named graphs
384 : : NamedGraphMap_t m_NamedGraphs;
385 : :
386 : : /// type conversion helper
387 : : librdf_TypeConverter m_TypeConverter;
388 : :
389 : : /// set of xml:ids of elements with xhtml:content
390 : : ::std::set< ::rtl::OUString > m_RDFaXHTMLContentSet;
391 : : };
392 : :
393 : :
394 : : ////////////////////////////////////////////////////////////////////////////
395 : :
396 : : /** result of operations that return a graph, i.e.,
397 : : an XEnumeration of statements.
398 : : */
399 : : class librdf_GraphResult:
400 : : private boost::noncopyable,
401 : : public ::cppu::WeakImplHelper1<
402 : : container::XEnumeration>
403 : : {
404 : : public:
405 : :
406 : 544 : librdf_GraphResult(librdf_Repository *i_pRepository,
407 : : ::osl::Mutex & i_rMutex,
408 : : boost::shared_ptr<librdf_stream> const& i_pStream,
409 : : boost::shared_ptr<librdf_node> const& i_pContext,
410 : : boost::shared_ptr<librdf_query> const& i_pQuery =
411 : : boost::shared_ptr<librdf_query>() )
412 : : : m_xRep(i_pRepository)
413 : : , m_rMutex(i_rMutex)
414 : : , m_pQuery(i_pQuery)
415 : : , m_pContext(i_pContext)
416 [ + - ][ + - ]: 544 : , m_pStream(i_pStream)
[ + - ][ + - ]
417 : 544 : { };
418 : :
419 [ + - ][ + - ]: 1088 : virtual ~librdf_GraphResult() {}
[ + - ][ + - ]
[ - + ]
420 : :
421 : : // ::com::sun::star::container::XEnumeration:
422 : : virtual ::sal_Bool SAL_CALL hasMoreElements()
423 : : throw (uno::RuntimeException);
424 : : virtual uno::Any SAL_CALL nextElement()
425 : : throw (uno::RuntimeException, container::NoSuchElementException,
426 : : lang::WrappedTargetException);
427 : :
428 : : private:
429 : : // NB: this is not a weak pointer: streams _must_ be deleted before the
430 : : // storage they point into, so we keep the repository alive here
431 : : // also, sequence is important: the stream must be destroyed first.
432 : : ::rtl::Reference< librdf_Repository > m_xRep;
433 : : // needed for synchronizing access to librdf (it doesnt do win32 threading)
434 : : ::osl::Mutex & m_rMutex;
435 : : // the query (in case this is a result of a graph query)
436 : : // not that the redland documentation spells this out explicity, but
437 : : // queries must be freed only after all the results are completely read
438 : : boost::shared_ptr<librdf_query> const m_pQuery;
439 : : boost::shared_ptr<librdf_node> const m_pContext;
440 : : boost::shared_ptr<librdf_stream> const m_pStream;
441 : :
442 : : librdf_node* getContext() const;
443 : : };
444 : :
445 : :
446 : : // ::com::sun::star::container::XEnumeration:
447 : : ::sal_Bool SAL_CALL
448 : 1030 : librdf_GraphResult::hasMoreElements() throw (uno::RuntimeException)
449 : : {
450 [ + - ]: 1030 : ::osl::MutexGuard g(m_rMutex);
451 [ + + ][ + - ]: 1030 : return m_pStream.get() && !librdf_stream_end(m_pStream.get());
[ + + ][ + - ]
452 : : }
453 : :
454 : 526 : librdf_node* librdf_GraphResult::getContext() const
455 : : {
456 [ + - ][ - + ]: 526 : if (!m_pStream.get() || librdf_stream_end(m_pStream.get()))
[ - + ]
457 : 0 : return NULL;
458 : : SAL_WNODEPRECATED_DECLARATIONS_PUSH;
459 : : librdf_node *pCtxt( static_cast<librdf_node *>
460 : 526 : (librdf_stream_get_context(m_pStream.get())) );
461 : : SAL_WNODEPRECATED_DECLARATIONS_POP;
462 [ + + ]: 526 : if (pCtxt)
463 : 524 : return pCtxt;
464 : 526 : return m_pContext.get();
465 : : }
466 : :
467 : : ::com::sun::star::uno::Any SAL_CALL
468 : 526 : librdf_GraphResult::nextElement()
469 : : throw (uno::RuntimeException, container::NoSuchElementException,
470 : : lang::WrappedTargetException)
471 : : {
472 [ + - ]: 526 : ::osl::MutexGuard g(m_rMutex);
473 [ + - ][ + - ]: 526 : if (!m_pStream.get() || !librdf_stream_end(m_pStream.get())) {
[ + - ][ + - ]
474 [ + - ]: 526 : librdf_node * pCtxt = getContext();
475 : :
476 [ + - ]: 526 : librdf_statement *pStmt( librdf_stream_get_object(m_pStream.get()) );
477 [ - + ]: 526 : if (!pStmt) {
478 : : rdf::QueryException e(
479 : : "librdf_GraphResult::nextElement: "
480 [ # # ][ # # ]: 0 : "librdf_stream_get_object failed", *this);
481 : : throw lang::WrappedTargetException(
482 : : "librdf_GraphResult::nextElement: "
483 : : "librdf_stream_get_object failed", *this,
484 [ # # ][ # # ]: 0 : uno::makeAny(e));
[ # # ]
485 : : }
486 : : // NB: pCtxt may be null here if this is result of a graph query
487 [ + + ][ + + ]: 526 : if (pCtxt && isInternalContext(pCtxt)) {
[ + + ]
488 : 122 : pCtxt = 0; // XML ID context is implementation detail!
489 : : }
490 : : rdf::Statement Stmt(
491 [ + - ]: 526 : m_xRep->getTypeConverter().convertToStatement(pStmt, pCtxt) );
492 : : // NB: this will invalidate current item.
493 [ + - ]: 526 : librdf_stream_next(m_pStream.get());
494 [ + - ][ + - ]: 1052 : return uno::makeAny(Stmt);
495 : : } else {
496 [ # # ]: 526 : throw container::NoSuchElementException();
497 [ + - ]: 526 : }
498 : : }
499 : :
500 : :
501 : : ////////////////////////////////////////////////////////////////////////////
502 : :
503 : : /** result of tuple queries ("SELECT").
504 : : */
505 : : class librdf_QuerySelectResult:
506 : : private boost::noncopyable,
507 : : public ::cppu::WeakImplHelper1<
508 : : rdf::XQuerySelectResult>
509 : : {
510 : : public:
511 : :
512 : 12 : librdf_QuerySelectResult(librdf_Repository *i_pRepository,
513 : : ::osl::Mutex & i_rMutex,
514 : : boost::shared_ptr<librdf_query> const& i_pQuery,
515 : : boost::shared_ptr<librdf_query_results> const& i_pQueryResult,
516 : : uno::Sequence< ::rtl::OUString > const& i_rBindingNames )
517 : : : m_xRep(i_pRepository)
518 : : , m_rMutex(i_rMutex)
519 : : , m_pQuery(i_pQuery)
520 : : , m_pQueryResult(i_pQueryResult)
521 [ + - ][ + - ]: 12 : , m_BindingNames(i_rBindingNames)
[ + - ][ + - ]
522 : 12 : { };
523 : :
524 [ + - ][ + - ]: 24 : virtual ~librdf_QuerySelectResult() {}
[ + - ][ + - ]
[ - + ]
525 : :
526 : : // ::com::sun::star::container::XEnumeration:
527 : : virtual ::sal_Bool SAL_CALL hasMoreElements()
528 : : throw (uno::RuntimeException);
529 : : virtual uno::Any SAL_CALL nextElement()
530 : : throw (uno::RuntimeException, container::NoSuchElementException,
531 : : lang::WrappedTargetException);
532 : :
533 : : // ::com::sun::star::rdf::XQuerySelectResult:
534 : : virtual uno::Sequence< ::rtl::OUString > SAL_CALL getBindingNames()
535 : : throw (uno::RuntimeException);
536 : :
537 : : private:
538 : :
539 : : // NB: this is not a weak pointer: streams _must_ be deleted before the
540 : : // storage they point into, so we keep the repository alive here
541 : : // also, sequence is important: the stream must be destroyed first.
542 : : ::rtl::Reference< librdf_Repository > m_xRep;
543 : : // needed for synchronizing access to librdf (it doesnt do win32 threading)
544 : : ::osl::Mutex & m_rMutex;
545 : : // not that the redland documentation spells this out explicity, but
546 : : // queries must be freed only after all the results are completely read
547 : : boost::shared_ptr<librdf_query> m_pQuery;
548 : : boost::shared_ptr<librdf_query_results> m_pQueryResult;
549 : : uno::Sequence< ::rtl::OUString > m_BindingNames;
550 : : };
551 : :
552 : :
553 : : // ::com::sun::star::container::XEnumeration:
554 : : ::sal_Bool SAL_CALL
555 : 22 : librdf_QuerySelectResult::hasMoreElements() throw (uno::RuntimeException)
556 : : {
557 [ + - ]: 22 : ::osl::MutexGuard g(m_rMutex);
558 [ + - ][ + - ]: 22 : return !librdf_query_results_finished(m_pQueryResult.get());
559 : : }
560 : :
561 : : class NodeArrayDeleter : public std::unary_function<librdf_node**, void>
562 : : {
563 : : const int m_Count;
564 : :
565 : : public:
566 : 10 : NodeArrayDeleter(int i_Count) : m_Count(i_Count) { }
567 : :
568 : 10 : void operator() (librdf_node** io_pArray) const throw ()
569 : : {
570 : 10 : std::for_each(io_pArray, io_pArray + m_Count, safe_librdf_free_node);
571 [ + - ]: 10 : delete[] io_pArray;
572 : 10 : }
573 : : };
574 : :
575 : : ::com::sun::star::uno::Any SAL_CALL
576 : 10 : librdf_QuerySelectResult::nextElement()
577 : : throw (uno::RuntimeException, container::NoSuchElementException,
578 : : lang::WrappedTargetException)
579 : : {
580 [ + - ]: 10 : ::osl::MutexGuard g(m_rMutex);
581 [ + - ][ + - ]: 10 : if (!librdf_query_results_finished(m_pQueryResult.get())) {
582 : 10 : sal_Int32 count(m_BindingNames.getLength());
583 : : OSL_ENSURE(count >= 0, "negative length?");
584 [ + - ]: 10 : boost::shared_array<librdf_node*> pNodes( new librdf_node*[count],
585 [ + - ]: 20 : NodeArrayDeleter(count));
586 [ + + ]: 28 : for (int i = 0; i < count; ++i) {
587 : 18 : pNodes[i] = 0;
588 : : }
589 [ - + ]: 10 : if (librdf_query_results_get_bindings(m_pQueryResult.get(), NULL,
590 [ + - ]: 10 : pNodes.get()))
591 : : {
592 : : rdf::QueryException e(
593 : : "librdf_QuerySelectResult::nextElement: "
594 [ # # ][ # # ]: 0 : "librdf_query_results_get_bindings failed", *this);
595 : : throw lang::WrappedTargetException(
596 : : "librdf_QuerySelectResult::nextElement: "
597 : : "librdf_query_results_get_bindings failed", *this,
598 [ # # ][ # # ]: 0 : uno::makeAny(e));
[ # # ]
599 : : }
600 [ + - ]: 10 : uno::Sequence< uno::Reference< rdf::XNode > > ret(count);
601 [ + + ]: 28 : for (int i = 0; i < count; ++i) {
602 [ + - ][ + - ]: 18 : ret[i] = m_xRep->getTypeConverter().convertToXNode(pNodes[i]);
[ + - ]
603 : : }
604 : : // NB: this will invalidate current item.
605 [ + - ]: 10 : librdf_query_results_next(m_pQueryResult.get());
606 [ + - ][ + - ]: 20 : return uno::makeAny(ret);
[ + - ]
607 : : } else {
608 [ # # ]: 10 : throw container::NoSuchElementException();
609 [ + - ]: 10 : }
610 : : }
611 : :
612 : : // ::com::sun::star::rdf::XQuerySelectResult:
613 : : uno::Sequence< ::rtl::OUString > SAL_CALL
614 : 12 : librdf_QuerySelectResult::getBindingNames() throw (uno::RuntimeException)
615 : : {
616 : 12 : return m_BindingNames;
617 : : }
618 : :
619 : :
620 : : ////////////////////////////////////////////////////////////////////////////
621 : :
622 : : /** represents a named graph, and forwards all the work to repository.
623 : : */
624 : : class librdf_NamedGraph:
625 : : private boost::noncopyable,
626 : : public ::cppu::WeakImplHelper1<
627 : : rdf::XNamedGraph>
628 : : {
629 : : public:
630 : 70 : librdf_NamedGraph(librdf_Repository * i_pRep,
631 : : uno::Reference<rdf::XURI> const & i_xName)
632 : : : m_wRep(i_pRep)
633 : : , m_pRep(i_pRep)
634 [ + - ][ + - ]: 70 : , m_xName(i_xName)
[ + - ][ + - ]
635 : 70 : { };
636 : :
637 [ + - ][ + - ]: 140 : virtual ~librdf_NamedGraph() {}
[ - + ]
638 : :
639 : : // ::com::sun::star::rdf::XNode:
640 : : virtual ::rtl::OUString SAL_CALL getStringValue()
641 : : throw (uno::RuntimeException);
642 : :
643 : : // ::com::sun::star::rdf::XURI:
644 : : virtual ::rtl::OUString SAL_CALL getNamespace()
645 : : throw (uno::RuntimeException);
646 : : virtual ::rtl::OUString SAL_CALL getLocalName()
647 : : throw (uno::RuntimeException);
648 : :
649 : : // ::com::sun::star::rdf::XNamedGraph:
650 : : virtual uno::Reference<rdf::XURI> SAL_CALL getName()
651 : : throw (uno::RuntimeException);
652 : : virtual void SAL_CALL clear()
653 : : throw (uno::RuntimeException,
654 : : container::NoSuchElementException, rdf::RepositoryException);
655 : : virtual void SAL_CALL addStatement(
656 : : const uno::Reference< rdf::XResource > & i_xSubject,
657 : : const uno::Reference< rdf::XURI > & i_xPredicate,
658 : : const uno::Reference< rdf::XNode > & i_xObject)
659 : : throw (uno::RuntimeException, lang::IllegalArgumentException,
660 : : container::NoSuchElementException, rdf::RepositoryException);
661 : : virtual void SAL_CALL removeStatements(
662 : : const uno::Reference< rdf::XResource > & i_xSubject,
663 : : const uno::Reference< rdf::XURI > & i_xPredicate,
664 : : const uno::Reference< rdf::XNode > & i_xObject)
665 : : throw (uno::RuntimeException,
666 : : container::NoSuchElementException, rdf::RepositoryException);
667 : : virtual uno::Reference< container::XEnumeration > SAL_CALL getStatements(
668 : : const uno::Reference< rdf::XResource > & i_xSubject,
669 : : const uno::Reference< rdf::XURI > & i_xPredicate,
670 : : const uno::Reference< rdf::XNode > & i_xObject)
671 : : throw (uno::RuntimeException,
672 : : container::NoSuchElementException, rdf::RepositoryException);
673 : :
674 : : private:
675 : :
676 : : /// weak reference: this is needed to check if m_pRep is valid
677 : : uno::WeakReference< rdf::XRepository > m_wRep;
678 : : librdf_Repository *m_pRep;
679 : : uno::Reference< rdf::XURI > m_xName;
680 : : };
681 : :
682 : :
683 : : // ::com::sun::star::rdf::XNode:
684 : 2 : ::rtl::OUString SAL_CALL librdf_NamedGraph::getStringValue()
685 : : throw (uno::RuntimeException)
686 : : {
687 : 2 : return m_xName->getStringValue();
688 : : }
689 : :
690 : : // ::com::sun::star::rdf::XURI:
691 : 0 : ::rtl::OUString SAL_CALL librdf_NamedGraph::getNamespace()
692 : : throw (uno::RuntimeException)
693 : : {
694 : 0 : return m_xName->getNamespace();
695 : : }
696 : :
697 : 0 : ::rtl::OUString SAL_CALL librdf_NamedGraph::getLocalName()
698 : : throw (uno::RuntimeException)
699 : : {
700 : 0 : return m_xName->getLocalName();
701 : : }
702 : :
703 : : // ::com::sun::star::rdf::XNamedGraph:
704 : 34 : uno::Reference< rdf::XURI > SAL_CALL librdf_NamedGraph::getName()
705 : : throw (uno::RuntimeException)
706 : : {
707 : 34 : return m_xName;
708 : : }
709 : :
710 : 4 : void SAL_CALL librdf_NamedGraph::clear()
711 : : throw (uno::RuntimeException,
712 : : container::NoSuchElementException, rdf::RepositoryException)
713 : : {
714 [ + - ]: 4 : uno::Reference< rdf::XRepository > xRep( m_wRep );
715 [ - + ]: 4 : if (!xRep.is()) {
716 : : throw rdf::RepositoryException(
717 [ # # ][ # # ]: 0 : "librdf_NamedGraph::clear: repository is gone", *this);
718 : : }
719 : : try {
720 [ + + ]: 4 : m_pRep->clearGraph(m_xName);
721 [ + - ]: 2 : } catch (lang::IllegalArgumentException &) {
722 [ # # ]: 0 : throw uno::RuntimeException();
723 : 4 : }
724 : 2 : }
725 : :
726 : 224 : void SAL_CALL librdf_NamedGraph::addStatement(
727 : : const uno::Reference< rdf::XResource > & i_xSubject,
728 : : const uno::Reference< rdf::XURI > & i_xPredicate,
729 : : const uno::Reference< rdf::XNode > & i_xObject)
730 : : throw (uno::RuntimeException, lang::IllegalArgumentException,
731 : : container::NoSuchElementException, rdf::RepositoryException)
732 : : {
733 [ + - ]: 224 : uno::Reference< rdf::XRepository > xRep( m_wRep );
734 [ - + ]: 224 : if (!xRep.is()) {
735 : : throw rdf::RepositoryException(
736 [ # # ][ # # ]: 0 : "librdf_NamedGraph::addStatement: repository is gone", *this);
737 : : }
738 [ + + ]: 224 : m_pRep->addStatementGraph(i_xSubject, i_xPredicate, i_xObject, m_xName);
739 : 222 : }
740 : :
741 : 18 : void SAL_CALL librdf_NamedGraph::removeStatements(
742 : : const uno::Reference< rdf::XResource > & i_xSubject,
743 : : const uno::Reference< rdf::XURI > & i_xPredicate,
744 : : const uno::Reference< rdf::XNode > & i_xObject)
745 : : throw (uno::RuntimeException,
746 : : container::NoSuchElementException, rdf::RepositoryException)
747 : : {
748 [ + - ]: 18 : uno::Reference< rdf::XRepository > xRep( m_wRep );
749 [ - + ]: 18 : if (!xRep.is()) {
750 : : throw rdf::RepositoryException(
751 [ # # ][ # # ]: 0 : "librdf_NamedGraph::removeStatements: repository is gone", *this);
752 : : }
753 [ + + ]: 18 : m_pRep->removeStatementsGraph(i_xSubject, i_xPredicate, i_xObject, m_xName);
754 : 16 : }
755 : :
756 : : uno::Reference< container::XEnumeration > SAL_CALL
757 : 172 : librdf_NamedGraph::getStatements(
758 : : const uno::Reference< rdf::XResource > & i_xSubject,
759 : : const uno::Reference< rdf::XURI > & i_xPredicate,
760 : : const uno::Reference< rdf::XNode > & i_xObject)
761 : : throw (uno::RuntimeException,
762 : : container::NoSuchElementException, rdf::RepositoryException)
763 : : {
764 [ + - ]: 172 : uno::Reference< rdf::XRepository > xRep( m_wRep );
765 [ - + ]: 172 : if (!xRep.is()) {
766 : : throw rdf::RepositoryException(
767 [ # # ][ # # ]: 0 : "librdf_NamedGraph::getStatements: repository is gone", *this);
768 : : }
769 : : return m_pRep->getStatementsGraph(
770 [ + + ]: 172 : i_xSubject, i_xPredicate, i_xObject, m_xName);
771 : : }
772 : :
773 : :
774 : : ////////////////////////////////////////////////////////////////////////////
775 : :
776 : 20 : boost::shared_ptr<librdf_world> librdf_Repository::m_pWorld;
777 : : sal_uInt32 librdf_Repository::m_NumInstances = 0;
778 : 20 : osl::Mutex librdf_Repository::m_aMutex;
779 : :
780 : 60 : librdf_Repository::librdf_Repository(
781 : : uno::Reference< uno::XComponentContext > const & i_xContext)
782 : : : /*BaseMutex(),*/ m_xContext(i_xContext)
783 : : // m_pWorld (static_cast<librdf_world *>(0), safe_librdf_free_world ),
784 : : , m_pStorage(static_cast<librdf_storage*>(0), safe_librdf_free_storage)
785 : : , m_pModel (static_cast<librdf_model *>(0), safe_librdf_free_model )
786 : : , m_NamedGraphs()
787 [ + - ][ + - ]: 60 : , m_TypeConverter(i_xContext, *this)
[ + - ][ + - ]
[ + - ][ + - ]
788 : : {
789 : : OSL_ENSURE(i_xContext.is(), "librdf_Repository: null context");
790 : :
791 [ + - ]: 60 : ::osl::MutexGuard g(m_aMutex);
792 [ + + ]: 60 : if (!m_NumInstances++) {
793 [ + - ][ + - ]: 20 : m_pWorld.reset(m_TypeConverter.createWorld(), safe_librdf_free_world);
794 [ + - ]: 60 : }
795 : 60 : }
796 : :
797 [ + - ][ + - ]: 60 : librdf_Repository::~librdf_Repository()
[ + - ][ + - ]
798 : : {
799 : : // must destroy these before world!
800 [ + - ]: 60 : m_pModel.reset();
801 [ + - ]: 60 : m_pStorage.reset();
802 : :
803 : : // FIXME: so it turns out that calling librdf_free_world will
804 : : // (via raptor_sax2_finish) call xmlCleanupParser, which will
805 : : // free libxml2's globals! ARRRGH!!! => never call librdf_free_world
806 : : #if 0
807 : : ::osl::MutexGuard g(m_aMutex);
808 : : if (!--m_NumInstances) {
809 : : m_pWorld.reset();
810 : : }
811 : : #endif
812 [ - + ]: 120 : }
813 : :
814 : : // com.sun.star.uno.XServiceInfo:
815 : 0 : ::rtl::OUString SAL_CALL librdf_Repository::getImplementationName()
816 : : throw (uno::RuntimeException)
817 : : {
818 : 0 : return comp_librdf_Repository::_getImplementationName();
819 : : }
820 : :
821 : 0 : ::sal_Bool SAL_CALL librdf_Repository::supportsService(
822 : : ::rtl::OUString const & serviceName) throw (uno::RuntimeException)
823 : : {
824 : : uno::Sequence< ::rtl::OUString > serviceNames
825 [ # # ]: 0 : = comp_librdf_Repository::_getSupportedServiceNames();
826 [ # # ]: 0 : for (::sal_Int32 i = 0; i < serviceNames.getLength(); ++i) {
827 [ # # ][ # # ]: 0 : if (serviceNames[i] == serviceName)
828 : 0 : return sal_True;
829 : : }
830 [ # # ]: 0 : return sal_False;
831 : : }
832 : :
833 : : uno::Sequence< ::rtl::OUString > SAL_CALL
834 : 0 : librdf_Repository::getSupportedServiceNames() throw (uno::RuntimeException)
835 : : {
836 : 0 : return comp_librdf_Repository::_getSupportedServiceNames();
837 : : }
838 : :
839 : : // ::com::sun::star::rdf::XRepository:
840 : 8 : uno::Reference< rdf::XBlankNode > SAL_CALL librdf_Repository::createBlankNode()
841 : : throw (uno::RuntimeException)
842 : : {
843 [ + - ]: 8 : ::osl::MutexGuard g(m_aMutex);
844 : : const boost::shared_ptr<librdf_node> pNode(
845 : : librdf_new_node_from_blank_identifier(m_pWorld.get(), NULL),
846 [ + - ][ + - ]: 8 : safe_librdf_free_node);
847 [ - + ]: 8 : if (!pNode) {
848 : : throw uno::RuntimeException(
849 : : "librdf_Repository::createBlankNode: "
850 [ # # ][ # # ]: 0 : "librdf_new_node_from_blank_identifier failed", *this);
851 : : }
852 [ + - ]: 8 : const unsigned char * id (librdf_node_get_blank_identifier(pNode.get()));
853 [ - + ]: 8 : if (!id) {
854 : : throw uno::RuntimeException(
855 : : "librdf_Repository::createBlankNode: "
856 [ # # ][ # # ]: 0 : "librdf_node_get_blank_identifier failed", *this);
857 : : }
858 : : const ::rtl::OUString nodeID(::rtl::OUString::createFromAscii(
859 : 8 : reinterpret_cast<const char *>(id)));
860 : : try {
861 [ + - ]: 16 : return rdf::BlankNode::create(m_xContext, nodeID);
862 [ # # ]: 0 : } catch (const lang::IllegalArgumentException & iae) {
863 : : throw lang::WrappedTargetRuntimeException(
864 : : "librdf_Repository::createBlankNode: "
865 [ # # # # : 0 : "illegal blank node label", *this, uno::makeAny(iae));
# # ]
866 [ + - ][ + - ]: 8 : }
867 : : }
868 : :
869 : 60 : bool formatNeedsBaseURI(::sal_Int16 i_Format)
870 : : {
871 : : (void) i_Format; //FIXME any which dont?
872 : 60 : return true;
873 : : }
874 : :
875 : 0 : xmlParserInputPtr myExtEntityLoader( const char* /*URL*/, const char* /*ID*/, xmlParserCtxtPtr /*context*/)
876 : : {
877 : 0 : return NULL;
878 : : }
879 : :
880 : : //void SAL_CALL
881 : : uno::Reference<rdf::XNamedGraph> SAL_CALL
882 : 38 : librdf_Repository::importGraph(::sal_Int16 i_Format,
883 : : const uno::Reference< io::XInputStream > & i_xInStream,
884 : : const uno::Reference< rdf::XURI > & i_xGraphName,
885 : : const uno::Reference< rdf::XURI > & i_xBaseURI)
886 : : throw (uno::RuntimeException, lang::IllegalArgumentException,
887 : : datatransfer::UnsupportedFlavorException,
888 : : container::ElementExistException, rdf::ParseException,
889 : : rdf::RepositoryException, io::IOException)
890 : : {
891 [ + - ]: 38 : ::osl::MutexGuard g(m_aMutex);
892 [ + + ]: 38 : if (!i_xInStream.is()) {
893 : : throw lang::IllegalArgumentException(
894 [ + - ][ + - ]: 2 : "librdf_Repository::importGraph: stream is null", *this, 1);
895 : : }
896 : : //FIXME: other formats
897 [ - + ]: 36 : if (i_Format != rdf::FileFormat::RDF_XML) {
898 : : throw datatransfer::UnsupportedFlavorException(
899 [ # # ][ # # ]: 0 : "librdf_Repository::importGraph: file format not supported", *this);
900 : : }
901 [ - + ]: 36 : if (!i_xGraphName.is()) {
902 : : throw lang::IllegalArgumentException(
903 [ # # ][ # # ]: 0 : "librdf_Repository::importGraph: graph name is null", *this, 2);
904 : : }
905 [ + - ][ + - ]: 36 : if (i_xGraphName->getStringValue().matchAsciiL(s_nsOOo, sizeof(s_nsOOo)-1))
[ - + ]
906 : : {
907 : : throw lang::IllegalArgumentException(
908 [ # # ][ # # ]: 0 : "librdf_Repository::importGraph: URI is reserved", *this, 0);
909 : : }
910 [ + - ][ + + ]: 36 : if (formatNeedsBaseURI(i_Format) && !i_xBaseURI.is()) {
[ + + ]
911 : : throw lang::IllegalArgumentException(
912 [ + - ][ + - ]: 2 : "librdf_Repository::importGraph: base URI is null", *this, 3);
913 : : }
914 : : OSL_ENSURE(i_xBaseURI.is(), "no base uri");
915 [ + - ][ + - ]: 34 : const ::rtl::OUString baseURIU( i_xBaseURI->getStringValue() );
916 [ + + ]: 34 : if (baseURIU.indexOf('#') >= 0) {
917 : : throw lang::IllegalArgumentException(
918 [ + - ][ + - ]: 2 : "librdf_Repository::importGraph: base URI is not absolute", *this, 3);
919 : : }
920 : :
921 [ + - ][ + - ]: 32 : const ::rtl::OUString contextU( i_xGraphName->getStringValue() );
922 [ + - ][ + + ]: 32 : if (m_NamedGraphs.find(contextU) != m_NamedGraphs.end()) {
923 : : throw container::ElementExistException(
924 [ + - ][ + - ]: 2 : "librdf_Repository::importGraph: graph with given URI exists", *this);
925 : : }
926 : : const ::rtl::OString context(
927 [ + - ]: 30 : ::rtl::OUStringToOString(contextU, RTL_TEXTENCODING_UTF8) );
928 : :
929 : : const boost::shared_ptr<librdf_node> pContext(
930 : : librdf_new_node_from_uri_string(m_pWorld.get(),
931 : 30 : reinterpret_cast<const unsigned char*> (context.getStr())),
932 [ + - ][ + - ]: 30 : safe_librdf_free_node);
933 [ - + ]: 30 : if (!pContext) {
934 : : throw uno::RuntimeException(
935 [ # # ][ # # ]: 0 : "librdf_Repository::importGraph: librdf_new_node_from_uri_string failed", *this);
936 : : }
937 : :
938 : : const ::rtl::OString baseURI(
939 [ + - ]: 30 : ::rtl::OUStringToOString(baseURIU, RTL_TEXTENCODING_UTF8) );
940 : : const boost::shared_ptr<librdf_uri> pBaseURI(
941 : : librdf_new_uri(m_pWorld.get(),
942 : 30 : reinterpret_cast<const unsigned char*> (baseURI.getStr())),
943 [ + - ][ + - ]: 30 : safe_librdf_free_uri);
944 [ - + ]: 30 : if (!pBaseURI) {
945 [ # # ][ # # ]: 0 : throw uno::RuntimeException( "librdf_Repository::importGraph: librdf_new_uri failed", *this);
946 : : }
947 : :
948 : : const boost::shared_ptr<librdf_parser> pParser(
949 : : librdf_new_parser(m_pWorld.get(), "rdfxml", NULL, NULL),
950 [ + - ][ + - ]: 30 : safe_librdf_free_parser);
951 [ - + ]: 30 : if (!pParser) {
952 : : throw uno::RuntimeException(
953 : : "librdf_Repository::importGraph: "
954 [ # # ][ # # ]: 0 : "librdf_new_parser failed", *this);
955 : : }
956 : :
957 [ + - ]: 30 : xmlExternalEntityLoader oldExtEntityLoader = xmlGetExternalEntityLoader();
958 [ + - ]: 30 : xmlSetExternalEntityLoader( myExtEntityLoader);
959 : :
960 [ + - ]: 30 : uno::Sequence<sal_Int8> buf;
961 [ + - ]: 30 : uno::Reference<io::XSeekable> xSeekable(i_xInStream, uno::UNO_QUERY);
962 : : // UGLY: if only that redland junk could read streams...
963 [ + + ][ + - ]: 30 : const sal_Int64 sz( xSeekable.is() ? xSeekable->getLength() : 1 << 20 );
[ + - ]
964 : : // exceptions are propagated
965 [ + - ][ + - ]: 30 : i_xInStream->readBytes( buf, static_cast<sal_Int32>( sz ) );
966 : : const boost::shared_ptr<librdf_stream> pStream(
967 : : librdf_parser_parse_counted_string_as_stream(pParser.get(),
968 : 30 : reinterpret_cast<const unsigned char*>(buf.getConstArray()),
969 : 30 : buf.getLength(), pBaseURI.get()),
970 [ + - ][ + - ]: 60 : safe_librdf_free_stream);
971 [ - + ]: 30 : if (!pStream) {
972 : : throw rdf::ParseException(
973 : : "librdf_Repository::importGraph: "
974 [ # # ][ # # ]: 0 : "librdf_parser_parse_counted_string_as_stream failed", *this);
975 : : }
976 : : m_NamedGraphs.insert(std::make_pair(contextU,
977 [ + - ][ + - ]: 30 : new librdf_NamedGraph(this, i_xGraphName)));
978 [ - + ]: 30 : if (librdf_model_context_add_statements(m_pModel.get(),
979 [ + - ]: 30 : pContext.get(), pStream.get())) {
980 : : throw rdf::RepositoryException(
981 : : "librdf_Repository::importGraph: "
982 [ # # ][ # # ]: 0 : "librdf_model_context_add_statements failed", *this);
983 : : }
984 : :
985 [ + - ]: 30 : xmlSetExternalEntityLoader( oldExtEntityLoader);
986 [ + - ][ + - ]: 38 : return getGraph(i_xGraphName);
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
987 : : }
988 : :
989 : 24 : void addChaffWhenEncryptedStorage(const uno::Reference< io::XOutputStream > &rStream, unsigned char* pBuffer, size_t length)
990 : : {
991 [ + - ]: 24 : if (!length)
992 : 24 : return;
993 : :
994 : : uno::Reference< embed::XEncryptionProtectedSource2 > xEncr(rStream,
995 [ + - ]: 24 : uno::UNO_QUERY);
996 : :
997 [ + + ][ + - ]: 24 : bool bAddChaff = xEncr.is() && xEncr->hasEncryptionData();
[ + - ][ - + ]
998 : :
999 : : // exceptions are propagated
1000 [ + - ]: 24 : if (!bAddChaff)
1001 : : {
1002 : : const uno::Sequence<sal_Int8> buf(
1003 [ + - ]: 24 : reinterpret_cast<sal_Int8*>(pBuffer), length);
1004 [ + - ][ + - ]: 24 : rStream->writeBytes(buf);
[ + - ]
1005 : : }
1006 : : else
1007 : : {
1008 : : unsigned char *postcomment =
1009 : 0 : (unsigned char*)strchr((const char*)pBuffer, '\n');
1010 [ # # ]: 0 : if (postcomment != NULL)
1011 : : {
1012 : 0 : ++postcomment;
1013 : :
1014 : 0 : size_t preamblelen = postcomment - pBuffer;
1015 : :
1016 : : uno::Sequence<sal_Int8> buf(
1017 [ # # ]: 0 : reinterpret_cast<sal_Int8*>(pBuffer), preamblelen);
1018 [ # # ][ # # ]: 0 : rStream->writeBytes(buf);
1019 : :
1020 : 0 : rtl::OStringBuffer aComment;
1021 [ # # ]: 0 : aComment.append("<!--");
1022 [ # # ][ # # ]: 0 : aComment.append(comphelper::xml::makeXMLChaff());
1023 [ # # ]: 0 : aComment.append("-->");
1024 : :
1025 : : buf = uno::Sequence<sal_Int8>(
1026 [ # # ][ # # ]: 0 : reinterpret_cast<const sal_Int8*>(aComment.getStr()), aComment.getLength());
[ # # ]
1027 [ # # ][ # # ]: 0 : rStream->writeBytes(buf);
1028 : :
1029 : : buf = uno::Sequence<sal_Int8>(
1030 [ # # ][ # # ]: 0 : reinterpret_cast<sal_Int8*>(postcomment), length-preamblelen);
[ # # ]
1031 [ # # ][ # # ]: 0 : rStream->writeBytes(buf);
[ # # ]
1032 : : }
1033 : 24 : }
1034 : : }
1035 : :
1036 : : void SAL_CALL
1037 : 24 : librdf_Repository::exportGraph(::sal_Int16 i_Format,
1038 : : const uno::Reference< io::XOutputStream > & i_xOutStream,
1039 : : const uno::Reference< rdf::XURI > & i_xGraphName,
1040 : : const uno::Reference< rdf::XURI > & i_xBaseURI)
1041 : : throw (uno::RuntimeException, lang::IllegalArgumentException,
1042 : : datatransfer::UnsupportedFlavorException,
1043 : : container::NoSuchElementException, rdf::RepositoryException,
1044 : : io::IOException)
1045 : : {
1046 [ + - ]: 24 : ::osl::MutexGuard g(m_aMutex);
1047 [ - + ]: 24 : if (!i_xOutStream.is()) {
1048 : : throw lang::IllegalArgumentException(
1049 [ # # ][ # # ]: 0 : "librdf_Repository::exportGraph: stream is null", *this, 1);
1050 : : }
1051 : : // FIXME: other formats
1052 [ - + ]: 24 : if (i_Format != rdf::FileFormat::RDF_XML) {
1053 : : throw datatransfer::UnsupportedFlavorException(
1054 : : "librdf_Repository::exportGraph: "
1055 [ # # ][ # # ]: 0 : "file format not supported", *this);
1056 : : }
1057 [ - + ]: 24 : if (!i_xGraphName.is()) {
1058 : : throw lang::IllegalArgumentException(
1059 : : "librdf_Repository::exportGraph: "
1060 [ # # ][ # # ]: 0 : "graph name is null", *this, 2);
1061 : : }
1062 [ + - ][ - + ]: 24 : if (formatNeedsBaseURI(i_Format) && !i_xBaseURI.is()) {
[ - + ]
1063 : : throw lang::IllegalArgumentException(
1064 : : "librdf_Repository::exportGraph: "
1065 [ # # ][ # # ]: 0 : "base URI is null", *this, 3);
1066 : : }
1067 : : OSL_ENSURE(i_xBaseURI.is(), "no base uri");
1068 [ + - ][ + - ]: 24 : const ::rtl::OUString baseURIU( i_xBaseURI->getStringValue() );
1069 [ - + ]: 24 : if (baseURIU.indexOf('#') >= 0) {
1070 : : throw lang::IllegalArgumentException(
1071 : : "librdf_Repository::exportGraph: "
1072 [ # # ][ # # ]: 0 : "base URI is not absolute", *this, 3);
1073 : : }
1074 : :
1075 [ + - ][ + - ]: 24 : const ::rtl::OUString contextU( i_xGraphName->getStringValue() );
1076 [ + - ][ - + ]: 24 : if (m_NamedGraphs.find(contextU) == m_NamedGraphs.end()) {
1077 : : throw container::NoSuchElementException(
1078 : : "librdf_Repository::exportGraph: "
1079 [ # # ][ # # ]: 0 : "no graph with given URI exists", *this);
1080 : : }
1081 : : const ::rtl::OString context(
1082 [ + - ]: 24 : ::rtl::OUStringToOString(contextU, RTL_TEXTENCODING_UTF8) );
1083 : :
1084 : : const boost::shared_ptr<librdf_node> pContext(
1085 : : librdf_new_node_from_uri_string(m_pWorld.get(),
1086 : 24 : reinterpret_cast<const unsigned char*> (context.getStr())),
1087 [ + - ][ + - ]: 24 : safe_librdf_free_node);
1088 [ - + ]: 24 : if (!pContext) {
1089 : : throw uno::RuntimeException(
1090 : : "librdf_Repository::exportGraph: "
1091 [ # # ][ # # ]: 0 : "librdf_new_node_from_uri_string failed", *this);
1092 : : }
1093 : : const ::rtl::OString baseURI(
1094 [ + - ]: 24 : ::rtl::OUStringToOString(baseURIU, RTL_TEXTENCODING_UTF8) );
1095 : : const boost::shared_ptr<librdf_uri> pBaseURI(
1096 : : librdf_new_uri(m_pWorld.get(),
1097 : 24 : reinterpret_cast<const unsigned char*> (baseURI.getStr())),
1098 [ + - ][ + - ]: 24 : safe_librdf_free_uri);
1099 [ - + ]: 24 : if (!pBaseURI) {
1100 : : throw uno::RuntimeException(
1101 : : "librdf_Repository::exportGraph: "
1102 [ # # ][ # # ]: 0 : "librdf_new_uri failed", *this);
1103 : : }
1104 : :
1105 : : const boost::shared_ptr<librdf_stream> pStream(
1106 : : librdf_model_context_as_stream(m_pModel.get(), pContext.get()),
1107 [ + - ][ + - ]: 24 : safe_librdf_free_stream);
1108 [ - + ]: 24 : if (!pStream) {
1109 : : throw rdf::RepositoryException(
1110 : : "librdf_Repository::exportGraph: "
1111 [ # # ][ # # ]: 0 : "librdf_model_context_as_stream failed", *this);
1112 : : }
1113 : 24 : const char *format("rdfxml");
1114 : : // #i116443#: abbrev breaks when certain URIs are used as data types
1115 : : // const char *format("rdfxml-abbrev");
1116 : : const boost::shared_ptr<librdf_serializer> pSerializer(
1117 : : librdf_new_serializer(m_pWorld.get(), format, NULL, NULL),
1118 [ + - ][ + - ]: 24 : safe_librdf_free_serializer);
1119 [ - + ]: 24 : if (!pSerializer) {
1120 : : throw uno::RuntimeException(
1121 : : "librdf_Repository::exportGraph: "
1122 [ # # ][ # # ]: 0 : "librdf_new_serializer failed", *this);
1123 : : }
1124 : :
1125 : : const boost::shared_ptr<librdf_uri> pRelativeURI(
1126 : : librdf_new_uri(m_pWorld.get(), reinterpret_cast<const unsigned char*>
1127 : : ("http://feature.librdf.org/raptor-relativeURIs")),
1128 [ + - ][ + - ]: 24 : safe_librdf_free_uri);
1129 : : const boost::shared_ptr<librdf_uri> pWriteBaseURI(
1130 : : librdf_new_uri(m_pWorld.get(), reinterpret_cast<const unsigned char*>
1131 : : ("http://feature.librdf.org/raptor-writeBaseURI")),
1132 [ + - ][ + - ]: 24 : safe_librdf_free_uri);
1133 : : const boost::shared_ptr<librdf_node> p0(
1134 : : librdf_new_node_from_literal(m_pWorld.get(),
1135 : : reinterpret_cast<const unsigned char*> ("0"), NULL, 0),
1136 [ + - ][ + - ]: 24 : safe_librdf_free_node);
1137 : : const boost::shared_ptr<librdf_node> p1(
1138 : : librdf_new_node_from_literal(m_pWorld.get(),
1139 : : reinterpret_cast<const unsigned char*> ("1"), NULL, 0),
1140 [ + - ][ + - ]: 24 : safe_librdf_free_node);
1141 [ + - ][ + - ]: 24 : if (!pWriteBaseURI || !pRelativeURI || !p0 || !p1) {
[ + - ][ - + ]
[ - + ]
1142 : : throw uno::RuntimeException(
1143 : : "librdf_Repository::exportGraph: "
1144 [ # # ][ # # ]: 0 : "librdf_new_uri or librdf_new_node_from_literal failed", *this);
1145 : : }
1146 : :
1147 : : // make URIs relative to base URI
1148 [ - + ]: 24 : if (librdf_serializer_set_feature(pSerializer.get(),
1149 [ + - ]: 24 : pRelativeURI.get(), p1.get()))
1150 : : {
1151 : : throw uno::RuntimeException(
1152 : : "librdf_Repository::exportGraph: "
1153 [ # # ][ # # ]: 0 : "librdf_serializer_set_feature relativeURIs failed", *this);
1154 : : }
1155 : : // but do not write the base URI to the file!
1156 [ - + ]: 24 : if (librdf_serializer_set_feature(pSerializer.get(),
1157 [ + - ]: 24 : pWriteBaseURI.get(), p0.get()))
1158 : : {
1159 : : throw uno::RuntimeException(
1160 : : "librdf_Repository::exportGraph: "
1161 [ # # ][ # # ]: 0 : "librdf_serializer_set_feature writeBaseURI failed", *this);
1162 : : }
1163 : :
1164 : : size_t length;
1165 : : const boost::shared_ptr<unsigned char> pBuf(
1166 : : librdf_serializer_serialize_stream_to_counted_string(
1167 [ + - ][ + - ]: 24 : pSerializer.get(), pBaseURI.get(), pStream.get(), &length), free);
1168 [ - + ]: 24 : if (!pBuf) {
1169 : : throw rdf::RepositoryException(
1170 : : "librdf_Repository::exportGraph: "
1171 : : "librdf_serializer_serialize_stream_to_counted_string failed",
1172 [ # # ][ # # ]: 0 : *this);
1173 : : }
1174 [ + - ][ + - ]: 24 : addChaffWhenEncryptedStorage(i_xOutStream, pBuf.get(), length);
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
1175 : 24 : }
1176 : :
1177 : : uno::Sequence< uno::Reference< rdf::XURI > > SAL_CALL
1178 : 28 : librdf_Repository::getGraphNames()
1179 : : throw (uno::RuntimeException, rdf::RepositoryException)
1180 : : {
1181 [ + - ]: 28 : ::osl::MutexGuard g(m_aMutex);
1182 [ + - ]: 28 : ::comphelper::SequenceAsVector< uno::Reference<rdf::XURI> > ret;
1183 : : std::transform(m_NamedGraphs.begin(), m_NamedGraphs.end(),
1184 : : std::back_inserter(ret),
1185 : : boost::bind(&rdf::XNamedGraph::getName,
1186 [ + - ][ + - ]: 28 : boost::bind(&NamedGraphMap_t::value_type::second, _1)));
[ + - ][ + - ]
1187 [ + - ][ + - ]: 28 : return ret.getAsConstList();
1188 : : }
1189 : :
1190 : : uno::Reference< rdf::XNamedGraph > SAL_CALL
1191 : 80 : librdf_Repository::getGraph(const uno::Reference< rdf::XURI > & i_xGraphName)
1192 : : throw (uno::RuntimeException, lang::IllegalArgumentException,
1193 : : rdf::RepositoryException)
1194 : : {
1195 [ + - ]: 80 : ::osl::MutexGuard g(m_aMutex);
1196 [ - + ]: 80 : if (!i_xGraphName.is()) {
1197 : : throw lang::IllegalArgumentException(
1198 [ # # ][ # # ]: 0 : "librdf_Repository::getGraph: URI is null", *this, 0);
1199 : : }
1200 : : const NamedGraphMap_t::iterator iter(
1201 [ + - ][ + - ]: 80 : m_NamedGraphs.find(i_xGraphName->getStringValue()) );
[ + - ]
1202 [ + + ]: 80 : if (iter != m_NamedGraphs.end()) {
1203 [ + - ][ + - ]: 62 : return uno::Reference<rdf::XNamedGraph>(iter->second.get());
1204 : : } else {
1205 [ + - ]: 18 : return 0;
1206 [ + - ]: 80 : }
1207 : : }
1208 : :
1209 : : uno::Reference< rdf::XNamedGraph > SAL_CALL
1210 : 44 : librdf_Repository::createGraph(const uno::Reference< rdf::XURI > & i_xGraphName)
1211 : : throw (uno::RuntimeException, lang::IllegalArgumentException,
1212 : : container::ElementExistException, rdf::RepositoryException)
1213 : : {
1214 [ + - ]: 44 : ::osl::MutexGuard g(m_aMutex);
1215 [ + + ]: 44 : if (!i_xGraphName.is()) {
1216 : : throw lang::IllegalArgumentException(
1217 [ + - ][ + - ]: 2 : "librdf_Repository::createGraph: URI is null", *this, 0);
1218 : : }
1219 [ + - ][ + - ]: 42 : if (i_xGraphName->getStringValue().matchAsciiL(s_nsOOo, sizeof(s_nsOOo)-1))
[ - + ]
1220 : : {
1221 : : throw lang::IllegalArgumentException(
1222 [ # # ][ # # ]: 0 : "librdf_Repository::createGraph: URI is reserved", *this, 0);
1223 : : }
1224 : :
1225 : : // NB: librdf does not have a concept of graphs as such;
1226 : : // a librdf named graph exists iff the model contains a statement with
1227 : : // the graph name as context
1228 [ + - ][ + - ]: 42 : const ::rtl::OUString contextU( i_xGraphName->getStringValue() );
1229 [ + - ][ + + ]: 42 : if (m_NamedGraphs.find(contextU) != m_NamedGraphs.end()) {
1230 : : throw container::ElementExistException(
1231 [ + - ][ + - ]: 2 : "librdf_Repository::createGraph: graph with given URI exists", *this);
1232 : : }
1233 : : m_NamedGraphs.insert(std::make_pair(contextU,
1234 [ + - ][ + - ]: 40 : new librdf_NamedGraph(this, i_xGraphName)));
1235 : : return uno::Reference<rdf::XNamedGraph>(
1236 [ + - ][ + - ]: 44 : m_NamedGraphs.find(contextU)->second.get());
[ + - ][ + - ]
1237 : : }
1238 : :
1239 : : void SAL_CALL
1240 : 6 : librdf_Repository::destroyGraph(
1241 : : const uno::Reference< rdf::XURI > & i_xGraphName)
1242 : : throw (uno::RuntimeException, lang::IllegalArgumentException,
1243 : : container::NoSuchElementException, rdf::RepositoryException)
1244 : : {
1245 [ + - ]: 6 : ::osl::MutexGuard g(m_aMutex);
1246 [ + + ]: 6 : const NamedGraphMap_t::iterator iter( clearGraph(i_xGraphName) );
1247 [ + - ][ + - ]: 6 : m_NamedGraphs.erase(iter);
1248 : 4 : }
1249 : :
1250 : 1622 : static bool isMetadatableWithoutMetadata(
1251 : : uno::Reference<uno::XInterface> const & i_xNode)
1252 : : {
1253 [ + - ]: 1622 : const uno::Reference<rdf::XMetadatable> xMeta( i_xNode, uno::UNO_QUERY );
1254 [ + + ][ + - ]: 1622 : return (xMeta.is() && xMeta->getMetadataReference().Second.isEmpty());
[ + - ][ + + ]
[ + + ][ # # ]
1255 : : }
1256 : :
1257 : : uno::Reference< container::XEnumeration > SAL_CALL
1258 : 242 : librdf_Repository::getStatements(
1259 : : const uno::Reference< rdf::XResource > & i_xSubject,
1260 : : const uno::Reference< rdf::XURI > & i_xPredicate,
1261 : : const uno::Reference< rdf::XNode > & i_xObject)
1262 : : throw (uno::RuntimeException, rdf::RepositoryException)
1263 : : {
1264 [ + - ][ + + ]: 662 : if (isMetadatableWithoutMetadata(i_xSubject) ||
[ + - ][ - + ]
[ + + ]
1265 [ + - ]: 210 : isMetadatableWithoutMetadata(i_xPredicate) ||
1266 [ + - ]: 210 : isMetadatableWithoutMetadata(i_xObject))
1267 : : {
1268 : : return new librdf_GraphResult(this, m_aMutex,
1269 : : ::boost::shared_ptr<librdf_stream>(),
1270 [ + - ][ + - ]: 32 : ::boost::shared_ptr<librdf_node>());
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
1271 : : }
1272 : :
1273 [ + - ]: 210 : ::osl::MutexGuard g(m_aMutex);
1274 : : const boost::shared_ptr<librdf_statement> pStatement(
1275 : : m_TypeConverter.mkStatement(m_pWorld.get(),
1276 : : i_xSubject, i_xPredicate, i_xObject),
1277 [ + - ][ + - ]: 210 : safe_librdf_free_statement);
1278 : : OSL_ENSURE(pStatement, "mkStatement failed");
1279 : :
1280 : : const boost::shared_ptr<librdf_stream> pStream(
1281 : : librdf_model_find_statements(m_pModel.get(), pStatement.get()),
1282 [ + - ][ + - ]: 210 : safe_librdf_free_stream);
1283 [ - + ]: 210 : if (!pStream) {
1284 : : throw rdf::RepositoryException(
1285 : : "librdf_Repository::getStatements: "
1286 [ # # ][ # # ]: 0 : "librdf_model_find_statements failed", *this);
1287 : : }
1288 : :
1289 : : return new librdf_GraphResult(this, m_aMutex, pStream,
1290 [ + - ][ + - ]: 242 : ::boost::shared_ptr<librdf_node>());
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
1291 : : }
1292 : :
1293 : :
1294 : : uno::Reference< rdf::XQuerySelectResult > SAL_CALL
1295 : 12 : librdf_Repository::querySelect(const ::rtl::OUString & i_rQuery)
1296 : : throw (uno::RuntimeException, rdf::QueryException, rdf::RepositoryException)
1297 : : {
1298 [ + - ]: 12 : ::osl::MutexGuard g(m_aMutex);
1299 : : const ::rtl::OString query(
1300 [ + - ]: 12 : ::rtl::OUStringToOString(i_rQuery, RTL_TEXTENCODING_UTF8) );
1301 : : const boost::shared_ptr<librdf_query> pQuery(
1302 : : librdf_new_query(m_pWorld.get(), s_sparql, NULL,
1303 : 12 : reinterpret_cast<const unsigned char*> (query.getStr()), NULL),
1304 [ + - ][ + - ]: 12 : safe_librdf_free_query);
1305 [ - + ]: 12 : if (!pQuery) {
1306 : : throw rdf::QueryException(
1307 : : "librdf_Repository::querySelect: "
1308 [ # # ][ # # ]: 0 : "librdf_new_query failed", *this);
1309 : : }
1310 : : const boost::shared_ptr<librdf_query_results> pResults(
1311 : : librdf_model_query_execute(m_pModel.get(), pQuery.get()),
1312 [ + - ][ + - ]: 12 : safe_librdf_free_query_results);
1313 [ + - ][ + - ]: 12 : if (!pResults || !librdf_query_results_is_bindings(pResults.get())) {
[ - + ][ - + ]
1314 : : throw rdf::QueryException(
1315 : : "librdf_Repository::querySelect: "
1316 [ # # ][ # # ]: 0 : "query result is null or not bindings", *this);
1317 : : }
1318 : :
1319 [ + - ]: 12 : const int count( librdf_query_results_get_bindings_count(pResults.get()) );
1320 [ + - ]: 12 : if (count >= 0) {
1321 [ + - ]: 12 : uno::Sequence< ::rtl::OUString > names(count);
1322 [ + + ]: 30 : for (int i = 0; i < count; ++i) {
1323 : : const char* name( librdf_query_results_get_binding_name(
1324 [ + - ]: 18 : pResults.get(), i) );
1325 [ - + ]: 18 : if (!name) {
1326 : : throw rdf::QueryException(
1327 [ # # ][ # # ]: 0 : "librdf_Repository::querySelect: binding is null", *this);
1328 : : }
1329 : :
1330 [ + - ]: 18 : names[i] = ::rtl::OUString::createFromAscii(name);
1331 : : }
1332 : :
1333 : : return new librdf_QuerySelectResult(this, m_aMutex,
1334 [ + - ][ + - ]: 24 : pQuery, pResults, names);
[ + - ][ + - ]
1335 : :
1336 : : } else {
1337 : : throw rdf::QueryException(
1338 : : "librdf_Repository::querySelect: "
1339 [ # # ][ # # ]: 0 : "librdf_query_results_get_bindings_count failed", *this);
1340 [ + - ][ + - ]: 12 : }
[ + - ]
1341 : : }
1342 : :
1343 : : uno::Reference< container::XEnumeration > SAL_CALL
1344 : 2 : librdf_Repository::queryConstruct(const ::rtl::OUString & i_rQuery)
1345 : : throw (uno::RuntimeException, rdf::QueryException, rdf::RepositoryException)
1346 : : {
1347 [ + - ]: 2 : ::osl::MutexGuard g(m_aMutex);
1348 : : const ::rtl::OString query(
1349 [ + - ]: 2 : ::rtl::OUStringToOString(i_rQuery, RTL_TEXTENCODING_UTF8) );
1350 : : const boost::shared_ptr<librdf_query> pQuery(
1351 : : librdf_new_query(m_pWorld.get(), s_sparql, NULL,
1352 : 2 : reinterpret_cast<const unsigned char*> (query.getStr()), NULL),
1353 [ + - ][ + - ]: 2 : safe_librdf_free_query);
1354 [ - + ]: 2 : if (!pQuery) {
1355 : : throw rdf::QueryException(
1356 : : "librdf_Repository::queryConstruct: "
1357 [ # # ][ # # ]: 0 : "librdf_new_query failed", *this);
1358 : : }
1359 : : const boost::shared_ptr<librdf_query_results> pResults(
1360 : : librdf_model_query_execute(m_pModel.get(), pQuery.get()),
1361 [ + - ][ + - ]: 2 : safe_librdf_free_query_results);
1362 [ + - ][ + - ]: 2 : if (!pResults || !librdf_query_results_is_graph(pResults.get())) {
[ - + ][ - + ]
1363 : : throw rdf::QueryException(
1364 : : "librdf_Repository::queryConstruct: "
1365 [ # # ][ # # ]: 0 : "query result is null or not graph", *this);
1366 : : }
1367 : : const boost::shared_ptr<librdf_stream> pStream(
1368 : : librdf_query_results_as_stream(pResults.get()),
1369 [ + - ][ + - ]: 2 : safe_librdf_free_stream);
1370 [ - + ]: 2 : if (!pStream) {
1371 : : throw rdf::QueryException(
1372 : : "librdf_Repository::queryConstruct: "
1373 [ # # ][ # # ]: 0 : "librdf_query_results_as_stream failed", *this);
1374 : : }
1375 : :
1376 : : return new librdf_GraphResult(this, m_aMutex, pStream,
1377 [ + - ][ + - ]: 2 : ::boost::shared_ptr<librdf_node>(), pQuery);
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
1378 : : }
1379 : :
1380 : : ::sal_Bool SAL_CALL
1381 : 2 : librdf_Repository::queryAsk(const ::rtl::OUString & i_rQuery)
1382 : : throw (uno::RuntimeException, rdf::QueryException, rdf::RepositoryException)
1383 : : {
1384 [ + - ]: 2 : ::osl::MutexGuard g(m_aMutex);
1385 : :
1386 : : const ::rtl::OString query(
1387 [ + - ]: 2 : ::rtl::OUStringToOString(i_rQuery, RTL_TEXTENCODING_UTF8) );
1388 : : const boost::shared_ptr<librdf_query> pQuery(
1389 : : librdf_new_query(m_pWorld.get(), s_sparql, NULL,
1390 : 2 : reinterpret_cast<const unsigned char*> (query.getStr()), NULL),
1391 [ + - ][ + - ]: 2 : safe_librdf_free_query);
1392 [ - + ]: 2 : if (!pQuery) {
1393 : : throw rdf::QueryException(
1394 : : "librdf_Repository::queryAsk: "
1395 [ # # ][ # # ]: 0 : "librdf_new_query failed", *this);
1396 : : }
1397 : : const boost::shared_ptr<librdf_query_results> pResults(
1398 : : librdf_model_query_execute(m_pModel.get(), pQuery.get()),
1399 [ + - ][ + - ]: 2 : safe_librdf_free_query_results);
1400 [ + - ][ + - ]: 2 : if (!pResults || !librdf_query_results_is_boolean(pResults.get())) {
[ - + ][ - + ]
1401 : : throw rdf::QueryException(
1402 : : "librdf_Repository::queryAsk: "
1403 [ # # ][ # # ]: 0 : "query result is null or not boolean", *this);
1404 : : }
1405 [ + - ]: 2 : return librdf_query_results_get_boolean(pResults.get())
1406 [ + - ][ + - ]: 2 : ? sal_True : sal_False;
[ + - ][ + - ]
1407 : : }
1408 : :
1409 : : // ::com::sun::star::rdf::XDocumentRepository:
1410 : 78 : void SAL_CALL librdf_Repository::setStatementRDFa(
1411 : : const uno::Reference< rdf::XResource > & i_xSubject,
1412 : : const uno::Sequence< uno::Reference< rdf::XURI > > & i_rPredicates,
1413 : : const uno::Reference< rdf::XMetadatable > & i_xObject,
1414 : : const ::rtl::OUString & i_rRDFaContent,
1415 : : const uno::Reference< rdf::XURI > & i_xRDFaDatatype)
1416 : : throw (uno::RuntimeException, lang::IllegalArgumentException,
1417 : : rdf::RepositoryException)
1418 : : {
1419 [ + + ][ + - ]: 78 : static const ::rtl::OUString s_cell("com.sun.star.table.Cell");
1420 [ + + ][ + - ]: 78 : static const ::rtl::OUString s_cellprops("com.sun.star.text.CellProperties"); // for writer
1421 [ + + ][ + - ]: 78 : static const ::rtl::OUString s_paragraph("com.sun.star.text.Paragraph");
1422 [ + + ][ + - ]: 78 : static const ::rtl::OUString s_bookmark("com.sun.star.text.Bookmark");
1423 [ + + ][ + - ]: 78 : static const ::rtl::OUString s_meta("com.sun.star.text.InContentMetadata");
1424 : :
1425 [ - + ]: 78 : if (!i_xSubject.is()) {
1426 : : throw lang::IllegalArgumentException(
1427 [ # # ][ # # ]: 0 : "librdf_Repository::setStatementRDFa: Subject is null", *this, 0);
1428 : : }
1429 [ + + ]: 78 : if (!i_rPredicates.getLength()) {
1430 : : throw lang::IllegalArgumentException(
1431 : : "librdf_Repository::setStatementRDFa: no Predicates",
1432 [ + - ][ + - ]: 2 : *this, 1);
1433 : : }
1434 [ + + ]: 168 : for (sal_Int32 i = 0; i < i_rPredicates.getLength(); ++i) {
1435 [ - + ]: 92 : if (!i_rPredicates[i].is()) {
1436 : : throw lang::IllegalArgumentException(
1437 : : "librdf_Repository::setStatementRDFa: Predicate is null",
1438 [ # # ][ # # ]: 0 : *this, 1);
1439 : : }
1440 : : }
1441 [ + + ]: 76 : if (!i_xObject.is()) {
1442 : : throw lang::IllegalArgumentException(
1443 [ + - ][ + - ]: 2 : "librdf_Repository::setStatementRDFa: Object is null", *this, 2);
1444 : : }
1445 : : const uno::Reference<lang::XServiceInfo> xService(i_xObject,
1446 [ + - ]: 74 : uno::UNO_QUERY_THROW);
1447 : 74 : uno::Reference<text::XTextRange> xTextRange;
1448 [ + - ][ + - ]: 222 : if (xService->supportsService(s_cell) ||
[ + - ][ + + ]
[ + + ][ + - ]
1449 [ + - ][ + - ]: 74 : xService->supportsService(s_cellprops) ||
1450 [ + - ][ + - ]: 74 : xService->supportsService(s_paragraph))
1451 : : {
1452 [ + - ]: 62 : xTextRange.set(i_xObject, uno::UNO_QUERY_THROW);
1453 : : }
1454 [ + - ][ + - ]: 16 : else if (xService->supportsService(s_bookmark) ||
[ + + ][ + - ]
[ + - ]
1455 [ + - ][ + - ]: 4 : xService->supportsService(s_meta))
1456 : : {
1457 : : const uno::Reference<text::XTextContent> xTextContent(i_xObject,
1458 [ + - ]: 12 : uno::UNO_QUERY_THROW);
1459 [ + - ][ + - ]: 12 : xTextRange = xTextContent->getAnchor();
[ + - ]
1460 : : }
1461 [ - + ]: 74 : if (!xTextRange.is()) {
1462 : : throw lang::IllegalArgumentException(
1463 : : "librdf_Repository::setStatementRDFa: "
1464 [ # # ][ # # ]: 0 : "Object does not support RDFa", *this, 2);
1465 : : }
1466 : : // ensure that the metadatable has an XML ID
1467 [ + - ][ + - ]: 74 : i_xObject->ensureMetadataReference();
1468 [ + - ][ + - ]: 74 : const beans::StringPair mdref( i_xObject->getMetadataReference() );
1469 [ + - ][ - + ]: 74 : if ((mdref.First.isEmpty()) || (mdref.Second.isEmpty())) {
[ - + ]
1470 : : throw uno::RuntimeException(
1471 : : "librdf_Repository::setStatementRDFa: "
1472 [ # # ][ # # ]: 0 : "ensureMetadataReference did not", *this);
1473 : : }
1474 : 74 : ::rtl::OUString const sXmlId(mdref.First + "#" + mdref.Second);
1475 : 74 : uno::Reference<rdf::XURI> xXmlId;
1476 : : try {
1477 : : xXmlId.set( rdf::URI::create(m_xContext,
1478 : : ::rtl::OUString::createFromAscii(s_nsOOo) + sXmlId),
1479 [ + - ][ + - ]: 74 : uno::UNO_QUERY_THROW);
1480 [ # # ]: 0 : } catch (const lang::IllegalArgumentException & iae) {
1481 : : throw lang::WrappedTargetRuntimeException(
1482 : : "librdf_Repository::setStatementRDFa: "
1483 [ # # # # : 0 : "cannot create URI for XML ID", *this, uno::makeAny(iae));
# # ]
1484 : : }
1485 : :
1486 [ + - ]: 74 : ::osl::MutexGuard g(m_aMutex);
1487 : 74 : ::rtl::OUString const content( (i_rRDFaContent.isEmpty())
1488 [ + - ]: 40 : ? xTextRange->getString()
1489 [ + - ][ + + ]: 114 : : i_rRDFaContent );
1490 : 74 : uno::Reference<rdf::XNode> xContent;
1491 : : try {
1492 [ + + ]: 74 : if (i_xRDFaDatatype.is()) {
1493 : : xContent.set(rdf::Literal::createWithType(m_xContext,
1494 : : content, i_xRDFaDatatype),
1495 [ + - ][ + - ]: 22 : uno::UNO_QUERY_THROW);
1496 : : } else {
1497 : : xContent.set(rdf::Literal::create(m_xContext, content),
1498 [ + - ][ + - ]: 52 : uno::UNO_QUERY_THROW);
1499 : : }
1500 [ # # ]: 0 : } catch (const lang::IllegalArgumentException & iae) {
1501 : : throw lang::WrappedTargetRuntimeException(
1502 : : "librdf_Repository::setStatementRDFa: "
1503 [ # # # # : 0 : "cannot create literal", *this, uno::makeAny(iae));
# # ]
1504 : : }
1505 [ + - ]: 74 : removeStatementRDFa(i_xObject);
1506 [ + + ]: 74 : if (i_rRDFaContent.isEmpty()) {
1507 [ + - ]: 40 : m_RDFaXHTMLContentSet.erase(sXmlId);
1508 : : } else {
1509 [ + - ]: 34 : m_RDFaXHTMLContentSet.insert(sXmlId);
1510 : : }
1511 : : ::std::for_each(::comphelper::stl_begin(i_rPredicates),
1512 : : ::comphelper::stl_end(i_rPredicates),
1513 : : ::boost::bind( &librdf_Repository::addStatementGraph,
1514 [ + - ][ + - ]: 78 : this, i_xSubject, _1, xContent, xXmlId, true));
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
1515 : 74 : }
1516 : :
1517 : 78 : void SAL_CALL librdf_Repository::removeStatementRDFa(
1518 : : const uno::Reference< rdf::XMetadatable > & i_xElement)
1519 : : throw (uno::RuntimeException, lang::IllegalArgumentException,
1520 : : rdf::RepositoryException)
1521 : : {
1522 [ - + ]: 78 : if (!i_xElement.is()) {
1523 : : throw lang::IllegalArgumentException(
1524 : : "librdf_Repository::removeStatementRDFa: Element is null",
1525 [ # # ][ # # ]: 0 : *this, 0);
1526 : : }
1527 : :
1528 [ + - ][ + - ]: 78 : const beans::StringPair mdref( i_xElement->getMetadataReference() );
1529 [ + - ][ - + ]: 78 : if ((mdref.First.isEmpty()) || (mdref.Second.isEmpty())) {
[ - + ]
1530 : 78 : return; // nothing to do...
1531 : : }
1532 : 78 : uno::Reference<rdf::XURI> xXmlId;
1533 : : try {
1534 : : xXmlId.set( rdf::URI::create(m_xContext,
1535 : : ::rtl::OUString::createFromAscii(s_nsOOo)
1536 : : + mdref.First + "#"
1537 : : + mdref.Second),
1538 [ + - ][ + - ]: 78 : uno::UNO_QUERY_THROW);
1539 [ # # ]: 0 : } catch (const lang::IllegalArgumentException & iae) {
1540 : : throw lang::WrappedTargetRuntimeException(
1541 : : "librdf_Repository::removeStatementRDFa: "
1542 [ # # # # : 0 : "cannot create URI for XML ID", *this, uno::makeAny(iae));
# # ]
1543 : : }
1544 : : // clearGraph does locking, not needed here
1545 [ + - ][ + - ]: 78 : clearGraph(xXmlId, true);
1546 : : }
1547 : :
1548 : : beans::Pair< uno::Sequence<rdf::Statement>, sal_Bool > SAL_CALL
1549 : 160 : librdf_Repository::getStatementRDFa(
1550 : : const uno::Reference< rdf::XMetadatable > & i_xElement)
1551 : : throw (uno::RuntimeException, lang::IllegalArgumentException,
1552 : : rdf::RepositoryException)
1553 : : {
1554 [ - + ]: 160 : if (!i_xElement.is()) {
1555 : : throw lang::IllegalArgumentException(
1556 [ # # ][ # # ]: 0 : "librdf_Repository::getStatementRDFa: Element is null", *this, 0);
1557 : : }
1558 [ + - ][ + - ]: 160 : const beans::StringPair mdref( i_xElement->getMetadataReference() );
1559 [ + + ][ - + ]: 160 : if ((mdref.First.isEmpty()) || (mdref.Second.isEmpty())) {
[ + + ]
1560 [ + - ]: 30 : return beans::Pair< uno::Sequence<rdf::Statement>, sal_Bool >();
1561 : : }
1562 : 130 : ::rtl::OUString const sXmlId(mdref.First + "#" + mdref.Second);
1563 : 130 : uno::Reference<rdf::XURI> xXmlId;
1564 : : try {
1565 : : xXmlId.set( rdf::URI::create(m_xContext,
1566 : : ::rtl::OUString::createFromAscii(s_nsOOo) + sXmlId),
1567 [ + - ][ + - ]: 130 : uno::UNO_QUERY_THROW);
1568 [ # # ]: 0 : } catch (const lang::IllegalArgumentException & iae) {
1569 : : throw lang::WrappedTargetRuntimeException(
1570 : : "librdf_Repository::getStatementRDFa: "
1571 [ # # # # : 0 : "cannot create URI for XML ID", *this, uno::makeAny(iae));
# # ]
1572 : : }
1573 : :
1574 [ + - ]: 130 : ::osl::MutexGuard g(m_aMutex);
1575 [ + - ]: 130 : ::comphelper::SequenceAsVector< rdf::Statement > ret;
1576 : : const uno::Reference<container::XEnumeration> xIter(
1577 [ + - ][ + - ]: 130 : getStatementsGraph(0, 0, 0, xXmlId, true) );
[ + - ][ + - ]
1578 : : OSL_ENSURE(xIter.is(), "getStatementRDFa: no result?");
1579 [ # # ][ - + ]: 130 : if (!xIter.is()) throw uno::RuntimeException();
1580 [ + - ][ + - ]: 252 : while (xIter->hasMoreElements()) {
[ + + ]
1581 [ + - ]: 122 : rdf::Statement stmt;
1582 [ + - ][ + - ]: 122 : if (!(xIter->nextElement() >>= stmt)) {
[ + - ][ + - ]
1583 : : OSL_FAIL("getStatementRDFa: result of wrong type?");
1584 : : } else {
1585 [ + - ]: 122 : ret.push_back(stmt);
1586 : : }
1587 [ + - ]: 122 : }
1588 : : return beans::Pair< uno::Sequence<rdf::Statement>, sal_Bool >(
1589 [ + - ][ + - ]: 160 : ret.getAsConstList(), 0 != m_RDFaXHTMLContentSet.count(sXmlId));
[ + - ][ + - ]
[ + - ]
1590 : : }
1591 : :
1592 : : extern "C"
1593 : 0 : librdf_statement *rdfa_context_stream_map_handler(
1594 : : librdf_stream *i_pStream, void *, librdf_statement *i_pStatement)
1595 : : {
1596 : : OSL_ENSURE(i_pStream, "rdfa_context_stream_map_handler: stream null");
1597 [ # # ]: 0 : if (i_pStream) {
1598 : : SAL_WNODEPRECATED_DECLARATIONS_PUSH;
1599 : : librdf_node *pCtxt( static_cast<librdf_node *>
1600 : 0 : (librdf_stream_get_context(i_pStream)) );
1601 : : SAL_WNODEPRECATED_DECLARATIONS_POP;
1602 : : OSL_ENSURE(pCtxt, "rdfa_context_stream_map_handler: context null");
1603 [ # # ][ # # ]: 0 : if (pCtxt && isInternalContext(pCtxt)) {
[ # # ]
1604 : 0 : return i_pStatement;
1605 : : }
1606 : : }
1607 : 0 : return 0;
1608 : : };
1609 : :
1610 : : uno::Reference< container::XEnumeration > SAL_CALL
1611 : 0 : librdf_Repository::getStatementsRDFa(
1612 : : const uno::Reference< rdf::XResource > & i_xSubject,
1613 : : const uno::Reference< rdf::XURI > & i_xPredicate,
1614 : : const uno::Reference< rdf::XNode > & i_xObject)
1615 : : throw (uno::RuntimeException, rdf::RepositoryException)
1616 : : {
1617 [ # # ][ # # ]: 0 : if (isMetadatableWithoutMetadata(i_xSubject) ||
[ # # ][ # # ]
[ # # ]
1618 [ # # ]: 0 : isMetadatableWithoutMetadata(i_xPredicate) ||
1619 [ # # ]: 0 : isMetadatableWithoutMetadata(i_xObject))
1620 : : {
1621 : : return new librdf_GraphResult(this, m_aMutex,
1622 : : ::boost::shared_ptr<librdf_stream>(),
1623 [ # # ][ # # ]: 0 : ::boost::shared_ptr<librdf_node>());
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ]
1624 : : }
1625 : :
1626 [ # # ]: 0 : ::osl::MutexGuard g(m_aMutex);
1627 : : const boost::shared_ptr<librdf_statement> pStatement(
1628 : : m_TypeConverter.mkStatement(m_pWorld.get(),
1629 : : i_xSubject, i_xPredicate, i_xObject),
1630 [ # # ][ # # ]: 0 : safe_librdf_free_statement);
1631 : : OSL_ENSURE(pStatement, "mkStatement failed");
1632 : :
1633 : : const boost::shared_ptr<librdf_stream> pStream(
1634 : : librdf_model_find_statements(m_pModel.get(), pStatement.get()),
1635 [ # # ][ # # ]: 0 : safe_librdf_free_stream);
1636 [ # # ]: 0 : if (!pStream) {
1637 : : throw rdf::RepositoryException(
1638 : : "librdf_Repository::getStatementsRDFa: "
1639 [ # # ][ # # ]: 0 : "librdf_model_find_statements failed", *this);
1640 : : }
1641 : :
1642 [ # # ]: 0 : if (librdf_stream_add_map(pStream.get(), rdfa_context_stream_map_handler,
1643 [ # # ]: 0 : 0, 0)) {
1644 : : throw rdf::RepositoryException(
1645 : : "librdf_Repository::getStatementsRDFa: "
1646 [ # # ][ # # ]: 0 : "librdf_stream_add_map failed", *this);
1647 : : }
1648 : :
1649 : : return new librdf_GraphResult(this, m_aMutex, pStream,
1650 [ # # ][ # # ]: 0 : ::boost::shared_ptr<librdf_node>());
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
1651 : : }
1652 : :
1653 : : // ::com::sun::star::lang::XInitialization:
1654 : 60 : void SAL_CALL librdf_Repository::initialize(
1655 : : const uno::Sequence< ::com::sun::star::uno::Any > & i_rArguments)
1656 : : throw (uno::RuntimeException, uno::Exception)
1657 : : {
1658 : : (void) i_rArguments;
1659 : :
1660 [ + - ]: 60 : ::osl::MutexGuard g(m_aMutex);
1661 : :
1662 : : // m_pWorld.reset(m_TypeConverter.createWorld(), safe_librdf_free_world);
1663 : : m_pStorage.reset(m_TypeConverter.createStorage(m_pWorld.get()),
1664 [ + - ][ + - ]: 60 : safe_librdf_free_storage);
1665 : : m_pModel.reset(m_TypeConverter.createModel(
1666 [ + - ][ + - ]: 60 : m_pWorld.get(), m_pStorage.get()), safe_librdf_free_model);
[ + - ]
1667 : 60 : }
1668 : :
1669 : 88 : const NamedGraphMap_t::iterator SAL_CALL librdf_Repository::clearGraph(
1670 : : const uno::Reference< rdf::XURI > & i_xGraphName, bool i_Internal)
1671 : : // throw (uno::RuntimeException, container::NoSuchElementException,
1672 : : // rdf::RepositoryException)
1673 : : {
1674 [ + + ]: 88 : if (!i_xGraphName.is()) {
1675 : : throw lang::IllegalArgumentException(
1676 [ + - ][ + - ]: 2 : "librdf_Repository::clearGraph: URI is null", *this, 0);
1677 : : }
1678 [ + - ]: 86 : ::osl::MutexGuard g(m_aMutex);
1679 [ + - ][ + - ]: 86 : const ::rtl::OUString contextU( i_xGraphName->getStringValue() );
1680 [ + - ]: 86 : const NamedGraphMap_t::iterator iter( m_NamedGraphs.find(contextU) );
1681 [ + + ][ + + ]: 86 : if (!i_Internal && iter == m_NamedGraphs.end()) {
[ + + ][ + + ]
1682 : : throw container::NoSuchElementException(
1683 : : "librdf_Repository::clearGraph: "
1684 [ + - ][ + - ]: 2 : "no graph with given URI exists", *this);
1685 : : }
1686 : : const ::rtl::OString context(
1687 [ + - ]: 84 : ::rtl::OUStringToOString(contextU, RTL_TEXTENCODING_UTF8) );
1688 : :
1689 : : const boost::shared_ptr<librdf_node> pContext(
1690 : : librdf_new_node_from_uri_string(m_pWorld.get(),
1691 : 84 : reinterpret_cast<const unsigned char*> (context.getStr())),
1692 [ + - ][ + - ]: 84 : safe_librdf_free_node);
1693 [ - + ]: 84 : if (!pContext) {
1694 : : throw uno::RuntimeException(
1695 : : "librdf_Repository::clearGraph: "
1696 [ # # ][ # # ]: 0 : "librdf_new_node_from_uri_string failed", *this);
1697 : : }
1698 [ + - ][ - + ]: 84 : if (librdf_model_context_remove_statements(m_pModel.get(), pContext.get()))
1699 : : {
1700 : : throw rdf::RepositoryException(
1701 : : "librdf_Repository::clearGraph: "
1702 [ # # ][ # # ]: 0 : "librdf_model_context_remove_statements failed", *this);
1703 : : }
1704 [ + - ][ + - ]: 88 : return iter;
1705 : : }
1706 : :
1707 : 314 : void librdf_Repository::addStatementGraph(
1708 : : const uno::Reference< rdf::XResource > & i_xSubject,
1709 : : const uno::Reference< rdf::XURI > & i_xPredicate,
1710 : : const uno::Reference< rdf::XNode > & i_xObject,
1711 : : const uno::Reference< rdf::XURI > & i_xGraphName,
1712 : : bool i_Internal)
1713 : : //throw (uno::RuntimeException, lang::IllegalArgumentException,
1714 : : // container::NoSuchElementException, rdf::RepositoryException)
1715 : : {
1716 [ - + ]: 314 : if (!i_xSubject.is()) {
1717 : : throw lang::IllegalArgumentException(
1718 [ # # ][ # # ]: 0 : "librdf_Repository::addStatement: Subject is null", *this, 0);
1719 : : }
1720 [ - + ]: 314 : if (!i_xPredicate.is()) {
1721 : : throw lang::IllegalArgumentException(
1722 : : "librdf_Repository::addStatement: Predicate is null",
1723 [ # # ][ # # ]: 0 : *this, 1);
1724 : : }
1725 [ - + ]: 314 : if (!i_xObject.is()) {
1726 : : throw lang::IllegalArgumentException(
1727 [ # # ][ # # ]: 0 : "librdf_Repository::addStatement: Object is null", *this, 2);
1728 : : }
1729 : :
1730 [ + - ]: 314 : ::osl::MutexGuard g(m_aMutex);
1731 [ + - ][ + - ]: 314 : const ::rtl::OUString contextU( i_xGraphName->getStringValue() );
1732 [ + + ][ + - ]: 314 : if (!i_Internal && (m_NamedGraphs.find(contextU) == m_NamedGraphs.end())) {
[ + + ][ + + ]
[ + + ][ + +
# # # # ]
1733 : : throw container::NoSuchElementException(
1734 : : "librdf_Repository::addStatement: "
1735 [ + - ][ + - ]: 2 : "no graph with given URI exists", *this);
1736 : : }
1737 : : const ::rtl::OString context(
1738 [ + - ]: 312 : ::rtl::OUStringToOString(contextU, RTL_TEXTENCODING_UTF8) );
1739 : :
1740 : : const boost::shared_ptr<librdf_node> pContext(
1741 : : librdf_new_node_from_uri_string(m_pWorld.get(),
1742 : 312 : reinterpret_cast<const unsigned char*> (context.getStr())),
1743 [ + - ][ + - ]: 312 : safe_librdf_free_node);
1744 [ - + ]: 312 : if (!pContext) {
1745 : : throw uno::RuntimeException(
1746 : : "librdf_Repository::addStatement: "
1747 [ # # ][ # # ]: 0 : "librdf_new_node_from_uri_string failed", *this);
1748 : : }
1749 : : const boost::shared_ptr<librdf_statement> pStatement(
1750 : : m_TypeConverter.mkStatement(m_pWorld.get(),
1751 : : i_xSubject, i_xPredicate, i_xObject),
1752 [ + - ][ + - ]: 312 : safe_librdf_free_statement);
1753 : : OSL_ENSURE(pStatement, "mkStatement failed");
1754 : :
1755 : : // Test for duplicate statement
1756 : : // librdf_model_add_statement disallows duplicates while
1757 : : // librdf_model_context_add_statement allows duplicates
1758 : : {
1759 : : const boost::shared_ptr<librdf_stream> pStream(
1760 : : librdf_model_find_statements_in_context(m_pModel.get(),
1761 : : pStatement.get(), pContext.get()),
1762 [ + - ][ + - ]: 312 : safe_librdf_free_stream);
1763 [ + - ][ + - ]: 312 : if (pStream && !librdf_stream_end(pStream.get()))
[ + + ][ + + ]
1764 [ + - ][ + + ]: 624 : return;
1765 : : }
1766 : :
1767 [ - + ]: 288 : if (librdf_model_context_add_statement(m_pModel.get(),
1768 [ + - ]: 312 : pContext.get(), pStatement.get())) {
1769 : : throw rdf::RepositoryException(
1770 : : "librdf_Repository::addStatement: "
1771 [ # # ][ # # ]: 288 : "librdf_model_context_add_statement failed", *this);
1772 [ + - ][ + + ]: 314 : }
[ + - ][ + + ]
[ + + ][ + + ]
[ + - ][ + + ]
1773 : : }
1774 : :
1775 : 18 : void librdf_Repository::removeStatementsGraph(
1776 : : const uno::Reference< rdf::XResource > & i_xSubject,
1777 : : const uno::Reference< rdf::XURI > & i_xPredicate,
1778 : : const uno::Reference< rdf::XNode > & i_xObject,
1779 : : const uno::Reference< rdf::XURI > & i_xGraphName)
1780 : : //throw (uno::RuntimeException, lang::IllegalArgumentException,
1781 : : // container::NoSuchElementException, rdf::RepositoryException)
1782 : : {
1783 [ + - ][ + - ]: 54 : if (isMetadatableWithoutMetadata(i_xSubject) ||
[ + - ][ - + ]
[ + - ]
1784 [ + - ]: 18 : isMetadatableWithoutMetadata(i_xPredicate) ||
1785 [ + - ]: 18 : isMetadatableWithoutMetadata(i_xObject))
1786 : : {
1787 : 16 : return;
1788 : : }
1789 : :
1790 [ + - ]: 18 : ::osl::MutexGuard g(m_aMutex);
1791 [ + - ][ + - ]: 18 : const ::rtl::OUString contextU( i_xGraphName->getStringValue() );
1792 [ + - ][ + + ]: 18 : if (m_NamedGraphs.find(contextU) == m_NamedGraphs.end()) {
1793 : : throw container::NoSuchElementException(
1794 : : "librdf_Repository::removeStatements: "
1795 [ + - ][ + - ]: 2 : "no graph with given URI exists", *this);
1796 : : }
1797 : : const ::rtl::OString context(
1798 [ + - ]: 16 : ::rtl::OUStringToOString(contextU, RTL_TEXTENCODING_UTF8) );
1799 : :
1800 : : const boost::shared_ptr<librdf_node> pContext(
1801 : : librdf_new_node_from_uri_string(m_pWorld.get(),
1802 : 16 : reinterpret_cast<const unsigned char*> (context.getStr())),
1803 [ + - ][ + - ]: 16 : safe_librdf_free_node);
1804 [ - + ]: 16 : if (!pContext) {
1805 : : throw uno::RuntimeException(
1806 : : "librdf_Repository::removeStatements: "
1807 [ # # ][ # # ]: 0 : "librdf_new_node_from_uri_string failed", *this);
1808 : : }
1809 : : const boost::shared_ptr<librdf_statement> pStatement(
1810 : : m_TypeConverter.mkStatement(m_pWorld.get(),
1811 : : i_xSubject, i_xPredicate, i_xObject),
1812 [ + - ][ + - ]: 16 : safe_librdf_free_statement);
1813 : : OSL_ENSURE(pStatement, "mkStatement failed");
1814 : :
1815 : : const boost::shared_ptr<librdf_stream> pStream(
1816 : : librdf_model_find_statements_in_context(m_pModel.get(),
1817 : : pStatement.get(), pContext.get()),
1818 [ + - ][ + - ]: 16 : safe_librdf_free_stream);
1819 [ - + ]: 16 : if (!pStream) {
1820 : : throw rdf::RepositoryException(
1821 : : "librdf_Repository::removeStatements: "
1822 [ # # ][ # # ]: 0 : "librdf_model_find_statements_in_context failed", *this);
1823 : : }
1824 : :
1825 [ + - ][ + - ]: 16 : if (!librdf_stream_end(pStream.get())) {
1826 [ + + ]: 20 : do {
1827 [ + - ]: 20 : librdf_statement *pStmt( librdf_stream_get_object(pStream.get()) );
1828 [ - + ]: 20 : if (!pStmt) {
1829 : : throw rdf::RepositoryException(
1830 : : "librdf_Repository::removeStatements: "
1831 [ # # ][ # # ]: 0 : "librdf_stream_get_object failed", *this);
1832 : : }
1833 [ - + ]: 20 : if (librdf_model_context_remove_statement(m_pModel.get(),
1834 [ + - ]: 20 : pContext.get(), pStmt)) {
1835 : : throw rdf::RepositoryException(
1836 : : "librdf_Repository::removeStatements: "
1837 [ # # ][ # # ]: 0 : "librdf_model_context_remove_statement failed", *this);
1838 : : }
1839 [ + - ]: 20 : } while (!librdf_stream_next(pStream.get()));
1840 [ + - ][ + - ]: 18 : }
[ + - ][ + - ]
1841 : : }
1842 : :
1843 : : uno::Reference< container::XEnumeration >
1844 : 302 : librdf_Repository::getStatementsGraph(
1845 : : const uno::Reference< rdf::XResource > & i_xSubject,
1846 : : const uno::Reference< rdf::XURI > & i_xPredicate,
1847 : : const uno::Reference< rdf::XNode > & i_xObject,
1848 : : const uno::Reference< rdf::XURI > & i_xGraphName,
1849 : : bool i_Internal)
1850 : : //throw (uno::RuntimeException, lang::IllegalArgumentException,
1851 : : // container::NoSuchElementException, rdf::RepositoryException)
1852 : : {
1853 : : // N.B.: if any of subject, predicate, object is an XMetadatable, and
1854 : : // has no metadata reference, then there cannot be any node in the graph
1855 : : // representing it; in order to prevent side effect
1856 : : // (ensureMetadataReference), check for this condition and return
1857 [ + - ][ + - ]: 906 : if (isMetadatableWithoutMetadata(i_xSubject) ||
[ + - ][ - + ]
[ - + ]
1858 [ + - ]: 302 : isMetadatableWithoutMetadata(i_xPredicate) ||
1859 [ + - ]: 302 : isMetadatableWithoutMetadata(i_xObject))
1860 : : {
1861 : : return new librdf_GraphResult(this, m_aMutex,
1862 : : ::boost::shared_ptr<librdf_stream>(),
1863 [ # # ][ # # ]: 0 : ::boost::shared_ptr<librdf_node>());
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ]
1864 : : }
1865 : :
1866 [ + - ]: 302 : ::osl::MutexGuard g(m_aMutex);
1867 [ + - ][ + - ]: 302 : const ::rtl::OUString contextU( i_xGraphName->getStringValue() );
1868 [ + + ][ + - ]: 302 : if (!i_Internal && (m_NamedGraphs.find(contextU) == m_NamedGraphs.end())) {
[ + + ][ + + ]
[ + + ][ + +
# # # # ]
1869 : : throw container::NoSuchElementException(
1870 : : "librdf_Repository::getStatements: "
1871 [ + - ][ + - ]: 2 : "no graph with given URI exists", *this);
1872 : : }
1873 : : const ::rtl::OString context(
1874 [ + - ]: 300 : ::rtl::OUStringToOString(contextU, RTL_TEXTENCODING_UTF8) );
1875 : :
1876 : : const boost::shared_ptr<librdf_node> pContext(
1877 : : librdf_new_node_from_uri_string(m_pWorld.get(),
1878 : 300 : reinterpret_cast<const unsigned char*> (context.getStr())),
1879 [ + - ][ + - ]: 300 : safe_librdf_free_node);
1880 [ - + ]: 300 : if (!pContext) {
1881 : : throw uno::RuntimeException(
1882 : : "librdf_Repository::getStatements: "
1883 [ # # ][ # # ]: 0 : "librdf_new_node_from_uri_string failed", *this);
1884 : : }
1885 : : const boost::shared_ptr<librdf_statement> pStatement(
1886 : : m_TypeConverter.mkStatement(m_pWorld.get(),
1887 : : i_xSubject, i_xPredicate, i_xObject),
1888 [ + - ][ + - ]: 300 : safe_librdf_free_statement);
1889 : : OSL_ENSURE(pStatement, "mkStatement failed");
1890 : :
1891 : : const boost::shared_ptr<librdf_stream> pStream(
1892 : : librdf_model_find_statements_in_context(m_pModel.get(),
1893 : : pStatement.get(), pContext.get()),
1894 [ + - ][ + - ]: 300 : safe_librdf_free_stream);
1895 [ - + ]: 300 : if (!pStream) {
1896 : : throw rdf::RepositoryException(
1897 : : "librdf_Repository::getStatements: "
1898 [ # # ][ # # ]: 0 : "librdf_model_find_statements_in_context failed", *this);
1899 : : }
1900 : :
1901 : : // librdf_model_find_statements_in_context is buggy and does not put
1902 : : // the context into result statements; pass it to librdf_GraphResult here
1903 [ + - ][ + - ]: 302 : return new librdf_GraphResult(this, m_aMutex, pStream, pContext);
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
1904 : : }
1905 : :
1906 : 20 : librdf_world *librdf_TypeConverter::createWorld() const
1907 : : {
1908 : : // create and initialize world
1909 : 20 : librdf_world *pWorld( librdf_new_world() );
1910 [ - + ]: 20 : if (!pWorld) {
1911 : : throw uno::RuntimeException(
1912 : : "librdf_TypeConverter::createWorld: librdf_new_world failed",
1913 [ # # ][ # # ]: 0 : m_rRep);
1914 : : }
1915 : : //FIXME logger, digest, features?
1916 : 20 : xsltSecurityPrefsPtr origprefs = xsltGetDefaultSecurityPrefs();
1917 : 20 : librdf_world_open(pWorld);
1918 : 20 : xsltSecurityPrefsPtr newprefs = xsltGetDefaultSecurityPrefs();
1919 [ - + ]: 20 : if (newprefs != origprefs) {
1920 : : // #i110523# restore libxslt global configuration
1921 : : // (gratuitously overwritten by raptor_init_parser_grddl_common)
1922 : : // (this is the only reason unordf is linked against libxslt)
1923 : 0 : xsltSetDefaultSecurityPrefs(origprefs);
1924 : : }
1925 : 20 : return pWorld;
1926 : : }
1927 : :
1928 : : librdf_storage *
1929 : 60 : librdf_TypeConverter::createStorage(librdf_world *i_pWorld) const
1930 : : {
1931 : : librdf_storage *pStorage(
1932 : : // librdf_new_storage(i_pWorld, "memory", NULL, "contexts='yes'") );
1933 : : librdf_new_storage(i_pWorld, "hashes", NULL,
1934 : 60 : "contexts='yes',hash-type='memory'") );
1935 [ - + ]: 60 : if (!pStorage) {
1936 : : throw uno::RuntimeException(
1937 : : "librdf_TypeConverter::createStorage: librdf_new_storage failed",
1938 [ # # ][ # # ]: 0 : m_rRep);
1939 : : }
1940 : 60 : return pStorage;
1941 : : }
1942 : :
1943 : 60 : librdf_model *librdf_TypeConverter::createModel(
1944 : : librdf_world *i_pWorld, librdf_storage * i_pStorage) const
1945 : : {
1946 : 60 : librdf_model *pRepository( librdf_new_model(i_pWorld, i_pStorage, NULL) );
1947 [ - + ]: 60 : if (!pRepository) {
1948 : : throw uno::RuntimeException(
1949 : : "librdf_TypeConverter::createModel: librdf_new_model failed",
1950 [ # # ][ # # ]: 0 : m_rRep);
1951 : : }
1952 : : //FIXME
1953 : : #if 0
1954 : : {
1955 : : librdf_uri * ctxt = librdf_new_uri(i_pWorld, reinterpret_cast<const unsigned char *>(LIBRDF_MODEL_FEATURE_CONTEXTS));
1956 : : librdf_node * contexts = librdf_model_get_feature(repository, ctxt);
1957 : : if (!contexts)
1958 : : throw;
1959 : : std::cout << "value of contexts feature: ";
1960 : : prtNode(contexts);
1961 : : std::cout << std::endl;
1962 : : // librdf_model_set_feature(repository, LIBRDF_FEATURE_CONTEXTS, ...);
1963 : : safe_librdf_free_node(contexts);
1964 : : safe_librdf_free_uri(ctxt);
1965 : : }
1966 : : #endif
1967 : 60 : return pRepository;
1968 : : }
1969 : :
1970 : : // this does NOT create a node, only URI
1971 : 24 : librdf_uri* librdf_TypeConverter::mkURI( librdf_world* i_pWorld,
1972 : : const uno::Reference< rdf::XURI > & i_xURI) const
1973 : : {
1974 : : const ::rtl::OString uri(
1975 [ + - ]: 24 : ::rtl::OUStringToOString(i_xURI->getStringValue(),
1976 [ + - ][ + - ]: 24 : RTL_TEXTENCODING_UTF8) );
1977 : : librdf_uri *pURI( librdf_new_uri(i_pWorld,
1978 [ + - ]: 24 : reinterpret_cast<const unsigned char *>(uri.getStr())));
1979 [ - + ]: 24 : if (!pURI) {
1980 : : throw uno::RuntimeException(
1981 [ # # ][ # # ]: 0 : "librdf_TypeConverter::mkURI: librdf_new_uri failed", 0);
1982 : : }
1983 : 24 : return pURI;
1984 : : }
1985 : :
1986 : : // create blank or URI node
1987 : 1986 : librdf_node* librdf_TypeConverter::mkResource( librdf_world* i_pWorld,
1988 : : const uno::Reference< rdf::XResource > & i_xResource) const
1989 : : {
1990 [ + + ]: 1986 : if (!i_xResource.is()) return 0;
1991 [ + - ]: 1566 : uno::Reference< rdf::XBlankNode > xBlankNode(i_xResource, uno::UNO_QUERY);
1992 [ + + ]: 1566 : if (xBlankNode.is()) {
1993 : : const ::rtl::OString label(
1994 [ + - ]: 14 : ::rtl::OUStringToOString(xBlankNode->getStringValue(),
1995 [ + - ][ + - ]: 14 : RTL_TEXTENCODING_UTF8) );
1996 : : librdf_node *pNode(
1997 : : librdf_new_node_from_blank_identifier(i_pWorld,
1998 [ + - ]: 14 : reinterpret_cast<const unsigned char*> (label.getStr())));
1999 [ - + ]: 14 : if (!pNode) {
2000 : : throw uno::RuntimeException(
2001 : : "librdf_TypeConverter::mkResource: "
2002 [ # # ][ # # ]: 0 : "librdf_new_node_from_blank_identifier failed", 0);
2003 : : }
2004 : 14 : return pNode;
2005 : : } else { // assumption: everything else is URI
2006 : : const ::rtl::OString uri(
2007 [ + - ]: 1552 : ::rtl::OUStringToOString(i_xResource->getStringValue(),
2008 [ + - ][ + - ]: 1552 : RTL_TEXTENCODING_UTF8) );
2009 : : librdf_node *pNode(
2010 : : librdf_new_node_from_uri_string(i_pWorld,
2011 [ + - ]: 1552 : reinterpret_cast<const unsigned char*> (uri.getStr())));
2012 [ - + ]: 1552 : if (!pNode) {
2013 : : throw uno::RuntimeException(
2014 : : "librdf_TypeConverter::mkResource: "
2015 [ # # ][ # # ]: 0 : "librdf_new_node_from_uri_string failed", 0);
2016 : : }
2017 : 1552 : return pNode;
2018 : 1986 : }
2019 : : }
2020 : :
2021 : : // create blank or URI or literal node
2022 : 838 : librdf_node* librdf_TypeConverter::mkNode( librdf_world* i_pWorld,
2023 : : const uno::Reference< rdf::XNode > & i_xNode) const
2024 : : {
2025 [ + + ]: 838 : if (!i_xNode.is()) return 0;
2026 [ + - ]: 412 : uno::Reference< rdf::XResource > xResource(i_xNode, uno::UNO_QUERY);
2027 [ + + ]: 412 : if (xResource.is()) {
2028 [ + - ]: 310 : return mkResource(i_pWorld, xResource);
2029 : : }
2030 [ + - ]: 102 : uno::Reference< rdf::XLiteral> xLiteral(i_xNode, uno::UNO_QUERY);
2031 : : OSL_ENSURE(xLiteral.is(),
2032 : : "mkNode: someone invented a new rdf.XNode and did not tell me");
2033 [ - + ]: 102 : if (!xLiteral.is()) return 0;
2034 : : const ::rtl::OString val(
2035 [ + - ]: 102 : ::rtl::OUStringToOString(xLiteral->getValue(),
2036 [ + - ][ + - ]: 102 : RTL_TEXTENCODING_UTF8) );
2037 : : const ::rtl::OString lang(
2038 [ + - ]: 102 : ::rtl::OUStringToOString(xLiteral->getLanguage(),
2039 [ + - ][ + - ]: 102 : RTL_TEXTENCODING_UTF8) );
2040 [ + - ][ + - ]: 102 : const uno::Reference< rdf::XURI > xType(xLiteral->getDatatype());
2041 : 102 : librdf_node * ret(0);
2042 [ + + ]: 102 : if (lang.isEmpty()) {
2043 [ + + ]: 98 : if (!xType.is()) {
2044 : : ret = librdf_new_node_from_literal(i_pWorld,
2045 : 74 : reinterpret_cast<const unsigned char*> (val.getStr()),
2046 [ + - ]: 74 : NULL, 0);
2047 : : } else {
2048 : : const boost::shared_ptr<librdf_uri> pDatatype(
2049 [ + - ][ + - ]: 24 : mkURI(i_pWorld, xType), safe_librdf_free_uri);
2050 : : ret = librdf_new_node_from_typed_literal(i_pWorld,
2051 : 24 : reinterpret_cast<const unsigned char*> (val.getStr()),
2052 [ + - ][ + - ]: 48 : NULL, pDatatype.get());
2053 : : }
2054 : : } else {
2055 [ + - ]: 4 : if (!xType.is()) {
2056 : : ret = librdf_new_node_from_literal(i_pWorld,
2057 : 4 : reinterpret_cast<const unsigned char*> (val.getStr()),
2058 [ + - ]: 8 : (lang.getStr()), 0);
2059 : :
2060 : : } else {
2061 : : OSL_FAIL("mkNode: invalid literal");
2062 : 0 : return 0;
2063 : : }
2064 : : }
2065 [ - + ]: 102 : if (!ret) {
2066 : : throw uno::RuntimeException(
2067 [ # # ][ # # ]: 0 : "librdf_TypeConverter::mkNode: librdf_new_node_from_literal failed", 0);
2068 : : }
2069 : 838 : return ret;
2070 : : }
2071 : :
2072 : 838 : librdf_statement* librdf_TypeConverter::mkStatement( librdf_world* i_pWorld,
2073 : : const uno::Reference< rdf::XResource > & i_xSubject,
2074 : : const uno::Reference< rdf::XURI > & i_xPredicate,
2075 : : const uno::Reference< rdf::XNode > & i_xObject) const
2076 : : {
2077 : 838 : librdf_node* pSubject( mkResource(i_pWorld, i_xSubject) );
2078 : 838 : librdf_node* pPredicate(0);
2079 : 838 : librdf_node* pObject(0);
2080 : : try {
2081 : : const uno::Reference<rdf::XResource> xPredicate(i_xPredicate,
2082 [ + - ]: 838 : uno::UNO_QUERY);
2083 [ + - ]: 838 : pPredicate = mkResource(i_pWorld, xPredicate);
2084 : : try {
2085 [ + - ]: 838 : pObject = mkNode(i_pWorld, i_xObject);
2086 : 0 : } catch (...) {
2087 [ # # ]: 0 : safe_librdf_free_node(pPredicate);
2088 : 0 : throw;
2089 : 838 : }
2090 : 0 : } catch (...) {
2091 [ # # ]: 0 : safe_librdf_free_node(pSubject);
2092 : 0 : throw;
2093 : : }
2094 : : // NB: this takes ownership of the nodes! (which is really ugly)
2095 : : librdf_statement* pStatement( librdf_new_statement_from_nodes(i_pWorld,
2096 : 838 : pSubject, pPredicate, pObject) );
2097 [ - + ]: 838 : if (!pStatement) {
2098 : : throw uno::RuntimeException(
2099 : : "librdf_TypeConverter::mkStatement: "
2100 [ # # ][ # # ]: 0 : "librdf_new_statement_from_nodes failed", 0);
2101 : : }
2102 : 838 : return pStatement;
2103 : : }
2104 : :
2105 : : uno::Reference<rdf::XURI>
2106 : 1696 : librdf_TypeConverter::convertToXURI(librdf_uri* i_pURI) const
2107 : : {
2108 [ - + ][ # # ]: 1696 : if (!i_pURI) return 0;
2109 [ + - ]: 1696 : const unsigned char* uri( librdf_uri_as_string(i_pURI) );
2110 [ - + ]: 1696 : if (!uri) {
2111 : : throw uno::RuntimeException(
2112 : : "librdf_TypeConverter::convertToXURI: "
2113 [ # # ][ # # ]: 0 : "librdf_uri_as_string failed", m_rRep);
2114 : : }
2115 : : ::rtl::OUString uriU( ::rtl::OStringToOUString(
2116 : : ::rtl::OString(reinterpret_cast<const sal_Char*>(uri)),
2117 [ + - ]: 1696 : RTL_TEXTENCODING_UTF8) );
2118 : : try {
2119 [ + - ]: 1696 : return rdf::URI::create(m_xContext, uriU);
2120 [ # # ]: 0 : } catch (const lang::IllegalArgumentException & iae) {
2121 : : throw lang::WrappedTargetRuntimeException(
2122 : : "librdf_TypeConverter::convertToXURI: "
2123 [ # # # # : 0 : "illegal uri", m_rRep, uno::makeAny(iae));
# # ]
2124 : 1696 : }
2125 : : }
2126 : :
2127 : : uno::Reference<rdf::XURI>
2128 : 1782 : librdf_TypeConverter::convertToXURI(librdf_node* i_pNode) const
2129 : : {
2130 [ + + ]: 1782 : if (!i_pNode) return 0;
2131 [ + - ]: 1658 : if (librdf_node_is_resource(i_pNode)) {
2132 : 1658 : librdf_uri* pURI( librdf_node_get_uri(i_pNode) );
2133 [ - + ]: 1658 : if (!pURI) {
2134 : : throw uno::RuntimeException(
2135 : : "librdf_TypeConverter::convertToXURI: "
2136 [ # # ][ # # ]: 0 : "resource has no uri", m_rRep);
2137 : : }
2138 : 1658 : return convertToXURI(pURI);
2139 : : } else {
2140 : : OSL_FAIL("convertToXURI: unknown librdf_node");
2141 : 1782 : return 0;
2142 : : }
2143 : : }
2144 : :
2145 : : uno::Reference<rdf::XResource>
2146 : 762 : librdf_TypeConverter::convertToXResource(librdf_node* i_pNode) const
2147 : : {
2148 [ - + ]: 762 : if (!i_pNode) return 0;
2149 [ + + ]: 762 : if (librdf_node_is_blank(i_pNode)) {
2150 [ + - ]: 32 : const unsigned char* label( librdf_node_get_blank_identifier(i_pNode) );
2151 [ - + ]: 32 : if (!label) {
2152 : : throw uno::RuntimeException(
2153 : : "librdf_TypeConverter::convertToXResource: "
2154 [ # # ][ # # ]: 0 : "blank node has no label", m_rRep);
2155 : : }
2156 : : ::rtl::OUString labelU( ::rtl::OStringToOUString(
2157 : : ::rtl::OString(reinterpret_cast<const sal_Char*>(label)),
2158 [ + - ]: 32 : RTL_TEXTENCODING_UTF8) );
2159 : : try {
2160 : : return uno::Reference<rdf::XResource>(
2161 [ + - ][ + - ]: 32 : rdf::BlankNode::create(m_xContext, labelU), uno::UNO_QUERY);
2162 [ # # ]: 0 : } catch (const lang::IllegalArgumentException & iae) {
2163 : : throw lang::WrappedTargetRuntimeException(
2164 : : "librdf_TypeConverter::convertToXResource: "
2165 [ # # # # : 0 : "illegal blank node label", m_rRep, uno::makeAny(iae));
# # ]
2166 : 32 : }
2167 : : } else {
2168 : : return uno::Reference<rdf::XResource>(convertToXURI(i_pNode),
2169 [ + - ]: 762 : uno::UNO_QUERY);
2170 : : }
2171 : : }
2172 : :
2173 : : uno::Reference<rdf::XNode>
2174 : 544 : librdf_TypeConverter::convertToXNode(librdf_node* i_pNode) const
2175 : : {
2176 [ - + ][ # # ]: 544 : if (!i_pNode) return 0;
2177 [ + - ][ + + ]: 544 : if (!librdf_node_is_literal(i_pNode)) {
2178 : : return uno::Reference<rdf::XNode>(convertToXResource(i_pNode),
2179 [ + - ][ + - ]: 236 : uno::UNO_QUERY);
2180 : : }
2181 [ + - ]: 308 : const unsigned char* value( librdf_node_get_literal_value(i_pNode) );
2182 [ - + ]: 308 : if (!value) {
2183 : : throw uno::RuntimeException(
2184 : : "librdf_TypeConverter::convertToXNode: "
2185 [ # # ][ # # ]: 0 : "literal has no value", m_rRep);
2186 : : }
2187 [ + - ]: 308 : const char * lang( librdf_node_get_literal_value_language(i_pNode) );
2188 : : librdf_uri* pType(
2189 [ + - ]: 308 : librdf_node_get_literal_value_datatype_uri(i_pNode) );
2190 : : OSL_ENSURE(!lang || !pType, "convertToXNode: invalid literal");
2191 : : const ::rtl::OUString valueU( ::rtl::OStringToOUString(
2192 : : ::rtl::OString(reinterpret_cast<const sal_Char*>(value)),
2193 [ + - ]: 308 : RTL_TEXTENCODING_UTF8) );
2194 [ + + ]: 308 : if (lang) {
2195 : : const ::rtl::OUString langU( ::rtl::OStringToOUString(
2196 : : ::rtl::OString(reinterpret_cast<const sal_Char*>(lang)),
2197 [ + - ]: 4 : RTL_TEXTENCODING_UTF8) );
2198 : : return uno::Reference<rdf::XNode>(
2199 : : rdf::Literal::createWithLanguage(m_xContext, valueU, langU),
2200 [ + - ][ + - ]: 4 : uno::UNO_QUERY);
2201 [ + + ]: 304 : } else if (pType) {
2202 [ + - ]: 38 : uno::Reference<rdf::XURI> xType(convertToXURI(pType));
2203 : : OSL_ENSURE(xType.is(), "convertToXNode: null uri");
2204 : : return uno::Reference<rdf::XNode>(
2205 : : rdf::Literal::createWithType(m_xContext, valueU, xType),
2206 [ + - ][ + - ]: 38 : uno::UNO_QUERY);
2207 : : } else {
2208 : : return uno::Reference<rdf::XNode>(
2209 : : rdf::Literal::create(m_xContext, valueU),
2210 [ + - ][ + - ]: 266 : uno::UNO_QUERY);
2211 : 544 : }
2212 : : }
2213 : :
2214 : : rdf::Statement
2215 : 526 : librdf_TypeConverter::convertToStatement(librdf_statement* i_pStmt,
2216 : : librdf_node* i_pContext) const
2217 : : {
2218 [ - + ]: 526 : if (!i_pStmt) {
2219 [ # # ]: 0 : throw uno::RuntimeException();
2220 : : }
2221 : : return rdf::Statement(
2222 : : convertToXResource(librdf_statement_get_subject(i_pStmt)),
2223 : : convertToXURI(librdf_statement_get_predicate(i_pStmt)),
2224 : : convertToXNode(librdf_statement_get_object(i_pStmt)),
2225 [ + - ][ + - ]: 526 : convertToXURI(i_pContext));
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
2226 : : }
2227 : :
2228 : : } // closing anonymous implementation namespace
2229 : :
2230 : :
2231 : :
2232 : : // component helper namespace
2233 : : namespace comp_librdf_Repository {
2234 : :
2235 : 50 : ::rtl::OUString SAL_CALL _getImplementationName() {
2236 : 50 : return rtl::OUString("librdf_Repository");
2237 : : }
2238 : :
2239 : 20 : uno::Sequence< ::rtl::OUString > SAL_CALL _getSupportedServiceNames()
2240 : : {
2241 : 20 : uno::Sequence< ::rtl::OUString > s(1);
2242 [ + - ]: 20 : s[0] = "com.sun.star.rdf.Repository";
2243 : 20 : return s;
2244 : : }
2245 : :
2246 : 60 : uno::Reference< uno::XInterface > SAL_CALL _create(
2247 : : const uno::Reference< uno::XComponentContext > & context)
2248 : : SAL_THROW((uno::Exception))
2249 : : {
2250 [ + - ]: 60 : return static_cast< ::cppu::OWeakObject * >(new librdf_Repository(context));
2251 : : }
2252 : :
2253 [ + - ][ + - ]: 60 : } // closing component helper namespace
2254 : :
2255 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|