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