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 <tools/diagnose_ex.h>
23 #include <sax/tools/converter.hxx>
24 #include <xmloff/xmltoken.hxx>
29 using ::com::sun::star::uno::Reference
;
30 using ::com::sun::star::uno::Any
;
31 using ::com::sun::star::xml::sax::XAttributeList
;
34 SettingsImport::SettingsImport()
38 SettingsImport::~SettingsImport()
42 void SettingsImport::startElement( const Reference
< XAttributeList
>& i_rAttributes
)
44 // find the name of the setting
45 if ( i_rAttributes
.is() )
47 m_sItemName
= i_rAttributes
->getValueByName( "config:name" );
48 m_sItemType
= i_rAttributes
->getValueByName( "config:type" );
52 void SettingsImport::endElement()
56 void SettingsImport::characters( const OUString
& i_rCharacters
)
58 m_aCharacters
.append( i_rCharacters
);
61 void SettingsImport::split( const OUString
& i_rElementName
, OUString
& o_rNamespace
, OUString
& o_rLocalName
)
64 o_rLocalName
= i_rElementName
;
65 const sal_Int32 nSeparatorPos
= i_rElementName
.indexOf( ':' );
66 if ( nSeparatorPos
> -1 )
68 o_rNamespace
= i_rElementName
.copy( 0, nSeparatorPos
);
69 o_rLocalName
= i_rElementName
.copy( nSeparatorPos
+ 1 );
72 OSL_ENSURE( o_rNamespace
== "config", "SettingsImport::split: unexpected namespace!" );
73 // our recovery file is kind of hand-made, so there shouldn't be anything else than "config".
74 // If there is, then just ignore it ...
77 // IgnoringSettingsImport
78 ::rtl::Reference
< SettingsImport
> IgnoringSettingsImport::nextState( const OUString
& i_rElementName
)
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 #if OSL_DEBUG_LEVEL > 0
105 OString sMessage
= "unknown (or unsupported at this place) element name '"
106 + OUStringToOString( i_rElementName
, RTL_TEXTENCODING_UTF8
)
108 OSL_FAIL( sMessage
.getStr() );
110 return new IgnoringSettingsImport
;
114 ConfigItemImport::ConfigItemImport( ::comphelper::NamedValueCollection
& o_rSettings
)
115 :m_rSettings( o_rSettings
)
119 ConfigItemImport::~ConfigItemImport()
123 ::rtl::Reference
< SettingsImport
> ConfigItemImport::nextState( const OUString
& i_rElementName
)
125 OSL_FAIL( "ConfigItemImport::nextState: unexpected: this class is responsible for child-less items only!" );
126 (void)i_rElementName
;
127 return new IgnoringSettingsImport
;
130 void ConfigItemImport::endElement()
132 SettingsImport::endElement();
134 const OUString
sItemName( getItemName() );
135 ENSURE_OR_RETURN_VOID( !sItemName
.isEmpty(), "no item name -> no item value" );
137 getItemValue( aValue
);
138 m_rSettings
.put( sItemName
, aValue
);
141 void ConfigItemImport::getItemValue( css::uno::Any
& o_rValue
) const
145 // the characters building up th evalue
146 OUStringBuffer
aCharacters( getAccumulatedCharacters() );
147 const OUString sValue
= aCharacters
.makeStringAndClear();
149 const OUString
& rItemType( getItemType() );
150 ENSURE_OR_RETURN_VOID( !rItemType
.isEmpty(), "no item type -> no item value" );
152 if ( ::xmloff::token::IsXMLToken( rItemType
, ::xmloff::token::XML_INT
) )
155 if (::sax::Converter::convertNumber( nValue
, sValue
))
161 OSL_FAIL( "ConfigItemImport::getItemValue: could not convert an int value!" );
164 else if ( ::xmloff::token::IsXMLToken( rItemType
, ::xmloff::token::XML_BOOLEAN
) )
167 if (::sax::Converter::convertBool( bValue
, sValue
))
173 OSL_FAIL( "ConfigItemImport::getItemValue: could not convert a boolean value!" );
176 else if ( ::xmloff::token::IsXMLToken( rItemType
, ::xmloff::token::XML_STRING
) )
180 #if OSL_DEBUG_LEVEL > 0
183 OString
sMessage( "ConfigItemImport::getItemValue: unsupported item type '" );
184 sMessage
+= OUStringToOString( rItemType
, RTL_TEXTENCODING_UTF8
);
185 sMessage
+= "', ignoring";
186 OSL_FAIL( sMessage
.getStr() );
191 // ConfigItemSetImport
192 ConfigItemSetImport::ConfigItemSetImport( ::comphelper::NamedValueCollection
& o_rSettings
)
193 :ConfigItemImport( o_rSettings
)
197 ConfigItemSetImport::~ConfigItemSetImport()
201 ::rtl::Reference
< SettingsImport
> ConfigItemSetImport::nextState( const OUString
& i_rElementName
)
203 // separate the namespace part from the element name
206 split( i_rElementName
, sNamespace
, sLocalName
);
208 if ( sLocalName
== "config-item-set" )
209 return new ConfigItemSetImport( m_aChildSettings
);
210 if ( sLocalName
== "config-item" )
211 return new ConfigItemImport( m_aChildSettings
);
213 #if OSL_DEBUG_LEVEL > 0
214 OString
sMessage( "unknown element name '" );
215 sMessage
+= OUStringToOString( i_rElementName
, RTL_TEXTENCODING_UTF8
);
216 sMessage
+= "', ignoring";
217 OSL_FAIL( sMessage
.getStr() );
219 return new IgnoringSettingsImport
;
222 void ConfigItemSetImport::getItemValue( Any
& o_rValue
) const
224 o_rValue
<<= m_aChildSettings
.getPropertyValues();
227 } // namespace dbaccess
229 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */