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