xref: /trunk/main/dbaccess/source/core/recovery/settingsimport.cxx (revision cf6516809c57e1bb0a940545cca99cdad54d4ce2)
1*96de5490SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*96de5490SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*96de5490SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*96de5490SAndrew Rist  * distributed with this work for additional information
6*96de5490SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*96de5490SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*96de5490SAndrew Rist  * "License"); you may not use this file except in compliance
9*96de5490SAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11*96de5490SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13*96de5490SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*96de5490SAndrew Rist  * software distributed under the License is distributed on an
15*96de5490SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*96de5490SAndrew Rist  * KIND, either express or implied.  See the License for the
17*96de5490SAndrew Rist  * specific language governing permissions and limitations
18*96de5490SAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20*96de5490SAndrew Rist  *************************************************************/
21*96de5490SAndrew Rist 
22*96de5490SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #include "precompiled_dbaccess.hxx"
25cdf0e10cSrcweir 
26cdf0e10cSrcweir #include "settingsimport.hxx"
27cdf0e10cSrcweir 
28cdf0e10cSrcweir /** === begin UNO includes === **/
29cdf0e10cSrcweir /** === end UNO includes === **/
30cdf0e10cSrcweir 
31cdf0e10cSrcweir #include <tools/diagnose_ex.h>
32cdf0e10cSrcweir #include <xmloff/xmltoken.hxx>
33cdf0e10cSrcweir #include <xmloff/xmluconv.hxx>
34cdf0e10cSrcweir 
35cdf0e10cSrcweir //........................................................................
36cdf0e10cSrcweir namespace dbaccess
37cdf0e10cSrcweir {
38cdf0e10cSrcweir //........................................................................
39cdf0e10cSrcweir 
40cdf0e10cSrcweir     /** === begin UNO using === **/
41cdf0e10cSrcweir     using ::com::sun::star::uno::Reference;
42cdf0e10cSrcweir     using ::com::sun::star::uno::XInterface;
43cdf0e10cSrcweir     using ::com::sun::star::uno::UNO_QUERY;
44cdf0e10cSrcweir     using ::com::sun::star::uno::UNO_QUERY_THROW;
45cdf0e10cSrcweir     using ::com::sun::star::uno::UNO_SET_THROW;
46cdf0e10cSrcweir     using ::com::sun::star::uno::Exception;
47cdf0e10cSrcweir     using ::com::sun::star::uno::RuntimeException;
48cdf0e10cSrcweir     using ::com::sun::star::uno::Any;
49cdf0e10cSrcweir     using ::com::sun::star::uno::makeAny;
50cdf0e10cSrcweir     using ::com::sun::star::uno::Sequence;
51cdf0e10cSrcweir     using ::com::sun::star::uno::Type;
52cdf0e10cSrcweir     using ::com::sun::star::xml::sax::XAttributeList;
53cdf0e10cSrcweir     /** === end UNO using === **/
54cdf0e10cSrcweir 
55cdf0e10cSrcweir     //====================================================================
56cdf0e10cSrcweir     //= SettingsImport
57cdf0e10cSrcweir     //====================================================================
58cdf0e10cSrcweir     //--------------------------------------------------------------------
SettingsImport()59cdf0e10cSrcweir     SettingsImport::SettingsImport()
60cdf0e10cSrcweir         :m_refCount( 0 )
61cdf0e10cSrcweir     {
62cdf0e10cSrcweir     }
63cdf0e10cSrcweir 
64cdf0e10cSrcweir     //--------------------------------------------------------------------
~SettingsImport()65cdf0e10cSrcweir     SettingsImport::~SettingsImport()
66cdf0e10cSrcweir     {
67cdf0e10cSrcweir     }
68cdf0e10cSrcweir 
69cdf0e10cSrcweir     //--------------------------------------------------------------------
acquire()70cdf0e10cSrcweir     oslInterlockedCount SAL_CALL SettingsImport::acquire()
71cdf0e10cSrcweir     {
72cdf0e10cSrcweir         return osl_incrementInterlockedCount( &m_refCount );
73cdf0e10cSrcweir     }
74cdf0e10cSrcweir 
75cdf0e10cSrcweir     //--------------------------------------------------------------------
release()76cdf0e10cSrcweir     oslInterlockedCount SAL_CALL SettingsImport::release()
77cdf0e10cSrcweir     {
78cdf0e10cSrcweir         oslInterlockedCount newCount = osl_decrementInterlockedCount( &m_refCount );
79cdf0e10cSrcweir         if ( newCount == 0 )
80cdf0e10cSrcweir             delete this;
81cdf0e10cSrcweir         return newCount;
82cdf0e10cSrcweir     }
83cdf0e10cSrcweir 
84cdf0e10cSrcweir     //--------------------------------------------------------------------
startElement(const Reference<XAttributeList> & i_rAttributes)85cdf0e10cSrcweir     void SettingsImport::startElement( const Reference< XAttributeList >& i_rAttributes )
86cdf0e10cSrcweir     {
87cdf0e10cSrcweir         // find the name of the setting
88cdf0e10cSrcweir         if ( i_rAttributes.is() )
89cdf0e10cSrcweir         {
90cdf0e10cSrcweir             m_sItemName = i_rAttributes->getValueByName( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "config:name" ) ) );
91cdf0e10cSrcweir             m_sItemType = i_rAttributes->getValueByName( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "config:type" ) ) );
92cdf0e10cSrcweir         }
93cdf0e10cSrcweir     }
94cdf0e10cSrcweir 
95cdf0e10cSrcweir     //--------------------------------------------------------------------
endElement()96cdf0e10cSrcweir     void SettingsImport::endElement()
97cdf0e10cSrcweir     {
98cdf0e10cSrcweir     }
99cdf0e10cSrcweir 
100cdf0e10cSrcweir     //--------------------------------------------------------------------
characters(const::rtl::OUString & i_rCharacters)101cdf0e10cSrcweir     void SettingsImport::characters( const ::rtl::OUString& i_rCharacters )
102cdf0e10cSrcweir     {
103cdf0e10cSrcweir         m_aCharacters.append( i_rCharacters );
104cdf0e10cSrcweir     }
105cdf0e10cSrcweir 
106cdf0e10cSrcweir     //--------------------------------------------------------------------
split(const::rtl::OUString & i_rElementName,::rtl::OUString & o_rNamespace,::rtl::OUString & o_rLocalName)107cdf0e10cSrcweir     void SettingsImport::split( const ::rtl::OUString& i_rElementName, ::rtl::OUString& o_rNamespace, ::rtl::OUString& o_rLocalName )
108cdf0e10cSrcweir     {
109cdf0e10cSrcweir         o_rNamespace = ::rtl::OUString();
110cdf0e10cSrcweir         o_rLocalName = i_rElementName;
111cdf0e10cSrcweir         const sal_Int32 nSeparatorPos = i_rElementName.indexOf( ':' );
112cdf0e10cSrcweir         if ( nSeparatorPos > -1 )
113cdf0e10cSrcweir         {
114cdf0e10cSrcweir             o_rNamespace = i_rElementName.copy( 0, nSeparatorPos );
115cdf0e10cSrcweir             o_rLocalName = i_rElementName.copy( nSeparatorPos + 1 );
116cdf0e10cSrcweir         }
117cdf0e10cSrcweir 
118cdf0e10cSrcweir         OSL_ENSURE( o_rNamespace.equalsAscii( "config" ), "SettingsImport::split: unexpected namespace!" );
119cdf0e10cSrcweir             // our recovery file is kind of hand-made, so there shouldn't be anything else than "config".
120cdf0e10cSrcweir             // If there is, then just ignore it ...
121cdf0e10cSrcweir     }
122cdf0e10cSrcweir 
123cdf0e10cSrcweir     //====================================================================
124cdf0e10cSrcweir     //= IgnoringSettingsImport
125cdf0e10cSrcweir     //====================================================================
126cdf0e10cSrcweir     //--------------------------------------------------------------------
nextState(const::rtl::OUString & i_rElementName)127cdf0e10cSrcweir     ::rtl::Reference< SettingsImport > IgnoringSettingsImport::nextState( const ::rtl::OUString& i_rElementName )
128cdf0e10cSrcweir     {
129cdf0e10cSrcweir         (void)i_rElementName;
130cdf0e10cSrcweir         return this;
131cdf0e10cSrcweir     }
132cdf0e10cSrcweir 
133cdf0e10cSrcweir     //====================================================================
134cdf0e10cSrcweir     //= OfficeSettingsImport
135cdf0e10cSrcweir     //====================================================================
136cdf0e10cSrcweir     //--------------------------------------------------------------------
OfficeSettingsImport(::comphelper::NamedValueCollection & o_rSettings)137cdf0e10cSrcweir     OfficeSettingsImport::OfficeSettingsImport( ::comphelper::NamedValueCollection& o_rSettings )
138cdf0e10cSrcweir         :m_rSettings( o_rSettings )
139cdf0e10cSrcweir     {
140cdf0e10cSrcweir     }
141cdf0e10cSrcweir 
142cdf0e10cSrcweir     //--------------------------------------------------------------------
~OfficeSettingsImport()143cdf0e10cSrcweir     OfficeSettingsImport::~OfficeSettingsImport()
144cdf0e10cSrcweir     {
145cdf0e10cSrcweir     }
146cdf0e10cSrcweir 
147cdf0e10cSrcweir     //--------------------------------------------------------------------
nextState(const::rtl::OUString & i_rElementName)148cdf0e10cSrcweir     ::rtl::Reference< SettingsImport > OfficeSettingsImport::nextState( const ::rtl::OUString& i_rElementName )
149cdf0e10cSrcweir     {
150cdf0e10cSrcweir         // separate the namespace part from the element name
151cdf0e10cSrcweir         ::rtl::OUString sNamespace;
152cdf0e10cSrcweir         ::rtl::OUString sLocalName;
153cdf0e10cSrcweir         split( i_rElementName, sNamespace, sLocalName );
154cdf0e10cSrcweir 
155cdf0e10cSrcweir         if ( sLocalName.equalsAscii( "config-item-set" ) )
156cdf0e10cSrcweir             return new ConfigItemSetImport( m_rSettings );
157cdf0e10cSrcweir 
158cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 0
159cdf0e10cSrcweir         ::rtl::OString sMessage( "unknown (or unsupported at this place) element name '" );
160cdf0e10cSrcweir         sMessage += ::rtl::OUStringToOString( i_rElementName, RTL_TEXTENCODING_UTF8 );
161cdf0e10cSrcweir         sMessage += "', ignoring";
162cdf0e10cSrcweir         OSL_ENSURE( false, sMessage.getStr() );
163cdf0e10cSrcweir #endif
164cdf0e10cSrcweir         return new IgnoringSettingsImport;
165cdf0e10cSrcweir     }
166cdf0e10cSrcweir 
167cdf0e10cSrcweir     //====================================================================
168cdf0e10cSrcweir     //= ConfigItemImport
169cdf0e10cSrcweir     //====================================================================
170cdf0e10cSrcweir     //--------------------------------------------------------------------
ConfigItemImport(::comphelper::NamedValueCollection & o_rSettings)171cdf0e10cSrcweir     ConfigItemImport::ConfigItemImport( ::comphelper::NamedValueCollection& o_rSettings )
172cdf0e10cSrcweir         :m_rSettings( o_rSettings )
173cdf0e10cSrcweir     {
174cdf0e10cSrcweir     }
175cdf0e10cSrcweir 
176cdf0e10cSrcweir     //--------------------------------------------------------------------
~ConfigItemImport()177cdf0e10cSrcweir     ConfigItemImport::~ConfigItemImport()
178cdf0e10cSrcweir     {
179cdf0e10cSrcweir     }
180cdf0e10cSrcweir 
181cdf0e10cSrcweir     //--------------------------------------------------------------------
nextState(const::rtl::OUString & i_rElementName)182cdf0e10cSrcweir     ::rtl::Reference< SettingsImport > ConfigItemImport::nextState( const ::rtl::OUString& i_rElementName )
183cdf0e10cSrcweir     {
184cdf0e10cSrcweir         OSL_ENSURE( false, "ConfigItemImport::nextState: unexpected: this class is responsible for child-less items only!" );
185cdf0e10cSrcweir         (void)i_rElementName;
186cdf0e10cSrcweir         return new IgnoringSettingsImport;
187cdf0e10cSrcweir     }
188cdf0e10cSrcweir 
189cdf0e10cSrcweir     //--------------------------------------------------------------------
endElement()190cdf0e10cSrcweir     void ConfigItemImport::endElement()
191cdf0e10cSrcweir     {
192cdf0e10cSrcweir         SettingsImport::endElement();
193cdf0e10cSrcweir 
194cdf0e10cSrcweir         const ::rtl::OUString sItemName( getItemName() );
195cdf0e10cSrcweir         ENSURE_OR_RETURN_VOID( sItemName.getLength(), "no item name -> no item value" );
196cdf0e10cSrcweir         Any aValue;
197cdf0e10cSrcweir         getItemValue( aValue );
198cdf0e10cSrcweir         m_rSettings.put( sItemName, aValue );
199cdf0e10cSrcweir     }
200cdf0e10cSrcweir 
201cdf0e10cSrcweir     //--------------------------------------------------------------------
getItemValue(::com::sun::star::uno::Any & o_rValue) const202cdf0e10cSrcweir     void ConfigItemImport::getItemValue( ::com::sun::star::uno::Any& o_rValue ) const
203cdf0e10cSrcweir     {
204cdf0e10cSrcweir         o_rValue.clear();
205cdf0e10cSrcweir 
206cdf0e10cSrcweir         // the characters building up th evalue
207cdf0e10cSrcweir         ::rtl::OUStringBuffer aCharacters( getAccumulatedCharacters() );
208cdf0e10cSrcweir         const ::rtl::OUString sValue = aCharacters.makeStringAndClear();
209cdf0e10cSrcweir 
210cdf0e10cSrcweir         const ::rtl::OUString& rItemType( getItemType() );
211cdf0e10cSrcweir         ENSURE_OR_RETURN_VOID( rItemType.getLength(), "no item type -> no item value" );
212cdf0e10cSrcweir 
213cdf0e10cSrcweir         if ( ::xmloff::token::IsXMLToken( rItemType, ::xmloff::token::XML_INT ) )
214cdf0e10cSrcweir         {
215cdf0e10cSrcweir             sal_Int32 nValue(0);
216cdf0e10cSrcweir             if ( SvXMLUnitConverter::convertNumber( nValue, sValue ) )
217cdf0e10cSrcweir                 o_rValue <<= nValue;
218cdf0e10cSrcweir             else
219cdf0e10cSrcweir             {
220cdf0e10cSrcweir                 OSL_ENSURE( false, "ConfigItemImport::getItemValue: could not convert an int value!" );
221cdf0e10cSrcweir             }
222cdf0e10cSrcweir         }
223cdf0e10cSrcweir         else if ( ::xmloff::token::IsXMLToken( rItemType, ::xmloff::token::XML_BOOLEAN ) )
224cdf0e10cSrcweir         {
225cdf0e10cSrcweir             sal_Bool nValue( sal_False );
226cdf0e10cSrcweir             if ( SvXMLUnitConverter::convertBool( nValue, sValue ) )
227cdf0e10cSrcweir                 o_rValue <<= nValue;
228cdf0e10cSrcweir             else
229cdf0e10cSrcweir             {
230cdf0e10cSrcweir                 OSL_ENSURE( false, "ConfigItemImport::getItemValue: could not convert a boolean value!" );
231cdf0e10cSrcweir             }
232cdf0e10cSrcweir         }
233cdf0e10cSrcweir         else if ( ::xmloff::token::IsXMLToken( rItemType, ::xmloff::token::XML_STRING ) )
234cdf0e10cSrcweir         {
235cdf0e10cSrcweir             o_rValue <<= sValue;
236cdf0e10cSrcweir         }
237cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 0
238cdf0e10cSrcweir         else
239cdf0e10cSrcweir         {
240cdf0e10cSrcweir             ::rtl::OString sMessage( "ConfigItemImport::getItemValue: unsupported item type '" );
241cdf0e10cSrcweir             sMessage += ::rtl::OUStringToOString( rItemType, RTL_TEXTENCODING_UTF8 );
242cdf0e10cSrcweir             sMessage += "', ignoring";
243cdf0e10cSrcweir             OSL_ENSURE( false, sMessage.getStr() );
244cdf0e10cSrcweir         }
245cdf0e10cSrcweir #endif
246cdf0e10cSrcweir     }
247cdf0e10cSrcweir 
248cdf0e10cSrcweir     //====================================================================
249cdf0e10cSrcweir     //= ConfigItemSetImport
250cdf0e10cSrcweir     //====================================================================
251cdf0e10cSrcweir     //--------------------------------------------------------------------
ConfigItemSetImport(::comphelper::NamedValueCollection & o_rSettings)252cdf0e10cSrcweir     ConfigItemSetImport::ConfigItemSetImport( ::comphelper::NamedValueCollection& o_rSettings )
253cdf0e10cSrcweir         :ConfigItemImport( o_rSettings )
254cdf0e10cSrcweir     {
255cdf0e10cSrcweir     }
256cdf0e10cSrcweir 
257cdf0e10cSrcweir     //--------------------------------------------------------------------
~ConfigItemSetImport()258cdf0e10cSrcweir     ConfigItemSetImport::~ConfigItemSetImport()
259cdf0e10cSrcweir     {
260cdf0e10cSrcweir     }
261cdf0e10cSrcweir 
262cdf0e10cSrcweir     //--------------------------------------------------------------------
nextState(const::rtl::OUString & i_rElementName)263cdf0e10cSrcweir     ::rtl::Reference< SettingsImport > ConfigItemSetImport::nextState( const ::rtl::OUString& i_rElementName )
264cdf0e10cSrcweir     {
265cdf0e10cSrcweir         // separate the namespace part from the element name
266cdf0e10cSrcweir         ::rtl::OUString sNamespace;
267cdf0e10cSrcweir         ::rtl::OUString sLocalName;
268cdf0e10cSrcweir         split( i_rElementName, sNamespace, sLocalName );
269cdf0e10cSrcweir 
270cdf0e10cSrcweir         if ( sLocalName.equalsAscii( "config-item-set" ) )
271cdf0e10cSrcweir             return new ConfigItemSetImport( m_aChildSettings );
272cdf0e10cSrcweir         if ( sLocalName.equalsAscii( "config-item" ) )
273cdf0e10cSrcweir             return new ConfigItemImport( m_aChildSettings );
274cdf0e10cSrcweir 
275cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 0
276cdf0e10cSrcweir         ::rtl::OString sMessage( "unknown element name '" );
277cdf0e10cSrcweir         sMessage += ::rtl::OUStringToOString( i_rElementName, RTL_TEXTENCODING_UTF8 );
278cdf0e10cSrcweir         sMessage += "', ignoring";
279cdf0e10cSrcweir         OSL_ENSURE( false, sMessage.getStr() );
280cdf0e10cSrcweir #endif
281cdf0e10cSrcweir         return new IgnoringSettingsImport;
282cdf0e10cSrcweir     }
283cdf0e10cSrcweir 
284cdf0e10cSrcweir     //--------------------------------------------------------------------
getItemValue(Any & o_rValue) const285cdf0e10cSrcweir     void ConfigItemSetImport::getItemValue( Any& o_rValue ) const
286cdf0e10cSrcweir     {
287cdf0e10cSrcweir         o_rValue <<= m_aChildSettings.getPropertyValues();
288cdf0e10cSrcweir     }
289cdf0e10cSrcweir 
290cdf0e10cSrcweir //........................................................................
291cdf0e10cSrcweir } // namespace dbaccess
292cdf0e10cSrcweir //........................................................................
293