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