xref: /aoo4110/main/sc/source/filter/xml/xmlrowi.cxx (revision b1cdbd2c)
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 // INCLUDE ---------------------------------------------------------------
30 
31 #include "xmlrowi.hxx"
32 #include "xmlimprt.hxx"
33 #include "xmlcelli.hxx"
34 #include "global.hxx"
35 #include "xmlstyli.hxx"
36 #include "document.hxx"
37 #include "docuno.hxx"
38 #include "olinetab.hxx"
39 #include "sheetdata.hxx"
40 
41 #include <xmloff/xmltkmap.hxx>
42 #include <xmloff/nmspmap.hxx>
43 #include <xmloff/xmlnmspe.hxx>
44 #include <xmloff/families.hxx>
45 #include <xmloff/xmltoken.hxx>
46 #include <com/sun/star/sheet/XSpreadsheetDocument.hpp>
47 #include <com/sun/star/sheet/XSpreadsheet.hpp>
48 #include <com/sun/star/table/XColumnRowRange.hpp>
49 #include <com/sun/star/sheet/XPrintAreas.hpp>
50 
51 #include <com/sun/star/table/CellAddress.hpp>
52 
53 #define SC_ISVISIBLE "IsVisible"
54 #define SC_OPTIMALHEIGHT "OptimalHeight"
55 #define SC_ISFILTERED "IsFiltered"
56 
57 using namespace com::sun::star;
58 using namespace xmloff::token;
59 
60 //------------------------------------------------------------------
61 
ScXMLTableRowContext(ScXMLImport & rImport,sal_uInt16 nPrfx,const::rtl::OUString & rLName,const::com::sun::star::uno::Reference<::com::sun::star::xml::sax::XAttributeList> & xAttrList)62 ScXMLTableRowContext::ScXMLTableRowContext( ScXMLImport& rImport,
63 									  sal_uInt16 nPrfx,
64 									  const ::rtl::OUString& rLName,
65 									  const ::com::sun::star::uno::Reference<
66 									  ::com::sun::star::xml::sax::XAttributeList>& xAttrList ) :
67 	SvXMLImportContext( rImport, nPrfx, rLName ),
68 	sVisibility(GetXMLToken(XML_VISIBLE)),
69 	nRepeatedRows(1),
70     bHasCell(sal_False)
71 {
72 	rtl::OUString sCellStyleName;
73 	sal_Int16 nAttrCount(xAttrList.is() ? xAttrList->getLength() : 0);
74 	const SvXMLTokenMap& rAttrTokenMap(GetScImport().GetTableRowAttrTokenMap());
75 	for( sal_Int16 i=0; i < nAttrCount; ++i )
76 	{
77 		const rtl::OUString& sAttrName(xAttrList->getNameByIndex( i ));
78 		rtl::OUString aLocalName;
79 		sal_uInt16 nPrefix(GetScImport().GetNamespaceMap().GetKeyByAttrName(
80 											sAttrName, &aLocalName ));
81 		const rtl::OUString& sValue(xAttrList->getValueByIndex( i ));
82 
83 		switch( rAttrTokenMap.Get( nPrefix, aLocalName ) )
84 		{
85 			case XML_TOK_TABLE_ROW_ATTR_STYLE_NAME:
86 			{
87 				sStyleName = sValue;
88 			}
89 			break;
90 			case XML_TOK_TABLE_ROW_ATTR_VISIBILITY:
91 			{
92 				sVisibility = sValue;
93 			}
94 			break;
95 			case XML_TOK_TABLE_ROW_ATTR_REPEATED:
96 			{
97                 nRepeatedRows = std::max( sValue.toInt32(), (sal_Int32) 1 );
98 			}
99 			break;
100 			case XML_TOK_TABLE_ROW_ATTR_DEFAULT_CELL_STYLE_NAME:
101 			{
102 				sCellStyleName = sValue;
103 			}
104 			break;
105 			/*case XML_TOK_TABLE_ROW_ATTR_USE_OPTIMAL_HEIGHT:
106 			{
107 				sOptimalHeight = sValue;
108 			}
109 			break;*/
110 		}
111 	}
112 	GetScImport().GetTables().AddRow();
113 	GetScImport().GetTables().SetRowStyle(sCellStyleName);
114 }
115 
~ScXMLTableRowContext()116 ScXMLTableRowContext::~ScXMLTableRowContext()
117 {
118 }
119 
CreateChildContext(sal_uInt16 nPrefix,const::rtl::OUString & rLName,const::com::sun::star::uno::Reference<::com::sun::star::xml::sax::XAttributeList> & xAttrList)120 SvXMLImportContext *ScXMLTableRowContext::CreateChildContext( sal_uInt16 nPrefix,
121 											const ::rtl::OUString& rLName,
122 											const ::com::sun::star::uno::Reference<
123 									  	::com::sun::star::xml::sax::XAttributeList>& xAttrList )
124 {
125 	SvXMLImportContext *pContext(0);
126 
127 	const SvXMLTokenMap& rTokenMap(GetScImport().GetTableRowElemTokenMap());
128 	switch( rTokenMap.Get( nPrefix, rLName ) )
129 	{
130 	case XML_TOK_TABLE_ROW_CELL:
131 //		if( IsInsertCellPossible() )
132         {
133             bHasCell = sal_True;
134 			pContext = new ScXMLTableRowCellContext( GetScImport(), nPrefix,
135 													  rLName, xAttrList, sal_False, nRepeatedRows
136 													  //this
137 													  );
138         }
139 		break;
140 	case XML_TOK_TABLE_ROW_COVERED_CELL:
141 //		if( IsInsertCellPossible() )
142         {
143             bHasCell = sal_True;
144 			pContext = new ScXMLTableRowCellContext( GetScImport(), nPrefix,
145 													  rLName, xAttrList, sal_True, nRepeatedRows
146 													  //this
147 													  );
148         }
149 		break;
150 	}
151 
152 	if( !pContext )
153 		pContext = new SvXMLImportContext( GetImport(), nPrefix, rLName );
154 
155 	return pContext;
156 }
157 
EndElement()158 void ScXMLTableRowContext::EndElement()
159 {
160 	ScXMLImport& rXMLImport(GetScImport());
161     if (!bHasCell && nRepeatedRows > 1)
162     {
163         for (sal_Int32 i = 0; i < nRepeatedRows - 1; ++i) //one row is always added
164     	    GetScImport().GetTables().AddRow();
165         DBG_ERRORFILE("it seems here is a nonvalid file; possible missing of table:table-cell element");
166     }
167     sal_Int32 nSheet = rXMLImport.GetTables().GetCurrentSheet();
168 	sal_Int32 nCurrentRow(rXMLImport.GetTables().GetCurrentRow());
169 	uno::Reference<sheet::XSpreadsheet> xSheet(rXMLImport.GetTables().GetCurrentXSheet());
170     ScDocument* pDoc = rXMLImport.GetDocument();
171 	if(xSheet.is())
172 	{
173 		sal_Int32 nFirstRow(nCurrentRow - nRepeatedRows + 1);
174 		if (nFirstRow > MAXROW)
175 			nFirstRow = MAXROW;
176 		if (nCurrentRow > MAXROW)
177 			nCurrentRow = MAXROW;
178 		uno::Reference <table::XCellRange> xCellRange(xSheet->getCellRangeByPosition(0, nFirstRow, 0, nCurrentRow));
179 		if (xCellRange.is())
180 		{
181 			uno::Reference<table::XColumnRowRange> xColumnRowRange (xCellRange, uno::UNO_QUERY);
182 			if (xColumnRowRange.is())
183 			{
184 				uno::Reference <beans::XPropertySet> xRowProperties(xColumnRowRange->getRows(), uno::UNO_QUERY);
185 				if (xRowProperties.is())
186 				{
187 					if (sStyleName.getLength())
188 					{
189 						XMLTableStylesContext *pStyles((XMLTableStylesContext *)rXMLImport.GetAutoStyles());
190                         if ( pStyles )
191                         {
192                             XMLTableStyleContext* pStyle((XMLTableStyleContext *)pStyles->FindStyleChildContext(
193                                 XML_STYLE_FAMILY_TABLE_ROW, sStyleName, sal_True));
194                             if (pStyle)
195                             {
196                                 pStyle->FillPropertySet(xRowProperties);
197 
198                                 if ( nSheet != pStyle->GetLastSheet() )
199                                 {
200                                     ScSheetSaveData* pSheetData = ScModelObj::getImplementation(rXMLImport.GetModel())->GetSheetSaveData();
201                                     pSheetData->AddRowStyle( sStyleName, ScAddress( 0, (SCROW)nFirstRow, (SCTAB)nSheet ) );
202                                     pStyle->SetLastSheet(nSheet);
203                                 }
204                             }
205                         }
206 					}
207 					sal_Bool bVisible (sal_True);
208 					sal_Bool bFiltered (sal_False);
209 					if (IsXMLToken(sVisibility, XML_COLLAPSE))
210 					{
211 						bVisible = sal_False;
212 					}
213 					else if (IsXMLToken(sVisibility, XML_FILTER))
214 					{
215 						bVisible = sal_False;
216 						bFiltered = sal_True;
217 					}
218 
219                     // #i116164# call SetRowHidden/SetRowFiltered directly, so the tree doesn't have to be rebuilt
220                     // to compare with existing hidden flags.
221                     if (!bVisible && pDoc)
222                         pDoc->SetRowHidden((SCROW)nFirstRow, (SCROW)nCurrentRow, (SCTAB)nSheet, true);
223                     if (bFiltered && pDoc)
224                         pDoc->SetRowFiltered((SCROW)nFirstRow, (SCROW)nCurrentRow, (SCTAB)nSheet, true);
225 
226                     //if (!bVisible)
227                     //    xRowProperties->setPropertyValue(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(SC_ISVISIBLE)), uno::makeAny(bVisible));
228                     //if (bFiltered)
229                     //    xRowProperties->setPropertyValue(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(SC_ISFILTERED)), uno::makeAny(bFiltered));
230 				}
231 			}
232 		}
233 	}
234 }
235 
ScXMLTableRowsContext(ScXMLImport & rImport,sal_uInt16 nPrfx,const::rtl::OUString & rLName,const::com::sun::star::uno::Reference<::com::sun::star::xml::sax::XAttributeList> & xAttrList,const sal_Bool bTempHeader,const sal_Bool bTempGroup)236 ScXMLTableRowsContext::ScXMLTableRowsContext( ScXMLImport& rImport,
237 									  sal_uInt16 nPrfx,
238 									  const ::rtl::OUString& rLName,
239 									  const ::com::sun::star::uno::Reference<
240 									  ::com::sun::star::xml::sax::XAttributeList>& xAttrList,
241 									  const sal_Bool bTempHeader, const sal_Bool bTempGroup ) :
242 	SvXMLImportContext( rImport, nPrfx, rLName ),
243 	nHeaderStartRow(0),
244 	nHeaderEndRow(0),
245 	nGroupStartRow(0),
246 	nGroupEndRow(0),
247 	bHeader(bTempHeader),
248 	bGroup(bTempGroup),
249 	bGroupDisplay(sal_True)
250 {
251 	// don't have any attributes
252 	if (bHeader)
253 	{
254 		nHeaderStartRow = rImport.GetTables().GetCurrentRow();
255 		++nHeaderStartRow;
256 	}
257 	else if (bGroup)
258 	{
259 		nGroupStartRow = rImport.GetTables().GetCurrentRow();
260 		++nGroupStartRow;
261 		sal_Int16 nAttrCount = xAttrList.is() ? xAttrList->getLength() : 0;
262 		for( sal_Int16 i=0; i < nAttrCount; ++i )
263 		{
264 			const rtl::OUString& sAttrName(xAttrList->getNameByIndex( i ));
265 			rtl::OUString aLocalName;
266 			sal_uInt16 nPrefix(GetScImport().GetNamespaceMap().GetKeyByAttrName(
267 												sAttrName, &aLocalName ));
268 			const rtl::OUString& sValue(xAttrList->getValueByIndex( i ));
269 
270             if ((nPrefix == XML_NAMESPACE_TABLE) && IsXMLToken(aLocalName, XML_DISPLAY))
271 				bGroupDisplay = IsXMLToken(sValue, XML_TRUE);
272 		}
273 	}
274 }
275 
~ScXMLTableRowsContext()276 ScXMLTableRowsContext::~ScXMLTableRowsContext()
277 {
278 }
279 
CreateChildContext(sal_uInt16 nPrefix,const::rtl::OUString & rLName,const::com::sun::star::uno::Reference<::com::sun::star::xml::sax::XAttributeList> & xAttrList)280 SvXMLImportContext *ScXMLTableRowsContext::CreateChildContext( sal_uInt16 nPrefix,
281 											const ::rtl::OUString& rLName,
282 											const ::com::sun::star::uno::Reference<
283 									  	::com::sun::star::xml::sax::XAttributeList>& xAttrList )
284 {
285 	SvXMLImportContext *pContext(0);
286 
287 	const SvXMLTokenMap& rTokenMap(GetScImport().GetTableRowsElemTokenMap());
288 	switch( rTokenMap.Get( nPrefix, rLName ) )
289 	{
290 	case XML_TOK_TABLE_ROWS_ROW_GROUP:
291 		pContext = new ScXMLTableRowsContext( GetScImport(), nPrefix,
292 												   rLName, xAttrList,
293 												   sal_False, sal_True );
294 		break;
295 	case XML_TOK_TABLE_ROWS_HEADER_ROWS:
296 		pContext = new ScXMLTableRowsContext( GetScImport(), nPrefix,
297 												   rLName, xAttrList,
298 												   sal_True, sal_False );
299 		break;
300 	case XML_TOK_TABLE_ROWS_ROWS:
301 		pContext = new ScXMLTableRowsContext( GetScImport(), nPrefix,
302 												   rLName, xAttrList,
303 												   sal_False, sal_False );
304 		break;
305 	case XML_TOK_TABLE_ROWS_ROW:
306 			pContext = new ScXMLTableRowContext( GetScImport(), nPrefix,
307 													  rLName, xAttrList//,
308 													  //this
309 													  );
310 		break;
311 	}
312 
313 	if( !pContext )
314 		pContext = new SvXMLImportContext( GetImport(), nPrefix, rLName );
315 
316 	return pContext;
317 }
318 
EndElement()319 void ScXMLTableRowsContext::EndElement()
320 {
321 	ScXMLImport& rXMLImport(GetScImport());
322 	if (bHeader)
323 	{
324 		nHeaderEndRow = rXMLImport.GetTables().GetCurrentRow();
325 		if (nHeaderStartRow <= nHeaderEndRow)
326 		{
327 			uno::Reference <sheet::XPrintAreas> xPrintAreas (rXMLImport.GetTables().GetCurrentXSheet(), uno::UNO_QUERY);
328 			if (xPrintAreas.is())
329 			{
330 				if (!xPrintAreas->getPrintTitleRows())
331 				{
332 					xPrintAreas->setPrintTitleRows(sal_True);
333 					table::CellRangeAddress aRowHeaderRange;
334 					aRowHeaderRange.StartRow = nHeaderStartRow;
335 					aRowHeaderRange.EndRow = nHeaderEndRow;
336 					xPrintAreas->setTitleRows(aRowHeaderRange);
337 				}
338 				else
339 				{
340 					table::CellRangeAddress aRowHeaderRange(xPrintAreas->getTitleRows());
341 					aRowHeaderRange.EndRow = nHeaderEndRow;
342 					xPrintAreas->setTitleRows(aRowHeaderRange);
343 				}
344 			}
345 		}
346 	}
347 	else if (bGroup)
348 	{
349 		nGroupEndRow = rXMLImport.GetTables().GetCurrentRow();
350 		sal_Int32 nSheet(rXMLImport.GetTables().GetCurrentSheet());
351 		if (nGroupStartRow <= nGroupEndRow)
352 		{
353 			ScDocument* pDoc(GetScImport().GetDocument());
354 			if (pDoc)
355 			{
356 				GetScImport().LockSolarMutex();
357 				ScOutlineTable* pOutlineTable(pDoc->GetOutlineTable(static_cast<SCTAB>(nSheet), sal_True));
358 				ScOutlineArray* pRowArray(pOutlineTable->GetRowArray());
359 				sal_Bool bResized;
360 				pRowArray->Insert(static_cast<SCROW>(nGroupStartRow), static_cast<SCROW>(nGroupEndRow), bResized, !bGroupDisplay, sal_True);
361 				GetScImport().UnlockSolarMutex();
362 			}
363 		}
364 	}
365 }
366