Update ooo320-m1
[ooovba.git] / ucbhelper / source / provider / provconf.cxx
blob5f0384ccad55a999e8fb241916a15ec4438c296d
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: provconf.cxx,v $
10 * $Revision: 1.7 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_ucbhelper.hxx"
34 /**************************************************************************
35 TODO
36 **************************************************************************
38 *************************************************************************/
40 #ifndef _UCBHELPER_PROVCONF_HXX_
41 #include <provconf.hxx>
42 #endif
43 #include <osl/diagnose.h>
44 #include <rtl/ustrbuf.hxx>
45 #include <com/sun/star/beans/PropertyValue.hpp>
46 #include <com/sun/star/container/XHierarchicalNameAccess.hpp>
47 #include <com/sun/star/container/XNameAccess.hpp>
48 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
50 using namespace com::sun::star;
52 //=========================================================================
54 #define CONFIG_CONTENTPROVIDERS_KEY \
55 "/org.openoffice.ucb.Configuration/ContentProviders"
57 //=========================================================================
59 namespace ucbhelper {
61 void makeAndAppendXMLName(
62 rtl::OUStringBuffer & rBuffer, const rtl::OUString & rIn )
64 sal_Int32 nCount = rIn.getLength();
65 for ( sal_Int32 n = 0; n < nCount; ++n )
67 const sal_Unicode c = rIn.getStr()[ n ];
68 switch ( c )
70 case '&':
71 rBuffer.appendAscii( "&amp;" );
72 break;
74 case '"':
75 rBuffer.appendAscii( "&quot;" );
76 break;
78 case '\'':
79 rBuffer.appendAscii( "&apos;" );
80 break;
82 case '<':
83 rBuffer.appendAscii( "&lt;" );
84 break;
86 case '>':
87 rBuffer.appendAscii( "&gt;" );
88 break;
90 default:
91 rBuffer.append( c );
92 break;
97 //=========================================================================
98 bool getContentProviderData(
99 const uno::Reference< lang::XMultiServiceFactory > & rServiceMgr,
100 const rtl::OUString & rKey1,
101 const rtl::OUString & rKey2,
102 ContentProviderDataList & rListToFill )
104 if ( !rServiceMgr.is() || !rKey1.getLength() || !rKey2.getLength() )
106 OSL_ENSURE( false,
107 "getContentProviderData - Invalid argument!" );
108 return false;
113 uno::Reference< lang::XMultiServiceFactory > xConfigProv(
114 rServiceMgr->createInstance(
115 rtl::OUString::createFromAscii(
116 "com.sun.star.configuration.ConfigurationProvider" ) ),
117 uno::UNO_QUERY );
119 if ( !xConfigProv.is() )
121 OSL_ENSURE( false,
122 "getContentProviderData - No config provider!" );
123 return false;
126 rtl::OUStringBuffer aFullPath;
127 aFullPath.appendAscii( CONFIG_CONTENTPROVIDERS_KEY "/['" );
128 makeAndAppendXMLName( aFullPath, rKey1 );
129 aFullPath.appendAscii( "']/SecondaryKeys/['" );
130 makeAndAppendXMLName( aFullPath, rKey2 );
131 aFullPath.appendAscii( "']/ProviderData" );
133 uno::Sequence< uno::Any > aArguments( 1 );
134 beans::PropertyValue aProperty;
135 aProperty.Name
136 = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "nodepath" ) );
137 aProperty.Value <<= aFullPath.makeStringAndClear();
138 aArguments[ 0 ] <<= aProperty;
140 uno::Reference< uno::XInterface > xInterface(
141 xConfigProv->createInstanceWithArguments(
142 rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(
143 "com.sun.star.configuration.ConfigurationAccess" ) ),
144 aArguments ) );
146 if ( !xInterface.is() )
148 OSL_ENSURE( false,
149 "getContentProviderData - No config access!" );
150 return false;
153 uno::Reference< container::XNameAccess > xNameAccess(
154 xInterface, uno::UNO_QUERY );
156 if ( !xNameAccess.is() )
158 OSL_ENSURE( false,
159 "getContentProviderData - No XNameAccess!" );
160 return false;
163 uno::Sequence< rtl::OUString > aElems = xNameAccess->getElementNames();
164 const rtl::OUString* pElems = aElems.getConstArray();
165 sal_Int32 nCount = aElems.getLength();
167 if ( nCount > 0 )
169 uno::Reference< container::XHierarchicalNameAccess >
170 xHierNameAccess( xInterface, uno::UNO_QUERY );
172 if ( !xHierNameAccess.is() )
174 OSL_ENSURE( false,
175 "getContentProviderData - "
176 "No XHierarchicalNameAccess!" );
177 return false;
180 // Iterate over children.
181 for ( sal_Int32 n = 0; n < nCount; ++n )
183 rtl::OUStringBuffer aElemBuffer;
184 aElemBuffer.appendAscii( "['" );
185 makeAndAppendXMLName( aElemBuffer, pElems[ n ] );
189 ContentProviderData aInfo;
191 // Obtain service name.
192 rtl::OUStringBuffer aKeyBuffer = aElemBuffer;
193 aKeyBuffer.appendAscii( "']/ServiceName" );
195 rtl::OUString aValue;
196 if ( !( xHierNameAccess->getByHierarchicalName(
197 aKeyBuffer.makeStringAndClear() ) >>= aValue ) )
199 OSL_ENSURE( false,
200 "getContentProviderData - "
201 "Error getting item value!" );
202 continue;
205 aInfo.ServiceName = aValue;
207 // Obtain URL Template.
208 aKeyBuffer = aElemBuffer;
209 aKeyBuffer.appendAscii( "']/URLTemplate" );
211 if ( !( xHierNameAccess->getByHierarchicalName(
212 aKeyBuffer.makeStringAndClear() ) >>= aValue ) )
214 OSL_ENSURE( false,
215 "getContentProviderData - "
216 "Error getting item value!" );
217 continue;
220 aInfo.URLTemplate = aValue;
222 // Obtain Arguments.
223 aKeyBuffer = aElemBuffer;
224 aKeyBuffer.appendAscii( "']/Arguments" );
226 if ( !( xHierNameAccess->getByHierarchicalName(
227 aKeyBuffer.makeStringAndClear() ) >>= aValue ) )
229 OSL_ENSURE( false,
230 "getContentProviderData - "
231 "Error getting item value!" );
232 continue;
235 aInfo.Arguments = aValue;
237 // Append info to list.
238 rListToFill.push_back( aInfo );
240 catch ( container::NoSuchElementException& )
242 // getByHierarchicalName
244 OSL_ENSURE( false,
245 "getContentProviderData - "
246 "caught NoSuchElementException!" );
251 catch ( uno::RuntimeException& )
253 OSL_ENSURE( false,
254 "getContentProviderData - caught RuntimeException!" );
255 return false;
257 catch ( uno::Exception& )
259 // createInstance, createInstanceWithArguments
261 OSL_ENSURE( false,
262 "getContentProviderData - caught Exception!" );
263 return false;
266 return true;