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 #include <ucbhelper/configureucb.hxx>
27 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
28 #include <com/sun/star/ucb/XContentProvider.hpp>
29 #include <com/sun/star/ucb/XContentProviderManager.hpp>
30 #include <com/sun/star/uno/Any.hxx>
31 #include <com/sun/star/uno/RuntimeException.hpp>
32 #include <rtl/ustrbuf.hxx>
33 
34 #include "osl/diagnose.h"
35 
36 #ifndef _UCBHELPER_PROVCONF_HXX_
37 #include <provconf.hxx>
38 #endif
39 #include <registerucb.hxx>
40 
41 using namespace com::sun::star;
42 
43 namespace {
44 
fillPlaceholders(rtl::OUString const & rInput,uno::Sequence<uno::Any> const & rReplacements,rtl::OUString * pOutput)45 bool fillPlaceholders(rtl::OUString const & rInput,
46 					  uno::Sequence< uno::Any > const & rReplacements,
47 					  rtl::OUString * pOutput)
48 {
49 	sal_Unicode const * p = rInput.getStr();
50 	sal_Unicode const * pEnd = p + rInput.getLength();
51 	sal_Unicode const * pCopy = p;
52 	rtl::OUStringBuffer aBuffer;
53 	while (p != pEnd)
54 		switch (*p++)
55 		{
56 			case '&':
57 				if (pEnd - p >= 4
58 					&& p[0] == 'a' && p[1] == 'm' && p[2] == 'p'
59 					&& p[3] == ';')
60 				{
61 					aBuffer.append(pCopy, p - 1 - pCopy);
62 					aBuffer.append(sal_Unicode('&'));
63 					p += 4;
64 					pCopy = p;
65 				}
66 				else if (pEnd - p >= 3
67 						 && p[0] == 'l' && p[1] == 't' && p[2] == ';')
68 				{
69 					aBuffer.append(pCopy, p - 1 - pCopy);
70 					aBuffer.append(sal_Unicode('<'));
71 					p += 3;
72 					pCopy = p;
73 				}
74 				else if (pEnd - p >= 3
75 						 && p[0] == 'g' && p[1] == 't' && p[2] == ';')
76 				{
77 					aBuffer.append(pCopy, p - 1 - pCopy);
78 					aBuffer.append(sal_Unicode('>'));
79 					p += 3;
80 					pCopy = p;
81 				}
82 				break;
83 
84 			case '<':
85 				sal_Unicode const * q = p;
86 				while (q != pEnd && *q != '>')
87 					++q;
88 				if (q == pEnd)
89 					break;
90 				rtl::OUString aKey(p, q - p);
91 				rtl::OUString aValue;
92 				bool bFound = false;
93 				for (sal_Int32 i = 2; i + 1 < rReplacements.getLength();
94 					 i += 2)
95 				{
96 					rtl::OUString aReplaceKey;
97 					if ((rReplacements[i] >>= aReplaceKey)
98 						&& aReplaceKey == aKey
99 						&& (rReplacements[i + 1] >>= aValue))
100 					{
101 						bFound = true;
102 						break;
103 					}
104 				}
105 				if (!bFound)
106 					return false;
107 				aBuffer.append(pCopy, p - 1 - pCopy);
108 				aBuffer.append(aValue);
109 				p = q + 1;
110 				pCopy = p;
111 				break;
112 		}
113 	aBuffer.append(pCopy, pEnd - pCopy);
114 	*pOutput = aBuffer.makeStringAndClear();
115 	return true;
116 }
117 
118 }
119 
120 namespace ucbhelper {
121 
122 //============================================================================
123 //
124 //  configureUcb
125 //
126 //============================================================================
127 
128 bool
configureUcb(uno::Reference<ucb::XContentProviderManager> const & rManager,uno::Reference<lang::XMultiServiceFactory> const & rServiceFactory,ContentProviderDataList const & rData,ContentProviderRegistrationInfoList * pInfos)129 configureUcb(
130 	uno::Reference< ucb::XContentProviderManager > const & rManager,
131 	uno::Reference< lang::XMultiServiceFactory > const & rServiceFactory,
132     ContentProviderDataList const & rData,
133     ContentProviderRegistrationInfoList * pInfos)
134 	throw (uno::RuntimeException)
135 {
136     ContentProviderDataList::const_iterator aEnd(rData.end());
137     for (ContentProviderDataList::const_iterator aIt(rData.begin());
138 		 aIt != aEnd; ++aIt)
139 	{
140         ContentProviderRegistrationInfo aInfo;
141         bool bSuccess = registerAtUcb(rManager,
142                                       rServiceFactory,
143                                       aIt->ServiceName,
144                                       aIt->Arguments,
145                                       aIt->URLTemplate,
146                                       &aInfo);
147 
148         if (bSuccess && pInfos)
149             pInfos->push_back(aInfo);
150 	}
151 
152 	return true;
153 }
154 
155 //============================================================================
156 //
157 //  configureUcb
158 //
159 //============================================================================
160 
161 bool
configureUcb(uno::Reference<ucb::XContentProviderManager> const & rManager,uno::Reference<lang::XMultiServiceFactory> const & rServiceFactory,uno::Sequence<uno::Any> const & rArguments,std::vector<ContentProviderRegistrationInfo> * pInfos)162 configureUcb(
163 	uno::Reference< ucb::XContentProviderManager > const & rManager,
164 	uno::Reference< lang::XMultiServiceFactory > const & rServiceFactory,
165 	uno::Sequence< uno::Any > const & rArguments,
166 	std::vector< ContentProviderRegistrationInfo > * pInfos)
167 	throw (uno::RuntimeException)
168 {
169 	rtl::OUString aKey1;
170 	rtl::OUString aKey2;
171 	if (rArguments.getLength() < 2
172 		|| !(rArguments[0] >>= aKey1) || !(rArguments[1] >>= aKey2))
173 	{
174 		OSL_ENSURE(false, "ucb::configureUcb(): Bad arguments");
175 		return false;
176 	}
177 
178 	ContentProviderDataList aData;
179 	if (!getContentProviderData(rServiceFactory, aKey1, aKey2, aData))
180 	{
181 		OSL_ENSURE(false, "ucb::configureUcb(): No configuration");
182 		return false;
183 	}
184 
185 	ContentProviderDataList::const_iterator aEnd(aData.end());
186 	for (ContentProviderDataList::const_iterator aIt(aData.begin());
187 		 aIt != aEnd; ++aIt)
188 	{
189 		rtl::OUString aProviderArguments;
190 		if (fillPlaceholders(aIt->Arguments,
191 							 rArguments,
192 							 &aProviderArguments))
193 		{
194 			ContentProviderRegistrationInfo aInfo;
195 			bool bSuccess = registerAtUcb(rManager,
196 										  rServiceFactory,
197 										  aIt->ServiceName,
198 										  aProviderArguments,
199 										  aIt->URLTemplate,
200 										  &aInfo);
201 			OSL_ENSURE(bSuccess, "ucb::configureUcb(): Bad content provider");
202 
203 			if (bSuccess && pInfos)
204 				pInfos->push_back(aInfo);
205 		}
206 		else
207 			OSL_ENSURE(false,
208 					   "ucb::configureUcb(): Bad argument placeholders");
209 	}
210 
211 	return true;
212 }
213 
214 }
215 
216 //============================================================================
217 //
218 //  unconfigureUcb
219 //
220 //============================================================================
221 
222 namespace ucbhelper {
223 
224 void
unconfigureUcb(uno::Reference<ucb::XContentProviderManager> const & rManager,std::vector<ContentProviderRegistrationInfo> const & rInfos)225 unconfigureUcb(
226 	uno::Reference< ucb::XContentProviderManager > const & rManager,
227 	std::vector< ContentProviderRegistrationInfo > const & rInfos)
228 	throw (uno::RuntimeException)
229 {
230 	std::vector< ContentProviderRegistrationInfo >::const_iterator
231 		aEnd(rInfos.end());
232 	for (std::vector< ContentProviderRegistrationInfo >::const_iterator
233 			 aIt(rInfos.begin());
234 		 aIt != aEnd; ++aIt)
235 		deregisterFromUcb(rManager, *aIt);
236 }
237 
238 }
239