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