Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / oox / source / drawingml / chart / datasourcecontext.cxx
blob7fa7b12852a752b28d3a1be5c2cade81bc43911d
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 <drawingml/chart/datasourcecontext.hxx>
22 #include <oox/drawingml/chart/datasourcemodel.hxx>
24 #include <comphelper/processfactory.hxx>
25 #include <oox/core/xmlfilterbase.hxx>
26 #include <oox/helper/attributelist.hxx>
27 #include <oox/token/namespaces.hxx>
28 #include <oox/token/tokens.hxx>
29 #include <svl/zforlist.hxx>
30 #include <osl/diagnose.h>
32 namespace oox {
33 namespace drawingml {
34 namespace chart {
36 using ::oox::core::ContextHandler2Helper;
37 using ::oox::core::ContextHandlerRef;
39 using namespace ::com::sun::star;
41 DoubleSequenceContext::DoubleSequenceContext( ContextHandler2Helper& rParent, DataSequenceModel& rModel ) :
42 DataSequenceContextBase( rParent, rModel ),
43 mnPtIndex( -1 )
47 DoubleSequenceContext::~DoubleSequenceContext()
51 ContextHandlerRef DoubleSequenceContext::onCreateContext( sal_Int32 nElement, const AttributeList& rAttribs )
53 switch( getCurrentElement() )
55 case C_TOKEN( numRef ):
56 switch( nElement )
58 case C_TOKEN( f ):
59 case C_TOKEN( numCache ):
60 return this;
62 break;
64 case C_TOKEN( numCache ):
65 case C_TOKEN( numLit ):
66 switch( nElement )
68 case C_TOKEN( formatCode ):
69 return this;
70 case C_TOKEN( ptCount ):
71 mrModel.mnPointCount = rAttribs.getInteger( XML_val, -1 );
72 return nullptr;
73 case C_TOKEN( pt ):
74 mnPtIndex = rAttribs.getInteger( XML_idx, -1 );
75 return this;
77 break;
79 case C_TOKEN( pt ):
80 switch( nElement )
82 case C_TOKEN( v ):
83 return this;
85 break;
87 return nullptr;
90 void DoubleSequenceContext::onCharacters( const OUString& rChars )
92 switch( getCurrentElement() )
94 case C_TOKEN( f ):
95 mrModel.maFormula = rChars;
96 break;
97 case C_TOKEN( formatCode ):
98 mrModel.maFormatCode = rChars;
99 break;
100 case C_TOKEN( v ):
101 if( mnPtIndex >= 0 )
103 /* Import categories as String even though it could
104 * be values.
105 * n#810508: xVal needs to be imported as double
106 * TODO: NumberFormat conversion, remove the check then.
108 if( isParentElement( C_TOKEN( cat ), 4 ) )
110 // workaround for bug n#889755
111 SvNumberFormatter* pNumFrmt = getNumberFormatter();
112 if( pNumFrmt )
114 sal_uInt32 nKey = pNumFrmt->GetEntryKey( mrModel.maFormatCode );
115 bool bNoKey = ( nKey == NUMBERFORMAT_ENTRY_NOT_FOUND );
116 if( bNoKey )
118 OUString aFormatCode = mrModel.maFormatCode;
119 sal_Int32 nCheckPos = 0;
120 SvNumFormatType nType;
121 pNumFrmt->PutEntry( aFormatCode, nCheckPos, nType, nKey );
122 bNoKey = (nCheckPos != 0);
124 if( bNoKey )
126 mrModel.maData[ mnPtIndex ] <<= rChars;
128 else
130 double fValue = rChars.toDouble();
131 ::Color* pColor = nullptr;
132 OUString aFormattedValue;
133 pNumFrmt->GetOutputString( fValue, nKey, aFormattedValue, &pColor );
134 mrModel.maData[ mnPtIndex ] <<= aFormattedValue;
137 else
139 mrModel.maData[ mnPtIndex ] <<= rChars;
142 else
144 mrModel.maData[ mnPtIndex ] <<= rChars.toDouble();
147 break;
152 SvNumberFormatter* DoubleSequenceContext::getNumberFormatter()
154 if( mpNumberFormatter == nullptr )
156 uno::Reference<uno::XComponentContext> rContext =
157 getFilter().getComponentContext();
158 mpNumberFormatter.reset(
159 new SvNumberFormatter(rContext, LANGUAGE_DONTKNOW) );
161 return mpNumberFormatter.get();
165 StringSequenceContext::StringSequenceContext( ContextHandler2Helper& rParent, DataSequenceModel& rModel )
166 : DataSequenceContextBase( rParent, rModel )
167 , mnPtIndex(-1)
171 StringSequenceContext::~StringSequenceContext()
175 ContextHandlerRef StringSequenceContext::onCreateContext( sal_Int32 nElement, const AttributeList& rAttribs )
177 switch( getCurrentElement() )
179 case C_TOKEN( multiLvlStrRef ):
180 switch( nElement )
182 case C_TOKEN( f ):
183 case C_TOKEN( multiLvlStrCache ):
184 return this;
186 break;
188 case C_TOKEN( strRef ):
189 switch( nElement )
191 case C_TOKEN( f ):
192 case C_TOKEN( strCache ):
193 return this;
195 break;
197 case C_TOKEN( strCache ):
198 case C_TOKEN( strLit ):
199 switch( nElement )
201 case C_TOKEN( ptCount ):
202 mrModel.mnPointCount = rAttribs.getInteger( XML_val, -1 );
203 return nullptr;
204 case C_TOKEN( pt ):
205 mnPtIndex = rAttribs.getInteger( XML_idx, -1 );
206 return this;
208 break;
210 case C_TOKEN( multiLvlStrCache ):
211 switch (nElement)
213 case C_TOKEN( ptCount ):
214 mrModel.mnPointCount = rAttribs.getInteger(XML_val, -1);
215 mrModel.mnLevelCount--; // normalize level count
216 return nullptr;
217 case C_TOKEN( lvl ):
218 mrModel.mnLevelCount++;
219 return this;
221 break;
223 case C_TOKEN( lvl ):
224 switch (nElement)
226 case C_TOKEN(pt):
227 mnPtIndex = rAttribs.getInteger(XML_idx, -1);
228 return this;
230 break;
232 case C_TOKEN( pt ):
233 switch( nElement )
235 case C_TOKEN( v ):
236 return this;
238 break;
240 return nullptr;
243 void StringSequenceContext::onCharacters( const OUString& rChars )
245 switch( getCurrentElement() )
247 case C_TOKEN( f ):
248 mrModel.maFormula = rChars;
249 break;
250 case C_TOKEN( v ):
251 if( mnPtIndex >= 0 )
252 mrModel.maData[ (mrModel.mnLevelCount-1) * mrModel.mnPointCount + mnPtIndex ] <<= rChars;
253 break;
257 DataSourceContext::DataSourceContext( ContextHandler2Helper& rParent, DataSourceModel& rModel ) :
258 ContextBase< DataSourceModel >( rParent, rModel )
262 DataSourceContext::~DataSourceContext()
266 ContextHandlerRef DataSourceContext::onCreateContext( sal_Int32 nElement, const AttributeList& )
268 switch( getCurrentElement() )
270 case C_TOKEN( cat ):
271 case C_TOKEN( xVal ):
272 switch( nElement )
274 case C_TOKEN( multiLvlStrRef ):
275 case C_TOKEN( strLit ):
276 case C_TOKEN( strRef ):
277 OSL_ENSURE( !mrModel.mxDataSeq, "DataSourceContext::onCreateContext - multiple data sequences" );
278 return new StringSequenceContext( *this, mrModel.mxDataSeq.create() );
280 case C_TOKEN( numLit ):
281 case C_TOKEN( numRef ):
282 OSL_ENSURE( !mrModel.mxDataSeq, "DataSourceContext::onCreateContext - multiple data sequences" );
283 return new DoubleSequenceContext( *this, mrModel.mxDataSeq.create() );
285 break;
287 case C_TOKEN( plus ):
288 case C_TOKEN( minus ):
289 case C_TOKEN( val ):
290 case C_TOKEN( yVal ):
291 case C_TOKEN( bubbleSize ):
292 switch( nElement )
294 case C_TOKEN( numLit ):
295 case C_TOKEN( numRef ):
296 OSL_ENSURE( !mrModel.mxDataSeq, "DataSourceContext::onCreateContext - multiple data sequences" );
297 return new DoubleSequenceContext( *this, mrModel.mxDataSeq.create() );
299 break;
301 return nullptr;
304 } // namespace chart
305 } // namespace drawingml
306 } // namespace oox
308 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */