1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 <comphelper/diagnose_ex.hxx>
23 #include <sal/log.hxx>
24 #include <sax/tools/converter.hxx>
25 #include <xmloff/xmltoken.hxx>
30 using ::com::sun::star::uno::Reference
;
31 using ::com::sun::star::uno::Any
;
32 using ::com::sun::star::xml::sax::XAttributeList
;
35 SettingsImport::SettingsImport()
39 SettingsImport::~SettingsImport()
43 void SettingsImport::startElement( const Reference
< XAttributeList
>& i_rAttributes
)
45 // find the name of the setting
46 if ( i_rAttributes
.is() )
48 m_sItemName
= i_rAttributes
->getValueByName( u
"config:name"_ustr
);
49 m_sItemType
= i_rAttributes
->getValueByName( u
"config:type"_ustr
);
53 void SettingsImport::endElement()
57 void SettingsImport::characters( std::u16string_view i_rCharacters
)
59 m_aCharacters
.append( i_rCharacters
);
62 void SettingsImport::split( const OUString
& i_rElementName
, OUString
& o_rNamespace
, OUString
& o_rLocalName
)
65 o_rLocalName
= i_rElementName
;
66 const sal_Int32 nSeparatorPos
= i_rElementName
.indexOf( ':' );
67 if ( nSeparatorPos
> -1 )
69 o_rNamespace
= i_rElementName
.copy( 0, nSeparatorPos
);
70 o_rLocalName
= i_rElementName
.copy( nSeparatorPos
+ 1 );
73 OSL_ENSURE( o_rNamespace
== "config", "SettingsImport::split: unexpected namespace!" );
74 // our recovery file is kind of hand-made, so there shouldn't be anything else than "config".
75 // If there is, then just ignore it ...
78 // IgnoringSettingsImport
79 ::rtl::Reference
< SettingsImport
> IgnoringSettingsImport::nextState( const OUString
& )
84 // OfficeSettingsImport
85 OfficeSettingsImport::OfficeSettingsImport( ::comphelper::NamedValueCollection
& o_rSettings
)
86 :m_rSettings( o_rSettings
)
90 OfficeSettingsImport::~OfficeSettingsImport()
94 ::rtl::Reference
< SettingsImport
> OfficeSettingsImport::nextState( const OUString
& i_rElementName
)
96 // separate the namespace part from the element name
99 split( i_rElementName
, sNamespace
, sLocalName
);
101 if ( sLocalName
== "config-item-set" )
102 return new ConfigItemSetImport( m_rSettings
);
104 SAL_WARN( "dbaccess", "unknown (or unsupported at this place) element name '"
107 return new IgnoringSettingsImport
;
111 ConfigItemImport::ConfigItemImport( ::comphelper::NamedValueCollection
& o_rSettings
)
112 :m_rSettings( o_rSettings
)
116 ConfigItemImport::~ConfigItemImport()
120 ::rtl::Reference
< SettingsImport
> ConfigItemImport::nextState( const OUString
& )
122 OSL_FAIL( "ConfigItemImport::nextState: unexpected: this class is responsible for child-less items only!" );
123 return new IgnoringSettingsImport
;
126 void ConfigItemImport::endElement()
128 SettingsImport::endElement();
130 const OUString
sItemName( getItemName() );
131 ENSURE_OR_RETURN_VOID( !sItemName
.isEmpty(), "no item name -> no item value" );
133 getItemValue( aValue
);
134 m_rSettings
.put( sItemName
, aValue
);
137 void ConfigItemImport::getItemValue( css::uno::Any
& o_rValue
) const
141 // the characters building up th evalue
142 std::u16string_view sValue
= getAccumulatedCharacters();
144 const OUString
& rItemType( getItemType() );
145 ENSURE_OR_RETURN_VOID( !rItemType
.isEmpty(), "no item type -> no item value" );
147 if ( ::xmloff::token::IsXMLToken( rItemType
, ::xmloff::token::XML_INT
) )
150 if (::sax::Converter::convertNumber( nValue
, sValue
))
156 OSL_FAIL( "ConfigItemImport::getItemValue: could not convert an int value!" );
159 else if ( ::xmloff::token::IsXMLToken( rItemType
, ::xmloff::token::XML_BOOLEAN
) )
162 if (::sax::Converter::convertBool( bValue
, sValue
))
168 OSL_FAIL( "ConfigItemImport::getItemValue: could not convert a boolean value!" );
171 else if ( ::xmloff::token::IsXMLToken( rItemType
, ::xmloff::token::XML_STRING
) )
173 o_rValue
<<= OUString(sValue
);
177 SAL_WARN( "dbaccess", "ConfigItemImport::getItemValue: unsupported item type '"
178 << rItemType
<< "', ignoring");
182 // ConfigItemSetImport
183 ConfigItemSetImport::ConfigItemSetImport( ::comphelper::NamedValueCollection
& o_rSettings
)
184 :ConfigItemImport( o_rSettings
)
188 ConfigItemSetImport::~ConfigItemSetImport()
192 ::rtl::Reference
< SettingsImport
> ConfigItemSetImport::nextState( const OUString
& i_rElementName
)
194 // separate the namespace part from the element name
197 split( i_rElementName
, sNamespace
, sLocalName
);
199 if ( sLocalName
== "config-item-set" )
200 return new ConfigItemSetImport( m_aChildSettings
);
201 if ( sLocalName
== "config-item" )
202 return new ConfigItemImport( m_aChildSettings
);
204 SAL_WARN( "dbaccess", "unknown element name '"
205 << i_rElementName
<< "', ignoring");
206 return new IgnoringSettingsImport
;
209 void ConfigItemSetImport::getItemValue( Any
& o_rValue
) const
211 o_rValue
<<= m_aChildSettings
.getPropertyValues();
214 } // namespace dbaccess
216 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */