1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: importmergehandler.cxx,v $
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 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_configmgr.hxx"
34 #include "importmergehandler.hxx"
35 #include <com/sun/star/lang/XInitialization.hpp>
36 #include <com/sun/star/beans/NamedValue.hpp>
37 #include <rtl/ustrbuf.hxx>
40 // -----------------------------------------------------------------------------
44 // -----------------------------------------------------------------------------
47 // -----------------------------------------------------------------------------
48 namespace beans
= ::com::sun::star::beans
;
50 // -----------------------------------------------------------------------------
52 ImportMergeHandler::ImportMergeHandler(
53 uno::Reference
< backenduno::XBackend
> const & xTargetBackend
, Mode mode
, rtl::OUString
const & aEntity
, sal_Bool
const & bNotify
)
54 : BasicImportHandler(xTargetBackend
,aEntity
, bNotify
)
59 // -----------------------------------------------------------------------------
61 void ImportMergeHandler::failNotStarted()
63 OSL_ENSURE(!hasComponent(), "Import handler after failure to create output handler or after closing");
64 raiseMalformedDataException("configmgr::backend::ImportHandler: Trying to generate output before identifying the target component");
66 // -----------------------------------------------------------------------------
68 inline bool ImportMergeHandler::isStarted() const
70 return !! m_xOutputHandler
.is();
72 // -----------------------------------------------------------------------------
74 inline void ImportMergeHandler::checkStarted()
76 if (!isStarted()) failNotStarted();
78 // -----------------------------------------------------------------------------
80 inline uno::Reference
< backenduno::XUpdateHandler
> ImportMergeHandler::getOutputHandler()
83 return m_xOutputHandler
;
85 // -----------------------------------------------------------------------------
88 bool setHandlerProperty(uno::Reference
< uno::XInterface
> const & xHandler
, char const * property
, sal_Bool value
)
91 uno::Reference
< lang::XInitialization
> xInitHandler( xHandler
, uno::UNO_QUERY
);
95 uno::Sequence
< uno::Any
> aArgs(1);
96 aArgs
[0] <<= beans::NamedValue( rtl::OUString::createFromAscii(property
), uno::makeAny(value
) );
97 xInitHandler
->initialize(aArgs
);
100 catch (uno::Exception
& e
)
102 OSL_TRACE("Configuration Import Handler - Could not set output handler property '%s': %s\n",
103 property
,rtl::OUStringToOString(e
.Message
,RTL_TEXTENCODING_ASCII_US
).getStr());
105 OSL_ENSURE(false, "Output Handler does not support expected property" );
109 OSL_TRACE("Configuration Import Handler - Could not set output handler property '%s': %s\n",
110 property
,"Object does not support expected interface");
112 OSL_ENSURE(false, "Output Handler does not support expected interface" );
116 // -----------------------------------------------------------------------------
117 uno::Reference
< backenduno::XUpdateHandler
> ImportMergeHandler::createOutputHandler()
119 OSL_PRECOND( hasComponent(), "Trying to create output-handler for Import Merger without setting a component first") ;
120 rtl::OUString
const aComponentName
= this->getComponent();
122 uno::Reference
< backenduno::XUpdateHandler
> xOutputHandler
;
125 xOutputHandler
= hasEntity() ? getBackend()->getUpdateHandler(aComponentName
,getEntity())
126 : getBackend()->getOwnUpdateHandler(aComponentName
);
128 catch (lang::NoSupportException
& e
)
130 rtl::OUStringBuffer sMessage
;
131 sMessage
.appendAscii("configmgr::backend::ImportHandler: ");
132 sMessage
.appendAscii("Could not get output handler for component ").append(aComponentName
);
133 sMessage
.appendAscii(": Backend does not support updates - ").append( e
.Message
);
135 throw lang::WrappedTargetException(sMessage
.makeStringAndClear(), *this, uno::makeAny(e
));
137 catch (lang::IllegalArgumentException
& e
)
139 rtl::OUStringBuffer sMessage
;
140 sMessage
.appendAscii("configmgr::backend::ImportHandler: ");
141 sMessage
.appendAscii("Could not get output handler for component ").append(aComponentName
);
142 sMessage
.appendAscii(" due to a backend exception: ").append( e
.Message
);
144 throw lang::WrappedTargetException(sMessage
.makeStringAndClear(), *this, uno::makeAny(e
));
147 if (!xOutputHandler
.is())
149 rtl::OUStringBuffer sMessage
;
150 sMessage
.appendAscii("configmgr::backend::ImportHandler: ");
151 sMessage
.appendAscii("Cannot import. ERROR - The backend returns a NULL handler for component ")
152 .append(aComponentName
).append( sal_Unicode('.') );
154 throw uno::RuntimeException(sMessage
.makeStringAndClear(), *this);
160 case copy
: setHandlerProperty(xOutputHandler
,"Truncate", sal_True
); break;
161 case no_overwrite
: setHandlerProperty(xOutputHandler
,"Overwrite",sal_False
); break;
163 default: OSL_ASSERT(false); break;
166 return xOutputHandler
;
168 // -----------------------------------------------------------------------------
172 void SAL_CALL
ImportMergeHandler::startLayer( )
173 throw (backenduno::MalformedDataException
, lang::WrappedTargetException
, uno::RuntimeException
)
175 m_xOutputHandler
.clear();
177 BasicImportHandler::startLayer();
179 // -----------------------------------------------------------------------------
181 void SAL_CALL
ImportMergeHandler::endLayer( )
182 throw (backenduno::MalformedDataException
, lang::WrappedTargetException
, uno::RuntimeException
)
187 getOutputHandler()->endUpdate();
189 catch (lang::IllegalAccessException
& iae
)
191 rtl::OUString
const sMsg(RTL_CONSTASCII_USTRINGPARAM("ImportHandler - no write access to layer: "));
192 throw lang::WrappedTargetException(sMsg
.concat(iae
.Message
),*this,uno::makeAny(iae
));
195 BasicImportHandler::endLayer();
196 m_xOutputHandler
.clear();
198 // -----------------------------------------------------------------------------
200 void SAL_CALL
ImportMergeHandler::overrideNode( const rtl::OUString
& aName
, sal_Int16 aAttributes
, sal_Bool bClear
)
201 throw (backenduno::MalformedDataException
, lang::WrappedTargetException
, uno::RuntimeException
)
203 if (!isStarted() && startComponent(aName
))
206 (m_xOutputHandler
= createOutputHandler())->startUpdate( );
208 catch (lang::IllegalAccessException
& iae
)
210 rtl::OUString
const sMsg(RTL_CONSTASCII_USTRINGPARAM("ImportHandler - no write access to layer: "));
211 throw lang::WrappedTargetException(sMsg
.concat(iae
.Message
),*this,uno::makeAny(iae
));
214 OSL_ENSURE(!bClear
,"'clear' operation not supported properly on import");
216 bool bReset
= (m_mode
!= merge
) || bClear
; // is not relevant for no_overwrite,but might be cheaper there
217 getOutputHandler()->modifyNode(aName
,aAttributes
,aAttributes
,bReset
);
219 // -----------------------------------------------------------------------------
221 void SAL_CALL
ImportMergeHandler::addOrReplaceNode( const rtl::OUString
& aName
, sal_Int16 aAttributes
)
222 throw (backenduno::MalformedDataException
, lang::WrappedTargetException
, uno::RuntimeException
)
224 getOutputHandler()->addOrReplaceNode(aName
,aAttributes
);
226 // -----------------------------------------------------------------------------
228 void SAL_CALL
ImportMergeHandler::addOrReplaceNodeFromTemplate( const rtl::OUString
& aName
, const backenduno::TemplateIdentifier
& aTemplate
, sal_Int16 aAttributes
)
229 throw (backenduno::MalformedDataException
, lang::WrappedTargetException
, uno::RuntimeException
)
231 getOutputHandler()->addOrReplaceNodeFromTemplate(aName
,aAttributes
,aTemplate
);
233 // -----------------------------------------------------------------------------
235 void SAL_CALL
ImportMergeHandler::endNode( )
236 throw (backenduno::MalformedDataException
, lang::WrappedTargetException
, uno::RuntimeException
)
238 getOutputHandler()->endNode();
240 // -----------------------------------------------------------------------------
242 void SAL_CALL
ImportMergeHandler::dropNode( const rtl::OUString
& aName
)
243 throw (backenduno::MalformedDataException
, lang::WrappedTargetException
, uno::RuntimeException
)
245 getOutputHandler()->removeNode(aName
);
247 // -----------------------------------------------------------------------------
249 void SAL_CALL
ImportMergeHandler::overrideProperty( const rtl::OUString
& aName
, sal_Int16 aAttributes
, const uno::Type
& aType
, sal_Bool bClear
)
250 throw (backenduno::MalformedDataException
, lang::WrappedTargetException
, uno::RuntimeException
)
252 (void) bClear
; // avoid warning about unused parameter
253 OSL_ENSURE(!bClear
,"'clear' operation not supported on import");
254 getOutputHandler()->modifyProperty(aName
,aAttributes
,aAttributes
,aType
);
256 // -----------------------------------------------------------------------------
258 void SAL_CALL
ImportMergeHandler::endProperty( )
259 throw (backenduno::MalformedDataException
, lang::WrappedTargetException
, uno::RuntimeException
)
261 getOutputHandler()->endProperty();
263 // -----------------------------------------------------------------------------
265 void SAL_CALL
ImportMergeHandler::setPropertyValue( const uno::Any
& aValue
)
266 throw (backenduno::MalformedDataException
, lang::WrappedTargetException
, uno::RuntimeException
)
268 getOutputHandler()->setPropertyValue(aValue
);
270 // -----------------------------------------------------------------------------
272 void SAL_CALL
ImportMergeHandler::setPropertyValueForLocale( const uno::Any
& aValue
, const rtl::OUString
& aLocale
)
273 throw (backenduno::MalformedDataException
, lang::WrappedTargetException
, uno::RuntimeException
)
275 getOutputHandler()->setPropertyValueForLocale(aValue
,aLocale
);
277 // -----------------------------------------------------------------------------
279 void SAL_CALL
ImportMergeHandler::addProperty( const rtl::OUString
& aName
, sal_Int16 aAttributes
, const uno::Type
& aType
)
280 throw (backenduno::MalformedDataException
, lang::WrappedTargetException
, uno::RuntimeException
)
282 getOutputHandler()->addOrReplaceProperty(aName
, aAttributes
, aType
);
284 // -----------------------------------------------------------------------------
286 void SAL_CALL
ImportMergeHandler::addPropertyWithValue( const rtl::OUString
& aName
, sal_Int16 aAttributes
, const uno::Any
& aValue
)
287 throw (backenduno::MalformedDataException
, lang::WrappedTargetException
, uno::RuntimeException
)
289 getOutputHandler()->addOrReplacePropertyWithValue(aName
, aAttributes
, aValue
);
291 // -----------------------------------------------------------------------------
293 // -----------------------------------------------------------------------------
294 // -----------------------------------------------------------------------------
297 // -----------------------------------------------------------------------------