1*ac9096f4SAndrew Rist /**************************************************************
2cdf0e10cSrcweir *
3*ac9096f4SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one
4*ac9096f4SAndrew Rist * or more contributor license agreements. See the NOTICE file
5*ac9096f4SAndrew Rist * distributed with this work for additional information
6*ac9096f4SAndrew Rist * regarding copyright ownership. The ASF licenses this file
7*ac9096f4SAndrew Rist * to you under the Apache License, Version 2.0 (the
8*ac9096f4SAndrew Rist * "License"); you may not use this file except in compliance
9*ac9096f4SAndrew Rist * with the License. You may obtain a copy of the License at
10cdf0e10cSrcweir *
11*ac9096f4SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir *
13*ac9096f4SAndrew Rist * Unless required by applicable law or agreed to in writing,
14*ac9096f4SAndrew Rist * software distributed under the License is distributed on an
15*ac9096f4SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*ac9096f4SAndrew Rist * KIND, either express or implied. See the License for the
17*ac9096f4SAndrew Rist * specific language governing permissions and limitations
18*ac9096f4SAndrew Rist * under the License.
19cdf0e10cSrcweir *
20*ac9096f4SAndrew Rist *************************************************************/
21*ac9096f4SAndrew Rist
22*ac9096f4SAndrew Rist
23cdf0e10cSrcweir
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_ucbhelper.hxx"
26cdf0e10cSrcweir
27cdf0e10cSrcweir /**************************************************************************
28cdf0e10cSrcweir TODO
29cdf0e10cSrcweir **************************************************************************
30cdf0e10cSrcweir
31cdf0e10cSrcweir *************************************************************************/
32cdf0e10cSrcweir
33cdf0e10cSrcweir #ifndef _UCBHELPER_PROVCONF_HXX_
34cdf0e10cSrcweir #include <provconf.hxx>
35cdf0e10cSrcweir #endif
36cdf0e10cSrcweir #include <osl/diagnose.h>
37cdf0e10cSrcweir #include <rtl/ustrbuf.hxx>
38cdf0e10cSrcweir #include <com/sun/star/beans/PropertyValue.hpp>
39cdf0e10cSrcweir #include <com/sun/star/container/XHierarchicalNameAccess.hpp>
40cdf0e10cSrcweir #include <com/sun/star/container/XNameAccess.hpp>
41cdf0e10cSrcweir #include <com/sun/star/lang/XMultiServiceFactory.hpp>
42cdf0e10cSrcweir
43cdf0e10cSrcweir using namespace com::sun::star;
44cdf0e10cSrcweir
45cdf0e10cSrcweir //=========================================================================
46cdf0e10cSrcweir
47cdf0e10cSrcweir #define CONFIG_CONTENTPROVIDERS_KEY \
48cdf0e10cSrcweir "/org.openoffice.ucb.Configuration/ContentProviders"
49cdf0e10cSrcweir
50cdf0e10cSrcweir //=========================================================================
51cdf0e10cSrcweir
52cdf0e10cSrcweir namespace ucbhelper {
53cdf0e10cSrcweir
makeAndAppendXMLName(rtl::OUStringBuffer & rBuffer,const rtl::OUString & rIn)54cdf0e10cSrcweir void makeAndAppendXMLName(
55cdf0e10cSrcweir rtl::OUStringBuffer & rBuffer, const rtl::OUString & rIn )
56cdf0e10cSrcweir {
57cdf0e10cSrcweir sal_Int32 nCount = rIn.getLength();
58cdf0e10cSrcweir for ( sal_Int32 n = 0; n < nCount; ++n )
59cdf0e10cSrcweir {
60cdf0e10cSrcweir const sal_Unicode c = rIn.getStr()[ n ];
61cdf0e10cSrcweir switch ( c )
62cdf0e10cSrcweir {
63cdf0e10cSrcweir case '&':
64cdf0e10cSrcweir rBuffer.appendAscii( "&" );
65cdf0e10cSrcweir break;
66cdf0e10cSrcweir
67cdf0e10cSrcweir case '"':
68cdf0e10cSrcweir rBuffer.appendAscii( """ );
69cdf0e10cSrcweir break;
70cdf0e10cSrcweir
71cdf0e10cSrcweir case '\'':
72cdf0e10cSrcweir rBuffer.appendAscii( "'" );
73cdf0e10cSrcweir break;
74cdf0e10cSrcweir
75cdf0e10cSrcweir case '<':
76cdf0e10cSrcweir rBuffer.appendAscii( "<" );
77cdf0e10cSrcweir break;
78cdf0e10cSrcweir
79cdf0e10cSrcweir case '>':
80cdf0e10cSrcweir rBuffer.appendAscii( ">" );
81cdf0e10cSrcweir break;
82cdf0e10cSrcweir
83cdf0e10cSrcweir default:
84cdf0e10cSrcweir rBuffer.append( c );
85cdf0e10cSrcweir break;
86cdf0e10cSrcweir }
87cdf0e10cSrcweir }
88cdf0e10cSrcweir }
89cdf0e10cSrcweir
90cdf0e10cSrcweir //=========================================================================
getContentProviderData(const uno::Reference<lang::XMultiServiceFactory> & rServiceMgr,const rtl::OUString & rKey1,const rtl::OUString & rKey2,ContentProviderDataList & rListToFill)91cdf0e10cSrcweir bool getContentProviderData(
92cdf0e10cSrcweir const uno::Reference< lang::XMultiServiceFactory > & rServiceMgr,
93cdf0e10cSrcweir const rtl::OUString & rKey1,
94cdf0e10cSrcweir const rtl::OUString & rKey2,
95cdf0e10cSrcweir ContentProviderDataList & rListToFill )
96cdf0e10cSrcweir {
97cdf0e10cSrcweir if ( !rServiceMgr.is() || !rKey1.getLength() || !rKey2.getLength() )
98cdf0e10cSrcweir {
99cdf0e10cSrcweir OSL_ENSURE( false,
100cdf0e10cSrcweir "getContentProviderData - Invalid argument!" );
101cdf0e10cSrcweir return false;
102cdf0e10cSrcweir }
103cdf0e10cSrcweir
104cdf0e10cSrcweir try
105cdf0e10cSrcweir {
106cdf0e10cSrcweir uno::Reference< lang::XMultiServiceFactory > xConfigProv(
107cdf0e10cSrcweir rServiceMgr->createInstance(
108cdf0e10cSrcweir rtl::OUString::createFromAscii(
109cdf0e10cSrcweir "com.sun.star.configuration.ConfigurationProvider" ) ),
110cdf0e10cSrcweir uno::UNO_QUERY );
111cdf0e10cSrcweir
112cdf0e10cSrcweir if ( !xConfigProv.is() )
113cdf0e10cSrcweir {
114cdf0e10cSrcweir OSL_ENSURE( false,
115cdf0e10cSrcweir "getContentProviderData - No config provider!" );
116cdf0e10cSrcweir return false;
117cdf0e10cSrcweir }
118cdf0e10cSrcweir
119cdf0e10cSrcweir rtl::OUStringBuffer aFullPath;
120cdf0e10cSrcweir aFullPath.appendAscii( CONFIG_CONTENTPROVIDERS_KEY "/['" );
121cdf0e10cSrcweir makeAndAppendXMLName( aFullPath, rKey1 );
122cdf0e10cSrcweir aFullPath.appendAscii( "']/SecondaryKeys/['" );
123cdf0e10cSrcweir makeAndAppendXMLName( aFullPath, rKey2 );
124cdf0e10cSrcweir aFullPath.appendAscii( "']/ProviderData" );
125cdf0e10cSrcweir
126cdf0e10cSrcweir uno::Sequence< uno::Any > aArguments( 1 );
127cdf0e10cSrcweir beans::PropertyValue aProperty;
128cdf0e10cSrcweir aProperty.Name
129cdf0e10cSrcweir = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "nodepath" ) );
130cdf0e10cSrcweir aProperty.Value <<= aFullPath.makeStringAndClear();
131cdf0e10cSrcweir aArguments[ 0 ] <<= aProperty;
132cdf0e10cSrcweir
133cdf0e10cSrcweir uno::Reference< uno::XInterface > xInterface(
134cdf0e10cSrcweir xConfigProv->createInstanceWithArguments(
135cdf0e10cSrcweir rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(
136cdf0e10cSrcweir "com.sun.star.configuration.ConfigurationAccess" ) ),
137cdf0e10cSrcweir aArguments ) );
138cdf0e10cSrcweir
139cdf0e10cSrcweir if ( !xInterface.is() )
140cdf0e10cSrcweir {
141cdf0e10cSrcweir OSL_ENSURE( false,
142cdf0e10cSrcweir "getContentProviderData - No config access!" );
143cdf0e10cSrcweir return false;
144cdf0e10cSrcweir }
145cdf0e10cSrcweir
146cdf0e10cSrcweir uno::Reference< container::XNameAccess > xNameAccess(
147cdf0e10cSrcweir xInterface, uno::UNO_QUERY );
148cdf0e10cSrcweir
149cdf0e10cSrcweir if ( !xNameAccess.is() )
150cdf0e10cSrcweir {
151cdf0e10cSrcweir OSL_ENSURE( false,
152cdf0e10cSrcweir "getContentProviderData - No XNameAccess!" );
153cdf0e10cSrcweir return false;
154cdf0e10cSrcweir }
155cdf0e10cSrcweir
156cdf0e10cSrcweir uno::Sequence< rtl::OUString > aElems = xNameAccess->getElementNames();
157cdf0e10cSrcweir const rtl::OUString* pElems = aElems.getConstArray();
158cdf0e10cSrcweir sal_Int32 nCount = aElems.getLength();
159cdf0e10cSrcweir
160cdf0e10cSrcweir if ( nCount > 0 )
161cdf0e10cSrcweir {
162cdf0e10cSrcweir uno::Reference< container::XHierarchicalNameAccess >
163cdf0e10cSrcweir xHierNameAccess( xInterface, uno::UNO_QUERY );
164cdf0e10cSrcweir
165cdf0e10cSrcweir if ( !xHierNameAccess.is() )
166cdf0e10cSrcweir {
167cdf0e10cSrcweir OSL_ENSURE( false,
168cdf0e10cSrcweir "getContentProviderData - "
169cdf0e10cSrcweir "No XHierarchicalNameAccess!" );
170cdf0e10cSrcweir return false;
171cdf0e10cSrcweir }
172cdf0e10cSrcweir
173cdf0e10cSrcweir // Iterate over children.
174cdf0e10cSrcweir for ( sal_Int32 n = 0; n < nCount; ++n )
175cdf0e10cSrcweir {
176cdf0e10cSrcweir rtl::OUStringBuffer aElemBuffer;
177cdf0e10cSrcweir aElemBuffer.appendAscii( "['" );
178cdf0e10cSrcweir makeAndAppendXMLName( aElemBuffer, pElems[ n ] );
179cdf0e10cSrcweir
180cdf0e10cSrcweir try
181cdf0e10cSrcweir {
182cdf0e10cSrcweir ContentProviderData aInfo;
183cdf0e10cSrcweir
184cdf0e10cSrcweir // Obtain service name.
185cdf0e10cSrcweir rtl::OUStringBuffer aKeyBuffer = aElemBuffer;
186cdf0e10cSrcweir aKeyBuffer.appendAscii( "']/ServiceName" );
187cdf0e10cSrcweir
188cdf0e10cSrcweir rtl::OUString aValue;
189cdf0e10cSrcweir if ( !( xHierNameAccess->getByHierarchicalName(
190cdf0e10cSrcweir aKeyBuffer.makeStringAndClear() ) >>= aValue ) )
191cdf0e10cSrcweir {
192cdf0e10cSrcweir OSL_ENSURE( false,
193cdf0e10cSrcweir "getContentProviderData - "
194cdf0e10cSrcweir "Error getting item value!" );
195cdf0e10cSrcweir continue;
196cdf0e10cSrcweir }
197cdf0e10cSrcweir
198cdf0e10cSrcweir aInfo.ServiceName = aValue;
199cdf0e10cSrcweir
200cdf0e10cSrcweir // Obtain URL Template.
201cdf0e10cSrcweir aKeyBuffer = aElemBuffer;
202cdf0e10cSrcweir aKeyBuffer.appendAscii( "']/URLTemplate" );
203cdf0e10cSrcweir
204cdf0e10cSrcweir if ( !( xHierNameAccess->getByHierarchicalName(
205cdf0e10cSrcweir aKeyBuffer.makeStringAndClear() ) >>= aValue ) )
206cdf0e10cSrcweir {
207cdf0e10cSrcweir OSL_ENSURE( false,
208cdf0e10cSrcweir "getContentProviderData - "
209cdf0e10cSrcweir "Error getting item value!" );
210cdf0e10cSrcweir continue;
211cdf0e10cSrcweir }
212cdf0e10cSrcweir
213cdf0e10cSrcweir aInfo.URLTemplate = aValue;
214cdf0e10cSrcweir
215cdf0e10cSrcweir // Obtain Arguments.
216cdf0e10cSrcweir aKeyBuffer = aElemBuffer;
217cdf0e10cSrcweir aKeyBuffer.appendAscii( "']/Arguments" );
218cdf0e10cSrcweir
219cdf0e10cSrcweir if ( !( xHierNameAccess->getByHierarchicalName(
220cdf0e10cSrcweir aKeyBuffer.makeStringAndClear() ) >>= aValue ) )
221cdf0e10cSrcweir {
222cdf0e10cSrcweir OSL_ENSURE( false,
223cdf0e10cSrcweir "getContentProviderData - "
224cdf0e10cSrcweir "Error getting item value!" );
225cdf0e10cSrcweir continue;
226cdf0e10cSrcweir }
227cdf0e10cSrcweir
228cdf0e10cSrcweir aInfo.Arguments = aValue;
229cdf0e10cSrcweir
230cdf0e10cSrcweir // Append info to list.
231cdf0e10cSrcweir rListToFill.push_back( aInfo );
232cdf0e10cSrcweir }
233cdf0e10cSrcweir catch ( container::NoSuchElementException& )
234cdf0e10cSrcweir {
235cdf0e10cSrcweir // getByHierarchicalName
236cdf0e10cSrcweir
237cdf0e10cSrcweir OSL_ENSURE( false,
238cdf0e10cSrcweir "getContentProviderData - "
239cdf0e10cSrcweir "caught NoSuchElementException!" );
240cdf0e10cSrcweir }
241cdf0e10cSrcweir }
242cdf0e10cSrcweir }
243cdf0e10cSrcweir }
244cdf0e10cSrcweir catch ( uno::RuntimeException& )
245cdf0e10cSrcweir {
246cdf0e10cSrcweir OSL_ENSURE( false,
247cdf0e10cSrcweir "getContentProviderData - caught RuntimeException!" );
248cdf0e10cSrcweir return false;
249cdf0e10cSrcweir }
250cdf0e10cSrcweir catch ( uno::Exception& )
251cdf0e10cSrcweir {
252cdf0e10cSrcweir // createInstance, createInstanceWithArguments
253cdf0e10cSrcweir
254cdf0e10cSrcweir OSL_ENSURE( false,
255cdf0e10cSrcweir "getContentProviderData - caught Exception!" );
256cdf0e10cSrcweir return false;
257cdf0e10cSrcweir }
258cdf0e10cSrcweir
259cdf0e10cSrcweir return true;
260cdf0e10cSrcweir }
261cdf0e10cSrcweir
262cdf0e10cSrcweir }
263