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