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