Version 4.0.0.1, tag libreoffice-4.0.0.1
[LibreOffice.git] / dbaccess / source / core / recovery / settingsimport.cxx
blob614c268d4b86e264eac5374a461bcec8ec0b19f6
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 /** === begin UNO using === **/
32 using ::com::sun::star::uno::Reference;
33 using ::com::sun::star::uno::XInterface;
34 using ::com::sun::star::uno::UNO_QUERY;
35 using ::com::sun::star::uno::UNO_QUERY_THROW;
36 using ::com::sun::star::uno::UNO_SET_THROW;
37 using ::com::sun::star::uno::Exception;
38 using ::com::sun::star::uno::RuntimeException;
39 using ::com::sun::star::uno::Any;
40 using ::com::sun::star::uno::makeAny;
41 using ::com::sun::star::uno::Sequence;
42 using ::com::sun::star::uno::Type;
43 using ::com::sun::star::xml::sax::XAttributeList;
44 /** === end UNO using === **/
46 //====================================================================
47 //= SettingsImport
48 //====================================================================
49 //--------------------------------------------------------------------
50 SettingsImport::SettingsImport()
51 :m_refCount( 0 )
55 //--------------------------------------------------------------------
56 SettingsImport::~SettingsImport()
60 //--------------------------------------------------------------------
61 oslInterlockedCount SAL_CALL SettingsImport::acquire()
63 return osl_atomic_increment( &m_refCount );
66 //--------------------------------------------------------------------
67 oslInterlockedCount SAL_CALL SettingsImport::release()
69 oslInterlockedCount newCount = osl_atomic_decrement( &m_refCount );
70 if ( newCount == 0 )
71 delete this;
72 return newCount;
75 //--------------------------------------------------------------------
76 void SettingsImport::startElement( const Reference< XAttributeList >& i_rAttributes )
78 // find the name of the setting
79 if ( i_rAttributes.is() )
81 m_sItemName = i_rAttributes->getValueByName( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "config:name" ) ) );
82 m_sItemType = i_rAttributes->getValueByName( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "config:type" ) ) );
86 //--------------------------------------------------------------------
87 void SettingsImport::endElement()
91 //--------------------------------------------------------------------
92 void SettingsImport::characters( const ::rtl::OUString& i_rCharacters )
94 m_aCharacters.append( i_rCharacters );
97 //--------------------------------------------------------------------
98 void SettingsImport::split( const ::rtl::OUString& i_rElementName, ::rtl::OUString& o_rNamespace, ::rtl::OUString& o_rLocalName )
100 o_rNamespace = ::rtl::OUString();
101 o_rLocalName = i_rElementName;
102 const sal_Int32 nSeparatorPos = i_rElementName.indexOf( ':' );
103 if ( nSeparatorPos > -1 )
105 o_rNamespace = i_rElementName.copy( 0, nSeparatorPos );
106 o_rLocalName = i_rElementName.copy( nSeparatorPos + 1 );
109 OSL_ENSURE( o_rNamespace == "config", "SettingsImport::split: unexpected namespace!" );
110 // our recovery file is kind of hand-made, so there shouldn't be anything else than "config".
111 // If there is, then just ignore it ...
114 //====================================================================
115 //= IgnoringSettingsImport
116 //====================================================================
117 //--------------------------------------------------------------------
118 ::rtl::Reference< SettingsImport > IgnoringSettingsImport::nextState( const ::rtl::OUString& i_rElementName )
120 (void)i_rElementName;
121 return this;
124 //====================================================================
125 //= OfficeSettingsImport
126 //====================================================================
127 //--------------------------------------------------------------------
128 OfficeSettingsImport::OfficeSettingsImport( ::comphelper::NamedValueCollection& o_rSettings )
129 :m_rSettings( o_rSettings )
133 //--------------------------------------------------------------------
134 OfficeSettingsImport::~OfficeSettingsImport()
138 //--------------------------------------------------------------------
139 ::rtl::Reference< SettingsImport > OfficeSettingsImport::nextState( const ::rtl::OUString& i_rElementName )
141 // separate the namespace part from the element name
142 ::rtl::OUString sNamespace;
143 ::rtl::OUString sLocalName;
144 split( i_rElementName, sNamespace, sLocalName );
146 if ( sLocalName == "config-item-set" )
147 return new ConfigItemSetImport( m_rSettings );
149 #if OSL_DEBUG_LEVEL > 0
150 ::rtl::OString sMessage( "unknown (or unsupported at this place) element name '" );
151 sMessage += ::rtl::OUStringToOString( i_rElementName, RTL_TEXTENCODING_UTF8 );
152 sMessage += "', ignoring";
153 OSL_FAIL( sMessage.getStr() );
154 #endif
155 return new IgnoringSettingsImport;
158 //====================================================================
159 //= ConfigItemImport
160 //====================================================================
161 //--------------------------------------------------------------------
162 ConfigItemImport::ConfigItemImport( ::comphelper::NamedValueCollection& o_rSettings )
163 :m_rSettings( o_rSettings )
167 //--------------------------------------------------------------------
168 ConfigItemImport::~ConfigItemImport()
172 //--------------------------------------------------------------------
173 ::rtl::Reference< SettingsImport > ConfigItemImport::nextState( const ::rtl::OUString& i_rElementName )
175 OSL_FAIL( "ConfigItemImport::nextState: unexpected: this class is responsible for child-less items only!" );
176 (void)i_rElementName;
177 return new IgnoringSettingsImport;
180 //--------------------------------------------------------------------
181 void ConfigItemImport::endElement()
183 SettingsImport::endElement();
185 const ::rtl::OUString sItemName( getItemName() );
186 ENSURE_OR_RETURN_VOID( !sItemName.isEmpty(), "no item name -> no item value" );
187 Any aValue;
188 getItemValue( aValue );
189 m_rSettings.put( sItemName, aValue );
192 //--------------------------------------------------------------------
193 void ConfigItemImport::getItemValue( ::com::sun::star::uno::Any& o_rValue ) const
195 o_rValue.clear();
197 // the characters building up th evalue
198 ::rtl::OUStringBuffer aCharacters( getAccumulatedCharacters() );
199 const ::rtl::OUString sValue = aCharacters.makeStringAndClear();
201 const ::rtl::OUString& rItemType( getItemType() );
202 ENSURE_OR_RETURN_VOID( !rItemType.isEmpty(), "no item type -> no item value" );
204 if ( ::xmloff::token::IsXMLToken( rItemType, ::xmloff::token::XML_INT ) )
206 sal_Int32 nValue(0);
207 if (::sax::Converter::convertNumber( nValue, sValue ))
209 o_rValue <<= nValue;
211 else
213 OSL_FAIL( "ConfigItemImport::getItemValue: could not convert an int value!" );
216 else if ( ::xmloff::token::IsXMLToken( rItemType, ::xmloff::token::XML_BOOLEAN ) )
218 bool bValue(false);
219 if (::sax::Converter::convertBool( bValue, sValue ))
221 o_rValue <<= bValue;
223 else
225 OSL_FAIL( "ConfigItemImport::getItemValue: could not convert a boolean value!" );
228 else if ( ::xmloff::token::IsXMLToken( rItemType, ::xmloff::token::XML_STRING ) )
230 o_rValue <<= sValue;
232 #if OSL_DEBUG_LEVEL > 0
233 else
235 ::rtl::OString sMessage( "ConfigItemImport::getItemValue: unsupported item type '" );
236 sMessage += ::rtl::OUStringToOString( rItemType, RTL_TEXTENCODING_UTF8 );
237 sMessage += "', ignoring";
238 OSL_FAIL( sMessage.getStr() );
240 #endif
243 //====================================================================
244 //= ConfigItemSetImport
245 //====================================================================
246 //--------------------------------------------------------------------
247 ConfigItemSetImport::ConfigItemSetImport( ::comphelper::NamedValueCollection& o_rSettings )
248 :ConfigItemImport( o_rSettings )
252 //--------------------------------------------------------------------
253 ConfigItemSetImport::~ConfigItemSetImport()
257 //--------------------------------------------------------------------
258 ::rtl::Reference< SettingsImport > ConfigItemSetImport::nextState( const ::rtl::OUString& i_rElementName )
260 // separate the namespace part from the element name
261 ::rtl::OUString sNamespace;
262 ::rtl::OUString sLocalName;
263 split( i_rElementName, sNamespace, sLocalName );
265 if ( sLocalName == "config-item-set" )
266 return new ConfigItemSetImport( m_aChildSettings );
267 if ( sLocalName == "config-item" )
268 return new ConfigItemImport( m_aChildSettings );
270 #if OSL_DEBUG_LEVEL > 0
271 ::rtl::OString sMessage( "unknown element name '" );
272 sMessage += ::rtl::OUStringToOString( i_rElementName, RTL_TEXTENCODING_UTF8 );
273 sMessage += "', ignoring";
274 OSL_FAIL( sMessage.getStr() );
275 #endif
276 return new IgnoringSettingsImport;
279 //--------------------------------------------------------------------
280 void ConfigItemSetImport::getItemValue( Any& o_rValue ) const
282 o_rValue <<= m_aChildSettings.getPropertyValues();
285 //........................................................................
286 } // namespace dbaccess
287 //........................................................................
289 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */