LibreOffice Module dbaccess (master) 1
settingsimport.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#include "settingsimport.hxx"
21
23#include <sal/log.hxx>
25#include <xmloff/xmltoken.hxx>
26
27namespace dbaccess
28{
29
30 using ::com::sun::star::uno::Reference;
31 using ::com::sun::star::uno::Any;
32 using ::com::sun::star::xml::sax::XAttributeList;
33
34 // SettingsImport
36 {
37 }
38
40 {
41 }
42
43 void SettingsImport::startElement( const Reference< XAttributeList >& i_rAttributes )
44 {
45 // find the name of the setting
46 if ( i_rAttributes.is() )
47 {
48 m_sItemName = i_rAttributes->getValueByName( "config:name" );
49 m_sItemType = i_rAttributes->getValueByName( "config:type" );
50 }
51 }
52
54 {
55 }
56
57 void SettingsImport::characters( std::u16string_view i_rCharacters )
58 {
59 m_aCharacters.append( i_rCharacters );
60 }
61
62 void SettingsImport::split( const OUString& i_rElementName, OUString& o_rNamespace, OUString& o_rLocalName )
63 {
64 o_rNamespace.clear();
65 o_rLocalName = i_rElementName;
66 const sal_Int32 nSeparatorPos = i_rElementName.indexOf( ':' );
67 if ( nSeparatorPos > -1 )
68 {
69 o_rNamespace = i_rElementName.copy( 0, nSeparatorPos );
70 o_rLocalName = i_rElementName.copy( nSeparatorPos + 1 );
71 }
72
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 ...
76 }
77
78 // IgnoringSettingsImport
80 {
81 return this;
82 }
83
84 // OfficeSettingsImport
86 :m_rSettings( o_rSettings )
87 {
88 }
89
91 {
92 }
93
95 {
96 // separate the namespace part from the element name
97 OUString sNamespace;
98 OUString sLocalName;
99 split( i_rElementName, sNamespace, sLocalName );
100
101 if ( sLocalName == "config-item-set" )
102 return new ConfigItemSetImport( m_rSettings );
103
104 SAL_WARN( "dbaccess", "unknown (or unsupported at this place) element name '"
105 << i_rElementName
106 << "', ignoring");
107 return new IgnoringSettingsImport;
108 }
109
110 // ConfigItemImport
112 :m_rSettings( o_rSettings )
113 {
114 }
115
117 {
118 }
119
121 {
122 OSL_FAIL( "ConfigItemImport::nextState: unexpected: this class is responsible for child-less items only!" );
123 return new IgnoringSettingsImport;
124 }
125
127 {
129
130 const OUString sItemName( getItemName() );
131 ENSURE_OR_RETURN_VOID( !sItemName.isEmpty(), "no item name -> no item value" );
132 Any aValue;
133 getItemValue( aValue );
134 m_rSettings.put( sItemName, aValue );
135 }
136
137 void ConfigItemImport::getItemValue( css::uno::Any& o_rValue ) const
138 {
139 o_rValue.clear();
140
141 // the characters building up th evalue
142 std::u16string_view sValue = getAccumulatedCharacters();
143
144 const OUString& rItemType( getItemType() );
145 ENSURE_OR_RETURN_VOID( !rItemType.isEmpty(), "no item type -> no item value" );
146
147 if ( ::xmloff::token::IsXMLToken( rItemType, ::xmloff::token::XML_INT ) )
148 {
149 sal_Int32 nValue(0);
151 {
152 o_rValue <<= nValue;
153 }
154 else
155 {
156 OSL_FAIL( "ConfigItemImport::getItemValue: could not convert an int value!" );
157 }
158 }
159 else if ( ::xmloff::token::IsXMLToken( rItemType, ::xmloff::token::XML_BOOLEAN ) )
160 {
161 bool bValue(false);
162 if (::sax::Converter::convertBool( bValue, sValue ))
163 {
164 o_rValue <<= bValue;
165 }
166 else
167 {
168 OSL_FAIL( "ConfigItemImport::getItemValue: could not convert a boolean value!" );
169 }
170 }
171 else if ( ::xmloff::token::IsXMLToken( rItemType, ::xmloff::token::XML_STRING ) )
172 {
173 o_rValue <<= OUString(sValue);
174 }
175 else
176 {
177 SAL_WARN( "dbaccess", "ConfigItemImport::getItemValue: unsupported item type '"
178 << rItemType << "', ignoring");
179 }
180 }
181
182 // ConfigItemSetImport
184 :ConfigItemImport( o_rSettings )
185 {
186 }
187
189 {
190 }
191
193 {
194 // separate the namespace part from the element name
195 OUString sNamespace;
196 OUString sLocalName;
197 split( i_rElementName, sNamespace, sLocalName );
198
199 if ( sLocalName == "config-item-set" )
201 if ( sLocalName == "config-item" )
203
204 SAL_WARN( "dbaccess", "unknown element name '"
205 << i_rElementName << "', ignoring");
206 return new IgnoringSettingsImport;
207 }
208
209 void ConfigItemSetImport::getItemValue( Any& o_rValue ) const
210 {
211 o_rValue <<= m_aChildSettings.getPropertyValues();
212 }
213
214} // namespace dbaccess
215
216/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
bool put(const OUString &_rValueName, const VALUE_TYPE &_rValue)
css::uno::Sequence< css::beans::PropertyValue > getPropertyValues() const
virtual ::rtl::Reference< SettingsImport > nextState(const OUString &i_rElementName) override
virtual ~ConfigItemImport() override
virtual void getItemValue(css::uno::Any &o_rValue) const
retrieves the value represented by the element
ConfigItemImport(::comphelper::NamedValueCollection &o_rSettings)
::comphelper::NamedValueCollection & m_rSettings
virtual void endElement() override
::comphelper::NamedValueCollection m_aChildSettings
the settings represented by our child elements
ConfigItemSetImport(::comphelper::NamedValueCollection &o_rSettings)
virtual ::rtl::Reference< SettingsImport > nextState(const OUString &i_rElementName) override
virtual void getItemValue(css::uno::Any &o_rValue) const override
retrieves the value represented by the element
virtual ~ConfigItemSetImport() override
virtual ::rtl::Reference< SettingsImport > nextState(const OUString &i_rElementName) override
OfficeSettingsImport(::comphelper::NamedValueCollection &o_rSettings)
virtual ::rtl::Reference< SettingsImport > nextState(const OUString &i_rElementName) override
virtual ~OfficeSettingsImport() override
::comphelper::NamedValueCollection & m_rSettings
const OUStringBuffer & getAccumulatedCharacters() const
virtual ~SettingsImport() override
const OUString & getItemType() const
void startElement(const css::uno::Reference< css::xml::sax::XAttributeList > &i_rAttributes)
OUStringBuffer m_aCharacters
static void split(const OUString &i_rElementName, OUString &o_rNamespace, OUString &o_rLocalName)
void characters(std::u16string_view i_rCharacters)
const OUString & getItemName() const
static bool convertNumber(sal_Int32 &rValue, std::u16string_view aString, sal_Int32 nMin=SAL_MIN_INT32, sal_Int32 nMax=SAL_MAX_INT32)
static bool convertBool(bool &rBool, std::u16string_view rString)
#define ENSURE_OR_RETURN_VOID(c, m)
sal_Int16 nValue
#define SAL_WARN(area, stream)