1 /************************************************************************* 2 * 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * Copyright 2000, 2010 Oracle and/or its affiliates. 6 * 7 * OpenOffice.org - a multi-platform office productivity suite 8 * 9 * This file is part of OpenOffice.org. 10 * 11 * OpenOffice.org is free software: you can redistribute it and/or modify 12 * it under the terms of the GNU Lesser General Public License version 3 13 * only, as published by the Free Software Foundation. 14 * 15 * OpenOffice.org is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU Lesser General Public License version 3 for more details 19 * (a copy is included in the LICENSE file that accompanied this code). 20 * 21 * You should have received a copy of the GNU Lesser General Public License 22 * version 3 along with OpenOffice.org. If not, see 23 * <http://www.openoffice.org/license.html> 24 * for a copy of the LGPLv3 License. 25 * 26 ************************************************************************/ 27 28 // MARKER(update_precomp.py): autogen include statement, do not remove 29 #include "precompiled_ucbhelper.hxx" 30 31 /************************************************************************** 32 TODO 33 ************************************************************************** 34 35 *************************************************************************/ 36 37 #ifndef _UCBHELPER_PROVCONF_HXX_ 38 #include <provconf.hxx> 39 #endif 40 #include <osl/diagnose.h> 41 #include <rtl/ustrbuf.hxx> 42 #include <com/sun/star/beans/PropertyValue.hpp> 43 #include <com/sun/star/container/XHierarchicalNameAccess.hpp> 44 #include <com/sun/star/container/XNameAccess.hpp> 45 #include <com/sun/star/lang/XMultiServiceFactory.hpp> 46 47 using namespace com::sun::star; 48 49 //========================================================================= 50 51 #define CONFIG_CONTENTPROVIDERS_KEY \ 52 "/org.openoffice.ucb.Configuration/ContentProviders" 53 54 //========================================================================= 55 56 namespace ucbhelper { 57 58 void makeAndAppendXMLName( 59 rtl::OUStringBuffer & rBuffer, const rtl::OUString & rIn ) 60 { 61 sal_Int32 nCount = rIn.getLength(); 62 for ( sal_Int32 n = 0; n < nCount; ++n ) 63 { 64 const sal_Unicode c = rIn.getStr()[ n ]; 65 switch ( c ) 66 { 67 case '&': 68 rBuffer.appendAscii( "&" ); 69 break; 70 71 case '"': 72 rBuffer.appendAscii( """ ); 73 break; 74 75 case '\'': 76 rBuffer.appendAscii( "'" ); 77 break; 78 79 case '<': 80 rBuffer.appendAscii( "<" ); 81 break; 82 83 case '>': 84 rBuffer.appendAscii( ">" ); 85 break; 86 87 default: 88 rBuffer.append( c ); 89 break; 90 } 91 } 92 } 93 94 //========================================================================= 95 bool getContentProviderData( 96 const uno::Reference< lang::XMultiServiceFactory > & rServiceMgr, 97 const rtl::OUString & rKey1, 98 const rtl::OUString & rKey2, 99 ContentProviderDataList & rListToFill ) 100 { 101 if ( !rServiceMgr.is() || !rKey1.getLength() || !rKey2.getLength() ) 102 { 103 OSL_ENSURE( false, 104 "getContentProviderData - Invalid argument!" ); 105 return false; 106 } 107 108 try 109 { 110 uno::Reference< lang::XMultiServiceFactory > xConfigProv( 111 rServiceMgr->createInstance( 112 rtl::OUString::createFromAscii( 113 "com.sun.star.configuration.ConfigurationProvider" ) ), 114 uno::UNO_QUERY ); 115 116 if ( !xConfigProv.is() ) 117 { 118 OSL_ENSURE( false, 119 "getContentProviderData - No config provider!" ); 120 return false; 121 } 122 123 rtl::OUStringBuffer aFullPath; 124 aFullPath.appendAscii( CONFIG_CONTENTPROVIDERS_KEY "/['" ); 125 makeAndAppendXMLName( aFullPath, rKey1 ); 126 aFullPath.appendAscii( "']/SecondaryKeys/['" ); 127 makeAndAppendXMLName( aFullPath, rKey2 ); 128 aFullPath.appendAscii( "']/ProviderData" ); 129 130 uno::Sequence< uno::Any > aArguments( 1 ); 131 beans::PropertyValue aProperty; 132 aProperty.Name 133 = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "nodepath" ) ); 134 aProperty.Value <<= aFullPath.makeStringAndClear(); 135 aArguments[ 0 ] <<= aProperty; 136 137 uno::Reference< uno::XInterface > xInterface( 138 xConfigProv->createInstanceWithArguments( 139 rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( 140 "com.sun.star.configuration.ConfigurationAccess" ) ), 141 aArguments ) ); 142 143 if ( !xInterface.is() ) 144 { 145 OSL_ENSURE( false, 146 "getContentProviderData - No config access!" ); 147 return false; 148 } 149 150 uno::Reference< container::XNameAccess > xNameAccess( 151 xInterface, uno::UNO_QUERY ); 152 153 if ( !xNameAccess.is() ) 154 { 155 OSL_ENSURE( false, 156 "getContentProviderData - No XNameAccess!" ); 157 return false; 158 } 159 160 uno::Sequence< rtl::OUString > aElems = xNameAccess->getElementNames(); 161 const rtl::OUString* pElems = aElems.getConstArray(); 162 sal_Int32 nCount = aElems.getLength(); 163 164 if ( nCount > 0 ) 165 { 166 uno::Reference< container::XHierarchicalNameAccess > 167 xHierNameAccess( xInterface, uno::UNO_QUERY ); 168 169 if ( !xHierNameAccess.is() ) 170 { 171 OSL_ENSURE( false, 172 "getContentProviderData - " 173 "No XHierarchicalNameAccess!" ); 174 return false; 175 } 176 177 // Iterate over children. 178 for ( sal_Int32 n = 0; n < nCount; ++n ) 179 { 180 rtl::OUStringBuffer aElemBuffer; 181 aElemBuffer.appendAscii( "['" ); 182 makeAndAppendXMLName( aElemBuffer, pElems[ n ] ); 183 184 try 185 { 186 ContentProviderData aInfo; 187 188 // Obtain service name. 189 rtl::OUStringBuffer aKeyBuffer = aElemBuffer; 190 aKeyBuffer.appendAscii( "']/ServiceName" ); 191 192 rtl::OUString aValue; 193 if ( !( xHierNameAccess->getByHierarchicalName( 194 aKeyBuffer.makeStringAndClear() ) >>= aValue ) ) 195 { 196 OSL_ENSURE( false, 197 "getContentProviderData - " 198 "Error getting item value!" ); 199 continue; 200 } 201 202 aInfo.ServiceName = aValue; 203 204 // Obtain URL Template. 205 aKeyBuffer = aElemBuffer; 206 aKeyBuffer.appendAscii( "']/URLTemplate" ); 207 208 if ( !( xHierNameAccess->getByHierarchicalName( 209 aKeyBuffer.makeStringAndClear() ) >>= aValue ) ) 210 { 211 OSL_ENSURE( false, 212 "getContentProviderData - " 213 "Error getting item value!" ); 214 continue; 215 } 216 217 aInfo.URLTemplate = aValue; 218 219 // Obtain Arguments. 220 aKeyBuffer = aElemBuffer; 221 aKeyBuffer.appendAscii( "']/Arguments" ); 222 223 if ( !( xHierNameAccess->getByHierarchicalName( 224 aKeyBuffer.makeStringAndClear() ) >>= aValue ) ) 225 { 226 OSL_ENSURE( false, 227 "getContentProviderData - " 228 "Error getting item value!" ); 229 continue; 230 } 231 232 aInfo.Arguments = aValue; 233 234 // Append info to list. 235 rListToFill.push_back( aInfo ); 236 } 237 catch ( container::NoSuchElementException& ) 238 { 239 // getByHierarchicalName 240 241 OSL_ENSURE( false, 242 "getContentProviderData - " 243 "caught NoSuchElementException!" ); 244 } 245 } 246 } 247 } 248 catch ( uno::RuntimeException& ) 249 { 250 OSL_ENSURE( false, 251 "getContentProviderData - caught RuntimeException!" ); 252 return false; 253 } 254 catch ( uno::Exception& ) 255 { 256 // createInstance, createInstanceWithArguments 257 258 OSL_ENSURE( false, 259 "getContentProviderData - caught Exception!" ); 260 return false; 261 } 262 263 return true; 264 } 265 266 } 267