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_sc.hxx"
26 
27 
28 
29 //___________________________________________________________________
30 #include "XMLConsolidationContext.hxx"
31 #include "document.hxx"
32 #include "rangeutl.hxx"
33 #include "xmlimprt.hxx"
34 #include "XMLConverter.hxx"
35 #include <xmloff/nmspmap.hxx>
36 #include <xmloff/xmltoken.hxx>
37 
38 using ::rtl::OUString;
39 using namespace ::com::sun::star;
40 using namespace xmloff::token;
41 
42 
43 //___________________________________________________________________
44 
ScXMLConsolidationContext(ScXMLImport & rImport,sal_uInt16 nPrfx,const OUString & rLName,const uno::Reference<xml::sax::XAttributeList> & xAttrList)45 ScXMLConsolidationContext::ScXMLConsolidationContext(
46 		ScXMLImport& rImport,
47 		sal_uInt16 nPrfx,
48 		const OUString& rLName,
49 		const uno::Reference< xml::sax::XAttributeList >& xAttrList ) :
50 	SvXMLImportContext( rImport, nPrfx, rLName ),
51 	eFunction( SUBTOTAL_FUNC_NONE ),
52 	bLinkToSource( sal_False ),
53 	bTargetAddr(sal_False)
54 {
55 	rImport.LockSolarMutex();
56 	if( !xAttrList.is() ) return;
57 
58 	sal_Int16				nAttrCount		= xAttrList->getLength();
59 	const SvXMLTokenMap&	rAttrTokenMap	= GetScImport().GetConsolidationAttrTokenMap();
60 
61 	for( sal_Int16 nIndex = 0; nIndex < nAttrCount; ++nIndex )
62 	{
63 		const rtl::OUString& sAttrName	(xAttrList->getNameByIndex( nIndex ));
64 		const rtl::OUString& sValue		(xAttrList->getValueByIndex( nIndex ));
65 		OUString aLocalName;
66 		sal_uInt16 nPrefix		= GetScImport().GetNamespaceMap().GetKeyByAttrName( sAttrName, &aLocalName );
67 
68 		switch( rAttrTokenMap.Get( nPrefix, aLocalName ) )
69 		{
70 			case XML_TOK_CONSOLIDATION_ATTR_FUNCTION:
71 				eFunction = ScXMLConverter::GetSubTotalFuncFromString( sValue );
72 			break;
73 			case XML_TOK_CONSOLIDATION_ATTR_SOURCE_RANGES:
74 				sSourceList = sValue;
75 			break;
76 			case XML_TOK_CONSOLIDATION_ATTR_TARGET_ADDRESS:
77 				{
78 					sal_Int32 nOffset(0);
79 					bTargetAddr = ScRangeStringConverter::GetAddressFromString(
80 						aTargetAddr, sValue, GetScImport().GetDocument(), ::formula::FormulaGrammar::CONV_OOO, nOffset );
81 				}
82 				break;
83 			case XML_TOK_CONSOLIDATION_ATTR_USE_LABEL:
84 				sUseLabel = sValue;
85 			break;
86 			case XML_TOK_CONSOLIDATION_ATTR_LINK_TO_SOURCE:
87 				bLinkToSource = IsXMLToken(sValue, XML_TRUE);
88 			break;
89 		}
90 	}
91 }
92 
~ScXMLConsolidationContext()93 ScXMLConsolidationContext::~ScXMLConsolidationContext()
94 {
95 }
96 
CreateChildContext(sal_uInt16 nPrefix,const OUString & rLName,const uno::Reference<xml::sax::XAttributeList> &)97 SvXMLImportContext *ScXMLConsolidationContext::CreateChildContext(
98 		sal_uInt16 nPrefix,
99 		const OUString& rLName,
100         const uno::Reference< xml::sax::XAttributeList>& /* xAttrList */ )
101 {
102 	return new SvXMLImportContext( GetImport(), nPrefix, rLName );
103 }
104 
EndElement()105 void ScXMLConsolidationContext::EndElement()
106 {
107 	if (bTargetAddr)
108 	{
109 		ScConsolidateParam aConsParam;
110 		aConsParam.nCol = aTargetAddr.Col();
111 		aConsParam.nRow = aTargetAddr.Row();
112 		aConsParam.nTab = aTargetAddr.Tab();
113 		aConsParam.eFunction = eFunction;
114 
115 		sal_Bool bError = sal_False;
116 		sal_uInt16 nCount = (sal_uInt16) Min( ScRangeStringConverter::GetTokenCount( sSourceList ), (sal_Int32)0xFFFF );
117 		ScArea** ppAreas = nCount ? new ScArea*[ nCount ] : NULL;
118 		if( ppAreas )
119 		{
120 			sal_Int32 nOffset = 0;
121 			sal_uInt16 nIndex;
122 			for( nIndex = 0; nIndex < nCount; ++nIndex )
123 			{
124 				ppAreas[ nIndex ] = new ScArea;
125 				if ( !ScRangeStringConverter::GetAreaFromString(
126 					*ppAreas[ nIndex ], sSourceList, GetScImport().GetDocument(), ::formula::FormulaGrammar::CONV_OOO, nOffset ) )
127 				{
128 					bError = sal_True;		//! handle error
129 				}
130 			}
131 
132 			aConsParam.SetAreas( ppAreas, nCount );
133 
134 			// array is copied in SetAreas
135 			for( nIndex = 0; nIndex < nCount; ++nIndex )
136 				delete ppAreas[nIndex];
137 			delete[] ppAreas;
138 		}
139 
140 		aConsParam.bByCol = aConsParam.bByRow = sal_False;
141 		if( IsXMLToken(sUseLabel, XML_COLUMN ) )
142 			aConsParam.bByCol = sal_True;
143 		else if( IsXMLToken( sUseLabel, XML_ROW ) )
144 			aConsParam.bByRow = sal_True;
145 		else if( IsXMLToken( sUseLabel, XML_BOTH ) )
146 			aConsParam.bByCol = aConsParam.bByRow = sal_True;
147 
148 		aConsParam.bReferenceData = bLinkToSource;
149 
150 		ScDocument* pDoc = GetScImport().GetDocument();
151 		if( pDoc )
152 			pDoc->SetConsolidateDlgData( &aConsParam );
153 	}
154 	GetScImport().UnlockSolarMutex();
155 }
156 
157