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