1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: xmlDataSourceSetting.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_dbaccess.hxx"
33 #ifndef DBA_XMLDATASOURCESETTING_HXX
34 #include "xmlDataSourceSetting.hxx"
36 #ifndef DBA_XMLDATASOURCE_HXX
37 #include "xmlDataSource.hxx"
39 #ifndef _XMLOFF_XMLUCONV_HXX
40 #include <xmloff/xmluconv.hxx>
42 #ifndef DBA_XMLFILTER_HXX
43 #include "xmlfilter.hxx"
45 #ifndef _XMLOFF_XMLTOKEN_HXX
46 #include <xmloff/xmltoken.hxx>
48 #ifndef _XMLOFF_XMLNMSPE_HXX
49 #include <xmloff/xmlnmspe.hxx>
51 #ifndef _XMLOFF_NMSPMAP_HXX
52 #include <xmloff/nmspmap.hxx>
54 #ifndef DBA_XMLENUMS_HXX
55 #include "xmlEnums.hxx"
57 #ifndef DBACCESS_SHARED_XMLSTRINGS_HRC
58 #include "xmlstrings.hrc"
60 #ifndef _TOOLS_DEBUG_HXX
61 #include <tools/debug.hxx>
66 using namespace ::com::sun::star::uno
;
67 using namespace ::com::sun::star::xml::sax
;
68 DBG_NAME(OXMLDataSourceSetting
)
70 OXMLDataSourceSetting::OXMLDataSourceSetting( ODBFilter
& rImport
72 ,const ::rtl::OUString
& _sLocalName
73 ,const Reference
< XAttributeList
> & _xAttrList
74 ,OXMLDataSourceSetting
* _pContainer
) :
75 SvXMLImportContext( rImport
, nPrfx
, _sLocalName
)
76 ,m_pContainer(_pContainer
)
79 DBG_CTOR(OXMLDataSourceSetting
,NULL
);
81 m_aPropType
= ::getVoidCppuType();
83 OSL_ENSURE(_xAttrList
.is(),"Attribute list is NULL!");
84 const SvXMLNamespaceMap
& rMap
= rImport
.GetNamespaceMap();
85 const SvXMLTokenMap
& rTokenMap
= rImport
.GetDataSourceInfoElemTokenMap();
87 sal_Int16 nLength
= (_xAttrList
.is()) ? _xAttrList
->getLength() : 0;
88 for(sal_Int16 i
= 0; i
< nLength
; ++i
)
90 ::rtl::OUString sLocalName
;
91 rtl::OUString sAttrName
= _xAttrList
->getNameByIndex( i
);
92 sal_uInt16 nPrefix
= rMap
.GetKeyByAttrName( sAttrName
,&sLocalName
);
93 rtl::OUString sValue
= _xAttrList
->getValueByIndex( i
);
95 switch( rTokenMap
.Get( nPrefix
, sLocalName
) )
97 case XML_TOK_DATA_SOURCE_SETTING_IS_LIST
:
98 m_bIsList
= sValue
.equalsAscii("true");
100 case XML_TOK_DATA_SOURCE_SETTING_TYPE
:
102 // needs to be translated into a ::com::sun::star::uno::Type
103 DECLARE_STL_USTRINGACCESS_MAP( ::com::sun::star::uno::Type
, MapString2Type
);
104 static MapString2Type s_aTypeNameMap
;
105 if (!s_aTypeNameMap
.size())
107 s_aTypeNameMap
[GetXMLToken( XML_BOOLEAN
)] = ::getBooleanCppuType();
108 s_aTypeNameMap
[GetXMLToken( XML_FLOAT
)] = ::getCppuType( static_cast< double* >(NULL
) );
109 s_aTypeNameMap
[GetXMLToken( XML_DOUBLE
)] = ::getCppuType( static_cast< double* >(NULL
) );
110 s_aTypeNameMap
[GetXMLToken( XML_STRING
)] = ::getCppuType( static_cast< ::rtl::OUString
* >(NULL
) );
111 s_aTypeNameMap
[GetXMLToken( XML_INT
)] = ::getCppuType( static_cast< sal_Int32
* >(NULL
) );
112 s_aTypeNameMap
[GetXMLToken( XML_SHORT
)] = ::getCppuType( static_cast< sal_Int16
* >(NULL
) );
113 s_aTypeNameMap
[GetXMLToken( XML_VOID
)] = ::getVoidCppuType();
116 const ConstMapString2TypeIterator aTypePos
= s_aTypeNameMap
.find(sValue
);
117 OSL_ENSURE(s_aTypeNameMap
.end() != aTypePos
, "OXMLDataSourceSetting::OXMLDataSourceSetting: invalid type!");
118 if (s_aTypeNameMap
.end() != aTypePos
)
119 m_aPropType
= aTypePos
->second
;
122 case XML_TOK_DATA_SOURCE_SETTING_NAME
:
123 m_aSetting
.Name
= sValue
;
129 // -----------------------------------------------------------------------------
131 OXMLDataSourceSetting::~OXMLDataSourceSetting()
133 DBG_DTOR(OXMLDataSourceSetting
,NULL
);
135 // -----------------------------------------------------------------------------
136 SvXMLImportContext
* OXMLDataSourceSetting::CreateChildContext(
138 const ::rtl::OUString
& rLocalName
,
139 const Reference
< XAttributeList
> & xAttrList
)
141 SvXMLImportContext
*pContext
= 0;
142 const SvXMLTokenMap
& rTokenMap
= GetOwnImport().GetDataSourceInfoElemTokenMap();
144 switch( rTokenMap
.Get( nPrefix
, rLocalName
) )
146 case XML_TOK_DATA_SOURCE_SETTING
:
147 GetOwnImport().GetProgressBarHelper()->Increment( PROGRESS_BAR_STEP
);
148 pContext
= new OXMLDataSourceSetting( GetOwnImport(), nPrefix
, rLocalName
,xAttrList
);
150 case XML_TOK_DATA_SOURCE_SETTING_VALUE
:
151 GetOwnImport().GetProgressBarHelper()->Increment( PROGRESS_BAR_STEP
);
152 pContext
= new OXMLDataSourceSetting( GetOwnImport(), nPrefix
, rLocalName
,xAttrList
,this );
157 pContext
= new SvXMLImportContext( GetImport(), nPrefix
, rLocalName
);
161 // -----------------------------------------------------------------------------
162 void OXMLDataSourceSetting::EndElement()
164 if ( m_aSetting
.Name
.getLength() )
166 if ( m_bIsList
&& m_aInfoSequence
.getLength() )
167 m_aSetting
.Value
<<= m_aInfoSequence
;
169 // if our property is of type string, but was empty, ensure that
170 // we don't add a VOID value
171 if ( !m_bIsList
&& ( m_aPropType
.getTypeClass() == TypeClass_STRING
) && !m_aSetting
.Value
.hasValue() )
172 m_aSetting
.Value
<<= ::rtl::OUString();
174 GetOwnImport().addInfo(m_aSetting
);
177 // -----------------------------------------------------------------------------
178 void OXMLDataSourceSetting::Characters( const ::rtl::OUString
& rChars
)
181 m_pContainer
->addValue(rChars
);
183 // -----------------------------------------------------------------------------
184 void OXMLDataSourceSetting::addValue(const ::rtl::OUString
& _sValue
)
187 if( TypeClass_VOID
!= m_aPropType
.getTypeClass() )
188 aValue
= convertString(m_aPropType
, _sValue
);
191 m_aSetting
.Value
= aValue
;
194 sal_Int32 nPos
= m_aInfoSequence
.getLength();
195 m_aInfoSequence
.realloc(nPos
+1);
196 m_aInfoSequence
[nPos
] = aValue
;
199 // -----------------------------------------------------------------------------
200 ODBFilter
& OXMLDataSourceSetting::GetOwnImport()
202 return static_cast<ODBFilter
&>(GetImport());
204 // -----------------------------------------------------------------------------
205 Any
OXMLDataSourceSetting::convertString(const ::com::sun::star::uno::Type
& _rExpectedType
, const ::rtl::OUString
& _rReadCharacters
)
207 ODBFilter
& rImporter
= GetOwnImport();
209 switch (_rExpectedType
.getTypeClass())
211 case TypeClass_BOOLEAN
: // sal_Bool
214 #if OSL_DEBUG_LEVEL > 0
217 rImporter
.GetMM100UnitConverter().convertBool(bValue
, _rReadCharacters
);
219 ::rtl::OString("OXMLDataSourceSetting::convertString: could not convert \"")
220 += ::rtl::OString(_rReadCharacters
.getStr(), _rReadCharacters
.getLength(), RTL_TEXTENCODING_ASCII_US
)
221 += ::rtl::OString("\" into a boolean!"));
225 case TypeClass_SHORT
: // sal_Int16
226 case TypeClass_LONG
: // sal_Int32
227 { // it's a real int32/16 property
229 #if OSL_DEBUG_LEVEL > 0
232 rImporter
.GetMM100UnitConverter().convertNumber(nValue
, _rReadCharacters
);
234 ::rtl::OString("OXMLDataSourceSetting::convertString: could not convert \"")
235 += ::rtl::OString(_rReadCharacters
.getStr(), _rReadCharacters
.getLength(), RTL_TEXTENCODING_ASCII_US
)
236 += ::rtl::OString("\" into an integer!"));
237 if (TypeClass_SHORT
== _rExpectedType
.getTypeClass())
238 aReturn
<<= (sal_Int16
)nValue
;
240 aReturn
<<= (sal_Int32
)nValue
;
243 case TypeClass_HYPER
:
245 OSL_ENSURE(sal_False
, "OXMLDataSourceSetting::convertString: 64-bit integers not implemented yet!");
248 case TypeClass_DOUBLE
:
251 #if OSL_DEBUG_LEVEL > 0
254 rImporter
.GetMM100UnitConverter().convertDouble(nValue
, _rReadCharacters
);
256 ::rtl::OString("OXMLDataSourceSetting::convertString: could not convert \"")
257 += ::rtl::OString(_rReadCharacters
.getStr(), _rReadCharacters
.getLength(), RTL_TEXTENCODING_ASCII_US
)
258 += ::rtl::OString("\" into a double!"));
259 aReturn
<<= (double)nValue
;
262 case TypeClass_STRING
:
263 aReturn
<<= _rReadCharacters
;
266 OSL_ENSURE(sal_False
, "OXMLDataSourceSetting::convertString: invalid type class!");
272 //----------------------------------------------------------------------------
273 } // namespace dbaxml
274 // -----------------------------------------------------------------------------