1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_dbaxml.hxx"
26 #ifndef DBA_XMLDATASOURCESETTING_HXX
27 #include "xmlDataSourceSetting.hxx"
28 #endif
29 #ifndef DBA_XMLDATASOURCE_HXX
30 #include "xmlDataSource.hxx"
31 #endif
32 #ifndef _XMLOFF_XMLUCONV_HXX
33 #include <xmloff/xmluconv.hxx>
34 #endif
35 #ifndef DBA_XMLFILTER_HXX
36 #include "xmlfilter.hxx"
37 #endif
38 #ifndef _XMLOFF_XMLTOKEN_HXX
39 #include <xmloff/xmltoken.hxx>
40 #endif
41 #ifndef _XMLOFF_XMLNMSPE_HXX
42 #include <xmloff/xmlnmspe.hxx>
43 #endif
44 #ifndef _XMLOFF_NMSPMAP_HXX
45 #include <xmloff/nmspmap.hxx>
46 #endif
47 #ifndef DBA_XMLENUMS_HXX
48 #include "xmlEnums.hxx"
49 #endif
50 #ifndef DBACCESS_SHARED_XMLSTRINGS_HRC
51 #include "xmlstrings.hrc"
52 #endif
53 #ifndef _TOOLS_DEBUG_HXX
54 #include <tools/debug.hxx>
55 #endif
56 
57 namespace dbaxml
58 {
59 	using namespace ::com::sun::star::uno;
60 	using namespace ::com::sun::star::xml::sax;
DBG_NAME(OXMLDataSourceSetting)61 DBG_NAME(OXMLDataSourceSetting)
62 
63 OXMLDataSourceSetting::OXMLDataSourceSetting( ODBFilter& rImport
64 				,sal_uInt16 nPrfx
65 				,const ::rtl::OUString& _sLocalName
66 				,const Reference< XAttributeList > & _xAttrList
67 				,OXMLDataSourceSetting* _pContainer) :
68 	SvXMLImportContext( rImport, nPrfx, _sLocalName )
69 	,m_pContainer(_pContainer)
70 	,m_bIsList(sal_False)
71 {
72     DBG_CTOR(OXMLDataSourceSetting,NULL);
73 
74 	m_aPropType = ::getVoidCppuType();
75 
76 	OSL_ENSURE(_xAttrList.is(),"Attribute list is NULL!");
77 	const SvXMLNamespaceMap& rMap = rImport.GetNamespaceMap();
78 	const SvXMLTokenMap& rTokenMap = rImport.GetDataSourceInfoElemTokenMap();
79 
80 	sal_Int16 nLength = (_xAttrList.is()) ? _xAttrList->getLength() : 0;
81 	for(sal_Int16 i = 0; i < nLength; ++i)
82 	{
83 		::rtl::OUString sLocalName;
84 		rtl::OUString sAttrName = _xAttrList->getNameByIndex( i );
85 		sal_uInt16 nPrefix = rMap.GetKeyByAttrName( sAttrName,&sLocalName );
86 		rtl::OUString sValue = _xAttrList->getValueByIndex( i );
87 
88 		switch( rTokenMap.Get( nPrefix, sLocalName ) )
89 		{
90 			case XML_TOK_DATA_SOURCE_SETTING_IS_LIST:
91 				m_bIsList = sValue.equalsAscii("true");
92 				break;
93 			case XML_TOK_DATA_SOURCE_SETTING_TYPE:
94 				{
95 					// needs to be translated into a ::com::sun::star::uno::Type
96 					DECLARE_STL_USTRINGACCESS_MAP( ::com::sun::star::uno::Type, MapString2Type );
97 					static MapString2Type s_aTypeNameMap;
98 					if (!s_aTypeNameMap.size())
99 					{
100 						s_aTypeNameMap[GetXMLToken( XML_BOOLEAN)]	= ::getBooleanCppuType();
101 						s_aTypeNameMap[GetXMLToken( XML_FLOAT)]		= ::getCppuType( static_cast< double* >(NULL) );
102                         s_aTypeNameMap[GetXMLToken( XML_DOUBLE)]	= ::getCppuType( static_cast< double* >(NULL) );
103 						s_aTypeNameMap[GetXMLToken( XML_STRING)]	= ::getCppuType( static_cast< ::rtl::OUString* >(NULL) );
104 						s_aTypeNameMap[GetXMLToken( XML_INT)]		= ::getCppuType( static_cast< sal_Int32* >(NULL) );
105 						s_aTypeNameMap[GetXMLToken( XML_SHORT)]		= ::getCppuType( static_cast< sal_Int16* >(NULL) );
106 						s_aTypeNameMap[GetXMLToken( XML_VOID)]		= ::getVoidCppuType();
107 					}
108 
109 					const ConstMapString2TypeIterator aTypePos = s_aTypeNameMap.find(sValue);
110 					OSL_ENSURE(s_aTypeNameMap.end() != aTypePos, "OXMLDataSourceSetting::OXMLDataSourceSetting: invalid type!");
111 					if (s_aTypeNameMap.end() != aTypePos)
112 						m_aPropType = aTypePos->second;
113 				}
114 				break;
115 			case XML_TOK_DATA_SOURCE_SETTING_NAME:
116 				m_aSetting.Name = sValue;
117 				break;
118 		}
119 	}
120 
121 }
122 // -----------------------------------------------------------------------------
123 
~OXMLDataSourceSetting()124 OXMLDataSourceSetting::~OXMLDataSourceSetting()
125 {
126     DBG_DTOR(OXMLDataSourceSetting,NULL);
127 }
128 // -----------------------------------------------------------------------------
CreateChildContext(sal_uInt16 nPrefix,const::rtl::OUString & rLocalName,const Reference<XAttributeList> & xAttrList)129 SvXMLImportContext* OXMLDataSourceSetting::CreateChildContext(
130 		sal_uInt16 nPrefix,
131 		const ::rtl::OUString& rLocalName,
132 		const Reference< XAttributeList > & xAttrList )
133 {
134 	SvXMLImportContext *pContext = 0;
135 	const SvXMLTokenMap&	rTokenMap	= GetOwnImport().GetDataSourceInfoElemTokenMap();
136 
137 	switch( rTokenMap.Get( nPrefix, rLocalName ) )
138 	{
139 		case XML_TOK_DATA_SOURCE_SETTING:
140 			GetOwnImport().GetProgressBarHelper()->Increment( PROGRESS_BAR_STEP );
141 			pContext = new OXMLDataSourceSetting( GetOwnImport(), nPrefix, rLocalName,xAttrList);
142 			break;
143 		case XML_TOK_DATA_SOURCE_SETTING_VALUE:
144 			GetOwnImport().GetProgressBarHelper()->Increment( PROGRESS_BAR_STEP );
145 			pContext = new OXMLDataSourceSetting( GetOwnImport(), nPrefix, rLocalName,xAttrList,this );
146 			break;
147 	}
148 
149 	if( !pContext )
150 		pContext = new SvXMLImportContext( GetImport(), nPrefix, rLocalName );
151 
152 	return pContext;
153 }
154 // -----------------------------------------------------------------------------
EndElement()155 void OXMLDataSourceSetting::EndElement()
156 {
157 	if ( m_aSetting.Name.getLength() )
158 	{
159 		if ( m_bIsList && m_aInfoSequence.getLength() )
160 			m_aSetting.Value <<= m_aInfoSequence;
161 
162         // if our property is of type string, but was empty, ensure that
163         // we don't add a VOID value
164         if ( !m_bIsList && ( m_aPropType.getTypeClass() == TypeClass_STRING ) && !m_aSetting.Value.hasValue() )
165             m_aSetting.Value <<= ::rtl::OUString();
166 
167 		GetOwnImport().addInfo(m_aSetting);
168 	}
169 }
170 // -----------------------------------------------------------------------------
Characters(const::rtl::OUString & rChars)171 void OXMLDataSourceSetting::Characters( const ::rtl::OUString& rChars )
172 {
173 	if ( m_pContainer )
174 		m_pContainer->addValue(rChars);
175 }
176 // -----------------------------------------------------------------------------
addValue(const::rtl::OUString & _sValue)177 void OXMLDataSourceSetting::addValue(const ::rtl::OUString& _sValue)
178 {
179 	Any aValue;
180 	if( TypeClass_VOID != m_aPropType.getTypeClass() )
181 		aValue = convertString(m_aPropType, _sValue);
182 
183 	if ( !m_bIsList )
184 		m_aSetting.Value = aValue;
185 	else
186 	{
187 		sal_Int32 nPos = m_aInfoSequence.getLength();
188 		m_aInfoSequence.realloc(nPos+1);
189 		m_aInfoSequence[nPos] = aValue;
190 	}
191 }
192 // -----------------------------------------------------------------------------
GetOwnImport()193 ODBFilter& OXMLDataSourceSetting::GetOwnImport()
194 {
195 	return static_cast<ODBFilter&>(GetImport());
196 }
197 // -----------------------------------------------------------------------------
convertString(const::com::sun::star::uno::Type & _rExpectedType,const::rtl::OUString & _rReadCharacters)198 Any OXMLDataSourceSetting::convertString(const ::com::sun::star::uno::Type& _rExpectedType, const ::rtl::OUString& _rReadCharacters)
199 {
200 	ODBFilter& rImporter = GetOwnImport();
201 	Any aReturn;
202 	switch (_rExpectedType.getTypeClass())
203 	{
204 		case TypeClass_BOOLEAN:		// sal_Bool
205 		{
206 			sal_Bool bValue;
207         #if OSL_DEBUG_LEVEL > 0
208 			sal_Bool bSuccess =
209 		#endif
210 			rImporter.GetMM100UnitConverter().convertBool(bValue, _rReadCharacters);
211 			OSL_ENSURE(bSuccess,
212 					::rtl::OString("OXMLDataSourceSetting::convertString: could not convert \"")
213 				+=	::rtl::OString(_rReadCharacters.getStr(), _rReadCharacters.getLength(), RTL_TEXTENCODING_ASCII_US)
214 				+=	::rtl::OString("\" into a boolean!"));
215 			aReturn <<= bValue;
216 		}
217 		break;
218 		case TypeClass_SHORT:		// sal_Int16
219 		case TypeClass_LONG:		// sal_Int32
220 			{	// it's a real int32/16 property
221 				sal_Int32 nValue(0);
222         #if OSL_DEBUG_LEVEL > 0
223 				sal_Bool bSuccess =
224 		#endif
225 				rImporter.GetMM100UnitConverter().convertNumber(nValue, _rReadCharacters);
226 				OSL_ENSURE(bSuccess,
227 						::rtl::OString("OXMLDataSourceSetting::convertString: could not convert \"")
228 					+=	::rtl::OString(_rReadCharacters.getStr(), _rReadCharacters.getLength(), RTL_TEXTENCODING_ASCII_US)
229 					+=	::rtl::OString("\" into an integer!"));
230 				if (TypeClass_SHORT == _rExpectedType.getTypeClass())
231 					aReturn <<= (sal_Int16)nValue;
232 				else
233 					aReturn <<= (sal_Int32)nValue;
234 				break;
235 			}
236 		case TypeClass_HYPER:
237 		{
238 			OSL_ENSURE(sal_False, "OXMLDataSourceSetting::convertString: 64-bit integers not implemented yet!");
239 		}
240 		break;
241 		case TypeClass_DOUBLE:
242 		{
243 			double nValue = 0.0;
244         #if OSL_DEBUG_LEVEL > 0
245 			sal_Bool bSuccess =
246 		#endif
247 			rImporter.GetMM100UnitConverter().convertDouble(nValue, _rReadCharacters);
248 			OSL_ENSURE(bSuccess,
249 					::rtl::OString("OXMLDataSourceSetting::convertString: could not convert \"")
250 				+=	::rtl::OString(_rReadCharacters.getStr(), _rReadCharacters.getLength(), RTL_TEXTENCODING_ASCII_US)
251 				+=	::rtl::OString("\" into a double!"));
252 			aReturn <<= (double)nValue;
253 		}
254 		break;
255 		case TypeClass_STRING:
256 			aReturn <<= _rReadCharacters;
257 			break;
258 		default:
259 			OSL_ENSURE(sal_False, "OXMLDataSourceSetting::convertString: invalid type class!");
260 	}
261 
262 	return aReturn;
263 }
264 
265 //----------------------------------------------------------------------------
266 } // namespace dbaxml
267 // -----------------------------------------------------------------------------
268