update dev300-m58
[ooovba.git] / configmgr / source / backendhelper / backendlayerhelper.cxx
blobf58f187ecb747ca1f40c4471bbbdc7d46a2f50c1
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: backendlayerhelper.cxx,v $
10 * $Revision: 1.5 $
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 ************************************************************************/
32 // MARKER(update_precomp.py): autogen include statement, do not remove
33 #include "precompiled_configmgr.hxx"
36 #include "backendlayerhelper.hxx"
37 #include <com/sun/star/configuration/backend/PropertyInfo.hpp>
39 namespace configmgr { namespace backendhelper {
41 //==============================================================================
43 //------------------------------------------------------------------------------
44 uno::Type toType(const ::rtl::OUString& _rType)
46 uno::Type aRet;
48 if (_rType.equalsIgnoreAsciiCase(::rtl::OUString::createFromAscii("boolean")))
49 aRet = ::getBooleanCppuType();
51 else if(_rType.equalsIgnoreAsciiCase(::rtl::OUString::createFromAscii("short")))
52 aRet = ::getCppuType(static_cast<sal_Int16 const*>(0));
54 else if(_rType.equalsIgnoreAsciiCase(::rtl::OUString::createFromAscii("int")))
55 aRet = ::getCppuType(static_cast<sal_Int32 const*>(0));
57 else if(_rType.equalsIgnoreAsciiCase(::rtl::OUString::createFromAscii("integer")))
58 aRet = ::getCppuType(static_cast<sal_Int32 const*>(0));
60 else if(_rType.equalsIgnoreAsciiCase(::rtl::OUString::createFromAscii("long")))
61 aRet = ::getCppuType(static_cast<sal_Int64 const*>(0));
63 else if(_rType.equalsIgnoreAsciiCase(::rtl::OUString::createFromAscii("double")))
64 aRet = ::getCppuType(static_cast<double const*>(0));
66 else if(_rType.equalsIgnoreAsciiCase(::rtl::OUString::createFromAscii("string")))
67 aRet = ::getCppuType(static_cast<rtl::OUString const*>(0));
69 else if(_rType.equalsIgnoreAsciiCase(::rtl::OUString::createFromAscii("binary")))
70 aRet = ::getCppuType(static_cast<uno::Sequence<sal_Int8> const*>(0));
72 else if(_rType.equalsIgnoreAsciiCase(::rtl::OUString::createFromAscii("any")))
73 aRet = ::getCppuType(static_cast<uno::Any const*>(0));
75 else if(_rType.equalsIgnoreAsciiCase(::rtl::OUString::createFromAscii("boolean-list")))
76 aRet = ::getCppuType(static_cast<uno::Sequence<sal_Bool> const*>(0));
78 else if(_rType.equalsIgnoreAsciiCase(::rtl::OUString::createFromAscii("short-list")))
79 aRet = ::getCppuType(static_cast<uno::Sequence<sal_Int16> const*>(0));
81 else if(_rType.equalsIgnoreAsciiCase(::rtl::OUString::createFromAscii("int-list")))
82 aRet = ::getCppuType(static_cast<uno::Sequence<sal_Int32> const*>(0));
84 else if(_rType.equalsIgnoreAsciiCase(::rtl::OUString::createFromAscii("integer-list")))
85 aRet = ::getCppuType(static_cast<uno::Sequence<sal_Int32> const*>(0));
87 else if(_rType.equalsIgnoreAsciiCase(::rtl::OUString::createFromAscii("long-list")))
88 aRet = ::getCppuType(static_cast<uno::Sequence<sal_Int64> const*>(0));
90 else if(_rType.equalsIgnoreAsciiCase(::rtl::OUString::createFromAscii("double-list")))
91 aRet = ::getCppuType(static_cast<uno::Sequence<double> const*>(0));
93 else if(_rType.equalsIgnoreAsciiCase(::rtl::OUString::createFromAscii("string-list")))
94 aRet = ::getCppuType(static_cast<uno::Sequence<rtl::OUString> const*>(0));
96 else if(_rType.equalsIgnoreAsciiCase(::rtl::OUString::createFromAscii("binary-list")))
97 aRet = ::getCppuType(static_cast<uno::Sequence<uno::Sequence<sal_Int8> > const*>(0));
99 else
101 ::rtl::OString aStr("Unknown type! ");
102 aStr += rtl::OUStringToOString(_rType,RTL_TEXTENCODING_ASCII_US);
103 OSL_ENSURE(0,aStr.getStr());
106 return aRet;
109 //------------------------------------------------------------------------------
111 IOONode::IOONode(const rtl::OUString& sName):
112 mName(sName)
116 //------------------------------------------------------------------------------
117 OOProperty::OOProperty(
118 const rtl::OUString& sName,const rtl::OUString& sPropType,
119 const uno::Any& aPropValue,sal_Bool bProtected)
120 :IOONode(sName), mPropType(sPropType), mPropValue(aPropValue),
121 mbProtected(bProtected)
124 //------------------------------------------------------------------------------
125 OONode::OONode(const rtl::OUString& sName)
126 :IOONode(sName)
130 OONode::OONode()
131 :IOONode(rtl::OUString())
134 IOONode* OONode::addChild(IOONode* aChild)
136 mChildList.push_back(aChild);
137 return aChild;
139 const std::vector<IOONode*>& OONode::getChildren()
141 return mChildList;
144 IOONode* OONode::getChild(const rtl::OUString& aChildName)
146 for (sal_uInt32 i=0; i< mChildList.size();++i)
148 if (mChildList[i]->getName() == aChildName)
149 return mChildList[i];
151 return NULL;
153 OONode::~OONode()
155 for (sal_uInt32 i=0; i< mChildList.size();++i)
157 delete mChildList[i];
159 mChildList.clear();
161 //------------------------------------------------------------------------------
162 sal_Bool addChildrenToNodeTree(
163 OONode * aNode,
164 sal_Int32 nNextToken,
165 const backend::PropertyInfo& aPropInfo,
166 const uno::Reference<uno::XInterface>& xContext)
170 rtl::OUString aName = aPropInfo.Name.getToken(0, '/',nNextToken);
171 if (aName.getLength() == 0)
173 throw backend::MalformedDataException(
174 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(
175 "Malformed OpenOffice Key specified")),
176 xContext, uno::Any()) ;
178 //Check if Property -> nNextToken == -1
179 if(nNextToken != -1)
181 //check if child already exists
182 IOONode* aChildNode= aNode->getChild(aName);
183 if (aChildNode == NULL)
185 aChildNode = new OONode(aName);
186 if (aChildNode != 0)
188 aNode->addChild( aChildNode);
192 sal_Bool bFinished =addChildrenToNodeTree(
193 aChildNode->getComposite(),
194 nNextToken,
195 aPropInfo,
196 xContext);
197 //Check that if you have finished parsing string therefore no
198 //more children
199 if (bFinished)
200 break;
202 else
204 //Add Property
205 IOONode* aProperty = new OOProperty(aName,
206 aPropInfo.Type,
207 aPropInfo.Value,
208 aPropInfo.Protected);
209 if (aProperty != 0)
211 aNode->addChild( aProperty);
213 //Return finished is true when you are finished parsing the string
214 if( nNextToken == -1)
216 return sal_True;
220 while (nNextToken >= 0 ) ;
221 return sal_True;
223 //------------------------------------------------------------------------------
224 void processChildren(
225 std::vector<IOONode*> aChildList,
226 const uno::Reference<backend::XLayerHandler>& xHandler)
228 for(sal_uInt32 i=0; i <aChildList.size(); ++i)
230 OONode * aTestOONode = aChildList[i]->getComposite();
231 if (aTestOONode)
233 xHandler->overrideNode(aTestOONode->getName(),0,false);
234 processChildren(aTestOONode->getChildren(),xHandler);
235 xHandler->endNode();
237 else
239 OOProperty* aProperty = aChildList[i]->asOOProperty();
240 sal_Int16 aAttributes = aProperty->isProtected() ? 256:0;
241 //Convert Type either simple or list
242 uno::Type aType = toType( aProperty->getType());
244 xHandler->overrideProperty(aProperty->getName(),
245 aAttributes,
246 aType,
247 false);
249 xHandler->setPropertyValue(aProperty->getValue());
250 xHandler->endProperty();
255 //------------------------------------------------------------------------------
256 void buildNodeTree(
257 const uno::Sequence< backend::PropertyInfo >& aPropertyInfos,
258 const uno::Reference<uno::XInterface>& xContext,
259 OONode& aNodeTree)
261 sal_Int32 nNextToken =0;
262 rtl::OUString aName = aPropertyInfos[0].Name.getToken(0, '/',nNextToken);
263 if((nNextToken ==-1)||(aName.getLength()==0))
265 throw backend::MalformedDataException(
266 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(
267 "Malformed OpenOffice Key specified")),
268 xContext, uno::Any()) ;
271 aNodeTree.setName(aName);
272 sal_Int32 size = aPropertyInfos.getLength();
273 for (sal_Int32 i =0; i < size; ++i)
275 addChildrenToNodeTree(&aNodeTree, nNextToken,aPropertyInfos[i],xContext);
279 //------------------------------------------------------------------------------
280 BackendLayerHelper::BackendLayerHelper(
281 const uno::Reference<uno::XComponentContext>& /*xContext*/)
282 :cppu::WeakComponentImplHelper2<backend::XLayerContentDescriber, lang::XServiceInfo>(mMutex)
285 //------------------------------------------------------------------------------
287 BackendLayerHelper::~BackendLayerHelper(void) {}
289 //------------------------------------------------------------------------------
291 rtl::OUString SAL_CALL BackendLayerHelper::
292 getBackendLayerHelperName(void)
294 static const rtl::OUString kImplementationName(
295 RTL_CONSTASCII_USTRINGPARAM(
296 "com.sun.star.comp.configuration.backend.LayerDescriber")) ;
298 return kImplementationName ;
300 //------------------------------------------------------------------------------
301 void SAL_CALL BackendLayerHelper::describeLayer(
302 const uno::Reference< backend::XLayerHandler >& xHandler,
303 const uno::Sequence< backend::PropertyInfo >& aPropertyInfos )
304 throw (lang::NullPointerException,
305 backend::MalformedDataException,
306 uno::RuntimeException)
311 OONode aNodeTree;
312 buildNodeTree(aPropertyInfos, *this, aNodeTree);
314 //Descirbe the Layer to the XHandler Object
315 xHandler->startLayer();
316 xHandler->overrideNode(aNodeTree.getName(),0,false);
317 std::vector<IOONode*> aChildList = aNodeTree.getChildren();
318 processChildren(aChildList,xHandler);
319 xHandler->endNode();
320 xHandler->endLayer();
324 //------------------------------------------------------------------------------
326 rtl::OUString SAL_CALL BackendLayerHelper::getImplementationName(void)
327 throw (uno::RuntimeException)
329 return getBackendLayerHelperName() ;
331 //------------------------------------------------------------------------------
333 uno::Sequence<rtl::OUString> SAL_CALL BackendLayerHelper::
334 getBackendLayerHelperServiceNames(void)
336 uno::Sequence<rtl::OUString> aServices(1) ;
337 aServices[0] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.configuration.backend.LayerDescriber")) ;
338 return aServices ;
340 //------------------------------------------------------------------------------
342 sal_Bool SAL_CALL BackendLayerHelper::supportsService(
343 const rtl::OUString& aServiceName)
344 throw (uno::RuntimeException)
346 uno::Sequence< rtl::OUString > const svc = getBackendLayerHelperServiceNames();
348 for(sal_Int32 i = 0; i < svc.getLength(); ++i )
349 if(svc[i] == aServiceName)
350 return true;
351 return false;
353 //------------------------------------------------------------------------------
355 uno::Sequence<rtl::OUString>
356 SAL_CALL BackendLayerHelper::getSupportedServiceNames(void)
357 throw (uno::RuntimeException)
359 return getBackendLayerHelperServiceNames() ;
361 //------------------------------------------------------------------------------
363 } // backendhelper
364 } // configmgr