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(false)
49 : {
50 :
51 0 : m_aPropType = cppu::UnoType<void>::get();
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)] = cppu::UnoType<bool>::get();
77 : // Not a copy paste error, see comment xmloff/source/forms/propertyimport.cxx lines 244-248
78 0 : s_aTypeNameMap[GetXMLToken( XML_FLOAT)] = ::cppu::UnoType<double>::get();
79 0 : s_aTypeNameMap[GetXMLToken( XML_DOUBLE)] = ::cppu::UnoType<double>::get();
80 0 : s_aTypeNameMap[GetXMLToken( XML_STRING)] = ::cppu::UnoType<OUString>::get();
81 0 : s_aTypeNameMap[GetXMLToken( XML_INT)] = ::cppu::UnoType<sal_Int32>::get();
82 0 : s_aTypeNameMap[GetXMLToken( XML_SHORT)] = ::cppu::UnoType<sal_Int16>::get();
83 0 : s_aTypeNameMap[GetXMLToken( XML_VOID)] = cppu::UnoType<void>::get();
84 : }
85 :
86 0 : 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 0 : if (s_aTypeNameMap.end() != aTypePos)
89 0 : m_aPropType = aTypePos->second;
90 : }
91 0 : break;
92 : case XML_TOK_DATA_SOURCE_SETTING_NAME:
93 0 : m_aSetting.Name = sValue;
94 0 : break;
95 : }
96 0 : }
97 :
98 0 : }
99 :
100 0 : OXMLDataSourceSetting::~OXMLDataSourceSetting()
101 : {
102 0 : }
103 :
104 0 : SvXMLImportContext* OXMLDataSourceSetting::CreateChildContext(
105 : sal_uInt16 nPrefix,
106 : const OUString& rLocalName,
107 : const Reference< XAttributeList > & xAttrList )
108 : {
109 0 : SvXMLImportContext *pContext = 0;
110 0 : const SvXMLTokenMap& rTokenMap = GetOwnImport().GetDataSourceInfoElemTokenMap();
111 :
112 0 : switch( rTokenMap.Get( nPrefix, rLocalName ) )
113 : {
114 : case XML_TOK_DATA_SOURCE_SETTING:
115 0 : GetOwnImport().GetProgressBarHelper()->Increment( PROGRESS_BAR_STEP );
116 0 : pContext = new OXMLDataSourceSetting( GetOwnImport(), nPrefix, rLocalName,xAttrList);
117 0 : break;
118 : case XML_TOK_DATA_SOURCE_SETTING_VALUE:
119 0 : GetOwnImport().GetProgressBarHelper()->Increment( PROGRESS_BAR_STEP );
120 0 : pContext = new OXMLDataSourceSetting( GetOwnImport(), nPrefix, rLocalName,xAttrList,this );
121 0 : break;
122 : }
123 :
124 0 : if( !pContext )
125 0 : pContext = new SvXMLImportContext( GetImport(), nPrefix, rLocalName );
126 :
127 0 : return pContext;
128 : }
129 :
130 0 : void OXMLDataSourceSetting::EndElement()
131 : {
132 0 : if ( !m_aSetting.Name.isEmpty() )
133 : {
134 0 : if ( m_bIsList && m_aInfoSequence.getLength() )
135 0 : m_aSetting.Value <<= m_aInfoSequence;
136 :
137 : // if our property is of type string, but was empty, ensure that
138 : // we don't add a VOID value
139 0 : if ( !m_bIsList && ( m_aPropType.getTypeClass() == TypeClass_STRING ) && !m_aSetting.Value.hasValue() )
140 0 : m_aSetting.Value <<= OUString();
141 :
142 0 : GetOwnImport().addInfo(m_aSetting);
143 : }
144 0 : }
145 :
146 0 : void OXMLDataSourceSetting::Characters( const OUString& rChars )
147 : {
148 0 : if ( m_pContainer )
149 0 : m_pContainer->addValue(rChars);
150 0 : }
151 :
152 0 : void OXMLDataSourceSetting::addValue(const OUString& _sValue)
153 : {
154 0 : Any aValue;
155 0 : if( TypeClass_VOID != m_aPropType.getTypeClass() )
156 0 : aValue = convertString(m_aPropType, _sValue);
157 :
158 0 : if ( !m_bIsList )
159 0 : m_aSetting.Value = aValue;
160 : else
161 : {
162 0 : sal_Int32 nPos = m_aInfoSequence.getLength();
163 0 : m_aInfoSequence.realloc(nPos+1);
164 0 : m_aInfoSequence[nPos] = aValue;
165 0 : }
166 0 : }
167 :
168 0 : ODBFilter& OXMLDataSourceSetting::GetOwnImport()
169 : {
170 0 : return static_cast<ODBFilter&>(GetImport());
171 : }
172 :
173 0 : Any OXMLDataSourceSetting::convertString(const ::com::sun::star::uno::Type& _rExpectedType, const OUString& _rReadCharacters)
174 : {
175 0 : Any aReturn;
176 0 : switch (_rExpectedType.getTypeClass())
177 : {
178 : case TypeClass_BOOLEAN: // sal_Bool
179 : {
180 0 : bool bValue(false);
181 : bool const bSuccess =
182 0 : ::sax::Converter::convertBool(bValue, _rReadCharacters);
183 : SAL_WARN_IF(!bSuccess, "dbaccess",
184 : "OXMLDataSourceSetting::convertString: could not convert \""
185 : << _rReadCharacters << "\" into a boolean!");
186 0 : aReturn <<= bValue;
187 : }
188 0 : break;
189 : case TypeClass_SHORT: // sal_Int16
190 : case TypeClass_LONG: // sal_Int32
191 : { // it's a real int32/16 property
192 0 : sal_Int32 nValue(0);
193 : bool const bSuccess =
194 0 : ::sax::Converter::convertNumber(nValue, _rReadCharacters);
195 : SAL_WARN_IF(!bSuccess, "dbaccess",
196 : "OXMLDataSourceSetting::convertString: could not convert \""
197 : << _rReadCharacters << "\" into an integer!");
198 0 : if (TypeClass_SHORT == _rExpectedType.getTypeClass())
199 0 : aReturn <<= (sal_Int16)nValue;
200 : else
201 0 : aReturn <<= (sal_Int32)nValue;
202 0 : break;
203 : }
204 : case TypeClass_HYPER:
205 : {
206 : OSL_FAIL("OXMLDataSourceSetting::convertString: 64-bit integers not implemented yet!");
207 : }
208 0 : break;
209 : case TypeClass_DOUBLE:
210 : {
211 0 : double nValue = 0.0;
212 : bool const bSuccess =
213 0 : ::sax::Converter::convertDouble(nValue, _rReadCharacters);
214 : SAL_WARN_IF(!bSuccess, "dbaccess",
215 : "OXMLDataSourceSetting::convertString: could not convert \""
216 : << _rReadCharacters << "\" into a double!");
217 0 : aReturn <<= (double)nValue;
218 : }
219 0 : break;
220 : case TypeClass_STRING:
221 0 : aReturn <<= _rReadCharacters;
222 0 : break;
223 : default:
224 : SAL_WARN("dbaccess",
225 : "OXMLDataSourceSetting::convertString: invalid type class!");
226 : }
227 :
228 0 : return aReturn;
229 : }
230 :
231 : } // namespace dbaxml
232 :
233 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|