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 "sal/config.h"
24 #include "xmlDataSourceSetting.hxx"
25 #include "xmlDataSource.hxx"
26 #include <sax/tools/converter.hxx>
27 #include "xmlfilter.hxx"
28 #include <xmloff/xmltoken.hxx>
29 #include <xmloff/xmlnmspe.hxx>
30 #include <xmloff/nmspmap.hxx>
31 #include "xmlEnums.hxx"
32 #include "xmlstrings.hrc"
33 #include <rtl/strbuf.hxx>
34 #include <tools/debug.hxx>
38 using namespace ::com::sun::star::uno
;
39 using namespace ::com::sun::star::xml::sax
;
41 OXMLDataSourceSetting::OXMLDataSourceSetting( ODBFilter
& rImport
43 ,const OUString
& _sLocalName
44 ,const Reference
< XAttributeList
> & _xAttrList
45 ,OXMLDataSourceSetting
* _pContainer
) :
46 SvXMLImportContext( rImport
, nPrfx
, _sLocalName
)
47 ,m_pContainer(_pContainer
)
51 m_aPropType
= cppu::UnoType
<void>::get();
53 OSL_ENSURE(_xAttrList
.is(),"Attribute list is NULL!");
54 const SvXMLNamespaceMap
& rMap
= rImport
.GetNamespaceMap();
55 const SvXMLTokenMap
& rTokenMap
= rImport
.GetDataSourceInfoElemTokenMap();
57 sal_Int16 nLength
= (_xAttrList
.is()) ? _xAttrList
->getLength() : 0;
58 for(sal_Int16 i
= 0; i
< nLength
; ++i
)
61 OUString sAttrName
= _xAttrList
->getNameByIndex( i
);
62 sal_uInt16 nPrefix
= rMap
.GetKeyByAttrName( sAttrName
,&sLocalName
);
63 OUString sValue
= _xAttrList
->getValueByIndex( i
);
65 switch( rTokenMap
.Get( nPrefix
, sLocalName
) )
67 case XML_TOK_DATA_SOURCE_SETTING_IS_LIST
:
68 m_bIsList
= sValue
== "true";
70 case XML_TOK_DATA_SOURCE_SETTING_TYPE
:
72 // needs to be translated into a ::com::sun::star::uno::Type
73 static std::map
< OUString
, css::uno::Type
> s_aTypeNameMap
;
74 if (s_aTypeNameMap
.empty())
76 s_aTypeNameMap
[GetXMLToken( XML_BOOLEAN
)] = cppu::UnoType
<bool>::get();
77 // Not a copy paste error, see comment xmloff/source/forms/propertyimport.cxx lines 244-248
78 s_aTypeNameMap
[GetXMLToken( XML_FLOAT
)] = ::cppu::UnoType
<double>::get();
79 s_aTypeNameMap
[GetXMLToken( XML_DOUBLE
)] = ::cppu::UnoType
<double>::get();
80 s_aTypeNameMap
[GetXMLToken( XML_STRING
)] = ::cppu::UnoType
<OUString
>::get();
81 s_aTypeNameMap
[GetXMLToken( XML_INT
)] = ::cppu::UnoType
<sal_Int32
>::get();
82 s_aTypeNameMap
[GetXMLToken( XML_SHORT
)] = ::cppu::UnoType
<sal_Int16
>::get();
83 s_aTypeNameMap
[GetXMLToken( XML_VOID
)] = cppu::UnoType
<void>::get();
86 const std::map
< OUString
, css::uno::Type
>::const_iterator aTypePos
= s_aTypeNameMap
.find(sValue
);
87 OSL_ENSURE(s_aTypeNameMap
.end() != aTypePos
, "OXMLDataSourceSetting::OXMLDataSourceSetting: invalid type!");
88 if (s_aTypeNameMap
.end() != aTypePos
)
89 m_aPropType
= aTypePos
->second
;
92 case XML_TOK_DATA_SOURCE_SETTING_NAME
:
93 m_aSetting
.Name
= sValue
;
100 OXMLDataSourceSetting::~OXMLDataSourceSetting()
104 SvXMLImportContext
* OXMLDataSourceSetting::CreateChildContext(
106 const OUString
& rLocalName
,
107 const Reference
< XAttributeList
> & xAttrList
)
109 SvXMLImportContext
*pContext
= 0;
110 const SvXMLTokenMap
& rTokenMap
= GetOwnImport().GetDataSourceInfoElemTokenMap();
112 switch( rTokenMap
.Get( nPrefix
, rLocalName
) )
114 case XML_TOK_DATA_SOURCE_SETTING
:
115 GetOwnImport().GetProgressBarHelper()->Increment( PROGRESS_BAR_STEP
);
116 pContext
= new OXMLDataSourceSetting( GetOwnImport(), nPrefix
, rLocalName
,xAttrList
);
118 case XML_TOK_DATA_SOURCE_SETTING_VALUE
:
119 GetOwnImport().GetProgressBarHelper()->Increment( PROGRESS_BAR_STEP
);
120 pContext
= new OXMLDataSourceSetting( GetOwnImport(), nPrefix
, rLocalName
,xAttrList
,this );
125 pContext
= new SvXMLImportContext( GetImport(), nPrefix
, rLocalName
);
130 void OXMLDataSourceSetting::EndElement()
132 if ( !m_aSetting
.Name
.isEmpty() )
134 if ( m_bIsList
&& m_aInfoSequence
.getLength() )
135 m_aSetting
.Value
<<= m_aInfoSequence
;
137 // if our property is of type string, but was empty, ensure that
138 // we don't add a VOID value
139 if ( !m_bIsList
&& ( m_aPropType
.getTypeClass() == TypeClass_STRING
) && !m_aSetting
.Value
.hasValue() )
140 m_aSetting
.Value
<<= OUString();
142 GetOwnImport().addInfo(m_aSetting
);
146 void OXMLDataSourceSetting::Characters( const OUString
& rChars
)
149 m_pContainer
->addValue(rChars
);
152 void OXMLDataSourceSetting::addValue(const OUString
& _sValue
)
155 if( TypeClass_VOID
!= m_aPropType
.getTypeClass() )
156 aValue
= convertString(m_aPropType
, _sValue
);
159 m_aSetting
.Value
= aValue
;
162 sal_Int32 nPos
= m_aInfoSequence
.getLength();
163 m_aInfoSequence
.realloc(nPos
+1);
164 m_aInfoSequence
[nPos
] = aValue
;
168 ODBFilter
& OXMLDataSourceSetting::GetOwnImport()
170 return static_cast<ODBFilter
&>(GetImport());
173 Any
OXMLDataSourceSetting::convertString(const ::com::sun::star::uno::Type
& _rExpectedType
, const OUString
& _rReadCharacters
)
176 switch (_rExpectedType
.getTypeClass())
178 case TypeClass_BOOLEAN
: // sal_Bool
181 bool const bSuccess
=
182 ::sax::Converter::convertBool(bValue
, _rReadCharacters
);
183 SAL_WARN_IF(!bSuccess
, "dbaccess",
184 "OXMLDataSourceSetting::convertString: could not convert \""
185 << _rReadCharacters
<< "\" into a boolean!");
189 case TypeClass_SHORT
: // sal_Int16
190 case TypeClass_LONG
: // sal_Int32
191 { // it's a real int32/16 property
193 bool const bSuccess
=
194 ::sax::Converter::convertNumber(nValue
, _rReadCharacters
);
195 SAL_WARN_IF(!bSuccess
, "dbaccess",
196 "OXMLDataSourceSetting::convertString: could not convert \""
197 << _rReadCharacters
<< "\" into an integer!");
198 if (TypeClass_SHORT
== _rExpectedType
.getTypeClass())
199 aReturn
<<= (sal_Int16
)nValue
;
201 aReturn
<<= (sal_Int32
)nValue
;
204 case TypeClass_HYPER
:
206 OSL_FAIL("OXMLDataSourceSetting::convertString: 64-bit integers not implemented yet!");
209 case TypeClass_DOUBLE
:
212 bool const bSuccess
=
213 ::sax::Converter::convertDouble(nValue
, _rReadCharacters
);
214 SAL_WARN_IF(!bSuccess
, "dbaccess",
215 "OXMLDataSourceSetting::convertString: could not convert \""
216 << _rReadCharacters
<< "\" into a double!");
217 aReturn
<<= (double)nValue
;
220 case TypeClass_STRING
:
221 aReturn
<<= _rReadCharacters
;
225 "OXMLDataSourceSetting::convertString: invalid type class!");
231 } // namespace dbaxml
233 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */