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 // MARKER(update_precomp.py): autogen include statement, do not remove 29*cdf0e10cSrcweir #include "precompiled_desktop.hxx" 30*cdf0e10cSrcweir 31*cdf0e10cSrcweir #include "rtl/string.h" 32*cdf0e10cSrcweir #include "rtl/strbuf.hxx" 33*cdf0e10cSrcweir #include "rtl/bootstrap.hxx" 34*cdf0e10cSrcweir #include "cppuhelper/exc_hlp.hxx" 35*cdf0e10cSrcweir #include "osl/file.hxx" 36*cdf0e10cSrcweir #include "com/sun/star/uno/XComponentContext.hpp" 37*cdf0e10cSrcweir #include "com/sun/star/xml/dom/XDocumentBuilder.hpp" 38*cdf0e10cSrcweir #include "com/sun/star/xml/xpath/XXPathAPI.hpp" 39*cdf0e10cSrcweir #include "com/sun/star/io/XActiveDataSource.hpp" 40*cdf0e10cSrcweir #include "com/sun/star/io/XActiveDataControl.hpp" 41*cdf0e10cSrcweir #include "dp_ucb.h" 42*cdf0e10cSrcweir #include "dp_misc.h" 43*cdf0e10cSrcweir #include "ucbhelper/content.hxx" 44*cdf0e10cSrcweir #include "xmlscript/xml_helper.hxx" 45*cdf0e10cSrcweir #include "dp_backenddb.hxx" 46*cdf0e10cSrcweir 47*cdf0e10cSrcweir 48*cdf0e10cSrcweir namespace css = ::com::sun::star; 49*cdf0e10cSrcweir using namespace ::com::sun::star::uno; 50*cdf0e10cSrcweir using ::rtl::OUString; 51*cdf0e10cSrcweir 52*cdf0e10cSrcweir 53*cdf0e10cSrcweir namespace dp_registry { 54*cdf0e10cSrcweir namespace backend { 55*cdf0e10cSrcweir 56*cdf0e10cSrcweir BackendDb::BackendDb( 57*cdf0e10cSrcweir Reference<css::uno::XComponentContext> const & xContext, 58*cdf0e10cSrcweir ::rtl::OUString const & url): 59*cdf0e10cSrcweir m_xContext(xContext) 60*cdf0e10cSrcweir { 61*cdf0e10cSrcweir m_urlDb = dp_misc::expandUnoRcUrl(url); 62*cdf0e10cSrcweir } 63*cdf0e10cSrcweir 64*cdf0e10cSrcweir void BackendDb::save() 65*cdf0e10cSrcweir { 66*cdf0e10cSrcweir const Reference<css::io::XActiveDataSource> xDataSource(m_doc,css::uno::UNO_QUERY_THROW); 67*cdf0e10cSrcweir ::rtl::ByteSequence bytes; 68*cdf0e10cSrcweir xDataSource->setOutputStream(::xmlscript::createOutputStream(&bytes)); 69*cdf0e10cSrcweir const Reference<css::io::XActiveDataControl> xDataControl(m_doc,css::uno::UNO_QUERY_THROW); 70*cdf0e10cSrcweir xDataControl->start(); 71*cdf0e10cSrcweir 72*cdf0e10cSrcweir const Reference<css::io::XInputStream> xData( 73*cdf0e10cSrcweir ::xmlscript::createInputStream(bytes)); 74*cdf0e10cSrcweir ::ucbhelper::Content ucbDb(m_urlDb, 0); 75*cdf0e10cSrcweir ucbDb.writeStream(xData, true /*replace existing*/); 76*cdf0e10cSrcweir } 77*cdf0e10cSrcweir 78*cdf0e10cSrcweir css::uno::Reference<css::xml::dom::XDocument> BackendDb::getDocument() 79*cdf0e10cSrcweir { 80*cdf0e10cSrcweir if (!m_doc.is()) 81*cdf0e10cSrcweir { 82*cdf0e10cSrcweir const Reference<css::xml::dom::XDocumentBuilder> xDocBuilder( 83*cdf0e10cSrcweir m_xContext->getServiceManager()->createInstanceWithContext( 84*cdf0e10cSrcweir OUSTR("com.sun.star.xml.dom.DocumentBuilder"), 85*cdf0e10cSrcweir m_xContext ), css::uno::UNO_QUERY); 86*cdf0e10cSrcweir if (!xDocBuilder.is()) 87*cdf0e10cSrcweir throw css::uno::RuntimeException( 88*cdf0e10cSrcweir OUSTR(" Could not create service com.sun.star.xml.dom.DocumentBuilder"), 0); 89*cdf0e10cSrcweir 90*cdf0e10cSrcweir ::osl::DirectoryItem item; 91*cdf0e10cSrcweir ::osl::File::RC err = ::osl::DirectoryItem::get(m_urlDb, item); 92*cdf0e10cSrcweir if (err == ::osl::File::E_None) 93*cdf0e10cSrcweir { 94*cdf0e10cSrcweir ::ucbhelper::Content descContent( 95*cdf0e10cSrcweir m_urlDb, css::uno::Reference<css::ucb::XCommandEnvironment>()); 96*cdf0e10cSrcweir Reference<css::io::XInputStream> xIn = descContent.openStream(); 97*cdf0e10cSrcweir m_doc = xDocBuilder->parse(xIn); 98*cdf0e10cSrcweir } 99*cdf0e10cSrcweir else if (err == ::osl::File::E_NOENT) 100*cdf0e10cSrcweir { 101*cdf0e10cSrcweir //Create a new document and insert some basic stuff 102*cdf0e10cSrcweir m_doc = xDocBuilder->newDocument(); 103*cdf0e10cSrcweir const Reference<css::xml::dom::XElement> rootNode = 104*cdf0e10cSrcweir m_doc->createElementNS(getDbNSName(), getNSPrefix() + 105*cdf0e10cSrcweir OUSTR(":") + getRootElementName()); 106*cdf0e10cSrcweir 107*cdf0e10cSrcweir m_doc->appendChild(Reference<css::xml::dom::XNode>( 108*cdf0e10cSrcweir rootNode, UNO_QUERY_THROW)); 109*cdf0e10cSrcweir save(); 110*cdf0e10cSrcweir } 111*cdf0e10cSrcweir else 112*cdf0e10cSrcweir throw css::uno::RuntimeException( 113*cdf0e10cSrcweir OUSTR("Extension manager could not access database file:" ) 114*cdf0e10cSrcweir + m_urlDb, 0); 115*cdf0e10cSrcweir 116*cdf0e10cSrcweir if (!m_doc.is()) 117*cdf0e10cSrcweir throw css::uno::RuntimeException( 118*cdf0e10cSrcweir OUSTR("Extension manager could not get root node of data base file: ") 119*cdf0e10cSrcweir + m_urlDb, 0); 120*cdf0e10cSrcweir } 121*cdf0e10cSrcweir 122*cdf0e10cSrcweir return m_doc; 123*cdf0e10cSrcweir } 124*cdf0e10cSrcweir 125*cdf0e10cSrcweir Reference<css::xml::xpath::XXPathAPI> BackendDb::getXPathAPI() 126*cdf0e10cSrcweir { 127*cdf0e10cSrcweir if (!m_xpathApi.is()) 128*cdf0e10cSrcweir { 129*cdf0e10cSrcweir m_xpathApi = Reference< css::xml::xpath::XXPathAPI >( 130*cdf0e10cSrcweir m_xContext->getServiceManager()->createInstanceWithContext( 131*cdf0e10cSrcweir OUSTR("com.sun.star.xml.xpath.XPathAPI"), 132*cdf0e10cSrcweir m_xContext), css::uno::UNO_QUERY); 133*cdf0e10cSrcweir 134*cdf0e10cSrcweir if (!m_xpathApi.is()) 135*cdf0e10cSrcweir throw css::uno::RuntimeException( 136*cdf0e10cSrcweir OUSTR(" Could not create service com.sun.star.xml.xpath.XPathAPI"), 0); 137*cdf0e10cSrcweir 138*cdf0e10cSrcweir m_xpathApi->registerNS( 139*cdf0e10cSrcweir getNSPrefix(), getDbNSName()); 140*cdf0e10cSrcweir } 141*cdf0e10cSrcweir 142*cdf0e10cSrcweir return m_xpathApi; 143*cdf0e10cSrcweir } 144*cdf0e10cSrcweir 145*cdf0e10cSrcweir void BackendDb::removeElement(::rtl::OUString const & sXPathExpression) 146*cdf0e10cSrcweir { 147*cdf0e10cSrcweir try 148*cdf0e10cSrcweir { 149*cdf0e10cSrcweir const Reference<css::xml::dom::XDocument> doc = getDocument(); 150*cdf0e10cSrcweir const Reference<css::xml::dom::XNode> root = doc->getFirstChild(); 151*cdf0e10cSrcweir const Reference<css::xml::xpath::XXPathAPI> xpathApi = getXPathAPI(); 152*cdf0e10cSrcweir //find the extension element that is to be removed 153*cdf0e10cSrcweir const Reference<css::xml::dom::XNode> aNode = 154*cdf0e10cSrcweir xpathApi->selectSingleNode(root, sXPathExpression); 155*cdf0e10cSrcweir 156*cdf0e10cSrcweir if (aNode.is()) 157*cdf0e10cSrcweir { 158*cdf0e10cSrcweir root->removeChild(aNode); 159*cdf0e10cSrcweir save(); 160*cdf0e10cSrcweir } 161*cdf0e10cSrcweir 162*cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 0 163*cdf0e10cSrcweir //There must not be any other entry with the same url 164*cdf0e10cSrcweir const Reference<css::xml::dom::XNode> nextNode = 165*cdf0e10cSrcweir xpathApi->selectSingleNode(root, sXPathExpression); 166*cdf0e10cSrcweir OSL_ASSERT(! nextNode.is()); 167*cdf0e10cSrcweir #endif 168*cdf0e10cSrcweir } 169*cdf0e10cSrcweir catch(css::uno::Exception &) 170*cdf0e10cSrcweir { 171*cdf0e10cSrcweir Any exc( ::cppu::getCaughtException() ); 172*cdf0e10cSrcweir throw css::deployment::DeploymentException( 173*cdf0e10cSrcweir OUSTR("Extension Manager: failed to write data entry in backend db: ") + 174*cdf0e10cSrcweir m_urlDb, 0, exc); 175*cdf0e10cSrcweir } 176*cdf0e10cSrcweir } 177*cdf0e10cSrcweir 178*cdf0e10cSrcweir void BackendDb::removeEntry(::rtl::OUString const & url) 179*cdf0e10cSrcweir { 180*cdf0e10cSrcweir const OUString sKeyElement = getKeyElementName(); 181*cdf0e10cSrcweir const OUString sPrefix = getNSPrefix(); 182*cdf0e10cSrcweir ::rtl::OUStringBuffer sExpression(500); 183*cdf0e10cSrcweir sExpression.append(sPrefix); 184*cdf0e10cSrcweir sExpression.appendAscii(":"); 185*cdf0e10cSrcweir sExpression.append(sKeyElement); 186*cdf0e10cSrcweir sExpression.append(OUSTR("[@url = \"")); 187*cdf0e10cSrcweir sExpression.append(url); 188*cdf0e10cSrcweir sExpression.appendAscii("\"]"); 189*cdf0e10cSrcweir 190*cdf0e10cSrcweir removeElement(sExpression.makeStringAndClear()); 191*cdf0e10cSrcweir } 192*cdf0e10cSrcweir 193*cdf0e10cSrcweir void BackendDb::revokeEntry(::rtl::OUString const & url) 194*cdf0e10cSrcweir { 195*cdf0e10cSrcweir try 196*cdf0e10cSrcweir { 197*cdf0e10cSrcweir Reference<css::xml::dom::XElement> entry = Reference<css::xml::dom::XElement>(getKeyElement(url), UNO_QUERY); 198*cdf0e10cSrcweir if (entry.is()) 199*cdf0e10cSrcweir { 200*cdf0e10cSrcweir entry->setAttribute(OUSTR("revoked"), OUSTR("true")); 201*cdf0e10cSrcweir save(); 202*cdf0e10cSrcweir } 203*cdf0e10cSrcweir } 204*cdf0e10cSrcweir catch(css::uno::Exception &) 205*cdf0e10cSrcweir { 206*cdf0e10cSrcweir Any exc( ::cppu::getCaughtException() ); 207*cdf0e10cSrcweir throw css::deployment::DeploymentException( 208*cdf0e10cSrcweir OUSTR("Extension Manager: failed to revoke data entry in backend db: ") + 209*cdf0e10cSrcweir m_urlDb, 0, exc); 210*cdf0e10cSrcweir } 211*cdf0e10cSrcweir } 212*cdf0e10cSrcweir 213*cdf0e10cSrcweir bool BackendDb::activateEntry(::rtl::OUString const & url) 214*cdf0e10cSrcweir { 215*cdf0e10cSrcweir try 216*cdf0e10cSrcweir { 217*cdf0e10cSrcweir bool ret = false; 218*cdf0e10cSrcweir Reference<css::xml::dom::XElement> entry = Reference<css::xml::dom::XElement>(getKeyElement(url), UNO_QUERY); 219*cdf0e10cSrcweir if (entry.is()) 220*cdf0e10cSrcweir { 221*cdf0e10cSrcweir //no attribute "active" means it is active, that is, registered. 222*cdf0e10cSrcweir entry->removeAttribute(OUSTR("revoked")); 223*cdf0e10cSrcweir save(); 224*cdf0e10cSrcweir ret = true; 225*cdf0e10cSrcweir } 226*cdf0e10cSrcweir return ret; 227*cdf0e10cSrcweir } 228*cdf0e10cSrcweir catch(css::uno::Exception &) 229*cdf0e10cSrcweir { 230*cdf0e10cSrcweir Any exc( ::cppu::getCaughtException() ); 231*cdf0e10cSrcweir throw css::deployment::DeploymentException( 232*cdf0e10cSrcweir OUSTR("Extension Manager: failed to revoke data entry in backend db: ") + 233*cdf0e10cSrcweir m_urlDb, 0, exc); 234*cdf0e10cSrcweir } 235*cdf0e10cSrcweir } 236*cdf0e10cSrcweir 237*cdf0e10cSrcweir bool BackendDb::hasActiveEntry(::rtl::OUString const & url) 238*cdf0e10cSrcweir { 239*cdf0e10cSrcweir try 240*cdf0e10cSrcweir { 241*cdf0e10cSrcweir bool ret = false; 242*cdf0e10cSrcweir Reference<css::xml::dom::XElement> entry = Reference<css::xml::dom::XElement>(getKeyElement(url), UNO_QUERY); 243*cdf0e10cSrcweir if (entry.is()) 244*cdf0e10cSrcweir { 245*cdf0e10cSrcweir OUString sActive = entry->getAttribute(OUSTR("revoked")); 246*cdf0e10cSrcweir if (!sActive.equals(OUSTR("true"))) 247*cdf0e10cSrcweir ret = true; 248*cdf0e10cSrcweir } 249*cdf0e10cSrcweir return ret; 250*cdf0e10cSrcweir 251*cdf0e10cSrcweir } 252*cdf0e10cSrcweir catch(css::uno::Exception &) 253*cdf0e10cSrcweir { 254*cdf0e10cSrcweir Any exc( ::cppu::getCaughtException() ); 255*cdf0e10cSrcweir throw css::deployment::DeploymentException( 256*cdf0e10cSrcweir OUSTR("Extension Manager: failed to determine an active entry in backend db: ") + 257*cdf0e10cSrcweir m_urlDb, 0, exc); 258*cdf0e10cSrcweir } 259*cdf0e10cSrcweir } 260*cdf0e10cSrcweir 261*cdf0e10cSrcweir Reference<css::xml::dom::XNode> BackendDb::getKeyElement( 262*cdf0e10cSrcweir ::rtl::OUString const & url) 263*cdf0e10cSrcweir { 264*cdf0e10cSrcweir try 265*cdf0e10cSrcweir { 266*cdf0e10cSrcweir const OUString sPrefix = getNSPrefix(); 267*cdf0e10cSrcweir const OUString sKeyElement = getKeyElementName(); 268*cdf0e10cSrcweir ::rtl::OUStringBuffer sExpression(500); 269*cdf0e10cSrcweir sExpression.append(sPrefix); 270*cdf0e10cSrcweir sExpression.appendAscii(":"); 271*cdf0e10cSrcweir sExpression.append(sKeyElement); 272*cdf0e10cSrcweir sExpression.append(OUSTR("[@url = \"")); 273*cdf0e10cSrcweir sExpression.append(url); 274*cdf0e10cSrcweir sExpression.appendAscii("\"]"); 275*cdf0e10cSrcweir 276*cdf0e10cSrcweir const Reference<css::xml::dom::XDocument> doc = getDocument(); 277*cdf0e10cSrcweir const Reference<css::xml::dom::XNode> root = doc->getFirstChild(); 278*cdf0e10cSrcweir const Reference<css::xml::xpath::XXPathAPI> xpathApi = getXPathAPI(); 279*cdf0e10cSrcweir return xpathApi->selectSingleNode(root, sExpression.makeStringAndClear()); 280*cdf0e10cSrcweir } 281*cdf0e10cSrcweir catch(css::uno::Exception &) 282*cdf0e10cSrcweir { 283*cdf0e10cSrcweir Any exc( ::cppu::getCaughtException() ); 284*cdf0e10cSrcweir throw css::deployment::DeploymentException( 285*cdf0e10cSrcweir OUSTR("Extension Manager: failed to read key element in backend db: ") + 286*cdf0e10cSrcweir m_urlDb, 0, exc); 287*cdf0e10cSrcweir } 288*cdf0e10cSrcweir } 289*cdf0e10cSrcweir 290*cdf0e10cSrcweir //Only writes the data if there is at least one entry 291*cdf0e10cSrcweir void BackendDb::writeVectorOfPair( 292*cdf0e10cSrcweir ::std::vector< ::std::pair< ::rtl::OUString, ::rtl::OUString > > const & vecPairs, 293*cdf0e10cSrcweir OUString const & sVectorTagName, 294*cdf0e10cSrcweir OUString const & sPairTagName, 295*cdf0e10cSrcweir OUString const & sFirstTagName, 296*cdf0e10cSrcweir OUString const & sSecondTagName, 297*cdf0e10cSrcweir css::uno::Reference<css::xml::dom::XNode> const & xParent) 298*cdf0e10cSrcweir { 299*cdf0e10cSrcweir try{ 300*cdf0e10cSrcweir if (vecPairs.size() == 0) 301*cdf0e10cSrcweir return; 302*cdf0e10cSrcweir const OUString sNameSpace = getDbNSName(); 303*cdf0e10cSrcweir OSL_ASSERT(sNameSpace.getLength()); 304*cdf0e10cSrcweir const OUString sPrefix(getNSPrefix() + OUSTR(":")); 305*cdf0e10cSrcweir const Reference<css::xml::dom::XDocument> doc = getDocument(); 306*cdf0e10cSrcweir const Reference<css::xml::dom::XNode> root = doc->getFirstChild(); 307*cdf0e10cSrcweir 308*cdf0e10cSrcweir const Reference<css::xml::dom::XElement> vectorNode( 309*cdf0e10cSrcweir doc->createElementNS(sNameSpace, sPrefix + sVectorTagName)); 310*cdf0e10cSrcweir 311*cdf0e10cSrcweir xParent->appendChild( 312*cdf0e10cSrcweir Reference<css::xml::dom::XNode>( 313*cdf0e10cSrcweir vectorNode, css::uno::UNO_QUERY_THROW)); 314*cdf0e10cSrcweir typedef ::std::vector< ::std::pair< OUString, OUString > >::const_iterator CIT; 315*cdf0e10cSrcweir for (CIT i = vecPairs.begin(); i != vecPairs.end(); i++) 316*cdf0e10cSrcweir { 317*cdf0e10cSrcweir const Reference<css::xml::dom::XElement> pairNode( 318*cdf0e10cSrcweir doc->createElementNS(sNameSpace, sPrefix + sPairTagName)); 319*cdf0e10cSrcweir 320*cdf0e10cSrcweir vectorNode->appendChild( 321*cdf0e10cSrcweir Reference<css::xml::dom::XNode>( 322*cdf0e10cSrcweir pairNode, css::uno::UNO_QUERY_THROW)); 323*cdf0e10cSrcweir 324*cdf0e10cSrcweir const Reference<css::xml::dom::XElement> firstNode( 325*cdf0e10cSrcweir doc->createElementNS(sNameSpace, sPrefix + sFirstTagName)); 326*cdf0e10cSrcweir 327*cdf0e10cSrcweir pairNode->appendChild( 328*cdf0e10cSrcweir Reference<css::xml::dom::XNode>( 329*cdf0e10cSrcweir firstNode, css::uno::UNO_QUERY_THROW)); 330*cdf0e10cSrcweir 331*cdf0e10cSrcweir const Reference<css::xml::dom::XText> firstTextNode( 332*cdf0e10cSrcweir doc->createTextNode( i->first)); 333*cdf0e10cSrcweir 334*cdf0e10cSrcweir firstNode->appendChild( 335*cdf0e10cSrcweir Reference<css::xml::dom::XNode>( 336*cdf0e10cSrcweir firstTextNode, css::uno::UNO_QUERY_THROW)); 337*cdf0e10cSrcweir 338*cdf0e10cSrcweir const Reference<css::xml::dom::XElement> secondNode( 339*cdf0e10cSrcweir doc->createElementNS(sNameSpace, sPrefix + sSecondTagName)); 340*cdf0e10cSrcweir 341*cdf0e10cSrcweir pairNode->appendChild( 342*cdf0e10cSrcweir Reference<css::xml::dom::XNode>( 343*cdf0e10cSrcweir secondNode, css::uno::UNO_QUERY_THROW)); 344*cdf0e10cSrcweir 345*cdf0e10cSrcweir const Reference<css::xml::dom::XText> secondTextNode( 346*cdf0e10cSrcweir doc->createTextNode( i->second)); 347*cdf0e10cSrcweir 348*cdf0e10cSrcweir secondNode->appendChild( 349*cdf0e10cSrcweir Reference<css::xml::dom::XNode>( 350*cdf0e10cSrcweir secondTextNode, css::uno::UNO_QUERY_THROW)); 351*cdf0e10cSrcweir } 352*cdf0e10cSrcweir } 353*cdf0e10cSrcweir catch(css::uno::Exception &) 354*cdf0e10cSrcweir { 355*cdf0e10cSrcweir Any exc( ::cppu::getCaughtException() ); 356*cdf0e10cSrcweir throw css::deployment::DeploymentException( 357*cdf0e10cSrcweir OUSTR("Extension Manager: failed to write data entry in backend db: ") + 358*cdf0e10cSrcweir m_urlDb, 0, exc); 359*cdf0e10cSrcweir } 360*cdf0e10cSrcweir } 361*cdf0e10cSrcweir 362*cdf0e10cSrcweir ::std::vector< ::std::pair< OUString, OUString > > 363*cdf0e10cSrcweir BackendDb::readVectorOfPair( 364*cdf0e10cSrcweir Reference<css::xml::dom::XNode> const & parent, 365*cdf0e10cSrcweir OUString const & sListTagName, 366*cdf0e10cSrcweir OUString const & sPairTagName, 367*cdf0e10cSrcweir OUString const & sFirstTagName, 368*cdf0e10cSrcweir OUString const & sSecondTagName) 369*cdf0e10cSrcweir { 370*cdf0e10cSrcweir try 371*cdf0e10cSrcweir { 372*cdf0e10cSrcweir OSL_ASSERT(parent.is()); 373*cdf0e10cSrcweir const OUString sPrefix(getNSPrefix() + OUSTR(":")); 374*cdf0e10cSrcweir const Reference<css::xml::xpath::XXPathAPI> xpathApi = getXPathAPI(); 375*cdf0e10cSrcweir const OUString sExprPairs( 376*cdf0e10cSrcweir sPrefix + sListTagName + OUSTR("/") + sPrefix + sPairTagName); 377*cdf0e10cSrcweir const Reference<css::xml::dom::XNodeList> listPairs = 378*cdf0e10cSrcweir xpathApi->selectNodeList(parent, sExprPairs); 379*cdf0e10cSrcweir 380*cdf0e10cSrcweir ::std::vector< ::std::pair< OUString, OUString > > retVector; 381*cdf0e10cSrcweir sal_Int32 length = listPairs->getLength(); 382*cdf0e10cSrcweir for (sal_Int32 i = 0; i < length; i++) 383*cdf0e10cSrcweir { 384*cdf0e10cSrcweir const Reference<css::xml::dom::XNode> aPair = listPairs->item(i); 385*cdf0e10cSrcweir const OUString sExprFirst(sPrefix + sFirstTagName + OUSTR("/text()")); 386*cdf0e10cSrcweir const Reference<css::xml::dom::XNode> first = 387*cdf0e10cSrcweir xpathApi->selectSingleNode(aPair, sExprFirst); 388*cdf0e10cSrcweir 389*cdf0e10cSrcweir const OUString sExprSecond(sPrefix + sSecondTagName + OUSTR("/text()")); 390*cdf0e10cSrcweir const Reference<css::xml::dom::XNode> second = 391*cdf0e10cSrcweir xpathApi->selectSingleNode(aPair, sExprSecond); 392*cdf0e10cSrcweir OSL_ASSERT(first.is() && second.is()); 393*cdf0e10cSrcweir 394*cdf0e10cSrcweir retVector.push_back(::std::make_pair( 395*cdf0e10cSrcweir first->getNodeValue(), second->getNodeValue())); 396*cdf0e10cSrcweir } 397*cdf0e10cSrcweir return retVector; 398*cdf0e10cSrcweir } 399*cdf0e10cSrcweir catch(css::uno::Exception &) 400*cdf0e10cSrcweir { 401*cdf0e10cSrcweir Any exc( ::cppu::getCaughtException() ); 402*cdf0e10cSrcweir throw css::deployment::DeploymentException( 403*cdf0e10cSrcweir OUSTR("Extension Manager: failed to read data entry in backend db: ") + 404*cdf0e10cSrcweir m_urlDb, 0, exc); 405*cdf0e10cSrcweir } 406*cdf0e10cSrcweir } 407*cdf0e10cSrcweir 408*cdf0e10cSrcweir //Only writes the data if there is at least one entry 409*cdf0e10cSrcweir void BackendDb::writeSimpleList( 410*cdf0e10cSrcweir ::std::list< ::rtl::OUString> const & list, 411*cdf0e10cSrcweir OUString const & sListTagName, 412*cdf0e10cSrcweir OUString const & sMemberTagName, 413*cdf0e10cSrcweir Reference<css::xml::dom::XNode> const & xParent) 414*cdf0e10cSrcweir { 415*cdf0e10cSrcweir try 416*cdf0e10cSrcweir { 417*cdf0e10cSrcweir if (list.size() == 0) 418*cdf0e10cSrcweir return; 419*cdf0e10cSrcweir const OUString sNameSpace = getDbNSName(); 420*cdf0e10cSrcweir const OUString sPrefix(getNSPrefix() + OUSTR(":")); 421*cdf0e10cSrcweir const Reference<css::xml::dom::XDocument> doc = getDocument(); 422*cdf0e10cSrcweir 423*cdf0e10cSrcweir const Reference<css::xml::dom::XElement> listNode( 424*cdf0e10cSrcweir doc->createElementNS(sNameSpace, sPrefix + sListTagName)); 425*cdf0e10cSrcweir 426*cdf0e10cSrcweir xParent->appendChild( 427*cdf0e10cSrcweir Reference<css::xml::dom::XNode>( 428*cdf0e10cSrcweir listNode, css::uno::UNO_QUERY_THROW)); 429*cdf0e10cSrcweir 430*cdf0e10cSrcweir typedef ::std::list<OUString>::const_iterator ITC_ITEMS; 431*cdf0e10cSrcweir for (ITC_ITEMS i = list.begin(); i != list.end(); i++) 432*cdf0e10cSrcweir { 433*cdf0e10cSrcweir const Reference<css::xml::dom::XNode> memberNode( 434*cdf0e10cSrcweir doc->createElementNS(sNameSpace, sPrefix + sMemberTagName), css::uno::UNO_QUERY_THROW); 435*cdf0e10cSrcweir 436*cdf0e10cSrcweir listNode->appendChild(memberNode); 437*cdf0e10cSrcweir 438*cdf0e10cSrcweir const Reference<css::xml::dom::XNode> textNode( 439*cdf0e10cSrcweir doc->createTextNode( *i), css::uno::UNO_QUERY_THROW); 440*cdf0e10cSrcweir 441*cdf0e10cSrcweir memberNode->appendChild(textNode); 442*cdf0e10cSrcweir } 443*cdf0e10cSrcweir } 444*cdf0e10cSrcweir catch(css::uno::Exception &) 445*cdf0e10cSrcweir { 446*cdf0e10cSrcweir Any exc( ::cppu::getCaughtException() ); 447*cdf0e10cSrcweir throw css::deployment::DeploymentException( 448*cdf0e10cSrcweir OUSTR("Extension Manager: failed to write data entry in backend db: ") + 449*cdf0e10cSrcweir m_urlDb, 0, exc); 450*cdf0e10cSrcweir } 451*cdf0e10cSrcweir } 452*cdf0e10cSrcweir 453*cdf0e10cSrcweir //Writes only the element if is has a value. 454*cdf0e10cSrcweir //The prefix is automatically added to the element name 455*cdf0e10cSrcweir void BackendDb::writeSimpleElement( 456*cdf0e10cSrcweir OUString const & sElementName, OUString const & value, 457*cdf0e10cSrcweir Reference<css::xml::dom::XNode> const & xParent) 458*cdf0e10cSrcweir { 459*cdf0e10cSrcweir try 460*cdf0e10cSrcweir { 461*cdf0e10cSrcweir if (value.getLength() == 0) 462*cdf0e10cSrcweir return; 463*cdf0e10cSrcweir const OUString sPrefix = getNSPrefix(); 464*cdf0e10cSrcweir const Reference<css::xml::dom::XDocument> doc = getDocument(); 465*cdf0e10cSrcweir const OUString sNameSpace = getDbNSName(); 466*cdf0e10cSrcweir const Reference<css::xml::dom::XNode> dataNode( 467*cdf0e10cSrcweir doc->createElementNS(sNameSpace, sPrefix + OUSTR(":") + sElementName), 468*cdf0e10cSrcweir UNO_QUERY_THROW); 469*cdf0e10cSrcweir xParent->appendChild(dataNode); 470*cdf0e10cSrcweir 471*cdf0e10cSrcweir const Reference<css::xml::dom::XNode> dataValue( 472*cdf0e10cSrcweir doc->createTextNode(value), UNO_QUERY_THROW); 473*cdf0e10cSrcweir dataNode->appendChild(dataValue); 474*cdf0e10cSrcweir } 475*cdf0e10cSrcweir catch(css::uno::Exception &) 476*cdf0e10cSrcweir { 477*cdf0e10cSrcweir Any exc( ::cppu::getCaughtException() ); 478*cdf0e10cSrcweir throw css::deployment::DeploymentException( 479*cdf0e10cSrcweir OUSTR("Extension Manager: failed to write data entry(writeSimpleElement) in backend db: ") + 480*cdf0e10cSrcweir m_urlDb, 0, exc); 481*cdf0e10cSrcweir } 482*cdf0e10cSrcweir 483*cdf0e10cSrcweir } 484*cdf0e10cSrcweir 485*cdf0e10cSrcweir /** The key elements have an url attribute and are always children of the root 486*cdf0e10cSrcweir element. 487*cdf0e10cSrcweir */ 488*cdf0e10cSrcweir Reference<css::xml::dom::XNode> BackendDb::writeKeyElement( 489*cdf0e10cSrcweir ::rtl::OUString const & url) 490*cdf0e10cSrcweir { 491*cdf0e10cSrcweir try 492*cdf0e10cSrcweir { 493*cdf0e10cSrcweir const OUString sNameSpace = getDbNSName(); 494*cdf0e10cSrcweir const OUString sPrefix = getNSPrefix(); 495*cdf0e10cSrcweir const OUString sElementName = getKeyElementName(); 496*cdf0e10cSrcweir const Reference<css::xml::dom::XDocument> doc = getDocument(); 497*cdf0e10cSrcweir const Reference<css::xml::dom::XNode> root = doc->getFirstChild(); 498*cdf0e10cSrcweir 499*cdf0e10cSrcweir //Check if there are an entry with the same url. This can be the case if the 500*cdf0e10cSrcweir //the status of an XPackage is ambiguous. In this case a call to activateExtension 501*cdf0e10cSrcweir //(dp_extensionmanager.cxx), will register the package again. See also 502*cdf0e10cSrcweir //Package::processPackage_impl in dp_backend.cxx. 503*cdf0e10cSrcweir //A package can become 504*cdf0e10cSrcweir //invalid after its successful registration, for example if a second extension with 505*cdf0e10cSrcweir //the same service is installed. 506*cdf0e10cSrcweir const OUString sExpression( 507*cdf0e10cSrcweir sPrefix + OUSTR(":") + sElementName + OUSTR("[@url = \"") + url + OUSTR("\"]")); 508*cdf0e10cSrcweir const Reference<css::xml::dom::XNode> existingNode = 509*cdf0e10cSrcweir getXPathAPI()->selectSingleNode(root, sExpression); 510*cdf0e10cSrcweir if (existingNode.is()) 511*cdf0e10cSrcweir { 512*cdf0e10cSrcweir OSL_ASSERT(0); 513*cdf0e10cSrcweir //replace the existing entry. 514*cdf0e10cSrcweir removeEntry(url); 515*cdf0e10cSrcweir } 516*cdf0e10cSrcweir 517*cdf0e10cSrcweir const Reference<css::xml::dom::XElement> keyElement( 518*cdf0e10cSrcweir doc->createElementNS(sNameSpace, sPrefix + OUSTR(":") + sElementName)); 519*cdf0e10cSrcweir 520*cdf0e10cSrcweir keyElement->setAttribute(OUSTR("url"), url); 521*cdf0e10cSrcweir 522*cdf0e10cSrcweir const Reference<css::xml::dom::XNode> keyNode( 523*cdf0e10cSrcweir keyElement, UNO_QUERY_THROW); 524*cdf0e10cSrcweir root->appendChild(keyNode); 525*cdf0e10cSrcweir return keyNode; 526*cdf0e10cSrcweir } 527*cdf0e10cSrcweir catch(css::uno::Exception &) 528*cdf0e10cSrcweir { 529*cdf0e10cSrcweir Any exc( ::cppu::getCaughtException() ); 530*cdf0e10cSrcweir throw css::deployment::DeploymentException( 531*cdf0e10cSrcweir OUSTR("Extension Manager: failed to write key element in backend db: ") + 532*cdf0e10cSrcweir m_urlDb, 0, exc); 533*cdf0e10cSrcweir } 534*cdf0e10cSrcweir } 535*cdf0e10cSrcweir 536*cdf0e10cSrcweir OUString BackendDb::readSimpleElement( 537*cdf0e10cSrcweir OUString const & sElementName, Reference<css::xml::dom::XNode> const & xParent) 538*cdf0e10cSrcweir { 539*cdf0e10cSrcweir try 540*cdf0e10cSrcweir { 541*cdf0e10cSrcweir const OUString sPrefix = getNSPrefix(); 542*cdf0e10cSrcweir const OUString sExpr(sPrefix + OUSTR(":") + sElementName + OUSTR("/text()")); 543*cdf0e10cSrcweir const Reference<css::xml::xpath::XXPathAPI> xpathApi = getXPathAPI(); 544*cdf0e10cSrcweir const Reference<css::xml::dom::XNode> val = 545*cdf0e10cSrcweir xpathApi->selectSingleNode(xParent, sExpr); 546*cdf0e10cSrcweir if (val.is()) 547*cdf0e10cSrcweir return val->getNodeValue(); 548*cdf0e10cSrcweir return OUString(); 549*cdf0e10cSrcweir } 550*cdf0e10cSrcweir catch(css::uno::Exception &) 551*cdf0e10cSrcweir { 552*cdf0e10cSrcweir Any exc( ::cppu::getCaughtException() ); 553*cdf0e10cSrcweir throw css::deployment::DeploymentException( 554*cdf0e10cSrcweir OUSTR("Extension Manager: failed to read data (readSimpleElement) in backend db: ") + 555*cdf0e10cSrcweir m_urlDb, 0, exc); 556*cdf0e10cSrcweir } 557*cdf0e10cSrcweir } 558*cdf0e10cSrcweir 559*cdf0e10cSrcweir 560*cdf0e10cSrcweir ::std::list< OUString> BackendDb::readList( 561*cdf0e10cSrcweir Reference<css::xml::dom::XNode> const & parent, 562*cdf0e10cSrcweir OUString const & sListTagName, 563*cdf0e10cSrcweir OUString const & sMemberTagName) 564*cdf0e10cSrcweir { 565*cdf0e10cSrcweir try 566*cdf0e10cSrcweir { 567*cdf0e10cSrcweir OSL_ASSERT(parent.is()); 568*cdf0e10cSrcweir const OUString sPrefix(getNSPrefix() + OUSTR(":")); 569*cdf0e10cSrcweir const Reference<css::xml::xpath::XXPathAPI> xpathApi = getXPathAPI(); 570*cdf0e10cSrcweir const OUString sExprList( 571*cdf0e10cSrcweir sPrefix + sListTagName + OUSTR("/") + sPrefix + sMemberTagName + OUSTR("/text()")); 572*cdf0e10cSrcweir const Reference<css::xml::dom::XNodeList> list = 573*cdf0e10cSrcweir xpathApi->selectNodeList(parent, sExprList); 574*cdf0e10cSrcweir 575*cdf0e10cSrcweir ::std::list<OUString > retList; 576*cdf0e10cSrcweir sal_Int32 length = list->getLength(); 577*cdf0e10cSrcweir for (sal_Int32 i = 0; i < length; i++) 578*cdf0e10cSrcweir { 579*cdf0e10cSrcweir const Reference<css::xml::dom::XNode> member = list->item(i); 580*cdf0e10cSrcweir retList.push_back(member->getNodeValue()); 581*cdf0e10cSrcweir } 582*cdf0e10cSrcweir return retList; 583*cdf0e10cSrcweir } 584*cdf0e10cSrcweir catch(css::uno::Exception &) 585*cdf0e10cSrcweir { 586*cdf0e10cSrcweir Any exc( ::cppu::getCaughtException() ); 587*cdf0e10cSrcweir throw css::deployment::DeploymentException( 588*cdf0e10cSrcweir OUSTR("Extension Manager: failed to read data entry in backend db: ") + 589*cdf0e10cSrcweir m_urlDb, 0, exc); 590*cdf0e10cSrcweir } 591*cdf0e10cSrcweir } 592*cdf0e10cSrcweir 593*cdf0e10cSrcweir ::std::list<OUString> BackendDb::getOneChildFromAllEntries( 594*cdf0e10cSrcweir OUString const & name) 595*cdf0e10cSrcweir { 596*cdf0e10cSrcweir try 597*cdf0e10cSrcweir { 598*cdf0e10cSrcweir ::std::list<OUString> listRet; 599*cdf0e10cSrcweir Reference<css::xml::dom::XDocument> doc = getDocument(); 600*cdf0e10cSrcweir Reference<css::xml::dom::XNode> root = doc->getFirstChild(); 601*cdf0e10cSrcweir 602*cdf0e10cSrcweir Reference<css::xml::xpath::XXPathAPI> xpathApi = getXPathAPI(); 603*cdf0e10cSrcweir const OUString sPrefix = getNSPrefix(); 604*cdf0e10cSrcweir const OUString sKeyElement = getKeyElementName(); 605*cdf0e10cSrcweir ::rtl::OUStringBuffer buf(512); 606*cdf0e10cSrcweir buf.append(sPrefix); 607*cdf0e10cSrcweir buf.appendAscii(":"); 608*cdf0e10cSrcweir buf.append(sKeyElement); 609*cdf0e10cSrcweir buf.appendAscii("/"); 610*cdf0e10cSrcweir buf.append(sPrefix); 611*cdf0e10cSrcweir buf.appendAscii(":"); 612*cdf0e10cSrcweir buf.append(name); 613*cdf0e10cSrcweir buf.append(OUSTR("/text()")); 614*cdf0e10cSrcweir 615*cdf0e10cSrcweir Reference<css::xml::dom::XNodeList> nodes = 616*cdf0e10cSrcweir xpathApi->selectNodeList(root, buf.makeStringAndClear()); 617*cdf0e10cSrcweir if (nodes.is()) 618*cdf0e10cSrcweir { 619*cdf0e10cSrcweir sal_Int32 length = nodes->getLength(); 620*cdf0e10cSrcweir for (sal_Int32 i = 0; i < length; i++) 621*cdf0e10cSrcweir listRet.push_back(nodes->item(i)->getNodeValue()); 622*cdf0e10cSrcweir } 623*cdf0e10cSrcweir return listRet; 624*cdf0e10cSrcweir } 625*cdf0e10cSrcweir catch (css::deployment::DeploymentException& ) 626*cdf0e10cSrcweir { 627*cdf0e10cSrcweir throw; 628*cdf0e10cSrcweir } 629*cdf0e10cSrcweir catch(css::uno::Exception &) 630*cdf0e10cSrcweir { 631*cdf0e10cSrcweir Any exc( ::cppu::getCaughtException() ); 632*cdf0e10cSrcweir throw css::deployment::DeploymentException( 633*cdf0e10cSrcweir OUSTR("Extension Manager: failed to read data entry in backend db: ") + 634*cdf0e10cSrcweir m_urlDb, 0, exc); 635*cdf0e10cSrcweir } 636*cdf0e10cSrcweir } 637*cdf0e10cSrcweir 638*cdf0e10cSrcweir 639*cdf0e10cSrcweir 640*cdf0e10cSrcweir //================================================================================ 641*cdf0e10cSrcweir RegisteredDb::RegisteredDb( 642*cdf0e10cSrcweir Reference<XComponentContext> const & xContext, 643*cdf0e10cSrcweir ::rtl::OUString const & url):BackendDb(xContext, url) 644*cdf0e10cSrcweir { 645*cdf0e10cSrcweir 646*cdf0e10cSrcweir } 647*cdf0e10cSrcweir 648*cdf0e10cSrcweir void RegisteredDb::addEntry(::rtl::OUString const & url) 649*cdf0e10cSrcweir { 650*cdf0e10cSrcweir try{ 651*cdf0e10cSrcweir if (!activateEntry(url)) 652*cdf0e10cSrcweir { 653*cdf0e10cSrcweir const OUString sNameSpace = getDbNSName(); 654*cdf0e10cSrcweir const OUString sPrefix = getNSPrefix(); 655*cdf0e10cSrcweir const OUString sEntry = getKeyElementName(); 656*cdf0e10cSrcweir 657*cdf0e10cSrcweir Reference<css::xml::dom::XDocument> doc = getDocument(); 658*cdf0e10cSrcweir Reference<css::xml::dom::XNode> root = doc->getFirstChild(); 659*cdf0e10cSrcweir 660*cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 0 661*cdf0e10cSrcweir //There must not be yet an entry with the same url 662*cdf0e10cSrcweir OUString sExpression( 663*cdf0e10cSrcweir sPrefix + OUSTR(":") + sEntry + OUSTR("[@url = \"") + url + OUSTR("\"]")); 664*cdf0e10cSrcweir Reference<css::xml::dom::XNode> _extensionNode = 665*cdf0e10cSrcweir getXPathAPI()->selectSingleNode(root, sExpression); 666*cdf0e10cSrcweir OSL_ASSERT(! _extensionNode.is()); 667*cdf0e10cSrcweir #endif 668*cdf0e10cSrcweir Reference<css::xml::dom::XElement> helpElement( 669*cdf0e10cSrcweir doc->createElementNS(sNameSpace, sPrefix + OUSTR(":") + sEntry)); 670*cdf0e10cSrcweir 671*cdf0e10cSrcweir helpElement->setAttribute(OUSTR("url"), url); 672*cdf0e10cSrcweir 673*cdf0e10cSrcweir Reference<css::xml::dom::XNode> helpNode( 674*cdf0e10cSrcweir helpElement, UNO_QUERY_THROW); 675*cdf0e10cSrcweir root->appendChild(helpNode); 676*cdf0e10cSrcweir 677*cdf0e10cSrcweir save(); 678*cdf0e10cSrcweir } 679*cdf0e10cSrcweir } 680*cdf0e10cSrcweir catch(css::uno::Exception &) 681*cdf0e10cSrcweir { 682*cdf0e10cSrcweir Any exc( ::cppu::getCaughtException() ); 683*cdf0e10cSrcweir throw css::deployment::DeploymentException( 684*cdf0e10cSrcweir OUSTR("Extension Manager: failed to write data entry in backend db: ") + 685*cdf0e10cSrcweir m_urlDb, 0, exc); 686*cdf0e10cSrcweir } 687*cdf0e10cSrcweir } 688*cdf0e10cSrcweir 689*cdf0e10cSrcweir bool RegisteredDb::getEntry(::rtl::OUString const & url) 690*cdf0e10cSrcweir { 691*cdf0e10cSrcweir try 692*cdf0e10cSrcweir { 693*cdf0e10cSrcweir const OUString sPrefix = getNSPrefix(); 694*cdf0e10cSrcweir const OUString sEntry = getKeyElementName(); 695*cdf0e10cSrcweir const OUString sExpression( 696*cdf0e10cSrcweir sPrefix + OUSTR(":") + sEntry + OUSTR("[@url = \"") + url + OUSTR("\"]")); 697*cdf0e10cSrcweir Reference<css::xml::dom::XDocument> doc = getDocument(); 698*cdf0e10cSrcweir Reference<css::xml::dom::XNode> root = doc->getFirstChild(); 699*cdf0e10cSrcweir 700*cdf0e10cSrcweir Reference<css::xml::xpath::XXPathAPI> xpathApi = getXPathAPI(); 701*cdf0e10cSrcweir //find the extension element that is to be removed 702*cdf0e10cSrcweir Reference<css::xml::dom::XNode> aNode = 703*cdf0e10cSrcweir xpathApi->selectSingleNode(root, sExpression); 704*cdf0e10cSrcweir if (!aNode.is()) 705*cdf0e10cSrcweir { 706*cdf0e10cSrcweir return false; 707*cdf0e10cSrcweir } 708*cdf0e10cSrcweir return true; 709*cdf0e10cSrcweir } 710*cdf0e10cSrcweir catch(css::uno::Exception &) 711*cdf0e10cSrcweir { 712*cdf0e10cSrcweir Any exc( ::cppu::getCaughtException() ); 713*cdf0e10cSrcweir throw css::deployment::DeploymentException( 714*cdf0e10cSrcweir OUSTR("Extension Manager: failed to read data entry in backend db: ") + 715*cdf0e10cSrcweir m_urlDb, 0, exc); 716*cdf0e10cSrcweir } 717*cdf0e10cSrcweir } 718*cdf0e10cSrcweir 719*cdf0e10cSrcweir 720*cdf0e10cSrcweir } // namespace backend 721*cdf0e10cSrcweir } // namespace dp_registry 722*cdf0e10cSrcweir 723