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::XInterface
;
31 using ::com::sun::star::uno::UNO_QUERY
;
32 using ::com::sun::star::uno::UNO_QUERY_THROW
;
33 using ::com::sun::star::uno::UNO_SET_THROW
;
34 using ::com::sun::star::uno::Exception
;
35 using ::com::sun::star::uno::RuntimeException
;
36 using ::com::sun::star::uno::Any
;
37 using ::com::sun::star::uno::makeAny
;
38 using ::com::sun::star::uno::Sequence
;
39 using ::com::sun::star::uno::Type
;
40 using ::com::sun::star::xml::sax::XAttributeList
;
43 SettingsImport::SettingsImport()
47 SettingsImport::~SettingsImport()
51 void SettingsImport::startElement( const Reference
< XAttributeList
>& i_rAttributes
)
53 // find the name of the setting
54 if ( i_rAttributes
.is() )
56 m_sItemName
= i_rAttributes
->getValueByName( "config:name" );
57 m_sItemType
= i_rAttributes
->getValueByName( "config:type" );
61 void SettingsImport::endElement()
65 void SettingsImport::characters( const OUString
& i_rCharacters
)
67 m_aCharacters
.append( i_rCharacters
);
70 void SettingsImport::split( const OUString
& i_rElementName
, OUString
& o_rNamespace
, OUString
& o_rLocalName
)
73 o_rLocalName
= i_rElementName
;
74 const sal_Int32 nSeparatorPos
= i_rElementName
.indexOf( ':' );
75 if ( nSeparatorPos
> -1 )
77 o_rNamespace
= i_rElementName
.copy( 0, nSeparatorPos
);
78 o_rLocalName
= i_rElementName
.copy( nSeparatorPos
+ 1 );
81 OSL_ENSURE( o_rNamespace
== "config", "SettingsImport::split: unexpected namespace!" );
82 // our recovery file is kind of hand-made, so there shouldn't be anything else than "config".
83 // If there is, then just ignore it ...
86 // IgnoringSettingsImport
87 ::rtl::Reference
< SettingsImport
> IgnoringSettingsImport::nextState( const OUString
& i_rElementName
)
93 // OfficeSettingsImport
94 OfficeSettingsImport::OfficeSettingsImport( ::comphelper::NamedValueCollection
& o_rSettings
)
95 :m_rSettings( o_rSettings
)
99 OfficeSettingsImport::~OfficeSettingsImport()
103 ::rtl::Reference
< SettingsImport
> OfficeSettingsImport::nextState( const OUString
& i_rElementName
)
105 // separate the namespace part from the element name
108 split( i_rElementName
, sNamespace
, sLocalName
);
110 if ( sLocalName
== "config-item-set" )
111 return new ConfigItemSetImport( m_rSettings
);
113 #if OSL_DEBUG_LEVEL > 0
114 OString
sMessage( "unknown (or unsupported at this place) element name '" );
115 sMessage
+= OUStringToOString( i_rElementName
, RTL_TEXTENCODING_UTF8
);
116 sMessage
+= "', ignoring";
117 OSL_FAIL( sMessage
.getStr() );
119 return new IgnoringSettingsImport
;
123 ConfigItemImport::ConfigItemImport( ::comphelper::NamedValueCollection
& o_rSettings
)
124 :m_rSettings( o_rSettings
)
128 ConfigItemImport::~ConfigItemImport()
132 ::rtl::Reference
< SettingsImport
> ConfigItemImport::nextState( const OUString
& i_rElementName
)
134 OSL_FAIL( "ConfigItemImport::nextState: unexpected: this class is responsible for child-less items only!" );
135 (void)i_rElementName
;
136 return new IgnoringSettingsImport
;
139 void ConfigItemImport::endElement()
141 SettingsImport::endElement();
143 const OUString
sItemName( getItemName() );
144 ENSURE_OR_RETURN_VOID( !sItemName
.isEmpty(), "no item name -> no item value" );
146 getItemValue( aValue
);
147 m_rSettings
.put( sItemName
, aValue
);
150 void ConfigItemImport::getItemValue( ::com::sun::star::uno::Any
& o_rValue
) const
154 // the characters building up th evalue
155 OUStringBuffer
aCharacters( getAccumulatedCharacters() );
156 const OUString sValue
= aCharacters
.makeStringAndClear();
158 const OUString
& rItemType( getItemType() );
159 ENSURE_OR_RETURN_VOID( !rItemType
.isEmpty(), "no item type -> no item value" );
161 if ( ::xmloff::token::IsXMLToken( rItemType
, ::xmloff::token::XML_INT
) )
164 if (::sax::Converter::convertNumber( nValue
, sValue
))
170 OSL_FAIL( "ConfigItemImport::getItemValue: could not convert an int value!" );
173 else if ( ::xmloff::token::IsXMLToken( rItemType
, ::xmloff::token::XML_BOOLEAN
) )
176 if (::sax::Converter::convertBool( bValue
, sValue
))
182 OSL_FAIL( "ConfigItemImport::getItemValue: could not convert a boolean value!" );
185 else if ( ::xmloff::token::IsXMLToken( rItemType
, ::xmloff::token::XML_STRING
) )
189 #if OSL_DEBUG_LEVEL > 0
192 OString
sMessage( "ConfigItemImport::getItemValue: unsupported item type '" );
193 sMessage
+= OUStringToOString( rItemType
, RTL_TEXTENCODING_UTF8
);
194 sMessage
+= "', ignoring";
195 OSL_FAIL( sMessage
.getStr() );
200 // ConfigItemSetImport
201 ConfigItemSetImport::ConfigItemSetImport( ::comphelper::NamedValueCollection
& o_rSettings
)
202 :ConfigItemImport( o_rSettings
)
206 ConfigItemSetImport::~ConfigItemSetImport()
210 ::rtl::Reference
< SettingsImport
> ConfigItemSetImport::nextState( const OUString
& i_rElementName
)
212 // separate the namespace part from the element name
215 split( i_rElementName
, sNamespace
, sLocalName
);
217 if ( sLocalName
== "config-item-set" )
218 return new ConfigItemSetImport( m_aChildSettings
);
219 if ( sLocalName
== "config-item" )
220 return new ConfigItemImport( m_aChildSettings
);
222 #if OSL_DEBUG_LEVEL > 0
223 OString
sMessage( "unknown element name '" );
224 sMessage
+= OUStringToOString( i_rElementName
, RTL_TEXTENCODING_UTF8
);
225 sMessage
+= "', ignoring";
226 OSL_FAIL( sMessage
.getStr() );
228 return new IgnoringSettingsImport
;
231 void ConfigItemSetImport::getItemValue( Any
& o_rValue
) const
233 o_rValue
<<= m_aChildSettings
.getPropertyValues();
236 } // namespace dbaccess
238 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */