bump product version to 4.1.6.2
[LibreOffice.git] / dbaccess / source / core / recovery / settingsimport.cxx
blobbd9c8b2d09c817d336e8825c2d9a9e9a6166d316
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include "settingsimport.hxx"
22 #include <tools/diagnose_ex.h>
23 #include <sax/tools/converter.hxx>
24 #include <xmloff/xmltoken.hxx>
26 //........................................................................
27 namespace dbaccess
29 //........................................................................
31 using ::com::sun::star::uno::Reference;
32 using ::com::sun::star::uno::XInterface;
33 using ::com::sun::star::uno::UNO_QUERY;
34 using ::com::sun::star::uno::UNO_QUERY_THROW;
35 using ::com::sun::star::uno::UNO_SET_THROW;
36 using ::com::sun::star::uno::Exception;
37 using ::com::sun::star::uno::RuntimeException;
38 using ::com::sun::star::uno::Any;
39 using ::com::sun::star::uno::makeAny;
40 using ::com::sun::star::uno::Sequence;
41 using ::com::sun::star::uno::Type;
42 using ::com::sun::star::xml::sax::XAttributeList;
44 //====================================================================
45 //= SettingsImport
46 //====================================================================
47 //--------------------------------------------------------------------
48 SettingsImport::SettingsImport()
49 :m_refCount( 0 )
53 //--------------------------------------------------------------------
54 SettingsImport::~SettingsImport()
58 oslInterlockedCount SAL_CALL SettingsImport::acquire()
60 return osl_atomic_increment( &m_refCount );
63 oslInterlockedCount SAL_CALL SettingsImport::release()
65 oslInterlockedCount newCount = osl_atomic_decrement( &m_refCount );
66 if ( newCount == 0 )
67 delete this;
68 return newCount;
71 void SettingsImport::startElement( const Reference< XAttributeList >& i_rAttributes )
73 // find the name of the setting
74 if ( i_rAttributes.is() )
76 m_sItemName = i_rAttributes->getValueByName( "config:name" );
77 m_sItemType = i_rAttributes->getValueByName( "config:type" );
81 void SettingsImport::endElement()
85 void SettingsImport::characters( const OUString& i_rCharacters )
87 m_aCharacters.append( i_rCharacters );
90 void SettingsImport::split( const OUString& i_rElementName, OUString& o_rNamespace, OUString& o_rLocalName )
92 o_rNamespace = OUString();
93 o_rLocalName = i_rElementName;
94 const sal_Int32 nSeparatorPos = i_rElementName.indexOf( ':' );
95 if ( nSeparatorPos > -1 )
97 o_rNamespace = i_rElementName.copy( 0, nSeparatorPos );
98 o_rLocalName = i_rElementName.copy( nSeparatorPos + 1 );
101 OSL_ENSURE( o_rNamespace == "config", "SettingsImport::split: unexpected namespace!" );
102 // our recovery file is kind of hand-made, so there shouldn't be anything else than "config".
103 // If there is, then just ignore it ...
106 //====================================================================
107 //= IgnoringSettingsImport
108 //====================================================================
109 //--------------------------------------------------------------------
110 ::rtl::Reference< SettingsImport > IgnoringSettingsImport::nextState( const OUString& i_rElementName )
112 (void)i_rElementName;
113 return this;
116 //====================================================================
117 //= OfficeSettingsImport
118 //====================================================================
119 //--------------------------------------------------------------------
120 OfficeSettingsImport::OfficeSettingsImport( ::comphelper::NamedValueCollection& o_rSettings )
121 :m_rSettings( o_rSettings )
125 //--------------------------------------------------------------------
126 OfficeSettingsImport::~OfficeSettingsImport()
130 //--------------------------------------------------------------------
131 ::rtl::Reference< SettingsImport > OfficeSettingsImport::nextState( const OUString& i_rElementName )
133 // separate the namespace part from the element name
134 OUString sNamespace;
135 OUString sLocalName;
136 split( i_rElementName, sNamespace, sLocalName );
138 if ( sLocalName == "config-item-set" )
139 return new ConfigItemSetImport( m_rSettings );
141 #if OSL_DEBUG_LEVEL > 0
142 OString sMessage( "unknown (or unsupported at this place) element name '" );
143 sMessage += OUStringToOString( i_rElementName, RTL_TEXTENCODING_UTF8 );
144 sMessage += "', ignoring";
145 OSL_FAIL( sMessage.getStr() );
146 #endif
147 return new IgnoringSettingsImport;
150 //====================================================================
151 //= ConfigItemImport
152 //====================================================================
153 //--------------------------------------------------------------------
154 ConfigItemImport::ConfigItemImport( ::comphelper::NamedValueCollection& o_rSettings )
155 :m_rSettings( o_rSettings )
159 //--------------------------------------------------------------------
160 ConfigItemImport::~ConfigItemImport()
164 //--------------------------------------------------------------------
165 ::rtl::Reference< SettingsImport > ConfigItemImport::nextState( const OUString& i_rElementName )
167 OSL_FAIL( "ConfigItemImport::nextState: unexpected: this class is responsible for child-less items only!" );
168 (void)i_rElementName;
169 return new IgnoringSettingsImport;
172 //--------------------------------------------------------------------
173 void ConfigItemImport::endElement()
175 SettingsImport::endElement();
177 const OUString sItemName( getItemName() );
178 ENSURE_OR_RETURN_VOID( !sItemName.isEmpty(), "no item name -> no item value" );
179 Any aValue;
180 getItemValue( aValue );
181 m_rSettings.put( sItemName, aValue );
184 //--------------------------------------------------------------------
185 void ConfigItemImport::getItemValue( ::com::sun::star::uno::Any& o_rValue ) const
187 o_rValue.clear();
189 // the characters building up th evalue
190 OUStringBuffer aCharacters( getAccumulatedCharacters() );
191 const OUString sValue = aCharacters.makeStringAndClear();
193 const OUString& rItemType( getItemType() );
194 ENSURE_OR_RETURN_VOID( !rItemType.isEmpty(), "no item type -> no item value" );
196 if ( ::xmloff::token::IsXMLToken( rItemType, ::xmloff::token::XML_INT ) )
198 sal_Int32 nValue(0);
199 if (::sax::Converter::convertNumber( nValue, sValue ))
201 o_rValue <<= nValue;
203 else
205 OSL_FAIL( "ConfigItemImport::getItemValue: could not convert an int value!" );
208 else if ( ::xmloff::token::IsXMLToken( rItemType, ::xmloff::token::XML_BOOLEAN ) )
210 bool bValue(false);
211 if (::sax::Converter::convertBool( bValue, sValue ))
213 o_rValue <<= bValue;
215 else
217 OSL_FAIL( "ConfigItemImport::getItemValue: could not convert a boolean value!" );
220 else if ( ::xmloff::token::IsXMLToken( rItemType, ::xmloff::token::XML_STRING ) )
222 o_rValue <<= sValue;
224 #if OSL_DEBUG_LEVEL > 0
225 else
227 OString sMessage( "ConfigItemImport::getItemValue: unsupported item type '" );
228 sMessage += OUStringToOString( rItemType, RTL_TEXTENCODING_UTF8 );
229 sMessage += "', ignoring";
230 OSL_FAIL( sMessage.getStr() );
232 #endif
235 //====================================================================
236 //= ConfigItemSetImport
237 //====================================================================
238 //--------------------------------------------------------------------
239 ConfigItemSetImport::ConfigItemSetImport( ::comphelper::NamedValueCollection& o_rSettings )
240 :ConfigItemImport( o_rSettings )
244 //--------------------------------------------------------------------
245 ConfigItemSetImport::~ConfigItemSetImport()
249 //--------------------------------------------------------------------
250 ::rtl::Reference< SettingsImport > ConfigItemSetImport::nextState( const OUString& i_rElementName )
252 // separate the namespace part from the element name
253 OUString sNamespace;
254 OUString sLocalName;
255 split( i_rElementName, sNamespace, sLocalName );
257 if ( sLocalName == "config-item-set" )
258 return new ConfigItemSetImport( m_aChildSettings );
259 if ( sLocalName == "config-item" )
260 return new ConfigItemImport( m_aChildSettings );
262 #if OSL_DEBUG_LEVEL > 0
263 OString sMessage( "unknown element name '" );
264 sMessage += OUStringToOString( i_rElementName, RTL_TEXTENCODING_UTF8 );
265 sMessage += "', ignoring";
266 OSL_FAIL( sMessage.getStr() );
267 #endif
268 return new IgnoringSettingsImport;
271 //--------------------------------------------------------------------
272 void ConfigItemSetImport::getItemValue( Any& o_rValue ) const
274 o_rValue <<= m_aChildSettings.getPropertyValues();
277 //........................................................................
278 } // namespace dbaccess
279 //........................................................................
281 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */