xref: /AOO41X/main/dbaccess/source/core/recovery/settingsimport.cxx (revision 1ecadb572e7010ff3b3382ad9bf179dbc6efadbb)
1 /*************************************************************************
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3 *
4 * Copyright 2009 by Sun Microsystems, Inc.
5 *
6 * OpenOffice.org - a multi-platform office productivity suite
7 *
8 * This file is part of OpenOffice.org.
9 *
10 * OpenOffice.org is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License version 3
12 * only, as published by the Free Software Foundation.
13 *
14 * OpenOffice.org is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 * GNU Lesser General Public License version 3 for more details
18 * (a copy is included in the LICENSE file that accompanied this code).
19 *
20 * You should have received a copy of the GNU Lesser General Public License
21 * version 3 along with OpenOffice.org.  If not, see
22 * <http://www.openoffice.org/license.html>
23 * for a copy of the LGPLv3 License.
24 ************************************************************************/
25 
26 #include "precompiled_dbaccess.hxx"
27 
28 #include "settingsimport.hxx"
29 
30 /** === begin UNO includes === **/
31 /** === end UNO includes === **/
32 
33 #include <tools/diagnose_ex.h>
34 #include <xmloff/xmltoken.hxx>
35 #include <xmloff/xmluconv.hxx>
36 
37 //........................................................................
38 namespace dbaccess
39 {
40 //........................................................................
41 
42 	/** === begin UNO using === **/
43 	using ::com::sun::star::uno::Reference;
44 	using ::com::sun::star::uno::XInterface;
45 	using ::com::sun::star::uno::UNO_QUERY;
46 	using ::com::sun::star::uno::UNO_QUERY_THROW;
47 	using ::com::sun::star::uno::UNO_SET_THROW;
48 	using ::com::sun::star::uno::Exception;
49 	using ::com::sun::star::uno::RuntimeException;
50 	using ::com::sun::star::uno::Any;
51 	using ::com::sun::star::uno::makeAny;
52 	using ::com::sun::star::uno::Sequence;
53 	using ::com::sun::star::uno::Type;
54     using ::com::sun::star::xml::sax::XAttributeList;
55 	/** === end UNO using === **/
56 
57 	//====================================================================
58 	//= SettingsImport
59 	//====================================================================
60 	//--------------------------------------------------------------------
61     SettingsImport::SettingsImport()
62         :m_refCount( 0 )
63     {
64     }
65 
66 	//--------------------------------------------------------------------
67     SettingsImport::~SettingsImport()
68     {
69     }
70 
71 	//--------------------------------------------------------------------
72     oslInterlockedCount SAL_CALL SettingsImport::acquire()
73     {
74         return osl_incrementInterlockedCount( &m_refCount );
75     }
76 
77 	//--------------------------------------------------------------------
78     oslInterlockedCount SAL_CALL SettingsImport::release()
79     {
80         oslInterlockedCount newCount = osl_decrementInterlockedCount( &m_refCount );
81         if ( newCount == 0 )
82             delete this;
83         return newCount;
84     }
85 
86 	//--------------------------------------------------------------------
87     void SettingsImport::startElement( const Reference< XAttributeList >& i_rAttributes )
88     {
89         // find the name of the setting
90         if ( i_rAttributes.is() )
91         {
92             m_sItemName = i_rAttributes->getValueByName( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "config:name" ) ) );
93             m_sItemType = i_rAttributes->getValueByName( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "config:type" ) ) );
94         }
95     }
96 
97 	//--------------------------------------------------------------------
98     void SettingsImport::endElement()
99     {
100     }
101 
102 	//--------------------------------------------------------------------
103     void SettingsImport::characters( const ::rtl::OUString& i_rCharacters )
104     {
105         m_aCharacters.append( i_rCharacters );
106     }
107 
108 	//--------------------------------------------------------------------
109     void SettingsImport::split( const ::rtl::OUString& i_rElementName, ::rtl::OUString& o_rNamespace, ::rtl::OUString& o_rLocalName )
110     {
111         o_rNamespace = ::rtl::OUString();
112         o_rLocalName = i_rElementName;
113         const sal_Int32 nSeparatorPos = i_rElementName.indexOf( ':' );
114         if ( nSeparatorPos > -1 )
115         {
116             o_rNamespace = i_rElementName.copy( 0, nSeparatorPos );
117             o_rLocalName = i_rElementName.copy( nSeparatorPos + 1 );
118         }
119 
120         OSL_ENSURE( o_rNamespace.equalsAscii( "config" ), "SettingsImport::split: unexpected namespace!" );
121             // our recovery file is kind of hand-made, so there shouldn't be anything else than "config".
122             // If there is, then just ignore it ...
123     }
124 
125 	//====================================================================
126 	//= IgnoringSettingsImport
127 	//====================================================================
128 	//--------------------------------------------------------------------
129     ::rtl::Reference< SettingsImport > IgnoringSettingsImport::nextState( const ::rtl::OUString& i_rElementName )
130     {
131         (void)i_rElementName;
132         return this;
133     }
134 
135 	//====================================================================
136 	//= OfficeSettingsImport
137 	//====================================================================
138 	//--------------------------------------------------------------------
139     OfficeSettingsImport::OfficeSettingsImport( ::comphelper::NamedValueCollection& o_rSettings )
140         :m_rSettings( o_rSettings )
141     {
142     }
143 
144 	//--------------------------------------------------------------------
145     OfficeSettingsImport::~OfficeSettingsImport()
146     {
147     }
148 
149 	//--------------------------------------------------------------------
150     ::rtl::Reference< SettingsImport > OfficeSettingsImport::nextState( const ::rtl::OUString& i_rElementName )
151     {
152         // separate the namespace part from the element name
153         ::rtl::OUString sNamespace;
154         ::rtl::OUString sLocalName;
155         split( i_rElementName, sNamespace, sLocalName );
156 
157         if ( sLocalName.equalsAscii( "config-item-set" ) )
158             return new ConfigItemSetImport( m_rSettings );
159 
160 #if OSL_DEBUG_LEVEL > 0
161         ::rtl::OString sMessage( "unknown (or unsupported at this place) element name '" );
162         sMessage += ::rtl::OUStringToOString( i_rElementName, RTL_TEXTENCODING_UTF8 );
163         sMessage += "', ignoring";
164         OSL_ENSURE( false, sMessage.getStr() );
165 #endif
166         return new IgnoringSettingsImport;
167     }
168 
169 	//====================================================================
170 	//= ConfigItemImport
171 	//====================================================================
172 	//--------------------------------------------------------------------
173     ConfigItemImport::ConfigItemImport( ::comphelper::NamedValueCollection& o_rSettings )
174         :m_rSettings( o_rSettings )
175     {
176     }
177 
178 	//--------------------------------------------------------------------
179     ConfigItemImport::~ConfigItemImport()
180     {
181     }
182 
183 	//--------------------------------------------------------------------
184     ::rtl::Reference< SettingsImport > ConfigItemImport::nextState( const ::rtl::OUString& i_rElementName )
185     {
186         OSL_ENSURE( false, "ConfigItemImport::nextState: unexpected: this class is responsible for child-less items only!" );
187         (void)i_rElementName;
188         return new IgnoringSettingsImport;
189     }
190 
191 	//--------------------------------------------------------------------
192     void ConfigItemImport::endElement()
193     {
194         SettingsImport::endElement();
195 
196         const ::rtl::OUString sItemName( getItemName() );
197         ENSURE_OR_RETURN_VOID( sItemName.getLength(), "no item name -> no item value" );
198         Any aValue;
199         getItemValue( aValue );
200         m_rSettings.put( sItemName, aValue );
201     }
202 
203 	//--------------------------------------------------------------------
204     void ConfigItemImport::getItemValue( ::com::sun::star::uno::Any& o_rValue ) const
205     {
206         o_rValue.clear();
207 
208         // the characters building up th evalue
209         ::rtl::OUStringBuffer aCharacters( getAccumulatedCharacters() );
210         const ::rtl::OUString sValue = aCharacters.makeStringAndClear();
211 
212         const ::rtl::OUString& rItemType( getItemType() );
213         ENSURE_OR_RETURN_VOID( rItemType.getLength(), "no item type -> no item value" );
214 
215         if ( ::xmloff::token::IsXMLToken( rItemType, ::xmloff::token::XML_INT ) )
216         {
217             sal_Int32 nValue(0);
218             if ( SvXMLUnitConverter::convertNumber( nValue, sValue ) )
219                 o_rValue <<= nValue;
220             else
221             {
222                 OSL_ENSURE( false, "ConfigItemImport::getItemValue: could not convert an int value!" );
223             }
224         }
225         else if ( ::xmloff::token::IsXMLToken( rItemType, ::xmloff::token::XML_BOOLEAN ) )
226         {
227             sal_Bool nValue( sal_False );
228             if ( SvXMLUnitConverter::convertBool( nValue, sValue ) )
229                 o_rValue <<= nValue;
230             else
231             {
232                 OSL_ENSURE( false, "ConfigItemImport::getItemValue: could not convert a boolean value!" );
233             }
234         }
235         else if ( ::xmloff::token::IsXMLToken( rItemType, ::xmloff::token::XML_STRING ) )
236         {
237             o_rValue <<= sValue;
238         }
239 #if OSL_DEBUG_LEVEL > 0
240         else
241         {
242             ::rtl::OString sMessage( "ConfigItemImport::getItemValue: unsupported item type '" );
243             sMessage += ::rtl::OUStringToOString( rItemType, RTL_TEXTENCODING_UTF8 );
244             sMessage += "', ignoring";
245             OSL_ENSURE( false, sMessage.getStr() );
246         }
247 #endif
248     }
249 
250 	//====================================================================
251 	//= ConfigItemSetImport
252 	//====================================================================
253 	//--------------------------------------------------------------------
254     ConfigItemSetImport::ConfigItemSetImport( ::comphelper::NamedValueCollection& o_rSettings )
255         :ConfigItemImport( o_rSettings )
256     {
257     }
258 
259 	//--------------------------------------------------------------------
260     ConfigItemSetImport::~ConfigItemSetImport()
261     {
262     }
263 
264 	//--------------------------------------------------------------------
265     ::rtl::Reference< SettingsImport > ConfigItemSetImport::nextState( const ::rtl::OUString& i_rElementName )
266     {
267         // separate the namespace part from the element name
268         ::rtl::OUString sNamespace;
269         ::rtl::OUString sLocalName;
270         split( i_rElementName, sNamespace, sLocalName );
271 
272         if ( sLocalName.equalsAscii( "config-item-set" ) )
273             return new ConfigItemSetImport( m_aChildSettings );
274         if ( sLocalName.equalsAscii( "config-item" ) )
275             return new ConfigItemImport( m_aChildSettings );
276 
277 #if OSL_DEBUG_LEVEL > 0
278         ::rtl::OString sMessage( "unknown element name '" );
279         sMessage += ::rtl::OUStringToOString( i_rElementName, RTL_TEXTENCODING_UTF8 );
280         sMessage += "', ignoring";
281         OSL_ENSURE( false, sMessage.getStr() );
282 #endif
283         return new IgnoringSettingsImport;
284     }
285 
286 	//--------------------------------------------------------------------
287     void ConfigItemSetImport::getItemValue( Any& o_rValue ) const
288     {
289         o_rValue <<= m_aChildSettings.getPropertyValues();
290     }
291 
292 //........................................................................
293 } // namespace dbaccess
294 //........................................................................
295