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_ucbhelper.hxx" 30*cdf0e10cSrcweir 31*cdf0e10cSrcweir /************************************************************************** 32*cdf0e10cSrcweir TODO 33*cdf0e10cSrcweir ************************************************************************** 34*cdf0e10cSrcweir 35*cdf0e10cSrcweir *************************************************************************/ 36*cdf0e10cSrcweir 37*cdf0e10cSrcweir #ifndef _UCBHELPER_PROVCONF_HXX_ 38*cdf0e10cSrcweir #include <provconf.hxx> 39*cdf0e10cSrcweir #endif 40*cdf0e10cSrcweir #include <osl/diagnose.h> 41*cdf0e10cSrcweir #include <rtl/ustrbuf.hxx> 42*cdf0e10cSrcweir #include <com/sun/star/beans/PropertyValue.hpp> 43*cdf0e10cSrcweir #include <com/sun/star/container/XHierarchicalNameAccess.hpp> 44*cdf0e10cSrcweir #include <com/sun/star/container/XNameAccess.hpp> 45*cdf0e10cSrcweir #include <com/sun/star/lang/XMultiServiceFactory.hpp> 46*cdf0e10cSrcweir 47*cdf0e10cSrcweir using namespace com::sun::star; 48*cdf0e10cSrcweir 49*cdf0e10cSrcweir //========================================================================= 50*cdf0e10cSrcweir 51*cdf0e10cSrcweir #define CONFIG_CONTENTPROVIDERS_KEY \ 52*cdf0e10cSrcweir "/org.openoffice.ucb.Configuration/ContentProviders" 53*cdf0e10cSrcweir 54*cdf0e10cSrcweir //========================================================================= 55*cdf0e10cSrcweir 56*cdf0e10cSrcweir namespace ucbhelper { 57*cdf0e10cSrcweir 58*cdf0e10cSrcweir void makeAndAppendXMLName( 59*cdf0e10cSrcweir rtl::OUStringBuffer & rBuffer, const rtl::OUString & rIn ) 60*cdf0e10cSrcweir { 61*cdf0e10cSrcweir sal_Int32 nCount = rIn.getLength(); 62*cdf0e10cSrcweir for ( sal_Int32 n = 0; n < nCount; ++n ) 63*cdf0e10cSrcweir { 64*cdf0e10cSrcweir const sal_Unicode c = rIn.getStr()[ n ]; 65*cdf0e10cSrcweir switch ( c ) 66*cdf0e10cSrcweir { 67*cdf0e10cSrcweir case '&': 68*cdf0e10cSrcweir rBuffer.appendAscii( "&" ); 69*cdf0e10cSrcweir break; 70*cdf0e10cSrcweir 71*cdf0e10cSrcweir case '"': 72*cdf0e10cSrcweir rBuffer.appendAscii( """ ); 73*cdf0e10cSrcweir break; 74*cdf0e10cSrcweir 75*cdf0e10cSrcweir case '\'': 76*cdf0e10cSrcweir rBuffer.appendAscii( "'" ); 77*cdf0e10cSrcweir break; 78*cdf0e10cSrcweir 79*cdf0e10cSrcweir case '<': 80*cdf0e10cSrcweir rBuffer.appendAscii( "<" ); 81*cdf0e10cSrcweir break; 82*cdf0e10cSrcweir 83*cdf0e10cSrcweir case '>': 84*cdf0e10cSrcweir rBuffer.appendAscii( ">" ); 85*cdf0e10cSrcweir break; 86*cdf0e10cSrcweir 87*cdf0e10cSrcweir default: 88*cdf0e10cSrcweir rBuffer.append( c ); 89*cdf0e10cSrcweir break; 90*cdf0e10cSrcweir } 91*cdf0e10cSrcweir } 92*cdf0e10cSrcweir } 93*cdf0e10cSrcweir 94*cdf0e10cSrcweir //========================================================================= 95*cdf0e10cSrcweir bool getContentProviderData( 96*cdf0e10cSrcweir const uno::Reference< lang::XMultiServiceFactory > & rServiceMgr, 97*cdf0e10cSrcweir const rtl::OUString & rKey1, 98*cdf0e10cSrcweir const rtl::OUString & rKey2, 99*cdf0e10cSrcweir ContentProviderDataList & rListToFill ) 100*cdf0e10cSrcweir { 101*cdf0e10cSrcweir if ( !rServiceMgr.is() || !rKey1.getLength() || !rKey2.getLength() ) 102*cdf0e10cSrcweir { 103*cdf0e10cSrcweir OSL_ENSURE( false, 104*cdf0e10cSrcweir "getContentProviderData - Invalid argument!" ); 105*cdf0e10cSrcweir return false; 106*cdf0e10cSrcweir } 107*cdf0e10cSrcweir 108*cdf0e10cSrcweir try 109*cdf0e10cSrcweir { 110*cdf0e10cSrcweir uno::Reference< lang::XMultiServiceFactory > xConfigProv( 111*cdf0e10cSrcweir rServiceMgr->createInstance( 112*cdf0e10cSrcweir rtl::OUString::createFromAscii( 113*cdf0e10cSrcweir "com.sun.star.configuration.ConfigurationProvider" ) ), 114*cdf0e10cSrcweir uno::UNO_QUERY ); 115*cdf0e10cSrcweir 116*cdf0e10cSrcweir if ( !xConfigProv.is() ) 117*cdf0e10cSrcweir { 118*cdf0e10cSrcweir OSL_ENSURE( false, 119*cdf0e10cSrcweir "getContentProviderData - No config provider!" ); 120*cdf0e10cSrcweir return false; 121*cdf0e10cSrcweir } 122*cdf0e10cSrcweir 123*cdf0e10cSrcweir rtl::OUStringBuffer aFullPath; 124*cdf0e10cSrcweir aFullPath.appendAscii( CONFIG_CONTENTPROVIDERS_KEY "/['" ); 125*cdf0e10cSrcweir makeAndAppendXMLName( aFullPath, rKey1 ); 126*cdf0e10cSrcweir aFullPath.appendAscii( "']/SecondaryKeys/['" ); 127*cdf0e10cSrcweir makeAndAppendXMLName( aFullPath, rKey2 ); 128*cdf0e10cSrcweir aFullPath.appendAscii( "']/ProviderData" ); 129*cdf0e10cSrcweir 130*cdf0e10cSrcweir uno::Sequence< uno::Any > aArguments( 1 ); 131*cdf0e10cSrcweir beans::PropertyValue aProperty; 132*cdf0e10cSrcweir aProperty.Name 133*cdf0e10cSrcweir = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "nodepath" ) ); 134*cdf0e10cSrcweir aProperty.Value <<= aFullPath.makeStringAndClear(); 135*cdf0e10cSrcweir aArguments[ 0 ] <<= aProperty; 136*cdf0e10cSrcweir 137*cdf0e10cSrcweir uno::Reference< uno::XInterface > xInterface( 138*cdf0e10cSrcweir xConfigProv->createInstanceWithArguments( 139*cdf0e10cSrcweir rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( 140*cdf0e10cSrcweir "com.sun.star.configuration.ConfigurationAccess" ) ), 141*cdf0e10cSrcweir aArguments ) ); 142*cdf0e10cSrcweir 143*cdf0e10cSrcweir if ( !xInterface.is() ) 144*cdf0e10cSrcweir { 145*cdf0e10cSrcweir OSL_ENSURE( false, 146*cdf0e10cSrcweir "getContentProviderData - No config access!" ); 147*cdf0e10cSrcweir return false; 148*cdf0e10cSrcweir } 149*cdf0e10cSrcweir 150*cdf0e10cSrcweir uno::Reference< container::XNameAccess > xNameAccess( 151*cdf0e10cSrcweir xInterface, uno::UNO_QUERY ); 152*cdf0e10cSrcweir 153*cdf0e10cSrcweir if ( !xNameAccess.is() ) 154*cdf0e10cSrcweir { 155*cdf0e10cSrcweir OSL_ENSURE( false, 156*cdf0e10cSrcweir "getContentProviderData - No XNameAccess!" ); 157*cdf0e10cSrcweir return false; 158*cdf0e10cSrcweir } 159*cdf0e10cSrcweir 160*cdf0e10cSrcweir uno::Sequence< rtl::OUString > aElems = xNameAccess->getElementNames(); 161*cdf0e10cSrcweir const rtl::OUString* pElems = aElems.getConstArray(); 162*cdf0e10cSrcweir sal_Int32 nCount = aElems.getLength(); 163*cdf0e10cSrcweir 164*cdf0e10cSrcweir if ( nCount > 0 ) 165*cdf0e10cSrcweir { 166*cdf0e10cSrcweir uno::Reference< container::XHierarchicalNameAccess > 167*cdf0e10cSrcweir xHierNameAccess( xInterface, uno::UNO_QUERY ); 168*cdf0e10cSrcweir 169*cdf0e10cSrcweir if ( !xHierNameAccess.is() ) 170*cdf0e10cSrcweir { 171*cdf0e10cSrcweir OSL_ENSURE( false, 172*cdf0e10cSrcweir "getContentProviderData - " 173*cdf0e10cSrcweir "No XHierarchicalNameAccess!" ); 174*cdf0e10cSrcweir return false; 175*cdf0e10cSrcweir } 176*cdf0e10cSrcweir 177*cdf0e10cSrcweir // Iterate over children. 178*cdf0e10cSrcweir for ( sal_Int32 n = 0; n < nCount; ++n ) 179*cdf0e10cSrcweir { 180*cdf0e10cSrcweir rtl::OUStringBuffer aElemBuffer; 181*cdf0e10cSrcweir aElemBuffer.appendAscii( "['" ); 182*cdf0e10cSrcweir makeAndAppendXMLName( aElemBuffer, pElems[ n ] ); 183*cdf0e10cSrcweir 184*cdf0e10cSrcweir try 185*cdf0e10cSrcweir { 186*cdf0e10cSrcweir ContentProviderData aInfo; 187*cdf0e10cSrcweir 188*cdf0e10cSrcweir // Obtain service name. 189*cdf0e10cSrcweir rtl::OUStringBuffer aKeyBuffer = aElemBuffer; 190*cdf0e10cSrcweir aKeyBuffer.appendAscii( "']/ServiceName" ); 191*cdf0e10cSrcweir 192*cdf0e10cSrcweir rtl::OUString aValue; 193*cdf0e10cSrcweir if ( !( xHierNameAccess->getByHierarchicalName( 194*cdf0e10cSrcweir aKeyBuffer.makeStringAndClear() ) >>= aValue ) ) 195*cdf0e10cSrcweir { 196*cdf0e10cSrcweir OSL_ENSURE( false, 197*cdf0e10cSrcweir "getContentProviderData - " 198*cdf0e10cSrcweir "Error getting item value!" ); 199*cdf0e10cSrcweir continue; 200*cdf0e10cSrcweir } 201*cdf0e10cSrcweir 202*cdf0e10cSrcweir aInfo.ServiceName = aValue; 203*cdf0e10cSrcweir 204*cdf0e10cSrcweir // Obtain URL Template. 205*cdf0e10cSrcweir aKeyBuffer = aElemBuffer; 206*cdf0e10cSrcweir aKeyBuffer.appendAscii( "']/URLTemplate" ); 207*cdf0e10cSrcweir 208*cdf0e10cSrcweir if ( !( xHierNameAccess->getByHierarchicalName( 209*cdf0e10cSrcweir aKeyBuffer.makeStringAndClear() ) >>= aValue ) ) 210*cdf0e10cSrcweir { 211*cdf0e10cSrcweir OSL_ENSURE( false, 212*cdf0e10cSrcweir "getContentProviderData - " 213*cdf0e10cSrcweir "Error getting item value!" ); 214*cdf0e10cSrcweir continue; 215*cdf0e10cSrcweir } 216*cdf0e10cSrcweir 217*cdf0e10cSrcweir aInfo.URLTemplate = aValue; 218*cdf0e10cSrcweir 219*cdf0e10cSrcweir // Obtain Arguments. 220*cdf0e10cSrcweir aKeyBuffer = aElemBuffer; 221*cdf0e10cSrcweir aKeyBuffer.appendAscii( "']/Arguments" ); 222*cdf0e10cSrcweir 223*cdf0e10cSrcweir if ( !( xHierNameAccess->getByHierarchicalName( 224*cdf0e10cSrcweir aKeyBuffer.makeStringAndClear() ) >>= aValue ) ) 225*cdf0e10cSrcweir { 226*cdf0e10cSrcweir OSL_ENSURE( false, 227*cdf0e10cSrcweir "getContentProviderData - " 228*cdf0e10cSrcweir "Error getting item value!" ); 229*cdf0e10cSrcweir continue; 230*cdf0e10cSrcweir } 231*cdf0e10cSrcweir 232*cdf0e10cSrcweir aInfo.Arguments = aValue; 233*cdf0e10cSrcweir 234*cdf0e10cSrcweir // Append info to list. 235*cdf0e10cSrcweir rListToFill.push_back( aInfo ); 236*cdf0e10cSrcweir } 237*cdf0e10cSrcweir catch ( container::NoSuchElementException& ) 238*cdf0e10cSrcweir { 239*cdf0e10cSrcweir // getByHierarchicalName 240*cdf0e10cSrcweir 241*cdf0e10cSrcweir OSL_ENSURE( false, 242*cdf0e10cSrcweir "getContentProviderData - " 243*cdf0e10cSrcweir "caught NoSuchElementException!" ); 244*cdf0e10cSrcweir } 245*cdf0e10cSrcweir } 246*cdf0e10cSrcweir } 247*cdf0e10cSrcweir } 248*cdf0e10cSrcweir catch ( uno::RuntimeException& ) 249*cdf0e10cSrcweir { 250*cdf0e10cSrcweir OSL_ENSURE( false, 251*cdf0e10cSrcweir "getContentProviderData - caught RuntimeException!" ); 252*cdf0e10cSrcweir return false; 253*cdf0e10cSrcweir } 254*cdf0e10cSrcweir catch ( uno::Exception& ) 255*cdf0e10cSrcweir { 256*cdf0e10cSrcweir // createInstance, createInstanceWithArguments 257*cdf0e10cSrcweir 258*cdf0e10cSrcweir OSL_ENSURE( false, 259*cdf0e10cSrcweir "getContentProviderData - caught Exception!" ); 260*cdf0e10cSrcweir return false; 261*cdf0e10cSrcweir } 262*cdf0e10cSrcweir 263*cdf0e10cSrcweir return true; 264*cdf0e10cSrcweir } 265*cdf0e10cSrcweir 266*cdf0e10cSrcweir } 267