Line data Source code
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 "sal/config.h"
21 :
22 : #include <map>
23 :
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>
35 :
36 : namespace dbaxml
37 : {
38 : using namespace ::com::sun::star::uno;
39 : using namespace ::com::sun::star::xml::sax;
40 :
41 0 : OXMLDataSourceSetting::OXMLDataSourceSetting( ODBFilter& rImport
42 : ,sal_uInt16 nPrfx
43 : ,const OUString& _sLocalName
44 : ,const Reference< XAttributeList > & _xAttrList
45 : ,OXMLDataSourceSetting* _pContainer) :
46 : SvXMLImportContext( rImport, nPrfx, _sLocalName )
47 : ,m_pContainer(_pContainer)
48 0 : ,m_bIsList(sal_False)
49 : {
50 :
51 0 : m_aPropType = ::getVoidCppuType();
52 :
53 : OSL_ENSURE(_xAttrList.is(),"Attribute list is NULL!");
54 0 : const SvXMLNamespaceMap& rMap = rImport.GetNamespaceMap();
55 0 : const SvXMLTokenMap& rTokenMap = rImport.GetDataSourceInfoElemTokenMap();
56 :
57 0 : sal_Int16 nLength = (_xAttrList.is()) ? _xAttrList->getLength() : 0;
58 0 : for(sal_Int16 i = 0; i < nLength; ++i)
59 : {
60 0 : OUString sLocalName;
61 0 : OUString sAttrName = _xAttrList->getNameByIndex( i );
62 0 : sal_uInt16 nPrefix = rMap.GetKeyByAttrName( sAttrName,&sLocalName );
63 0 : OUString sValue = _xAttrList->getValueByIndex( i );
64 :
65 0 : switch( rTokenMap.Get( nPrefix, sLocalName ) )
66 : {
67 : case XML_TOK_DATA_SOURCE_SETTING_IS_LIST:
68 0 : m_bIsList = sValue == "true";
69 0 : break;
70 : case XML_TOK_DATA_SOURCE_SETTING_TYPE:
71 : {
72 : // needs to be translated into a ::com::sun::star::uno::Type
73 0 : static std::map< OUString, css::uno::Type > s_aTypeNameMap;
74 0 : if (s_aTypeNameMap.empty())
75 : {
76 0 : s_aTypeNameMap[GetXMLToken( XML_BOOLEAN)] = ::getBooleanCppuType();
77 0 : s_aTypeNameMap[GetXMLToken( XML_FLOAT)] = ::getCppuType( static_cast< double* >(NULL) );
78 0 : s_aTypeNameMap[GetXMLToken( XML_DOUBLE)] = ::getCppuType( static_cast< double* >(NULL) );
79 0 : s_aTypeNameMap[GetXMLToken( XML_STRING)] = ::getCppuType( static_cast< OUString* >(NULL) );
80 0 : s_aTypeNameMap[GetXMLToken( XML_INT)] = ::getCppuType( static_cast< sal_Int32* >(NULL) );
81 0 : s_aTypeNameMap[GetXMLToken( XML_SHORT)] = ::getCppuType( static_cast< sal_Int16* >(NULL) );
82 0 : s_aTypeNameMap[GetXMLToken( XML_VOID)] = ::getVoidCppuType();
83 : }
84 :
85 0 : const std::map< OUString, css::uno::Type >::const_iterator aTypePos = s_aTypeNameMap.find(sValue);
86 : OSL_ENSURE(s_aTypeNameMap.end() != aTypePos, "OXMLDataSourceSetting::OXMLDataSourceSetting: invalid type!");
87 0 : if (s_aTypeNameMap.end() != aTypePos)
88 0 : m_aPropType = aTypePos->second;
89 : }
90 0 : break;
91 : case XML_TOK_DATA_SOURCE_SETTING_NAME:
92 0 : m_aSetting.Name = sValue;
93 0 : break;
94 : }
95 0 : }
96 :
97 0 : }
98 :
99 0 : OXMLDataSourceSetting::~OXMLDataSourceSetting()
100 : {
101 0 : }
102 :
103 0 : SvXMLImportContext* OXMLDataSourceSetting::CreateChildContext(
104 : sal_uInt16 nPrefix,
105 : const OUString& rLocalName,
106 : const Reference< XAttributeList > & xAttrList )
107 : {
108 0 : SvXMLImportContext *pContext = 0;
109 0 : const SvXMLTokenMap& rTokenMap = GetOwnImport().GetDataSourceInfoElemTokenMap();
110 :
111 0 : switch( rTokenMap.Get( nPrefix, rLocalName ) )
112 : {
113 : case XML_TOK_DATA_SOURCE_SETTING:
114 0 : GetOwnImport().GetProgressBarHelper()->Increment( PROGRESS_BAR_STEP );
115 0 : pContext = new OXMLDataSourceSetting( GetOwnImport(), nPrefix, rLocalName,xAttrList);
116 0 : break;
117 : case XML_TOK_DATA_SOURCE_SETTING_VALUE:
118 0 : GetOwnImport().GetProgressBarHelper()->Increment( PROGRESS_BAR_STEP );
119 0 : pContext = new OXMLDataSourceSetting( GetOwnImport(), nPrefix, rLocalName,xAttrList,this );
120 0 : break;
121 : }
122 :
123 0 : if( !pContext )
124 0 : pContext = new SvXMLImportContext( GetImport(), nPrefix, rLocalName );
125 :
126 0 : return pContext;
127 : }
128 :
129 0 : void OXMLDataSourceSetting::EndElement()
130 : {
131 0 : if ( !m_aSetting.Name.isEmpty() )
132 : {
133 0 : if ( m_bIsList && m_aInfoSequence.getLength() )
134 0 : m_aSetting.Value <<= m_aInfoSequence;
135 :
136 : // if our property is of type string, but was empty, ensure that
137 : // we don't add a VOID value
138 0 : if ( !m_bIsList && ( m_aPropType.getTypeClass() == TypeClass_STRING ) && !m_aSetting.Value.hasValue() )
139 0 : m_aSetting.Value <<= OUString();
140 :
141 0 : GetOwnImport().addInfo(m_aSetting);
142 : }
143 0 : }
144 :
145 0 : void OXMLDataSourceSetting::Characters( const OUString& rChars )
146 : {
147 0 : if ( m_pContainer )
148 0 : m_pContainer->addValue(rChars);
149 0 : }
150 :
151 0 : void OXMLDataSourceSetting::addValue(const OUString& _sValue)
152 : {
153 0 : Any aValue;
154 0 : if( TypeClass_VOID != m_aPropType.getTypeClass() )
155 0 : aValue = convertString(m_aPropType, _sValue);
156 :
157 0 : if ( !m_bIsList )
158 0 : m_aSetting.Value = aValue;
159 : else
160 : {
161 0 : sal_Int32 nPos = m_aInfoSequence.getLength();
162 0 : m_aInfoSequence.realloc(nPos+1);
163 0 : m_aInfoSequence[nPos] = aValue;
164 0 : }
165 0 : }
166 :
167 0 : ODBFilter& OXMLDataSourceSetting::GetOwnImport()
168 : {
169 0 : return static_cast<ODBFilter&>(GetImport());
170 : }
171 :
172 0 : Any OXMLDataSourceSetting::convertString(const ::com::sun::star::uno::Type& _rExpectedType, const OUString& _rReadCharacters)
173 : {
174 0 : Any aReturn;
175 0 : switch (_rExpectedType.getTypeClass())
176 : {
177 : case TypeClass_BOOLEAN: // sal_Bool
178 : {
179 0 : bool bValue(false);
180 : bool const bSuccess =
181 0 : ::sax::Converter::convertBool(bValue, _rReadCharacters);
182 : SAL_WARN_IF(!bSuccess, "dbaccess",
183 : "OXMLDataSourceSetting::convertString: could not convert \""
184 : << _rReadCharacters << "\" into a boolean!");
185 0 : aReturn <<= bValue;
186 : }
187 0 : break;
188 : case TypeClass_SHORT: // sal_Int16
189 : case TypeClass_LONG: // sal_Int32
190 : { // it's a real int32/16 property
191 0 : sal_Int32 nValue(0);
192 : bool const bSuccess =
193 0 : ::sax::Converter::convertNumber(nValue, _rReadCharacters);
194 : SAL_WARN_IF(!bSuccess, "dbaccess",
195 : "OXMLDataSourceSetting::convertString: could not convert \""
196 : << _rReadCharacters << "\" into an integer!");
197 0 : if (TypeClass_SHORT == _rExpectedType.getTypeClass())
198 0 : aReturn <<= (sal_Int16)nValue;
199 : else
200 0 : aReturn <<= (sal_Int32)nValue;
201 0 : break;
202 : }
203 : case TypeClass_HYPER:
204 : {
205 : OSL_FAIL("OXMLDataSourceSetting::convertString: 64-bit integers not implemented yet!");
206 : }
207 0 : break;
208 : case TypeClass_DOUBLE:
209 : {
210 0 : double nValue = 0.0;
211 : bool const bSuccess =
212 0 : ::sax::Converter::convertDouble(nValue, _rReadCharacters);
213 : SAL_WARN_IF(!bSuccess, "dbaccess",
214 : "OXMLDataSourceSetting::convertString: could not convert \""
215 : << _rReadCharacters << "\" into a double!");
216 0 : aReturn <<= (double)nValue;
217 : }
218 0 : break;
219 : case TypeClass_STRING:
220 0 : aReturn <<= _rReadCharacters;
221 0 : break;
222 : default:
223 : SAL_WARN("dbaccess",
224 : "OXMLDataSourceSetting::convertString: invalid type class!");
225 : }
226 :
227 0 : return aReturn;
228 : }
229 :
230 : } // namespace dbaxml
231 :
232 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|