Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*
3 : : * This file is part of the LibreOffice project.
4 : : *
5 : : * This Source Code Form is subject to the terms of the Mozilla Public
6 : : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : : *
9 : : * This file incorporates work covered by the following license notice:
10 : : *
11 : : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : : * contributor license agreements. See the NOTICE file distributed
13 : : * with this work for additional information regarding copyright
14 : : * ownership. The ASF licenses this file to you under the Apache
15 : : * License, Version 2.0 (the "License"); you may not use this file
16 : : * except in compliance with the License. You may obtain a copy of
17 : : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : : */
19 : :
20 : :
21 : : #include <unotools/confignode.hxx>
22 : : #include <unotools/configpaths.hxx>
23 : : #include <tools/diagnose_ex.h>
24 : : #include <osl/diagnose.h>
25 : : #include <com/sun/star/container/XHierarchicalName.hpp>
26 : : #include <com/sun/star/beans/PropertyValue.hpp>
27 : : #include <com/sun/star/lang/XSingleServiceFactory.hpp>
28 : : #include <com/sun/star/lang/XComponent.hpp>
29 : : #include <com/sun/star/util/XStringEscape.hpp>
30 : : #include <com/sun/star/lang/XServiceInfo.hpp>
31 : : #include <com/sun/star/container/XNamed.hpp>
32 : : #include <comphelper/extract.hxx>
33 : : #include <comphelper/componentcontext.hxx>
34 : : #include <comphelper/namedvaluecollection.hxx>
35 : : #include <rtl/string.hxx>
36 : : #if OSL_DEBUG_LEVEL > 0
37 : : #include <rtl/strbuf.hxx>
38 : : #endif
39 : :
40 : : //........................................................................
41 : : namespace utl
42 : : {
43 : : //........................................................................
44 : :
45 : : using namespace ::com::sun::star::uno;
46 : : using namespace ::com::sun::star::lang;
47 : : using namespace ::com::sun::star::util;
48 : : using namespace ::com::sun::star::beans;
49 : : using namespace ::com::sun::star::container;
50 : :
51 : : //========================================================================
52 : : //= OConfigurationNode
53 : : //========================================================================
54 : : //------------------------------------------------------------------------
55 : 4203 : OConfigurationNode::OConfigurationNode(const Reference< XInterface >& _rxNode )
56 : 4203 : :m_bEscapeNames(sal_False)
57 : : {
58 : : OSL_ENSURE(_rxNode.is(), "OConfigurationNode::OConfigurationNode: invalid node interface!");
59 [ + - ]: 4203 : if (_rxNode.is())
60 : : {
61 : : // collect all interfaces necessary
62 [ + - ][ + - ]: 4203 : m_xHierarchyAccess = Reference< XHierarchicalNameAccess >(_rxNode, UNO_QUERY);
63 [ + - ][ + - ]: 4203 : m_xDirectAccess = Reference< XNameAccess >(_rxNode, UNO_QUERY);
64 : :
65 : : // reset _all_ interfaces if _one_ of them is not supported
66 [ - + ][ - + ]: 4203 : if (!m_xHierarchyAccess.is() || !m_xDirectAccess.is())
[ + - ]
67 : : {
68 [ # # ]: 0 : m_xHierarchyAccess = NULL;
69 [ # # ]: 0 : m_xDirectAccess = NULL;
70 : : }
71 : :
72 : : // now for the non-critical interfaces
73 [ + - ][ + - ]: 4203 : m_xReplaceAccess = Reference< XNameReplace >(_rxNode, UNO_QUERY);
74 [ + - ][ + - ]: 4203 : m_xContainerAccess = Reference< XNameContainer >(_rxNode, UNO_QUERY);
75 : : }
76 : :
77 [ + - ]: 4203 : Reference< XComponent > xConfigNodeComp(m_xDirectAccess, UNO_QUERY);
78 [ + - ]: 4203 : if (xConfigNodeComp.is())
79 [ + - ]: 4203 : startComponentListening(xConfigNodeComp);
80 : :
81 [ + - ]: 4203 : if (isValid())
82 [ + - ][ + - ]: 4203 : setEscape(isSetNode());
83 : 4203 : }
84 : :
85 : : //------------------------------------------------------------------------
86 : 2096 : OConfigurationNode::OConfigurationNode(const OConfigurationNode& _rSource)
87 : : :OEventListenerAdapter()
88 : : ,m_xHierarchyAccess(_rSource.m_xHierarchyAccess)
89 : : ,m_xDirectAccess(_rSource.m_xDirectAccess)
90 : : ,m_xReplaceAccess(_rSource.m_xReplaceAccess)
91 : : ,m_xContainerAccess(_rSource.m_xContainerAccess)
92 : : ,m_bEscapeNames(_rSource.m_bEscapeNames)
93 : 2096 : ,m_sCompletePath(_rSource.m_sCompletePath)
94 : : {
95 [ + - ]: 2096 : Reference< XComponent > xConfigNodeComp(m_xDirectAccess, UNO_QUERY);
96 [ + - ]: 2096 : if (xConfigNodeComp.is())
97 [ + - ]: 2096 : startComponentListening(xConfigNodeComp);
98 : 2096 : }
99 : :
100 : : //------------------------------------------------------------------------
101 : 43 : const OConfigurationNode& OConfigurationNode::operator=(const OConfigurationNode& _rSource)
102 : : {
103 [ + - ]: 43 : stopAllComponentListening();
104 : :
105 [ + - ]: 43 : m_xHierarchyAccess = _rSource.m_xHierarchyAccess;
106 [ + - ]: 43 : m_xDirectAccess = _rSource.m_xDirectAccess;
107 [ + - ]: 43 : m_xContainerAccess = _rSource.m_xContainerAccess;
108 [ + - ]: 43 : m_xReplaceAccess = _rSource.m_xReplaceAccess;
109 : 43 : m_bEscapeNames = _rSource.m_bEscapeNames;
110 : 43 : m_sCompletePath = _rSource.m_sCompletePath;
111 : :
112 [ + - ]: 43 : Reference< XComponent > xConfigNodeComp(m_xDirectAccess, UNO_QUERY);
113 [ + - ]: 43 : if (xConfigNodeComp.is())
114 [ + - ]: 43 : startComponentListening(xConfigNodeComp);
115 : :
116 : 43 : return *this;
117 : : }
118 : :
119 : : //------------------------------------------------------------------------
120 : 0 : void OConfigurationNode::_disposing( const EventObject& _rSource )
121 : : {
122 [ # # ]: 0 : Reference< XComponent > xDisposingSource(_rSource.Source, UNO_QUERY);
123 [ # # ]: 0 : Reference< XComponent > xConfigNodeComp(m_xDirectAccess, UNO_QUERY);
124 [ # # ][ # # ]: 0 : if (xDisposingSource.get() == xConfigNodeComp.get())
[ # # ]
125 : 0 : clear();
126 : 0 : }
127 : :
128 : : //------------------------------------------------------------------------
129 : 302 : ::rtl::OUString OConfigurationNode::getLocalName() const
130 : : {
131 : 302 : ::rtl::OUString sLocalName;
132 : : try
133 : : {
134 [ + - ]: 302 : Reference< XNamed > xNamed( m_xDirectAccess, UNO_QUERY_THROW );
135 [ + - ][ + - ]: 302 : sLocalName = xNamed->getName();
[ # # ]
136 : : }
137 [ # # ]: 0 : catch( const Exception& )
138 : : {
139 : : DBG_UNHANDLED_EXCEPTION();
140 : : }
141 : 302 : return sLocalName;
142 : : }
143 : :
144 : : //------------------------------------------------------------------------
145 : 130 : ::rtl::OUString OConfigurationNode::getNodePath() const
146 : : {
147 : 130 : ::rtl::OUString sNodePath;
148 : : try
149 : : {
150 [ + - ]: 130 : Reference< XHierarchicalName > xNamed( m_xDirectAccess, UNO_QUERY_THROW );
151 [ + - ][ + - ]: 130 : sNodePath = xNamed->getHierarchicalName();
[ # # ]
152 : : }
153 [ # # ]: 0 : catch( const Exception& )
154 : : {
155 : : DBG_UNHANDLED_EXCEPTION();
156 : : }
157 : 130 : return sNodePath;
158 : : }
159 : :
160 : : //------------------------------------------------------------------------
161 : 25530 : ::rtl::OUString OConfigurationNode::normalizeName(const ::rtl::OUString& _rName, NAMEORIGIN _eOrigin) const
162 : : {
163 : 25530 : ::rtl::OUString sName(_rName);
164 [ - + ]: 25530 : if (getEscape())
165 : : {
166 [ # # ]: 0 : Reference< XStringEscape > xEscaper(m_xDirectAccess, UNO_QUERY);
167 [ # # ][ # # ]: 0 : if (xEscaper.is() && !sName.isEmpty())
[ # # ]
168 : : {
169 : : try
170 : : {
171 [ # # ]: 0 : if (NO_CALLER == _eOrigin)
172 [ # # ][ # # ]: 0 : sName = xEscaper->escapeString(sName);
173 : : else
174 [ # # ][ # # ]: 0 : sName = xEscaper->unescapeString(sName);
175 : : }
176 [ # # ]: 0 : catch(Exception&)
177 : : {
178 : : DBG_UNHANDLED_EXCEPTION();
179 : : }
180 : 0 : }
181 : : }
182 [ # # ]: 25530 : return sName;
183 : : }
184 : :
185 : : //------------------------------------------------------------------------
186 : 2274 : Sequence< ::rtl::OUString > OConfigurationNode::getNodeNames() const throw()
187 : : {
188 : : OSL_ENSURE(m_xDirectAccess.is(), "OConfigurationNode::getNodeNames: object is invalid!");
189 : 2274 : Sequence< ::rtl::OUString > aReturn;
190 [ + - ]: 2274 : if (m_xDirectAccess.is())
191 : : {
192 : : try
193 : : {
194 [ + - ][ + - ]: 2274 : aReturn = m_xDirectAccess->getElementNames();
[ + - ]
[ + - # # ]
195 : : // normalize the names
196 [ + - ]: 2274 : ::rtl::OUString* pNames = aReturn.getArray();
197 [ + + ]: 14158 : for (sal_Int32 i=0; i<aReturn.getLength(); ++i, ++pNames)
198 [ + - ]: 11884 : *pNames = normalizeName(*pNames, NO_CONFIGURATION);
199 : : }
200 [ # # ]: 0 : catch(Exception&)
201 : : {
202 : : OSL_FAIL("OConfigurationNode::getNodeNames: caught a generic exception!");
203 : : }
204 : : }
205 : :
206 : 2274 : return aReturn;
207 : : }
208 : :
209 : : //------------------------------------------------------------------------
210 : 172 : sal_Bool OConfigurationNode::removeNode(const ::rtl::OUString& _rName) const throw()
211 : : {
212 : : OSL_ENSURE(m_xContainerAccess.is(), "OConfigurationNode::removeNode: object is invalid!");
213 [ + - ]: 172 : if (m_xContainerAccess.is())
214 : : {
215 : : try
216 : : {
217 [ + - ]: 172 : ::rtl::OUString sName = normalizeName(_rName, NO_CALLER);
218 [ + - ][ + - ]: 172 : m_xContainerAccess->removeByName(sName);
219 [ # # # # ]: 172 : return sal_True;
220 : : }
221 : 0 : catch (NoSuchElementException&)
222 : : {
223 : : #if OSL_DEBUG_LEVEL > 0
224 : : rtl::OStringBuffer aBuf( 256 );
225 : : aBuf.append("OConfigurationNode::removeNode: there is no element named!");
226 : : aBuf.append( rtl::OUStringToOString( _rName, RTL_TEXTENCODING_ASCII_US ) );
227 : : aBuf.append( "!" );
228 : : OSL_FAIL(aBuf.getStr());
229 : : #endif
230 : : }
231 : 0 : catch (WrappedTargetException&)
232 : : {
233 : : OSL_FAIL("OConfigurationNode::removeNode: caught a WrappedTargetException!");
234 : : }
235 : 0 : catch(Exception&)
236 : : {
237 : : OSL_FAIL("OConfigurationNode::removeNode: caught a generic exception!");
238 : : }
239 : : }
240 : 172 : return sal_False;
241 : : }
242 : : //------------------------------------------------------------------------
243 : 174 : OConfigurationNode OConfigurationNode::insertNode(const ::rtl::OUString& _rName,const Reference< XInterface >& _xNode) const throw()
244 : : {
245 [ + - ]: 174 : if(_xNode.is())
246 : : {
247 : : try
248 : : {
249 [ + - ]: 174 : ::rtl::OUString sName = normalizeName(_rName, NO_CALLER);
250 [ + - ][ + - ]: 174 : m_xContainerAccess->insertByName(sName, makeAny(_xNode));
[ + - ]
251 : : // if we're here, all was ok ...
252 [ # # ][ + - ]: 174 : return OConfigurationNode( _xNode );
253 : : }
254 [ # # # # ]: 0 : catch(const Exception&)
255 : : {
256 : : DBG_UNHANDLED_EXCEPTION();
257 : : }
258 : :
259 : : // dispose the child if it has already been created, but could not be inserted
260 [ # # ]: 0 : Reference< XComponent > xChildComp(_xNode, UNO_QUERY);
261 [ # # ]: 0 : if (xChildComp.is())
262 [ # # # # : 0 : try { xChildComp->dispose(); } catch(Exception&) { }
# # ]
263 : : }
264 : :
265 : 174 : return OConfigurationNode();
266 : : }
267 : : //------------------------------------------------------------------------
268 : 174 : OConfigurationNode OConfigurationNode::createNode(const ::rtl::OUString& _rName) const throw()
269 : : {
270 [ + - ]: 174 : Reference< XSingleServiceFactory > xChildFactory(m_xContainerAccess, UNO_QUERY);
271 : : OSL_ENSURE(xChildFactory.is(), "OConfigurationNode::createNode: object is invalid or read-only!");
272 : :
273 [ + - ]: 174 : if (xChildFactory.is()) // implies m_xContainerAccess.is()
274 : : {
275 : 174 : Reference< XInterface > xNewChild;
276 : : try
277 : : {
278 [ + - ][ + - ]: 174 : xNewChild = xChildFactory->createInstance();
[ # # ][ + - ]
279 : : }
280 [ # # ]: 0 : catch(const Exception&)
281 : : {
282 : : DBG_UNHANDLED_EXCEPTION();
283 : : }
284 : 174 : return insertNode(_rName,xNewChild);
285 : : }
286 : :
287 [ # # ]: 174 : return OConfigurationNode();
288 : : }
289 : :
290 : : //------------------------------------------------------------------------
291 : 2868 : OConfigurationNode OConfigurationNode::openNode(const ::rtl::OUString& _rPath) const throw()
292 : : {
293 : : OSL_ENSURE(m_xDirectAccess.is(), "OConfigurationNode::openNode: object is invalid!");
294 : : OSL_ENSURE(m_xHierarchyAccess.is(), "OConfigurationNode::openNode: object is invalid!");
295 : : try
296 : : {
297 [ + - ]: 2868 : ::rtl::OUString sNormalized = normalizeName(_rPath, NO_CALLER);
298 : :
299 : 2868 : Reference< XInterface > xNode;
300 [ + - ][ + - ]: 2868 : if (m_xDirectAccess.is() && m_xDirectAccess->hasByName(sNormalized))
[ + - ][ + - ]
[ + - ]
301 : : {
302 [ + - ][ + - ]: 2868 : if (!::cppu::extractInterface(xNode, m_xDirectAccess->getByName(sNormalized)))
[ + - ]
303 : : OSL_FAIL("OConfigurationNode::openNode: could not open the node!");
304 : : }
305 [ # # ]: 0 : else if (m_xHierarchyAccess.is())
306 : : {
307 [ # # ][ # # ]: 0 : if (!::cppu::extractInterface(xNode, m_xHierarchyAccess->getByHierarchicalName(_rPath)))
[ # # ]
308 : : OSL_FAIL("OConfigurationNode::openNode: could not open the node!");
309 : : }
310 [ + - ]: 2868 : if (xNode.is())
311 [ + - ][ + - ]: 2868 : return OConfigurationNode( xNode );
[ - + ]
[ # # # ]
312 : : }
313 : 0 : catch(const NoSuchElementException&)
314 : : {
315 : : #if OSL_DEBUG_LEVEL > 0
316 : : rtl::OStringBuffer aBuf( 256 );
317 : : aBuf.append("OConfigurationNode::openNode: there is no element named ");
318 : : aBuf.append( rtl::OUStringToOString( _rPath, RTL_TEXTENCODING_ASCII_US ) );
319 : : aBuf.append("!");
320 : : OSL_FAIL(aBuf.getStr());
321 : : #endif
322 : : }
323 : 0 : catch(Exception&)
324 : : {
325 : : OSL_FAIL("OConfigurationNode::openNode: caught an exception while retrieving the node!");
326 : : }
327 : 2868 : return OConfigurationNode();
328 : : }
329 : :
330 : : //------------------------------------------------------------------------
331 : 4203 : void OConfigurationNode::setEscape(sal_Bool _bEnable)
332 : : {
333 [ + + ][ + - ]: 4203 : m_bEscapeNames = _bEnable && Reference< XStringEscape >::query(m_xDirectAccess).is();
[ - + ][ + + ]
[ # # ]
334 : 4203 : }
335 : :
336 : : //------------------------------------------------------------------------
337 : 4203 : sal_Bool OConfigurationNode::isSetNode() const
338 : : {
339 : 4203 : sal_Bool bIsSet = sal_False;
340 [ + - ]: 4203 : Reference< XServiceInfo > xSI(m_xHierarchyAccess, UNO_QUERY);
341 [ + - ]: 4203 : if (xSI.is())
342 : : {
343 [ + - ][ + - ]: 4203 : try { bIsSet = xSI->supportsService(::rtl::OUString("com.sun.star.configuration.SetAccess")); }
[ # # ]
344 [ # # ]: 0 : catch(Exception&) { }
345 : : }
346 : 4203 : return bIsSet;
347 : : }
348 : :
349 : 0 : sal_Bool OConfigurationNode::hasByHierarchicalName( const ::rtl::OUString& _rName ) const throw()
350 : : {
351 : : OSL_ENSURE( m_xHierarchyAccess.is(), "OConfigurationNode::hasByHierarchicalName: no hierarchy access!" );
352 : : try
353 : : {
354 [ # # ]: 0 : if ( m_xHierarchyAccess.is() )
355 : : {
356 [ # # ]: 0 : ::rtl::OUString sName = normalizeName( _rName, NO_CALLER );
357 [ # # ][ # # ]: 0 : return m_xHierarchyAccess->hasByHierarchicalName( sName );
[ # # ]
358 : : }
359 : : }
360 : 0 : catch(Exception&)
361 : : {
362 : : }
363 : 0 : return sal_False;
364 : : }
365 : :
366 : : //------------------------------------------------------------------------
367 : 174 : sal_Bool OConfigurationNode::hasByName(const ::rtl::OUString& _rName) const throw()
368 : : {
369 : : OSL_ENSURE(m_xDirectAccess.is(), "OConfigurationNode::hasByName: object is invalid!");
370 : : try
371 : : {
372 [ + - ]: 174 : ::rtl::OUString sName = normalizeName(_rName, NO_CALLER);
373 [ + - ]: 174 : if (m_xDirectAccess.is())
374 [ + - ][ + - ]: 174 : return m_xDirectAccess->hasByName(sName);
[ - + ][ # # ]
375 : : }
376 : 0 : catch(Exception&)
377 : : {
378 : : }
379 : 174 : return sal_False;
380 : : }
381 : :
382 : : //------------------------------------------------------------------------
383 : 448 : sal_Bool OConfigurationNode::setNodeValue(const ::rtl::OUString& _rPath, const Any& _rValue) const throw()
384 : : {
385 : 448 : sal_Bool bResult = false;
386 : :
387 : : OSL_ENSURE(m_xReplaceAccess.is(), "OConfigurationNode::setNodeValue: object is invalid!");
388 [ + - ]: 448 : if (m_xReplaceAccess.is())
389 : : {
390 : : try
391 : : {
392 : : // check if _rPath is a level-1 path
393 [ + - ]: 448 : ::rtl::OUString sNormalizedName = normalizeName(_rPath, NO_CALLER);
394 [ + - ][ + - ]: 448 : if (m_xReplaceAccess->hasByName(sNormalizedName))
[ + - ]
395 : : {
396 [ + - ][ + - ]: 448 : m_xReplaceAccess->replaceByName(sNormalizedName, _rValue);
397 : 448 : bResult = true;
398 : : }
399 : :
400 : : // check if the name refers to a indirect descendant
401 [ # # ][ # # ]: 0 : else if (m_xHierarchyAccess.is() && m_xHierarchyAccess->hasByHierarchicalName(_rPath))
[ # # ][ # # ]
[ # # ]
402 : : {
403 : : OSL_ASSERT(!_rPath.isEmpty());
404 : :
405 : 0 : ::rtl::OUString sParentPath, sLocalName;
406 : :
407 [ # # ][ # # ]: 0 : if ( splitLastFromConfigurationPath(_rPath, sParentPath, sLocalName) )
408 : : {
409 : 0 : OConfigurationNode aParentAccess = openNode(sParentPath);
410 [ # # ]: 0 : if (aParentAccess.isValid())
411 [ # # ]: 0 : bResult = aParentAccess.setNodeValue(sLocalName, _rValue);
412 : : }
413 : : else
414 : : {
415 [ # # ][ # # ]: 0 : m_xReplaceAccess->replaceByName(sLocalName, _rValue);
416 : 0 : bResult = true;
417 : 0 : }
418 [ # # # # : 448 : }
# ]
419 : :
420 : : }
421 : 0 : catch(IllegalArgumentException&)
422 : : {
423 : : OSL_FAIL("OConfigurationNode::setNodeValue: could not replace the value: caught an IllegalArgumentException!");
424 : : }
425 : 0 : catch(NoSuchElementException&)
426 : : {
427 : : OSL_FAIL("OConfigurationNode::setNodeValue: could not replace the value: caught a NoSuchElementException!");
428 : : }
429 : 0 : catch(WrappedTargetException&)
430 : : {
431 : : OSL_FAIL("OConfigurationNode::setNodeValue: could not replace the value: caught a WrappedTargetException!");
432 : : }
433 : 0 : catch(Exception&)
434 : : {
435 : : OSL_FAIL("OConfigurationNode::setNodeValue: could not replace the value: caught a generic Exception!");
436 : : }
437 : :
438 : :
439 : : }
440 : 448 : return bResult;
441 : : }
442 : :
443 : : //------------------------------------------------------------------------
444 : 9810 : Any OConfigurationNode::getNodeValue(const ::rtl::OUString& _rPath) const throw()
445 : : {
446 : : OSL_ENSURE(m_xDirectAccess.is(), "OConfigurationNode::hasByName: object is invalid!");
447 : : OSL_ENSURE(m_xHierarchyAccess.is(), "OConfigurationNode::hasByName: object is invalid!");
448 : 9810 : Any aReturn;
449 : : try
450 : : {
451 [ + - ]: 9810 : ::rtl::OUString sNormalizedPath = normalizeName(_rPath, NO_CALLER);
452 [ + - ][ + - ]: 9810 : if (m_xDirectAccess.is() && m_xDirectAccess->hasByName(sNormalizedPath) )
[ + - ][ + + ]
[ + + ]
453 : : {
454 [ + - ][ + - ]: 4299 : aReturn = m_xDirectAccess->getByName(sNormalizedPath);
455 : : }
456 [ + - ]: 5511 : else if (m_xHierarchyAccess.is())
457 : : {
458 [ + - ][ + + ]: 5511 : aReturn = m_xHierarchyAccess->getByHierarchicalName(_rPath);
459 [ - + ]: 9810 : }
460 : : }
461 [ + - ]: 3 : catch(const NoSuchElementException&)
462 : : {
463 : : DBG_UNHANDLED_EXCEPTION();
464 : : }
465 : 9810 : return aReturn;
466 : : }
467 : :
468 : : //------------------------------------------------------------------------
469 : 0 : void OConfigurationNode::clear() throw()
470 : : {
471 : 0 : m_xHierarchyAccess.clear();
472 : 0 : m_xDirectAccess.clear();
473 : 0 : m_xReplaceAccess.clear();
474 : 0 : m_xContainerAccess.clear();
475 : 0 : }
476 : :
477 : : //========================================================================
478 : : //= helper
479 : : //========================================================================
480 : : namespace
481 : : {
482 : : static const char s_sProviderServiceName[] = "com.sun.star.configuration.ConfigurationProvider";
483 : :
484 : : //--------------------------------------------------------------------
485 : 549 : Reference< XMultiServiceFactory > lcl_getConfigProvider( const ::comphelper::ComponentContext& i_rContext )
486 : : {
487 : : try
488 : : {
489 [ + - ][ + - ]: 549 : Reference< XMultiServiceFactory > xProvider( i_rContext.createComponent(s_sProviderServiceName), UNO_QUERY_THROW );
[ # # ]
490 : 549 : return xProvider;
491 : : }
492 : 0 : catch ( const Exception& )
493 : : {
494 : : DBG_UNHANDLED_EXCEPTION();
495 : : }
496 : 0 : return NULL;
497 : : }
498 : :
499 : : //--------------------------------------------------------------------
500 : 1161 : Reference< XInterface > lcl_createConfigurationRoot( const Reference< XMultiServiceFactory >& i_rxConfigProvider,
501 : : const ::rtl::OUString& i_rNodePath, const bool i_bUpdatable, const sal_Int32 i_nDepth, const bool i_bLazyWrite )
502 : : {
503 [ - + ]: 1161 : ENSURE_OR_RETURN( i_rxConfigProvider.is(), "invalid provider", NULL );
504 : : try
505 : : {
506 [ + - ]: 1161 : ::comphelper::NamedValueCollection aArgs;
507 [ + - ]: 1161 : aArgs.put( "nodepath", i_rNodePath );
508 [ + - ]: 1161 : aArgs.put( "lazywrite", i_bLazyWrite );
509 [ + - ]: 1161 : aArgs.put( "depth", i_nDepth );
510 : :
511 : : ::rtl::OUString sAccessService( i_bUpdatable ?
512 : : ::rtl::OUString( "com.sun.star.configuration.ConfigurationUpdateAccess" ) :
513 [ + + ][ + + ]: 1161 : ::rtl::OUString( "com.sun.star.configuration.ConfigurationAccess" ));
[ + + ]
514 : :
515 : : Reference< XInterface > xRoot(
516 [ + - ]: 1161 : i_rxConfigProvider->createInstanceWithArguments( sAccessService, aArgs.getWrappedPropertyValues() ),
517 : : UNO_SET_THROW
518 [ + - ][ + - ]: 1161 : );
[ + - ][ + - ]
519 [ + - ][ # # ]: 1161 : return xRoot;
520 : : }
521 : 0 : catch ( const Exception& )
522 : : {
523 : : DBG_UNHANDLED_EXCEPTION();
524 : : }
525 : 0 : return NULL;
526 : : }
527 : : }
528 : :
529 : : //------------------------------------------------------------------------
530 : 1039 : OConfigurationTreeRoot::OConfigurationTreeRoot( const Reference< XInterface >& _rxRootNode )
531 : : :OConfigurationNode( _rxRootNode )
532 [ + - ]: 1039 : ,m_xCommitter( _rxRootNode, UNO_QUERY )
533 : : {
534 : 1039 : }
535 : :
536 : : //------------------------------------------------------------------------
537 : 122 : OConfigurationTreeRoot::OConfigurationTreeRoot( const ::comphelper::ComponentContext& i_rContext, const ::rtl::OUString& i_rNodePath, const bool i_bUpdatable )
538 : : :OConfigurationNode( lcl_createConfigurationRoot( lcl_getConfigProvider( i_rContext.getLegacyServiceFactory() ),
539 : : i_rNodePath, i_bUpdatable, -1, false ).get() )
540 [ + - ][ + - ]: 122 : ,m_xCommitter()
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
541 : : {
542 [ + + ]: 122 : if ( i_bUpdatable )
543 : : {
544 [ + - ]: 38 : m_xCommitter.set( getUNONode(), UNO_QUERY );
545 : : OSL_ENSURE( m_xCommitter.is(), "OConfigurationTreeRoot::OConfigurationTreeRoot: could not create an updatable node!" );
546 : : }
547 : 122 : }
548 : :
549 : : //------------------------------------------------------------------------
550 : 0 : void OConfigurationTreeRoot::clear() throw()
551 : : {
552 : 0 : OConfigurationNode::clear();
553 : 0 : m_xCommitter.clear();
554 : 0 : }
555 : :
556 : : //------------------------------------------------------------------------
557 : 446 : sal_Bool OConfigurationTreeRoot::commit() const throw()
558 : : {
559 : : OSL_ENSURE(isValid(), "OConfigurationTreeRoot::commit: object is invalid!");
560 [ - + ]: 446 : if (!isValid())
561 : 0 : return sal_False;
562 : : OSL_ENSURE(m_xCommitter.is(), "OConfigurationTreeRoot::commit: I'm a readonly node!");
563 [ - + ]: 446 : if (!m_xCommitter.is())
564 : 0 : return sal_False;
565 : :
566 : : try
567 : : {
568 [ + - ][ + - ]: 446 : m_xCommitter->commitChanges();
569 [ # # ]: 446 : return sal_True;
570 : : }
571 : 0 : catch(const Exception&)
572 : : {
573 : : DBG_UNHANDLED_EXCEPTION();
574 : : }
575 : 0 : return sal_False;
576 : : }
577 : :
578 : : //------------------------------------------------------------------------
579 : 1039 : OConfigurationTreeRoot OConfigurationTreeRoot::createWithProvider(const Reference< XMultiServiceFactory >& _rxConfProvider, const ::rtl::OUString& _rPath, sal_Int32 _nDepth, CREATION_MODE _eMode, sal_Bool _bLazyWrite)
580 : : {
581 : : Reference< XInterface > xRoot( lcl_createConfigurationRoot(
582 [ + - ]: 1039 : _rxConfProvider, _rPath, _eMode != CM_READONLY, _nDepth, _bLazyWrite ) );
583 [ + - ]: 1039 : if ( xRoot.is() )
584 [ + - ]: 1039 : return OConfigurationTreeRoot( xRoot );
585 [ # # ]: 1039 : return OConfigurationTreeRoot();
586 : : }
587 : :
588 : : //------------------------------------------------------------------------
589 : 427 : OConfigurationTreeRoot OConfigurationTreeRoot::createWithServiceFactory( const Reference< XMultiServiceFactory >& _rxORB, const ::rtl::OUString& _rPath, sal_Int32 _nDepth, CREATION_MODE _eMode, sal_Bool _bLazyWrite )
590 : : {
591 [ + - ][ + - ]: 427 : return createWithProvider( lcl_getConfigProvider( _rxORB ), _rPath, _nDepth, _eMode, _bLazyWrite );
592 : : }
593 : :
594 : : //------------------------------------------------------------------------
595 : 612 : OConfigurationTreeRoot OConfigurationTreeRoot::tryCreateWithServiceFactory( const Reference< XMultiServiceFactory >& _rxORB,
596 : : const ::rtl::OUString& _rPath, sal_Int32 _nDepth , CREATION_MODE _eMode , sal_Bool _bLazyWrite )
597 : : {
598 : : OSL_ENSURE( _rxORB.is(), "OConfigurationTreeRoot::tryCreateWithServiceFactory: invalid service factory!" );
599 [ + - ]: 612 : if ( _rxORB.is() )
600 : : {
601 : : try
602 : : {
603 [ + - ][ + - ]: 612 : Reference< XMultiServiceFactory > xConfigFactory( _rxORB->createInstance(s_sProviderServiceName), UNO_QUERY );
[ + - ]
604 [ + - ]: 612 : if ( xConfigFactory.is() )
605 [ + - ][ - + ]: 612 : return createWithProvider( xConfigFactory, _rPath, _nDepth, _eMode, _bLazyWrite );
[ # # ]
606 : : }
607 : 0 : catch(const Exception&)
608 : : {
609 : : // silence this, 'cause the contract of this method states "no assertions"
610 : : }
611 : : }
612 : 612 : return OConfigurationTreeRoot();
613 : : }
614 : :
615 : : //........................................................................
616 : : } // namespace utl
617 : : //........................................................................
618 : :
619 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|